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