FontCollection.cpp revision b80c1f19c58b927820a8a24bf2218e5645724608
1/* 2 * Copyright (C) 2013 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 VERBOSE_DEBUG 18 19#define LOG_TAG "Minikin" 20#include <cutils/log.h> 21 22#include <minikin/CmapCoverage.h> 23#include <minikin/FontCollection.h> 24 25using std::vector; 26 27namespace android { 28 29template <typename T> 30static inline T max(T a, T b) { 31 return a>b ? a : b; 32} 33 34FontCollection::FontCollection(const vector<FontFamily*>& typefaces) : 35 mMaxChar(0) { 36 vector<uint32_t> lastChar; 37 size_t nTypefaces = typefaces.size(); 38#ifdef VERBOSE_DEBUG 39 ALOGD("nTypefaces = %d\n", nTypefaces); 40#endif 41 const FontStyle defaultStyle; 42 for (size_t i = 0; i < nTypefaces; i++) { 43 FontFamily* family = typefaces[i]; 44 family->RefLocked(); 45 FontInstance dummy; 46 mInstances.push_back(dummy); // emplace_back would be better 47 FontInstance* instance = &mInstances.back(); 48 instance->mFamily = family; 49 instance->mCoverage = new SparseBitSet; 50 MinikinFont* typeface = family->getClosestMatch(defaultStyle); 51#ifdef VERBOSE_DEBUG 52 ALOGD("closest match = %p, family size = %d\n", typeface, family->getNumFonts()); 53#endif 54 const uint32_t cmapTag = MinikinFont::MakeTag('c', 'm', 'a', 'p'); 55 size_t cmapSize = 0; 56 bool ok = typeface->GetTable(cmapTag, NULL, &cmapSize); 57 UniquePtr<uint8_t[]> cmapData(new uint8_t[cmapSize]); 58 ok = typeface->GetTable(cmapTag, cmapData.get(), &cmapSize); 59 CmapCoverage::getCoverage(*instance->mCoverage, cmapData.get(), cmapSize); 60#ifdef VERBOSE_DEBUG 61 ALOGD("font coverage length=%d, first ch=%x\n", instance->mCoverage->length(), 62 instance->mCoverage->nextSetBit(0)); 63#endif 64 mMaxChar = max(mMaxChar, instance->mCoverage->length()); 65 lastChar.push_back(instance->mCoverage->nextSetBit(0)); 66 } 67 size_t nPages = mMaxChar >> kLogCharsPerPage; 68 size_t offset = 0; 69 for (size_t i = 0; i < nPages; i++) { 70 Range dummy; 71 mRanges.push_back(dummy); 72 Range* range = &mRanges.back(); 73#ifdef VERBOSE_DEBUG 74 ALOGD("i=%d: range start = %d\n", i, offset); 75#endif 76 range->start = offset; 77 for (size_t j = 0; j < nTypefaces; j++) { 78 if (lastChar[j] < (i + 1) << kLogCharsPerPage) { 79 const FontInstance* instance = &mInstances[j]; 80 mInstanceVec.push_back(instance); 81 offset++; 82 uint32_t nextChar = instance->mCoverage->nextSetBit((i + 1) << kLogCharsPerPage); 83#ifdef VERBOSE_DEBUG 84 ALOGD("nextChar = %d (j = %d)\n", nextChar, j); 85#endif 86 lastChar[j] = nextChar; 87 } 88 } 89 range->end = offset; 90 } 91} 92 93FontCollection::~FontCollection() { 94 for (size_t i = 0; i < mInstances.size(); i++) { 95 delete mInstances[i].mCoverage; 96 mInstances[i].mFamily->UnrefLocked(); 97 } 98} 99 100const FontFamily* FontCollection::getFamilyForChar(uint32_t ch) const { 101 if (ch >= mMaxChar) { 102 return NULL; 103 } 104 const Range& range = mRanges[ch >> kLogCharsPerPage]; 105#ifdef VERBOSE_DEBUG 106 ALOGD("querying range %d:%d\n", range.start, range.end); 107#endif 108 for (size_t i = range.start; i < range.end; i++) { 109 const FontInstance* instance = mInstanceVec[i]; 110 if (instance->mCoverage->get(ch)) { 111 return instance->mFamily; 112 } 113 } 114 return NULL; 115} 116 117void FontCollection::itemize(const uint16_t *string, size_t string_size, FontStyle style, 118 vector<Run>* result) const { 119 const FontFamily* lastFamily = NULL; 120 Run* run = NULL; 121 int nShorts; 122 for (size_t i = 0; i < string_size; i += nShorts) { 123 nShorts = 1; 124 uint32_t ch = string[i]; 125 // sigh, decode UTF-16 by hand here 126 if ((ch & 0xfc00) == 0xd800) { 127 if ((i + 1) < string_size) { 128 ch = 0x10000 + ((ch & 0x3ff) << 10) + (string[i + 1] & 0x3ff); 129 nShorts = 2; 130 } 131 } 132 const FontFamily* family = getFamilyForChar(ch); 133 if (i == 0 || family != lastFamily) { 134 Run dummy; 135 result->push_back(dummy); 136 run = &result->back(); 137 if (family == NULL) { 138 run->font = NULL; // maybe we should do something different here 139 } else { 140 run->font = family->getClosestMatch(style); 141 run->font->RefLocked(); 142 } 143 lastFamily = family; 144 run->start = i; 145 } 146 run->end = i + nShorts; 147 } 148} 149 150} // namespace android 151