1 /*
2 * Copyright (c) 2020-2022 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 #if ENABLE_ICU
17 #include "common/typed_text.h"
18 #include "draw/draw_utils.h"
19 #include "font/ui_font.h"
20 #include "font/ui_line_break.h"
21 #include "font/icu_umutex_stub.h"
22 #include "rbbidata.h"
23 #include "ucmndata.h"
24 #include "unicode/ucptrie.h"
25
26 using namespace U_ICU_NAMESPACE;
27 namespace OHOS {
MemAlloc(const void * context,size_t size)28 static void* MemAlloc(const void* context, size_t size)
29 {
30 return UIMalloc(size);
31 }
32
MemFree(const void * context,void * mem)33 static void MemFree(const void* context, void* mem)
34 {
35 if (mem == nullptr) {
36 return;
37 }
38 UIFree(mem);
39 }
40
MemRealloc(const void * context,void * mem,size_t size)41 static void* MemRealloc(const void* context, void* mem, size_t size)
42 {
43 return UIRealloc(mem, size);
44 }
45
GetInstance()46 UILineBreakEngine& UILineBreakEngine::GetInstance()
47 {
48 static UILineBreakEngine instance;
49 return instance;
50 }
51
GetNextBreakPos(UILineBreakProxy & record)52 uint16_t UILineBreakEngine::GetNextBreakPos(UILineBreakProxy& record)
53 {
54 const uint32_t* str = record.GetStr();
55 if ((str == nullptr) || !initSuccess_ || (lineBreakTrie_ == nullptr)) {
56 return 0;
57 }
58 int32_t state = LINE_BREAK_STATE_START;
59 const RBBIStateTable* rbbStateTable = reinterpret_cast<const RBBIStateTable*>(stateTbl_);
60 const RBBIStateTableRow8* row =
61 reinterpret_cast<const RBBIStateTableRow8*>(rbbStateTable->fTableData + rbbStateTable->fRowLen * state);
62 UCPTrie* trie = reinterpret_cast<UCPTrie*>(lineBreakTrie_);
63 for (uint16_t index = 0; index < record.GetStrLen(); ++index) {
64 uint16_t category = UCPTRIE_FAST_GET(trie, UCPTRIE_8, str[index]);
65 // 0x4000: remove the dictionary flag bit
66 if ((category & 0x4000) != 0) {
67 // 0x4000: remove the dictionary flag bit
68 category &= ~0x4000;
69 }
70 state = row->fNextState[category];
71 row = reinterpret_cast<const RBBIStateTableRow8*>(rbbStateTable->fTableData + rbbStateTable->fRowLen * state);
72 int16_t completedRule = row->fAccepting;
73 if ((completedRule > 1) || (state == LINE_BREAK_STATE_STOP)) {
74 return index;
75 }
76 }
77 return record.GetStrLen();
78 }
79
LoadRule()80 void UILineBreakEngine::LoadRule()
81 {
82 if ((fp_ < 0) || (addr_ == nullptr)) {
83 return;
84 }
85 UErrorCode status = U_ZERO_ERROR;
86 u_setMemoryFunctions(nullptr, MemAlloc, MemRealloc, MemFree, &status);
87 if (status != U_ZERO_ERROR) {
88 return;
89 }
90 int32_t ret = lseek(fp_, offset_, SEEK_SET);
91 if (ret != offset_) {
92 return;
93 }
94 char* buf = addr_;
95 ret = read(fp_, buf, size_);
96 if (ret != size_) {
97 return;
98 }
99 const char* dataInBytes = reinterpret_cast<const char*>(buf);
100 const DataHeader* dh = reinterpret_cast<const DataHeader*>(buf);
101 const RBBIDataHeader* rbbidh = reinterpret_cast<const RBBIDataHeader*>(dataInBytes + dh->dataHeader.headerSize);
102 stateTbl_ = reinterpret_cast<const RBBIStateTable*>(reinterpret_cast<const char*>(rbbidh) + rbbidh->fFTable);
103 status = U_ZERO_ERROR;
104 lineBreakTrie_ = reinterpret_cast<UCPTrie*>(ucptrie_openFromBinary(UCPTRIE_TYPE_FAST, UCPTRIE_VALUE_BITS_8,
105 reinterpret_cast<const uint8_t*>(rbbidh)
106 + rbbidh->fTrie,
107 rbbidh->fTrieLen, nullptr, &status));
108 if (status != U_ZERO_ERROR) {
109 return;
110 }
111 initSuccess_ = true;
112 }
113
GetNextLineAndWidth(const char * text,uint16_t fontId,uint8_t fontSize,int16_t space,bool allBreak,int16_t & maxWidth,int16_t & maxHeight,uint16_t & letterIndex,SizeSpan * sizeSpans,uint16_t len)114 uint32_t UILineBreakEngine::GetNextLineAndWidth(const char* text,
115 uint16_t fontId,
116 uint8_t fontSize,
117 int16_t space,
118 bool allBreak,
119 int16_t& maxWidth,
120 int16_t& maxHeight,
121 uint16_t& letterIndex,
122 SizeSpan* sizeSpans,
123 uint16_t len)
124 {
125 if (text == nullptr) {
126 return 0;
127 }
128 bool isAllCanBreak = allBreak;
129 uint32_t byteIdx = 0;
130 uint32_t preIndex = 0;
131 int16_t lastWidth = 0;
132 int16_t lastIndex = 0;
133 int16_t curWidth = 0;
134 int32_t state = LINE_BREAK_STATE_START;
135 int16_t width = 0;
136 int16_t height = 0;
137 while ((byteIdx < len) && (text[byteIdx] != '\0')) {
138 uint32_t unicode = TypedText::GetUTF8Next(text, preIndex, byteIdx);
139 if (unicode == 0) {
140 preIndex = byteIdx;
141 continue;
142 }
143 if (isAllCanBreak || IsBreakPos(unicode, fontId, fontSize, state)) {
144 state = LINE_BREAK_STATE_START;
145 // Accumulates the status value from the current character.
146 IsBreakPos(unicode, fontId, fontSize, state);
147 lastIndex = preIndex;
148 lastWidth = curWidth;
149 }
150 width = GetLetterWidth(unicode, letterIndex, height, fontId, fontSize, sizeSpans);
151 letterIndex++;
152 if (height > maxHeight) {
153 maxHeight = height;
154 }
155 int16_t nextWidth = (curWidth > 0 && width > 0) ? (curWidth + space + width) : (curWidth + width);
156 if (nextWidth > maxWidth) {
157 letterIndex--;
158 if (lastIndex == 0) {
159 break;
160 }
161 maxWidth = lastWidth;
162 return lastIndex;
163 }
164 curWidth = nextWidth;
165 preIndex = byteIdx;
166 if (byteIdx > 0 && ((text[byteIdx - 1] == '\r') || (text[byteIdx - 1] == '\n'))) {
167 break;
168 }
169 }
170 maxWidth = curWidth;
171 return preIndex;
172 }
173
GetLetterWidth(uint32_t unicode,uint16_t & letterIndex,int16_t & height,uint16_t fontId,uint8_t fontSize,SizeSpan * sizeSpans)174 int16_t UILineBreakEngine::GetLetterWidth(uint32_t unicode, uint16_t& letterIndex, int16_t& height,
175 uint16_t fontId, uint8_t fontSize, SizeSpan* sizeSpans)
176 {
177 if (sizeSpans != nullptr && sizeSpans[letterIndex].isSizeSpan) {
178 int16_t width = UIFont::GetInstance()->GetWidth(unicode, sizeSpans[letterIndex].fontId,
179 sizeSpans[letterIndex].size, 0);
180
181 if (sizeSpans[letterIndex].height == 0) {
182 height = UIFont::GetInstance()->GetHeight(sizeSpans[letterIndex].fontId,
183 sizeSpans[letterIndex].size);
184 sizeSpans[letterIndex].height = height;
185 } else {
186 height = sizeSpans[letterIndex].height;
187 }
188 return width;
189 } else {
190 height = UIFont::GetInstance()->GetHeight(fontId, fontSize);
191 return UIFont::GetInstance()->GetWidth(unicode, fontId, fontSize, 0);
192 }
193 }
194
IsBreakPos(uint32_t unicode,uint16_t fontId,uint8_t fontSize,int32_t & state)195 bool UILineBreakEngine::IsBreakPos(uint32_t unicode, uint16_t fontId, uint8_t fontSize, int32_t& state)
196 {
197 if (TypedText::IsEmoji(unicode)) {
198 return true;
199 }
200 if ((unicode > TypedText::MAX_UINT16_HIGH_SCOPE) || (stateTbl_ == nullptr) || (lineBreakTrie_ == nullptr)) {
201 return true;
202 }
203 const RBBIStateTable* rbbStateTable = reinterpret_cast<const RBBIStateTable*>(stateTbl_);
204 const RBBIStateTableRow8* row =
205 reinterpret_cast<const RBBIStateTableRow8*>(rbbStateTable->fTableData + rbbStateTable->fRowLen * state);
206 uint16_t utf16 = 0;
207 if (unicode <= TypedText::MAX_UINT16_LOW_SCOPE) {
208 utf16 = (unicode & TypedText::MAX_UINT16_LOW_SCOPE);
209 } else if (unicode <= TypedText::MAX_UINT16_HIGH_SCOPE) {
210 utf16 = static_cast<uint16_t>(TypedText::UTF16_LOW_PARAM + (unicode & TypedText::UTF16_LOW_MASK)); // low
211 uint16_t category = UCPTRIE_FAST_GET(reinterpret_cast<UCPTrie*>(lineBreakTrie_), UCPTRIE_8, utf16);
212 // 0x4000: remove the dictionary flag bit
213 if ((category & 0x4000) != 0) {
214 // 0x4000: remove the dictionary flag bit
215 category &= ~0x4000;
216 }
217 state = row->fNextState[category];
218 row = reinterpret_cast<const RBBIStateTableRow8*>(rbbStateTable->fTableData + rbbStateTable->fRowLen * state);
219 utf16 = static_cast<uint16_t>(TypedText::UTF16_HIGH_PARAM1 + (unicode >> TypedText::UTF16_HIGH_SHIFT) -
220 TypedText::UTF16_HIGH_PARAM2); // high
221 }
222 uint16_t category = UCPTRIE_FAST_GET(reinterpret_cast<UCPTrie*>(lineBreakTrie_), UCPTRIE_8, utf16);
223 // 0x4000: remove the dictionary flag bit
224 if ((category & 0x4000) != 0) {
225 // 0x4000: remove the dictionary flag bit
226 category &= ~0x4000;
227 }
228 state = row->fNextState[category];
229 row = reinterpret_cast<const RBBIStateTableRow8*>(rbbStateTable->fTableData + rbbStateTable->fRowLen * state);
230 return (row->fAccepting > 1 || state == LINE_BREAK_STATE_STOP);
231 }
232 } // namespace OHOS
233 #endif // ENABLE_ICU
234