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 <unicode/uloc.h>
25
26 #include "Locale.h"
27 #include "MinikinInternal.h"
28
29 namespace minikin {
30
31 const uint32_t LocaleListCache::kEmptyListId;
32
33 // Returns the text length of output.
toLanguageTag(char * output,size_t outSize,const StringPiece & locale)34 static size_t toLanguageTag(char* output, size_t outSize, const StringPiece& locale) {
35 output[0] = '\0';
36 if (locale.empty()) {
37 return 0;
38 }
39
40 std::string localeString = locale.toString(); // ICU only understands C-style string.
41
42 size_t outLength = 0;
43 UErrorCode uErr = U_ZERO_ERROR;
44 outLength = uloc_canonicalize(localeString.c_str(), output, outSize, &uErr);
45 if (U_FAILURE(uErr)) {
46 // unable to build a proper locale identifier
47 ALOGD("uloc_canonicalize(\"%s\") failed: %s", localeString.c_str(), u_errorName(uErr));
48 output[0] = '\0';
49 return 0;
50 }
51
52 // Preserve "" and "_****" since uloc_addLikelySubtags changes "" to "en_Latn_US".
53 if (outLength == 0 || (outLength == 5 && output[0] == '_')) {
54 if (output[0] == '_') {
55 output[0] = '-';
56 }
57 std::string buf(output, outLength);
58 outLength = (size_t)snprintf(output, outSize, "und%s", buf.c_str());
59 return outLength;
60 }
61
62 char likelyChars[ULOC_FULLNAME_CAPACITY];
63 uErr = U_ZERO_ERROR;
64 uloc_addLikelySubtags(output, likelyChars, ULOC_FULLNAME_CAPACITY, &uErr);
65 if (U_FAILURE(uErr)) {
66 // unable to build a proper locale identifier
67 ALOGD("uloc_addLikelySubtags(\"%s\") failed: %s", output, u_errorName(uErr));
68 output[0] = '\0';
69 return 0;
70 }
71
72 uErr = U_ZERO_ERROR;
73 outLength = uloc_toLanguageTag(likelyChars, output, outSize, FALSE, &uErr);
74 if (U_FAILURE(uErr)) {
75 // unable to build a proper locale identifier
76 ALOGD("uloc_toLanguageTag(\"%s\") failed: %s", likelyChars, u_errorName(uErr));
77 output[0] = '\0';
78 return 0;
79 }
80 return outLength;
81 }
82
parseLocaleList(const std::string & input)83 static std::vector<Locale> parseLocaleList(const std::string& input) {
84 std::vector<Locale> result;
85 char langTag[ULOC_FULLNAME_CAPACITY];
86 std::unordered_set<uint64_t> seen;
87
88 SplitIterator it(input, ',');
89 while (it.hasNext()) {
90 StringPiece localeStr = it.next();
91 size_t length = toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, localeStr);
92 Locale locale(StringPiece(langTag, length));
93 if (locale.isUnsupported()) {
94 continue;
95 }
96 const bool isNewLocale = seen.insert(locale.getIdentifier()).second;
97 if (!isNewLocale) {
98 continue;
99 }
100
101 result.push_back(locale);
102 if (result.size() >= FONT_LOCALE_LIMIT) {
103 break;
104 }
105 }
106 return result;
107 }
108
LocaleListCache()109 LocaleListCache::LocaleListCache() {
110 // Insert an empty locale list for mapping default locale list to kEmptyListId.
111 // The default locale list has only one Locale and it is the unsupported locale.
112 mLocaleLists.emplace_back();
113 mLocaleListLookupTable.insert(std::make_pair("", kEmptyListId));
114 }
115
getIdInternal(const std::string & locales)116 uint32_t LocaleListCache::getIdInternal(const std::string& locales) {
117 std::lock_guard<std::mutex> lock(mMutex);
118 const auto& it = mLocaleListLookupTable.find(locales);
119 if (it != mLocaleListLookupTable.end()) {
120 return it->second;
121 }
122
123 // Given locale list is not in cache. Insert it and return newly assigned ID.
124 const uint32_t nextId = mLocaleLists.size();
125 LocaleList fontLocales(parseLocaleList(locales));
126 if (fontLocales.empty()) {
127 return kEmptyListId;
128 }
129 mLocaleLists.push_back(std::move(fontLocales));
130 mLocaleListLookupTable.insert(std::make_pair(locales, nextId));
131 return nextId;
132 }
133
getByIdInternal(uint32_t id)134 const LocaleList& LocaleListCache::getByIdInternal(uint32_t id) {
135 std::lock_guard<std::mutex> lock(mMutex);
136 MINIKIN_ASSERT(id < mLocaleLists.size(), "Lookup by unknown locale list ID.");
137 return mLocaleLists[id];
138 }
139
140 } // namespace minikin
141