FontCollection.cpp revision 6c4c098cbd37eccef483ab1986127250b4d2ddf2
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 "FontLanguageListCache.h"
26#include "MinikinInternal.h"
27#include <minikin/FontCollection.h>
28
29using std::vector;
30
31namespace android {
32
33template <typename T>
34static inline T max(T a, T b) {
35    return a>b ? a : b;
36}
37
38uint32_t FontCollection::sNextId = 0;
39
40FontCollection::FontCollection(const vector<FontFamily*>& typefaces) :
41    mMaxChar(0) {
42    AutoMutex _l(gMinikinLock);
43    mId = sNextId++;
44    vector<uint32_t> lastChar;
45    size_t nTypefaces = typefaces.size();
46#ifdef VERBOSE_DEBUG
47    ALOGD("nTypefaces = %zd\n", nTypefaces);
48#endif
49    const FontStyle defaultStyle;
50    for (size_t i = 0; i < nTypefaces; i++) {
51        FontFamily* family = typefaces[i];
52        MinikinFont* typeface = family->getClosestMatch(defaultStyle).font;
53        if (typeface == NULL) {
54            continue;
55        }
56        family->RefLocked();
57        const SparseBitSet* coverage = family->getCoverage();
58        if (coverage == nullptr) {
59            family->UnrefLocked();
60            continue;
61        }
62        mFamilies.push_back(family);  // emplace_back would be better
63        mMaxChar = max(mMaxChar, coverage->length());
64        lastChar.push_back(coverage->nextSetBit(0));
65    }
66    nTypefaces = mFamilies.size();
67    LOG_ALWAYS_FATAL_IF(nTypefaces == 0,
68        "Font collection must have at least one valid typeface");
69    size_t nPages = (mMaxChar + kPageMask) >> kLogCharsPerPage;
70    size_t offset = 0;
71    // TODO: Use variation selector map for mRanges construction.
72    // A font can have a glyph for a base code point and variation selector pair but no glyph for
73    // the base code point without variation selector. The family won't be listed in the range in
74    // this case.
75    for (size_t i = 0; i < nPages; i++) {
76        Range dummy;
77        mRanges.push_back(dummy);
78        Range* range = &mRanges.back();
79#ifdef VERBOSE_DEBUG
80        ALOGD("i=%zd: range start = %zd\n", i, offset);
81#endif
82        range->start = offset;
83        for (size_t j = 0; j < nTypefaces; j++) {
84            if (lastChar[j] < (i + 1) << kLogCharsPerPage) {
85                FontFamily* family = mFamilies[j];
86                mFamilyVec.push_back(family);
87                offset++;
88                uint32_t nextChar = family->getCoverage()->nextSetBit((i + 1) << kLogCharsPerPage);
89#ifdef VERBOSE_DEBUG
90                ALOGD("nextChar = %d (j = %zd)\n", nextChar, j);
91#endif
92                lastChar[j] = nextChar;
93            }
94        }
95        range->end = offset;
96    }
97}
98
99FontCollection::~FontCollection() {
100    for (size_t i = 0; i < mFamilies.size(); i++) {
101        mFamilies[i]->UnrefLocked();
102    }
103}
104
105// Implement heuristic for choosing best-match font. Here are the rules:
106// 1. If first font in the collection has the character, it wins.
107// 2. If a font matches language, it gets a score of 2.
108// 3. Matching the "compact" or "elegant" variant adds one to the score.
109// 4. If there is a variation selector and a font supports the complete variation sequence, we add
110//    8 to the score.
111// 5. If there is a color variation selector (U+FE0F), we add 4 to the score if the font is an emoji
112//    font. This additional score of 4 is only given if the base character is supported in the font,
113//    but not the whole variation sequence.
114// 6. If there is a text variation selector (U+FE0E), we add 4 to the score if the font is not an
115//    emoji font. This additional score of 4 is only given if the base character is supported in the
116//    font, but not the whole variation sequence.
117// 7. Highest score wins, with ties resolved to the first font.
118FontFamily* FontCollection::getFamilyForChar(uint32_t ch, uint32_t vs,
119            uint32_t langListId, int variant) const {
120    if (ch >= mMaxChar) {
121        return NULL;
122    }
123
124    const FontLanguages& langList = FontLanguageListCache::getById(langListId);
125    // TODO: use all languages in langList.
126    const FontLanguage lang = (langList.size() == 0) ? FontLanguage() : langList[0];
127
128    // Even if the font supports variation sequence, mRanges isn't aware of the base character of
129    // the sequence. Search all FontFamilies if variation sequence is specified.
130    // TODO: Always use mRanges for font search.
131    const std::vector<FontFamily*>& familyVec = (vs == 0) ? mFamilyVec : mFamilies;
132    Range range;
133    if (vs == 0) {
134        range = mRanges[ch >> kLogCharsPerPage];
135    } else {
136        range = { 0, mFamilies.size() };
137    }
138
139#ifdef VERBOSE_DEBUG
140    ALOGD("querying range %zd:%zd\n", range.start, range.end);
141#endif
142    FontFamily* bestFamily = nullptr;
143    int bestScore = -1;
144    for (size_t i = range.start; i < range.end; i++) {
145        FontFamily* family = familyVec[i];
146        const bool hasVSGlyph = (vs != 0) && family->hasVariationSelector(ch, vs);
147        if (hasVSGlyph || family->getCoverage()->get(ch)) {
148            if ((vs == 0 || hasVSGlyph) && mFamilies[0] == family) {
149                // If the first font family in collection supports the given character or sequence,
150                // always use it.
151                return family;
152            }
153            int score = lang.match(family->lang()) * 2;
154            if (family->variant() == 0 || family->variant() == variant) {
155                score++;
156            }
157            if (hasVSGlyph) {
158                score += 8;
159            } else if (((vs == 0xFE0F) && family->lang().hasEmojiFlag()) ||
160                    ((vs == 0xFE0E) && !family->lang().hasEmojiFlag())) {
161                score += 4;
162            }
163            if (score > bestScore) {
164                bestScore = score;
165                bestFamily = family;
166            }
167        }
168    }
169    if (bestFamily == nullptr && vs != 0) {
170        // If no fonts support the codepoint and variation selector pair,
171        // fallback to select a font family that supports just the base
172        // character, ignoring the variation selector.
173        return getFamilyForChar(ch, 0, langListId, variant);
174    }
175    if (bestFamily == nullptr && !mFamilyVec.empty()) {
176        UErrorCode errorCode = U_ZERO_ERROR;
177        const UNormalizer2* normalizer = unorm2_getNFDInstance(&errorCode);
178        if (U_SUCCESS(errorCode)) {
179            UChar decomposed[4];
180            int len = unorm2_getRawDecomposition(normalizer, ch, decomposed, 4, &errorCode);
181            if (U_SUCCESS(errorCode) && len > 0) {
182                int off = 0;
183                U16_NEXT_UNSAFE(decomposed, off, ch);
184                return getFamilyForChar(ch, vs, langListId, variant);
185            }
186        }
187        bestFamily = mFamilies[0];
188    }
189    return bestFamily;
190}
191
192const uint32_t NBSP = 0xa0;
193const uint32_t ZWJ = 0x200c;
194const uint32_t ZWNJ = 0x200d;
195const uint32_t KEYCAP = 0x20e3;
196const uint32_t HYPHEN = 0x2010;
197const uint32_t NB_HYPHEN = 0x2011;
198
199// Characters where we want to continue using existing font run instead of
200// recomputing the best match in the fallback list.
201static const uint32_t stickyWhitelist[] = { '!', ',', '-', '.', ':', ';', '?', NBSP, ZWJ, ZWNJ,
202        KEYCAP, HYPHEN, NB_HYPHEN };
203
204static bool isStickyWhitelisted(uint32_t c) {
205    for (size_t i = 0; i < sizeof(stickyWhitelist) / sizeof(stickyWhitelist[0]); i++) {
206        if (stickyWhitelist[i] == c) return true;
207    }
208    return false;
209}
210
211static bool isVariationSelector(uint32_t c) {
212    return (0xFE00 <= c && c <= 0xFE0F) || (0xE0100 <= c && c <= 0xE01EF);
213}
214
215bool FontCollection::hasVariationSelector(uint32_t baseCodepoint,
216        uint32_t variationSelector) const {
217    if (!isVariationSelector(variationSelector)) {
218        return false;
219    }
220    if (baseCodepoint >= mMaxChar) {
221        return false;
222    }
223    // Currently mRanges can not be used here since it isn't aware of the variation sequence.
224    // TODO: Use mRanges for narrowing down the search range.
225    for (size_t i = 0; i < mFamilies.size(); i++) {
226        if (mFamilies[i]->hasVariationSelector(baseCodepoint, variationSelector)) {
227          return true;
228        }
229    }
230    return false;
231}
232
233void FontCollection::itemize(const uint16_t *string, size_t string_size, FontStyle style,
234        vector<Run>* result) const {
235    const uint32_t langListId = style.getLanguageListId();
236    int variant = style.getVariant();
237    FontFamily* lastFamily = NULL;
238    Run* run = NULL;
239
240    if (string_size == 0) {
241        return;
242    }
243
244    const uint32_t kEndOfString = 0xFFFFFFFF;
245
246    uint32_t nextCh = 0;
247    uint32_t prevCh = 0;
248    size_t nextUtf16Pos = 0;
249    size_t readLength = 0;
250    U16_NEXT(string, readLength, string_size, nextCh);
251
252    do {
253        const uint32_t ch = nextCh;
254        const size_t utf16Pos = nextUtf16Pos;
255        nextUtf16Pos = readLength;
256        if (readLength < string_size) {
257            U16_NEXT(string, readLength, string_size, nextCh);
258        } else {
259            nextCh = kEndOfString;
260        }
261
262        bool shouldContinueRun = false;
263        if (lastFamily != nullptr) {
264            if (isStickyWhitelisted(ch)) {
265                // Continue using existing font as long as it has coverage and is whitelisted
266                shouldContinueRun = lastFamily->getCoverage()->get(ch);
267            } else if (isVariationSelector(ch)) {
268                // Always continue if the character is a variation selector.
269                shouldContinueRun = true;
270            }
271        }
272
273        if (!shouldContinueRun) {
274            FontFamily* family = getFamilyForChar(ch, isVariationSelector(nextCh) ? nextCh : 0,
275                    langListId, variant);
276            if (utf16Pos == 0 || family != lastFamily) {
277                size_t start = utf16Pos;
278                // Workaround for Emoji keycap until we implement per-cluster font
279                // selection: if keycap is found in a different font that also
280                // supports previous char, attach previous char to the new run.
281                // Bug 7557244.
282                if (ch == KEYCAP && utf16Pos != 0 && family && family->getCoverage()->get(prevCh)) {
283                    const size_t prevChLength = U16_LENGTH(prevCh);
284                    run->end -= prevChLength;
285                    if (run->start == run->end) {
286                        result->pop_back();
287                    }
288                    start -= prevChLength;
289                }
290                Run dummy;
291                result->push_back(dummy);
292                run = &result->back();
293                if (family == NULL) {
294                    run->fakedFont.font = NULL;
295                } else {
296                    run->fakedFont = family->getClosestMatch(style);
297                }
298                lastFamily = family;
299                run->start = start;
300            }
301        }
302        prevCh = ch;
303        run->end = nextUtf16Pos;  // exclusive
304    } while (nextCh != kEndOfString);
305}
306
307MinikinFont* FontCollection::baseFont(FontStyle style) {
308    return baseFontFaked(style).font;
309}
310
311FakedFont FontCollection::baseFontFaked(FontStyle style) {
312    if (mFamilies.empty()) {
313        return FakedFont();
314    }
315    return mFamilies[0]->getClosestMatch(style);
316}
317
318uint32_t FontCollection::getId() const {
319    return mId;
320}
321
322void FontCollection::purgeFontFamilyHbFontCache() const {
323    assertMinikinLocked();
324    for (size_t i = 0; i < mFamilies.size(); ++i) {
325        mFamilies[i]->purgeHbFontCache();
326    }
327}
328
329}  // namespace android
330