• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unicode/uloc.h>
22 #include <unicode/umachine.h>
23 #include <unordered_set>
24 
25 #include <log/log.h>
26 
27 #include "FontLanguage.h"
28 #include "MinikinInternal.h"
29 
30 namespace minikin {
31 
32 const uint32_t FontLanguageListCache::kEmptyListId;
33 
34 // Returns the text length of output.
toLanguageTag(char * output,size_t outSize,const std::string & locale)35 static size_t toLanguageTag(char* output,
36                             size_t outSize,
37                             const std::string& locale) {
38   output[0] = '\0';
39   if (locale.empty()) {
40     return 0;
41   }
42 
43   size_t outLength = 0;
44   UErrorCode uErr = U_ZERO_ERROR;
45   outLength = uloc_canonicalize(locale.c_str(), output, outSize, &uErr);
46   if (U_FAILURE(uErr)) {
47     // unable to build a proper language identifier
48     ALOGD("uloc_canonicalize(\"%s\") failed: %s", locale.c_str(),
49           u_errorName(uErr));
50     output[0] = '\0';
51     return 0;
52   }
53 
54   // Preserve "und" and "und-****" since uloc_addLikelySubtags changes "und" to
55   // "en-Latn-US".
56   if (strncmp(output, "und", 3) == 0 &&
57       (outLength == 3 || (outLength == 8 && output[3] == '_'))) {
58     return outLength;
59   }
60 
61   char likelyChars[ULOC_FULLNAME_CAPACITY];
62   uErr = U_ZERO_ERROR;
63   uloc_addLikelySubtags(output, likelyChars, ULOC_FULLNAME_CAPACITY, &uErr);
64   if (U_FAILURE(uErr)) {
65     // unable to build a proper language identifier
66     ALOGD("uloc_addLikelySubtags(\"%s\") failed: %s", output,
67           u_errorName(uErr));
68     output[0] = '\0';
69     return 0;
70   }
71 
72   uErr = U_ZERO_ERROR;
73   outLength =
74       uloc_toLanguageTag(likelyChars, output, outSize, /*false*/ 0, &uErr);
75   if (U_FAILURE(uErr)) {
76     // unable to build a proper language identifier
77     ALOGD("uloc_toLanguageTag(\"%s\") failed: %s", likelyChars,
78           u_errorName(uErr));
79     output[0] = '\0';
80     return 0;
81   }
82 #ifdef VERBOSE_DEBUG
83   ALOGD("ICU normalized '%s' to '%s'", locale.c_str(), output);
84 #endif
85   return outLength;
86 }
87 
parseLanguageList(const std::string & input)88 static std::vector<FontLanguage> parseLanguageList(const std::string& input) {
89   std::vector<FontLanguage> result;
90   size_t currentIdx = 0;
91   size_t commaLoc = 0;
92   char langTag[ULOC_FULLNAME_CAPACITY];
93   std::unordered_set<uint64_t> seen;
94   std::string locale(input.size(), 0);
95 
96   while ((commaLoc = input.find_first_of(',', currentIdx)) !=
97          std::string::npos) {
98     locale.assign(input, currentIdx, commaLoc - currentIdx);
99     currentIdx = commaLoc + 1;
100     size_t length = toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, locale);
101     FontLanguage lang(langTag, length);
102     uint64_t identifier = lang.getIdentifier();
103     if (!lang.isUnsupported() && seen.count(identifier) == 0) {
104       result.push_back(lang);
105       if (result.size() == FONT_LANGUAGES_LIMIT) {
106         break;
107       }
108       seen.insert(identifier);
109     }
110   }
111   if (result.size() < FONT_LANGUAGES_LIMIT) {
112     locale.assign(input, currentIdx, input.size() - currentIdx);
113     size_t length = toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, locale);
114     FontLanguage lang(langTag, length);
115     uint64_t identifier = lang.getIdentifier();
116     if (!lang.isUnsupported() && seen.count(identifier) == 0) {
117       result.push_back(lang);
118     }
119   }
120   return result;
121 }
122 
123 // static
getId(const std::string & languages)124 uint32_t FontLanguageListCache::getId(const std::string& languages) {
125   FontLanguageListCache* inst = FontLanguageListCache::getInstance();
126   std::unordered_map<std::string, uint32_t>::const_iterator it =
127       inst->mLanguageListLookupTable.find(languages);
128   if (it != inst->mLanguageListLookupTable.end()) {
129     return it->second;
130   }
131 
132   // Given language list is not in cache. Insert it and return newly assigned
133   // ID.
134   const uint32_t nextId = inst->mLanguageLists.size();
135   FontLanguages fontLanguages(parseLanguageList(languages));
136   if (fontLanguages.empty()) {
137     return kEmptyListId;
138   }
139   inst->mLanguageLists.push_back(std::move(fontLanguages));
140   inst->mLanguageListLookupTable.insert(std::make_pair(languages, nextId));
141   return nextId;
142 }
143 
144 // static
getById(uint32_t id)145 const FontLanguages& FontLanguageListCache::getById(uint32_t id) {
146   FontLanguageListCache* inst = FontLanguageListCache::getInstance();
147   LOG_ALWAYS_FATAL_IF(id >= inst->mLanguageLists.size(),
148                       "Lookup by unknown language list ID.");
149   return inst->mLanguageLists[id];
150 }
151 
152 // static
getInstance()153 FontLanguageListCache* FontLanguageListCache::getInstance() {
154   assertMinikinLocked();
155   static FontLanguageListCache* instance = nullptr;
156   if (instance == nullptr) {
157     instance = new FontLanguageListCache();
158 
159     // Insert an empty language list for mapping default language list to
160     // kEmptyListId. The default language list has only one FontLanguage and it
161     // is the unsupported language.
162     instance->mLanguageLists.push_back(FontLanguages());
163     instance->mLanguageListLookupTable.insert(std::make_pair("", kEmptyListId));
164   }
165   return instance;
166 }
167 
168 }  // namespace minikin
169