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
21 #include <algorithm>
22
23 #include <log/log.h>
24 #include "unicode/unistr.h"
25 #include "unicode/unorm2.h"
26 #include "unicode/utf16.h"
27
28 #include <minikin/Emoji.h>
29 #include <minikin/FontCollection.h>
30 #include "FontLanguage.h"
31 #include "FontLanguageListCache.h"
32 #include "MinikinInternal.h"
33
34 using std::vector;
35
36 namespace minikin {
37
38 template <typename T>
max(T a,T b)39 static inline T max(T a, T b) {
40 return a > b ? a : b;
41 }
42
43 const uint32_t EMOJI_STYLE_VS = 0xFE0F;
44 const uint32_t TEXT_STYLE_VS = 0xFE0E;
45
46 uint32_t FontCollection::sNextId = 0;
47
48 // libtxt: return a locale string for a language list ID
GetFontLocale(uint32_t langListId)49 std::string GetFontLocale(uint32_t langListId) {
50 const FontLanguages& langs = FontLanguageListCache::getById(langListId);
51 return langs.size() ? langs[0].getString() : "";
52 }
53
Create(const std::vector<std::shared_ptr<FontFamily>> & typefaces)54 std::shared_ptr<minikin::FontCollection> FontCollection::Create(
55 const std::vector<std::shared_ptr<FontFamily>>& typefaces) {
56 std::shared_ptr<minikin::FontCollection> font_collection(
57 new minikin::FontCollection());
58 if (!font_collection || !font_collection->init(typefaces)) {
59 return nullptr;
60 }
61 return font_collection;
62 }
63
FontCollection()64 FontCollection::FontCollection() : mMaxChar(0) {}
65
init(const std::vector<std::shared_ptr<FontFamily>> & typefaces)66 bool FontCollection::init(
67 const std::vector<std::shared_ptr<FontFamily>>& typefaces) {
68 std::scoped_lock _l(gMinikinLock);
69 mId = sNextId++;
70 vector<uint32_t> lastChar;
71 size_t nTypefaces = typefaces.size();
72 #ifdef VERBOSE_DEBUG
73 ALOGD("nTypefaces = %zd\n", nTypefaces);
74 #endif
75 const FontStyle defaultStyle;
76 for (size_t i = 0; i < nTypefaces; i++) {
77 const std::shared_ptr<FontFamily>& family = typefaces[i];
78 if (family->getClosestMatch(defaultStyle).font == nullptr) {
79 continue;
80 }
81 const SparseBitSet& coverage = family->getCoverage();
82 mFamilies.push_back(family); // emplace_back would be better
83 if (family->hasVSTable()) {
84 mVSFamilyVec.push_back(family);
85 }
86 mMaxChar = max(mMaxChar, coverage.length());
87 lastChar.push_back(coverage.nextSetBit(0));
88
89 const std::unordered_set<AxisTag>& supportedAxes = family->supportedAxes();
90 mSupportedAxes.insert(supportedAxes.begin(), supportedAxes.end());
91 }
92 nTypefaces = mFamilies.size();
93 if (nTypefaces == 0) {
94 ALOGE("Font collection must have at least one valid typeface.");
95 return false;
96 }
97 if (nTypefaces > 254) {
98 ALOGE("Font collection may only have up to 254 font families.");
99 return false;
100 }
101 size_t nPages = (mMaxChar + kPageMask) >> kLogCharsPerPage;
102 // TODO: Use variation selector map for mRanges construction.
103 // A font can have a glyph for a base code point and variation selector pair
104 // but no glyph for the base code point without variation selector. The family
105 // won't be listed in the range in this case.
106 for (size_t i = 0; i < nPages; i++) {
107 Range dummy;
108 mRanges.push_back(dummy);
109 Range* range = &mRanges.back();
110 #ifdef VERBOSE_DEBUG
111 ALOGD("i=%zd: range start = %zd\n", i, offset);
112 #endif
113 range->start = mFamilyVec.size();
114 for (size_t j = 0; j < nTypefaces; j++) {
115 if (lastChar[j] < (i + 1) << kLogCharsPerPage) {
116 const std::shared_ptr<FontFamily>& family = mFamilies[j];
117 mFamilyVec.push_back(static_cast<uint8_t>(j));
118 uint32_t nextChar =
119 family->getCoverage().nextSetBit((i + 1) << kLogCharsPerPage);
120 #ifdef VERBOSE_DEBUG
121 ALOGD("nextChar = %d (j = %zd)\n", nextChar, j);
122 #endif
123 lastChar[j] = nextChar;
124 }
125 }
126 range->end = mFamilyVec.size();
127 }
128
129 if (mFamilyVec.size() >= 0xFFFF) {
130 ALOGE("Exceeded the maximum indexable cmap coverage.");
131 return false;
132 }
133 return true;
134 }
135
136 // Special scores for the font fallback.
137 const uint32_t kUnsupportedFontScore = 0;
138 const uint32_t kFirstFontScore = UINT32_MAX;
139
140 // Calculates a font score.
141 // The score of the font family is based on three subscores.
142 // - Coverage Score: How well the font family covers the given character or
143 // variation sequence.
144 // - Language Score: How well the font family is appropriate for the language.
145 // - Variant Score: Whether the font family matches the variant. Note that this
146 // variant is not the
147 // one in BCP47. This is our own font variant (e.g., elegant, compact).
148 //
149 // Then, there is a priority for these three subscores as follow:
150 // Coverage Score > Language Score > Variant Score
151 // The returned score reflects this priority order.
152 //
153 // Note that there are two special scores.
154 // - kUnsupportedFontScore: When the font family doesn't support the variation
155 // sequence or even its
156 // base character.
157 // - kFirstFontScore: When the font is the first font family in the collection
158 // and it supports the
159 // given character or variation sequence.
calcFamilyScore(uint32_t ch,uint32_t vs,int variant,uint32_t langListId,const std::shared_ptr<FontFamily> & fontFamily) const160 uint32_t FontCollection::calcFamilyScore(
161 uint32_t ch,
162 uint32_t vs,
163 int variant,
164 uint32_t langListId,
165 const std::shared_ptr<FontFamily>& fontFamily) const {
166 const uint32_t coverageScore = calcCoverageScore(ch, vs, fontFamily);
167 if (coverageScore == kFirstFontScore ||
168 coverageScore == kUnsupportedFontScore) {
169 // No need to calculate other scores.
170 return coverageScore;
171 }
172
173 const uint32_t languageScore =
174 calcLanguageMatchingScore(langListId, *fontFamily);
175 const uint32_t variantScore = calcVariantMatchingScore(variant, *fontFamily);
176
177 // Subscores are encoded into 31 bits representation to meet the subscore
178 // priority. The highest 2 bits are for coverage score, then following 28 bits
179 // are for language score, then the last 1 bit is for variant score.
180 return coverageScore << 29 | languageScore << 1 | variantScore;
181 }
182
183 // Calculates a font score based on variation sequence coverage.
184 // - Returns kUnsupportedFontScore if the font doesn't support the variation
185 // sequence or its base
186 // character.
187 // - Returns kFirstFontScore if the font family is the first font family in the
188 // collection and it
189 // supports the given character or variation sequence.
190 // - Returns 3 if the font family supports the variation sequence.
191 // - Returns 2 if the vs is a color variation selector (U+FE0F) and if the font
192 // is an emoji font.
193 // - Returns 2 if the vs is a text variation selector (U+FE0E) and if the font
194 // is not an emoji font.
195 // - Returns 1 if the variation selector is not specified or if the font family
196 // only supports the
197 // variation sequence's base character.
calcCoverageScore(uint32_t ch,uint32_t vs,const std::shared_ptr<FontFamily> & fontFamily) const198 uint32_t FontCollection::calcCoverageScore(
199 uint32_t ch,
200 uint32_t vs,
201 const std::shared_ptr<FontFamily>& fontFamily) const {
202 const bool hasVSGlyph = (vs != 0) && fontFamily->hasGlyph(ch, vs);
203 if (!hasVSGlyph && !fontFamily->getCoverage().get(ch)) {
204 // The font doesn't support either variation sequence or even the base
205 // character.
206 return kUnsupportedFontScore;
207 }
208
209 if ((vs == 0 || hasVSGlyph) && mFamilies[0] == fontFamily) {
210 // If the first font family supports the given character or variation
211 // sequence, always use it.
212 return kFirstFontScore;
213 }
214
215 if (vs == 0) {
216 return 1;
217 }
218
219 if (hasVSGlyph) {
220 return 3;
221 }
222
223 if (vs == EMOJI_STYLE_VS || vs == TEXT_STYLE_VS) {
224 const FontLanguages& langs =
225 FontLanguageListCache::getById(fontFamily->langId());
226 bool hasEmojiFlag = false;
227 for (size_t i = 0; i < langs.size(); ++i) {
228 if (langs[i].getEmojiStyle() == FontLanguage::EMSTYLE_EMOJI) {
229 hasEmojiFlag = true;
230 break;
231 }
232 }
233
234 if (vs == EMOJI_STYLE_VS) {
235 return hasEmojiFlag ? 2 : 1;
236 } else { // vs == TEXT_STYLE_VS
237 return hasEmojiFlag ? 1 : 2;
238 }
239 }
240 return 1;
241 }
242
243 // Calculate font scores based on the script matching, subtag matching and
244 // primary language matching.
245 //
246 // 1. If only the font's language matches or there is no matches between
247 // requested font and
248 // supported font, then the font obtains a score of 0.
249 // 2. Without a match in language, considering subtag may change font's
250 // EmojiStyle over script,
251 // a match in subtag gets a score of 2 and a match in scripts gains a score
252 // of 1.
253 // 3. Regarding to two elements matchings, language-and-subtag matching has a
254 // score of 4, while
255 // language-and-script obtains a socre of 3 with the same reason above.
256 //
257 // If two languages in the requested list have the same language score, the font
258 // matching with higher priority language gets a higher score. For example, in
259 // the case the user requested language list is "ja-Jpan,en-Latn". The score of
260 // for the font of "ja-Jpan" gets a higher score than the font of "en-Latn".
261 //
262 // To achieve score calculation with priorities, the language score is
263 // determined as follows:
264 // LanguageScore = s(0) * 5^(m - 1) + s(1) * 5^(m - 2) + ... + s(m - 2) * 5 +
265 // s(m - 1)
266 // Here, m is the maximum number of languages to be compared, and s(i) is the
267 // i-th language's matching score. The possible values of s(i) are 0, 1, 2, 3
268 // and 4.
calcLanguageMatchingScore(uint32_t userLangListId,const FontFamily & fontFamily)269 uint32_t FontCollection::calcLanguageMatchingScore(
270 uint32_t userLangListId,
271 const FontFamily& fontFamily) {
272 const FontLanguages& langList =
273 FontLanguageListCache::getById(userLangListId);
274 const FontLanguages& fontLanguages =
275 FontLanguageListCache::getById(fontFamily.langId());
276
277 const size_t maxCompareNum = std::min(langList.size(), FONT_LANGUAGES_LIMIT);
278 uint32_t score = 0;
279 for (size_t i = 0; i < maxCompareNum; ++i) {
280 score = score * 5u + langList[i].calcScoreFor(fontLanguages);
281 }
282 return score;
283 }
284
285 // Calculates a font score based on variant ("compact" or "elegant") matching.
286 // - Returns 1 if the font doesn't have variant or the variant matches with the
287 // text style.
288 // - No score if the font has a variant but it doesn't match with the text
289 // style.
calcVariantMatchingScore(int variant,const FontFamily & fontFamily)290 uint32_t FontCollection::calcVariantMatchingScore(
291 int variant,
292 const FontFamily& fontFamily) {
293 return (fontFamily.variant() == 0 || fontFamily.variant() == variant) ? 1 : 0;
294 }
295
296 // Implement heuristic for choosing best-match font. Here are the rules:
297 // 1. If first font in the collection has the character, it wins.
298 // 2. Calculate a score for the font family. See comments in calcFamilyScore for
299 // the detail.
300 // 3. Highest score wins, with ties resolved to the first font.
301 // This method never returns nullptr.
getFamilyForChar(uint32_t ch,uint32_t vs,uint32_t langListId,int variant) const302 const std::shared_ptr<FontFamily>& FontCollection::getFamilyForChar(
303 uint32_t ch,
304 uint32_t vs,
305 uint32_t langListId,
306 int variant) const {
307 // First find font in hw theme.
308 for (size_t k = 0; k < mFamilies.size(); k++) {
309 if (NULL == mFamilies[k]) {
310 continue;
311 }
312 if (mFamilies[k]->getHwFontFamilyType() > 0) {
313 if(mFamilies[k] ->getCoverage().get(ch)) {
314 return mFamilies[k];
315 }
316 }
317 }
318
319 if (mFallbackFontProvider) {
320 const std::shared_ptr<FontFamily>& fallback =
321 mFallbackFontProvider->matchFallbackFontFromHwFont(ch,
322 GetFontLocale(langListId));
323 if (fallback) {
324 return fallback;
325 }
326 }
327
328 if (ch >= mMaxChar) {
329 // libtxt: check if the fallback font provider can match this character
330 if (mFallbackFontProvider) {
331 const std::shared_ptr<FontFamily>& fallback =
332 findFallbackFont(ch, vs, langListId);
333 if (fallback) {
334 return fallback;
335 }
336 }
337 return mFamilies[0];
338 }
339 // 0x1000 to 0x109f is range of Burmese, force ch to 0x1000 or 0x1050 to match Z-encoding and U-encoding of Burmese.
340 if (ch >= 0x1000 && ch <= 0x109f) {
341 const uint32_t ZAWGYI_START_CODE = 0x1000;
342 const uint32_t UNICODE_START_CODE = 0x1050;
343 ch = mIsZawgyiMyanmar ? ZAWGYI_START_CODE : UNICODE_START_CODE; // 0x1050 is exist in U-encoding of Burmese only.
344 }
345
346 Range range = mRanges[ch >> kLogCharsPerPage];
347
348 if (vs != 0) {
349 range = {0, static_cast<uint16_t>(mFamilies.size())};
350 }
351
352 #ifdef VERBOSE_DEBUG
353 ALOGD("querying range %zd:%zd\n", range.start, range.end);
354 #endif
355 int bestFamilyIndex = -1;
356 uint32_t bestScore = kUnsupportedFontScore;
357 for (size_t i = range.start; i < range.end; i++) {
358 const std::shared_ptr<FontFamily>& family =
359 vs == 0 ? mFamilies[mFamilyVec[i]] : mFamilies[i];
360 const uint32_t score = calcFamilyScore(ch, vs, variant, langListId, family);
361 if (score == kFirstFontScore) {
362 // If the first font family supports the given character or variation
363 // sequence, always use it.
364 return family;
365 }
366 if (score > bestScore) {
367 bestScore = score;
368 bestFamilyIndex = i;
369 }
370 }
371 if (bestFamilyIndex == -1) {
372 // libtxt: check if the fallback font provider can match this character
373 if (mFallbackFontProvider) {
374 const std::shared_ptr<FontFamily>& fallback =
375 findFallbackFont(ch, vs, langListId);
376 if (fallback) {
377 return fallback;
378 }
379 }
380
381 UErrorCode errorCode = U_ZERO_ERROR;
382 const UNormalizer2* normalizer = unorm2_getNFDInstance(&errorCode);
383 if (U_SUCCESS(errorCode)) {
384 UChar decomposed[4];
385 int len =
386 unorm2_getRawDecomposition(normalizer, ch, decomposed, 4, &errorCode);
387 if (U_SUCCESS(errorCode) && len > 0) {
388 int off = 0;
389 U16_NEXT_UNSAFE(decomposed, off, ch);
390 return getFamilyForChar(ch, vs, langListId, variant);
391 }
392 }
393 return mFamilies[0];
394 }
395 return vs == 0 ? mFamilies[mFamilyVec[bestFamilyIndex]]
396 : mFamilies[bestFamilyIndex];
397 }
398
findFallbackFont(uint32_t ch,uint32_t vs,uint32_t langListId) const399 const std::shared_ptr<FontFamily>& FontCollection::findFallbackFont(
400 uint32_t ch,
401 uint32_t vs,
402 uint32_t langListId) const {
403 std::string locale = GetFontLocale(langListId);
404
405 const auto it = mCachedFallbackFamilies.find(locale);
406 if (it != mCachedFallbackFamilies.end()) {
407 for (const auto& fallbackFamily : it->second) {
408 if (calcCoverageScore(ch, vs, fallbackFamily)) {
409 return fallbackFamily;
410 }
411 }
412 }
413
414 const std::shared_ptr<FontFamily>& fallback =
415 mFallbackFontProvider->matchFallbackFont(ch, GetFontLocale(langListId));
416
417 if (fallback) {
418 mCachedFallbackFamilies[locale].push_back(fallback);
419 }
420 return fallback;
421 }
422
423 const uint32_t NBSP = 0x00A0;
424 const uint32_t SOFT_HYPHEN = 0x00AD;
425 const uint32_t ZWJ = 0x200C;
426 const uint32_t ZWNJ = 0x200D;
427 const uint32_t HYPHEN = 0x2010;
428 const uint32_t NB_HYPHEN = 0x2011;
429 const uint32_t NNBSP = 0x202F;
430 const uint32_t FEMALE_SIGN = 0x2640;
431 const uint32_t MALE_SIGN = 0x2642;
432 const uint32_t STAFF_OF_AESCULAPIUS = 0x2695;
433
434 // Characters where we want to continue using existing font run instead of
435 // recomputing the best match in the fallback list.
436 static const uint32_t stickyAllowlist[] = {
437 '!', ',', '-', '.',
438 ':', ';', '?', NBSP,
439 ZWJ, ZWNJ, HYPHEN, NB_HYPHEN,
440 NNBSP, FEMALE_SIGN, MALE_SIGN, STAFF_OF_AESCULAPIUS};
441
isStickyAllowed(uint32_t c)442 static bool isStickyAllowed(uint32_t c) {
443 for (size_t i = 0; i < sizeof(stickyAllowlist) / sizeof(stickyAllowlist[0]);
444 i++) {
445 if (stickyAllowlist[i] == c)
446 return true;
447 }
448 return false;
449 }
450
isVariationSelector(uint32_t c)451 static bool isVariationSelector(uint32_t c) {
452 return (0xFE00 <= c && c <= 0xFE0F) || (0xE0100 <= c && c <= 0xE01EF);
453 }
454
hasVariationSelector(uint32_t baseCodepoint,uint32_t variationSelector) const455 bool FontCollection::hasVariationSelector(uint32_t baseCodepoint,
456 uint32_t variationSelector) const {
457 if (!isVariationSelector(variationSelector)) {
458 return false;
459 }
460 if (baseCodepoint >= mMaxChar) {
461 return false;
462 }
463
464 std::scoped_lock _l(gMinikinLock);
465
466 // Currently mRanges can not be used here since it isn't aware of the
467 // variation sequence.
468 for (size_t i = 0; i < mVSFamilyVec.size(); i++) {
469 if (mVSFamilyVec[i]->hasGlyph(baseCodepoint, variationSelector)) {
470 return true;
471 }
472 }
473
474 // Even if there is no cmap format 14 subtable entry for the given sequence,
475 // should return true for <char, text presentation selector> case since we
476 // have special fallback rule for the sequence. Note that we don't need to
477 // restrict this to already standardized variation sequences, since Unicode is
478 // adding variation sequences more frequently now and may even move towards
479 // allowing text and emoji variation selectors on any character.
480 if (variationSelector == TEXT_STYLE_VS) {
481 for (size_t i = 0; i < mFamilies.size(); ++i) {
482 if (!mFamilies[i]->isColorEmojiFamily() &&
483 mFamilies[i]->hasGlyph(baseCodepoint, 0)) {
484 return true;
485 }
486 }
487 }
488
489 return false;
490 }
491
itemize(const uint16_t * string,size_t string_size,FontStyle style,vector<Run> * result) const492 void FontCollection::itemize(const uint16_t* string,
493 size_t string_size,
494 FontStyle style,
495 vector<Run>* result) const {
496 const uint32_t langListId = style.getLanguageListId();
497 int variant = style.getVariant();
498 const FontFamily* lastFamily = nullptr;
499 Run* run = NULL;
500
501 if (string_size == 0) {
502 return;
503 }
504
505 const uint32_t kEndOfString = 0xFFFFFFFF;
506
507 uint32_t nextCh = 0;
508 uint32_t prevCh = 0;
509 size_t nextUtf16Pos = 0;
510 size_t readLength = 0;
511 U16_NEXT(string, readLength, string_size, nextCh);
512
513 do {
514 const uint32_t ch = nextCh;
515 const size_t utf16Pos = nextUtf16Pos;
516 nextUtf16Pos = readLength;
517 if (readLength < string_size) {
518 U16_NEXT(string, readLength, string_size, nextCh);
519 } else {
520 nextCh = kEndOfString;
521 }
522
523 bool shouldContinueRun = false;
524 if (lastFamily != nullptr) {
525 if (isStickyAllowed(ch)) {
526 // Continue using existing font as long as it has coverage and is
527 // allowed.
528 shouldContinueRun = lastFamily->getCoverage().get(ch);
529 } else if (ch == SOFT_HYPHEN || isVariationSelector(ch)) {
530 // Always continue if the character is the soft hyphen or a variation
531 // selector.
532 shouldContinueRun = true;
533 }
534 }
535
536 if (!shouldContinueRun) {
537 const std::shared_ptr<FontFamily>& family = getFamilyForChar(
538 ch, isVariationSelector(nextCh) ? nextCh : 0, langListId, variant);
539 if (utf16Pos == 0 || family.get() != lastFamily) {
540 size_t start = utf16Pos;
541 // Workaround for combining marks and emoji modifiers until we implement
542 // per-cluster font selection: if a combining mark or an emoji modifier
543 // is found in a different font that also supports the previous
544 // character, attach previous character to the new run. U+20E3 COMBINING
545 // ENCLOSING KEYCAP, used in emoji, is handled properly by this since
546 // it's a combining mark too.
547 if (utf16Pos != 0 &&
548 ((U_GET_GC_MASK(ch) & U_GC_M_MASK) != 0 ||
549 (isEmojiModifier(ch) && isEmojiBase(prevCh))) &&
550 family != nullptr && family->getCoverage().get(prevCh)) {
551 const size_t prevChLength = U16_LENGTH(prevCh);
552 run->end -= prevChLength;
553 if (run->start == run->end) {
554 result->pop_back();
555 }
556 start -= prevChLength;
557 }
558 result->push_back(
559 {family->getClosestMatch(style), static_cast<int>(start), 0});
560 run = &result->back();
561 lastFamily = family.get();
562 }
563 }
564 prevCh = ch;
565 run->end = nextUtf16Pos; // exclusive
566 } while (nextCh != kEndOfString);
567 }
568
baseFontFaked(FontStyle style)569 FakedFont FontCollection::baseFontFaked(FontStyle style) {
570 return mFamilies[0]->getClosestMatch(style);
571 }
572
createCollectionWithVariation(const std::vector<FontVariation> & variations)573 std::shared_ptr<FontCollection> FontCollection::createCollectionWithVariation(
574 const std::vector<FontVariation>& variations) {
575 if (variations.empty() || mSupportedAxes.empty()) {
576 return nullptr;
577 }
578
579 bool hasSupportedAxis = false;
580 for (const FontVariation& variation : variations) {
581 if (mSupportedAxes.find(variation.axisTag) != mSupportedAxes.end()) {
582 hasSupportedAxis = true;
583 break;
584 }
585 }
586 if (!hasSupportedAxis) {
587 // None of variation axes are supported by this font collection.
588 return nullptr;
589 }
590
591 std::vector<std::shared_ptr<FontFamily>> families;
592 for (const std::shared_ptr<FontFamily>& family : mFamilies) {
593 std::shared_ptr<FontFamily> newFamily =
594 family->createFamilyWithVariation(variations);
595 if (newFamily) {
596 families.push_back(newFamily);
597 } else {
598 families.push_back(family);
599 }
600 }
601
602 return FontCollection::Create(std::move(families));
603 }
604
getId() const605 uint32_t FontCollection::getId() const {
606 return mId;
607 }
608
609 } // namespace minikin
610