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" |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 13 | #include "modules/skunicode/include/SkUnicode.h" |
| 14 | #include "modules/skunicode/src/SkUnicode_icu.h" |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 15 | #include "src/utils/SkUTF.h" |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 16 | #include <vector> |
| 17 | #include <functional> |
| 18 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 19 | #if defined(SK_USING_THIRD_PARTY_ICU) |
| 20 | #include "SkLoadICU.h" |
| 21 | #endif |
| 22 | |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 23 | static const SkICULib* ICULib() { |
| 24 | static const auto gICU = SkLoadICULib(); |
Julia Lavrova | 4414f87 | 2021-03-03 10:19:41 -0500 | [diff] [blame] | 25 | |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 26 | return gICU.get(); |
| 27 | } |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 28 | |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 29 | // sk_* wrappers for ICU funcs |
| 30 | #define SKICU_FUNC(funcname) \ |
| 31 | template <typename... Args> \ |
| 32 | auto sk_##funcname(Args&&... args) -> decltype(funcname(std::forward<Args>(args)...)) { \ |
| 33 | return ICULib()->f_##funcname(std::forward<Args>(args)...); \ |
| 34 | } \ |
| 35 | |
| 36 | SKICU_EMIT_FUNCS |
| 37 | #undef SKICU_FUNC |
| 38 | |
| 39 | static inline UBreakIterator* sk_ubrk_clone(const UBreakIterator* bi, UErrorCode* status) { |
| 40 | const auto* icu = ICULib(); |
| 41 | SkASSERT(icu->f_ubrk_clone_ || icu->f_ubrk_safeClone_); |
| 42 | return icu->f_ubrk_clone_ |
| 43 | ? icu->f_ubrk_clone_(bi, status) |
| 44 | : icu->f_ubrk_safeClone_(bi, nullptr, nullptr, status); |
| 45 | } |
| 46 | |
| 47 | static void ubidi_close_wrapper(UBiDi* bidi) { |
| 48 | sk_ubidi_close(bidi); |
| 49 | } |
| 50 | static UText* utext_close_wrapper(UText* ut) { |
| 51 | return sk_utext_close(ut); |
| 52 | } |
| 53 | static void ubrk_close_wrapper(UBreakIterator* bi) { |
| 54 | sk_ubrk_close(bi); |
| 55 | } |
| 56 | |
| 57 | using SkUnicodeBidi = std::unique_ptr<UBiDi, SkFunctionWrapper<decltype(ubidi_close), |
| 58 | ubidi_close_wrapper>>; |
| 59 | using ICUUText = std::unique_ptr<UText, SkFunctionWrapper<decltype(utext_close), |
| 60 | utext_close_wrapper>>; |
| 61 | using ICUBreakIterator = std::unique_ptr<UBreakIterator, SkFunctionWrapper<decltype(ubrk_close), |
| 62 | ubrk_close_wrapper>>; |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 63 | /** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */ |
| 64 | static inline SkUnichar utf8_next(const char** ptr, const char* end) { |
| 65 | SkUnichar val = SkUTF::NextUTF8(ptr, end); |
| 66 | return val < 0 ? 0xFFFD : val; |
| 67 | } |
| 68 | |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 69 | static UBreakIteratorType convertType(SkUnicode::BreakType type) { |
| 70 | switch (type) { |
| 71 | case SkUnicode::BreakType::kLines: return UBRK_LINE; |
| 72 | case SkUnicode::BreakType::kGraphemes: return UBRK_CHARACTER; |
| 73 | case SkUnicode::BreakType::kWords: return UBRK_WORD; |
| 74 | default: |
| 75 | return UBRK_CHARACTER; |
| 76 | } |
| 77 | } |
| 78 | |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 79 | class SkBidiIterator_icu : public SkBidiIterator { |
| 80 | SkUnicodeBidi fBidi; |
| 81 | public: |
| 82 | explicit SkBidiIterator_icu(SkUnicodeBidi bidi) : fBidi(std::move(bidi)) {} |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 83 | Position getLength() override { return sk_ubidi_getLength(fBidi.get()); } |
| 84 | Level getLevelAt(Position pos) override { return sk_ubidi_getLevelAt(fBidi.get(), pos); } |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 85 | |
| 86 | static std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t utf16[], int utf16Units, Direction dir) { |
| 87 | UErrorCode status = U_ZERO_ERROR; |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 88 | SkUnicodeBidi bidi(sk_ubidi_openSized(utf16Units, 0, &status)); |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 89 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 90 | SkDEBUGF("Bidi error: %s", sk_u_errorName(status)); |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 91 | return nullptr; |
| 92 | } |
| 93 | SkASSERT(bidi); |
| 94 | uint8_t bidiLevel = (dir == SkBidiIterator::kLTR) ? UBIDI_LTR : UBIDI_RTL; |
| 95 | // The required lifetime of utf16 isn't well documented. |
| 96 | // It appears it isn't used after ubidi_setPara except through ubidi_getText. |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 97 | sk_ubidi_setPara(bidi.get(), (const UChar*)utf16, utf16Units, bidiLevel, nullptr, &status); |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 98 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 99 | SkDEBUGF("Bidi error: %s", sk_u_errorName(status)); |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 100 | return nullptr; |
| 101 | } |
| 102 | return std::unique_ptr<SkBidiIterator>(new SkBidiIterator_icu(std::move(bidi))); |
| 103 | } |
| 104 | |
| 105 | // ICU bidi iterator works with utf16 but clients (Flutter for instance) may work with utf8 |
| 106 | // This method allows the clients not to think about all these details |
| 107 | static std::unique_ptr<SkBidiIterator> makeBidiIterator(const char utf8[], int utf8Units, Direction dir) { |
| 108 | // Convert utf8 into utf16 since ubidi only accepts utf16 |
| 109 | if (!SkTFitsIn<int32_t>(utf8Units)) { |
| 110 | SkDEBUGF("Bidi error: text too long"); |
| 111 | return nullptr; |
| 112 | } |
| 113 | |
| 114 | // Getting the length like this seems to always set U_BUFFER_OVERFLOW_ERROR |
| 115 | int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units); |
| 116 | if (utf16Units < 0) { |
| 117 | SkDEBUGF("Bidi error: Invalid utf8 input"); |
| 118 | return nullptr; |
| 119 | } |
| 120 | std::unique_ptr<uint16_t[]> utf16(new uint16_t[utf16Units]); |
| 121 | SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16.get(), utf16Units, utf8, utf8Units); |
| 122 | SkASSERT(dstLen == utf16Units); |
| 123 | |
| 124 | return makeBidiIterator(utf16.get(), utf16Units, dir); |
| 125 | } |
| 126 | |
| 127 | // This method returns the final results only: a list of bidi regions |
| 128 | // (this is all SkParagraph really needs; SkShaper however uses the iterator itself) |
| 129 | static std::vector<Region> getBidiRegions(const char utf8[], int utf8Units, Direction dir) { |
| 130 | |
| 131 | auto bidiIterator = makeBidiIterator(utf8, utf8Units, dir); |
| 132 | std::vector<Region> bidiRegions; |
| 133 | const char* start8 = utf8; |
| 134 | const char* end8 = utf8 + utf8Units; |
| 135 | SkBidiIterator::Level currentLevel = 0; |
| 136 | |
| 137 | Position pos8 = 0; |
| 138 | Position pos16 = 0; |
| 139 | Position end16 = bidiIterator->getLength(); |
| 140 | while (pos16 < end16) { |
| 141 | auto level = bidiIterator->getLevelAt(pos16); |
| 142 | if (pos16 == 0) { |
| 143 | currentLevel = level; |
| 144 | } else if (level != currentLevel) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 145 | auto end = SkTo<Position>(start8 - utf8); |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 146 | bidiRegions.emplace_back(pos8, end, currentLevel); |
| 147 | currentLevel = level; |
| 148 | pos8 = end; |
| 149 | } |
| 150 | SkUnichar u = utf8_next(&start8, end8); |
| 151 | pos16 += SkUTF::ToUTF16(u); |
| 152 | } |
| 153 | auto end = start8 - utf8; |
| 154 | if (end != pos8) { |
| 155 | bidiRegions.emplace_back(pos8, end, currentLevel); |
| 156 | } |
| 157 | return bidiRegions; |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | void SkBidiIterator::ReorderVisual(const Level runLevels[], int levelsCount, |
| 162 | int32_t logicalFromVisual[]) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 163 | sk_ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual); |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 164 | } |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 165 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 166 | class SkBreakIterator_icu : public SkBreakIterator { |
| 167 | ICUBreakIterator fBreakIterator; |
| 168 | Position fLastResult; |
| 169 | public: |
| 170 | explicit SkBreakIterator_icu(ICUBreakIterator iter) |
| 171 | : fBreakIterator(std::move(iter)), fLastResult(0) {} |
| 172 | Position first() override |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 173 | { return fLastResult = sk_ubrk_first(fBreakIterator.get()); } |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 174 | Position current() override |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 175 | { return fLastResult = sk_ubrk_current(fBreakIterator.get()); } |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 176 | Position next() override |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 177 | { return fLastResult = sk_ubrk_next(fBreakIterator.get()); } |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 178 | Position preceding(Position offset) override |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 179 | { return fLastResult = sk_ubrk_preceding(fBreakIterator.get(), offset); } |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 180 | Position following(Position offset) override |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 181 | { return fLastResult = sk_ubrk_following(fBreakIterator.get(), offset);} |
| 182 | Status status() override { return sk_ubrk_getRuleStatus(fBreakIterator.get()); } |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 183 | bool isDone() override { return fLastResult == UBRK_DONE; } |
| 184 | |
| 185 | bool setText(const char utftext8[], int utf8Units) override { |
| 186 | UErrorCode status = U_ZERO_ERROR; |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 187 | ICUUText text(sk_utext_openUTF8(nullptr, &utftext8[0], utf8Units, &status)); |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 188 | |
| 189 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 190 | SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 191 | return false; |
| 192 | } |
| 193 | SkASSERT(text); |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 194 | sk_ubrk_setUText(fBreakIterator.get(), text.get(), &status); |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 195 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 196 | SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | fLastResult = 0; |
| 200 | return true; |
| 201 | } |
Julia Lavrova | 10e7e77 | 2021-05-10 16:16:44 -0400 | [diff] [blame] | 202 | bool setText(const char16_t utftext16[], int utf16Units) override { |
| 203 | UErrorCode status = U_ZERO_ERROR; |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 204 | ICUUText text(sk_utext_openUChars(nullptr, reinterpret_cast<const UChar*>(&utftext16[0]), |
| 205 | utf16Units, &status)); |
Julia Lavrova | 10e7e77 | 2021-05-10 16:16:44 -0400 | [diff] [blame] | 206 | |
| 207 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 208 | SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
Julia Lavrova | 10e7e77 | 2021-05-10 16:16:44 -0400 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | SkASSERT(text); |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 212 | sk_ubrk_setUText(fBreakIterator.get(), text.get(), &status); |
Julia Lavrova | 10e7e77 | 2021-05-10 16:16:44 -0400 | [diff] [blame] | 213 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 214 | SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
Julia Lavrova | 10e7e77 | 2021-05-10 16:16:44 -0400 | [diff] [blame] | 215 | return false; |
| 216 | } |
| 217 | fLastResult = 0; |
| 218 | return true; |
| 219 | } |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 220 | }; |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 221 | |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 222 | class SkIcuBreakIteratorCache { |
| 223 | SkTHashMap<SkUnicode::BreakType, ICUBreakIterator> fBreakCache; |
| 224 | SkMutex fBreakCacheMutex; |
| 225 | |
| 226 | public: |
| 227 | static SkIcuBreakIteratorCache& get() { |
| 228 | static SkIcuBreakIteratorCache instance; |
| 229 | return instance; |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 230 | } |
| 231 | |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 232 | ICUBreakIterator makeBreakIterator(SkUnicode::BreakType type) { |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 233 | UErrorCode status = U_ZERO_ERROR; |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 234 | ICUBreakIterator* cachedIterator; |
| 235 | { |
| 236 | SkAutoMutexExclusive lock(fBreakCacheMutex); |
| 237 | cachedIterator = fBreakCache.find(type); |
| 238 | if (!cachedIterator) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 239 | ICUBreakIterator newIterator(sk_ubrk_open(convertType(type), sk_uloc_getDefault(), |
| 240 | nullptr, 0, &status)); |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 241 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 242 | SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 243 | } else { |
| 244 | cachedIterator = fBreakCache.set(type, std::move(newIterator)); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | ICUBreakIterator iterator; |
| 249 | if (cachedIterator) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 250 | iterator.reset(sk_ubrk_clone(cachedIterator->get(), &status)); |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 251 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 252 | SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | return iterator; |
| 256 | } |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 257 | }; |
| 258 | |
Julia Lavrova | 36700ef | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 259 | class SkScriptIterator_icu : public SkScriptIterator { |
| 260 | public: |
| 261 | bool getScript(SkUnichar u, ScriptID* script) override { |
| 262 | UErrorCode status = U_ZERO_ERROR; |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 263 | UScriptCode scriptCode = sk_uscript_getScript(u, &status); |
Julia Lavrova | 36700ef | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 264 | if (U_FAILURE (status)) { |
| 265 | return false; |
| 266 | } |
| 267 | if (script) { |
| 268 | *script = (ScriptID)scriptCode; |
| 269 | } |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | static std::unique_ptr<SkScriptIterator> makeScriptIterator() { |
| 274 | return std::unique_ptr<SkScriptIterator>(new SkScriptIterator_icu()); |
| 275 | } |
| 276 | }; |
| 277 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 278 | class SkUnicode_icu : public SkUnicode { |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 279 | static bool extractBidi(const char utf8[], |
| 280 | int utf8Units, |
| 281 | TextDirection dir, |
| 282 | std::vector<BidiRegion>* bidiRegions) { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 283 | |
| 284 | // Convert to UTF16 since for now bidi iterator only operates on utf16 |
Julia Lavrova | 398ef44 | 2021-09-14 20:37:58 +0000 | [diff] [blame] | 285 | auto utf16 = convertUtf8ToUtf16(utf8, utf8Units); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 286 | |
| 287 | // Create bidi iterator |
| 288 | UErrorCode status = U_ZERO_ERROR; |
Julia Lavrova | 398ef44 | 2021-09-14 20:37:58 +0000 | [diff] [blame] | 289 | SkUnicodeBidi bidi(sk_ubidi_openSized(utf16.size(), 0, &status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 290 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 291 | SkDEBUGF("Bidi error: %s", sk_u_errorName(status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 292 | return false; |
| 293 | } |
| 294 | SkASSERT(bidi); |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 295 | uint8_t bidiLevel = (dir == TextDirection::kLTR) ? UBIDI_LTR : UBIDI_RTL; |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 296 | // The required lifetime of utf16 isn't well documented. |
| 297 | // It appears it isn't used after ubidi_setPara except through ubidi_getText. |
Julia Lavrova | 398ef44 | 2021-09-14 20:37:58 +0000 | [diff] [blame] | 298 | sk_ubidi_setPara(bidi.get(), (const UChar*)utf16.c_str(), utf16.size(), bidiLevel, nullptr, |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 299 | &status); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 300 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 301 | SkDEBUGF("Bidi error: %s", sk_u_errorName(status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 302 | return false; |
| 303 | } |
| 304 | |
| 305 | // Iterate through bidi regions and the result positions into utf8 |
| 306 | const char* start8 = utf8; |
| 307 | const char* end8 = utf8 + utf8Units; |
| 308 | BidiLevel currentLevel = 0; |
| 309 | |
| 310 | Position pos8 = 0; |
| 311 | Position pos16 = 0; |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 312 | Position end16 = sk_ubidi_getLength(bidi.get()); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 313 | while (pos16 < end16) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 314 | auto level = sk_ubidi_getLevelAt(bidi.get(), pos16); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 315 | if (pos16 == 0) { |
| 316 | currentLevel = level; |
| 317 | } else if (level != currentLevel) { |
| 318 | Position end = start8 - utf8; |
| 319 | bidiRegions->emplace_back(pos8, end, currentLevel); |
| 320 | currentLevel = level; |
| 321 | pos8 = end; |
| 322 | } |
| 323 | SkUnichar u = utf8_next(&start8, end8); |
| 324 | pos16 += SkUTF::ToUTF16(u); |
| 325 | } |
| 326 | Position end = start8 - utf8; |
| 327 | if (end != pos8) { |
| 328 | bidiRegions->emplace_back(pos8, end, currentLevel); |
| 329 | } |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | static bool extractWords(uint16_t utf16[], int utf16Units, std::vector<Position>* words) { |
| 334 | |
| 335 | UErrorCode status = U_ZERO_ERROR; |
| 336 | |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 337 | ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(BreakType::kWords); |
| 338 | if (!iterator) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 339 | SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 340 | return false; |
| 341 | } |
| 342 | SkASSERT(iterator); |
| 343 | |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 344 | ICUUText utf16UText(sk_utext_openUChars(nullptr, (UChar*)utf16, utf16Units, &status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 345 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 346 | SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 347 | return false; |
| 348 | } |
| 349 | |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 350 | sk_ubrk_setUText(iterator.get(), utf16UText.get(), &status); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 351 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 352 | SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 353 | return false; |
| 354 | } |
| 355 | |
| 356 | // Get the words |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 357 | int32_t pos = sk_ubrk_first(iterator.get()); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 358 | while (pos != UBRK_DONE) { |
| 359 | words->emplace_back(pos); |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 360 | pos = sk_ubrk_next(iterator.get()); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | return true; |
| 364 | } |
| 365 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 366 | static bool extractPositions |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 367 | (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] | 368 | |
| 369 | UErrorCode status = U_ZERO_ERROR; |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 370 | ICUUText text(sk_utext_openUTF8(nullptr, &utf8[0], utf8Units, &status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 371 | |
| 372 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 373 | SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 374 | return false; |
| 375 | } |
| 376 | SkASSERT(text); |
| 377 | |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 378 | ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(type); |
| 379 | if (!iterator) { |
| 380 | return false; |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 383 | sk_ubrk_setUText(iterator.get(), text.get(), &status); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 384 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 385 | SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 386 | return false; |
| 387 | } |
| 388 | |
| 389 | auto iter = iterator.get(); |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 390 | int32_t pos = sk_ubrk_first(iter); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 391 | while (pos != UBRK_DONE) { |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 392 | auto status = type == SkUnicode::BreakType::kLines |
| 393 | ? UBRK_LINE_SOFT |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 394 | : sk_ubrk_getRuleStatus(iter); |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 395 | setBreak(pos, status); |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 396 | pos = sk_ubrk_next(iter); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 397 | } |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 398 | |
| 399 | if (type == SkUnicode::BreakType::kLines) { |
| 400 | // This is a workaround for https://bugs.chromium.org/p/skia/issues/detail?id=10715 |
| 401 | // (ICU line break iterator does not work correctly on Thai text with new lines) |
| 402 | // So, we only use the iterator to collect soft line breaks and |
| 403 | // scan the text for all hard line breaks ourselves |
| 404 | const char* end = utf8 + utf8Units; |
| 405 | const char* ch = utf8; |
| 406 | while (ch < end) { |
| 407 | auto unichar = utf8_next(&ch, end); |
| 408 | if (isHardLineBreak(unichar)) { |
| 409 | setBreak(ch - utf8, UBRK_LINE_HARD); |
| 410 | } |
| 411 | } |
| 412 | } |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 413 | return true; |
| 414 | } |
| 415 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 416 | public: |
| 417 | ~SkUnicode_icu() override { } |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 418 | std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t text[], int count, |
| 419 | SkBidiIterator::Direction dir) override { |
| 420 | return SkBidiIterator_icu::makeBidiIterator(text, count, dir); |
| 421 | } |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 422 | std::unique_ptr<SkBidiIterator> makeBidiIterator(const char text[], |
| 423 | int count, |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 424 | SkBidiIterator::Direction dir) override { |
| 425 | return SkBidiIterator_icu::makeBidiIterator(text, count, dir); |
| 426 | } |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 427 | std::unique_ptr<SkBreakIterator> makeBreakIterator(const char locale[], |
| 428 | BreakType breakType) override { |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 429 | UErrorCode status = U_ZERO_ERROR; |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 430 | ICUBreakIterator iterator(sk_ubrk_open(convertType(breakType), locale, nullptr, 0, |
| 431 | &status)); |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 432 | if (U_FAILURE(status)) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 433 | SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
Jason Simmons | d8e9436 | 2021-01-11 15:52:03 -0800 | [diff] [blame] | 434 | return nullptr; |
| 435 | } |
| 436 | return std::unique_ptr<SkBreakIterator>(new SkBreakIterator_icu(std::move(iterator))); |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 437 | } |
Julia Lavrova | 10e7e77 | 2021-05-10 16:16:44 -0400 | [diff] [blame] | 438 | std::unique_ptr<SkBreakIterator> makeBreakIterator(BreakType breakType) override { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 439 | return makeBreakIterator(sk_uloc_getDefault(), breakType); |
Julia Lavrova | 10e7e77 | 2021-05-10 16:16:44 -0400 | [diff] [blame] | 440 | } |
Julia Lavrova | 36700ef | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 441 | std::unique_ptr<SkScriptIterator> makeScriptIterator() override { |
| 442 | return SkScriptIterator_icu::makeScriptIterator(); |
| 443 | } |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 444 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 445 | // TODO: Use ICU data file to detect controls and whitespaces |
| 446 | bool isControl(SkUnichar utf8) override { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 447 | return sk_u_iscntrl(utf8); |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | bool isWhitespace(SkUnichar utf8) override { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 451 | return sk_u_isWhitespace(utf8); |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Julia Lavrova | c70f8c3 | 2021-03-04 16:29:06 -0500 | [diff] [blame] | 454 | bool isSpace(SkUnichar utf8) override { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 455 | return sk_u_isspace(utf8); |
Julia Lavrova | c70f8c3 | 2021-03-04 16:29:06 -0500 | [diff] [blame] | 456 | } |
| 457 | |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 458 | static bool isHardLineBreak(SkUnichar utf8) { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 459 | auto property = sk_u_getIntPropertyValue(utf8, UCHAR_LINE_BREAK); |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 460 | return property == U_LB_LINE_FEED || property == U_LB_MANDATORY_BREAK; |
| 461 | } |
| 462 | |
Florin Malita | 5572b2a | 2021-09-03 09:45:53 -0400 | [diff] [blame] | 463 | SkString toUpper(const SkString& str) override { |
| 464 | // Convert to UTF16 since that's what ICU wants. |
Julia Lavrova | 398ef44 | 2021-09-14 20:37:58 +0000 | [diff] [blame] | 465 | auto str16 = convertUtf8ToUtf16(str.c_str(), str.size()); |
Florin Malita | 5572b2a | 2021-09-03 09:45:53 -0400 | [diff] [blame] | 466 | |
| 467 | UErrorCode icu_err = U_ZERO_ERROR; |
Julia Lavrova | 398ef44 | 2021-09-14 20:37:58 +0000 | [diff] [blame] | 468 | const auto upper16len = sk_u_strToUpper(nullptr, 0, (UChar*)(str16.c_str()), str16.size(), |
Florin Malita | 5572b2a | 2021-09-03 09:45:53 -0400 | [diff] [blame] | 469 | nullptr, &icu_err); |
| 470 | if (icu_err != U_BUFFER_OVERFLOW_ERROR || upper16len <= 0) { |
| 471 | return SkString(); |
| 472 | } |
| 473 | |
| 474 | SkAutoSTArray<128, uint16_t> upper16(upper16len); |
| 475 | icu_err = U_ZERO_ERROR; |
| 476 | sk_u_strToUpper((UChar*)(upper16.get()), SkToS32(upper16.size()), |
Julia Lavrova | 398ef44 | 2021-09-14 20:37:58 +0000 | [diff] [blame] | 477 | (UChar*)(str16.c_str()), str16.size(), |
Florin Malita | 5572b2a | 2021-09-03 09:45:53 -0400 | [diff] [blame] | 478 | nullptr, &icu_err); |
| 479 | SkASSERT(!U_FAILURE(icu_err)); |
| 480 | |
| 481 | // ... and back to utf8 'cause that's what we want. |
Julia Lavrova | 398ef44 | 2021-09-14 20:37:58 +0000 | [diff] [blame] | 482 | return convertUtf16ToUtf8((char16_t*)upper16.get(), upper16.size()); |
Florin Malita | 5572b2a | 2021-09-03 09:45:53 -0400 | [diff] [blame] | 483 | } |
| 484 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 485 | bool getBidiRegions(const char utf8[], |
| 486 | int utf8Units, |
| 487 | TextDirection dir, |
| 488 | std::vector<BidiRegion>* results) override { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 489 | return extractBidi(utf8, utf8Units, dir, results); |
| 490 | } |
| 491 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 492 | bool getLineBreaks(const char utf8[], |
| 493 | int utf8Units, |
| 494 | std::vector<LineBreakBefore>* results) override { |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 495 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 496 | return extractPositions(utf8, utf8Units, BreakType::kLines, |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 497 | [results](int pos, int status) { |
Julia Lavrova | 1d1c5c1 | 2020-11-04 13:46:01 -0500 | [diff] [blame] | 498 | results->emplace_back(pos, status == UBRK_LINE_HARD |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 499 | ? LineBreakType::kHardLineBreak |
| 500 | : LineBreakType::kSoftLineBreak); |
| 501 | }); |
| 502 | } |
| 503 | |
| 504 | bool getWords(const char utf8[], int utf8Units, std::vector<Position>* results) override { |
| 505 | |
| 506 | // Convert to UTF16 since we want the results in utf16 |
Julia Lavrova | 398ef44 | 2021-09-14 20:37:58 +0000 | [diff] [blame] | 507 | auto utf16 = convertUtf8ToUtf16(utf8, utf8Units); |
| 508 | return extractWords((uint16_t*)utf16.c_str(), utf16.size(), results); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | bool getGraphemes(const char utf8[], int utf8Units, std::vector<Position>* results) override { |
| 512 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 513 | return extractPositions(utf8, utf8Units, BreakType::kGraphemes, |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 514 | [results](int pos, int status) { results->emplace_back(pos); |
| 515 | }); |
| 516 | } |
| 517 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 518 | void reorderVisual(const BidiLevel runLevels[], |
| 519 | int levelsCount, |
| 520 | int32_t logicalFromVisual[]) override { |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 521 | sk_ubidi_reorderVisual(runLevels, levelsCount, logicalFromVisual); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 522 | } |
| 523 | }; |
| 524 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 525 | std::unique_ptr<SkUnicode> SkUnicode::Make() { |
| 526 | #if defined(SK_USING_THIRD_PARTY_ICU) |
| 527 | if (!SkLoadICU()) { |
John Stiles | 154bd1f | 2021-05-13 09:38:51 -0400 | [diff] [blame] | 528 | static SkOnce once; |
| 529 | once([] { SkDEBUGF("SkLoadICU() failed!\n"); }); |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 530 | return nullptr; |
| 531 | } |
| 532 | #endif |
Florin Malita | 40f3db4 | 2021-07-27 14:02:28 -0400 | [diff] [blame] | 533 | |
| 534 | return ICULib() |
| 535 | ? std::make_unique<SkUnicode_icu>() |
| 536 | : nullptr; |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 537 | } |