Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1062)

Unified Diff: third_party/WebKit/Source/platform/fonts/AcceptLanguagesResolver.cpp

Issue 1693563003: Take Accept-Language into account in CJK font fallback for Android/Win (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@aceept-lang
Patch Set: Fix non-Windows builds Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/platform/fonts/AcceptLanguagesResolver.cpp
diff --git a/third_party/WebKit/Source/platform/fonts/AcceptLanguagesResolver.cpp b/third_party/WebKit/Source/platform/fonts/AcceptLanguagesResolver.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..dd67516705b201673ba410be2d6e25eb126894ff
--- /dev/null
+++ b/third_party/WebKit/Source/platform/fonts/AcceptLanguagesResolver.cpp
@@ -0,0 +1,78 @@
+// 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/AcceptLanguagesResolver.h"
+
+#include "platform/Language.h"
+#include "platform/text/LocaleToScriptMapping.h"
+#include <unicode/locid.h>
+
+namespace blink {
+
+UScriptCode AcceptLanguagesResolver::m_preferredHanScript = USCRIPT_COMMON;
+const char* AcceptLanguagesResolver::m_preferredHanSkFontMgrLocale = nullptr;
+
+// SkFontMgr requires script-based locale names, like "zh-Hant" and "zh-Hans",
+// instead of "zh-CN" and "zh-TW".
+static const char* toSkFontMgrLocaleForHan(UScriptCode script)
+{
+ switch (script) {
+ case USCRIPT_KATAKANA_OR_HIRAGANA:
+ return "ja-jp";
+ case USCRIPT_HANGUL:
+ return "ko-kr";
+ case USCRIPT_SIMPLIFIED_HAN:
+ return "zh-Hans";
+ case USCRIPT_TRADITIONAL_HAN:
+ return "zh-Hant";
+ default:
+ ASSERT_NOT_REACHED();
+ return nullptr;
+ }
+}
+
+void AcceptLanguagesResolver::acceptLanguagesChanged(
+ const String& acceptLanguages)
+{
+ // Use the UI locale if it can disambiguate the Unified Han.
+ // Historically we use ICU on Windows. crbug.com/586520
+#if OS(WIN)
+ // Since Chrome synchronizes the ICU default locale with its UI locale,
+ // this ICU locale tells the current UI locale of Chrome.
+ m_preferredHanScript = scriptCodeForHanFromLocale(
+ icu::Locale::getDefault().getName(), '_');
+#else
+ m_preferredHanScript = scriptCodeForHanFromLocale(defaultLanguage());
+#endif
+ if (m_preferredHanScript != USCRIPT_COMMON) {
+ // We don't need additional locale if defaultLanguage() can disambiguate
+ // since it's always passed to matchFamilyStyleCharacter() separately.
+ m_preferredHanSkFontMgrLocale = nullptr;
+ return;
+ }
+
+ updateFromAcceptLanguages(acceptLanguages);
+}
+
+void AcceptLanguagesResolver::updateFromAcceptLanguages(
+ const String& acceptLanguages)
+{
+ // Use the first acceptLanguages that can disambiguate.
+ Vector<String> languages;
+ acceptLanguages.split(',', languages);
+ for (String token : languages) {
+ token = token.stripWhiteSpace();
+ m_preferredHanScript = scriptCodeForHanFromLocale(token);
+ if (m_preferredHanScript != USCRIPT_COMMON) {
+ m_preferredHanSkFontMgrLocale = toSkFontMgrLocaleForHan(
+ m_preferredHanScript);
+ return;
+ }
+ }
+
+ m_preferredHanScript = USCRIPT_COMMON;
+ m_preferredHanSkFontMgrLocale = nullptr;
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698