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