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/fonts/FontLocale.h" |
| 6 |
| 7 #include "platform/Language.h" |
| 8 #include "platform/text/Hyphenation.h" |
| 9 #include "platform/text/LocaleToScriptMapping.h" |
| 10 #include "wtf/HashMap.h" |
| 11 #include "wtf/text/AtomicStringHash.h" |
| 12 #include "wtf/text/StringHash.h" |
| 13 |
| 14 #include <hb.h> |
| 15 |
| 16 namespace blink { |
| 17 |
| 18 using FontLocaleMap = HashMap<AtomicString, RefPtr<FontLocale>, CaseFoldingHash>
; |
| 19 |
| 20 static FontLocaleMap& getLocaleMap() |
| 21 { |
| 22 DEFINE_STATIC_LOCAL(FontLocaleMap, localeMap, ()); |
| 23 return localeMap; |
| 24 } |
| 25 |
| 26 const FontLocale* FontLocale::get(const AtomicString& locale) |
| 27 { |
| 28 if (locale.isNull()) |
| 29 return nullptr; |
| 30 auto result = getLocaleMap().add(locale, nullptr); |
| 31 if (result.isNewEntry) |
| 32 result.storedValue->value = adoptRef(new FontLocale(locale.lower())); |
| 33 return result.storedValue->value.get(); |
| 34 } |
| 35 |
| 36 static const FontLocale* computeDefaultLocale() |
| 37 { |
| 38 AtomicString locale = defaultLanguage(); |
| 39 if (locale.isEmpty()) |
| 40 locale = AtomicString("en"); |
| 41 return FontLocale::get(locale); |
| 42 } |
| 43 |
| 44 const FontLocale& FontLocale::getDefault() |
| 45 { |
| 46 DEFINE_STATIC_LOCAL(const FontLocale*, defaultLocale, (computeDefaultLocale(
))); |
| 47 return *defaultLocale; |
| 48 } |
| 49 |
| 50 static hb_language_t toHarfbuzLanguage(const AtomicString& locale) |
| 51 { |
| 52 CString localeAsLatin1 = locale.latin1(); |
| 53 return hb_language_from_string(localeAsLatin1.data(), localeAsLatin1.length(
)); |
| 54 } |
| 55 |
| 56 FontLocale::FontLocale(const AtomicString& locale) |
| 57 : m_string(locale) |
| 58 , m_script(localeToScriptCodeForFontSelection(locale)) |
| 59 , m_harfbuzzLanguage(static_cast<const void*>(toHarfbuzLanguage(locale))) |
| 60 { |
| 61 } |
| 62 |
| 63 // SkFontMgr requires script-based locale names, like "zh-Hant" and "zh-Hans", |
| 64 // instead of "zh-CN" and "zh-TW". |
| 65 static CString toSkFontMgrLocale(const String& locale) |
| 66 { |
| 67 if (!locale.startsWith("zh", TextCaseInsensitive)) |
| 68 return locale.ascii(); |
| 69 |
| 70 switch (localeToScriptCodeForFontSelection(locale)) { |
| 71 case USCRIPT_SIMPLIFIED_HAN: |
| 72 return "zh-Hans"; |
| 73 case USCRIPT_TRADITIONAL_HAN: |
| 74 return "zh-Hant"; |
| 75 default: |
| 76 return locale.ascii(); |
| 77 } |
| 78 } |
| 79 |
| 80 const CString& FontLocale::localeForSkFontMgr() const |
| 81 { |
| 82 if (m_stringForSkFontMgr.isNull()) |
| 83 m_stringForSkFontMgr = toSkFontMgrLocale(m_string); |
| 84 return m_stringForSkFontMgr; |
| 85 } |
| 86 |
| 87 Hyphenation* FontLocale::getHyphenation() const |
| 88 { |
| 89 return Hyphenation::get(localeString()); |
| 90 } |
| 91 |
| 92 } // namespace blink |
OLD | NEW |