FontCollection.cpp revision adaf42f0d3444de2b0bb977ccc94801458497f46
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 "unicode/unistr.h" 23#include "unicode/unorm2.h" 24 25#include "MinikinInternal.h" 26#include <minikin/FontCollection.h> 27 28using std::vector; 29 30namespace android { 31 32template <typename T> 33static inline T max(T a, T b) { 34 return a>b ? a : b; 35} 36 37uint32_t FontCollection::sNextId = 0; 38 39FontCollection::FontCollection(const vector<FontFamily*>& typefaces) : 40 mMaxChar(0) { 41 AutoMutex _l(gMinikinLock); 42 mId = sNextId++; 43 vector<uint32_t> lastChar; 44 size_t nTypefaces = typefaces.size(); 45#ifdef VERBOSE_DEBUG 46 ALOGD("nTypefaces = %d\n", nTypefaces); 47#endif 48 const FontStyle defaultStyle; 49 for (size_t i = 0; i < nTypefaces; i++) { 50 FontFamily* family = typefaces[i]; 51 MinikinFont* typeface = family->getClosestMatch(defaultStyle).font; 52 if (typeface == NULL) { 53 continue; 54 } 55 family->RefLocked(); 56 const SparseBitSet* coverage = family->getCoverage(); 57 if (coverage == nullptr) { 58 family->UnrefLocked(); 59 continue; 60 } 61 mFamilies.push_back(family); // emplace_back would be better 62 mMaxChar = max(mMaxChar, coverage->length()); 63 lastChar.push_back(coverage->nextSetBit(0)); 64 } 65 nTypefaces = mFamilies.size(); 66 LOG_ALWAYS_FATAL_IF(nTypefaces == 0, 67 "Font collection must have at least one valid typeface"); 68 size_t nPages = (mMaxChar + kPageMask) >> kLogCharsPerPage; 69 size_t offset = 0; 70 for (size_t i = 0; i < nPages; i++) { 71 Range dummy; 72 mRanges.push_back(dummy); 73 Range* range = &mRanges.back(); 74#ifdef VERBOSE_DEBUG 75 ALOGD("i=%d: range start = %d\n", i, offset); 76#endif 77 range->start = offset; 78 for (size_t j = 0; j < nTypefaces; j++) { 79 if (lastChar[j] < (i + 1) << kLogCharsPerPage) { 80 FontFamily* family = mFamilies[j]; 81 mFamilyVec.push_back(family); 82 offset++; 83 uint32_t nextChar = family->getCoverage()->nextSetBit((i + 1) << kLogCharsPerPage); 84#ifdef VERBOSE_DEBUG 85 ALOGD("nextChar = %d (j = %d)\n", nextChar, j); 86#endif 87 lastChar[j] = nextChar; 88 } 89 } 90 range->end = offset; 91 } 92} 93 94FontCollection::~FontCollection() { 95 for (size_t i = 0; i < mFamilies.size(); i++) { 96 mFamilies[i]->UnrefLocked(); 97 } 98} 99 100// Implement heuristic for choosing best-match font. Here are the rules: 101// 1. If first font in the collection has the character, it wins. 102// 2. If a font matches both language and script, it gets a score of 4. 103// 3. If a font matches just language, it gets a score of 2. 104// 4. Matching the "compact" or "elegant" variant adds one to the score. 105// 5. Highest score wins, with ties resolved to the first font. 106FontFamily* FontCollection::getFamilyForChar(uint32_t ch, 107 FontLanguage lang, int variant) const { 108 if (ch >= mMaxChar) { 109 return NULL; 110 } 111 const Range& range = mRanges[ch >> kLogCharsPerPage]; 112#ifdef VERBOSE_DEBUG 113 ALOGD("querying range %d:%d\n", range.start, range.end); 114#endif 115 FontFamily* bestFamily = NULL; 116 int bestScore = -1; 117 for (size_t i = range.start; i < range.end; i++) { 118 FontFamily* family = mFamilyVec[i]; 119 if (family->getCoverage()->get(ch)) { 120 // First font family in collection always matches 121 if (mFamilies[0] == family) { 122 return family; 123 } 124 int score = lang.match(family->lang()) * 2; 125 if (family->variant() == 0 || family->variant() == variant) { 126 score++; 127 } 128 if (score > bestScore) { 129 bestScore = score; 130 bestFamily = family; 131 } 132 } 133 } 134 if (bestFamily == NULL && !mFamilyVec.empty()) { 135 UErrorCode errorCode = U_ZERO_ERROR; 136 const UNormalizer2* normalizer = unorm2_getNFDInstance(&errorCode); 137 if (U_SUCCESS(errorCode)) { 138 UChar decomposed[4]; 139 int len = unorm2_getRawDecomposition(normalizer, ch, decomposed, 4, &errorCode); 140 if (U_SUCCESS(errorCode) && len > 0) { 141 int off = 0; 142 U16_NEXT_UNSAFE(decomposed, off, ch); 143 return getFamilyForChar(ch, lang, variant); 144 } 145 } 146 bestFamily = mFamilies[0]; 147 } 148 return bestFamily; 149} 150 151const uint32_t NBSP = 0xa0; 152const uint32_t ZWJ = 0x200c; 153const uint32_t ZWNJ = 0x200d; 154const uint32_t KEYCAP = 0x20e3; 155const uint32_t HYPHEN = 0x2010; 156const uint32_t NB_HYPHEN = 0x2011; 157 158// Characters where we want to continue using existing font run instead of 159// recomputing the best match in the fallback list. 160static const uint32_t stickyWhitelist[] = { '!', ',', '-', '.', ':', ';', '?', NBSP, ZWJ, ZWNJ, 161 KEYCAP, HYPHEN, NB_HYPHEN }; 162 163static bool isStickyWhitelisted(uint32_t c) { 164 for (size_t i = 0; i < sizeof(stickyWhitelist) / sizeof(stickyWhitelist[0]); i++) { 165 if (stickyWhitelist[i] == c) return true; 166 } 167 return false; 168} 169 170static bool isVariationSelector(uint32_t c) { 171 return (0xFE00 <= c && c <= 0xFE0F) || (0xE0100 <= c && c <= 0xE01EF); 172} 173 174void FontCollection::itemize(const uint16_t *string, size_t string_size, FontStyle style, 175 vector<Run>* result) const { 176 FontLanguage lang = style.getLanguage(); 177 int variant = style.getVariant(); 178 FontFamily* lastFamily = NULL; 179 Run* run = NULL; 180 int nShorts; 181 for (size_t i = 0; i < string_size; i += nShorts) { 182 nShorts = 1; 183 uint32_t ch = string[i]; 184 // sigh, decode UTF-16 by hand here 185 if ((ch & 0xfc00) == 0xd800) { 186 if ((i + 1) < string_size) { 187 ch = 0x10000 + ((ch & 0x3ff) << 10) + (string[i + 1] & 0x3ff); 188 nShorts = 2; 189 } 190 } 191 // Continue using existing font as long as it has coverage and is whitelisted; 192 // also variation sequences continue existing run. 193 if (lastFamily == NULL 194 || !((isStickyWhitelisted(ch) && lastFamily->getCoverage()->get(ch)) 195 || isVariationSelector(ch))) { 196 FontFamily* family = getFamilyForChar(ch, lang, variant); 197 if (i == 0 || family != lastFamily) { 198 size_t start = i; 199 // Workaround for Emoji keycap until we implement per-cluster font 200 // selection: if keycap is found in a different font that also 201 // supports previous char, attach previous char to the new run. 202 // Only handles non-surrogate characters. 203 // Bug 7557244. 204 if (ch == KEYCAP && i && family && family->getCoverage()->get(string[i - 1])) { 205 run->end--; 206 if (run->start == run->end) { 207 result->pop_back(); 208 } 209 start--; 210 } 211 Run dummy; 212 result->push_back(dummy); 213 run = &result->back(); 214 if (family == NULL) { 215 run->fakedFont.font = NULL; 216 } else { 217 run->fakedFont = family->getClosestMatch(style); 218 } 219 lastFamily = family; 220 run->start = start; 221 } 222 } 223 run->end = i + nShorts; 224 } 225} 226 227MinikinFont* FontCollection::baseFont(FontStyle style) { 228 return baseFontFaked(style).font; 229} 230 231FakedFont FontCollection::baseFontFaked(FontStyle style) { 232 if (mFamilies.empty()) { 233 return FakedFont(); 234 } 235 return mFamilies[0]->getClosestMatch(style); 236} 237 238uint32_t FontCollection::getId() const { 239 return mId; 240} 241 242} // namespace android 243