blob: 621be641019539f51b3564f5161d42ccd503b459 [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 Simmons504451c2021-01-07 11:10:01 -08008#include "include/private/SkMutex.h"
Julia Lavrova90787fe2020-07-20 17:32:03 +00009#include "include/private/SkTFitsIn.h"
Jason Simmons504451c2021-01-07 11:10:01 -080010#include "include/private/SkTHash.h"
Julia Lavrova90787fe2020-07-20 17:32:03 +000011#include "include/private/SkTemplates.h"
12#include "modules/skshaper/src/SkUnicode.h"
13#include "src/utils/SkUTF.h"
14#include <unicode/ubidi.h>
15#include <unicode/ubrk.h>
Julia Lavrova36700ef2020-08-25 11:42:51 -040016#include <unicode/uscript.h>
Julia Lavrovab6b7fff2020-09-11 13:59:49 +000017#include <unicode/ustring.h>
Julia Lavrova90787fe2020-07-20 17:32:03 +000018#include <unicode/utext.h>
19#include <unicode/utypes.h>
20#include <vector>
21#include <functional>
22
Julia Lavrovab6b7fff2020-09-11 13:59:49 +000023#if defined(SK_USING_THIRD_PARTY_ICU)
24#include "SkLoadICU.h"
25#endif
26
Julia Lavrova1798f4f2020-08-26 14:22:48 +000027using SkUnicodeBidi = std::unique_ptr<UBiDi, SkFunctionWrapper<decltype(ubidi_close), ubidi_close>>;
Julia Lavrova90787fe2020-07-20 17:32:03 +000028using ICUUText = std::unique_ptr<UText, SkFunctionWrapper<decltype(utext_close), utext_close>>;
29using ICUBreakIterator = std::unique_ptr<UBreakIterator, SkFunctionWrapper<decltype(ubrk_close), ubrk_close>>;
30
31/** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */
32static inline SkUnichar utf8_next(const char** ptr, const char* end) {
33 SkUnichar val = SkUTF::NextUTF8(ptr, end);
34 return val < 0 ? 0xFFFD : val;
35}
36
Jason Simmons504451c2021-01-07 11:10:01 -080037static UBreakIteratorType convertType(SkUnicode::BreakType type) {
38 switch (type) {
39 case SkUnicode::BreakType::kLines: return UBRK_LINE;
40 case SkUnicode::BreakType::kGraphemes: return UBRK_CHARACTER;
41 case SkUnicode::BreakType::kWords: return UBRK_WORD;
42 default:
43 return UBRK_CHARACTER;
44 }
45}
46
Julia Lavrova1798f4f2020-08-26 14:22:48 +000047class SkBidiIterator_icu : public SkBidiIterator {
48 SkUnicodeBidi fBidi;
49public:
50 explicit SkBidiIterator_icu(SkUnicodeBidi bidi) : fBidi(std::move(bidi)) {}
51 Position getLength() override { return ubidi_getLength(fBidi.get()); }
52 Level getLevelAt(Position pos) override { return ubidi_getLevelAt(fBidi.get(), pos); }
53
54 static std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t utf16[], int utf16Units, Direction dir) {
55 UErrorCode status = U_ZERO_ERROR;
56 SkUnicodeBidi bidi(ubidi_openSized(utf16Units, 0, &status));
57 if (U_FAILURE(status)) {
58 SkDEBUGF("Bidi error: %s", u_errorName(status));
59 return nullptr;
60 }
61 SkASSERT(bidi);
62 uint8_t bidiLevel = (dir == SkBidiIterator::kLTR) ? UBIDI_LTR : UBIDI_RTL;
63 // The required lifetime of utf16 isn't well documented.
64 // It appears it isn't used after ubidi_setPara except through ubidi_getText.
65 ubidi_setPara(bidi.get(), (const UChar*)utf16, utf16Units, bidiLevel, nullptr, &status);
66 if (U_FAILURE(status)) {
67 SkDEBUGF("Bidi error: %s", u_errorName(status));
68 return nullptr;
69 }
70 return std::unique_ptr<SkBidiIterator>(new SkBidiIterator_icu(std::move(bidi)));
71 }
72
73 // ICU bidi iterator works with utf16 but clients (Flutter for instance) may work with utf8
74 // This method allows the clients not to think about all these details
75 static std::unique_ptr<SkBidiIterator> makeBidiIterator(const char utf8[], int utf8Units, Direction dir) {
76 // Convert utf8 into utf16 since ubidi only accepts utf16
77 if (!SkTFitsIn<int32_t>(utf8Units)) {
78 SkDEBUGF("Bidi error: text too long");
79 return nullptr;
80 }
81
82 // Getting the length like this seems to always set U_BUFFER_OVERFLOW_ERROR
83 int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units);
84 if (utf16Units < 0) {
85 SkDEBUGF("Bidi error: Invalid utf8 input");
86 return nullptr;
87 }
88 std::unique_ptr<uint16_t[]> utf16(new uint16_t[utf16Units]);
89 SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16.get(), utf16Units, utf8, utf8Units);
90 SkASSERT(dstLen == utf16Units);
91
92 return makeBidiIterator(utf16.get(), utf16Units, dir);
93 }
94
95 // This method returns the final results only: a list of bidi regions
96 // (this is all SkParagraph really needs; SkShaper however uses the iterator itself)
97 static std::vector<Region> getBidiRegions(const char utf8[], int utf8Units, Direction dir) {
98
99 auto bidiIterator = makeBidiIterator(utf8, utf8Units, dir);
100 std::vector<Region> bidiRegions;
101 const char* start8 = utf8;
102 const char* end8 = utf8 + utf8Units;
103 SkBidiIterator::Level currentLevel = 0;
104
105 Position pos8 = 0;
106 Position pos16 = 0;
107 Position end16 = bidiIterator->getLength();
108 while (pos16 < end16) {
109 auto level = bidiIterator->getLevelAt(pos16);
110 if (pos16 == 0) {
111 currentLevel = level;
112 } else if (level != currentLevel) {
113 auto end = start8 - utf8;
114 bidiRegions.emplace_back(pos8, end, currentLevel);
115 currentLevel = level;
116 pos8 = end;
117 }
118 SkUnichar u = utf8_next(&start8, end8);
119 pos16 += SkUTF::ToUTF16(u);
120 }
121 auto end = start8 - utf8;
122 if (end != pos8) {
123 bidiRegions.emplace_back(pos8, end, currentLevel);
124 }
125 return bidiRegions;
126 }
127};
128
129void SkBidiIterator::ReorderVisual(const Level runLevels[], int levelsCount,
130 int32_t logicalFromVisual[]) {
131 ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual);
132}
Julia Lavrova90787fe2020-07-20 17:32:03 +0000133
Julia Lavrova6e51d922020-08-25 11:42:51 -0400134class SkBreakIterator_icu : public SkBreakIterator {
135 ICUBreakIterator fBreakIterator;
136 Position fLastResult;
137 public:
138 explicit SkBreakIterator_icu(ICUBreakIterator iter)
139 : fBreakIterator(std::move(iter)), fLastResult(0) {}
140 Position first() override
141 { return fLastResult = ubrk_first(fBreakIterator.get()); }
142 Position current() override
143 { return fLastResult = ubrk_current(fBreakIterator.get()); }
144 Position next() override
145 { return fLastResult = ubrk_next(fBreakIterator.get()); }
146 Position preceding(Position offset) override
147 { return fLastResult = ubrk_preceding(fBreakIterator.get(), offset); }
148 Position following(Position offset) override
149 { return fLastResult = ubrk_following(fBreakIterator.get(), offset);}
150 Status status() override { return ubrk_getRuleStatus(fBreakIterator.get()); }
151 bool isDone() override { return fLastResult == UBRK_DONE; }
152
153 bool setText(const char utftext8[], int utf8Units) override {
154 UErrorCode status = U_ZERO_ERROR;
Julia Lavrovaacace5d2020-12-16 11:48:48 -0500155 ICUUText text(utext_openUTF8(nullptr, &utftext8[0], utf8Units, &status));
Julia Lavrova6e51d922020-08-25 11:42:51 -0400156
157 if (U_FAILURE(status)) {
158 SkDEBUGF("Break error: %s", u_errorName(status));
159 return false;
160 }
161 SkASSERT(text);
162 ubrk_setUText(fBreakIterator.get(), text.get(), &status);
163 if (U_FAILURE(status)) {
164 SkDEBUGF("Break error: %s", u_errorName(status));
165 return false;
166 }
167 fLastResult = 0;
168 return true;
169 }
Jason Simmons504451c2021-01-07 11:10:01 -0800170};
Julia Lavrova6e51d922020-08-25 11:42:51 -0400171
Jason Simmons504451c2021-01-07 11:10:01 -0800172class SkIcuBreakIteratorCache {
173 SkTHashMap<SkUnicode::BreakType, ICUBreakIterator> fBreakCache;
174 SkMutex fBreakCacheMutex;
175
176 public:
177 static SkIcuBreakIteratorCache& get() {
178 static SkIcuBreakIteratorCache instance;
179 return instance;
Julia Lavrova6e51d922020-08-25 11:42:51 -0400180 }
181
Jason Simmons504451c2021-01-07 11:10:01 -0800182 ICUBreakIterator makeBreakIterator(SkUnicode::BreakType type) {
Julia Lavrova6e51d922020-08-25 11:42:51 -0400183 UErrorCode status = U_ZERO_ERROR;
Jason Simmons504451c2021-01-07 11:10:01 -0800184 ICUBreakIterator* cachedIterator;
185 {
186 SkAutoMutexExclusive lock(fBreakCacheMutex);
187 cachedIterator = fBreakCache.find(type);
188 if (!cachedIterator) {
189 ICUBreakIterator newIterator(ubrk_open(convertType(type), uloc_getDefault(), nullptr, 0, &status));
190 if (U_FAILURE(status)) {
191 SkDEBUGF("Break error: %s", u_errorName(status));
192 } else {
193 cachedIterator = fBreakCache.set(type, std::move(newIterator));
194 }
195 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400196 }
Jason Simmons504451c2021-01-07 11:10:01 -0800197 ICUBreakIterator iterator;
198 if (cachedIterator) {
199 iterator.reset(ubrk_safeClone(cachedIterator->get(), nullptr, nullptr, &status));
200 if (U_FAILURE(status)) {
201 SkDEBUGF("Break error: %s", u_errorName(status));
202 }
203 }
204 return iterator;
Julia Lavrova6e51d922020-08-25 11:42:51 -0400205 }
206};
207
Julia Lavrova36700ef2020-08-25 11:42:51 -0400208class SkScriptIterator_icu : public SkScriptIterator {
209 public:
210 bool getScript(SkUnichar u, ScriptID* script) override {
211 UErrorCode status = U_ZERO_ERROR;
212 UScriptCode scriptCode = uscript_getScript(u, &status);
213 if (U_FAILURE (status)) {
214 return false;
215 }
216 if (script) {
217 *script = (ScriptID)scriptCode;
218 }
219 return true;
220 }
221
222 static std::unique_ptr<SkScriptIterator> makeScriptIterator() {
223 return std::unique_ptr<SkScriptIterator>(new SkScriptIterator_icu());
224 }
225};
226
Julia Lavrova90787fe2020-07-20 17:32:03 +0000227class SkUnicode_icu : public SkUnicode {
Julia Lavrova6e51d922020-08-25 11:42:51 -0400228 static bool extractBidi(const char utf8[],
229 int utf8Units,
230 TextDirection dir,
231 std::vector<BidiRegion>* bidiRegions) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000232
233 // Convert to UTF16 since for now bidi iterator only operates on utf16
234 std::unique_ptr<uint16_t[]> utf16;
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000235 auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000236 if (utf16Units < 0) {
237 return false;
238 }
239
240 // Create bidi iterator
241 UErrorCode status = U_ZERO_ERROR;
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000242 SkUnicodeBidi bidi(ubidi_openSized(utf16Units, 0, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000243 if (U_FAILURE(status)) {
244 SkDEBUGF("Bidi error: %s", u_errorName(status));
245 return false;
246 }
247 SkASSERT(bidi);
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000248 uint8_t bidiLevel = (dir == TextDirection::kLTR) ? UBIDI_LTR : UBIDI_RTL;
Julia Lavrova90787fe2020-07-20 17:32:03 +0000249 // The required lifetime of utf16 isn't well documented.
250 // It appears it isn't used after ubidi_setPara except through ubidi_getText.
251 ubidi_setPara(bidi.get(), (const UChar*)utf16.get(), utf16Units, bidiLevel, nullptr, &status);
252 if (U_FAILURE(status)) {
253 SkDEBUGF("Bidi error: %s", u_errorName(status));
254 return false;
255 }
256
257 // Iterate through bidi regions and the result positions into utf8
258 const char* start8 = utf8;
259 const char* end8 = utf8 + utf8Units;
260 BidiLevel currentLevel = 0;
261
262 Position pos8 = 0;
263 Position pos16 = 0;
264 Position end16 = ubidi_getLength(bidi.get());
265 while (pos16 < end16) {
266 auto level = ubidi_getLevelAt(bidi.get(), pos16);
267 if (pos16 == 0) {
268 currentLevel = level;
269 } else if (level != currentLevel) {
270 Position end = start8 - utf8;
271 bidiRegions->emplace_back(pos8, end, currentLevel);
272 currentLevel = level;
273 pos8 = end;
274 }
275 SkUnichar u = utf8_next(&start8, end8);
276 pos16 += SkUTF::ToUTF16(u);
277 }
278 Position end = start8 - utf8;
279 if (end != pos8) {
280 bidiRegions->emplace_back(pos8, end, currentLevel);
281 }
282 return true;
283 }
284
285 static bool extractWords(uint16_t utf16[], int utf16Units, std::vector<Position>* words) {
286
287 UErrorCode status = U_ZERO_ERROR;
288
Jason Simmons504451c2021-01-07 11:10:01 -0800289 ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(BreakType::kWords);
290 if (!iterator) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000291 SkDEBUGF("Break error: %s", u_errorName(status));
292 return false;
293 }
294 SkASSERT(iterator);
295
Julia Lavrovaacace5d2020-12-16 11:48:48 -0500296 ICUUText utf16UText(utext_openUChars(nullptr, (UChar*)utf16, utf16Units, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000297 if (U_FAILURE(status)) {
298 SkDEBUGF("Break error: %s", u_errorName(status));
299 return false;
300 }
301
302 ubrk_setUText(iterator.get(), utf16UText.get(), &status);
303 if (U_FAILURE(status)) {
304 SkDEBUGF("Break error: %s", u_errorName(status));
305 return false;
306 }
307
308 // Get the words
309 int32_t pos = ubrk_first(iterator.get());
310 while (pos != UBRK_DONE) {
311 words->emplace_back(pos);
312 pos = ubrk_next(iterator.get());
313 }
314
315 return true;
316 }
317
Julia Lavrova6e51d922020-08-25 11:42:51 -0400318 static bool extractPositions
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500319 (const char utf8[], int utf8Units, BreakType type, std::function<void(int, int)> setBreak) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000320
321 UErrorCode status = U_ZERO_ERROR;
Julia Lavrovaacace5d2020-12-16 11:48:48 -0500322 ICUUText text(utext_openUTF8(nullptr, &utf8[0], utf8Units, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000323
324 if (U_FAILURE(status)) {
325 SkDEBUGF("Break error: %s", u_errorName(status));
326 return false;
327 }
328 SkASSERT(text);
329
Jason Simmons504451c2021-01-07 11:10:01 -0800330 ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(type);
331 if (!iterator) {
332 return false;
Julia Lavrova90787fe2020-07-20 17:32:03 +0000333 }
334
335 ubrk_setUText(iterator.get(), text.get(), &status);
336 if (U_FAILURE(status)) {
337 SkDEBUGF("Break error: %s", u_errorName(status));
338 return false;
339 }
340
341 auto iter = iterator.get();
342 int32_t pos = ubrk_first(iter);
343 while (pos != UBRK_DONE) {
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500344 auto status = type == SkUnicode::BreakType::kLines
345 ? UBRK_LINE_SOFT
346 : ubrk_getRuleStatus(iter);
347 setBreak(pos, status);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000348 pos = ubrk_next(iter);
349 }
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500350
351 if (type == SkUnicode::BreakType::kLines) {
352 // This is a workaround for https://bugs.chromium.org/p/skia/issues/detail?id=10715
353 // (ICU line break iterator does not work correctly on Thai text with new lines)
354 // So, we only use the iterator to collect soft line breaks and
355 // scan the text for all hard line breaks ourselves
356 const char* end = utf8 + utf8Units;
357 const char* ch = utf8;
358 while (ch < end) {
359 auto unichar = utf8_next(&ch, end);
360 if (isHardLineBreak(unichar)) {
361 setBreak(ch - utf8, UBRK_LINE_HARD);
362 }
363 }
364 }
Julia Lavrova90787fe2020-07-20 17:32:03 +0000365 return true;
366 }
367
Julia Lavrova6e51d922020-08-25 11:42:51 -0400368 static bool extractWhitespaces(const char utf8[],
369 int utf8Units,
370 std::vector<Position>* whitespaces) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000371
372 const char* start = utf8;
373 const char* end = utf8 + utf8Units;
374 const char* ch = start;
375 while (ch < end) {
376 auto index = ch - start;
377 auto unichar = utf8_next(&ch, end);
378 if (u_isWhitespace(unichar)) {
379 auto ending = ch - start;
380 for (auto k = index; k < ending; ++k) {
381 whitespaces->emplace_back(k);
382 }
383 }
384 }
385 return true;
386 }
387
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000388 static int utf8ToUtf16(const char* utf8, size_t utf8Units, std::unique_ptr<uint16_t[]>* utf16) {
389 int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units);
390 if (utf16Units < 0) {
391 SkDEBUGF("Convert error: Invalid utf8 input");
392 return utf16Units;
393 }
394 *utf16 = std::unique_ptr<uint16_t[]>(new uint16_t[utf16Units]);
395 SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16->get(), utf16Units, utf8, utf8Units);
396 SkASSERT(dstLen == utf16Units);
397 return utf16Units;
398 }
399
400 static int utf16ToUtf8(const uint16_t* utf16, size_t utf16Units, std::unique_ptr<char[]>* utf8) {
401 int utf8Units = SkUTF::UTF16ToUTF8(nullptr, 0, utf16, utf16Units);
402 if (utf8Units < 0) {
403 SkDEBUGF("Convert error: Invalid utf16 input");
404 return utf8Units;
405 }
406 *utf8 = std::unique_ptr<char[]>(new char[utf8Units]);
407 SkDEBUGCODE(int dstLen =) SkUTF::UTF16ToUTF8(utf8->get(), utf8Units, utf16, utf16Units);
408 SkASSERT(dstLen == utf8Units);
409 return utf8Units;
410 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400411
Julia Lavrova90787fe2020-07-20 17:32:03 +0000412public:
413 ~SkUnicode_icu() override { }
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000414 std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t text[], int count,
415 SkBidiIterator::Direction dir) override {
416 return SkBidiIterator_icu::makeBidiIterator(text, count, dir);
417 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400418 std::unique_ptr<SkBidiIterator> makeBidiIterator(const char text[],
419 int count,
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000420 SkBidiIterator::Direction dir) override {
421 return SkBidiIterator_icu::makeBidiIterator(text, count, dir);
422 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400423 std::unique_ptr<SkBreakIterator> makeBreakIterator(const char locale[],
424 BreakType breakType) override {
Jason Simmons504451c2021-01-07 11:10:01 -0800425 UErrorCode status = U_ZERO_ERROR;
426 ICUBreakIterator iterator(ubrk_open(convertType(breakType), locale, nullptr, 0, &status));
427 if (U_FAILURE(status)) {
428 SkDEBUGF("Break error: %s", u_errorName(status));
429 return nullptr;
430 }
431 return std::unique_ptr<SkBreakIterator>(new SkBreakIterator_icu(std::move(iterator)));
Julia Lavrova6e51d922020-08-25 11:42:51 -0400432 }
Julia Lavrova36700ef2020-08-25 11:42:51 -0400433 std::unique_ptr<SkScriptIterator> makeScriptIterator() override {
434 return SkScriptIterator_icu::makeScriptIterator();
435 }
Julia Lavrova90787fe2020-07-20 17:32:03 +0000436
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000437 // TODO: Use ICU data file to detect controls and whitespaces
438 bool isControl(SkUnichar utf8) override {
439 return u_iscntrl(utf8);
440 }
441
442 bool isWhitespace(SkUnichar utf8) override {
443 return u_isWhitespace(utf8);
444 }
445
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500446 static bool isHardLineBreak(SkUnichar utf8) {
447 auto property = u_getIntPropertyValue(utf8, UCHAR_LINE_BREAK);
448 return property == U_LB_LINE_FEED || property == U_LB_MANDATORY_BREAK;
449 }
450
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000451 SkString convertUtf16ToUtf8(const std::u16string& utf16) override {
452 std::unique_ptr<char[]> utf8;
453 auto utf8Units = SkUnicode_icu::utf16ToUtf8((uint16_t*)utf16.data(), utf16.size(), &utf8);
454 if (utf8Units >= 0) {
455 return SkString(utf8.get(), utf8Units);
456 } else {
457 return SkString();
458 }
459 }
460
Julia Lavrova6e51d922020-08-25 11:42:51 -0400461 bool getBidiRegions(const char utf8[],
462 int utf8Units,
463 TextDirection dir,
464 std::vector<BidiRegion>* results) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000465 return extractBidi(utf8, utf8Units, dir, results);
466 }
467
Julia Lavrova6e51d922020-08-25 11:42:51 -0400468 bool getLineBreaks(const char utf8[],
469 int utf8Units,
470 std::vector<LineBreakBefore>* results) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000471
Julia Lavrova6e51d922020-08-25 11:42:51 -0400472 return extractPositions(utf8, utf8Units, BreakType::kLines,
Julia Lavrova90787fe2020-07-20 17:32:03 +0000473 [results](int pos, int status) {
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500474 results->emplace_back(pos, status == UBRK_LINE_HARD
Julia Lavrova90787fe2020-07-20 17:32:03 +0000475 ? LineBreakType::kHardLineBreak
476 : LineBreakType::kSoftLineBreak);
477 });
478 }
479
480 bool getWords(const char utf8[], int utf8Units, std::vector<Position>* results) override {
481
482 // Convert to UTF16 since we want the results in utf16
483 std::unique_ptr<uint16_t[]> utf16;
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000484 auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000485 if (utf16Units < 0) {
486 return false;
487 }
488
489 return extractWords(utf16.get(), utf16Units, results);
490 }
491
492 bool getGraphemes(const char utf8[], int utf8Units, std::vector<Position>* results) override {
493
Julia Lavrova6e51d922020-08-25 11:42:51 -0400494 return extractPositions(utf8, utf8Units, BreakType::kGraphemes,
Julia Lavrova90787fe2020-07-20 17:32:03 +0000495 [results](int pos, int status) { results->emplace_back(pos);
496 });
497 }
498
499 bool getWhitespaces(const char utf8[], int utf8Units, std::vector<Position>* results) override {
500
501 return extractWhitespaces(utf8, utf8Units, results);
502 }
503
Julia Lavrova6e51d922020-08-25 11:42:51 -0400504 void reorderVisual(const BidiLevel runLevels[],
505 int levelsCount,
506 int32_t logicalFromVisual[]) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000507 ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual);
508 }
509};
510
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000511std::unique_ptr<SkUnicode> SkUnicode::Make() {
512 #if defined(SK_USING_THIRD_PARTY_ICU)
513 if (!SkLoadICU()) {
514 SkDEBUGF("SkLoadICU() failed!\n");
515 return nullptr;
516 }
517 #endif
518 return std::make_unique<SkUnicode_icu>();
519}