1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "text_merger.h"
17
18 #include <unicode/ubidi.h>
19
20 #include "texgine_exception.h"
21 #include "texgine/utils/exlog.h"
22 #include "texgine/utils/trace.h"
23 #include "text_span.h"
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace TextEngine {
operator <<(std::ostream & os,const MergeResult & result)28 std::ostream &operator <<(std::ostream &os, const MergeResult &result)
29 {
30 switch (result) {
31 case MergeResult::ACCEPTED:
32 os << "accepted";
33 break;
34 case MergeResult::BREAKED:
35 os << "breaked";
36 break;
37 case MergeResult::REJECTED:
38 os << "rejected";
39 break;
40 case MergeResult::IGNORE:
41 os << "ignore";
42 break;
43 default:
44 break;
45 }
46 return os;
47 }
48
MergeSpans(const std::vector<VariantSpan> & spans)49 std::vector<VariantSpan> TextMerger::MergeSpans(const std::vector<VariantSpan> &spans)
50 {
51 ScopedTrace scope("TextMerger::MergeSpans");
52 std::vector<VariantSpan> vss;
53 auto it = spans.begin();
54 while (it != spans.end()) {
55 CharGroups cgs;
56 std::optional<bool> currentRTL = std::nullopt;
57 for (; it != spans.end(); it++) {
58 LOGSCOPED(sl, LOGEX_FUNC_LINE_DEBUG(), "MergeSpan");
59 auto result = MergeSpan(*it, currentRTL, cgs);
60 it->Dump(DumpType::DONT_RETURN);
61 LOGCEX_DEBUG() << result;
62 if (result == MergeResult::BREAKED) {
63 it++;
64 break;
65 } else if (result == MergeResult::REJECTED) {
66 break;
67 } else if (result == MergeResult::IGNORE) {
68 vss.push_back(*it);
69 }
70 }
71
72 if (it == spans.begin()) {
73 throw TEXGINE_EXCEPTION(ERROR_STATUS);
74 }
75
76 auto &lastSpan = *(it - 1);
77 if (auto ts = lastSpan.TryToTextSpan(); ts != nullptr) {
78 lastSpan.Dump(DumpType::DONT_RETURN);
79 LOGCEX_DEBUG() << "generated";
80
81 auto mergedSpan = ts->CloneWithCharGroups(cgs);
82 VariantSpan vs(mergedSpan);
83 vs.SetTextStyle(lastSpan.GetTextStyle());
84 vss.push_back(vs);
85 }
86 }
87 return vss;
88 }
89
MergeSpan(const VariantSpan & span,std::optional<bool> & currentRTL,CharGroups & cgs)90 MergeResult TextMerger::MergeSpan(const VariantSpan &span, std::optional<bool> ¤tRTL, CharGroups &cgs)
91 {
92 if (span == nullptr) {
93 throw TEXGINE_EXCEPTION(INVALID_ARGUMENT);
94 }
95
96 auto ts = span.TryToTextSpan();
97 if (ts == nullptr && cgs.IsValid()) {
98 return MergeResult::REJECTED;
99 }
100
101 if (ts == nullptr && !cgs.IsValid()) {
102 return MergeResult::IGNORE;
103 }
104
105 auto rtl = ts->IsRTL();
106 if (rtl != currentRTL.value_or(rtl)) {
107 return MergeResult::REJECTED;
108 }
109
110 currentRTL = rtl;
111 if (!ts->cgs_.IsValid() || ts->cgs_.GetSize() == 0) {
112 throw TEXGINE_EXCEPTION(ERROR_STATUS);
113 }
114
115 if (cgs.IsValid() && !cgs.IsSameCharGroups(ts->cgs_)) {
116 return MergeResult::REJECTED;
117 }
118
119 if (!cgs.IsValid()) {
120 cgs = ts->cgs_;
121 } else if (cgs.Get(0).typeface != ts->typeface_) {
122 return MergeResult::REJECTED;
123 } else {
124 cgs.Merge(ts->cgs_);
125 }
126
127 bool isWhitespace = (u_isWhitespace(ts->cgs_.GetBack().chars[0]) == 1);
128 if (isWhitespace) {
129 return MergeResult::BREAKED;
130 } else {
131 return MergeResult::ACCEPTED;
132 }
133 }
134 } // namespace TextEngine
135 } // namespace Rosen
136 } // namespace OHOS
137