FontCollection.cpp 7.0 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>
R
Raph Levien 已提交
21

22
#include "MinikinInternal.h"
R
Raph Levien 已提交
23 24 25 26 27 28 29 30 31 32 33 34
#include <minikin/CmapCoverage.h>
#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;
}

35 36
uint32_t FontCollection::sNextId = 0;

R
Raph Levien 已提交
37 38
FontCollection::FontCollection(const vector<FontFamily*>& typefaces) :
    mMaxChar(0) {
39
    AutoMutex _l(gMinikinLock);
40
    mId = sNextId++;
R
Raph Levien 已提交
41 42 43
    vector<uint32_t> lastChar;
    size_t nTypefaces = typefaces.size();
#ifdef VERBOSE_DEBUG
44
    ALOGD("nTypefaces = %d\n", nTypefaces);
R
Raph Levien 已提交
45 46 47 48
#endif
    const FontStyle defaultStyle;
    for (size_t i = 0; i < nTypefaces; i++) {
        FontFamily* family = typefaces[i];
R
Raph Levien 已提交
49
        family->RefLocked();
R
Raph Levien 已提交
50 51 52 53 54
        FontInstance dummy;
        mInstances.push_back(dummy);  // emplace_back would be better
        FontInstance* instance = &mInstances.back();
        instance->mFamily = family;
        instance->mCoverage = new SparseBitSet;
R
Raph Levien 已提交
55
        MinikinFont* typeface = family->getClosestMatch(defaultStyle).font;
56 57 58 59 60 61
        if (typeface == NULL) {
            ALOGE("FontCollection: closest match was null");
            // TODO: we shouldn't hit this, as there should be more robust
            // checks upstream to prevent empty/invalid FontFamily objects
            continue;
        }
R
Raph Levien 已提交
62
#ifdef VERBOSE_DEBUG
63
        ALOGD("closest match = %p, family size = %d\n", typeface, family->getNumFonts());
R
Raph Levien 已提交
64
#endif
R
Raph Levien 已提交
65 66 67
        const uint32_t cmapTag = MinikinFont::MakeTag('c', 'm', 'a', 'p');
        size_t cmapSize = 0;
        bool ok = typeface->GetTable(cmapTag, NULL, &cmapSize);
R
Raph Levien 已提交
68
        UniquePtr<uint8_t[]> cmapData(new uint8_t[cmapSize]);
R
Raph Levien 已提交
69
        ok = typeface->GetTable(cmapTag, cmapData.get(), &cmapSize);
R
Raph Levien 已提交
70 71
        CmapCoverage::getCoverage(*instance->mCoverage, cmapData.get(), cmapSize);
#ifdef VERBOSE_DEBUG
72
        ALOGD("font coverage length=%d, first ch=%x\n", instance->mCoverage->length(),
R
Raph Levien 已提交
73 74 75 76 77
                instance->mCoverage->nextSetBit(0));
#endif
        mMaxChar = max(mMaxChar, instance->mCoverage->length());
        lastChar.push_back(instance->mCoverage->nextSetBit(0));
    }
78
    size_t nPages = (mMaxChar + kPageMask) >> kLogCharsPerPage;
R
Raph Levien 已提交
79 80 81 82 83 84
    size_t offset = 0;
    for (size_t i = 0; i < nPages; i++) {
        Range dummy;
        mRanges.push_back(dummy);
        Range* range = &mRanges.back();
#ifdef VERBOSE_DEBUG
85
        ALOGD("i=%d: range start = %d\n", i, offset);
R
Raph Levien 已提交
86 87 88 89 90 91 92 93 94
#endif
        range->start = offset;
        for (size_t j = 0; j < nTypefaces; j++) {
            if (lastChar[j] < (i + 1) << kLogCharsPerPage) {
                const FontInstance* instance = &mInstances[j];
                mInstanceVec.push_back(instance);
                offset++;
                uint32_t nextChar = instance->mCoverage->nextSetBit((i + 1) << kLogCharsPerPage);
#ifdef VERBOSE_DEBUG
95
                ALOGD("nextChar = %d (j = %d)\n", nextChar, j);
R
Raph Levien 已提交
96 97 98 99 100 101 102 103 104 105 106
#endif
                lastChar[j] = nextChar;
            }
        }
        range->end = offset;
    }
}

FontCollection::~FontCollection() {
    for (size_t i = 0; i < mInstances.size(); i++) {
        delete mInstances[i].mCoverage;
R
Raph Levien 已提交
107
        mInstances[i].mFamily->UnrefLocked();
R
Raph Levien 已提交
108 109 110
    }
}

R
Raph Levien 已提交
111 112 113 114 115 116
// Implement heuristic for choosing best-match font. Here are the rules:
// 1. If first font in the collection has the character, it wins.
// 2. If a font matches both language and script, it gets a score of 4.
// 3. If a font matches just language, it gets a score of 2.
// 4. Matching the "compact" or "elegant" variant adds one to the score.
// 5. Highest score wins, with ties resolved to the first font.
117 118
const FontCollection::FontInstance* FontCollection::getInstanceForChar(uint32_t ch,
            FontLanguage lang, int variant) const {
R
Raph Levien 已提交
119 120 121 122 123
    if (ch >= mMaxChar) {
        return NULL;
    }
    const Range& range = mRanges[ch >> kLogCharsPerPage];
#ifdef VERBOSE_DEBUG
124
    ALOGD("querying range %d:%d\n", range.start, range.end);
R
Raph Levien 已提交
125
#endif
126
    const FontInstance* bestInstance = NULL;
R
Raph Levien 已提交
127
    int bestScore = -1;
R
Raph Levien 已提交
128 129 130
    for (size_t i = range.start; i < range.end; i++) {
        const FontInstance* instance = mInstanceVec[i];
        if (instance->mCoverage->get(ch)) {
R
Raph Levien 已提交
131 132 133
            FontFamily* family = instance->mFamily;
            // First font family in collection always matches
            if (mInstances[0].mFamily == family) {
134
                return instance;
R
Raph Levien 已提交
135 136 137 138 139 140 141
            }
            int score = lang.match(family->lang()) * 2;
            if (variant != 0 && variant == family->variant()) {
                score++;
            }
            if (score > bestScore) {
                bestScore = score;
142
                bestInstance = instance;
R
Raph Levien 已提交
143
            }
R
Raph Levien 已提交
144 145
        }
    }
146
    return bestInstance;
R
Raph Levien 已提交
147 148 149 150
}

void FontCollection::itemize(const uint16_t *string, size_t string_size, FontStyle style,
        vector<Run>* result) const {
R
Raph Levien 已提交
151 152
    FontLanguage lang = style.getLanguage();
    int variant = style.getVariant();
153
    const FontInstance* lastInstance = NULL;
R
Raph Levien 已提交
154 155 156 157 158 159 160 161 162 163 164 165
    Run* run = NULL;
    int nShorts;
    for (size_t i = 0; i < string_size; i += nShorts) {
        nShorts = 1;
        uint32_t ch = string[i];
        // sigh, decode UTF-16 by hand here
        if ((ch & 0xfc00) == 0xd800) {
            if ((i + 1) < string_size) {
                ch = 0x10000 + ((ch & 0x3ff) << 10) + (string[i + 1] & 0x3ff);
                nShorts = 2;
            }
        }
166 167 168 169 170 171 172 173
        // Continue using existing font as long as it has coverage.
        if (lastInstance == NULL || !lastInstance->mCoverage->get(ch)) {
            const FontInstance* instance = getInstanceForChar(ch, lang, variant);
            if (i == 0 || instance != lastInstance) {
                Run dummy;
                result->push_back(dummy);
                run = &result->back();
                if (instance == NULL) {
R
Raph Levien 已提交
174
                    run->fakedFont.font = NULL;
175
                } else {
R
Raph Levien 已提交
176
                    run->fakedFont = instance->mFamily->getClosestMatch(style);
177 178 179
                }
                lastInstance = instance;
                run->start = i;
R
Raph Levien 已提交
180 181 182 183 184 185
            }
        }
        run->end = i + nShorts;
    }
}

186
MinikinFont* FontCollection::baseFont(FontStyle style) {
R
Raph Levien 已提交
187 188 189 190
    return baseFontFaked(style).font;
}

FakedFont FontCollection::baseFontFaked(FontStyle style) {
191
    if (mInstances.empty()) {
R
Raph Levien 已提交
192
        return FakedFont();
193 194 195 196
    }
    return mInstances[0].mFamily->getClosestMatch(style);
}

197 198 199 200
uint32_t FontCollection::getId() const {
    return mId;
}

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