| 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 |
*/ |
| 7 |
#include "include/core/SkRefCnt.h" |
| 8 |
#include "include/core/SkString.h" |
| 9 |
#include "include/core/SkTypes.h" |
| 10 |
#include "include/private/base/SkDebug.h" |
| 11 |
#include "include/private/base/SkMutex.h" |
| 12 |
#include "include/private/base/SkSpan_impl.h" |
| 13 |
#include "include/private/base/SkTArray.h" |
| 14 |
#include "include/private/base/SkTemplates.h" |
| 15 |
#include "include/private/base/SkTo.h" |
| 16 |
#include "modules/skunicode/include/SkUnicode.h" |
| 17 |
#include "modules/skunicode/include/SkUnicode_icu.h" |
| 18 |
#include "modules/skunicode/src/SkBidiFactory_icu_full.h" |
| 19 |
#include "modules/skunicode/src/SkUnicode_icu_bidi.h" |
| 20 |
#include "modules/skunicode/src/SkUnicode_icupriv.h" |
| 21 |
#include "src/base/SkBitmaskEnum.h" |
| 22 |
#include "src/base/SkUTF.h" |
| 23 |
#include "src/core/SkChecksum.h" |
| 24 |
#include "src/core/SkTHash.h" |
| 25 |
|
| 26 |
#include <unicode/ubrk.h> |
| 27 |
#include <unicode/uchar.h> |
| 28 |
#include <unicode/uloc.h> |
| 29 |
#include <unicode/umachine.h> |
| 30 |
#include <unicode/utext.h> |
| 31 |
#include <unicode/utypes.h> |
| 32 |
|
| 33 |
#include <cstdint> |
| 34 |
#include <cstring> |
| 35 |
#include <functional> |
| 36 |
#include <memory> |
| 37 |
#include <string> |
| 38 |
#include <utility> |
| 39 |
#include <vector> |
| 40 |
|
| 41 |
#if defined(SK_USING_THIRD_PARTY_ICU) && defined(SK_BUILD_FOR_WIN) |
| 42 |
#include "SkLoadICU.h" |
| 43 |
#include "include/private/base/SkOnce.h" |
| 44 |
#endif |
| 45 |
|
| 46 |
using namespace skia_private; |
| 47 |
|
| 48 |
const SkICULib* SkGetICULib() { |
| 49 |
static const auto gICU = SkLoadICULib(); |
| 50 |
return gICU.get(); |
| 51 |
} |
| 52 |
|
| 53 |
// sk_* wrappers for ICU funcs |
| 54 |
#define SKICU_FUNC(funcname) \ |
| 55 |
template <typename... Args> \ |
| 56 |
auto sk_##funcname(Args&&... args) -> decltype(funcname(std::forward<Args>(args)...)) { \ |
| 57 |
return SkGetICULib()->f_##funcname(std::forward<Args>(args)...); \ |
| 58 |
} \ |
| 59 |
|
| 60 |
SKICU_EMIT_FUNCS |
| 61 |
#undef SKICU_FUNC |
| 62 |
|
| 63 |
static inline UBreakIterator* sk_ubrk_clone(const UBreakIterator* bi, UErrorCode* status) { |
| 64 |
const auto* icu = SkGetICULib(); |
| 65 |
SkASSERT(icu->f_ubrk_clone_ || icu->f_ubrk_safeClone_); |
| 66 |
return icu->f_ubrk_clone_ |
| 67 |
? icu->f_ubrk_clone_(bi, status) |
| 68 |
: icu->f_ubrk_safeClone_(bi, nullptr, nullptr, status); |
| 69 |
} |
| 70 |
|
| 71 |
static UText* utext_close_wrapper(UText* ut) { |
| 72 |
return sk_utext_close(ut); |
| 73 |
} |
| 74 |
static void ubrk_close_wrapper(UBreakIterator* bi) { |
| 75 |
sk_ubrk_close(bi); |
| 76 |
} |
| 77 |
|
| 78 |
using ICUUText = std::unique_ptr<UText, SkFunctionObject<utext_close_wrapper>>; |
| 79 |
using ICUBreakIterator = std::unique_ptr<UBreakIterator, SkFunctionObject<ubrk_close_wrapper>>; |
| 80 |
/** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */ |
| 81 |
static inline SkUnichar utf8_next(const char** ptr, const char* end) { |
| 82 |
SkUnichar val = SkUTF::NextUTF8(ptr, end); |
| 83 |
return val < 0 ? 0xFFFD : val; |
| 84 |
} |
| 85 |
|
| 86 |
static UBreakIteratorType convertType(SkUnicode::BreakType type) { |
| 87 |
switch (type) { |
| 88 |
case SkUnicode::BreakType::kLines: return UBRK_LINE; |
| 89 |
case SkUnicode::BreakType::kGraphemes: return UBRK_CHARACTER; |
| 90 |
case SkUnicode::BreakType::kWords: return UBRK_WORD; |
| 91 |
case SkUnicode::BreakType::kSentences: |
| 92 |
return UBRK_SENTENCE; |
| 93 |
default: |
| 94 |
return UBRK_CHARACTER; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
class SkBreakIterator_icu : public SkBreakIterator { |
| 99 |
ICUBreakIterator fBreakIterator; |
| 100 |
Position fLastResult; |
| 101 |
public: |
| 102 |
explicit SkBreakIterator_icu(ICUBreakIterator iter) |
| 103 |
: fBreakIterator(std::move(iter)) |
| 104 |
, fLastResult(0) {} |
| 105 |
Position first() override { return fLastResult = sk_ubrk_first(fBreakIterator.get()); } |
| 106 |
Position current() override { return fLastResult = sk_ubrk_current(fBreakIterator.get()); } |
| 107 |
Position next() override { return fLastResult = sk_ubrk_next(fBreakIterator.get()); } |
| 108 |
Status status() override { return sk_ubrk_getRuleStatus(fBreakIterator.get()); } |
| 109 |
bool isDone() override { return fLastResult == UBRK_DONE; } |
| 110 |
|
| 111 |
bool setText(const char utftext8[], int utf8Units) override { |
| 112 |
UErrorCode status = U_ZERO_ERROR; |
| 113 |
ICUUText text(sk_utext_openUTF8(nullptr, &utftext8[0], utf8Units, &status)); |
| 114 |
|
| 115 |
if (U_FAILURE(status)) { |
| 116 |
SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
| 117 |
return false; |
| 118 |
} |
| 119 |
SkASSERT(text); |
| 120 |
sk_ubrk_setUText(fBreakIterator.get(), text.get(), &status); |
| 121 |
if (U_FAILURE(status)) { |
| 122 |
SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
| 123 |
return false; |
| 124 |
} |
| 125 |
fLastResult = 0; |
| 126 |
return true; |
| 127 |
} |
| 128 |
bool setText(const char16_t utftext16[], int utf16Units) override { |
| 129 |
UErrorCode status = U_ZERO_ERROR; |
| 130 |
ICUUText text(sk_utext_openUChars(nullptr, reinterpret_cast<const UChar*>(&utftext16[0]), |
| 131 |
utf16Units, &status)); |
| 132 |
|
| 133 |
if (U_FAILURE(status)) { |
| 134 |
SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
| 135 |
return false; |
| 136 |
} |
| 137 |
SkASSERT(text); |
| 138 |
sk_ubrk_setUText(fBreakIterator.get(), text.get(), &status); |
| 139 |
if (U_FAILURE(status)) { |
| 140 |
SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
| 141 |
return false; |
| 142 |
} |
| 143 |
fLastResult = 0; |
| 144 |
return true; |
| 145 |
} |
| 146 |
}; |
| 147 |
|
| 148 |
class SkIcuBreakIteratorCache final { |
| 149 |
struct Request final { |
| 150 |
Request(SkUnicode::BreakType type, const char* icuLocale) |
| 151 |
: fType(type) |
| 152 |
, fIcuLocale(icuLocale) |
| 153 |
, hash(SkGoodHash()(type) ^ SkGoodHash()(fIcuLocale)) |
| 154 |
{} |
| 155 |
const SkUnicode::BreakType fType; |
| 156 |
const SkString fIcuLocale; |
| 157 |
const uint32_t hash; |
| 158 |
struct Hash { |
| 159 |
uint32_t operator()(const Request& key) const { |
| 160 |
return key.hash; |
| 161 |
} |
| 162 |
}; |
| 163 |
bool operator==(const Request& that) const { |
| 164 |
return this->fType == that.fType && this->fIcuLocale == that.fIcuLocale; |
| 165 |
} |
| 166 |
}; |
| 167 |
/* Every holder of this class is referencing the same (logical) break iterator. |
| 168 |
* Due to caching, the actual break iterator may come and go. |
| 169 |
*/ |
| 170 |
class BreakIteratorRef final { |
| 171 |
public: |
| 172 |
BreakIteratorRef(ICUBreakIterator iter) : breakIterator(iter.release()), fRefCnt(1) { |
| 173 |
++Instances; |
| 174 |
} |
| 175 |
BreakIteratorRef(SkRefCntBase&&) = delete; |
| 176 |
BreakIteratorRef(const SkRefCntBase&) = delete; |
| 177 |
BreakIteratorRef& operator=(SkRefCntBase&&) = delete; |
| 178 |
BreakIteratorRef& operator=(const SkRefCntBase&) = delete; |
| 179 |
~BreakIteratorRef() { |
| 180 |
if (breakIterator) { |
| 181 |
ubrk_close_wrapper(breakIterator); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
void ref() const { |
| 186 |
SkASSERT(fRefCnt > 0); |
| 187 |
++fRefCnt; |
| 188 |
} |
| 189 |
void unref() const { |
| 190 |
SkASSERT(fRefCnt > 0); |
| 191 |
if (1 == fRefCnt--) { |
| 192 |
delete this; |
| 193 |
--Instances; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
UBreakIterator* breakIterator; |
| 198 |
static int32_t GetInstanceCount() { return Instances; } |
| 199 |
private: |
| 200 |
mutable int32_t fRefCnt; |
| 201 |
static int32_t Instances; |
| 202 |
}; |
| 203 |
THashMap<Request, sk_sp<BreakIteratorRef>, Request::Hash> fRequestCache; |
| 204 |
SkMutex fCacheMutex; |
| 205 |
|
| 206 |
void purgeIfNeeded() { |
| 207 |
// If there are too many requests remove some (oldest first?) |
| 208 |
// This may free some break iterators |
| 209 |
if (fRequestCache.count() > 100) { |
| 210 |
// remove the oldest requests |
| 211 |
fRequestCache.reset(); |
| 212 |
} |
| 213 |
// If there are still too many break iterators remove some (oldest first?) |
| 214 |
if (BreakIteratorRef::GetInstanceCount() > 4) { |
| 215 |
// delete the oldest break iterators and set the references to nullptr |
| 216 |
for (auto&& [key, value] : fRequestCache) { |
| 217 |
if (value->breakIterator) { |
| 218 |
sk_ubrk_close(value->breakIterator); |
| 219 |
value->breakIterator = nullptr; |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
public: |
| 226 |
static SkIcuBreakIteratorCache& get() { |
| 227 |
static SkIcuBreakIteratorCache instance; |
| 228 |
return instance; |
| 229 |
} |
| 230 |
|
| 231 |
ICUBreakIterator makeBreakIterator(SkUnicode::BreakType type, const char* bcp47) { |
| 232 |
SkAutoMutexExclusive lock(fCacheMutex); |
| 233 |
UErrorCode status = U_ZERO_ERROR; |
| 234 |
|
| 235 |
// Get ICU locale for BCP47 langtag |
| 236 |
char localeIDStorage[ULOC_FULLNAME_CAPACITY]; |
| 237 |
const char* localeID = nullptr; |
| 238 |
if (bcp47) { |
| 239 |
sk_uloc_forLanguageTag(bcp47, localeIDStorage, ULOC_FULLNAME_CAPACITY, nullptr, &status); |
| 240 |
if (U_FAILURE(status)) { |
| 241 |
SkDEBUGF("Break error could not get language tag: %s", sk_u_errorName(status)); |
| 242 |
} else if (localeIDStorage[0]) { |
| 243 |
localeID = localeIDStorage; |
| 244 |
} |
| 245 |
} |
| 246 |
if (!localeID) { |
| 247 |
localeID = sk_uloc_getDefault(); |
| 248 |
} |
| 249 |
|
| 250 |
auto make = [](const Request& request) -> UBreakIterator* { |
| 251 |
UErrorCode status = U_ZERO_ERROR; |
| 252 |
UBreakIterator* bi = sk_ubrk_open(convertType(request.fType), |
| 253 |
request.fIcuLocale.c_str(), |
| 254 |
nullptr, 0, &status); |
| 255 |
if (U_FAILURE(status)) { |
| 256 |
SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
| 257 |
} |
| 258 |
return bi; |
| 259 |
}; |
| 260 |
|
| 261 |
auto clone = [](const UBreakIterator* existing) -> ICUBreakIterator { |
| 262 |
if (!existing) { |
| 263 |
return nullptr; |
| 264 |
} |
| 265 |
|
| 266 |
UErrorCode status = U_ZERO_ERROR; |
| 267 |
ICUBreakIterator clone(sk_ubrk_clone(existing, &status)); |
| 268 |
if (U_FAILURE(status)) { |
| 269 |
SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
| 270 |
} |
| 271 |
return clone; |
| 272 |
}; |
| 273 |
|
| 274 |
Request request(type, localeID); |
| 275 |
|
| 276 |
// See if this request is already in the cache |
| 277 |
const sk_sp<BreakIteratorRef>* ref = fRequestCache.find(request); |
| 278 |
if (ref) { |
| 279 |
// See if the breakIterator needs to be re-created |
| 280 |
if (!(*ref)->breakIterator) { |
| 281 |
(*ref)->breakIterator = make(request); |
| 282 |
} |
| 283 |
return clone((*ref)->breakIterator); |
| 284 |
} |
| 285 |
|
| 286 |
// This request was not in the cache, create an iterator. |
| 287 |
ICUBreakIterator newIter(make(request)); |
| 288 |
if (!newIter) { |
| 289 |
return nullptr; |
| 290 |
} |
| 291 |
|
| 292 |
sk_sp<BreakIteratorRef> newRef; |
| 293 |
|
| 294 |
// Check if the new iterator is a duplicate |
| 295 |
// Android doesn't expose ubrk_getLocaleByType so there is no means of de-duplicating. |
| 296 |
// ubrk_getAvailable seems like it should work, but the implementation is just every locale. |
| 297 |
if (SkGetICULib()->f_ubrk_getLocaleByType) { |
| 298 |
const char* actualLocale = SkGetICULib()->f_ubrk_getLocaleByType( |
| 299 |
newIter.get(), ULOC_ACTUAL_LOCALE, &status); |
| 300 |
// Android doesn't expose ubrk_getLocaleByType so a wrapper may return an error. |
| 301 |
if (!U_FAILURE(status)) { |
| 302 |
if (!actualLocale) { |
| 303 |
actualLocale = ""; |
| 304 |
} |
| 305 |
// If the actual locale is the same as the requested locale we know there is no entry. |
| 306 |
if (strcmp(actualLocale, localeID) != 0) { |
| 307 |
Request actualRequest(type, actualLocale); |
| 308 |
const sk_sp<BreakIteratorRef>* actualRef = fRequestCache.find(actualRequest); |
| 309 |
if (actualRef) { |
| 310 |
if (!(*actualRef)->breakIterator) { |
| 311 |
(*actualRef)->breakIterator = newIter.release(); |
| 312 |
} |
| 313 |
actualRef = fRequestCache.set(request, *actualRef); |
| 314 |
return clone((*actualRef)->breakIterator); |
| 315 |
} else { |
| 316 |
this->purgeIfNeeded(); |
| 317 |
newRef = sk_make_sp<BreakIteratorRef>(std::move(newIter)); |
| 318 |
fRequestCache.set(actualRequest, newRef); |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
if (!newRef) { |
| 325 |
this->purgeIfNeeded(); |
| 326 |
newRef = sk_make_sp<BreakIteratorRef>(std::move(newIter)); |
| 327 |
} |
| 328 |
fRequestCache.set(request, newRef); |
| 329 |
|
| 330 |
return clone(newRef->breakIterator); |
| 331 |
} |
| 332 |
}; |
| 333 |
/*static*/ int32_t SkIcuBreakIteratorCache::BreakIteratorRef::Instances{0}; |
| 334 |
|
| 335 |
class SkUnicode_icu : public SkUnicode { |
| 336 |
|
| 337 |
static bool extractWords(uint16_t utf16[], int utf16Units, const char* locale, |
| 338 |
std::vector<Position>* words) { |
| 339 |
|
| 340 |
UErrorCode status = U_ZERO_ERROR; |
| 341 |
|
| 342 |
const BreakType type = BreakType::kWords; |
| 343 |
ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(type, locale); |
| 344 |
if (!iterator) { |
| 345 |
SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
| 346 |
return false; |
| 347 |
} |
| 348 |
SkASSERT(iterator); |
| 349 |
|
| 350 |
ICUUText utf16UText(sk_utext_openUChars(nullptr, (UChar*)utf16, utf16Units, &status)); |
| 351 |
if (U_FAILURE(status)) { |
| 352 |
SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
| 353 |
return false; |
| 354 |
} |
| 355 |
|
| 356 |
sk_ubrk_setUText(iterator.get(), utf16UText.get(), &status); |
| 357 |
if (U_FAILURE(status)) { |
| 358 |
SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
| 359 |
return false; |
| 360 |
} |
| 361 |
|
| 362 |
// Get the words |
| 363 |
int32_t pos = sk_ubrk_first(iterator.get()); |
| 364 |
while (pos != UBRK_DONE) { |
| 365 |
words->emplace_back(pos); |
| 366 |
pos = sk_ubrk_next(iterator.get()); |
| 367 |
} |
| 368 |
|
| 369 |
return true; |
| 370 |
} |
| 371 |
|
| 372 |
static bool extractPositions(const char utf8[], int utf8Units, |
| 373 |
BreakType type, const char* locale, |
| 374 |
const std::function<void(int, int)>& setBreak) { |
| 375 |
|
| 376 |
UErrorCode status = U_ZERO_ERROR; |
| 377 |
ICUUText text(sk_utext_openUTF8(nullptr, &utf8[0], utf8Units, &status)); |
| 378 |
if (U_FAILURE(status)) { |
| 379 |
SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
| 380 |
return false; |
| 381 |
} |
| 382 |
SkASSERT(text); |
| 383 |
|
| 384 |
ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(type, locale); |
| 385 |
if (!iterator) { |
| 386 |
return false; |
| 387 |
} |
| 388 |
|
| 389 |
sk_ubrk_setUText(iterator.get(), text.get(), &status); |
| 390 |
if (U_FAILURE(status)) { |
| 391 |
SkDEBUGF("Break error: %s", sk_u_errorName(status)); |
| 392 |
return false; |
| 393 |
} |
| 394 |
|
| 395 |
auto iter = iterator.get(); |
| 396 |
int32_t pos = sk_ubrk_first(iter); |
| 397 |
while (pos != UBRK_DONE) { |
| 398 |
int s = type == SkUnicode::BreakType::kLines |
| 399 |
? UBRK_LINE_SOFT |
| 400 |
: sk_ubrk_getRuleStatus(iter); |
| 401 |
setBreak(pos, s); |
| 402 |
pos = sk_ubrk_next(iter); |
| 403 |
} |
| 404 |
|
| 405 |
if (type == SkUnicode::BreakType::kLines) { |
| 406 |
// This is a workaround for https://bugs.chromium.org/p/skia/issues/detail?id=10715 |
| 407 |
// (ICU line break iterator does not work correctly on Thai text with new lines) |
| 408 |
// So, we only use the iterator to collect soft line breaks and |
| 409 |
// scan the text for all hard line breaks ourselves |
| 410 |
const char* end = utf8 + utf8Units; |
| 411 |
const char* ch = utf8; |
| 412 |
while (ch < end) { |
| 413 |
auto unichar = utf8_next(&ch, end); |
| 414 |
if (SkUnicode_icu::isHardLineBreak(unichar)) { |
| 415 |
setBreak(ch - utf8, UBRK_LINE_HARD); |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
return true; |
| 420 |
} |
| 421 |
|
| 422 |
bool isControl(SkUnichar utf8) override { |
| 423 |
return sk_u_iscntrl(utf8); |
| 424 |
} |
| 425 |
|
| 426 |
bool isWhitespace(SkUnichar utf8) override { |
| 427 |
return sk_u_isWhitespace(utf8); |
| 428 |
} |
| 429 |
|
| 430 |
bool isSpace(SkUnichar utf8) override { |
| 431 |
return sk_u_isspace(utf8); |
| 432 |
} |
| 433 |
|
| 434 |
bool isHardBreak(SkUnichar utf8) override { |
| 435 |
return SkUnicode_icu::isHardLineBreak(utf8); |
| 436 |
} |
| 437 |
|
| 438 |
bool isEmoji(SkUnichar unichar) override { |
| 439 |
return sk_u_hasBinaryProperty(unichar, UCHAR_EMOJI); |
| 440 |
} |
| 441 |
|
| 442 |
bool isEmojiComponent(SkUnichar unichar) override { |
| 443 |
return sk_u_hasBinaryProperty(unichar, UCHAR_EMOJI_COMPONENT); |
| 444 |
} |
| 445 |
|
| 446 |
bool isEmojiModifierBase(SkUnichar unichar) override { |
| 447 |
return sk_u_hasBinaryProperty(unichar, UCHAR_EMOJI_MODIFIER_BASE); |
| 448 |
} |
| 449 |
|
| 450 |
bool isEmojiModifier(SkUnichar unichar) override { |
| 451 |
return sk_u_hasBinaryProperty(unichar, UCHAR_EMOJI_MODIFIER); |
| 452 |
} |
| 453 |
|
| 454 |
bool isRegionalIndicator(SkUnichar unichar) override { |
| 455 |
return sk_u_hasBinaryProperty(unichar, UCHAR_REGIONAL_INDICATOR); |
| 456 |
} |
| 457 |
|
| 458 |
bool isIdeographic(SkUnichar unichar) override { |
| 459 |
return sk_u_hasBinaryProperty(unichar, UCHAR_IDEOGRAPHIC); |
| 460 |
} |
| 461 |
|
| 462 |
bool isTabulation(SkUnichar utf8) override { |
| 463 |
return utf8 == '\t'; |
| 464 |
} |
| 465 |
|
| 466 |
static bool isHardLineBreak(SkUnichar utf8) { |
| 467 |
auto property = sk_u_getIntPropertyValue(utf8, UCHAR_LINE_BREAK); |
| 468 |
return property == U_LB_LINE_FEED || property == U_LB_MANDATORY_BREAK; |
| 469 |
} |
| 470 |
|
| 471 |
public: |
| 472 |
~SkUnicode_icu() override { } |
| 473 |
std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t text[], int count, |
| 474 |
SkBidiIterator::Direction dir) override { |
| 475 |
return fBidiFact->MakeIterator(text, count, dir); |
| 476 |
} |
| 477 |
std::unique_ptr<SkBidiIterator> makeBidiIterator(const char text[], |
| 478 |
int count, |
| 479 |
SkBidiIterator::Direction dir) override { |
| 480 |
return fBidiFact->MakeIterator(text, count, dir); |
| 481 |
} |
| 482 |
std::unique_ptr<SkBreakIterator> makeBreakIterator(const char locale[], |
| 483 |
BreakType type) override { |
| 484 |
ICUBreakIterator iterator = SkIcuBreakIteratorCache::get().makeBreakIterator(type, locale); |
| 485 |
if (!iterator) { |
| 486 |
return nullptr; |
| 487 |
} |
| 488 |
return std::unique_ptr<SkBreakIterator>(new SkBreakIterator_icu(std::move(iterator))); |
| 489 |
} |
| 490 |
std::unique_ptr<SkBreakIterator> makeBreakIterator(BreakType type) override { |
| 491 |
return makeBreakIterator(sk_uloc_getDefault(), type); |
| 492 |
} |
| 493 |
|
| 494 |
SkString toUpper(const SkString& str) override { |
| 495 |
return this->toUpper(str, nullptr); |
| 496 |
} |
| 497 |
|
| 498 |
SkString toUpper(const SkString& str, const char* locale) override { |
| 499 |
// Convert to UTF16 since that's what ICU wants. |
| 500 |
auto str16 = SkUnicode::convertUtf8ToUtf16(str.c_str(), str.size()); |
| 501 |
|
| 502 |
UErrorCode icu_err = U_ZERO_ERROR; |
| 503 |
const auto upper16len = sk_u_strToUpper( |
| 504 |
nullptr, 0, |
| 505 |
reinterpret_cast<const UChar*>(str16.c_str()), str16.size(), |
| 506 |
locale, &icu_err); |
| 507 |
if (icu_err != U_BUFFER_OVERFLOW_ERROR || upper16len <= 0) { |
| 508 |
return SkString(); |
| 509 |
} |
| 510 |
|
| 511 |
AutoSTArray<128, uint16_t> upper16(upper16len); |
| 512 |
icu_err = U_ZERO_ERROR; |
| 513 |
sk_u_strToUpper(reinterpret_cast<UChar*>(upper16.get()), SkToS32(upper16.size()), |
| 514 |
reinterpret_cast<const UChar*>(str16.c_str()), str16.size(), |
| 515 |
locale, &icu_err); |
| 516 |
SkASSERT(!U_FAILURE(icu_err)); |
| 517 |
|
| 518 |
// ... and back to utf8 'cause that's what we want. |
| 519 |
return convertUtf16ToUtf8((char16_t*)upper16.get(), upper16.size()); |
| 520 |
} |
| 521 |
|
| 522 |
bool getBidiRegions(const char utf8[], |
| 523 |
int utf8Units, |
| 524 |
TextDirection dir, |
| 525 |
std::vector<BidiRegion>* results) override { |
| 526 |
return fBidiFact->ExtractBidi(utf8, utf8Units, dir, results); |
| 527 |
} |
| 528 |
|
| 529 |
bool getWords(const char utf8[], int utf8Units, const char* locale, |
| 530 |
std::vector<Position>* results) override { |
| 531 |
|
| 532 |
// Convert to UTF16 since we want the results in utf16 |
| 533 |
auto utf16 = convertUtf8ToUtf16(utf8, utf8Units); |
| 534 |
return SkUnicode_icu::extractWords(reinterpret_cast<uint16_t*>(utf16.data()), utf16.size(), |
| 535 |
locale, results); |
| 536 |
} |
| 537 |
|
| 538 |
bool getUtf8Words(const char utf8[], |
| 539 |
int utf8Units, |
| 540 |
const char* locale, |
| 541 |
std::vector<Position>* results) override { |
| 542 |
// Convert to UTF16 since we want the results in utf16 |
| 543 |
auto utf16 = convertUtf8ToUtf16(utf8, utf8Units); |
| 544 |
std::vector<Position> utf16Results; |
| 545 |
if (!SkUnicode_icu::extractWords(reinterpret_cast<uint16_t*>(utf16.data()), utf16.size(), |
| 546 |
locale, &utf16Results)) |
| 547 |
{ |
| 548 |
return false; |
| 549 |
} |
| 550 |
|
| 551 |
std::vector<Position> mapping; |
| 552 |
SkSpan<const char> text(utf8, utf8Units); |
| 553 |
SkUnicode::extractUtfConversionMapping( |
| 554 |
text, [&](size_t index) { mapping.emplace_back(index); }, [&](size_t index) {}); |
| 555 |
|
| 556 |
for (auto i16 : utf16Results) { |
| 557 |
results->emplace_back(mapping[i16]); |
| 558 |
} |
| 559 |
return true; |
| 560 |
} |
| 561 |
|
| 562 |
bool getSentences(const char utf8[], |
| 563 |
int utf8Units, |
| 564 |
const char* locale, |
| 565 |
std::vector<SkUnicode::Position>* results) override { |
| 566 |
SkUnicode_icu::extractPositions( |
| 567 |
utf8, utf8Units, BreakType::kSentences, nullptr, |
| 568 |
[&](int pos, int status) { |
| 569 |
results->emplace_back(pos); |
| 570 |
}); |
| 571 |
return true; |
| 572 |
} |
| 573 |
|
| 574 |
bool computeCodeUnitFlags(char utf8[], int utf8Units, bool replaceTabs, |
| 575 |
TArray<SkUnicode::CodeUnitFlags, true>* results) override { |
| 576 |
results->clear(); |
| 577 |
results->push_back_n(utf8Units + 1, CodeUnitFlags::kNoCodeUnitFlag); |
| 578 |
|
| 579 |
SkUnicode_icu::extractPositions(utf8, utf8Units, BreakType::kLines, nullptr, // TODO: locale |
| 580 |
[&](int pos, int status) { |
| 581 |
(*results)[pos] |= status == UBRK_LINE_HARD |
| 582 |
? CodeUnitFlags::kHardLineBreakBefore |
| 583 |
: CodeUnitFlags::kSoftLineBreakBefore; |
| 584 |
}); |
| 585 |
|
| 586 |
SkUnicode_icu::extractPositions(utf8, utf8Units, BreakType::kGraphemes, nullptr, //TODO |
| 587 |
[&](int pos, int status) { |
| 588 |
(*results)[pos] |= CodeUnitFlags::kGraphemeStart; |
| 589 |
}); |
| 590 |
|
| 591 |
const char* current = utf8; |
| 592 |
const char* end = utf8 + utf8Units; |
| 593 |
while (current < end) { |
| 594 |
auto before = current - utf8; |
| 595 |
SkUnichar unichar = SkUTF::NextUTF8(¤t, end); |
| 596 |
if (unichar < 0) unichar = 0xFFFD; |
| 597 |
auto after = current - utf8; |
| 598 |
if (replaceTabs && this->isTabulation(unichar)) { |
| 599 |
results->at(before) |= SkUnicode::kTabulation; |
| 600 |
if (replaceTabs) { |
| 601 |
unichar = ' '; |
| 602 |
utf8[before] = ' '; |
| 603 |
} |
| 604 |
} |
| 605 |
for (auto i = before; i < after; ++i) { |
| 606 |
if (this->isSpace(unichar)) { |
| 607 |
results->at(i) |= SkUnicode::kPartOfIntraWordBreak; |
| 608 |
} |
| 609 |
if (this->isWhitespace(unichar)) { |
| 610 |
results->at(i) |= SkUnicode::kPartOfWhiteSpaceBreak; |
| 611 |
} |
| 612 |
if (this->isControl(unichar)) { |
| 613 |
results->at(i) |= SkUnicode::kControl; |
| 614 |
} |
| 615 |
if (this->isIdeographic(unichar)) { |
| 616 |
results->at(i) |= SkUnicode::kIdeographic; |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
return true; |
| 622 |
} |
| 623 |
|
| 624 |
bool computeCodeUnitFlags(char16_t utf16[], int utf16Units, bool replaceTabs, |
| 625 |
TArray<SkUnicode::CodeUnitFlags, true>* results) override { |
| 626 |
results->clear(); |
| 627 |
results->push_back_n(utf16Units + 1, CodeUnitFlags::kNoCodeUnitFlag); |
| 628 |
|
| 629 |
// Get white spaces |
| 630 |
this->forEachCodepoint((char16_t*)&utf16[0], utf16Units, |
| 631 |
[this, results, replaceTabs, &utf16](SkUnichar unichar, int32_t start, int32_t end) { |
| 632 |
for (auto i = start; i < end; ++i) { |
| 633 |
if (replaceTabs && this->isTabulation(unichar)) { |
| 634 |
results->at(i) |= SkUnicode::kTabulation; |
| 635 |
if (replaceTabs) { |
| 636 |
unichar = ' '; |
| 637 |
utf16[start] = ' '; |
| 638 |
} |
| 639 |
} |
| 640 |
if (this->isSpace(unichar)) { |
| 641 |
results->at(i) |= SkUnicode::kPartOfIntraWordBreak; |
| 642 |
} |
| 643 |
if (this->isWhitespace(unichar)) { |
| 644 |
results->at(i) |= SkUnicode::kPartOfWhiteSpaceBreak; |
| 645 |
} |
| 646 |
if (this->isControl(unichar)) { |
| 647 |
results->at(i) |= SkUnicode::kControl; |
| 648 |
} |
| 649 |
} |
| 650 |
}); |
| 651 |
// Get graphemes |
| 652 |
this->forEachBreak((char16_t*)&utf16[0], |
| 653 |
utf16Units, |
| 654 |
SkUnicode::BreakType::kGraphemes, |
| 655 |
[results](SkBreakIterator::Position pos, SkBreakIterator::Status) { |
| 656 |
(*results)[pos] |= CodeUnitFlags::kGraphemeStart; |
| 657 |
}); |
| 658 |
// Get line breaks |
| 659 |
this->forEachBreak( |
| 660 |
(char16_t*)&utf16[0], |
| 661 |
utf16Units, |
| 662 |
SkUnicode::BreakType::kLines, |
| 663 |
[results](SkBreakIterator::Position pos, SkBreakIterator::Status status) { |
| 664 |
if (status == |
| 665 |
(SkBreakIterator::Status)SkUnicode::LineBreakType::kHardLineBreak) { |
| 666 |
// Hard line breaks clears off all the other flags |
| 667 |
// TODO: Treat \n as a formatting mark and do not pass it to SkShaper |
| 668 |
(*results)[pos-1] = CodeUnitFlags::kHardLineBreakBefore; |
| 669 |
} else { |
| 670 |
(*results)[pos] |= CodeUnitFlags::kSoftLineBreakBefore; |
| 671 |
} |
| 672 |
}); |
| 673 |
|
| 674 |
return true; |
| 675 |
} |
| 676 |
|
| 677 |
void reorderVisual(const BidiLevel runLevels[], |
| 678 |
int levelsCount, |
| 679 |
int32_t logicalFromVisual[]) override { |
| 680 |
if (levelsCount == 0) { |
| 681 |
// To avoid an assert in unicode |
| 682 |
return; |
| 683 |
} |
| 684 |
SkASSERT(runLevels != nullptr); |
| 685 |
fBidiFact->bidi_reorderVisual(runLevels, levelsCount, logicalFromVisual); |
| 686 |
} |
| 687 |
|
| 688 |
private: |
| 689 |
sk_sp<SkBidiFactory> fBidiFact = sk_make_sp<SkBidiICUFactory>(); |
| 690 |
}; |
| 691 |
|
| 692 |
namespace SkUnicodes::ICU { |
| 693 |
sk_sp<SkUnicode> Make() { |
| 694 |
// We haven't yet created a way to encode the ICU data for assembly on Windows, |
| 695 |
// so we use a helper library to load icudtl.dat from the harddrive. |
| 696 |
#if defined(SK_USING_THIRD_PARTY_ICU) && defined(SK_BUILD_FOR_WIN) |
| 697 |
if (!SkLoadICU()) { |
| 698 |
static SkOnce once; |
| 699 |
once([] { SkDEBUGF("SkLoadICU() failed!\n"); }); |
| 700 |
return nullptr; |
| 701 |
} |
| 702 |
#endif |
| 703 |
if (SkGetICULib()) { |
| 704 |
return sk_make_sp<SkUnicode_icu>(); |
| 705 |
} |
| 706 |
return nullptr; |
| 707 |
} |
| 708 |
} // namespace SkUnicodes::ICU |