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