FontLanguageListCache.cpp revision 6d9dcd2cf3d3ed26a886e02d94c907311e7b1f83
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 <cutils/log.h> 22 23#include "MinikinInternal.h" 24 25namespace android { 26 27const uint32_t FontLanguageListCache::kEmptyListId; 28 29// static 30uint32_t FontLanguageListCache::getId(const std::string& languages) { 31 FontLanguageListCache* inst = FontLanguageListCache::getInstance(); 32 std::unordered_map<std::string, uint32_t>::const_iterator it = 33 inst->mLanguageListLookupTable.find(languages); 34 if (it != inst->mLanguageListLookupTable.end()) { 35 return it->second; 36 } 37 38 // Given language list is not in cache. Insert it and return newly assigned ID. 39 const uint32_t nextId = inst->mLanguageLists.size(); 40 inst->mLanguageLists.push_back(FontLanguages(languages.c_str(), languages.size())); 41 inst->mLanguageListLookupTable.insert(std::make_pair(languages, nextId)); 42 return nextId; 43} 44 45// static 46const FontLanguages& FontLanguageListCache::getById(uint32_t id) { 47 FontLanguageListCache* inst = FontLanguageListCache::getInstance(); 48 LOG_ALWAYS_FATAL_IF(id >= inst->mLanguageLists.size(), "Lookup by unknown language list ID."); 49 return inst->mLanguageLists[id]; 50} 51 52// static 53FontLanguageListCache* FontLanguageListCache::getInstance() { 54 assertMinikinLocked(); 55 static FontLanguageListCache* instance = nullptr; 56 if (instance == nullptr) { 57 instance = new FontLanguageListCache(); 58 59 // Insert an empty language list for mapping empty language list to kEmptyListId. 60 instance->mLanguageLists.push_back(FontLanguages()); 61 instance->mLanguageListLookupTable.insert(std::make_pair("", kEmptyListId)); 62 } 63 return instance; 64} 65 66} // namespace android 67