FontCollection.cpp revision 3dd8757fcf48976295bac566277b6da1046e8362
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 = %zd\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=%zd: range start = %zd\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 = %zd)\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, uint32_t vs, 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 %zd:%zd\n", range.start, range.end); 114#endif 115 FontFamily* bestFamily = nullptr; 116 int bestScore = -1; 117 for (size_t i = range.start; i < range.end; i++) { 118 FontFamily* family = mFamilyVec[i]; 119 if (vs == 0 ? family->getCoverage()->get(ch) : family->hasVariationSelector(ch, vs)) { 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 == nullptr && vs != 0) { 135 // If no fonts support the codepoint and variation selector pair, 136 // fallback to select a font family that supports just the base 137 // character, ignoring the variation selector. 138 return getFamilyForChar(ch, 0, lang, variant); 139 } 140 if (bestFamily == nullptr && !mFamilyVec.empty()) { 141 UErrorCode errorCode = U_ZERO_ERROR; 142 const UNormalizer2* normalizer = unorm2_getNFDInstance(&errorCode); 143 if (U_SUCCESS(errorCode)) { 144 UChar decomposed[4]; 145 int len = unorm2_getRawDecomposition(normalizer, ch, decomposed, 4, &errorCode); 146 if (U_SUCCESS(errorCode) && len > 0) { 147 int off = 0; 148 U16_NEXT_UNSAFE(decomposed, off, ch); 149 return getFamilyForChar(ch, vs, lang, variant); 150 } 151 } 152 bestFamily = mFamilies[0]; 153 } 154 return bestFamily; 155} 156 157const uint32_t NBSP = 0xa0; 158const uint32_t ZWJ = 0x200c; 159const uint32_t ZWNJ = 0x200d; 160const uint32_t KEYCAP = 0x20e3; 161const uint32_t HYPHEN = 0x2010; 162const uint32_t NB_HYPHEN = 0x2011; 163 164// Characters where we want to continue using existing font run instead of 165// recomputing the best match in the fallback list. 166static const uint32_t stickyWhitelist[] = { '!', ',', '-', '.', ':', ';', '?', NBSP, ZWJ, ZWNJ, 167 KEYCAP, HYPHEN, NB_HYPHEN }; 168 169static bool isStickyWhitelisted(uint32_t c) { 170 for (size_t i = 0; i < sizeof(stickyWhitelist) / sizeof(stickyWhitelist[0]); i++) { 171 if (stickyWhitelist[i] == c) return true; 172 } 173 return false; 174} 175 176static bool isVariationSelector(uint32_t c) { 177 return (0xFE00 <= c && c <= 0xFE0F) || (0xE0100 <= c && c <= 0xE01EF); 178} 179 180void FontCollection::itemize(const uint16_t *string, size_t string_size, FontStyle style, 181 vector<Run>* result) const { 182 FontLanguage lang = style.getLanguage(); 183 int variant = style.getVariant(); 184 FontFamily* lastFamily = NULL; 185 Run* run = NULL; 186 187 if (string_size == 0) { 188 return; 189 } 190 191 const uint32_t kEndOfString = 0xFFFFFFFF; 192 193 uint32_t nextCh = 0; 194 uint32_t prevCh = 0; 195 size_t nextUtf16Pos = 0; 196 size_t readLength = 0; 197 U16_NEXT(string, readLength, string_size, nextCh); 198 199 do { 200 const uint32_t ch = nextCh; 201 const size_t utf16Pos = nextUtf16Pos; 202 nextUtf16Pos = readLength; 203 if (readLength < string_size) { 204 U16_NEXT(string, readLength, string_size, nextCh); 205 } else { 206 nextCh = kEndOfString; 207 } 208 209 bool shouldContinueRun = false; 210 if (lastFamily != nullptr) { 211 if (isStickyWhitelisted(ch)) { 212 // Continue using existing font as long as it has coverage and is whitelisted 213 shouldContinueRun = lastFamily->getCoverage()->get(ch); 214 } else if (isVariationSelector(ch)) { 215 // Always continue if the character is a variation selector. 216 shouldContinueRun = true; 217 } 218 } 219 220 if (!shouldContinueRun) { 221 FontFamily* family = 222 getFamilyForChar(ch, isVariationSelector(nextCh) ? nextCh : 0, lang, variant); 223 if (utf16Pos == 0 || family != lastFamily) { 224 size_t start = utf16Pos; 225 // Workaround for Emoji keycap until we implement per-cluster font 226 // selection: if keycap is found in a different font that also 227 // supports previous char, attach previous char to the new run. 228 // Bug 7557244. 229 if (ch == KEYCAP && utf16Pos != 0 && family && family->getCoverage()->get(prevCh)) { 230 const size_t prevChLength = U16_LENGTH(prevCh); 231 run->end -= prevChLength; 232 if (run->start == run->end) { 233 result->pop_back(); 234 } 235 start -= prevChLength; 236 } 237 Run dummy; 238 result->push_back(dummy); 239 run = &result->back(); 240 if (family == NULL) { 241 run->fakedFont.font = NULL; 242 } else { 243 run->fakedFont = family->getClosestMatch(style); 244 } 245 lastFamily = family; 246 run->start = start; 247 } 248 } 249 prevCh = ch; 250 run->end = nextUtf16Pos; // exclusive 251 } while (nextCh != kEndOfString); 252} 253 254MinikinFont* FontCollection::baseFont(FontStyle style) { 255 return baseFontFaked(style).font; 256} 257 258FakedFont FontCollection::baseFontFaked(FontStyle style) { 259 if (mFamilies.empty()) { 260 return FakedFont(); 261 } 262 return mFamilies[0]->getClosestMatch(style); 263} 264 265uint32_t FontCollection::getId() const { 266 return mId; 267} 268 269void FontCollection::purgeFontFamilyHbFontCache() const { 270 assertMinikinLocked(); 271 for (size_t i = 0; i < mFamilies.size(); ++i) { 272 mFamilies[i]->purgeHbFontCache(); 273 } 274} 275 276} // namespace android 277