FontCollection.cpp revision 9cc9bbe1461f359f0b27c5e7645c17dda001ab1d
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#ifdef VERBOSE_DEBUG 18#include <stdio.h> // for debugging - remove 19#endif 20 21#include <minikin/CmapCoverage.h> 22#include <minikin/FontCollection.h> 23 24using std::vector; 25 26namespace android { 27 28template <typename T> 29static inline T max(T a, T b) { 30 return a>b ? a : b; 31} 32 33FontCollection::FontCollection(const vector<FontFamily*>& typefaces) : 34 mMaxChar(0) { 35 vector<uint32_t> lastChar; 36 size_t nTypefaces = typefaces.size(); 37#ifdef VERBOSE_DEBUG 38 printf("nTypefaces = %d\n", nTypefaces); 39#endif 40 const FontStyle defaultStyle; 41 for (size_t i = 0; i < nTypefaces; i++) { 42 FontFamily* family = typefaces[i]; 43 FontInstance dummy; 44 mInstances.push_back(dummy); // emplace_back would be better 45 FontInstance* instance = &mInstances.back(); 46 instance->mFamily = family; 47 instance->mCoverage = new SparseBitSet; 48 FT_Face typeface = family->getClosestMatch(defaultStyle); 49#ifdef VERBOSE_DEBUG 50 printf("closest match = %x, family size = %d\n", typeface, family->getNumFonts()); 51#endif 52 const uint32_t cmapTag = FT_MAKE_TAG('c', 'm', 'a', 'p'); 53 FT_ULong cmapSize = 0; 54 FT_Error error = FT_Load_Sfnt_Table(typeface, cmapTag, 0, NULL, &cmapSize); 55 UniquePtr<uint8_t[]> cmapData(new uint8_t[cmapSize]); 56 error = FT_Load_Sfnt_Table(typeface, cmapTag, 0, 57 cmapData.get(), &cmapSize); 58 CmapCoverage::getCoverage(*instance->mCoverage, cmapData.get(), cmapSize); 59#ifdef VERBOSE_DEBUG 60 printf("font coverage length=%d, first ch=%x\n", instance->mCoverage->length(), 61 instance->mCoverage->nextSetBit(0)); 62#endif 63 mMaxChar = max(mMaxChar, instance->mCoverage->length()); 64 lastChar.push_back(instance->mCoverage->nextSetBit(0)); 65 // TODO: should probably ref typeface here, hmm 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 printf("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 printf("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 // probably unref the typeface here too 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 printf("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 FT_Reference_Face(run->font); 142 } 143 lastFamily = family; 144 run->start = i; 145 } 146 run->end = i + nShorts; 147 } 148} 149 150} // namespace android 151