Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 1 | /* |
| 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 Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 7 | #include "include/core/SkString.h" |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 8 | #include "include/private/SkMutex.h" |
John Stiles | 154bd1f | 2021-05-13 09:38:51 -0400 | [diff] [blame] | 9 | #include "include/private/SkOnce.h" |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 10 | #include "include/private/SkTFitsIn.h" |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 11 | #include "include/private/SkTHash.h" |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 12 | #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 Lavrova | 36700ef | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 17 | #include <unicode/uscript.h> |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 18 | #include <unicode/ustring.h> |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 19 | #include <unicode/utext.h> |
| 20 | #include <unicode/utypes.h> |
| 21 | #include <vector> |
| 22 | #include <functional> |
| 23 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 24 | #if defined(SK_USING_THIRD_PARTY_ICU) |
| 25 | #include "SkLoadICU.h" |
| 26 | #endif |
| 27 | |
Julia Lavrova | 4414f87 | 2021-03-03 10:19:41 -0500 | [diff] [blame] | 28 | // 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. |
| 30 | template<typename...> using void_t = void; |
| 31 | template<typename T, typename = void> |
| 32 | struct SkUbrkClone { |
| 33 | UBreakIterator* operator()(T bi, UErrorCode* status) { |
| 34 | return ubrk_safeClone(bi, nullptr, nullptr, status); |
| 35 | } |
| 36 | }; |
| 37 | template<typename T> |
| 38 | struct 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 Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 44 | using SkUnicodeBidi = std::unique_ptr<UBiDi, SkFunctionWrapper<decltype(ubidi_close), ubidi_close>>; |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 45 | using ICUUText = std::unique_ptr<UText, SkFunctionWrapper<decltype(utext_close), utext_close>>; |
| 46 | using ICUBreakIterator = std::unique_ptr<UBreakIterator, SkFunctionWrapper<decltype(ubrk_close), ubrk_close>>; |
| 47 | |
| 48 | /** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */ |
| 49 | static 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 Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 54 | static 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 Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 64 | class SkBidiIterator_icu : public SkBidiIterator { |
| 65 | SkUnicodeBidi fBidi; |
| 66 | public: |
| 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 | |
| 146 | void SkBidiIterator::ReorderVisual(const Level runLevels[], int levelsCount, |
| 147 | int32_t logicalFromVisual[]) { |
| 148 | ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual); |
| 149 | } |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 150 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 151 | class 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 Lavrova | acace5d | 2020-12-16 11:48:48 -0500 | [diff] [blame] | 172 | ICUUText text(utext_openUTF8(nullptr, &utftext8[0], utf8Units, &status)); |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 173 | |
| 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 | } |
Julia Lavrova | 10e7e77 | 2021-05-10 16:16:44 -0400 | [diff] [blame^] | 187 | bool setText(const char16_t utftext16[], int utf16Units) override { |
| 188 | UErrorCode status = U_ZERO_ERROR; |
| 189 | ICUUText text(utext_openUChars(nullptr, reinterpret_cast<const UChar*>(&utftext16[0]), utf16Units, &status)); |
| 190 | |
| 191 | if (U_FAILURE(status)) { |
| 192 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 193 | return false; |
| 194 | } |
| 195 | SkASSERT(text); |
| 196 | ubrk_setUText(fBreakIterator.get(), text.get(), &status); |
| 197 | if (U_FAILURE(status)) { |
| 198 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 199 | return false; |
| 200 | } |
| 201 | fLastResult = 0; |
| 202 | return true; |
| 203 | } |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 204 | }; |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 205 | |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 206 | class SkIcuBreakIteratorCache { |
| 207 | SkTHashMap<SkUnicode::BreakType, ICUBreakIterator> fBreakCache; |
| 208 | SkMutex fBreakCacheMutex; |
| 209 | |
| 210 | public: |
| 211 | static SkIcuBreakIteratorCache& get() { |
| 212 | static SkIcuBreakIteratorCache instance; |
| 213 | return instance; |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 216 | ICUBreakIterator makeBreakIterator(SkUnicode::BreakType type) { |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 217 | UErrorCode status = U_ZERO_ERROR; |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 218 | ICUBreakIterator* cachedIterator; |
| 219 | { |
| 220 | SkAutoMutexExclusive lock(fBreakCacheMutex); |
| 221 | cachedIterator = fBreakCache.find(type); |
| 222 | if (!cachedIterator) { |
| 223 | ICUBreakIterator newIterator(ubrk_open(convertType(type), uloc_getDefault(), nullptr, 0, &status)); |
| 224 | if (U_FAILURE(status)) { |
| 225 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 226 | } else { |
| 227 | cachedIterator = fBreakCache.set(type, std::move(newIterator)); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | ICUBreakIterator iterator; |
| 232 | if (cachedIterator) { |
Julia Lavrova | 4414f87 | 2021-03-03 10:19:41 -0500 | [diff] [blame] | 233 | iterator.reset(SkUbrkClone<const UBreakIterator*>()(cachedIterator->get(), &status)); |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 234 | if (U_FAILURE(status)) { |
| 235 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 236 | } |
| 237 | } |
| 238 | return iterator; |
| 239 | } |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 240 | }; |
| 241 | |
Julia Lavrova | 36700ef | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 242 | class SkScriptIterator_icu : public SkScriptIterator { |
| 243 | public: |
| 244 | bool getScript(SkUnichar u, ScriptID* script) override { |
| 245 | UErrorCode status = U_ZERO_ERROR; |
| 246 | UScriptCode scriptCode = uscript_getScript(u, &status); |
| 247 | if (U_FAILURE (status)) { |
| 248 | return false; |
| 249 | } |
| 250 | if (script) { |
| 251 | *script = (ScriptID)scriptCode; |
| 252 | } |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | static std::unique_ptr<SkScriptIterator> makeScriptIterator() { |
| 257 | return std::unique_ptr<SkScriptIterator>(new SkScriptIterator_icu()); |
| 258 | } |
| 259 | }; |
| 260 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 261 | class SkUnicode_icu : public SkUnicode { |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 262 | static bool extractBidi(const char utf8[], |
| 263 | int utf8Units, |
| 264 | TextDirection dir, |
| 265 | std::vector<BidiRegion>* bidiRegions) { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 266 | |
| 267 | // Convert to UTF16 since for now bidi iterator only operates on utf16 |
| 268 | std::unique_ptr<uint16_t[]> utf16; |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 269 | auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 270 | if (utf16Units < 0) { |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | // Create bidi iterator |
| 275 | UErrorCode status = U_ZERO_ERROR; |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 276 | SkUnicodeBidi bidi(ubidi_openSized(utf16Units, 0, &status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 277 | if (U_FAILURE(status)) { |
| 278 | SkDEBUGF("Bidi error: %s", u_errorName(status)); |
| 279 | return false; |
| 280 | } |
| 281 | SkASSERT(bidi); |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 282 | uint8_t bidiLevel = (dir == TextDirection::kLTR) ? UBIDI_LTR : UBIDI_RTL; |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 283 | // The required lifetime of utf16 isn't well documented. |
| 284 | // It appears it isn't used after ubidi_setPara except through ubidi_getText. |
| 285 | ubidi_setPara(bidi.get(), (const UChar*)utf16.get(), utf16Units, bidiLevel, nullptr, &status); |
| 286 | if (U_FAILURE(status)) { |
| 287 | SkDEBUGF("Bidi error: %s", u_errorName(status)); |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | // Iterate through bidi regions and the result positions into utf8 |
| 292 | const char* start8 = utf8; |
| 293 | const char* end8 = utf8 + utf8Units; |
| 294 | BidiLevel currentLevel = 0; |
| 295 | |
| 296 | Position pos8 = 0; |
| 297 | Position pos16 = 0; |
| 298 | Position end16 = ubidi_getLength(bidi.get()); |
| 299 | while (pos16 < end16) { |
| 300 | auto level = ubidi_getLevelAt(bidi.get(), pos16); |
| 301 | if (pos16 == 0) { |
| 302 | currentLevel = level; |
| 303 | } else if (level != currentLevel) { |
| 304 | Position end = start8 - utf8; |
| 305 | bidiRegions->emplace_back(pos8, end, currentLevel); |
| 306 | currentLevel = level; |
| 307 | pos8 = end; |
| 308 | } |
| 309 | SkUnichar u = utf8_next(&start8, end8); |
| 310 | pos16 += SkUTF::ToUTF16(u); |
| 311 | } |
| 312 | Position end = start8 - utf8; |
| 313 | if (end != pos8) { |
| 314 | bidiRegions->emplace_back(pos8, end, currentLevel); |
| 315 | } |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | static bool extractWords(uint16_t utf16[], int utf16Units, std::vector<Position>* words) { |
| 320 | |
| 321 | UErrorCode status = U_ZERO_ERROR; |
| 322 | |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 323 | ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(BreakType::kWords); |
| 324 | if (!iterator) { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 325 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 326 | return false; |
| 327 | } |
| 328 | SkASSERT(iterator); |
| 329 | |
Julia Lavrova | acace5d | 2020-12-16 11:48:48 -0500 | [diff] [blame] | 330 | ICUUText utf16UText(utext_openUChars(nullptr, (UChar*)utf16, utf16Units, &status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 331 | if (U_FAILURE(status)) { |
| 332 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | ubrk_setUText(iterator.get(), utf16UText.get(), &status); |
| 337 | if (U_FAILURE(status)) { |
| 338 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | // Get the words |
| 343 | int32_t pos = ubrk_first(iterator.get()); |
| 344 | while (pos != UBRK_DONE) { |
| 345 | words->emplace_back(pos); |
| 346 | pos = ubrk_next(iterator.get()); |
| 347 | } |
| 348 | |
| 349 | return true; |
| 350 | } |
| 351 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 352 | static bool extractPositions |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 353 | (const char utf8[], int utf8Units, BreakType type, std::function<void(int, int)> setBreak) { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 354 | |
| 355 | UErrorCode status = U_ZERO_ERROR; |
Julia Lavrova | acace5d | 2020-12-16 11:48:48 -0500 | [diff] [blame] | 356 | ICUUText text(utext_openUTF8(nullptr, &utf8[0], utf8Units, &status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 357 | |
| 358 | if (U_FAILURE(status)) { |
| 359 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 360 | return false; |
| 361 | } |
| 362 | SkASSERT(text); |
| 363 | |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 364 | ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(type); |
| 365 | if (!iterator) { |
| 366 | return false; |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | ubrk_setUText(iterator.get(), text.get(), &status); |
| 370 | if (U_FAILURE(status)) { |
| 371 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | auto iter = iterator.get(); |
| 376 | int32_t pos = ubrk_first(iter); |
| 377 | while (pos != UBRK_DONE) { |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 378 | auto status = type == SkUnicode::BreakType::kLines |
| 379 | ? UBRK_LINE_SOFT |
| 380 | : ubrk_getRuleStatus(iter); |
| 381 | setBreak(pos, status); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 382 | pos = ubrk_next(iter); |
| 383 | } |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 384 | |
| 385 | if (type == SkUnicode::BreakType::kLines) { |
| 386 | // This is a workaround for https://bugs.chromium.org/p/skia/issues/detail?id=10715 |
| 387 | // (ICU line break iterator does not work correctly on Thai text with new lines) |
| 388 | // So, we only use the iterator to collect soft line breaks and |
| 389 | // scan the text for all hard line breaks ourselves |
| 390 | const char* end = utf8 + utf8Units; |
| 391 | const char* ch = utf8; |
| 392 | while (ch < end) { |
| 393 | auto unichar = utf8_next(&ch, end); |
| 394 | if (isHardLineBreak(unichar)) { |
| 395 | setBreak(ch - utf8, UBRK_LINE_HARD); |
| 396 | } |
| 397 | } |
| 398 | } |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 399 | return true; |
| 400 | } |
| 401 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 402 | static int utf8ToUtf16(const char* utf8, size_t utf8Units, std::unique_ptr<uint16_t[]>* utf16) { |
| 403 | int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units); |
| 404 | if (utf16Units < 0) { |
| 405 | SkDEBUGF("Convert error: Invalid utf8 input"); |
| 406 | return utf16Units; |
| 407 | } |
| 408 | *utf16 = std::unique_ptr<uint16_t[]>(new uint16_t[utf16Units]); |
| 409 | SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16->get(), utf16Units, utf8, utf8Units); |
| 410 | SkASSERT(dstLen == utf16Units); |
| 411 | return utf16Units; |
| 412 | } |
| 413 | |
| 414 | static int utf16ToUtf8(const uint16_t* utf16, size_t utf16Units, std::unique_ptr<char[]>* utf8) { |
| 415 | int utf8Units = SkUTF::UTF16ToUTF8(nullptr, 0, utf16, utf16Units); |
| 416 | if (utf8Units < 0) { |
| 417 | SkDEBUGF("Convert error: Invalid utf16 input"); |
| 418 | return utf8Units; |
| 419 | } |
| 420 | *utf8 = std::unique_ptr<char[]>(new char[utf8Units]); |
| 421 | SkDEBUGCODE(int dstLen =) SkUTF::UTF16ToUTF8(utf8->get(), utf8Units, utf16, utf16Units); |
| 422 | SkASSERT(dstLen == utf8Units); |
| 423 | return utf8Units; |
| 424 | } |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 425 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 426 | public: |
| 427 | ~SkUnicode_icu() override { } |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 428 | std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t text[], int count, |
| 429 | SkBidiIterator::Direction dir) override { |
| 430 | return SkBidiIterator_icu::makeBidiIterator(text, count, dir); |
| 431 | } |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 432 | std::unique_ptr<SkBidiIterator> makeBidiIterator(const char text[], |
| 433 | int count, |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 434 | SkBidiIterator::Direction dir) override { |
| 435 | return SkBidiIterator_icu::makeBidiIterator(text, count, dir); |
| 436 | } |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 437 | std::unique_ptr<SkBreakIterator> makeBreakIterator(const char locale[], |
| 438 | BreakType breakType) override { |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 439 | UErrorCode status = U_ZERO_ERROR; |
| 440 | ICUBreakIterator iterator(ubrk_open(convertType(breakType), locale, nullptr, 0, &status)); |
| 441 | if (U_FAILURE(status)) { |
| 442 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 443 | return nullptr; |
| 444 | } |
| 445 | return std::unique_ptr<SkBreakIterator>(new SkBreakIterator_icu(std::move(iterator))); |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 446 | } |
Julia Lavrova | 10e7e77 | 2021-05-10 16:16:44 -0400 | [diff] [blame^] | 447 | std::unique_ptr<SkBreakIterator> makeBreakIterator(BreakType breakType) override { |
| 448 | return makeBreakIterator(uloc_getDefault(), breakType); |
| 449 | } |
Julia Lavrova | 36700ef | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 450 | std::unique_ptr<SkScriptIterator> makeScriptIterator() override { |
| 451 | return SkScriptIterator_icu::makeScriptIterator(); |
| 452 | } |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 453 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 454 | // TODO: Use ICU data file to detect controls and whitespaces |
| 455 | bool isControl(SkUnichar utf8) override { |
| 456 | return u_iscntrl(utf8); |
| 457 | } |
| 458 | |
| 459 | bool isWhitespace(SkUnichar utf8) override { |
| 460 | return u_isWhitespace(utf8); |
| 461 | } |
| 462 | |
Julia Lavrova | c70f8c3 | 2021-03-04 16:29:06 -0500 | [diff] [blame] | 463 | bool isSpace(SkUnichar utf8) override { |
| 464 | return u_isspace(utf8); |
| 465 | } |
| 466 | |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 467 | static bool isHardLineBreak(SkUnichar utf8) { |
| 468 | auto property = u_getIntPropertyValue(utf8, UCHAR_LINE_BREAK); |
| 469 | return property == U_LB_LINE_FEED || property == U_LB_MANDATORY_BREAK; |
| 470 | } |
| 471 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 472 | SkString convertUtf16ToUtf8(const std::u16string& utf16) override { |
| 473 | std::unique_ptr<char[]> utf8; |
| 474 | auto utf8Units = SkUnicode_icu::utf16ToUtf8((uint16_t*)utf16.data(), utf16.size(), &utf8); |
| 475 | if (utf8Units >= 0) { |
| 476 | return SkString(utf8.get(), utf8Units); |
| 477 | } else { |
| 478 | return SkString(); |
| 479 | } |
| 480 | } |
| 481 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 482 | bool getBidiRegions(const char utf8[], |
| 483 | int utf8Units, |
| 484 | TextDirection dir, |
| 485 | std::vector<BidiRegion>* results) override { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 486 | return extractBidi(utf8, utf8Units, dir, results); |
| 487 | } |
| 488 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 489 | bool getLineBreaks(const char utf8[], |
| 490 | int utf8Units, |
| 491 | std::vector<LineBreakBefore>* results) override { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 492 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 493 | return extractPositions(utf8, utf8Units, BreakType::kLines, |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 494 | [results](int pos, int status) { |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 495 | results->emplace_back(pos, status == UBRK_LINE_HARD |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 496 | ? LineBreakType::kHardLineBreak |
| 497 | : LineBreakType::kSoftLineBreak); |
| 498 | }); |
| 499 | } |
| 500 | |
| 501 | bool getWords(const char utf8[], int utf8Units, std::vector<Position>* results) override { |
| 502 | |
| 503 | // Convert to UTF16 since we want the results in utf16 |
| 504 | std::unique_ptr<uint16_t[]> utf16; |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 505 | auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 506 | if (utf16Units < 0) { |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | return extractWords(utf16.get(), utf16Units, results); |
| 511 | } |
| 512 | |
| 513 | bool getGraphemes(const char utf8[], int utf8Units, std::vector<Position>* results) override { |
| 514 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 515 | return extractPositions(utf8, utf8Units, BreakType::kGraphemes, |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 516 | [results](int pos, int status) { results->emplace_back(pos); |
| 517 | }); |
| 518 | } |
| 519 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 520 | void reorderVisual(const BidiLevel runLevels[], |
| 521 | int levelsCount, |
| 522 | int32_t logicalFromVisual[]) override { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 523 | ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual); |
| 524 | } |
| 525 | }; |
| 526 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 527 | std::unique_ptr<SkUnicode> SkUnicode::Make() { |
| 528 | #if defined(SK_USING_THIRD_PARTY_ICU) |
| 529 | if (!SkLoadICU()) { |
John Stiles | 154bd1f | 2021-05-13 09:38:51 -0400 | [diff] [blame] | 530 | static SkOnce once; |
| 531 | once([] { SkDEBUGF("SkLoadICU() failed!\n"); }); |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 532 | return nullptr; |
| 533 | } |
| 534 | #endif |
| 535 | return std::make_unique<SkUnicode_icu>(); |
| 536 | } |