FontCollection.cpp 16.3 KB
Newer Older
R
Raph Levien 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17 18 19 20
// #define VERBOSE_DEBUG

#define LOG_TAG "Minikin"
#include <cutils/log.h>
21
#include <algorithm>
R
Raph Levien 已提交
22

23 24 25
#include "unicode/unistr.h"
#include "unicode/unorm2.h"

26
#include "FontLanguage.h"
S
Seigo Nonaka 已提交
27
#include "FontLanguageListCache.h"
28
#include "MinikinInternal.h"
R
Raph Levien 已提交
29 30 31 32 33 34 35 36 37 38 39
#include <minikin/FontCollection.h>

using std::vector;

namespace android {

template <typename T>
static inline T max(T a, T b) {
    return a>b ? a : b;
}

40 41
uint32_t FontCollection::sNextId = 0;

R
Raph Levien 已提交
42 43
FontCollection::FontCollection(const vector<FontFamily*>& typefaces) :
    mMaxChar(0) {
44
    AutoMutex _l(gMinikinLock);
45
    mId = sNextId++;
R
Raph Levien 已提交
46 47 48
    vector<uint32_t> lastChar;
    size_t nTypefaces = typefaces.size();
#ifdef VERBOSE_DEBUG
49
    ALOGD("nTypefaces = %zd\n", nTypefaces);
R
Raph Levien 已提交
50 51 52 53
#endif
    const FontStyle defaultStyle;
    for (size_t i = 0; i < nTypefaces; i++) {
        FontFamily* family = typefaces[i];
R
Raph Levien 已提交
54
        MinikinFont* typeface = family->getClosestMatch(defaultStyle).font;
55 56 57
        if (typeface == NULL) {
            continue;
        }
58
        family->RefLocked();
59
        const SparseBitSet* coverage = family->getCoverage();
60 61 62 63 64
        if (coverage == nullptr) {
            family->UnrefLocked();
            continue;
        }
        mFamilies.push_back(family);  // emplace_back would be better
65 66 67
        if (family->hasVSTable()) {
            mVSFamilyVec.push_back(family);
        }
68 69
        mMaxChar = max(mMaxChar, coverage->length());
        lastChar.push_back(coverage->nextSetBit(0));
R
Raph Levien 已提交
70
    }
71
    nTypefaces = mFamilies.size();
72 73
    LOG_ALWAYS_FATAL_IF(nTypefaces == 0,
        "Font collection must have at least one valid typeface");
74
    size_t nPages = (mMaxChar + kPageMask) >> kLogCharsPerPage;
R
Raph Levien 已提交
75
    size_t offset = 0;
76 77 78 79
    // TODO: Use variation selector map for mRanges construction.
    // A font can have a glyph for a base code point and variation selector pair but no glyph for
    // the base code point without variation selector. The family won't be listed in the range in
    // this case.
R
Raph Levien 已提交
80 81 82 83 84
    for (size_t i = 0; i < nPages; i++) {
        Range dummy;
        mRanges.push_back(dummy);
        Range* range = &mRanges.back();
#ifdef VERBOSE_DEBUG
85
        ALOGD("i=%zd: range start = %zd\n", i, offset);
R
Raph Levien 已提交
86 87 88 89
#endif
        range->start = offset;
        for (size_t j = 0; j < nTypefaces; j++) {
            if (lastChar[j] < (i + 1) << kLogCharsPerPage) {
90 91
                FontFamily* family = mFamilies[j];
                mFamilyVec.push_back(family);
R
Raph Levien 已提交
92
                offset++;
93
                uint32_t nextChar = family->getCoverage()->nextSetBit((i + 1) << kLogCharsPerPage);
R
Raph Levien 已提交
94
#ifdef VERBOSE_DEBUG
95
                ALOGD("nextChar = %d (j = %zd)\n", nextChar, j);
R
Raph Levien 已提交
96 97 98 99 100 101 102 103 104
#endif
                lastChar[j] = nextChar;
            }
        }
        range->end = offset;
    }
}

FontCollection::~FontCollection() {
105 106
    for (size_t i = 0; i < mFamilies.size(); i++) {
        mFamilies[i]->UnrefLocked();
R
Raph Levien 已提交
107 108 109
    }
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
// Special scores for the font fallback.
const uint32_t kUnsupportedFontScore = 0;
const uint32_t kFirstFontScore = UINT32_MAX;

// Calculates a font score.
// The score of the font family is based on three subscores.
//  - Coverage Score: How well the font family covers the given character or variation sequence.
//  - Language Score: How well the font family is appropriate for the language.
//  - Variant Score: Whether the font family matches the variant. Note that this variant is not the
//    one in BCP47. This is our own font variant (e.g., elegant, compact).
//
// Then, there is a priority for these three subscores as follow:
//   Coverage Score > Language Score > Variant Score
// The returned score reflects this priority order.
//
// Note that there are two special scores.
//  - kUnsupportedFontScore: When the font family doesn't support the variation sequence or even its
//    base character.
//  - kFirstFontScore: When the font is the first font family in the collection and it supports the
//    given character or variation sequence.
uint32_t FontCollection::calcFamilyScore(uint32_t ch, uint32_t vs, int variant, uint32_t langListId,
                                        FontFamily* fontFamily) const {

    const uint32_t coverageScore = calcCoverageScore(ch, vs, fontFamily);
    if (coverageScore == kFirstFontScore || coverageScore == kUnsupportedFontScore) {
        // No need to calculate other scores.
        return coverageScore;
    }

    const uint32_t languageScore = calcLanguageMatchingScore(langListId, *fontFamily);
    const uint32_t variantScore = calcVariantMatchingScore(variant, *fontFamily);

    // Subscores are encoded into 31 bits representation to meet the subscore priority.
    // The highest 2 bits are for coverage score, then following 28 bits are for language score,
    // then the last 1 bit is for variant score.
    return coverageScore << 29 | languageScore << 1 | variantScore;
}

// Calculates a font score based on variation sequence coverage.
// - Returns kUnsupportedFontScore if the font doesn't support the variation sequence or its base
//   character.
// - Returns kFirstFontScore if the font family is the first font family in the collection and it
//   supports the given character or variation sequence.
// - Returns 3 if the font family supports the variation sequence.
// - Returns 2 if the vs is a color variation selector (U+FE0F) and if the font is an emoji font.
// - Returns 2 if the vs is a text variation selector (U+FE0E) and if the font is not an emoji font.
// - Returns 1 if the variation selector is not specified or if the font family only supports the
//   variation sequence's base character.
uint32_t FontCollection::calcCoverageScore(uint32_t ch, uint32_t vs, FontFamily* fontFamily) const {
    const bool hasVSGlyph = (vs != 0) && fontFamily->hasVariationSelector(ch, vs);
    if (!hasVSGlyph && !fontFamily->getCoverage()->get(ch)) {
        // The font doesn't support either variation sequence or even the base character.
        return kUnsupportedFontScore;
    }

    if ((vs == 0 || hasVSGlyph) && mFamilies[0] == fontFamily) {
        // If the first font family supports the given character or variation sequence, always use
        // it.
        return kFirstFontScore;
    }

    if (vs == 0) {
        return 1;
    }

    if (hasVSGlyph) {
        return 3;
    }

    if (vs == 0xFE0F || vs == 0xFE0E) {
        // TODO use all language in the list.
        const FontLanguage lang = FontLanguageListCache::getById(fontFamily->langId())[0];
        const bool hasEmojiFlag = lang.hasEmojiFlag();
        if (vs == 0xFE0F) {
            return hasEmojiFlag ? 2 : 1;
        } else {  // vs == 0xFE0E
            return hasEmojiFlag ? 1 : 2;
        }
    }
    return 1;
}

// Calculates font scores based on the script matching and primary langauge matching.
//
// If the font's script doesn't support the requested script, the font gets a score of 0. If the
// font's script supports the requested script and the font has the same primary language as the
// requested one, the font gets a score of 2. If the font's script supports the requested script
// but the primary language is different from the requested one, the font gets a score of 1.
//
// If two languages in the requested list have the same language score, the font matching with
// higher priority language gets a higher score. For example, in the case the user requested
// language list is "ja-Jpan,en-Latn". The score of for the font of "ja-Jpan" gets a higher score
// than the font of "en-Latn".
//
// To achieve the above two conditions, the language score is determined as follows:
//   LanguageScore = s(0) * 3^(m - 1) + s(1) * 3^(m - 2) + ... + s(m - 2) * 3 + s(m - 1)
// Here, m is the maximum number of languages to be compared, and s(i) is the i-th language's
// matching score. The possible values of s(i) are 0, 1 and 2.
uint32_t FontCollection::calcLanguageMatchingScore(
        uint32_t userLangListId, const FontFamily& fontFamily) {
    const FontLanguages& langList = FontLanguageListCache::getById(userLangListId);
    // TODO use all language in the list.
    FontLanguage fontLanguage = FontLanguageListCache::getById(fontFamily.langId())[0];

    const size_t maxCompareNum = std::min(langList.size(), FONT_LANGUAGES_LIMIT);
    uint32_t score = fontLanguage.getScoreFor(langList[0]);  // maxCompareNum can't be zero.
    for (size_t i = 1; i < maxCompareNum; ++i) {
        score = score * 3u + fontLanguage.getScoreFor(langList[i]);
    }
    return score;
}

// Calculates a font score based on variant ("compact" or "elegant") matching.
//  - Returns 1 if the font doesn't have variant or the variant matches with the text style.
//  - No score if the font has a variant but it doesn't match with the text style.
uint32_t FontCollection::calcVariantMatchingScore(int variant, const FontFamily& fontFamily) {
    return (fontFamily.variant() == 0 || fontFamily.variant() == variant) ? 1 : 0;
}

R
Raph Levien 已提交
229 230
// Implement heuristic for choosing best-match font. Here are the rules:
// 1. If first font in the collection has the character, it wins.
231 232
// 2. Calculate a score for the font family. See comments in calcFamilyScore for the detail.
// 3. Highest score wins, with ties resolved to the first font.
233
FontFamily* FontCollection::getFamilyForChar(uint32_t ch, uint32_t vs,
S
Seigo Nonaka 已提交
234
            uint32_t langListId, int variant) const {
R
Raph Levien 已提交
235 236 237
    if (ch >= mMaxChar) {
        return NULL;
    }
238

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    const std::vector<FontFamily*>* familyVec = &mFamilyVec;
    Range range = mRanges[ch >> kLogCharsPerPage];

    std::vector<FontFamily*> familyVecForVS;
    if (vs != 0) {
        // If variation selector is specified, need to search for both the variation sequence and
        // its base codepoint. Compute the union vector of them.
        familyVecForVS = mVSFamilyVec;
        familyVecForVS.insert(familyVecForVS.end(),
                mFamilyVec.begin() + range.start, mFamilyVec.begin() + range.end);
        std::sort(familyVecForVS.begin(), familyVecForVS.end());
        auto last = std::unique(familyVecForVS.begin(), familyVecForVS.end());
        familyVecForVS.erase(last, familyVecForVS.end());

        familyVec = &familyVecForVS;
        range = { 0, familyVecForVS.size() };
255 256
    }

R
Raph Levien 已提交
257
#ifdef VERBOSE_DEBUG
258
    ALOGD("querying range %zd:%zd\n", range.start, range.end);
R
Raph Levien 已提交
259
#endif
260
    FontFamily* bestFamily = nullptr;
261
    uint32_t bestScore = kUnsupportedFontScore;
R
Raph Levien 已提交
262
    for (size_t i = range.start; i < range.end; i++) {
263
        FontFamily* family = (*familyVec)[i];
264 265 266 267 268 269 270 271 272
        const uint32_t score = calcFamilyScore(ch, vs, variant, langListId, family);
        if (score == kFirstFontScore) {
            // If the first font family supports the given character or variation sequence, always
            // use it.
            return family;
        }
        if (score > bestScore) {
            bestScore = score;
            bestFamily = family;
R
Raph Levien 已提交
273
        }
S
Seigo Nonaka 已提交
274
    }
275
    if (bestFamily == nullptr && !mFamilyVec.empty()) {
276 277 278 279 280 281 282 283
        UErrorCode errorCode = U_ZERO_ERROR;
        const UNormalizer2* normalizer = unorm2_getNFDInstance(&errorCode);
        if (U_SUCCESS(errorCode)) {
            UChar decomposed[4];
            int len = unorm2_getRawDecomposition(normalizer, ch, decomposed, 4, &errorCode);
            if (U_SUCCESS(errorCode) && len > 0) {
                int off = 0;
                U16_NEXT_UNSAFE(decomposed, off, ch);
S
Seigo Nonaka 已提交
284
                return getFamilyForChar(ch, vs, langListId, variant);
285 286
            }
        }
287
        bestFamily = mFamilies[0];
288
    }
289
    return bestFamily;
R
Raph Levien 已提交
290 291
}

R
Raph Levien 已提交
292 293 294
const uint32_t NBSP = 0xa0;
const uint32_t ZWJ = 0x200c;
const uint32_t ZWNJ = 0x200d;
295
const uint32_t KEYCAP = 0x20e3;
296 297
const uint32_t HYPHEN = 0x2010;
const uint32_t NB_HYPHEN = 0x2011;
298

R
Raph Levien 已提交
299 300
// Characters where we want to continue using existing font run instead of
// recomputing the best match in the fallback list.
301 302
static const uint32_t stickyWhitelist[] = { '!', ',', '-', '.', ':', ';', '?', NBSP, ZWJ, ZWNJ,
        KEYCAP, HYPHEN, NB_HYPHEN };
R
Raph Levien 已提交
303 304 305 306 307 308 309 310

static bool isStickyWhitelisted(uint32_t c) {
    for (size_t i = 0; i < sizeof(stickyWhitelist) / sizeof(stickyWhitelist[0]); i++) {
        if (stickyWhitelist[i] == c) return true;
    }
    return false;
}

311 312 313 314
static bool isVariationSelector(uint32_t c) {
    return (0xFE00 <= c && c <= 0xFE0F) || (0xE0100 <= c && c <= 0xE01EF);
}

315 316 317 318 319 320 321 322
bool FontCollection::hasVariationSelector(uint32_t baseCodepoint,
        uint32_t variationSelector) const {
    if (!isVariationSelector(variationSelector)) {
        return false;
    }
    if (baseCodepoint >= mMaxChar) {
        return false;
    }
323 324 325 326
    if (variationSelector == 0) {
        return false;
    }

327
    // Currently mRanges can not be used here since it isn't aware of the variation sequence.
328
    for (size_t i = 0; i < mVSFamilyVec.size(); i++) {
329
        AutoMutex _l(gMinikinLock);
330 331
        if (mVSFamilyVec[i]->hasVariationSelector(baseCodepoint, variationSelector)) {
            return true;
332 333 334 335 336
        }
    }
    return false;
}

R
Raph Levien 已提交
337 338
void FontCollection::itemize(const uint16_t *string, size_t string_size, FontStyle style,
        vector<Run>* result) const {
S
Seigo Nonaka 已提交
339
    const uint32_t langListId = style.getLanguageListId();
R
Raph Levien 已提交
340
    int variant = style.getVariant();
341
    FontFamily* lastFamily = NULL;
R
Raph Levien 已提交
342
    Run* run = NULL;
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

    if (string_size == 0) {
        return;
    }

    const uint32_t kEndOfString = 0xFFFFFFFF;

    uint32_t nextCh = 0;
    uint32_t prevCh = 0;
    size_t nextUtf16Pos = 0;
    size_t readLength = 0;
    U16_NEXT(string, readLength, string_size, nextCh);

    do {
        const uint32_t ch = nextCh;
        const size_t utf16Pos = nextUtf16Pos;
        nextUtf16Pos = readLength;
        if (readLength < string_size) {
            U16_NEXT(string, readLength, string_size, nextCh);
        } else {
            nextCh = kEndOfString;
        }

        bool shouldContinueRun = false;
        if (lastFamily != nullptr) {
            if (isStickyWhitelisted(ch)) {
                // Continue using existing font as long as it has coverage and is whitelisted
                shouldContinueRun = lastFamily->getCoverage()->get(ch);
            } else if (isVariationSelector(ch)) {
                // Always continue if the character is a variation selector.
                shouldContinueRun = true;
R
Raph Levien 已提交
374 375
            }
        }
376 377

        if (!shouldContinueRun) {
S
Seigo Nonaka 已提交
378 379
            FontFamily* family = getFamilyForChar(ch, isVariationSelector(nextCh) ? nextCh : 0,
                    langListId, variant);
380 381
            if (utf16Pos == 0 || family != lastFamily) {
                size_t start = utf16Pos;
382 383 384 385
                // Workaround for Emoji keycap until we implement per-cluster font
                // selection: if keycap is found in a different font that also
                // supports previous char, attach previous char to the new run.
                // Bug 7557244.
386
                if (ch == KEYCAP && utf16Pos != 0 && family && family->getCoverage()->get(prevCh)) {
387 388
                    const size_t prevChLength = U16_LENGTH(prevCh);
                    run->end -= prevChLength;
389 390 391
                    if (run->start == run->end) {
                        result->pop_back();
                    }
392
                    start -= prevChLength;
393
                }
394 395 396
                Run dummy;
                result->push_back(dummy);
                run = &result->back();
397
                if (family == NULL) {
R
Raph Levien 已提交
398
                    run->fakedFont.font = NULL;
399
                } else {
400
                    run->fakedFont = family->getClosestMatch(style);
401
                }
402
                lastFamily = family;
403
                run->start = start;
R
Raph Levien 已提交
404 405
            }
        }
406 407 408
        prevCh = ch;
        run->end = nextUtf16Pos;  // exclusive
    } while (nextCh != kEndOfString);
R
Raph Levien 已提交
409 410
}

411
MinikinFont* FontCollection::baseFont(FontStyle style) {
R
Raph Levien 已提交
412 413 414 415
    return baseFontFaked(style).font;
}

FakedFont FontCollection::baseFontFaked(FontStyle style) {
416
    if (mFamilies.empty()) {
R
Raph Levien 已提交
417
        return FakedFont();
418
    }
419
    return mFamilies[0]->getClosestMatch(style);
420 421
}

422 423 424 425
uint32_t FontCollection::getId() const {
    return mId;
}

R
Raph Levien 已提交
426
}  // namespace android