blob: ee196f2113e4a6ac1ea5025f0018e7e71021cdce [file] [log] [blame]
Julia Lavrova90787fe2020-07-20 17:32:03 +00001/*
2* Copyright 2020 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
Julia Lavrovab6b7fff2020-09-11 13:59:49 +00007#include "include/core/SkString.h"
Jason Simmonsd8e94362021-01-11 15:52:03 -08008#include "include/private/SkMutex.h"
John Stiles154bd1f2021-05-13 09:38:51 -04009#include "include/private/SkOnce.h"
Julia Lavrova90787fe2020-07-20 17:32:03 +000010#include "include/private/SkTFitsIn.h"
Jason Simmonsd8e94362021-01-11 15:52:03 -080011#include "include/private/SkTHash.h"
Julia Lavrova90787fe2020-07-20 17:32:03 +000012#include "include/private/SkTemplates.h"
Florin Malita40f3db42021-07-27 14:02:28 -040013#include "modules/skunicode/include/SkUnicode.h"
14#include "modules/skunicode/src/SkUnicode_icu.h"
Julia Lavrova90787fe2020-07-20 17:32:03 +000015#include "src/utils/SkUTF.h"
Julia Lavrova90787fe2020-07-20 17:32:03 +000016#include <vector>
17#include <functional>
18
Julia Lavrovab6b7fff2020-09-11 13:59:49 +000019#if defined(SK_USING_THIRD_PARTY_ICU)
20#include "SkLoadICU.h"
21#endif
22
Florin Malita40f3db42021-07-27 14:02:28 -040023static const SkICULib* ICULib() {
24 static const auto gICU = SkLoadICULib();
Julia Lavrova4414f872021-03-03 10:19:41 -050025
Florin Malita40f3db42021-07-27 14:02:28 -040026 return gICU.get();
27}
Julia Lavrova90787fe2020-07-20 17:32:03 +000028
Florin Malita40f3db42021-07-27 14:02:28 -040029// sk_* wrappers for ICU funcs
30#define SKICU_FUNC(funcname) \
31 template <typename... Args> \
32 auto sk_##funcname(Args&&... args) -> decltype(funcname(std::forward<Args>(args)...)) { \
33 return ICULib()->f_##funcname(std::forward<Args>(args)...); \
34 } \
35
36SKICU_EMIT_FUNCS
37#undef SKICU_FUNC
38
39static inline UBreakIterator* sk_ubrk_clone(const UBreakIterator* bi, UErrorCode* status) {
40 const auto* icu = ICULib();
41 SkASSERT(icu->f_ubrk_clone_ || icu->f_ubrk_safeClone_);
42 return icu->f_ubrk_clone_
43 ? icu->f_ubrk_clone_(bi, status)
44 : icu->f_ubrk_safeClone_(bi, nullptr, nullptr, status);
45}
46
47static void ubidi_close_wrapper(UBiDi* bidi) {
48 sk_ubidi_close(bidi);
49}
50static UText* utext_close_wrapper(UText* ut) {
51 return sk_utext_close(ut);
52}
53static void ubrk_close_wrapper(UBreakIterator* bi) {
54 sk_ubrk_close(bi);
55}
56
57using SkUnicodeBidi = std::unique_ptr<UBiDi, SkFunctionWrapper<decltype(ubidi_close),
58 ubidi_close_wrapper>>;
59using ICUUText = std::unique_ptr<UText, SkFunctionWrapper<decltype(utext_close),
60 utext_close_wrapper>>;
61using ICUBreakIterator = std::unique_ptr<UBreakIterator, SkFunctionWrapper<decltype(ubrk_close),
62 ubrk_close_wrapper>>;
Julia Lavrova90787fe2020-07-20 17:32:03 +000063/** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */
64static inline SkUnichar utf8_next(const char** ptr, const char* end) {
65 SkUnichar val = SkUTF::NextUTF8(ptr, end);
66 return val < 0 ? 0xFFFD : val;
67}
68
Jason Simmonsd8e94362021-01-11 15:52:03 -080069static UBreakIteratorType convertType(SkUnicode::BreakType type) {
70 switch (type) {
71 case SkUnicode::BreakType::kLines: return UBRK_LINE;
72 case SkUnicode::BreakType::kGraphemes: return UBRK_CHARACTER;
73 case SkUnicode::BreakType::kWords: return UBRK_WORD;
74 default:
75 return UBRK_CHARACTER;
76 }
77}
78
Julia Lavrova1798f4f2020-08-26 14:22:48 +000079class SkBidiIterator_icu : public SkBidiIterator {
80 SkUnicodeBidi fBidi;
81public:
82 explicit SkBidiIterator_icu(SkUnicodeBidi bidi) : fBidi(std::move(bidi)) {}
Florin Malita40f3db42021-07-27 14:02:28 -040083 Position getLength() override { return sk_ubidi_getLength(fBidi.get()); }
84 Level getLevelAt(Position pos) override { return sk_ubidi_getLevelAt(fBidi.get(), pos); }
Julia Lavrova1798f4f2020-08-26 14:22:48 +000085
86 static std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t utf16[], int utf16Units, Direction dir) {
87 UErrorCode status = U_ZERO_ERROR;
Florin Malita40f3db42021-07-27 14:02:28 -040088 SkUnicodeBidi bidi(sk_ubidi_openSized(utf16Units, 0, &status));
Julia Lavrova1798f4f2020-08-26 14:22:48 +000089 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -040090 SkDEBUGF("Bidi error: %s", sk_u_errorName(status));
Julia Lavrova1798f4f2020-08-26 14:22:48 +000091 return nullptr;
92 }
93 SkASSERT(bidi);
94 uint8_t bidiLevel = (dir == SkBidiIterator::kLTR) ? UBIDI_LTR : UBIDI_RTL;
95 // The required lifetime of utf16 isn't well documented.
96 // It appears it isn't used after ubidi_setPara except through ubidi_getText.
Florin Malita40f3db42021-07-27 14:02:28 -040097 sk_ubidi_setPara(bidi.get(), (const UChar*)utf16, utf16Units, bidiLevel, nullptr, &status);
Julia Lavrova1798f4f2020-08-26 14:22:48 +000098 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -040099 SkDEBUGF("Bidi error: %s", sk_u_errorName(status));
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000100 return nullptr;
101 }
102 return std::unique_ptr<SkBidiIterator>(new SkBidiIterator_icu(std::move(bidi)));
103 }
104
105 // ICU bidi iterator works with utf16 but clients (Flutter for instance) may work with utf8
106 // This method allows the clients not to think about all these details
107 static std::unique_ptr<SkBidiIterator> makeBidiIterator(const char utf8[], int utf8Units, Direction dir) {
108 // Convert utf8 into utf16 since ubidi only accepts utf16
109 if (!SkTFitsIn<int32_t>(utf8Units)) {
110 SkDEBUGF("Bidi error: text too long");
111 return nullptr;
112 }
113
114 // Getting the length like this seems to always set U_BUFFER_OVERFLOW_ERROR
115 int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units);
116 if (utf16Units < 0) {
117 SkDEBUGF("Bidi error: Invalid utf8 input");
118 return nullptr;
119 }
120 std::unique_ptr<uint16_t[]> utf16(new uint16_t[utf16Units]);
121 SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16.get(), utf16Units, utf8, utf8Units);
122 SkASSERT(dstLen == utf16Units);
123
124 return makeBidiIterator(utf16.get(), utf16Units, dir);
125 }
126
127 // This method returns the final results only: a list of bidi regions
128 // (this is all SkParagraph really needs; SkShaper however uses the iterator itself)
129 static std::vector<Region> getBidiRegions(const char utf8[], int utf8Units, Direction dir) {
130
131 auto bidiIterator = makeBidiIterator(utf8, utf8Units, dir);
132 std::vector<Region> bidiRegions;
133 const char* start8 = utf8;
134 const char* end8 = utf8 + utf8Units;
135 SkBidiIterator::Level currentLevel = 0;
136
137 Position pos8 = 0;
138 Position pos16 = 0;
139 Position end16 = bidiIterator->getLength();
140 while (pos16 < end16) {
141 auto level = bidiIterator->getLevelAt(pos16);
142 if (pos16 == 0) {
143 currentLevel = level;
144 } else if (level != currentLevel) {
Florin Malita40f3db42021-07-27 14:02:28 -0400145 auto end = SkTo<Position>(start8 - utf8);
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000146 bidiRegions.emplace_back(pos8, end, currentLevel);
147 currentLevel = level;
148 pos8 = end;
149 }
150 SkUnichar u = utf8_next(&start8, end8);
151 pos16 += SkUTF::ToUTF16(u);
152 }
153 auto end = start8 - utf8;
154 if (end != pos8) {
155 bidiRegions.emplace_back(pos8, end, currentLevel);
156 }
157 return bidiRegions;
158 }
159};
160
161void SkBidiIterator::ReorderVisual(const Level runLevels[], int levelsCount,
162 int32_t logicalFromVisual[]) {
Florin Malita40f3db42021-07-27 14:02:28 -0400163 sk_ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual);
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000164}
Julia Lavrova90787fe2020-07-20 17:32:03 +0000165
Julia Lavrova6e51d922020-08-25 11:42:51 -0400166class SkBreakIterator_icu : public SkBreakIterator {
167 ICUBreakIterator fBreakIterator;
168 Position fLastResult;
169 public:
170 explicit SkBreakIterator_icu(ICUBreakIterator iter)
171 : fBreakIterator(std::move(iter)), fLastResult(0) {}
172 Position first() override
Florin Malita40f3db42021-07-27 14:02:28 -0400173 { return fLastResult = sk_ubrk_first(fBreakIterator.get()); }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400174 Position current() override
Florin Malita40f3db42021-07-27 14:02:28 -0400175 { return fLastResult = sk_ubrk_current(fBreakIterator.get()); }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400176 Position next() override
Florin Malita40f3db42021-07-27 14:02:28 -0400177 { return fLastResult = sk_ubrk_next(fBreakIterator.get()); }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400178 Position preceding(Position offset) override
Florin Malita40f3db42021-07-27 14:02:28 -0400179 { return fLastResult = sk_ubrk_preceding(fBreakIterator.get(), offset); }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400180 Position following(Position offset) override
Florin Malita40f3db42021-07-27 14:02:28 -0400181 { return fLastResult = sk_ubrk_following(fBreakIterator.get(), offset);}
182 Status status() override { return sk_ubrk_getRuleStatus(fBreakIterator.get()); }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400183 bool isDone() override { return fLastResult == UBRK_DONE; }
184
185 bool setText(const char utftext8[], int utf8Units) override {
186 UErrorCode status = U_ZERO_ERROR;
Florin Malita40f3db42021-07-27 14:02:28 -0400187 ICUUText text(sk_utext_openUTF8(nullptr, &utftext8[0], utf8Units, &status));
Julia Lavrova6e51d922020-08-25 11:42:51 -0400188
189 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400190 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Julia Lavrova6e51d922020-08-25 11:42:51 -0400191 return false;
192 }
193 SkASSERT(text);
Florin Malita40f3db42021-07-27 14:02:28 -0400194 sk_ubrk_setUText(fBreakIterator.get(), text.get(), &status);
Julia Lavrova6e51d922020-08-25 11:42:51 -0400195 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400196 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Julia Lavrova6e51d922020-08-25 11:42:51 -0400197 return false;
198 }
199 fLastResult = 0;
200 return true;
201 }
Julia Lavrova10e7e772021-05-10 16:16:44 -0400202 bool setText(const char16_t utftext16[], int utf16Units) override {
203 UErrorCode status = U_ZERO_ERROR;
Florin Malita40f3db42021-07-27 14:02:28 -0400204 ICUUText text(sk_utext_openUChars(nullptr, reinterpret_cast<const UChar*>(&utftext16[0]),
205 utf16Units, &status));
Julia Lavrova10e7e772021-05-10 16:16:44 -0400206
207 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400208 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Julia Lavrova10e7e772021-05-10 16:16:44 -0400209 return false;
210 }
211 SkASSERT(text);
Florin Malita40f3db42021-07-27 14:02:28 -0400212 sk_ubrk_setUText(fBreakIterator.get(), text.get(), &status);
Julia Lavrova10e7e772021-05-10 16:16:44 -0400213 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400214 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Julia Lavrova10e7e772021-05-10 16:16:44 -0400215 return false;
216 }
217 fLastResult = 0;
218 return true;
219 }
Jason Simmonsd8e94362021-01-11 15:52:03 -0800220};
Julia Lavrova6e51d922020-08-25 11:42:51 -0400221
Jason Simmonsd8e94362021-01-11 15:52:03 -0800222class SkIcuBreakIteratorCache {
223 SkTHashMap<SkUnicode::BreakType, ICUBreakIterator> fBreakCache;
224 SkMutex fBreakCacheMutex;
225
226 public:
227 static SkIcuBreakIteratorCache& get() {
228 static SkIcuBreakIteratorCache instance;
229 return instance;
Julia Lavrova6e51d922020-08-25 11:42:51 -0400230 }
231
Jason Simmonsd8e94362021-01-11 15:52:03 -0800232 ICUBreakIterator makeBreakIterator(SkUnicode::BreakType type) {
Julia Lavrova6e51d922020-08-25 11:42:51 -0400233 UErrorCode status = U_ZERO_ERROR;
Jason Simmonsd8e94362021-01-11 15:52:03 -0800234 ICUBreakIterator* cachedIterator;
235 {
236 SkAutoMutexExclusive lock(fBreakCacheMutex);
237 cachedIterator = fBreakCache.find(type);
238 if (!cachedIterator) {
Florin Malita40f3db42021-07-27 14:02:28 -0400239 ICUBreakIterator newIterator(sk_ubrk_open(convertType(type), sk_uloc_getDefault(),
240 nullptr, 0, &status));
Jason Simmonsd8e94362021-01-11 15:52:03 -0800241 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400242 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Jason Simmonsd8e94362021-01-11 15:52:03 -0800243 } else {
244 cachedIterator = fBreakCache.set(type, std::move(newIterator));
245 }
246 }
247 }
248 ICUBreakIterator iterator;
249 if (cachedIterator) {
Florin Malita40f3db42021-07-27 14:02:28 -0400250 iterator.reset(sk_ubrk_clone(cachedIterator->get(), &status));
Jason Simmonsd8e94362021-01-11 15:52:03 -0800251 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400252 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Jason Simmonsd8e94362021-01-11 15:52:03 -0800253 }
254 }
255 return iterator;
256 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400257};
258
Julia Lavrova36700ef2020-08-25 11:42:51 -0400259class SkScriptIterator_icu : public SkScriptIterator {
260 public:
261 bool getScript(SkUnichar u, ScriptID* script) override {
262 UErrorCode status = U_ZERO_ERROR;
Florin Malita40f3db42021-07-27 14:02:28 -0400263 UScriptCode scriptCode = sk_uscript_getScript(u, &status);
Julia Lavrova36700ef2020-08-25 11:42:51 -0400264 if (U_FAILURE (status)) {
265 return false;
266 }
267 if (script) {
268 *script = (ScriptID)scriptCode;
269 }
270 return true;
271 }
272
273 static std::unique_ptr<SkScriptIterator> makeScriptIterator() {
274 return std::unique_ptr<SkScriptIterator>(new SkScriptIterator_icu());
275 }
276};
277
Julia Lavrova90787fe2020-07-20 17:32:03 +0000278class SkUnicode_icu : public SkUnicode {
Julia Lavrova6e51d922020-08-25 11:42:51 -0400279 static bool extractBidi(const char utf8[],
280 int utf8Units,
281 TextDirection dir,
282 std::vector<BidiRegion>* bidiRegions) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000283
284 // Convert to UTF16 since for now bidi iterator only operates on utf16
Julia Lavrova398ef442021-09-14 20:37:58 +0000285 auto utf16 = convertUtf8ToUtf16(utf8, utf8Units);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000286
287 // Create bidi iterator
288 UErrorCode status = U_ZERO_ERROR;
Julia Lavrova398ef442021-09-14 20:37:58 +0000289 SkUnicodeBidi bidi(sk_ubidi_openSized(utf16.size(), 0, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000290 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400291 SkDEBUGF("Bidi error: %s", sk_u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000292 return false;
293 }
294 SkASSERT(bidi);
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000295 uint8_t bidiLevel = (dir == TextDirection::kLTR) ? UBIDI_LTR : UBIDI_RTL;
Julia Lavrova90787fe2020-07-20 17:32:03 +0000296 // The required lifetime of utf16 isn't well documented.
297 // It appears it isn't used after ubidi_setPara except through ubidi_getText.
Julia Lavrova398ef442021-09-14 20:37:58 +0000298 sk_ubidi_setPara(bidi.get(), (const UChar*)utf16.c_str(), utf16.size(), bidiLevel, nullptr,
Florin Malita40f3db42021-07-27 14:02:28 -0400299 &status);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000300 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400301 SkDEBUGF("Bidi error: %s", sk_u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000302 return false;
303 }
304
305 // Iterate through bidi regions and the result positions into utf8
306 const char* start8 = utf8;
307 const char* end8 = utf8 + utf8Units;
308 BidiLevel currentLevel = 0;
309
310 Position pos8 = 0;
311 Position pos16 = 0;
Florin Malita40f3db42021-07-27 14:02:28 -0400312 Position end16 = sk_ubidi_getLength(bidi.get());
Julia Lavrova90787fe2020-07-20 17:32:03 +0000313 while (pos16 < end16) {
Florin Malita40f3db42021-07-27 14:02:28 -0400314 auto level = sk_ubidi_getLevelAt(bidi.get(), pos16);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000315 if (pos16 == 0) {
316 currentLevel = level;
317 } else if (level != currentLevel) {
318 Position end = start8 - utf8;
319 bidiRegions->emplace_back(pos8, end, currentLevel);
320 currentLevel = level;
321 pos8 = end;
322 }
323 SkUnichar u = utf8_next(&start8, end8);
324 pos16 += SkUTF::ToUTF16(u);
325 }
326 Position end = start8 - utf8;
327 if (end != pos8) {
328 bidiRegions->emplace_back(pos8, end, currentLevel);
329 }
330 return true;
331 }
332
333 static bool extractWords(uint16_t utf16[], int utf16Units, std::vector<Position>* words) {
334
335 UErrorCode status = U_ZERO_ERROR;
336
Jason Simmonsd8e94362021-01-11 15:52:03 -0800337 ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(BreakType::kWords);
338 if (!iterator) {
Florin Malita40f3db42021-07-27 14:02:28 -0400339 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000340 return false;
341 }
342 SkASSERT(iterator);
343
Florin Malita40f3db42021-07-27 14:02:28 -0400344 ICUUText utf16UText(sk_utext_openUChars(nullptr, (UChar*)utf16, utf16Units, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000345 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400346 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000347 return false;
348 }
349
Florin Malita40f3db42021-07-27 14:02:28 -0400350 sk_ubrk_setUText(iterator.get(), utf16UText.get(), &status);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000351 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400352 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000353 return false;
354 }
355
356 // Get the words
Florin Malita40f3db42021-07-27 14:02:28 -0400357 int32_t pos = sk_ubrk_first(iterator.get());
Julia Lavrova90787fe2020-07-20 17:32:03 +0000358 while (pos != UBRK_DONE) {
359 words->emplace_back(pos);
Florin Malita40f3db42021-07-27 14:02:28 -0400360 pos = sk_ubrk_next(iterator.get());
Julia Lavrova90787fe2020-07-20 17:32:03 +0000361 }
362
363 return true;
364 }
365
Julia Lavrova6e51d922020-08-25 11:42:51 -0400366 static bool extractPositions
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500367 (const char utf8[], int utf8Units, BreakType type, std::function<void(int, int)> setBreak) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000368
369 UErrorCode status = U_ZERO_ERROR;
Florin Malita40f3db42021-07-27 14:02:28 -0400370 ICUUText text(sk_utext_openUTF8(nullptr, &utf8[0], utf8Units, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000371
372 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400373 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000374 return false;
375 }
376 SkASSERT(text);
377
Jason Simmonsd8e94362021-01-11 15:52:03 -0800378 ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(type);
379 if (!iterator) {
380 return false;
Julia Lavrova90787fe2020-07-20 17:32:03 +0000381 }
382
Florin Malita40f3db42021-07-27 14:02:28 -0400383 sk_ubrk_setUText(iterator.get(), text.get(), &status);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000384 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400385 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000386 return false;
387 }
388
389 auto iter = iterator.get();
Florin Malita40f3db42021-07-27 14:02:28 -0400390 int32_t pos = sk_ubrk_first(iter);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000391 while (pos != UBRK_DONE) {
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500392 auto status = type == SkUnicode::BreakType::kLines
393 ? UBRK_LINE_SOFT
Florin Malita40f3db42021-07-27 14:02:28 -0400394 : sk_ubrk_getRuleStatus(iter);
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500395 setBreak(pos, status);
Florin Malita40f3db42021-07-27 14:02:28 -0400396 pos = sk_ubrk_next(iter);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000397 }
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500398
399 if (type == SkUnicode::BreakType::kLines) {
400 // This is a workaround for https://bugs.chromium.org/p/skia/issues/detail?id=10715
401 // (ICU line break iterator does not work correctly on Thai text with new lines)
402 // So, we only use the iterator to collect soft line breaks and
403 // scan the text for all hard line breaks ourselves
404 const char* end = utf8 + utf8Units;
405 const char* ch = utf8;
406 while (ch < end) {
407 auto unichar = utf8_next(&ch, end);
408 if (isHardLineBreak(unichar)) {
409 setBreak(ch - utf8, UBRK_LINE_HARD);
410 }
411 }
412 }
Julia Lavrova90787fe2020-07-20 17:32:03 +0000413 return true;
414 }
415
Julia Lavrova90787fe2020-07-20 17:32:03 +0000416public:
417 ~SkUnicode_icu() override { }
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000418 std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t text[], int count,
419 SkBidiIterator::Direction dir) override {
420 return SkBidiIterator_icu::makeBidiIterator(text, count, dir);
421 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400422 std::unique_ptr<SkBidiIterator> makeBidiIterator(const char text[],
423 int count,
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000424 SkBidiIterator::Direction dir) override {
425 return SkBidiIterator_icu::makeBidiIterator(text, count, dir);
426 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400427 std::unique_ptr<SkBreakIterator> makeBreakIterator(const char locale[],
428 BreakType breakType) override {
Jason Simmonsd8e94362021-01-11 15:52:03 -0800429 UErrorCode status = U_ZERO_ERROR;
Florin Malita40f3db42021-07-27 14:02:28 -0400430 ICUBreakIterator iterator(sk_ubrk_open(convertType(breakType), locale, nullptr, 0,
431 &status));
Jason Simmonsd8e94362021-01-11 15:52:03 -0800432 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400433 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Jason Simmonsd8e94362021-01-11 15:52:03 -0800434 return nullptr;
435 }
436 return std::unique_ptr<SkBreakIterator>(new SkBreakIterator_icu(std::move(iterator)));
Julia Lavrova6e51d922020-08-25 11:42:51 -0400437 }
Julia Lavrova10e7e772021-05-10 16:16:44 -0400438 std::unique_ptr<SkBreakIterator> makeBreakIterator(BreakType breakType) override {
Florin Malita40f3db42021-07-27 14:02:28 -0400439 return makeBreakIterator(sk_uloc_getDefault(), breakType);
Julia Lavrova10e7e772021-05-10 16:16:44 -0400440 }
Julia Lavrova36700ef2020-08-25 11:42:51 -0400441 std::unique_ptr<SkScriptIterator> makeScriptIterator() override {
442 return SkScriptIterator_icu::makeScriptIterator();
443 }
Julia Lavrova90787fe2020-07-20 17:32:03 +0000444
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000445 // TODO: Use ICU data file to detect controls and whitespaces
446 bool isControl(SkUnichar utf8) override {
Florin Malita40f3db42021-07-27 14:02:28 -0400447 return sk_u_iscntrl(utf8);
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000448 }
449
450 bool isWhitespace(SkUnichar utf8) override {
Florin Malita40f3db42021-07-27 14:02:28 -0400451 return sk_u_isWhitespace(utf8);
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000452 }
453
Julia Lavrovac70f8c32021-03-04 16:29:06 -0500454 bool isSpace(SkUnichar utf8) override {
Florin Malita40f3db42021-07-27 14:02:28 -0400455 return sk_u_isspace(utf8);
Julia Lavrovac70f8c32021-03-04 16:29:06 -0500456 }
457
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500458 static bool isHardLineBreak(SkUnichar utf8) {
Florin Malita40f3db42021-07-27 14:02:28 -0400459 auto property = sk_u_getIntPropertyValue(utf8, UCHAR_LINE_BREAK);
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500460 return property == U_LB_LINE_FEED || property == U_LB_MANDATORY_BREAK;
461 }
462
Florin Malita5572b2a2021-09-03 09:45:53 -0400463 SkString toUpper(const SkString& str) override {
464 // Convert to UTF16 since that's what ICU wants.
Julia Lavrova398ef442021-09-14 20:37:58 +0000465 auto str16 = convertUtf8ToUtf16(str.c_str(), str.size());
Florin Malita5572b2a2021-09-03 09:45:53 -0400466
467 UErrorCode icu_err = U_ZERO_ERROR;
Julia Lavrova398ef442021-09-14 20:37:58 +0000468 const auto upper16len = sk_u_strToUpper(nullptr, 0, (UChar*)(str16.c_str()), str16.size(),
Florin Malita5572b2a2021-09-03 09:45:53 -0400469 nullptr, &icu_err);
470 if (icu_err != U_BUFFER_OVERFLOW_ERROR || upper16len <= 0) {
471 return SkString();
472 }
473
474 SkAutoSTArray<128, uint16_t> upper16(upper16len);
475 icu_err = U_ZERO_ERROR;
476 sk_u_strToUpper((UChar*)(upper16.get()), SkToS32(upper16.size()),
Julia Lavrova398ef442021-09-14 20:37:58 +0000477 (UChar*)(str16.c_str()), str16.size(),
Florin Malita5572b2a2021-09-03 09:45:53 -0400478 nullptr, &icu_err);
479 SkASSERT(!U_FAILURE(icu_err));
480
481 // ... and back to utf8 'cause that's what we want.
Julia Lavrova398ef442021-09-14 20:37:58 +0000482 return convertUtf16ToUtf8((char16_t*)upper16.get(), upper16.size());
Florin Malita5572b2a2021-09-03 09:45:53 -0400483 }
484
Julia Lavrova6e51d922020-08-25 11:42:51 -0400485 bool getBidiRegions(const char utf8[],
486 int utf8Units,
487 TextDirection dir,
488 std::vector<BidiRegion>* results) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000489 return extractBidi(utf8, utf8Units, dir, results);
490 }
491
Julia Lavrova6e51d922020-08-25 11:42:51 -0400492 bool getLineBreaks(const char utf8[],
493 int utf8Units,
494 std::vector<LineBreakBefore>* results) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000495
Julia Lavrova6e51d922020-08-25 11:42:51 -0400496 return extractPositions(utf8, utf8Units, BreakType::kLines,
Julia Lavrova90787fe2020-07-20 17:32:03 +0000497 [results](int pos, int status) {
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500498 results->emplace_back(pos, status == UBRK_LINE_HARD
Julia Lavrova90787fe2020-07-20 17:32:03 +0000499 ? LineBreakType::kHardLineBreak
500 : LineBreakType::kSoftLineBreak);
501 });
502 }
503
504 bool getWords(const char utf8[], int utf8Units, std::vector<Position>* results) override {
505
506 // Convert to UTF16 since we want the results in utf16
Julia Lavrova398ef442021-09-14 20:37:58 +0000507 auto utf16 = convertUtf8ToUtf16(utf8, utf8Units);
508 return extractWords((uint16_t*)utf16.c_str(), utf16.size(), results);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000509 }
510
511 bool getGraphemes(const char utf8[], int utf8Units, std::vector<Position>* results) override {
512
Julia Lavrova6e51d922020-08-25 11:42:51 -0400513 return extractPositions(utf8, utf8Units, BreakType::kGraphemes,
Julia Lavrova90787fe2020-07-20 17:32:03 +0000514 [results](int pos, int status) { results->emplace_back(pos);
515 });
516 }
517
Julia Lavrova6e51d922020-08-25 11:42:51 -0400518 void reorderVisual(const BidiLevel runLevels[],
519 int levelsCount,
520 int32_t logicalFromVisual[]) override {
Florin Malita40f3db42021-07-27 14:02:28 -0400521 sk_ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000522 }
523};
524
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000525std::unique_ptr<SkUnicode> SkUnicode::Make() {
526 #if defined(SK_USING_THIRD_PARTY_ICU)
527 if (!SkLoadICU()) {
John Stiles154bd1f2021-05-13 09:38:51 -0400528 static SkOnce once;
529 once([] { SkDEBUGF("SkLoadICU() failed!\n"); });
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000530 return nullptr;
531 }
532 #endif
Florin Malita40f3db42021-07-27 14:02:28 -0400533
534 return ICULib()
535 ? std::make_unique<SkUnicode_icu>()
536 : nullptr;
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000537}