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