blob: 66185b624d75b617e52286863f201ef9a4cfbdba [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"
Julia Lavrova90787fe2020-07-20 17:32:03 +00008#include "include/private/SkTFitsIn.h"
9#include "include/private/SkTemplates.h"
10#include "modules/skshaper/src/SkUnicode.h"
11#include "src/utils/SkUTF.h"
12#include <unicode/ubidi.h>
13#include <unicode/ubrk.h>
Julia Lavrova36700ef2020-08-25 11:42:51 -040014#include <unicode/uscript.h>
Julia Lavrovab6b7fff2020-09-11 13:59:49 +000015#include <unicode/ustring.h>
Julia Lavrova90787fe2020-07-20 17:32:03 +000016#include <unicode/utext.h>
17#include <unicode/utypes.h>
18#include <vector>
19#include <functional>
20
Julia Lavrovab6b7fff2020-09-11 13:59:49 +000021#if defined(SK_USING_THIRD_PARTY_ICU)
22#include "SkLoadICU.h"
23#endif
24
Julia Lavrova1798f4f2020-08-26 14:22:48 +000025using SkUnicodeBidi = std::unique_ptr<UBiDi, SkFunctionWrapper<decltype(ubidi_close), ubidi_close>>;
Julia Lavrova90787fe2020-07-20 17:32:03 +000026using ICUUText = std::unique_ptr<UText, SkFunctionWrapper<decltype(utext_close), utext_close>>;
27using ICUBreakIterator = std::unique_ptr<UBreakIterator, SkFunctionWrapper<decltype(ubrk_close), ubrk_close>>;
28
29/** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */
30static inline SkUnichar utf8_next(const char** ptr, const char* end) {
31 SkUnichar val = SkUTF::NextUTF8(ptr, end);
32 return val < 0 ? 0xFFFD : val;
33}
34
Julia Lavrova1798f4f2020-08-26 14:22:48 +000035class SkBidiIterator_icu : public SkBidiIterator {
36 SkUnicodeBidi fBidi;
37public:
38 explicit SkBidiIterator_icu(SkUnicodeBidi bidi) : fBidi(std::move(bidi)) {}
39 Position getLength() override { return ubidi_getLength(fBidi.get()); }
40 Level getLevelAt(Position pos) override { return ubidi_getLevelAt(fBidi.get(), pos); }
41
42 static std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t utf16[], int utf16Units, Direction dir) {
43 UErrorCode status = U_ZERO_ERROR;
44 SkUnicodeBidi bidi(ubidi_openSized(utf16Units, 0, &status));
45 if (U_FAILURE(status)) {
46 SkDEBUGF("Bidi error: %s", u_errorName(status));
47 return nullptr;
48 }
49 SkASSERT(bidi);
50 uint8_t bidiLevel = (dir == SkBidiIterator::kLTR) ? UBIDI_LTR : UBIDI_RTL;
51 // The required lifetime of utf16 isn't well documented.
52 // It appears it isn't used after ubidi_setPara except through ubidi_getText.
53 ubidi_setPara(bidi.get(), (const UChar*)utf16, utf16Units, bidiLevel, nullptr, &status);
54 if (U_FAILURE(status)) {
55 SkDEBUGF("Bidi error: %s", u_errorName(status));
56 return nullptr;
57 }
58 return std::unique_ptr<SkBidiIterator>(new SkBidiIterator_icu(std::move(bidi)));
59 }
60
61 // ICU bidi iterator works with utf16 but clients (Flutter for instance) may work with utf8
62 // This method allows the clients not to think about all these details
63 static std::unique_ptr<SkBidiIterator> makeBidiIterator(const char utf8[], int utf8Units, Direction dir) {
64 // Convert utf8 into utf16 since ubidi only accepts utf16
65 if (!SkTFitsIn<int32_t>(utf8Units)) {
66 SkDEBUGF("Bidi error: text too long");
67 return nullptr;
68 }
69
70 // Getting the length like this seems to always set U_BUFFER_OVERFLOW_ERROR
71 int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units);
72 if (utf16Units < 0) {
73 SkDEBUGF("Bidi error: Invalid utf8 input");
74 return nullptr;
75 }
76 std::unique_ptr<uint16_t[]> utf16(new uint16_t[utf16Units]);
77 SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16.get(), utf16Units, utf8, utf8Units);
78 SkASSERT(dstLen == utf16Units);
79
80 return makeBidiIterator(utf16.get(), utf16Units, dir);
81 }
82
83 // This method returns the final results only: a list of bidi regions
84 // (this is all SkParagraph really needs; SkShaper however uses the iterator itself)
85 static std::vector<Region> getBidiRegions(const char utf8[], int utf8Units, Direction dir) {
86
87 auto bidiIterator = makeBidiIterator(utf8, utf8Units, dir);
88 std::vector<Region> bidiRegions;
89 const char* start8 = utf8;
90 const char* end8 = utf8 + utf8Units;
91 SkBidiIterator::Level currentLevel = 0;
92
93 Position pos8 = 0;
94 Position pos16 = 0;
95 Position end16 = bidiIterator->getLength();
96 while (pos16 < end16) {
97 auto level = bidiIterator->getLevelAt(pos16);
98 if (pos16 == 0) {
99 currentLevel = level;
100 } else if (level != currentLevel) {
101 auto end = start8 - utf8;
102 bidiRegions.emplace_back(pos8, end, currentLevel);
103 currentLevel = level;
104 pos8 = end;
105 }
106 SkUnichar u = utf8_next(&start8, end8);
107 pos16 += SkUTF::ToUTF16(u);
108 }
109 auto end = start8 - utf8;
110 if (end != pos8) {
111 bidiRegions.emplace_back(pos8, end, currentLevel);
112 }
113 return bidiRegions;
114 }
115};
116
117void SkBidiIterator::ReorderVisual(const Level runLevels[], int levelsCount,
118 int32_t logicalFromVisual[]) {
119 ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual);
120}
Julia Lavrova90787fe2020-07-20 17:32:03 +0000121
Julia Lavrova6e51d922020-08-25 11:42:51 -0400122class SkBreakIterator_icu : public SkBreakIterator {
123 ICUBreakIterator fBreakIterator;
124 Position fLastResult;
125 public:
126 explicit SkBreakIterator_icu(ICUBreakIterator iter)
127 : fBreakIterator(std::move(iter)), fLastResult(0) {}
128 Position first() override
129 { return fLastResult = ubrk_first(fBreakIterator.get()); }
130 Position current() override
131 { return fLastResult = ubrk_current(fBreakIterator.get()); }
132 Position next() override
133 { return fLastResult = ubrk_next(fBreakIterator.get()); }
134 Position preceding(Position offset) override
135 { return fLastResult = ubrk_preceding(fBreakIterator.get(), offset); }
136 Position following(Position offset) override
137 { return fLastResult = ubrk_following(fBreakIterator.get(), offset);}
138 Status status() override { return ubrk_getRuleStatus(fBreakIterator.get()); }
139 bool isDone() override { return fLastResult == UBRK_DONE; }
140
141 bool setText(const char utftext8[], int utf8Units) override {
142 UErrorCode status = U_ZERO_ERROR;
Julia Lavrovaacace5d2020-12-16 11:48:48 -0500143 ICUUText text(utext_openUTF8(nullptr, &utftext8[0], utf8Units, &status));
Julia Lavrova6e51d922020-08-25 11:42:51 -0400144
145 if (U_FAILURE(status)) {
146 SkDEBUGF("Break error: %s", u_errorName(status));
147 return false;
148 }
149 SkASSERT(text);
150 ubrk_setUText(fBreakIterator.get(), text.get(), &status);
151 if (U_FAILURE(status)) {
152 SkDEBUGF("Break error: %s", u_errorName(status));
153 return false;
154 }
155 fLastResult = 0;
156 return true;
157 }
158
Mike Reedf4ea3052021-01-08 02:27:50 +0000159 static UBreakIteratorType convertType(SkUnicode::BreakType type) {
160 switch (type) {
161 case SkUnicode::BreakType::kLines: return UBRK_LINE;
162 case SkUnicode::BreakType::kGraphemes: return UBRK_CHARACTER;
163 case SkUnicode::BreakType::kWords: return UBRK_WORD;
164 default:
165 return UBRK_CHARACTER;
166 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400167 }
168
Mike Reedf4ea3052021-01-08 02:27:50 +0000169 static std::unique_ptr<SkBreakIterator> makeUtf8BreakIterator
170 (const char locale[], SkUnicode::BreakType type) {
Julia Lavrova6e51d922020-08-25 11:42:51 -0400171 UErrorCode status = U_ZERO_ERROR;
Mike Reedf4ea3052021-01-08 02:27:50 +0000172 ICUBreakIterator iterator(ubrk_open(convertType(type), locale, nullptr, 0, &status));
173 if (U_FAILURE(status)) {
174 SkDEBUGF("Break error: %s", u_errorName(status));
175 return nullptr;
Julia Lavrova6e51d922020-08-25 11:42:51 -0400176 }
Mike Reedf4ea3052021-01-08 02:27:50 +0000177 return std::unique_ptr<SkBreakIterator>(new SkBreakIterator_icu(std::move(iterator)));
Julia Lavrova6e51d922020-08-25 11:42:51 -0400178 }
179};
180
Julia Lavrova36700ef2020-08-25 11:42:51 -0400181class SkScriptIterator_icu : public SkScriptIterator {
182 public:
183 bool getScript(SkUnichar u, ScriptID* script) override {
184 UErrorCode status = U_ZERO_ERROR;
185 UScriptCode scriptCode = uscript_getScript(u, &status);
186 if (U_FAILURE (status)) {
187 return false;
188 }
189 if (script) {
190 *script = (ScriptID)scriptCode;
191 }
192 return true;
193 }
194
195 static std::unique_ptr<SkScriptIterator> makeScriptIterator() {
196 return std::unique_ptr<SkScriptIterator>(new SkScriptIterator_icu());
197 }
198};
199
Julia Lavrova90787fe2020-07-20 17:32:03 +0000200class SkUnicode_icu : public SkUnicode {
Mike Reedf4ea3052021-01-08 02:27:50 +0000201
202 static UBreakIteratorType convertType(BreakType type) {
203 switch (type) {
204 case BreakType::kLines: return UBRK_LINE;
205 case BreakType::kGraphemes: return UBRK_CHARACTER;
206 case BreakType::kWords: return UBRK_WORD;
207 default:
208 SkDEBUGF("Convert error: wrong break type");
209 return UBRK_CHARACTER;
210 }
211 }
212
Julia Lavrova6e51d922020-08-25 11:42:51 -0400213 static bool extractBidi(const char utf8[],
214 int utf8Units,
215 TextDirection dir,
216 std::vector<BidiRegion>* bidiRegions) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000217
218 // Convert to UTF16 since for now bidi iterator only operates on utf16
219 std::unique_ptr<uint16_t[]> utf16;
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000220 auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000221 if (utf16Units < 0) {
222 return false;
223 }
224
225 // Create bidi iterator
226 UErrorCode status = U_ZERO_ERROR;
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000227 SkUnicodeBidi bidi(ubidi_openSized(utf16Units, 0, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000228 if (U_FAILURE(status)) {
229 SkDEBUGF("Bidi error: %s", u_errorName(status));
230 return false;
231 }
232 SkASSERT(bidi);
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000233 uint8_t bidiLevel = (dir == TextDirection::kLTR) ? UBIDI_LTR : UBIDI_RTL;
Julia Lavrova90787fe2020-07-20 17:32:03 +0000234 // The required lifetime of utf16 isn't well documented.
235 // It appears it isn't used after ubidi_setPara except through ubidi_getText.
236 ubidi_setPara(bidi.get(), (const UChar*)utf16.get(), utf16Units, bidiLevel, nullptr, &status);
237 if (U_FAILURE(status)) {
238 SkDEBUGF("Bidi error: %s", u_errorName(status));
239 return false;
240 }
241
242 // Iterate through bidi regions and the result positions into utf8
243 const char* start8 = utf8;
244 const char* end8 = utf8 + utf8Units;
245 BidiLevel currentLevel = 0;
246
247 Position pos8 = 0;
248 Position pos16 = 0;
249 Position end16 = ubidi_getLength(bidi.get());
250 while (pos16 < end16) {
251 auto level = ubidi_getLevelAt(bidi.get(), pos16);
252 if (pos16 == 0) {
253 currentLevel = level;
254 } else if (level != currentLevel) {
255 Position end = start8 - utf8;
256 bidiRegions->emplace_back(pos8, end, currentLevel);
257 currentLevel = level;
258 pos8 = end;
259 }
260 SkUnichar u = utf8_next(&start8, end8);
261 pos16 += SkUTF::ToUTF16(u);
262 }
263 Position end = start8 - utf8;
264 if (end != pos8) {
265 bidiRegions->emplace_back(pos8, end, currentLevel);
266 }
267 return true;
268 }
269
270 static bool extractWords(uint16_t utf16[], int utf16Units, std::vector<Position>* words) {
271
272 UErrorCode status = U_ZERO_ERROR;
273
Mike Reedf4ea3052021-01-08 02:27:50 +0000274 UBreakIteratorType breakType = convertType(BreakType::kWords);
275 ICUBreakIterator iterator(ubrk_open(breakType, uloc_getDefault(), nullptr, 0, &status));
276 if (U_FAILURE(status)) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000277 SkDEBUGF("Break error: %s", u_errorName(status));
278 return false;
279 }
280 SkASSERT(iterator);
281
Julia Lavrovaacace5d2020-12-16 11:48:48 -0500282 ICUUText utf16UText(utext_openUChars(nullptr, (UChar*)utf16, utf16Units, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000283 if (U_FAILURE(status)) {
284 SkDEBUGF("Break error: %s", u_errorName(status));
285 return false;
286 }
287
288 ubrk_setUText(iterator.get(), utf16UText.get(), &status);
289 if (U_FAILURE(status)) {
290 SkDEBUGF("Break error: %s", u_errorName(status));
291 return false;
292 }
293
294 // Get the words
295 int32_t pos = ubrk_first(iterator.get());
296 while (pos != UBRK_DONE) {
297 words->emplace_back(pos);
298 pos = ubrk_next(iterator.get());
299 }
300
301 return true;
302 }
303
Julia Lavrova6e51d922020-08-25 11:42:51 -0400304 static bool extractPositions
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500305 (const char utf8[], int utf8Units, BreakType type, std::function<void(int, int)> setBreak) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000306
307 UErrorCode status = U_ZERO_ERROR;
Julia Lavrovaacace5d2020-12-16 11:48:48 -0500308 ICUUText text(utext_openUTF8(nullptr, &utf8[0], utf8Units, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000309
310 if (U_FAILURE(status)) {
311 SkDEBUGF("Break error: %s", u_errorName(status));
312 return false;
313 }
314 SkASSERT(text);
315
Mike Reedf4ea3052021-01-08 02:27:50 +0000316 ICUBreakIterator iterator(ubrk_open(convertType(type), uloc_getDefault(), nullptr, 0, &status));
317 if (U_FAILURE(status)) {
318 SkDEBUGF("Break error: %s", u_errorName(status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000319 }
320
321 ubrk_setUText(iterator.get(), text.get(), &status);
322 if (U_FAILURE(status)) {
323 SkDEBUGF("Break error: %s", u_errorName(status));
324 return false;
325 }
326
327 auto iter = iterator.get();
328 int32_t pos = ubrk_first(iter);
329 while (pos != UBRK_DONE) {
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500330 auto status = type == SkUnicode::BreakType::kLines
331 ? UBRK_LINE_SOFT
332 : ubrk_getRuleStatus(iter);
333 setBreak(pos, status);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000334 pos = ubrk_next(iter);
335 }
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500336
337 if (type == SkUnicode::BreakType::kLines) {
338 // This is a workaround for https://bugs.chromium.org/p/skia/issues/detail?id=10715
339 // (ICU line break iterator does not work correctly on Thai text with new lines)
340 // So, we only use the iterator to collect soft line breaks and
341 // scan the text for all hard line breaks ourselves
342 const char* end = utf8 + utf8Units;
343 const char* ch = utf8;
344 while (ch < end) {
345 auto unichar = utf8_next(&ch, end);
346 if (isHardLineBreak(unichar)) {
347 setBreak(ch - utf8, UBRK_LINE_HARD);
348 }
349 }
350 }
Julia Lavrova90787fe2020-07-20 17:32:03 +0000351 return true;
352 }
353
Julia Lavrova6e51d922020-08-25 11:42:51 -0400354 static bool extractWhitespaces(const char utf8[],
355 int utf8Units,
356 std::vector<Position>* whitespaces) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000357
358 const char* start = utf8;
359 const char* end = utf8 + utf8Units;
360 const char* ch = start;
361 while (ch < end) {
362 auto index = ch - start;
363 auto unichar = utf8_next(&ch, end);
364 if (u_isWhitespace(unichar)) {
365 auto ending = ch - start;
366 for (auto k = index; k < ending; ++k) {
367 whitespaces->emplace_back(k);
368 }
369 }
370 }
371 return true;
372 }
373
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000374 static int utf8ToUtf16(const char* utf8, size_t utf8Units, std::unique_ptr<uint16_t[]>* utf16) {
375 int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units);
376 if (utf16Units < 0) {
377 SkDEBUGF("Convert error: Invalid utf8 input");
378 return utf16Units;
379 }
380 *utf16 = std::unique_ptr<uint16_t[]>(new uint16_t[utf16Units]);
381 SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16->get(), utf16Units, utf8, utf8Units);
382 SkASSERT(dstLen == utf16Units);
383 return utf16Units;
384 }
385
386 static int utf16ToUtf8(const uint16_t* utf16, size_t utf16Units, std::unique_ptr<char[]>* utf8) {
387 int utf8Units = SkUTF::UTF16ToUTF8(nullptr, 0, utf16, utf16Units);
388 if (utf8Units < 0) {
389 SkDEBUGF("Convert error: Invalid utf16 input");
390 return utf8Units;
391 }
392 *utf8 = std::unique_ptr<char[]>(new char[utf8Units]);
393 SkDEBUGCODE(int dstLen =) SkUTF::UTF16ToUTF8(utf8->get(), utf8Units, utf16, utf16Units);
394 SkASSERT(dstLen == utf8Units);
395 return utf8Units;
396 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400397
Julia Lavrova90787fe2020-07-20 17:32:03 +0000398public:
399 ~SkUnicode_icu() override { }
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000400 std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t text[], int count,
401 SkBidiIterator::Direction dir) override {
402 return SkBidiIterator_icu::makeBidiIterator(text, count, dir);
403 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400404 std::unique_ptr<SkBidiIterator> makeBidiIterator(const char text[],
405 int count,
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000406 SkBidiIterator::Direction dir) override {
407 return SkBidiIterator_icu::makeBidiIterator(text, count, dir);
408 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400409 std::unique_ptr<SkBreakIterator> makeBreakIterator(const char locale[],
410 BreakType breakType) override {
Mike Reedf4ea3052021-01-08 02:27:50 +0000411 return SkBreakIterator_icu::makeUtf8BreakIterator(locale, breakType);
Julia Lavrova6e51d922020-08-25 11:42:51 -0400412 }
Julia Lavrova36700ef2020-08-25 11:42:51 -0400413 std::unique_ptr<SkScriptIterator> makeScriptIterator() override {
414 return SkScriptIterator_icu::makeScriptIterator();
415 }
Julia Lavrova90787fe2020-07-20 17:32:03 +0000416
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000417 // TODO: Use ICU data file to detect controls and whitespaces
418 bool isControl(SkUnichar utf8) override {
419 return u_iscntrl(utf8);
420 }
421
422 bool isWhitespace(SkUnichar utf8) override {
423 return u_isWhitespace(utf8);
424 }
425
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500426 static bool isHardLineBreak(SkUnichar utf8) {
427 auto property = u_getIntPropertyValue(utf8, UCHAR_LINE_BREAK);
428 return property == U_LB_LINE_FEED || property == U_LB_MANDATORY_BREAK;
429 }
430
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000431 SkString convertUtf16ToUtf8(const std::u16string& utf16) override {
432 std::unique_ptr<char[]> utf8;
433 auto utf8Units = SkUnicode_icu::utf16ToUtf8((uint16_t*)utf16.data(), utf16.size(), &utf8);
434 if (utf8Units >= 0) {
435 return SkString(utf8.get(), utf8Units);
436 } else {
437 return SkString();
438 }
439 }
440
Julia Lavrova6e51d922020-08-25 11:42:51 -0400441 bool getBidiRegions(const char utf8[],
442 int utf8Units,
443 TextDirection dir,
444 std::vector<BidiRegion>* results) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000445 return extractBidi(utf8, utf8Units, dir, results);
446 }
447
Julia Lavrova6e51d922020-08-25 11:42:51 -0400448 bool getLineBreaks(const char utf8[],
449 int utf8Units,
450 std::vector<LineBreakBefore>* results) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000451
Julia Lavrova6e51d922020-08-25 11:42:51 -0400452 return extractPositions(utf8, utf8Units, BreakType::kLines,
Julia Lavrova90787fe2020-07-20 17:32:03 +0000453 [results](int pos, int status) {
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500454 results->emplace_back(pos, status == UBRK_LINE_HARD
Julia Lavrova90787fe2020-07-20 17:32:03 +0000455 ? LineBreakType::kHardLineBreak
456 : LineBreakType::kSoftLineBreak);
457 });
458 }
459
460 bool getWords(const char utf8[], int utf8Units, std::vector<Position>* results) override {
461
462 // Convert to UTF16 since we want the results in utf16
463 std::unique_ptr<uint16_t[]> utf16;
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000464 auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000465 if (utf16Units < 0) {
466 return false;
467 }
468
469 return extractWords(utf16.get(), utf16Units, results);
470 }
471
472 bool getGraphemes(const char utf8[], int utf8Units, std::vector<Position>* results) override {
473
Julia Lavrova6e51d922020-08-25 11:42:51 -0400474 return extractPositions(utf8, utf8Units, BreakType::kGraphemes,
Julia Lavrova90787fe2020-07-20 17:32:03 +0000475 [results](int pos, int status) { results->emplace_back(pos);
476 });
477 }
478
479 bool getWhitespaces(const char utf8[], int utf8Units, std::vector<Position>* results) override {
480
481 return extractWhitespaces(utf8, utf8Units, results);
482 }
483
Julia Lavrova6e51d922020-08-25 11:42:51 -0400484 void reorderVisual(const BidiLevel runLevels[],
485 int levelsCount,
486 int32_t logicalFromVisual[]) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000487 ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual);
488 }
489};
490
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000491std::unique_ptr<SkUnicode> SkUnicode::Make() {
492 #if defined(SK_USING_THIRD_PARTY_ICU)
493 if (!SkLoadICU()) {
494 SkDEBUGF("SkLoadICU() failed!\n");
495 return nullptr;
496 }
497 #endif
498 return std::make_unique<SkUnicode_icu>();
499}