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 std::vector<FontLanguage> parseLanguageList(const std::string& input) {
80    std::vector<FontLanguage> 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            if (result.size() == FONT_LANGUAGES_LIMIT) {
96              break;
97            }
98            seen.insert(identifier);
99        }
100    }
101    if (result.size() < FONT_LANGUAGES_LIMIT) {
102      locale.assign(input, currentIdx, input.size() - currentIdx);
103      size_t length = toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, locale);
104      FontLanguage lang(langTag, length);
105      uint64_t identifier = lang.getIdentifier();
106      if (!lang.isUnsupported() && seen.count(identifier) == 0) {
107          result.push_back(lang);
108      }
109    }
110    return result;
111}
112
113// static
114uint32_t FontLanguageListCache::getId(const std::string& languages) {
115    FontLanguageListCache* inst = FontLanguageListCache::getInstance();
116    std::unordered_map<std::string, uint32_t>::const_iterator it =
117            inst->mLanguageListLookupTable.find(languages);
118    if (it != inst->mLanguageListLookupTable.end()) {
119        return it->second;
120    }
121
122    // Given language list is not in cache. Insert it and return newly assigned ID.
123    const uint32_t nextId = inst->mLanguageLists.size();
124    FontLanguages fontLanguages(parseLanguageList(languages));
125    if (fontLanguages.empty()) {
126        return kEmptyListId;
127    }
128    inst->mLanguageLists.push_back(std::move(fontLanguages));
129    inst->mLanguageListLookupTable.insert(std::make_pair(languages, nextId));
130    return nextId;
131}
132
133// static
134const FontLanguages& FontLanguageListCache::getById(uint32_t id) {
135    FontLanguageListCache* inst = FontLanguageListCache::getInstance();
136    LOG_ALWAYS_FATAL_IF(id >= inst->mLanguageLists.size(), "Lookup by unknown language list ID.");
137    return inst->mLanguageLists[id];
138}
139
140// static
141FontLanguageListCache* FontLanguageListCache::getInstance() {
142    assertMinikinLocked();
143    static FontLanguageListCache* instance = nullptr;
144    if (instance == nullptr) {
145        instance = new FontLanguageListCache();
146
147        // Insert an empty language list for mapping default language list to kEmptyListId.
148        // The default language list has only one FontLanguage and it is the unsupported language.
149        instance->mLanguageLists.push_back(FontLanguages());
150        instance->mLanguageListLookupTable.insert(std::make_pair("", kEmptyListId));
151    }
152    return instance;
153}
154
155}  // namespace android
156