OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "platform/LayoutLocale.h" |
| 6 |
| 7 #include "platform/Language.h" |
| 8 #include "platform/text/LocaleToScriptMapping.h" |
| 9 #include "wtf/HashMap.h" |
| 10 #include "wtf/text/AtomicStringHash.h" |
| 11 #include "wtf/text/StringHash.h" |
| 12 |
| 13 #include <hb.h> |
| 14 |
| 15 namespace blink { |
| 16 |
| 17 const LayoutLocale* LayoutLocale::s_default = nullptr; |
| 18 |
| 19 static hb_language_t toHarfbuzLanguage(const AtomicString& locale) |
| 20 { |
| 21 CString localeAsLatin1 = locale.latin1(); |
| 22 return hb_language_from_string(localeAsLatin1.data(), localeAsLatin1.length(
)); |
| 23 } |
| 24 |
| 25 // SkFontMgr requires script-based locale names, like "zh-Hant" and "zh-Hans", |
| 26 // instead of "zh-CN" and "zh-TW". |
| 27 static CString toSkFontMgrLocale(const String& locale) |
| 28 { |
| 29 if (!locale.startsWith("zh", TextCaseInsensitive)) |
| 30 return locale.ascii(); |
| 31 |
| 32 switch (localeToScriptCodeForFontSelection(locale)) { |
| 33 case USCRIPT_SIMPLIFIED_HAN: |
| 34 return "zh-Hans"; |
| 35 case USCRIPT_TRADITIONAL_HAN: |
| 36 return "zh-Hant"; |
| 37 default: |
| 38 return locale.ascii(); |
| 39 } |
| 40 } |
| 41 |
| 42 const CString& LayoutLocale::localeForSkFontMgr() const |
| 43 { |
| 44 if (m_stringForSkFontMgr.isNull()) |
| 45 m_stringForSkFontMgr = toSkFontMgrLocale(m_string); |
| 46 return m_stringForSkFontMgr; |
| 47 } |
| 48 |
| 49 UScriptCode LayoutLocale::scriptForHan() const |
| 50 { |
| 51 if (m_scriptForHan != USCRIPT_COMMON) |
| 52 return m_scriptForHan; |
| 53 |
| 54 m_scriptForHan = scriptCodeForHanFromLocale(script(), localeString()); |
| 55 if (m_scriptForHan == USCRIPT_COMMON) |
| 56 m_scriptForHan = USCRIPT_HAN; |
| 57 return m_scriptForHan; |
| 58 } |
| 59 |
| 60 LayoutLocale::LayoutLocale(const AtomicString& locale) |
| 61 : m_string(locale) |
| 62 , m_harfbuzzLanguage(toHarfbuzLanguage(locale)) |
| 63 , m_script(localeToScriptCodeForFontSelection(locale)) |
| 64 , m_scriptForHan(USCRIPT_COMMON) |
| 65 , m_hyphenationComputed(false) |
| 66 { |
| 67 } |
| 68 |
| 69 using LayoutLocaleMap = HashMap<AtomicString, RefPtr<LayoutLocale>, CaseFoldingH
ash>; |
| 70 |
| 71 static LayoutLocaleMap& getLocaleMap() |
| 72 { |
| 73 DEFINE_STATIC_LOCAL(LayoutLocaleMap, localeMap, ()); |
| 74 return localeMap; |
| 75 } |
| 76 |
| 77 const LayoutLocale* LayoutLocale::get(const AtomicString& locale) |
| 78 { |
| 79 if (locale.isNull()) |
| 80 return nullptr; |
| 81 |
| 82 auto result = getLocaleMap().add(locale, nullptr); |
| 83 if (result.isNewEntry) |
| 84 result.storedValue->value = adoptRef(new LayoutLocale(locale)); |
| 85 return result.storedValue->value.get(); |
| 86 } |
| 87 |
| 88 static const LayoutLocale* computeDefaultLocale() |
| 89 { |
| 90 AtomicString locale = defaultLanguage(); |
| 91 if (!locale.isEmpty()) |
| 92 return LayoutLocale::get(locale); |
| 93 return LayoutLocale::get("en"); |
| 94 } |
| 95 |
| 96 const LayoutLocale& LayoutLocale::getDefault() |
| 97 { |
| 98 if (!s_default) |
| 99 s_default = computeDefaultLocale(); |
| 100 return *s_default; |
| 101 } |
| 102 |
| 103 void LayoutLocale::clearForTesting() |
| 104 { |
| 105 s_default = nullptr; |
| 106 getLocaleMap().clear(); |
| 107 } |
| 108 |
| 109 Hyphenation* LayoutLocale::getHyphenation() const |
| 110 { |
| 111 if (m_hyphenationComputed) |
| 112 return m_hyphenation.get(); |
| 113 |
| 114 m_hyphenationComputed = true; |
| 115 m_hyphenation = Hyphenation::platformGetHyphenation(localeString()); |
| 116 return m_hyphenation.get(); |
| 117 } |
| 118 |
| 119 void LayoutLocale::setHyphenationForTesting(const AtomicString& localeString, Pa
ssRefPtr<Hyphenation> hyphenation) |
| 120 { |
| 121 const LayoutLocale& locale = valueOrDefault(get(localeString)); |
| 122 locale.m_hyphenationComputed = true; |
| 123 locale.m_hyphenation = hyphenation; |
| 124 } |
| 125 |
| 126 } // namespace blink |
OLD | NEW |