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