Index: third_party/WebKit/Source/platform/fonts/FontLocale.cpp |
diff --git a/third_party/WebKit/Source/platform/fonts/FontLocale.cpp b/third_party/WebKit/Source/platform/fonts/FontLocale.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..29586def57f0f7f82ee5b81126af6c7b2f1f5dfa |
--- /dev/null |
+++ b/third_party/WebKit/Source/platform/fonts/FontLocale.cpp |
@@ -0,0 +1,92 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "platform/fonts/FontLocale.h" |
+ |
+#include "platform/Language.h" |
+#include "platform/text/Hyphenation.h" |
+#include "platform/text/LocaleToScriptMapping.h" |
+#include "wtf/HashMap.h" |
+#include "wtf/text/AtomicStringHash.h" |
+#include "wtf/text/StringHash.h" |
+ |
+#include <hb.h> |
+ |
+namespace blink { |
+ |
+using FontLocaleMap = HashMap<AtomicString, RefPtr<FontLocale>, CaseFoldingHash>; |
+ |
+static FontLocaleMap& getLocaleMap() |
+{ |
+ DEFINE_STATIC_LOCAL(FontLocaleMap, localeMap, ()); |
+ return localeMap; |
+} |
+ |
+const FontLocale* FontLocale::get(const AtomicString& locale) |
+{ |
+ if (locale.isNull()) |
+ return nullptr; |
+ auto result = getLocaleMap().add(locale, nullptr); |
+ if (result.isNewEntry) |
+ result.storedValue->value = adoptRef(new FontLocale(locale.lower())); |
+ return result.storedValue->value.get(); |
+} |
+ |
+static const FontLocale* computeDefaultLocale() |
+{ |
+ AtomicString locale = defaultLanguage(); |
+ if (locale.isEmpty()) |
+ locale = AtomicString("en"); |
+ return FontLocale::get(locale); |
+} |
+ |
+const FontLocale& FontLocale::getDefault() |
+{ |
+ DEFINE_STATIC_LOCAL(const FontLocale*, defaultLocale, (computeDefaultLocale())); |
+ return *defaultLocale; |
+} |
+ |
+static hb_language_t toHarfbuzLanguage(const AtomicString& locale) |
+{ |
+ CString localeAsLatin1 = locale.latin1(); |
+ return hb_language_from_string(localeAsLatin1.data(), localeAsLatin1.length()); |
+} |
+ |
+FontLocale::FontLocale(const AtomicString& locale) |
+ : m_string(locale) |
+ , m_script(localeToScriptCodeForFontSelection(locale)) |
+ , m_harfbuzzLanguage(static_cast<const void*>(toHarfbuzLanguage(locale))) |
+{ |
+} |
+ |
+// SkFontMgr requires script-based locale names, like "zh-Hant" and "zh-Hans", |
+// instead of "zh-CN" and "zh-TW". |
+static CString toSkFontMgrLocale(const String& locale) |
+{ |
+ if (!locale.startsWith("zh", TextCaseInsensitive)) |
+ return locale.ascii(); |
+ |
+ switch (localeToScriptCodeForFontSelection(locale)) { |
+ case USCRIPT_SIMPLIFIED_HAN: |
+ return "zh-Hans"; |
+ case USCRIPT_TRADITIONAL_HAN: |
+ return "zh-Hant"; |
+ default: |
+ return locale.ascii(); |
+ } |
+} |
+ |
+const CString& FontLocale::localeForSkFontMgr() const |
+{ |
+ if (m_stringForSkFontMgr.isNull()) |
+ m_stringForSkFontMgr = toSkFontMgrLocale(m_string); |
+ return m_stringForSkFontMgr; |
+} |
+ |
+Hyphenation* FontLocale::getHyphenation() const |
+{ |
+ return Hyphenation::get(localeString()); |
+} |
+ |
+} // namespace blink |