blob: 99bb55b53df0384e55c360fc27be06db335d1b10 [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"
Julia Lavrova90787fe2020-07-20 17:32:03 +00009#include "include/private/SkTFitsIn.h"
Jason Simmonsd8e94362021-01-11 15:52:03 -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 Simmonsd8e94362021-01-11 15:52:03 -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 Simmonsd8e94362021-01-11 15:52:03 -0800170};
Julia Lavrova6e51d922020-08-25 11:42:51 -0400171
Jason Simmonsd8e94362021-01-11 15:52:03 -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 Simmonsd8e94362021-01-11 15:52:03 -0800182#ifdef SK_ENABLE_ICU_UBRK_SAFECLONE
183
184 ICUBreakIterator makeBreakIterator(SkUnicode::BreakType type) {
Julia Lavrova6e51d922020-08-25 11:42:51 -0400185 UErrorCode status = U_ZERO_ERROR;
Jason Simmonsd8e94362021-01-11 15:52:03 -0800186 ICUBreakIterator* cachedIterator;
187 {
188 SkAutoMutexExclusive lock(fBreakCacheMutex);
189 cachedIterator = fBreakCache.find(type);
190 if (!cachedIterator) {
191 ICUBreakIterator newIterator(ubrk_open(convertType(type), uloc_getDefault(), nullptr, 0, &status));
192 if (U_FAILURE(status)) {
193 SkDEBUGF("Break error: %s", u_errorName(status));
194 } else {
195 cachedIterator = fBreakCache.set(type, std::move(newIterator));
196 }
197 }
198 }
199 ICUBreakIterator iterator;
200 if (cachedIterator) {
201 iterator.reset(ubrk_safeClone(cachedIterator->get(), nullptr, nullptr, &status));
202 if (U_FAILURE(status)) {
203 SkDEBUGF("Break error: %s", u_errorName(status));
204 }
205 }
206 return iterator;
207 }
208
209#else // SK_ENABLE_ICU_UBRK_SAFECLONE
210
211 ICUBreakIterator makeBreakIterator(SkUnicode::BreakType type) {
212 UErrorCode status = U_ZERO_ERROR;
213 ICUBreakIterator iterator(ubrk_open(convertType(type), uloc_getDefault(), nullptr, 0, &status));
Mike Reedf4ea3052021-01-08 02:27:50 +0000214 if (U_FAILURE(status)) {
215 SkDEBUGF("Break error: %s", u_errorName(status));
Julia Lavrova6e51d922020-08-25 11:42:51 -0400216 }
Jason Simmonsd8e94362021-01-11 15:52:03 -0800217 return iterator;
Julia Lavrova6e51d922020-08-25 11:42:51 -0400218 }
Jason Simmonsd8e94362021-01-11 15:52:03 -0800219
220#endif // SK_ENABLE_ICU_UBRK_SAFECLONE
221
Julia Lavrova6e51d922020-08-25 11:42:51 -0400222};
223
Julia Lavrova36700ef2020-08-25 11:42:51 -0400224class SkScriptIterator_icu : public SkScriptIterator {
225 public:
226 bool getScript(SkUnichar u, ScriptID* script) override {
227 UErrorCode status = U_ZERO_ERROR;
228 UScriptCode scriptCode = uscript_getScript(u, &status);
229 if (U_FAILURE (status)) {
230 return false;
231 }
232 if (script) {
233 *script = (ScriptID)scriptCode;
234 }
235 return true;
236 }
237
238 static std::unique_ptr<SkScriptIterator> makeScriptIterator() {
239 return std::unique_ptr<SkScriptIterator>(new SkScriptIterator_icu());
240 }
241};
242
Julia Lavrova90787fe2020-07-20 17:32:03 +0000243class SkUnicode_icu : public SkUnicode {
Julia Lavrova6e51d922020-08-25 11:42:51 -0400244 static bool extractBidi(const char utf8[],
245 int utf8Units,
246 TextDirection dir,
247 std::vector<BidiRegion>* bidiRegions) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000248
249 // Convert to UTF16 since for now bidi iterator only operates on utf16
250 std::unique_ptr<uint16_t[]> utf16;
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000251 auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000252 if (utf16Units < 0) {
253 return false;
254 }
255
256 // Create bidi iterator
257 UErrorCode status = U_ZERO_ERROR;
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000258 SkUnicodeBidi bidi(ubidi_openSized(utf16Units, 0, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000259 if (U_FAILURE(status)) {
260 SkDEBUGF("Bidi error: %s", u_errorName(status));
261 return false;
262 }
263 SkASSERT(bidi);
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000264 uint8_t bidiLevel = (dir == TextDirection::kLTR) ? UBIDI_LTR : UBIDI_RTL;
Julia Lavrova90787fe2020-07-20 17:32:03 +0000265 // The required lifetime of utf16 isn't well documented.
266 // It appears it isn't used after ubidi_setPara except through ubidi_getText.
267 ubidi_setPara(bidi.get(), (const UChar*)utf16.get(), utf16Units, bidiLevel, nullptr, &status);
268 if (U_FAILURE(status)) {
269 SkDEBUGF("Bidi error: %s", u_errorName(status));
270 return false;
271 }
272
273 // Iterate through bidi regions and the result positions into utf8
274 const char* start8 = utf8;
275 const char* end8 = utf8 + utf8Units;
276 BidiLevel currentLevel = 0;
277
278 Position pos8 = 0;
279 Position pos16 = 0;
280 Position end16 = ubidi_getLength(bidi.get());
281 while (pos16 < end16) {
282 auto level = ubidi_getLevelAt(bidi.get(), pos16);
283 if (pos16 == 0) {
284 currentLevel = level;
285 } else if (level != currentLevel) {
286 Position end = start8 - utf8;
287 bidiRegions->emplace_back(pos8, end, currentLevel);
288 currentLevel = level;
289 pos8 = end;
290 }
291 SkUnichar u = utf8_next(&start8, end8);
292 pos16 += SkUTF::ToUTF16(u);
293 }
294 Position end = start8 - utf8;
295 if (end != pos8) {
296 bidiRegions->emplace_back(pos8, end, currentLevel);
297 }
298 return true;
299 }
300
301 static bool extractWords(uint16_t utf16[], int utf16Units, std::vector<Position>* words) {
302
303 UErrorCode status = U_ZERO_ERROR;
304
Jason Simmonsd8e94362021-01-11 15:52:03 -0800305 ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(BreakType::kWords);
306 if (!iterator) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000307 SkDEBUGF("Break error: %s", u_errorName(status));
308 return false;
309 }
310 SkASSERT(iterator);
311
Julia Lavrovaacace5d2020-12-16 11:48:48 -0500312 ICUUText utf16UText(utext_openUChars(nullptr, (UChar*)utf16, utf16Units, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000313 if (U_FAILURE(status)) {
314 SkDEBUGF("Break error: %s", u_errorName(status));
315 return false;
316 }
317
318 ubrk_setUText(iterator.get(), utf16UText.get(), &status);
319 if (U_FAILURE(status)) {
320 SkDEBUGF("Break error: %s", u_errorName(status));
321 return false;
322 }
323
324 // Get the words
325 int32_t pos = ubrk_first(iterator.get());
326 while (pos != UBRK_DONE) {
327 words->emplace_back(pos);
328 pos = ubrk_next(iterator.get());
329 }
330
331 return true;
332 }
333
Julia Lavrova6e51d922020-08-25 11:42:51 -0400334 static bool extractPositions
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500335 (const char utf8[], int utf8Units, BreakType type, std::function<void(int, int)> setBreak) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000336
337 UErrorCode status = U_ZERO_ERROR;
Julia Lavrovaacace5d2020-12-16 11:48:48 -0500338 ICUUText text(utext_openUTF8(nullptr, &utf8[0], utf8Units, &status));
Julia Lavrova90787fe2020-07-20 17:32:03 +0000339
340 if (U_FAILURE(status)) {
341 SkDEBUGF("Break error: %s", u_errorName(status));
342 return false;
343 }
344 SkASSERT(text);
345
Jason Simmonsd8e94362021-01-11 15:52:03 -0800346 ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(type);
347 if (!iterator) {
348 return false;
Julia Lavrova90787fe2020-07-20 17:32:03 +0000349 }
350
351 ubrk_setUText(iterator.get(), text.get(), &status);
352 if (U_FAILURE(status)) {
353 SkDEBUGF("Break error: %s", u_errorName(status));
354 return false;
355 }
356
357 auto iter = iterator.get();
358 int32_t pos = ubrk_first(iter);
359 while (pos != UBRK_DONE) {
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500360 auto status = type == SkUnicode::BreakType::kLines
361 ? UBRK_LINE_SOFT
362 : ubrk_getRuleStatus(iter);
363 setBreak(pos, status);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000364 pos = ubrk_next(iter);
365 }
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500366
367 if (type == SkUnicode::BreakType::kLines) {
368 // This is a workaround for https://bugs.chromium.org/p/skia/issues/detail?id=10715
369 // (ICU line break iterator does not work correctly on Thai text with new lines)
370 // So, we only use the iterator to collect soft line breaks and
371 // scan the text for all hard line breaks ourselves
372 const char* end = utf8 + utf8Units;
373 const char* ch = utf8;
374 while (ch < end) {
375 auto unichar = utf8_next(&ch, end);
376 if (isHardLineBreak(unichar)) {
377 setBreak(ch - utf8, UBRK_LINE_HARD);
378 }
379 }
380 }
Julia Lavrova90787fe2020-07-20 17:32:03 +0000381 return true;
382 }
383
Julia Lavrova6e51d922020-08-25 11:42:51 -0400384 static bool extractWhitespaces(const char utf8[],
385 int utf8Units,
386 std::vector<Position>* whitespaces) {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000387
388 const char* start = utf8;
389 const char* end = utf8 + utf8Units;
390 const char* ch = start;
391 while (ch < end) {
392 auto index = ch - start;
393 auto unichar = utf8_next(&ch, end);
394 if (u_isWhitespace(unichar)) {
395 auto ending = ch - start;
396 for (auto k = index; k < ending; ++k) {
397 whitespaces->emplace_back(k);
398 }
399 }
400 }
401 return true;
402 }
403
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000404 static int utf8ToUtf16(const char* utf8, size_t utf8Units, std::unique_ptr<uint16_t[]>* utf16) {
405 int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units);
406 if (utf16Units < 0) {
407 SkDEBUGF("Convert error: Invalid utf8 input");
408 return utf16Units;
409 }
410 *utf16 = std::unique_ptr<uint16_t[]>(new uint16_t[utf16Units]);
411 SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16->get(), utf16Units, utf8, utf8Units);
412 SkASSERT(dstLen == utf16Units);
413 return utf16Units;
414 }
415
416 static int utf16ToUtf8(const uint16_t* utf16, size_t utf16Units, std::unique_ptr<char[]>* utf8) {
417 int utf8Units = SkUTF::UTF16ToUTF8(nullptr, 0, utf16, utf16Units);
418 if (utf8Units < 0) {
419 SkDEBUGF("Convert error: Invalid utf16 input");
420 return utf8Units;
421 }
422 *utf8 = std::unique_ptr<char[]>(new char[utf8Units]);
423 SkDEBUGCODE(int dstLen =) SkUTF::UTF16ToUTF8(utf8->get(), utf8Units, utf16, utf16Units);
424 SkASSERT(dstLen == utf8Units);
425 return utf8Units;
426 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400427
Julia Lavrova90787fe2020-07-20 17:32:03 +0000428public:
429 ~SkUnicode_icu() override { }
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000430 std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t text[], int count,
431 SkBidiIterator::Direction dir) override {
432 return SkBidiIterator_icu::makeBidiIterator(text, count, dir);
433 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400434 std::unique_ptr<SkBidiIterator> makeBidiIterator(const char text[],
435 int count,
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000436 SkBidiIterator::Direction dir) override {
437 return SkBidiIterator_icu::makeBidiIterator(text, count, dir);
438 }
Julia Lavrova6e51d922020-08-25 11:42:51 -0400439 std::unique_ptr<SkBreakIterator> makeBreakIterator(const char locale[],
440 BreakType breakType) override {
Jason Simmonsd8e94362021-01-11 15:52:03 -0800441 UErrorCode status = U_ZERO_ERROR;
442 ICUBreakIterator iterator(ubrk_open(convertType(breakType), locale, nullptr, 0, &status));
443 if (U_FAILURE(status)) {
444 SkDEBUGF("Break error: %s", u_errorName(status));
445 return nullptr;
446 }
447 return std::unique_ptr<SkBreakIterator>(new SkBreakIterator_icu(std::move(iterator)));
Julia Lavrova6e51d922020-08-25 11:42:51 -0400448 }
Julia Lavrova36700ef2020-08-25 11:42:51 -0400449 std::unique_ptr<SkScriptIterator> makeScriptIterator() override {
450 return SkScriptIterator_icu::makeScriptIterator();
451 }
Julia Lavrova90787fe2020-07-20 17:32:03 +0000452
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000453 // TODO: Use ICU data file to detect controls and whitespaces
454 bool isControl(SkUnichar utf8) override {
455 return u_iscntrl(utf8);
456 }
457
458 bool isWhitespace(SkUnichar utf8) override {
459 return u_isWhitespace(utf8);
460 }
461
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500462 static bool isHardLineBreak(SkUnichar utf8) {
463 auto property = u_getIntPropertyValue(utf8, UCHAR_LINE_BREAK);
464 return property == U_LB_LINE_FEED || property == U_LB_MANDATORY_BREAK;
465 }
466
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000467 SkString convertUtf16ToUtf8(const std::u16string& utf16) override {
468 std::unique_ptr<char[]> utf8;
469 auto utf8Units = SkUnicode_icu::utf16ToUtf8((uint16_t*)utf16.data(), utf16.size(), &utf8);
470 if (utf8Units >= 0) {
471 return SkString(utf8.get(), utf8Units);
472 } else {
473 return SkString();
474 }
475 }
476
Julia Lavrova6e51d922020-08-25 11:42:51 -0400477 bool getBidiRegions(const char utf8[],
478 int utf8Units,
479 TextDirection dir,
480 std::vector<BidiRegion>* results) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000481 return extractBidi(utf8, utf8Units, dir, results);
482 }
483
Julia Lavrova6e51d922020-08-25 11:42:51 -0400484 bool getLineBreaks(const char utf8[],
485 int utf8Units,
486 std::vector<LineBreakBefore>* results) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000487
Julia Lavrova6e51d922020-08-25 11:42:51 -0400488 return extractPositions(utf8, utf8Units, BreakType::kLines,
Julia Lavrova90787fe2020-07-20 17:32:03 +0000489 [results](int pos, int status) {
Julia Lavrova1d1c5c12020-11-04 13:46:01 -0500490 results->emplace_back(pos, status == UBRK_LINE_HARD
Julia Lavrova90787fe2020-07-20 17:32:03 +0000491 ? LineBreakType::kHardLineBreak
492 : LineBreakType::kSoftLineBreak);
493 });
494 }
495
496 bool getWords(const char utf8[], int utf8Units, std::vector<Position>* results) override {
497
498 // Convert to UTF16 since we want the results in utf16
499 std::unique_ptr<uint16_t[]> utf16;
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000500 auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16);
Julia Lavrova90787fe2020-07-20 17:32:03 +0000501 if (utf16Units < 0) {
502 return false;
503 }
504
505 return extractWords(utf16.get(), utf16Units, results);
506 }
507
508 bool getGraphemes(const char utf8[], int utf8Units, std::vector<Position>* results) override {
509
Julia Lavrova6e51d922020-08-25 11:42:51 -0400510 return extractPositions(utf8, utf8Units, BreakType::kGraphemes,
Julia Lavrova90787fe2020-07-20 17:32:03 +0000511 [results](int pos, int status) { results->emplace_back(pos);
512 });
513 }
514
515 bool getWhitespaces(const char utf8[], int utf8Units, std::vector<Position>* results) override {
516
517 return extractWhitespaces(utf8, utf8Units, results);
518 }
519
Julia Lavrova6e51d922020-08-25 11:42:51 -0400520 void reorderVisual(const BidiLevel runLevels[],
521 int levelsCount,
522 int32_t logicalFromVisual[]) override {
Julia Lavrova90787fe2020-07-20 17:32:03 +0000523 ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual);
524 }
525};
526
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000527std::unique_ptr<SkUnicode> SkUnicode::Make() {
528 #if defined(SK_USING_THIRD_PARTY_ICU)
529 if (!SkLoadICU()) {
530 SkDEBUGF("SkLoadICU() failed!\n");
531 return nullptr;
532 }
533 #endif
534 return std::make_unique<SkUnicode_icu>();
535}