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" |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 8 | #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 Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame^] | 14 | #include <unicode/ustring.h> |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 15 | #include <unicode/utext.h> |
| 16 | #include <unicode/utypes.h> |
| 17 | #include <vector> |
| 18 | #include <functional> |
| 19 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame^] | 20 | #if defined(SK_USING_THIRD_PARTY_ICU) |
| 21 | #include "SkLoadICU.h" |
| 22 | #endif |
| 23 | |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 24 | using SkUnicodeBidi = std::unique_ptr<UBiDi, SkFunctionWrapper<decltype(ubidi_close), ubidi_close>>; |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 25 | using ICUUText = std::unique_ptr<UText, SkFunctionWrapper<decltype(utext_close), utext_close>>; |
| 26 | using ICUBreakIterator = std::unique_ptr<UBreakIterator, SkFunctionWrapper<decltype(ubrk_close), ubrk_close>>; |
| 27 | |
| 28 | /** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */ |
| 29 | static inline SkUnichar utf8_next(const char** ptr, const char* end) { |
| 30 | SkUnichar val = SkUTF::NextUTF8(ptr, end); |
| 31 | return val < 0 ? 0xFFFD : val; |
| 32 | } |
| 33 | |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 34 | class SkBidiIterator_icu : public SkBidiIterator { |
| 35 | SkUnicodeBidi fBidi; |
| 36 | public: |
| 37 | explicit SkBidiIterator_icu(SkUnicodeBidi bidi) : fBidi(std::move(bidi)) {} |
| 38 | Position getLength() override { return ubidi_getLength(fBidi.get()); } |
| 39 | Level getLevelAt(Position pos) override { return ubidi_getLevelAt(fBidi.get(), pos); } |
| 40 | |
| 41 | static std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t utf16[], int utf16Units, Direction dir) { |
| 42 | UErrorCode status = U_ZERO_ERROR; |
| 43 | SkUnicodeBidi bidi(ubidi_openSized(utf16Units, 0, &status)); |
| 44 | if (U_FAILURE(status)) { |
| 45 | SkDEBUGF("Bidi error: %s", u_errorName(status)); |
| 46 | return nullptr; |
| 47 | } |
| 48 | SkASSERT(bidi); |
| 49 | uint8_t bidiLevel = (dir == SkBidiIterator::kLTR) ? UBIDI_LTR : UBIDI_RTL; |
| 50 | // The required lifetime of utf16 isn't well documented. |
| 51 | // It appears it isn't used after ubidi_setPara except through ubidi_getText. |
| 52 | ubidi_setPara(bidi.get(), (const UChar*)utf16, utf16Units, bidiLevel, nullptr, &status); |
| 53 | if (U_FAILURE(status)) { |
| 54 | SkDEBUGF("Bidi error: %s", u_errorName(status)); |
| 55 | return nullptr; |
| 56 | } |
| 57 | return std::unique_ptr<SkBidiIterator>(new SkBidiIterator_icu(std::move(bidi))); |
| 58 | } |
| 59 | |
| 60 | // ICU bidi iterator works with utf16 but clients (Flutter for instance) may work with utf8 |
| 61 | // This method allows the clients not to think about all these details |
| 62 | static std::unique_ptr<SkBidiIterator> makeBidiIterator(const char utf8[], int utf8Units, Direction dir) { |
| 63 | // Convert utf8 into utf16 since ubidi only accepts utf16 |
| 64 | if (!SkTFitsIn<int32_t>(utf8Units)) { |
| 65 | SkDEBUGF("Bidi error: text too long"); |
| 66 | return nullptr; |
| 67 | } |
| 68 | |
| 69 | // Getting the length like this seems to always set U_BUFFER_OVERFLOW_ERROR |
| 70 | int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units); |
| 71 | if (utf16Units < 0) { |
| 72 | SkDEBUGF("Bidi error: Invalid utf8 input"); |
| 73 | return nullptr; |
| 74 | } |
| 75 | std::unique_ptr<uint16_t[]> utf16(new uint16_t[utf16Units]); |
| 76 | SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16.get(), utf16Units, utf8, utf8Units); |
| 77 | SkASSERT(dstLen == utf16Units); |
| 78 | |
| 79 | return makeBidiIterator(utf16.get(), utf16Units, dir); |
| 80 | } |
| 81 | |
| 82 | // This method returns the final results only: a list of bidi regions |
| 83 | // (this is all SkParagraph really needs; SkShaper however uses the iterator itself) |
| 84 | static std::vector<Region> getBidiRegions(const char utf8[], int utf8Units, Direction dir) { |
| 85 | |
| 86 | auto bidiIterator = makeBidiIterator(utf8, utf8Units, dir); |
| 87 | std::vector<Region> bidiRegions; |
| 88 | const char* start8 = utf8; |
| 89 | const char* end8 = utf8 + utf8Units; |
| 90 | SkBidiIterator::Level currentLevel = 0; |
| 91 | |
| 92 | Position pos8 = 0; |
| 93 | Position pos16 = 0; |
| 94 | Position end16 = bidiIterator->getLength(); |
| 95 | while (pos16 < end16) { |
| 96 | auto level = bidiIterator->getLevelAt(pos16); |
| 97 | if (pos16 == 0) { |
| 98 | currentLevel = level; |
| 99 | } else if (level != currentLevel) { |
| 100 | auto end = start8 - utf8; |
| 101 | bidiRegions.emplace_back(pos8, end, currentLevel); |
| 102 | currentLevel = level; |
| 103 | pos8 = end; |
| 104 | } |
| 105 | SkUnichar u = utf8_next(&start8, end8); |
| 106 | pos16 += SkUTF::ToUTF16(u); |
| 107 | } |
| 108 | auto end = start8 - utf8; |
| 109 | if (end != pos8) { |
| 110 | bidiRegions.emplace_back(pos8, end, currentLevel); |
| 111 | } |
| 112 | return bidiRegions; |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | void SkBidiIterator::ReorderVisual(const Level runLevels[], int levelsCount, |
| 117 | int32_t logicalFromVisual[]) { |
| 118 | ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual); |
| 119 | } |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 120 | |
| 121 | class SkUnicode_icu : public SkUnicode { |
| 122 | |
| 123 | static UBreakIteratorType convertType(UBreakType type) { |
| 124 | switch (type) { |
| 125 | case UBreakType::kLines: return UBRK_LINE; |
| 126 | case UBreakType::kGraphemes: return UBRK_CHARACTER; |
| 127 | case UBreakType::kWords: return UBRK_WORD; |
| 128 | default: |
| 129 | SkDEBUGF("Convert error: wrong break type"); |
| 130 | return UBRK_CHARACTER; |
| 131 | } |
| 132 | } |
| 133 | |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 134 | static bool extractBidi(const char utf8[], int utf8Units, TextDirection dir, std::vector<BidiRegion>* bidiRegions) { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 135 | |
| 136 | // Convert to UTF16 since for now bidi iterator only operates on utf16 |
| 137 | std::unique_ptr<uint16_t[]> utf16; |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame^] | 138 | auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 139 | if (utf16Units < 0) { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | // Create bidi iterator |
| 144 | UErrorCode status = U_ZERO_ERROR; |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 145 | SkUnicodeBidi bidi(ubidi_openSized(utf16Units, 0, &status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 146 | if (U_FAILURE(status)) { |
| 147 | SkDEBUGF("Bidi error: %s", u_errorName(status)); |
| 148 | return false; |
| 149 | } |
| 150 | SkASSERT(bidi); |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 151 | uint8_t bidiLevel = (dir == TextDirection::kLTR) ? UBIDI_LTR : UBIDI_RTL; |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 152 | // The required lifetime of utf16 isn't well documented. |
| 153 | // It appears it isn't used after ubidi_setPara except through ubidi_getText. |
| 154 | ubidi_setPara(bidi.get(), (const UChar*)utf16.get(), utf16Units, bidiLevel, nullptr, &status); |
| 155 | if (U_FAILURE(status)) { |
| 156 | SkDEBUGF("Bidi error: %s", u_errorName(status)); |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | // Iterate through bidi regions and the result positions into utf8 |
| 161 | const char* start8 = utf8; |
| 162 | const char* end8 = utf8 + utf8Units; |
| 163 | BidiLevel currentLevel = 0; |
| 164 | |
| 165 | Position pos8 = 0; |
| 166 | Position pos16 = 0; |
| 167 | Position end16 = ubidi_getLength(bidi.get()); |
| 168 | while (pos16 < end16) { |
| 169 | auto level = ubidi_getLevelAt(bidi.get(), pos16); |
| 170 | if (pos16 == 0) { |
| 171 | currentLevel = level; |
| 172 | } else if (level != currentLevel) { |
| 173 | Position end = start8 - utf8; |
| 174 | bidiRegions->emplace_back(pos8, end, currentLevel); |
| 175 | currentLevel = level; |
| 176 | pos8 = end; |
| 177 | } |
| 178 | SkUnichar u = utf8_next(&start8, end8); |
| 179 | pos16 += SkUTF::ToUTF16(u); |
| 180 | } |
| 181 | Position end = start8 - utf8; |
| 182 | if (end != pos8) { |
| 183 | bidiRegions->emplace_back(pos8, end, currentLevel); |
| 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | static bool extractWords(uint16_t utf16[], int utf16Units, std::vector<Position>* words) { |
| 189 | |
| 190 | UErrorCode status = U_ZERO_ERROR; |
| 191 | |
| 192 | UBreakIteratorType breakType = convertType(UBreakType::kWords); |
| 193 | ICUBreakIterator iterator(ubrk_open(breakType, uloc_getDefault(), nullptr, 0, &status)); |
| 194 | if (U_FAILURE(status)) { |
| 195 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 196 | return false; |
| 197 | } |
| 198 | SkASSERT(iterator); |
| 199 | |
| 200 | UText sUtf16UText = UTEXT_INITIALIZER; |
| 201 | ICUUText utf16UText(utext_openUChars(&sUtf16UText, (UChar*)utf16, utf16Units, &status)); |
| 202 | if (U_FAILURE(status)) { |
| 203 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | ubrk_setUText(iterator.get(), utf16UText.get(), &status); |
| 208 | if (U_FAILURE(status)) { |
| 209 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | // Get the words |
| 214 | int32_t pos = ubrk_first(iterator.get()); |
| 215 | while (pos != UBRK_DONE) { |
| 216 | words->emplace_back(pos); |
| 217 | pos = ubrk_next(iterator.get()); |
| 218 | } |
| 219 | |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | static bool extractPositions(const char utf8[], int utf8Units, UBreakType type, std::function<void(int, int)> add) { |
| 224 | |
| 225 | UErrorCode status = U_ZERO_ERROR; |
| 226 | UText sUtf8UText = UTEXT_INITIALIZER; |
| 227 | ICUUText text(utext_openUTF8(&sUtf8UText, &utf8[0], utf8Units, &status)); |
| 228 | |
| 229 | if (U_FAILURE(status)) { |
| 230 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 231 | return false; |
| 232 | } |
| 233 | SkASSERT(text); |
| 234 | |
| 235 | ICUBreakIterator iterator(ubrk_open(convertType(type), uloc_getDefault(), nullptr, 0, &status)); |
| 236 | if (U_FAILURE(status)) { |
| 237 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 238 | } |
| 239 | |
| 240 | ubrk_setUText(iterator.get(), text.get(), &status); |
| 241 | if (U_FAILURE(status)) { |
| 242 | SkDEBUGF("Break error: %s", u_errorName(status)); |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | auto iter = iterator.get(); |
| 247 | int32_t pos = ubrk_first(iter); |
| 248 | while (pos != UBRK_DONE) { |
| 249 | add(pos, ubrk_getRuleStatus(iter)); |
| 250 | pos = ubrk_next(iter); |
| 251 | } |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | static bool extractWhitespaces(const char utf8[], int utf8Units, std::vector<Position>* whitespaces) { |
| 256 | |
| 257 | const char* start = utf8; |
| 258 | const char* end = utf8 + utf8Units; |
| 259 | const char* ch = start; |
| 260 | while (ch < end) { |
| 261 | auto index = ch - start; |
| 262 | auto unichar = utf8_next(&ch, end); |
| 263 | if (u_isWhitespace(unichar)) { |
| 264 | auto ending = ch - start; |
| 265 | for (auto k = index; k < ending; ++k) { |
| 266 | whitespaces->emplace_back(k); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | return true; |
| 271 | } |
| 272 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame^] | 273 | static int utf8ToUtf16(const char* utf8, size_t utf8Units, std::unique_ptr<uint16_t[]>* utf16) { |
| 274 | int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units); |
| 275 | if (utf16Units < 0) { |
| 276 | SkDEBUGF("Convert error: Invalid utf8 input"); |
| 277 | return utf16Units; |
| 278 | } |
| 279 | *utf16 = std::unique_ptr<uint16_t[]>(new uint16_t[utf16Units]); |
| 280 | SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16->get(), utf16Units, utf8, utf8Units); |
| 281 | SkASSERT(dstLen == utf16Units); |
| 282 | return utf16Units; |
| 283 | } |
| 284 | |
| 285 | static int utf16ToUtf8(const uint16_t* utf16, size_t utf16Units, std::unique_ptr<char[]>* utf8) { |
| 286 | int utf8Units = SkUTF::UTF16ToUTF8(nullptr, 0, utf16, utf16Units); |
| 287 | if (utf8Units < 0) { |
| 288 | SkDEBUGF("Convert error: Invalid utf16 input"); |
| 289 | return utf8Units; |
| 290 | } |
| 291 | *utf8 = std::unique_ptr<char[]>(new char[utf8Units]); |
| 292 | SkDEBUGCODE(int dstLen =) SkUTF::UTF16ToUTF8(utf8->get(), utf8Units, utf16, utf16Units); |
| 293 | SkASSERT(dstLen == utf8Units); |
| 294 | return utf8Units; |
| 295 | } |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 296 | public: |
| 297 | ~SkUnicode_icu() override { } |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 298 | std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t text[], int count, |
| 299 | SkBidiIterator::Direction dir) override { |
| 300 | return SkBidiIterator_icu::makeBidiIterator(text, count, dir); |
| 301 | } |
| 302 | std::unique_ptr<SkBidiIterator> makeBidiIterator(const char text[], int count, |
| 303 | SkBidiIterator::Direction dir) override { |
| 304 | return SkBidiIterator_icu::makeBidiIterator(text, count, dir); |
| 305 | } |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 306 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame^] | 307 | // TODO: Use ICU data file to detect controls and whitespaces |
| 308 | bool isControl(SkUnichar utf8) override { |
| 309 | return u_iscntrl(utf8); |
| 310 | } |
| 311 | |
| 312 | bool isWhitespace(SkUnichar utf8) override { |
| 313 | return u_isWhitespace(utf8); |
| 314 | } |
| 315 | |
| 316 | SkString convertUtf16ToUtf8(const std::u16string& utf16) override { |
| 317 | std::unique_ptr<char[]> utf8; |
| 318 | auto utf8Units = SkUnicode_icu::utf16ToUtf8((uint16_t*)utf16.data(), utf16.size(), &utf8); |
| 319 | if (utf8Units >= 0) { |
| 320 | return SkString(utf8.get(), utf8Units); |
| 321 | } else { |
| 322 | return SkString(); |
| 323 | } |
| 324 | } |
| 325 | |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 326 | bool getBidiRegions(const char utf8[], int utf8Units, TextDirection dir, std::vector<BidiRegion>* results) override { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 327 | return extractBidi(utf8, utf8Units, dir, results); |
| 328 | } |
| 329 | |
| 330 | bool getLineBreaks(const char utf8[], int utf8Units, std::vector<LineBreakBefore>* results) override { |
| 331 | |
| 332 | return extractPositions(utf8, utf8Units, UBreakType::kLines, |
| 333 | [results](int pos, int status) { |
| 334 | results->emplace_back(pos,status == UBRK_LINE_HARD |
| 335 | ? LineBreakType::kHardLineBreak |
| 336 | : LineBreakType::kSoftLineBreak); |
| 337 | }); |
| 338 | } |
| 339 | |
| 340 | bool getWords(const char utf8[], int utf8Units, std::vector<Position>* results) override { |
| 341 | |
| 342 | // Convert to UTF16 since we want the results in utf16 |
| 343 | std::unique_ptr<uint16_t[]> utf16; |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame^] | 344 | auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 345 | if (utf16Units < 0) { |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | return extractWords(utf16.get(), utf16Units, results); |
| 350 | } |
| 351 | |
| 352 | bool getGraphemes(const char utf8[], int utf8Units, std::vector<Position>* results) override { |
| 353 | |
| 354 | return extractPositions(utf8, utf8Units, UBreakType::kGraphemes, |
| 355 | [results](int pos, int status) { results->emplace_back(pos); |
| 356 | }); |
| 357 | } |
| 358 | |
| 359 | bool getWhitespaces(const char utf8[], int utf8Units, std::vector<Position>* results) override { |
| 360 | |
| 361 | return extractWhitespaces(utf8, utf8Units, results); |
| 362 | } |
| 363 | |
| 364 | void reorderVisual(const BidiLevel runLevels[], int levelsCount, int32_t logicalFromVisual[]) override { |
| 365 | ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual); |
| 366 | } |
| 367 | }; |
| 368 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame^] | 369 | std::unique_ptr<SkUnicode> SkUnicode::Make() { |
| 370 | #if defined(SK_USING_THIRD_PARTY_ICU) |
| 371 | if (!SkLoadICU()) { |
| 372 | SkDEBUGF("SkLoadICU() failed!\n"); |
| 373 | return nullptr; |
| 374 | } |
| 375 | #endif |
| 376 | return std::make_unique<SkUnicode_icu>(); |
| 377 | } |