FontLanguageListCache.cpp revision 198b46f1fea3f47ef8eb6317799c0d77aaec52f6
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Minikin"
18
19#include "FontLanguageListCache.h"
20
21#include <cutils/log.h>
22#include <unicode/uloc.h>
23#include <unordered_set>
24
25#include "MinikinInternal.h"
26#include "FontLanguage.h"
27
28namespace android {
29
30const uint32_t FontLanguageListCache::kEmptyListId;
31
32// Returns the text length of output.
33static size_t toLanguageTag(char* output, size_t outSize, const std::string& locale) {
34    output[0] = '\0';
35    if (locale.empty()) {
36        return 0;
37    }
38
39    size_t outLength = 0;
40    UErrorCode uErr = U_ZERO_ERROR;
41    outLength = uloc_canonicalize(locale.c_str(), output, outSize, &uErr);
42    if (U_FAILURE(uErr)) {
43        // unable to build a proper language identifier
44        ALOGD("uloc_canonicalize(\"%s\") failed: %s", locale.c_str(), u_errorName(uErr));
45        output[0] = '\0';
46        return 0;
47    }
48
49    // Preserve "und" and "und-****" since uloc_addLikelySubtags changes "und" to "en-Latn-US".
50    if (strncmp(output, "und", 3) == 0 &&
51        (outLength == 3 || (outLength == 8 && output[3]  == '_'))) {
52        return outLength;
53    }
54
55    char likelyChars[ULOC_FULLNAME_CAPACITY];
56    uErr = U_ZERO_ERROR;
57    uloc_addLikelySubtags(output, likelyChars, ULOC_FULLNAME_CAPACITY, &uErr);
58    if (U_FAILURE(uErr)) {
59        // unable to build a proper language identifier
60        ALOGD("uloc_addLikelySubtags(\"%s\") failed: %s", output, u_errorName(uErr));
61        output[0] = '\0';
62        return 0;
63    }
64
65    uErr = U_ZERO_ERROR;
66    outLength = uloc_toLanguageTag(likelyChars, output, outSize, FALSE, &uErr);
67    if (U_FAILURE(uErr)) {
68        // unable to build a proper language identifier
69        ALOGD("uloc_toLanguageTag(\"%s\") failed: %s", likelyChars, u_errorName(uErr));
70        output[0] = '\0';
71        return 0;
72    }
73#ifdef VERBOSE_DEBUG
74    ALOGD("ICU normalized '%s' to '%s'", locale.c_str(), output);
75#endif
76    return outLength;
77}
78
79static FontLanguages constructFontLanguages(const std::string& input) {
80    FontLanguages result;
81    size_t currentIdx = 0;
82    size_t commaLoc = 0;
83    char langTag[ULOC_FULLNAME_CAPACITY];
84    std::unordered_set<uint64_t> seen;
85    std::string locale(input.size(), 0);
86
87    while ((commaLoc = input.find_first_of(',', currentIdx)) != std::string::npos) {
88        locale.assign(input, currentIdx, commaLoc - currentIdx);
89        currentIdx = commaLoc + 1;
90        size_t length = toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, locale);
91        FontLanguage lang(langTag, length);
92        uint64_t identifier = lang.getIdentifier();
93        if (!lang.isUnsupported() && seen.count(identifier) == 0) {
94            result.push_back(lang);
95            seen.insert(identifier);
96        }
97    }
98    locale.assign(input, currentIdx, input.size() - currentIdx);
99    size_t length = toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, locale);
100    FontLanguage lang(langTag, length);
101    uint64_t identifier = lang.getIdentifier();
102    if (!lang.isUnsupported() && seen.count(identifier) == 0) {
103        result.push_back(lang);
104    }
105    return result;
106}
107
108// static
109uint32_t FontLanguageListCache::getId(const std::string& languages) {
110    FontLanguageListCache* inst = FontLanguageListCache::getInstance();
111    std::unordered_map<std::string, uint32_t>::const_iterator it =
112            inst->mLanguageListLookupTable.find(languages);
113    if (it != inst->mLanguageListLookupTable.end()) {
114        return it->second;
115    }
116
117    // Given language list is not in cache. Insert it and return newly assigned ID.
118    const uint32_t nextId = inst->mLanguageLists.size();
119    FontLanguages fontLanguages = constructFontLanguages(languages);
120    if (fontLanguages.empty()) {
121        return kEmptyListId;
122    }
123    inst->mLanguageLists.push_back(fontLanguages);
124    inst->mLanguageListLookupTable.insert(std::make_pair(languages, nextId));
125    return nextId;
126}
127
128// static
129const FontLanguages& FontLanguageListCache::getById(uint32_t id) {
130    FontLanguageListCache* inst = FontLanguageListCache::getInstance();
131    LOG_ALWAYS_FATAL_IF(id >= inst->mLanguageLists.size(), "Lookup by unknown language list ID.");
132    return inst->mLanguageLists[id];
133}
134
135// static
136FontLanguageListCache* FontLanguageListCache::getInstance() {
137    assertMinikinLocked();
138    static FontLanguageListCache* instance = nullptr;
139    if (instance == nullptr) {
140        instance = new FontLanguageListCache();
141
142        // Insert an empty language list for mapping default language list to kEmptyListId.
143        // The default language list has only one FontLanguage and it is the unsupported language.
144        instance->mLanguageLists.push_back(FontLanguages({FontLanguage()}));
145        instance->mLanguageListLookupTable.insert(std::make_pair("", kEmptyListId));
146    }
147    return instance;
148}
149
150}  // namespace android
151