blob: 4a3a2e50ed621233b884b4eea64ef25ed60a6d39 [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"
13#include "modules/skshaper/src/SkUnicode.h"
14#include "src/utils/SkUTF.h"
15#include <unicode/ubidi.h>
16#include <unicode/ubrk.h>
Julia Lavrova36700ef2020-08-25 11:42:51 -040017#include <unicode/uscript.h>
Julia Lavrovab6b7fff2020-09-11 13:59:49 +000018#include <unicode/ustring.h>
Julia Lavrova90787fe2020-07-20 17:32:03 +000019#include <unicode/utext.h>
20#include <unicode/utypes.h>
21#include <vector>
22#include <functional>
23
Julia Lavrovab6b7fff2020-09-11 13:59:49 +000024#if defined(SK_USING_THIRD_PARTY_ICU)
25#include "SkLoadICU.h"
26#endif
27
Julia Lavrova4414f872021-03-03 10:19:41 -050028// ubrk_clone added as draft in ICU69 and Android API 31 (first ICU NDK).
29// ubrk_safeClone deprecated in ICU69 and not exposed by Android.
30template<typename...> using void_t = void;
31template<typename T, typename = void>
32struct SkUbrkClone {
33 UBreakIterator* operator()(T bi, UErrorCode* status) {
34 return ubrk_safeClone(bi, nullptr, nullptr, status);
35 }
36};
37template<typename T>
38struct SkUbrkClone<T, void_t<decltype(ubrk_clone(std::declval<T>(), nullptr))>> {
39 UBreakIterator* operator()(T bi, UErrorCode* status) {
40 return ubrk_clone(bi, status);
41 }
42};
43
Julia Lavrova1798f4f2020-08-26 14:22:48 +000044using SkUnicodeBidi = std::unique_ptr<UBiDi, SkFunctionWrapper<decltype(ubidi_close), ubidi_close>>;
Julia Lavrova90787fe2020-07-20 17:32:03 +000045using ICUUText = std::unique_ptr<UText, SkFunctionWrapper<decltype(utext_close), utext_close>>;
46using ICUBreakIterator = std::unique_ptr<UBreakIterator, SkFunctionWrapper<decltype(ubrk_close), ubrk_close>>;
47
48/** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */
49static inline SkUnichar utf8_next(const char** ptr, const char* end) {
50 SkUnichar val = SkUTF::NextUTF8(ptr, end);
51 return val < 0 ? 0xFFFD : val;
52}
53
Jason Simmonsd8e94362021-01-11 15:52:03 -080054static UBreakIteratorType convertType(SkUnicode::BreakType type) {
55 switch (type) {
56 case SkUnicode::BreakType::kLines: return UBRK_LINE;
57 case SkUnicode::BreakType::kGraphemes: return UBRK_CHARACTER;
58 case SkUnicode::BreakType::kWords: return UBRK_WORD;
59 default:
60 return UBRK_CHARACTER;
61 }
62}
63
Julia Lavrova1798f4f2020-08-26 14:22:48 +000064class SkBidiIterator_icu : public SkBidiIterator {
65 SkUnicodeBidi fBidi;
66public:
67 explicit SkBidiIterator_icu(SkUnicodeBidi bidi) : fBidi(std::move(bidi)) {}
68 Position getLength() override { return ubidi_getLength(fBidi.get()); }
69 Level getLevelAt(Position pos) override { return ubidi_getLevelAt(fBidi.get(), pos); }
70
71 static std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t utf16[], int utf16Units, Direction dir) {
72 UErrorCode status = U_ZERO_ERROR;
73 SkUnicodeBidi bidi(ubidi_openSized(utf16Units, 0, &status));
74 if (U_FAILURE(status)) {
75 SkDEBUGF("Bidi error: %s", u_errorName(status));
76 return nullptr;
77 }
78 SkASSERT(bidi);
79 uint8_t bidiLevel = (dir == SkBidiIterator::kLTR) ? UBIDI_LTR : UBIDI_RTL;
80 // The required lifetime of utf16 isn't well documented.
81 // It appears it isn't used after ubidi_setPara except through ubidi_getText.
82 ubidi_setPara(bidi.get(), (const UChar*)utf16, utf16Units, bidiLevel, nullptr, &status);
83 if (U_FAILURE(status)) {
84 SkDEBUGF("Bidi error: %s", u_errorName(status));
85 return nullptr;
86 }
87 return std::unique_ptr<SkBidiIterator>(new SkBidiIterator_icu(std::move(bidi)));
88 }
89
90 // ICU bidi iterator works with utf16 but clients (Flutter for instance) may work with utf8
91 // This method allows the clients not to think about all these details
92 static std::unique_ptr<SkBidiIterator> makeBidiIterator(const char utf8[], int utf8Units, Direction dir) {
93 // Convert utf8 into utf16 since ubidi only accepts utf16
94 if (!SkTFitsIn<int32_t>(utf8Units)) {
95 SkDEBUGF("Bidi error: text too long");
96 return nullptr;
97 }
98
99 // Getting the length like this seems to always set U_BUFFER_OVERFLOW_ERROR
100 int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units);
101 if (utf16Units < 0) {
102 SkDEBUGF("Bidi error: Invalid utf8 input");
103 return nullptr;
104 }
105 std::unique_ptr<uint16_t[]> utf16(new uint16_t[utf16Units]);
106 SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16.get(), utf16Units, utf8, utf8Units);
107 SkASSERT(dstLen == utf16Units);
108
109 return makeBidiIterator(utf16.get(), utf16Units, dir);
110 }
111
112 // This method returns the final results only: a list of bidi regions
113 // (this is all SkParagraph really needs; SkShaper however uses the iterator itself)
114 static std::vector<Region> getBidiRegions(const char utf8[], int utf8Units, Direction dir) {
115
116 auto bidiIterator = makeBidiIterator(utf8, utf8Units, dir);
117 std::vector<Region> bidiRegions;
118 const char* start8 = utf8;
119 const char* end8 = utf8 + utf8Units;
120 SkBidiIterator::Level currentLevel = 0;
121
122 Position pos8 = 0;
123 Position pos16 = 0;
124 Position end16 = bidiIterator->getLength();
125 while (pos16 < end16) {
126 auto level = bidiIterator->getLevelAt(pos16);
127 if (pos16 == 0) {
128 currentLevel = level;
129 } else if (level != currentLevel) {
130 auto end = start8 - utf8;
131 bidiRegions.emplace_back(pos8, end, currentLevel);
132 currentLevel = level;
133 pos8 = end;
134 }
135 SkUnichar u = utf8_next(&start8, end8);
136 pos16 += SkUTF::ToUTF16(u);
137 }
138 auto end = start8 - utf8;
139 if (end != pos8) {
140 bidiRegions.emplace_back(pos8, end, currentLevel);
141 }
142 return bidiRegions;
143 }
144};
145
146void SkBidiIterator::ReorderVisual(const Level runLevels[], int levelsCount,
147 int32_t logicalFromVisual[]) {
148 ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual);
149}
Julia Lavrova90787fe2020-07-20 17:32:03 +0000150
Julia Lavrova6e51d922020-08-25 11:42:51 -0400151class SkBreakIterator_icu : public SkBreakIterator {
152 ICUBreakIterator fBreakIterator;
153 Position fLastResult;
154 public:
155 explicit SkBreakIterator_icu(ICUBreakIterator iter)
156 : fBreakIterator(std::move(iter)), fLastResult(0) {}
157 Position first() override
158 { return fLastResult = ubrk_first(fBreakIterator.get()); }
159 Position current() override
160 { return fLastResult = ubrk_current(fBreakIterator.get()); }
161 Position next() override
162 { return fLastResult = ubrk_next(fBreakIterator.get()); }
163 Position preceding(Position offset) override
164 { return fLastResult = ubrk_preceding(fBreakIterator.get(), offset); }
165 Position following(Position offset) override
166 { return fLastResult = ubrk_following(fBreakIterator.get(), offset);}
167 Status status() override { return ubrk_getRuleStatus(fBreakIterator.get()); }
168 bool isDone() override { return fLastResult == UBRK_DONE; }
169
170 bool setText(const char utftext8[], int utf8Units) override {
171 UErrorCode status = U_ZERO_ERROR;
Julia Lavrovaacace5d2020-12-16 11:48:48 -0500172 ICUUText text(utext_openUTF8(nullptr, &utftext8[0], utf8Units, &status));
Julia Lavrova6e51d922020-08-25 11:42:51 -0400173
174 if (U_FAILURE(status)) {
175 SkDEBUGF("Break error: %s", u_errorName(status));
176 return false;
177 }
178 SkASSERT(text);
179 ubrk_setUText(fBreakIterator.get(), text.get(), &status);
180 if (U_FAILURE(status)) {
181 SkDEBUGF("Break error: %s", u_errorName(status));
182 return false;
183 }
184 fLastResult = 0;
185 return true;
186 }
Jason Simmonsd8e94362021-01-11 15:52:03 -0800187};
Julia Lavrova6e51d922020-08-25 11:42:51 -0400188
Jason Simmonsd8e94362021-01-11 15:52:03 -0800189class SkIcuBreakIteratorCache {
190 SkTHashMap<SkUnicode::BreakType, ICUBreakIterator> fBreakCache;
191 SkMutex fBreakCacheMutex;
192
193 public:
194 static SkIcuBreakIteratorCache& get() {
195 static SkIcuBreakIteratorCache instance;
196 return instance;
Julia Lavrova6e51d922020-08-25 11:42:51 -0400197 }
198
Jason Simmonsd8e94362021-01-11 15:52:03 -0800199 ICUBreakIterator makeBreakIterator(SkUnicode::BreakType type) {
Julia Lavrova6e51d922020-08-25 11:42:51 -0400200 UErrorCode status = U_ZERO_ERROR;
Jason Simmonsd8e94362021-01-11 15:52:03 -0800201 ICUBreakIterator* cachedIterator;
202 {
203 SkAutoMutexExclusive lock(fBreakCacheMutex);
204 cachedIterator = fBreakCache.find(type);
205 if (!cachedIterator) {
206 ICUBreakIterator newIterator(ubrk_open(convertType(type), uloc_getDefault(), nullptr, 0, &status));
207 if (U_FAILURE(status)) {
208 SkDEBUGF("Break error: %s", u_errorName(status));
209 } else {
210 cachedIterator = fBreakCache.set(type, std::move(newIterator));
211 }
212 }
213 }
214 ICUBreakIterator iterator;
215 if (cachedIterator) {
Julia Lavrova4414f872021-03-03 10:19:41 -0500216 iterator.reset(SkUbrkClone<const UBreakIterator*>()(cachedIterator->get(), &status));
Jason Simmonsd8e94362021-01-11 15:52:03 -0800217 if (U_FAILURE(status)) {
218 SkDEBUGF("Break error: %s", u_errorName(status));
219 }
220 }
221 return iterator;
222 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400223};
224
Julia Lavrova36700ef2020-08-25 11:42:51 -0400225class SkScriptIterator_icu : public SkScriptIterator {
226 public:
227 bool getScript(SkUnichar u, ScriptID* script) override {
228 UErrorCode status = U_ZERO_ERROR;
229 UScriptCode scriptCode = uscript_getScript(u, &status);
230 if (U_FAILURE (status)) {
231 return false;
232 }
233 if (script) {
234 *script = (ScriptID)scriptCode;
235 }
236 return true;
237 }
238
239 static std::unique_ptr<SkScriptIterator> makeScriptIterator() {
240 return std::unique_ptr<SkScriptIterator>(new SkScriptIterator_icu());
241 }
242};
243
Julia Lavrova90787fe2020-07-20 17:32:03 +0000244class SkUnicode_icu : public SkUnicode {
Julia Lavrova6e51d922020-08-25 11:42:51 -0400245 static bool extractBidi(const char utf8[],
246 int utf8Units,
247 TextDirection dir,
248 std::vector<BidiRegion>* bidiRegions) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000249
250 // Convert to UTF16 since for now bidi iterator only operates on utf16
251 std::unique_ptr<uint16_t[]> utf16;
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000252 auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000253 if (utf16Units < 0) {
254 return false;
255 }
256
257 // Create bidi iterator
258 UErrorCode status = U_ZERO_ERROR;
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000259 SkUnicodeBidi bidi(ubidi_openSized(utf16Units, 0, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000260 if (U_FAILURE(status)) {
261 SkDEBUGF("Bidi error: %s", u_errorName(status));
262 return false;
263 }
264 SkASSERT(bidi);
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000265 uint8_t bidiLevel = (dir == TextDirection::kLTR) ? UBIDI_LTR : UBIDI_RTL;
Julia Lavrova90787fe2020-07-20 17:32:03 +0000266 // The required lifetime of utf16 isn't well documented.
267 // It appears it isn't used after ubidi_setPara except through ubidi_getText.
268 ubidi_setPara(bidi.get(), (const UChar*)utf16.get(), utf16Units, bidiLevel, nullptr, &status);
269 if (U_FAILURE(status)) {
270 SkDEBUGF("Bidi error: %s", u_errorName(status));
271 return false;
272 }
273
274 // Iterate through bidi regions and the result positions into utf8
275 const char* start8 = utf8;
276 const char* end8 = utf8 + utf8Units;
277 BidiLevel currentLevel = 0;
278
279 Position pos8 = 0;
280 Position pos16 = 0;
281 Position end16 = ubidi_getLength(bidi.get());
282 while (pos16 < end16) {
283 auto level = ubidi_getLevelAt(bidi.get(), pos16);
284 if (pos16 == 0) {
285 currentLevel = level;
286 } else if (level != currentLevel) {
287 Position end = start8 - utf8;
288 bidiRegions->emplace_back(pos8, end, currentLevel);
289 currentLevel = level;
290 pos8 = end;
291 }
292 SkUnichar u = utf8_next(&start8, end8);
293 pos16 += SkUTF::ToUTF16(u);
294 }
295 Position end = start8 - utf8;
296 if (end != pos8) {
297 bidiRegions->emplace_back(pos8, end, currentLevel);
298 }
299 return true;
300 }
301
302 static bool extractWords(uint16_t utf16[], int utf16Units, std::vector<Position>* words) {
303
304 UErrorCode status = U_ZERO_ERROR;
305
Jason Simmonsd8e94362021-01-11 15:52:03 -0800306 ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(BreakType::kWords);
307 if (!iterator) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000308 SkDEBUGF("Break error: %s", u_errorName(status));
309 return false;
310 }
311 SkASSERT(iterator);
312
Julia Lavrovaacace5d2020-12-16 11:48:48 -0500313 ICUUText utf16UText(utext_openUChars(nullptr, (UChar*)utf16, utf16Units, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000314 if (U_FAILURE(status)) {
315 SkDEBUGF("Break error: %s", u_errorName(status));
316 return false;
317 }
318
319 ubrk_setUText(iterator.get(), utf16UText.get(), &status);
320 if (U_FAILURE(status)) {
321 SkDEBUGF("Break error: %s", u_errorName(status));
322 return false;
323 }
324
325 // Get the words
326 int32_t pos = ubrk_first(iterator.get());
327 while (pos != UBRK_DONE) {
328 words->emplace_back(pos);
329 pos = ubrk_next(iterator.get());
330 }
331
332 return true;
333 }
334
Julia Lavrova6e51d922020-08-25 11:42:51 -0400335 static bool extractPositions
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500336 (const char utf8[], int utf8Units, BreakType type, std::function<void(int, int)> setBreak) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000337
338 UErrorCode status = U_ZERO_ERROR;
Julia Lavrovaacace5d2020-12-16 11:48:48 -0500339 ICUUText text(utext_openUTF8(nullptr, &utf8[0], utf8Units, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000340
341 if (U_FAILURE(status)) {
342 SkDEBUGF("Break error: %s", u_errorName(status));
343 return false;
344 }
345 SkASSERT(text);
346
Jason Simmonsd8e94362021-01-11 15:52:03 -0800347 ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(type);
348 if (!iterator) {
349 return false;
Julia Lavrova90787fe2020-07-20 17:32:03 +0000350 }
351
352 ubrk_setUText(iterator.get(), text.get(), &status);
353 if (U_FAILURE(status)) {
354 SkDEBUGF("Break error: %s", u_errorName(status));
355 return false;
356 }
357
358 auto iter = iterator.get();
359 int32_t pos = ubrk_first(iter);
360 while (pos != UBRK_DONE) {
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500361 auto status = type == SkUnicode::BreakType::kLines
362 ? UBRK_LINE_SOFT
363 : ubrk_getRuleStatus(iter);
364 setBreak(pos, status);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000365 pos = ubrk_next(iter);
366 }
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500367
368 if (type == SkUnicode::BreakType::kLines) {
369 // This is a workaround for https://bugs.chromium.org/p/skia/issues/detail?id=10715
370 // (ICU line break iterator does not work correctly on Thai text with new lines)
371 // So, we only use the iterator to collect soft line breaks and
372 // scan the text for all hard line breaks ourselves
373 const char* end = utf8 + utf8Units;
374 const char* ch = utf8;
375 while (ch < end) {
376 auto unichar = utf8_next(&ch, end);
377 if (isHardLineBreak(unichar)) {
378 setBreak(ch - utf8, UBRK_LINE_HARD);
379 }
380 }
381 }
Julia Lavrova90787fe2020-07-20 17:32:03 +0000382 return true;
383 }
384
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000385 static int utf8ToUtf16(const char* utf8, size_t utf8Units, std::unique_ptr<uint16_t[]>* utf16) {
386 int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units);
387 if (utf16Units < 0) {
388 SkDEBUGF("Convert error: Invalid utf8 input");
389 return utf16Units;
390 }
391 *utf16 = std::unique_ptr<uint16_t[]>(new uint16_t[utf16Units]);
392 SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16->get(), utf16Units, utf8, utf8Units);
393 SkASSERT(dstLen == utf16Units);
394 return utf16Units;
395 }
396
397 static int utf16ToUtf8(const uint16_t* utf16, size_t utf16Units, std::unique_ptr<char[]>* utf8) {
398 int utf8Units = SkUTF::UTF16ToUTF8(nullptr, 0, utf16, utf16Units);
399 if (utf8Units < 0) {
400 SkDEBUGF("Convert error: Invalid utf16 input");
401 return utf8Units;
402 }
403 *utf8 = std::unique_ptr<char[]>(new char[utf8Units]);
404 SkDEBUGCODE(int dstLen =) SkUTF::UTF16ToUTF8(utf8->get(), utf8Units, utf16, utf16Units);
405 SkASSERT(dstLen == utf8Units);
406 return utf8Units;
407 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400408
Julia Lavrova90787fe2020-07-20 17:32:03 +0000409public:
410 ~SkUnicode_icu() override { }
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000411 std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t text[], int count,
412 SkBidiIterator::Direction dir) override {
413 return SkBidiIterator_icu::makeBidiIterator(text, count, dir);
414 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400415 std::unique_ptr<SkBidiIterator> makeBidiIterator(const char text[],
416 int count,
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000417 SkBidiIterator::Direction dir) override {
418 return SkBidiIterator_icu::makeBidiIterator(text, count, dir);
419 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400420 std::unique_ptr<SkBreakIterator> makeBreakIterator(const char locale[],
421 BreakType breakType) override {
Jason Simmonsd8e94362021-01-11 15:52:03 -0800422 UErrorCode status = U_ZERO_ERROR;
423 ICUBreakIterator iterator(ubrk_open(convertType(breakType), locale, nullptr, 0, &status));
424 if (U_FAILURE(status)) {
425 SkDEBUGF("Break error: %s", u_errorName(status));
426 return nullptr;
427 }
428 return std::unique_ptr<SkBreakIterator>(new SkBreakIterator_icu(std::move(iterator)));
Julia Lavrova6e51d922020-08-25 11:42:51 -0400429 }
Julia Lavrova36700ef2020-08-25 11:42:51 -0400430 std::unique_ptr<SkScriptIterator> makeScriptIterator() override {
431 return SkScriptIterator_icu::makeScriptIterator();
432 }
Julia Lavrova90787fe2020-07-20 17:32:03 +0000433
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000434 // TODO: Use ICU data file to detect controls and whitespaces
435 bool isControl(SkUnichar utf8) override {
436 return u_iscntrl(utf8);
437 }
438
439 bool isWhitespace(SkUnichar utf8) override {
440 return u_isWhitespace(utf8);
441 }
442
Julia Lavrovac70f8c32021-03-04 16:29:06 -0500443 bool isSpace(SkUnichar utf8) override {
444 return u_isspace(utf8);
445 }
446
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500447 static bool isHardLineBreak(SkUnichar utf8) {
448 auto property = u_getIntPropertyValue(utf8, UCHAR_LINE_BREAK);
449 return property == U_LB_LINE_FEED || property == U_LB_MANDATORY_BREAK;
450 }
451
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000452 SkString convertUtf16ToUtf8(const std::u16string& utf16) override {
453 std::unique_ptr<char[]> utf8;
454 auto utf8Units = SkUnicode_icu::utf16ToUtf8((uint16_t*)utf16.data(), utf16.size(), &utf8);
455 if (utf8Units >= 0) {
456 return SkString(utf8.get(), utf8Units);
457 } else {
458 return SkString();
459 }
460 }
461
Julia Lavrova6e51d922020-08-25 11:42:51 -0400462 bool getBidiRegions(const char utf8[],
463 int utf8Units,
464 TextDirection dir,
465 std::vector<BidiRegion>* results) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000466 return extractBidi(utf8, utf8Units, dir, results);
467 }
468
Julia Lavrova6e51d922020-08-25 11:42:51 -0400469 bool getLineBreaks(const char utf8[],
470 int utf8Units,
471 std::vector<LineBreakBefore>* results) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000472
Julia Lavrova6e51d922020-08-25 11:42:51 -0400473 return extractPositions(utf8, utf8Units, BreakType::kLines,
Julia Lavrova90787fe2020-07-20 17:32:03 +0000474 [results](int pos, int status) {
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500475 results->emplace_back(pos, status == UBRK_LINE_HARD
Julia Lavrova90787fe2020-07-20 17:32:03 +0000476 ? LineBreakType::kHardLineBreak
477 : LineBreakType::kSoftLineBreak);
478 });
479 }
480
481 bool getWords(const char utf8[], int utf8Units, std::vector<Position>* results) override {
482
483 // Convert to UTF16 since we want the results in utf16
484 std::unique_ptr<uint16_t[]> utf16;
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000485 auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000486 if (utf16Units < 0) {
487 return false;
488 }
489
490 return extractWords(utf16.get(), utf16Units, results);
491 }
492
493 bool getGraphemes(const char utf8[], int utf8Units, std::vector<Position>* results) override {
494
Julia Lavrova6e51d922020-08-25 11:42:51 -0400495 return extractPositions(utf8, utf8Units, BreakType::kGraphemes,
Julia Lavrova90787fe2020-07-20 17:32:03 +0000496 [results](int pos, int status) { results->emplace_back(pos);
497 });
498 }
499
Julia Lavrova6e51d922020-08-25 11:42:51 -0400500 void reorderVisual(const BidiLevel runLevels[],
501 int levelsCount,
502 int32_t logicalFromVisual[]) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000503 ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual);
504 }
505};
506
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000507std::unique_ptr<SkUnicode> SkUnicode::Make() {
508 #if defined(SK_USING_THIRD_PARTY_ICU)
509 if (!SkLoadICU()) {
John Stiles154bd1f2021-05-13 09:38:51 -0400510 static SkOnce once;
511 once([] { SkDEBUGF("SkLoadICU() failed!\n"); });
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000512 return nullptr;
513 }
514 #endif
515 return std::make_unique<SkUnicode_icu>();
516}