FontCollection.cpp revision c31e3883456e018d742e9f29815ba5ff8b315ea1
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 "MinikinInternal.h"
23#include <minikin/CmapCoverage.h>
24#include <minikin/FontCollection.h>
25
26using std::vector;
27
28namespace android {
29
30template <typename T>
31static inline T max(T a, T b) {
32    return a>b ? a : b;
33}
34
35FontCollection::FontCollection(const vector<FontFamily*>& typefaces) :
36    mMaxChar(0) {
37    AutoMutex _l(gMinikinLock);
38    vector<uint32_t> lastChar;
39    size_t nTypefaces = typefaces.size();
40#ifdef VERBOSE_DEBUG
41    ALOGD("nTypefaces = %d\n", nTypefaces);
42#endif
43    const FontStyle defaultStyle;
44    for (size_t i = 0; i < nTypefaces; i++) {
45        FontFamily* family = typefaces[i];
46        family->RefLocked();
47        FontInstance dummy;
48        mInstances.push_back(dummy);  // emplace_back would be better
49        FontInstance* instance = &mInstances.back();
50        instance->mFamily = family;
51        instance->mCoverage = new SparseBitSet;
52        MinikinFont* typeface = family->getClosestMatch(defaultStyle);
53#ifdef VERBOSE_DEBUG
54        ALOGD("closest match = %p, family size = %d\n", typeface, family->getNumFonts());
55#endif
56        const uint32_t cmapTag = MinikinFont::MakeTag('c', 'm', 'a', 'p');
57        size_t cmapSize = 0;
58        bool ok = typeface->GetTable(cmapTag, NULL, &cmapSize);
59        UniquePtr<uint8_t[]> cmapData(new uint8_t[cmapSize]);
60        ok = typeface->GetTable(cmapTag, cmapData.get(), &cmapSize);
61        CmapCoverage::getCoverage(*instance->mCoverage, cmapData.get(), cmapSize);
62#ifdef VERBOSE_DEBUG
63        ALOGD("font coverage length=%d, first ch=%x\n", instance->mCoverage->length(),
64                instance->mCoverage->nextSetBit(0));
65#endif
66        mMaxChar = max(mMaxChar, instance->mCoverage->length());
67        lastChar.push_back(instance->mCoverage->nextSetBit(0));
68    }
69    size_t nPages = mMaxChar >> kLogCharsPerPage;
70    size_t offset = 0;
71    for (size_t i = 0; i < nPages; i++) {
72        Range dummy;
73        mRanges.push_back(dummy);
74        Range* range = &mRanges.back();
75#ifdef VERBOSE_DEBUG
76        ALOGD("i=%d: range start = %d\n", i, offset);
77#endif
78        range->start = offset;
79        for (size_t j = 0; j < nTypefaces; j++) {
80            if (lastChar[j] < (i + 1) << kLogCharsPerPage) {
81                const FontInstance* instance = &mInstances[j];
82                mInstanceVec.push_back(instance);
83                offset++;
84                uint32_t nextChar = instance->mCoverage->nextSetBit((i + 1) << kLogCharsPerPage);
85#ifdef VERBOSE_DEBUG
86                ALOGD("nextChar = %d (j = %d)\n", nextChar, j);
87#endif
88                lastChar[j] = nextChar;
89            }
90        }
91        range->end = offset;
92    }
93}
94
95FontCollection::~FontCollection() {
96    for (size_t i = 0; i < mInstances.size(); i++) {
97        delete mInstances[i].mCoverage;
98        mInstances[i].mFamily->UnrefLocked();
99    }
100}
101
102const FontFamily* FontCollection::getFamilyForChar(uint32_t ch) const {
103    if (ch >= mMaxChar) {
104        return NULL;
105    }
106    const Range& range = mRanges[ch >> kLogCharsPerPage];
107#ifdef VERBOSE_DEBUG
108    ALOGD("querying range %d:%d\n", range.start, range.end);
109#endif
110    for (size_t i = range.start; i < range.end; i++) {
111        const FontInstance* instance = mInstanceVec[i];
112        if (instance->mCoverage->get(ch)) {
113            return instance->mFamily;
114        }
115    }
116    return NULL;
117}
118
119void FontCollection::itemize(const uint16_t *string, size_t string_size, FontStyle style,
120        vector<Run>* result) const {
121    const FontFamily* lastFamily = NULL;
122    Run* run = NULL;
123    int nShorts;
124    for (size_t i = 0; i < string_size; i += nShorts) {
125        nShorts = 1;
126        uint32_t ch = string[i];
127        // sigh, decode UTF-16 by hand here
128        if ((ch & 0xfc00) == 0xd800) {
129            if ((i + 1) < string_size) {
130                ch = 0x10000 + ((ch & 0x3ff) << 10) + (string[i + 1] & 0x3ff);
131                nShorts = 2;
132            }
133        }
134        const FontFamily* family = getFamilyForChar(ch);
135        if (i == 0 || family != lastFamily) {
136            Run dummy;
137            result->push_back(dummy);
138            run = &result->back();
139            if (family == NULL) {
140                run->font = NULL;  // maybe we should do something different here
141            } else {
142                run->font = family->getClosestMatch(style);
143                run->font->RefLocked();
144            }
145            lastFamily = family;
146            run->start = i;
147        }
148        run->end = i + nShorts;
149    }
150}
151
152}  // namespace android
153