1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/text/layout_locale.h"
#include <hb.h>
#include <unicode/locid.h>
#include <unicode/ulocdata.h>
#include <array>
#include "base/compiler_specific.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "third_party/blink/renderer/platform/language.h"
#include "third_party/blink/renderer/platform/text/hyphenation.h"
#include "third_party/blink/renderer/platform/text/icu_error.h"
#include "third_party/blink/renderer/platform/text/locale_to_script_mapping.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string_hash.h"
#include "third_party/blink/renderer/platform/wtf/text/case_folding_hash.h"
#include "third_party/blink/renderer/platform/wtf/thread_specific.h"
namespace blink {
namespace {
struct PerThreadData {
HashMap<AtomicString,
scoped_refptr<LayoutLocale>,
CaseFoldingHashTraits<AtomicString>>
locale_map;
raw_ptr<const LayoutLocale> default_locale = nullptr;
raw_ptr<const LayoutLocale> system_locale = nullptr;
raw_ptr<const LayoutLocale> default_locale_for_han = nullptr;
bool default_locale_for_han_computed = false;
String current_accept_languages;
};
PerThreadData& GetPerThreadData() {
DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<PerThreadData>, data, ());
return *data;
}
struct DelimiterConfig {
ULocaleDataDelimiterType type;
raw_ptr<UChar> result;
};
// Use ICU ulocdata to find quote delimiters for an ICU locale
// https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/ulocdata_8h.html#a0bf1fdd1a86918871ae2c84b5ce8421f
scoped_refptr<QuotesData> GetQuotesDataForLanguage(const char* locale) {
UErrorCode status = U_ZERO_ERROR;
// Expect returned buffer size is 1 to match QuotesData type
constexpr int ucharDelimMaxLength = 1;
ULocaleData* uld = ulocdata_open(locale, &status);
if (U_FAILURE(status)) {
ulocdata_close(uld);
return nullptr;
}
std::array<UChar, ucharDelimMaxLength> open1, close1, open2, close2;
int32_t delimResultLength;
struct DelimiterConfig delimiters[] = {
{ULOCDATA_QUOTATION_START, open1.data()},
{ULOCDATA_QUOTATION_END, close1.data()},
{ULOCDATA_ALT_QUOTATION_START, open2.data()},
{ULOCDATA_ALT_QUOTATION_END, close2.data()},
};
for (DelimiterConfig delim : delimiters) {
delimResultLength = ulocdata_getDelimiter(uld, delim.type, delim.result,
ucharDelimMaxLength, &status);
if (U_FAILURE(status) || delimResultLength != 1) {
ulocdata_close(uld);
return nullptr;
}
}
ulocdata_close(uld);
return QuotesData::Create(open1[0], close1[0], open2[0], close2[0]);
}
// Returns the Unicode Line Break Style Identifier (key "lb") value.
// https://www.unicode.org/reports/tr35/#UnicodeLineBreakStyleIdentifier
inline const char* LbValueFromStrictness(LineBreakStrictness strictness) {
switch (strictness) {
case LineBreakStrictness::kDefault:
return nullptr; // nullptr removes any existing values.
case LineBreakStrictness::kNormal:
return "normal";
case LineBreakStrictness::kStrict:
return "strict";
case LineBreakStrictness::kLoose:
return "loose";
}
NOTREACHED();
}
} // namespace
static hb_language_t ToHarfbuzLanguage(const AtomicString& locale) {
std::string locale_as_latin1 = locale.Latin1();
return hb_language_from_string(locale_as_latin1.data(),
static_cast<int>(locale_as_latin1.length()));
}
// SkFontMgr uses two/three-letter language code with an optional ISO 15924
// four-letter script code, in POSIX style (with '-' as the separator,) such as
// "zh-Hant" and "zh-Hans". See `fonts.xml`.
static const char* ToSkFontMgrLocale(UScriptCode script) {
switch (script) {
case USCRIPT_KATAKANA_OR_HIRAGANA:
return "ja";
case USCRIPT_HANGUL:
return "ko";
case USCRIPT_SIMPLIFIED_HAN:
return "zh-Hans";
case USCRIPT_TRADITIONAL_HAN:
return "zh-Hant";
default:
return nullptr;
}
}
const char* LayoutLocale::LocaleForSkFontMgr() const {
if (!string_for_sk_font_mgr_.empty())
return string_for_sk_font_mgr_.c_str();
if (const char* sk_font_mgr_locale = ToSkFontMgrLocale(script_)) {
string_for_sk_font_mgr_ = sk_font_mgr_locale;
DCHECK(!string_for_sk_font_mgr_.empty());
return string_for_sk_font_mgr_.c_str();
}
const icu::Locale locale(Ascii().c_str());
const char* language = locale.getLanguage();
string_for_sk_font_mgr_ = language && *language ? language : "und";
const char* script = locale.getScript();
if (script && *script)
string_for_sk_font_mgr_ = string_for_sk_font_mgr_ + "-" + script;
DCHECK(!string_for_sk_font_mgr_.empty());
return string_for_sk_font_mgr_.c_str();
}
void LayoutLocale::ComputeScriptForHan() const {
if (IsUnambiguousHanScript(script_)) {
script_for_han_ = script_;
has_script_for_han_ = true;
return;
}
script_for_han_ = ScriptCodeForHanFromSubtags(string_);
if (script_for_han_ == USCRIPT_COMMON)
script_for_han_ = USCRIPT_SIMPLIFIED_HAN;
else
has_script_for_han_ = true;
DCHECK(IsUnambiguousHanScript(script_for_han_));
}
UScriptCode LayoutLocale::GetScriptForHan() const {
if (script_for_han_ == USCRIPT_COMMON)
ComputeScriptForHan();
return script_for_han_;
}
bool LayoutLocale::HasScriptForHan() const {
if (script_for_han_ == USCRIPT_COMMON)
ComputeScriptForHan();
return has_script_for_han_;
}
// static
const LayoutLocale* LayoutLocale::LocaleForHan(
const LayoutLocale* content_locale) {
if (content_locale && content_locale->HasScriptForHan())
return content_locale;
PerThreadData& data = GetPerThreadData();
if (!data.default_locale_for_han_computed) [[unlikely]] {
// Use the first acceptLanguages that can disambiguate.
Vector<String> languages;
data.current_accept_languages.Split(',', languages);
for (String token : languages) {
token = token.StripWhiteSpace();
const LayoutLocale* locale = LayoutLocale::Get(AtomicString(token));
if (locale->HasScriptForHan()) {
data.default_locale_for_han = locale;
break;
}
}
if (!data.default_locale_for_han) {
const LayoutLocale& default_locale = GetDefault();
if (default_locale.HasScriptForHan())
data.default_locale_for_han = &default_locale;
}
if (!data.default_locale_for_han) {
const LayoutLocale& system_locale = GetSystem();
if (system_locale.HasScriptForHan())
data.default_locale_for_han = &system_locale;
}
data.default_locale_for_han_computed = true;
}
return data.default_locale_for_han;
}
const char* LayoutLocale::LocaleForHanForSkFontMgr() const {
const char* locale = ToSkFontMgrLocale(GetScriptForHan());
DCHECK(locale);
return locale;
}
void LayoutLocale::ComputeCaseMapLocale() const {
DCHECK(!case_map_computed_);
case_map_computed_ = true;
locale_for_case_map_ = CaseMap::Locale(LocaleString());
}
LayoutLocale::LayoutLocale(const AtomicString& locale)
: string_(locale),
harfbuzz_language_(ToHarfbuzLanguage(locale)),
script_(LocaleToScriptCodeForFontSelection(locale)),
script_for_han_(USCRIPT_COMMON),
has_script_for_han_(false),
hyphenation_computed_(false),
quotes_data_computed_(false),
case_map_computed_(false) {}
// static
const LayoutLocale* LayoutLocale::Get(const AtomicString& locale) {
if (locale.IsNull())
return nullptr;
auto result = GetPerThreadData().locale_map.insert(locale, nullptr);
if (result.is_new_entry)
result.stored_value->value = base::AdoptRef(new LayoutLocale(locale));
return result.stored_value->value.get();
}
// static
const LayoutLocale& LayoutLocale::GetDefault() {
PerThreadData& data = GetPerThreadData();
if (!data.default_locale) [[unlikely]] {
AtomicString language = DefaultLanguage();
data.default_locale =
LayoutLocale::Get(!language.empty() ? language : AtomicString("en"));
}
return *data.default_locale;
}
// static
const LayoutLocale& LayoutLocale::GetSystem() {
PerThreadData& data = GetPerThreadData();
if (!data.system_locale) [[unlikely]] {
// Platforms such as Windows can give more information than the default
// locale, such as "en-JP" for English speakers in Japan.
String name = icu::Locale::getDefault().getName();
data.system_locale =
LayoutLocale::Get(AtomicString(name.Replace('_', '-')));
}
return *data.system_locale;
}
scoped_refptr<LayoutLocale> LayoutLocale::CreateForTesting(
const AtomicString& locale) {
return base::AdoptRef(new LayoutLocale(locale));
}
Hyphenation* LayoutLocale::GetHyphenation() const {
if (hyphenation_computed_)
return hyphenation_.get();
hyphenation_computed_ = true;
hyphenation_ = Hyphenation::PlatformGetHyphenation(LocaleString());
return hyphenation_.get();
}
void LayoutLocale::SetHyphenationForTesting(
const AtomicString& locale_string,
scoped_refptr<Hyphenation> hyphenation) {
const LayoutLocale& locale = ValueOrDefault(Get(locale_string));
locale.hyphenation_computed_ = true;
locale.hyphenation_ = std::move(hyphenation);
}
scoped_refptr<QuotesData> LayoutLocale::GetQuotesData() const {
if (quotes_data_computed_)
return quotes_data_;
quotes_data_computed_ = true;
// BCP 47 uses '-' as the delimiter but ICU uses '_'.
// https://tools.ietf.org/html/bcp47
String normalized_lang = LocaleString();
normalized_lang.Replace('-', '_');
UErrorCode status = U_ZERO_ERROR;
// Use uloc_openAvailableByType() to find all CLDR recognized locales
// https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/uloc_8h.html#aa0332857185774f3e0520a0823c14d16
UEnumeration* ulocales =
uloc_openAvailableByType(ULOC_AVAILABLE_DEFAULT, &status);
if (U_FAILURE(status)) {
uenum_close(ulocales);
return nullptr;
}
// Try to find exact match
while (const char* loc = uenum_next(ulocales, nullptr, &status)) {
if (U_FAILURE(status)) {
uenum_close(ulocales);
return nullptr;
}
if (EqualIgnoringASCIICase(loc, normalized_lang)) {
quotes_data_ = GetQuotesDataForLanguage(loc);
uenum_close(ulocales);
return quotes_data_;
}
}
uenum_close(ulocales);
// No exact match, try to find without subtags.
wtf_size_t hyphen_offset = normalized_lang.ReverseFind('_');
if (hyphen_offset == kNotFound)
return nullptr;
normalized_lang = normalized_lang.Substring(0, hyphen_offset);
ulocales = uloc_openAvailableByType(ULOC_AVAILABLE_DEFAULT, &status);
if (U_FAILURE(status)) {
uenum_close(ulocales);
return nullptr;
}
while (const char* loc = uenum_next(ulocales, nullptr, &status)) {
if (U_FAILURE(status)) {
uenum_close(ulocales);
return nullptr;
}
if (EqualIgnoringASCIICase(loc, normalized_lang)) {
quotes_data_ = GetQuotesDataForLanguage(loc);
uenum_close(ulocales);
return quotes_data_;
}
}
uenum_close(ulocales);
return nullptr;
}
AtomicString LayoutLocale::LocaleWithBreakKeyword(
LineBreakStrictness strictness,
bool use_phrase) const {
if (string_.empty())
return string_;
// uloc_setKeywordValue_58 has a problem to handle "@" in the original
// string. crbug.com/697859
if (string_.Contains('@'))
return string_;
constexpr wtf_size_t kMaxLbValueLen = 6;
constexpr wtf_size_t kMaxKeywordsLen =
/* strlen("@lb=") */ 4 + kMaxLbValueLen + /* strlen("@lw=phrase") */ 10;
class ULocaleKeywordBuilder {
public:
explicit ULocaleKeywordBuilder(const std::string& utf8_locale)
: length_(base::saturated_cast<wtf_size_t>(utf8_locale.length())),
buffer_(length_ + kMaxKeywordsLen + 1, 0) {
// The `buffer_` is initialized to 0 above.
base::span(buffer_).copy_prefix_from(
base::span(utf8_locale).first(length_));
}
explicit ULocaleKeywordBuilder(const String& locale)
: ULocaleKeywordBuilder(locale.Utf8()) {}
AtomicString ToAtomicString() const {
return AtomicString::FromUTF8(base::as_byte_span(buffer_).first(length_));
}
bool SetStrictness(LineBreakStrictness strictness) {
const char* const lb_value = LbValueFromStrictness(strictness);
DCHECK(!lb_value || strlen(lb_value) <= kMaxLbValueLen);
return SetKeywordValue("lb", lb_value);
}
bool SetKeywordValue(const char* keyword_name, const char* value) {
ICUError status;
int32_t length_needed = uloc_setKeywordValue(
keyword_name, value, buffer_.data(), buffer_.size(), &status);
if (U_SUCCESS(status)) {
DCHECK_GE(length_needed, 0);
length_ = length_needed;
DCHECK_LT(length_, buffer_.size());
return true;
}
DCHECK_NE(status, U_BUFFER_OVERFLOW_ERROR);
return false;
}
private:
wtf_size_t length_;
Vector<char> buffer_;
} builder(string_);
if (builder.SetStrictness(strictness) &&
(!use_phrase || builder.SetKeywordValue("lw", "phrase"))) {
return builder.ToAtomicString();
}
NOTREACHED();
}
// static
void LayoutLocale::AcceptLanguagesChanged(const String& accept_languages) {
PerThreadData& data = GetPerThreadData();
if (data.current_accept_languages == accept_languages)
return;
data.current_accept_languages = accept_languages;
data.default_locale_for_han = nullptr;
data.default_locale_for_han_computed = false;
}
// static
void LayoutLocale::ClearForTesting() {
GetPerThreadData() = PerThreadData();
}
} // namespace blink
|