blob: 4d5b960da17192baa21c2e6be6c76c0556291788 [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
285 std::unique_ptr<uint16_t[]> utf16;
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000286 auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000287 if (utf16Units < 0) {
288 return false;
289 }
290
291 // Create bidi iterator
292 UErrorCode status = U_ZERO_ERROR;
Florin Malita40f3db42021-07-27 14:02:28 -0400293 SkUnicodeBidi bidi(sk_ubidi_openSized(utf16Units, 0, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000294 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400295 SkDEBUGF("Bidi error: %s", sk_u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000296 return false;
297 }
298 SkASSERT(bidi);
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000299 uint8_t bidiLevel = (dir == TextDirection::kLTR) ? UBIDI_LTR : UBIDI_RTL;
Julia Lavrova90787fe2020-07-20 17:32:03 +0000300 // The required lifetime of utf16 isn't well documented.
301 // It appears it isn't used after ubidi_setPara except through ubidi_getText.
Florin Malita40f3db42021-07-27 14:02:28 -0400302 sk_ubidi_setPara(bidi.get(), (const UChar*)utf16.get(), utf16Units, bidiLevel, nullptr,
303 &status);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000304 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400305 SkDEBUGF("Bidi error: %s", sk_u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000306 return false;
307 }
308
309 // Iterate through bidi regions and the result positions into utf8
310 const char* start8 = utf8;
311 const char* end8 = utf8 + utf8Units;
312 BidiLevel currentLevel = 0;
313
314 Position pos8 = 0;
315 Position pos16 = 0;
Florin Malita40f3db42021-07-27 14:02:28 -0400316 Position end16 = sk_ubidi_getLength(bidi.get());
Julia Lavrova90787fe2020-07-20 17:32:03 +0000317 while (pos16 < end16) {
Florin Malita40f3db42021-07-27 14:02:28 -0400318 auto level = sk_ubidi_getLevelAt(bidi.get(), pos16);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000319 if (pos16 == 0) {
320 currentLevel = level;
321 } else if (level != currentLevel) {
322 Position end = start8 - utf8;
323 bidiRegions->emplace_back(pos8, end, currentLevel);
324 currentLevel = level;
325 pos8 = end;
326 }
327 SkUnichar u = utf8_next(&start8, end8);
328 pos16 += SkUTF::ToUTF16(u);
329 }
330 Position end = start8 - utf8;
331 if (end != pos8) {
332 bidiRegions->emplace_back(pos8, end, currentLevel);
333 }
334 return true;
335 }
336
337 static bool extractWords(uint16_t utf16[], int utf16Units, std::vector<Position>* words) {
338
339 UErrorCode status = U_ZERO_ERROR;
340
Jason Simmonsd8e94362021-01-11 15:52:03 -0800341 ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(BreakType::kWords);
342 if (!iterator) {
Florin Malita40f3db42021-07-27 14:02:28 -0400343 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000344 return false;
345 }
346 SkASSERT(iterator);
347
Florin Malita40f3db42021-07-27 14:02:28 -0400348 ICUUText utf16UText(sk_utext_openUChars(nullptr, (UChar*)utf16, utf16Units, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000349 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400350 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000351 return false;
352 }
353
Florin Malita40f3db42021-07-27 14:02:28 -0400354 sk_ubrk_setUText(iterator.get(), utf16UText.get(), &status);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000355 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400356 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000357 return false;
358 }
359
360 // Get the words
Florin Malita40f3db42021-07-27 14:02:28 -0400361 int32_t pos = sk_ubrk_first(iterator.get());
Julia Lavrova90787fe2020-07-20 17:32:03 +0000362 while (pos != UBRK_DONE) {
363 words->emplace_back(pos);
Florin Malita40f3db42021-07-27 14:02:28 -0400364 pos = sk_ubrk_next(iterator.get());
Julia Lavrova90787fe2020-07-20 17:32:03 +0000365 }
366
367 return true;
368 }
369
Julia Lavrova6e51d922020-08-25 11:42:51 -0400370 static bool extractPositions
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500371 (const char utf8[], int utf8Units, BreakType type, std::function<void(int, int)> setBreak) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000372
373 UErrorCode status = U_ZERO_ERROR;
Florin Malita40f3db42021-07-27 14:02:28 -0400374 ICUUText text(sk_utext_openUTF8(nullptr, &utf8[0], utf8Units, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000375
376 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400377 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000378 return false;
379 }
380 SkASSERT(text);
381
Jason Simmonsd8e94362021-01-11 15:52:03 -0800382 ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(type);
383 if (!iterator) {
384 return false;
Julia Lavrova90787fe2020-07-20 17:32:03 +0000385 }
386
Florin Malita40f3db42021-07-27 14:02:28 -0400387 sk_ubrk_setUText(iterator.get(), text.get(), &status);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000388 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400389 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000390 return false;
391 }
392
393 auto iter = iterator.get();
Florin Malita40f3db42021-07-27 14:02:28 -0400394 int32_t pos = sk_ubrk_first(iter);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000395 while (pos != UBRK_DONE) {
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500396 auto status = type == SkUnicode::BreakType::kLines
397 ? UBRK_LINE_SOFT
Florin Malita40f3db42021-07-27 14:02:28 -0400398 : sk_ubrk_getRuleStatus(iter);
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500399 setBreak(pos, status);
Florin Malita40f3db42021-07-27 14:02:28 -0400400 pos = sk_ubrk_next(iter);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000401 }
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500402
403 if (type == SkUnicode::BreakType::kLines) {
404 // This is a workaround for https://bugs.chromium.org/p/skia/issues/detail?id=10715
405 // (ICU line break iterator does not work correctly on Thai text with new lines)
406 // So, we only use the iterator to collect soft line breaks and
407 // scan the text for all hard line breaks ourselves
408 const char* end = utf8 + utf8Units;
409 const char* ch = utf8;
410 while (ch < end) {
411 auto unichar = utf8_next(&ch, end);
412 if (isHardLineBreak(unichar)) {
413 setBreak(ch - utf8, UBRK_LINE_HARD);
414 }
415 }
416 }
Julia Lavrova90787fe2020-07-20 17:32:03 +0000417 return true;
418 }
419
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000420 static int utf8ToUtf16(const char* utf8, size_t utf8Units, std::unique_ptr<uint16_t[]>* utf16) {
421 int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units);
422 if (utf16Units < 0) {
423 SkDEBUGF("Convert error: Invalid utf8 input");
424 return utf16Units;
425 }
426 *utf16 = std::unique_ptr<uint16_t[]>(new uint16_t[utf16Units]);
427 SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16->get(), utf16Units, utf8, utf8Units);
428 SkASSERT(dstLen == utf16Units);
429 return utf16Units;
430 }
431
432 static int utf16ToUtf8(const uint16_t* utf16, size_t utf16Units, std::unique_ptr<char[]>* utf8) {
433 int utf8Units = SkUTF::UTF16ToUTF8(nullptr, 0, utf16, utf16Units);
434 if (utf8Units < 0) {
435 SkDEBUGF("Convert error: Invalid utf16 input");
436 return utf8Units;
437 }
438 *utf8 = std::unique_ptr<char[]>(new char[utf8Units]);
439 SkDEBUGCODE(int dstLen =) SkUTF::UTF16ToUTF8(utf8->get(), utf8Units, utf16, utf16Units);
440 SkASSERT(dstLen == utf8Units);
441 return utf8Units;
442 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400443
Julia Lavrova90787fe2020-07-20 17:32:03 +0000444public:
445 ~SkUnicode_icu() override { }
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000446 std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t text[], int count,
447 SkBidiIterator::Direction dir) override {
448 return SkBidiIterator_icu::makeBidiIterator(text, count, dir);
449 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400450 std::unique_ptr<SkBidiIterator> makeBidiIterator(const char text[],
451 int count,
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000452 SkBidiIterator::Direction dir) override {
453 return SkBidiIterator_icu::makeBidiIterator(text, count, dir);
454 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400455 std::unique_ptr<SkBreakIterator> makeBreakIterator(const char locale[],
456 BreakType breakType) override {
Jason Simmonsd8e94362021-01-11 15:52:03 -0800457 UErrorCode status = U_ZERO_ERROR;
Florin Malita40f3db42021-07-27 14:02:28 -0400458 ICUBreakIterator iterator(sk_ubrk_open(convertType(breakType), locale, nullptr, 0,
459 &status));
Jason Simmonsd8e94362021-01-11 15:52:03 -0800460 if (U_FAILURE(status)) {
Florin Malita40f3db42021-07-27 14:02:28 -0400461 SkDEBUGF("Break error: %s", sk_u_errorName(status));
Jason Simmonsd8e94362021-01-11 15:52:03 -0800462 return nullptr;
463 }
464 return std::unique_ptr<SkBreakIterator>(new SkBreakIterator_icu(std::move(iterator)));
Julia Lavrova6e51d922020-08-25 11:42:51 -0400465 }
Julia Lavrova10e7e772021-05-10 16:16:44 -0400466 std::unique_ptr<SkBreakIterator> makeBreakIterator(BreakType breakType) override {
Florin Malita40f3db42021-07-27 14:02:28 -0400467 return makeBreakIterator(sk_uloc_getDefault(), breakType);
Julia Lavrova10e7e772021-05-10 16:16:44 -0400468 }
Julia Lavrova36700ef2020-08-25 11:42:51 -0400469 std::unique_ptr<SkScriptIterator> makeScriptIterator() override {
470 return SkScriptIterator_icu::makeScriptIterator();
471 }
Julia Lavrova90787fe2020-07-20 17:32:03 +0000472
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000473 // TODO: Use ICU data file to detect controls and whitespaces
474 bool isControl(SkUnichar utf8) override {
Florin Malita40f3db42021-07-27 14:02:28 -0400475 return sk_u_iscntrl(utf8);
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000476 }
477
478 bool isWhitespace(SkUnichar utf8) override {
Florin Malita40f3db42021-07-27 14:02:28 -0400479 return sk_u_isWhitespace(utf8);
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000480 }
481
Julia Lavrovac70f8c32021-03-04 16:29:06 -0500482 bool isSpace(SkUnichar utf8) override {
Florin Malita40f3db42021-07-27 14:02:28 -0400483 return sk_u_isspace(utf8);
Julia Lavrovac70f8c32021-03-04 16:29:06 -0500484 }
485
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500486 static bool isHardLineBreak(SkUnichar utf8) {
Florin Malita40f3db42021-07-27 14:02:28 -0400487 auto property = sk_u_getIntPropertyValue(utf8, UCHAR_LINE_BREAK);
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500488 return property == U_LB_LINE_FEED || property == U_LB_MANDATORY_BREAK;
489 }
490
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000491 SkString convertUtf16ToUtf8(const std::u16string& utf16) override {
492 std::unique_ptr<char[]> utf8;
493 auto utf8Units = SkUnicode_icu::utf16ToUtf8((uint16_t*)utf16.data(), utf16.size(), &utf8);
494 if (utf8Units >= 0) {
495 return SkString(utf8.get(), utf8Units);
496 } else {
497 return SkString();
498 }
499 }
500
Julia Lavrova6e51d922020-08-25 11:42:51 -0400501 bool getBidiRegions(const char utf8[],
502 int utf8Units,
503 TextDirection dir,
504 std::vector<BidiRegion>* results) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000505 return extractBidi(utf8, utf8Units, dir, results);
506 }
507
Julia Lavrova6e51d922020-08-25 11:42:51 -0400508 bool getLineBreaks(const char utf8[],
509 int utf8Units,
510 std::vector<LineBreakBefore>* results) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000511
Julia Lavrova6e51d922020-08-25 11:42:51 -0400512 return extractPositions(utf8, utf8Units, BreakType::kLines,
Julia Lavrova90787fe2020-07-20 17:32:03 +0000513 [results](int pos, int status) {
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500514 results->emplace_back(pos, status == UBRK_LINE_HARD
Julia Lavrova90787fe2020-07-20 17:32:03 +0000515 ? LineBreakType::kHardLineBreak
516 : LineBreakType::kSoftLineBreak);
517 });
518 }
519
520 bool getWords(const char utf8[], int utf8Units, std::vector<Position>* results) override {
521
522 // Convert to UTF16 since we want the results in utf16
523 std::unique_ptr<uint16_t[]> utf16;
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000524 auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000525 if (utf16Units < 0) {
526 return false;
527 }
528
529 return extractWords(utf16.get(), utf16Units, results);
530 }
531
532 bool getGraphemes(const char utf8[], int utf8Units, std::vector<Position>* results) override {
533
Julia Lavrova6e51d922020-08-25 11:42:51 -0400534 return extractPositions(utf8, utf8Units, BreakType::kGraphemes,
Julia Lavrova90787fe2020-07-20 17:32:03 +0000535 [results](int pos, int status) { results->emplace_back(pos);
536 });
537 }
538
Julia Lavrova6e51d922020-08-25 11:42:51 -0400539 void reorderVisual(const BidiLevel runLevels[],
540 int levelsCount,
541 int32_t logicalFromVisual[]) override {
Florin Malita40f3db42021-07-27 14:02:28 -0400542 sk_ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000543 }
544};
545
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000546std::unique_ptr<SkUnicode> SkUnicode::Make() {
547 #if defined(SK_USING_THIRD_PARTY_ICU)
548 if (!SkLoadICU()) {
John Stiles154bd1f2021-05-13 09:38:51 -0400549 static SkOnce once;
550 once([] { SkDEBUGF("SkLoadICU() failed!\n"); });
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000551 return nullptr;
552 }
553 #endif
Florin Malita40f3db42021-07-27 14:02:28 -0400554
555 return ICULib()
556 ? std::make_unique<SkUnicode_icu>()
557 : nullptr;
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000558}