summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/platform/fonts/AcceptLanguagesResolver.cpp
blob: dd67516705b201673ba410be2d6e25eb126894ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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