OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/fonts/AcceptLanguagesResolver.h" | 5 #include "platform/fonts/AcceptLanguagesResolver.h" |
6 | 6 |
7 #include "platform/Language.h" | 7 #include "platform/LayoutLocale.h" |
8 #include "platform/text/LocaleToScriptMapping.h" | |
9 #include <unicode/locid.h> | |
10 | 8 |
11 namespace blink { | 9 namespace blink { |
12 | 10 |
13 UScriptCode AcceptLanguagesResolver::m_preferredHanScript = USCRIPT_COMMON; | |
14 const char* AcceptLanguagesResolver::m_preferredHanSkFontMgrLocale = nullptr; | |
15 | |
16 // SkFontMgr requires script-based locale names, like "zh-Hant" and "zh-Hans", | |
17 // instead of "zh-CN" and "zh-TW". | |
18 static const char* toSkFontMgrLocaleForHan(UScriptCode script) | |
19 { | |
20 switch (script) { | |
21 case USCRIPT_KATAKANA_OR_HIRAGANA: | |
22 return "ja-jp"; | |
23 case USCRIPT_HANGUL: | |
24 return "ko-kr"; | |
25 case USCRIPT_SIMPLIFIED_HAN: | |
26 return "zh-Hans"; | |
27 case USCRIPT_TRADITIONAL_HAN: | |
28 return "zh-Hant"; | |
29 default: | |
30 ASSERT_NOT_REACHED(); | |
31 return nullptr; | |
32 } | |
33 } | |
34 | |
35 void AcceptLanguagesResolver::acceptLanguagesChanged( | 11 void AcceptLanguagesResolver::acceptLanguagesChanged( |
36 const String& acceptLanguages) | 12 const String& acceptLanguages) |
37 { | 13 { |
38 // Use the UI locale if it can disambiguate the Unified Han. | 14 LayoutLocale::setLocaleForHan( |
drott
2016/07/28 08:05:23
This line looks a bit surprising to me. Is this tr
kojii
2016/07/29 04:27:40
No, the returned object is const. The pointer to t
drott
2016/08/02 07:14:15
Got it, thanks for the explanation. Now I see that
| |
39 // Historically we use ICU on Windows. crbug.com/586520 | 15 localeForHanFromAcceptLanguages(acceptLanguages)); |
40 #if OS(WIN) | |
41 // Since Chrome synchronizes the ICU default locale with its UI locale, | |
42 // this ICU locale tells the current UI locale of Chrome. | |
43 m_preferredHanScript = scriptCodeForHanFromLocale( | |
44 icu::Locale::getDefault().getName(), '_'); | |
45 #else | |
46 m_preferredHanScript = scriptCodeForHanFromLocale(defaultLanguage()); | |
47 #endif | |
48 if (m_preferredHanScript != USCRIPT_COMMON) { | |
49 // We don't need additional locale if defaultLanguage() can disambiguate | |
50 // since it's always passed to matchFamilyStyleCharacter() separately. | |
51 m_preferredHanSkFontMgrLocale = nullptr; | |
52 return; | |
53 } | |
54 | |
55 updateFromAcceptLanguages(acceptLanguages); | |
56 } | 16 } |
57 | 17 |
58 void AcceptLanguagesResolver::updateFromAcceptLanguages( | 18 const LayoutLocale* AcceptLanguagesResolver::localeForHanFromAcceptLanguages( |
59 const String& acceptLanguages) | 19 const String& acceptLanguages) |
60 { | 20 { |
61 // Use the first acceptLanguages that can disambiguate. | 21 // Use the first acceptLanguages that can disambiguate. |
62 Vector<String> languages; | 22 Vector<String> languages; |
63 acceptLanguages.split(',', languages); | 23 acceptLanguages.split(',', languages); |
64 for (String token : languages) { | 24 for (String token : languages) { |
65 token = token.stripWhiteSpace(); | 25 token = token.stripWhiteSpace(); |
66 m_preferredHanScript = scriptCodeForHanFromLocale(token); | 26 const LayoutLocale* locale = LayoutLocale::get(AtomicString(token)); |
67 if (m_preferredHanScript != USCRIPT_COMMON) { | 27 if (locale->hasScriptForHan()) |
68 m_preferredHanSkFontMgrLocale = toSkFontMgrLocaleForHan( | 28 return locale; |
69 m_preferredHanScript); | |
70 return; | |
71 } | |
72 } | 29 } |
73 | 30 |
74 m_preferredHanScript = USCRIPT_COMMON; | 31 return nullptr; |
75 m_preferredHanSkFontMgrLocale = nullptr; | |
76 } | 32 } |
77 | 33 |
78 } // namespace blink | 34 } // namespace blink |
OLD | NEW |