• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef LOGGER_ENABLE_SCOPE
23 #include "texgine/utils/trace.h"
24 #endif
25 #include "text_span.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace TextEngine {
operator <<(std::ostream & os,const MergeResult & result)30 std::ostream &operator <<(std::ostream &os, const MergeResult &result)
31 {
32     switch (result) {
33         case MergeResult::ACCEPTED:
34             os << "accepted";
35             break;
36         case MergeResult::BREAKED:
37             os << "breaked";
38             break;
39         case MergeResult::REJECTED:
40             os << "rejected";
41             break;
42         case MergeResult::IGNORE:
43             os << "ignore";
44             break;
45         default:
46             break;
47     }
48     return os;
49 }
50 
MergeSpans(const std::vector<VariantSpan> & spans)51 std::vector<VariantSpan> TextMerger::MergeSpans(const std::vector<VariantSpan> &spans)
52 {
53 #ifdef LOGGER_ENABLE_SCOPE
54     ScopedTrace scope("TextMerger::MergeSpans");
55 #endif
56     std::vector<VariantSpan> vss;
57     auto it = spans.begin();
58     while (it != spans.end()) {
59         CharGroups cgs;
60         std::optional<bool> currentRTL = std::nullopt;
61         for (; it != spans.end(); it++) {
62             LOGSCOPED(sl, LOGEX_FUNC_LINE_DEBUG(), "MergeSpan");
63             auto result = MergeSpan(*it, currentRTL, cgs);
64             it->Dump(DumpType::DONT_RETURN);
65             LOGCEX_DEBUG() << result;
66             if (result == MergeResult::BREAKED) {
67                 it++;
68                 break;
69             } else if (result == MergeResult::REJECTED) {
70                 break;
71             } else if (result == MergeResult::IGNORE) {
72                 vss.push_back(*it);
73             }
74         }
75 
76         if (it == spans.begin()) {
77             return {};
78         }
79 
80         auto &lastSpan = *(it - 1);
81         if (auto ts = lastSpan.TryToTextSpan(); ts != nullptr) {
82             lastSpan.Dump(DumpType::DONT_RETURN);
83             LOGCEX_DEBUG() << "generated";
84 
85             auto mergedSpan = ts->CloneWithCharGroups(cgs);
86             VariantSpan vs(mergedSpan);
87             vs.SetTextStyle(lastSpan.GetTextStyle());
88             vss.push_back(vs);
89         }
90     }
91     return vss;
92 }
93 
MergeSpan(const VariantSpan & span,std::optional<bool> & currentRTL,CharGroups & cgs)94 MergeResult TextMerger::MergeSpan(const VariantSpan &span, std::optional<bool> &currentRTL, CharGroups &cgs)
95 {
96     if (span == nullptr) {
97         throw TEXGINE_EXCEPTION(INVALID_ARGUMENT);
98     }
99 
100     auto ts = span.TryToTextSpan();
101     if (ts == nullptr && cgs.IsValid()) {
102         return MergeResult::REJECTED;
103     }
104 
105     if (ts == nullptr && !cgs.IsValid()) {
106         return MergeResult::IGNORE;
107     }
108 
109     auto rtl = ts->IsRTL();
110     if (rtl != currentRTL.value_or(rtl)) {
111         return MergeResult::REJECTED;
112     }
113 
114     currentRTL = rtl;
115     if (!ts->cgs_.IsValid() || ts->cgs_.GetSize() == 0) {
116         throw TEXGINE_EXCEPTION(ERROR_STATUS);
117     }
118 
119     if (cgs.IsValid() && !cgs.IsSameCharGroups(ts->cgs_)) {
120         return MergeResult::REJECTED;
121     }
122 
123     if (!cgs.IsValid()) {
124         cgs = ts->cgs_;
125     } else if (cgs.Get(0).typeface != ts->typeface_) {
126         return MergeResult::REJECTED;
127     } else if (cgs.GetBack().isWordEnd) {
128         return MergeResult::REJECTED;
129     } else {
130         cgs.Merge(ts->cgs_);
131     }
132 
133     bool isWhitespace = (u_isWhitespace(ts->cgs_.GetBack().chars[0]) == 1);
134     if (isWhitespace) {
135         return MergeResult::BREAKED;
136     } else {
137         return MergeResult::ACCEPTED;
138     }
139 }
140 } // namespace TextEngine
141 } // namespace Rosen
142 } // namespace OHOS
143