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 "LocaleListCache.h"
20
21 #include <unordered_set>
22
23 #include <log/log.h>
24 #include <minikin/Hasher.h>
25 #include <minikin/LocaleList.h>
26 #include <unicode/uloc.h>
27 #include <unicode/umachine.h>
28
29 #include "Locale.h"
30 #include "MinikinInternal.h"
31
32 namespace minikin {
33
34 // Returns the text length of output.
toLanguageTag(char * output,size_t outSize,const StringPiece & locale)35 static size_t toLanguageTag(char* output, size_t outSize, const StringPiece& locale) {
36 output[0] = '\0';
37 if (locale.empty()) {
38 return 0;
39 }
40
41 std::string localeString = locale.toString(); // ICU only understands C-style string.
42
43 size_t outLength = 0;
44 UErrorCode uErr = U_ZERO_ERROR;
45 outLength = uloc_canonicalize(localeString.c_str(), output, outSize, &uErr);
46 if (U_FAILURE(uErr) || (uErr == U_STRING_NOT_TERMINATED_WARNING)) {
47 // unable to build a proper locale identifier
48 ALOGD("uloc_canonicalize(\"%s\") failed: %s", localeString.c_str(), u_errorName(uErr));
49 output[0] = '\0';
50 return 0;
51 }
52
53 // Preserve "" and "_****" since uloc_addLikelySubtags changes "" to "en_Latn_US".
54 if (outLength == 0 || (outLength == 5 && output[0] == '_')) {
55 if (output[0] == '_') {
56 output[0] = '-';
57 }
58 std::string buf(output, outLength);
59 outLength = (size_t)snprintf(output, outSize, "und%s", buf.c_str());
60 return outLength;
61 }
62
63 char likelyChars[ULOC_FULLNAME_CAPACITY];
64 uErr = U_ZERO_ERROR;
65 uloc_addLikelySubtags(output, likelyChars, ULOC_FULLNAME_CAPACITY, &uErr);
66 if (U_FAILURE(uErr) || (uErr == U_STRING_NOT_TERMINATED_WARNING)) {
67 // unable to build a proper locale identifier
68 ALOGD("uloc_addLikelySubtags(\"%s\") failed: %s", output, u_errorName(uErr));
69 output[0] = '\0';
70 return 0;
71 }
72
73 uErr = U_ZERO_ERROR;
74 outLength = uloc_toLanguageTag(likelyChars, output, outSize, false, &uErr);
75 if (U_FAILURE(uErr) || (uErr == U_STRING_NOT_TERMINATED_WARNING)) {
76 // unable to build a proper locale identifier
77 ALOGD("uloc_toLanguageTag(\"%s\") failed: %s", likelyChars, u_errorName(uErr));
78 output[0] = '\0';
79 return 0;
80 }
81 return outLength;
82 }
83
parseLocaleList(const std::string & input)84 static std::vector<Locale> parseLocaleList(const std::string& input) {
85 std::vector<Locale> result;
86 char langTag[ULOC_FULLNAME_CAPACITY];
87 std::unordered_set<uint64_t> seen;
88
89 SplitIterator it(input, ',');
90 while (it.hasNext()) {
91 StringPiece localeStr = it.next();
92 size_t length = toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, localeStr);
93 Locale locale(StringPiece(langTag, length));
94 if (locale.isUnsupported()) {
95 continue;
96 }
97 const bool isNewLocale = seen.insert(locale.getIdentifier()).second;
98 if (!isNewLocale) {
99 continue;
100 }
101
102 result.push_back(locale);
103 if (result.size() >= FONT_LOCALE_LIMIT) {
104 break;
105 }
106 }
107 return result;
108 }
109
operator ()(const std::vector<Locale> & locales) const110 size_t LocaleListCache::LocaleVectorHash::operator()(const std::vector<Locale>& locales) const {
111 Hasher hasher;
112 for (const auto& locale : locales) {
113 uint64_t id = locale.getIdentifier();
114 hasher.update(static_cast<uint32_t>((id >> 32) & 0xFFFFFFFF));
115 hasher.update(static_cast<uint32_t>(id & 0xFFFFFFFF));
116 }
117 return hasher.hash();
118 }
119
LocaleListCache()120 LocaleListCache::LocaleListCache() {
121 // Insert an empty locale list for mapping default locale list to kEmptyLocaleListId.
122 // The default locale list has only one Locale and it is the unsupported locale.
123 mLocaleLists.emplace_back();
124 mLocaleListLookupTable.emplace(std::vector<Locale>(), kEmptyLocaleListId);
125 mLocaleListStringCache.emplace("", kEmptyLocaleListId);
126 }
127
getIdInternal(const std::string & locales)128 uint32_t LocaleListCache::getIdInternal(const std::string& locales) {
129 std::lock_guard<std::mutex> lock(mMutex);
130 const auto& it = mLocaleListStringCache.find(locales);
131 if (it != mLocaleListStringCache.end()) {
132 return it->second;
133 }
134 uint32_t id = getIdInternal(parseLocaleList(locales));
135 mLocaleListStringCache.emplace(locales, id);
136 return id;
137 }
138
getIdInternal(std::vector<Locale> && locales)139 uint32_t LocaleListCache::getIdInternal(std::vector<Locale>&& locales) {
140 if (locales.empty()) {
141 return kEmptyLocaleListId;
142 }
143 const auto& it = mLocaleListLookupTable.find(locales);
144 if (it != mLocaleListLookupTable.end()) {
145 return it->second;
146 }
147
148 // Given locale list is not in cache. Insert it and return newly assigned ID.
149 const uint32_t nextId = mLocaleLists.size();
150 mLocaleListLookupTable.emplace(locales, nextId);
151 LocaleList fontLocales(std::move(locales));
152 mLocaleLists.push_back(std::move(fontLocales));
153 return nextId;
154 }
155
readFromInternal(BufferReader * reader)156 uint32_t LocaleListCache::readFromInternal(BufferReader* reader) {
157 uint32_t size = reader->read<uint32_t>();
158 std::vector<Locale> locales;
159 locales.reserve(size);
160 for (uint32_t i = 0; i < size; i++) {
161 locales.emplace_back(reader->read<uint64_t>());
162 }
163 std::lock_guard<std::mutex> lock(mMutex);
164 return getIdInternal(std::move(locales));
165 }
166
writeToInternal(BufferWriter * writer,uint32_t id)167 void LocaleListCache::writeToInternal(BufferWriter* writer, uint32_t id) {
168 const LocaleList& localeList = getByIdInternal(id);
169 writer->write<uint32_t>(localeList.size());
170 for (size_t i = 0; i < localeList.size(); i++) {
171 writer->write<uint64_t>(localeList[i].getIdentifier());
172 }
173 }
174
getByIdInternal(uint32_t id)175 const LocaleList& LocaleListCache::getByIdInternal(uint32_t id) {
176 std::lock_guard<std::mutex> lock(mMutex);
177 MINIKIN_ASSERT(id < mLocaleLists.size(), "Lookup by unknown locale list ID.");
178 return mLocaleLists[id];
179 }
180
181 } // namespace minikin
182