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" |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 9 | #include "include/private/SkTFitsIn.h" |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame^] | 10 | #include "include/private/SkTHash.h" |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 11 | #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 Lavrova | 36700ef | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 16 | #include <unicode/uscript.h> |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 17 | #include <unicode/ustring.h> |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 18 | #include <unicode/utext.h> |
| 19 | #include <unicode/utypes.h> |
| 20 | #include <vector> |
| 21 | #include <functional> |
| 22 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 23 | #if defined(SK_USING_THIRD_PARTY_ICU) |
| 24 | #include "SkLoadICU.h" |
| 25 | #endif |
| 26 | |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 27 | using SkUnicodeBidi = std::unique_ptr<UBiDi, SkFunctionWrapper<decltype(ubidi_close), ubidi_close>>; |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 28 | using ICUUText = std::unique_ptr<UText, SkFunctionWrapper<decltype(utext_close), utext_close>>; |
| 29 | using ICUBreakIterator = std::unique_ptr<UBreakIterator, SkFunctionWrapper<decltype(ubrk_close), ubrk_close>>; |
| 30 | |
| 31 | /** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */ |
| 32 | static 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 Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame^] | 37 | static 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 Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 47 | class SkBidiIterator_icu : public SkBidiIterator { |
| 48 | SkUnicodeBidi fBidi; |
| 49 | public: |
| 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 | |
| 129 | void SkBidiIterator::ReorderVisual(const Level runLevels[], int levelsCount, |
| 130 | int32_t logicalFromVisual[]) { |
| 131 | ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual); |
| 132 | } |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 133 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 134 | class 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 Lavrova | acace5d | 2020-12-16 11:48:48 -0500 | [diff] [blame] | 155 | ICUUText text(utext_openUTF8(nullptr, &utftext8[0], utf8Units, &status)); |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 156 | |
| 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 Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame^] | 170 | }; |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 171 | |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame^] | 172 | class 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 Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame^] | 182 | #ifdef SK_ENABLE_ICU_UBRK_SAFECLONE |
| 183 | |
| 184 | ICUBreakIterator makeBreakIterator(SkUnicode::BreakType type) { |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 185 | UErrorCode status = U_ZERO_ERROR; |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame^] | 186 | 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 Reed | f4ea305 | 2021-01-08 02:27:50 +0000 | [diff] [blame] | 214 | if (U_FAILURE(status)) { |
| 215 | SkDEBUGF("Break error: %s", u_errorName(status)); |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 216 | } |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame^] | 217 | return iterator; |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 218 | } |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame^] | 219 | |
| 220 | #endif // SK_ENABLE_ICU_UBRK_SAFECLONE |
| 221 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 222 | }; |
| 223 | |
Julia Lavrova | 36700ef | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 224 | class 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 Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 243 | class SkUnicode_icu : public SkUnicode { |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 244 | static bool extractBidi(const char utf8[], |
| 245 | int utf8Units, |
| 246 | TextDirection dir, |
| 247 | std::vector<BidiRegion>* bidiRegions) { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 248 | |
| 249 | // Convert to UTF16 since for now bidi iterator only operates on utf16 |
| 250 | std::unique_ptr<uint16_t[]> utf16; |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 251 | auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 252 | if (utf16Units < 0) { |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | // Create bidi iterator |
| 257 | UErrorCode status = U_ZERO_ERROR; |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 258 | SkUnicodeBidi bidi(ubidi_openSized(utf16Units, 0, &status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 259 | if (U_FAILURE(status)) { |
| 260 | SkDEBUGF("Bidi error: %s", u_errorName(status)); |
| 261 | return false; |
| 262 | } |
| 263 | SkASSERT(bidi); |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 264 | uint8_t bidiLevel = (dir == TextDirection::kLTR) ? UBIDI_LTR : UBIDI_RTL; |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 265 | // 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 Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame^] | 305 | ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(BreakType::kWords); |
| 306 | if (!iterator) { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 307 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 308 | return false; |
| 309 | } |
| 310 | SkASSERT(iterator); |
| 311 | |
Julia Lavrova | acace5d | 2020-12-16 11:48:48 -0500 | [diff] [blame] | 312 | ICUUText utf16UText(utext_openUChars(nullptr, (UChar*)utf16, utf16Units, &status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 313 | 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 Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 334 | static bool extractPositions |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 335 | (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] | 336 | |
| 337 | UErrorCode status = U_ZERO_ERROR; |
Julia Lavrova | acace5d | 2020-12-16 11:48:48 -0500 | [diff] [blame] | 338 | ICUUText text(utext_openUTF8(nullptr, &utf8[0], utf8Units, &status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 339 | |
| 340 | if (U_FAILURE(status)) { |
| 341 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 342 | return false; |
| 343 | } |
| 344 | SkASSERT(text); |
| 345 | |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame^] | 346 | ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(type); |
| 347 | if (!iterator) { |
| 348 | return false; |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 349 | } |
| 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 Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 360 | auto status = type == SkUnicode::BreakType::kLines |
| 361 | ? UBRK_LINE_SOFT |
| 362 | : ubrk_getRuleStatus(iter); |
| 363 | setBreak(pos, status); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 364 | pos = ubrk_next(iter); |
| 365 | } |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 366 | |
| 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 Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 381 | return true; |
| 382 | } |
| 383 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 384 | static bool extractWhitespaces(const char utf8[], |
| 385 | int utf8Units, |
| 386 | std::vector<Position>* whitespaces) { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 387 | |
| 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 Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 404 | 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 Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 427 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 428 | public: |
| 429 | ~SkUnicode_icu() override { } |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 430 | 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 Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 434 | std::unique_ptr<SkBidiIterator> makeBidiIterator(const char text[], |
| 435 | int count, |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 436 | SkBidiIterator::Direction dir) override { |
| 437 | return SkBidiIterator_icu::makeBidiIterator(text, count, dir); |
| 438 | } |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 439 | std::unique_ptr<SkBreakIterator> makeBreakIterator(const char locale[], |
| 440 | BreakType breakType) override { |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame^] | 441 | 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 Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 448 | } |
Julia Lavrova | 36700ef | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 449 | std::unique_ptr<SkScriptIterator> makeScriptIterator() override { |
| 450 | return SkScriptIterator_icu::makeScriptIterator(); |
| 451 | } |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 452 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 453 | // 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 Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 462 | 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 Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 467 | 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 Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 477 | bool getBidiRegions(const char utf8[], |
| 478 | int utf8Units, |
| 479 | TextDirection dir, |
| 480 | std::vector<BidiRegion>* results) override { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 481 | return extractBidi(utf8, utf8Units, dir, results); |
| 482 | } |
| 483 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 484 | bool getLineBreaks(const char utf8[], |
| 485 | int utf8Units, |
| 486 | std::vector<LineBreakBefore>* results) override { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 487 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 488 | return extractPositions(utf8, utf8Units, BreakType::kLines, |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 489 | [results](int pos, int status) { |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 490 | results->emplace_back(pos, status == UBRK_LINE_HARD |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 491 | ? 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 Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 500 | auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 501 | 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 Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 510 | return extractPositions(utf8, utf8Units, BreakType::kGraphemes, |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 511 | [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 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()) { |
| 530 | SkDEBUGF("SkLoadICU() failed!\n"); |
| 531 | return nullptr; |
| 532 | } |
| 533 | #endif |
| 534 | return std::make_unique<SkUnicode_icu>(); |
| 535 | } |