FontFamily.cpp revision 7b221d97b7b64dc5ce457e19666d55d042e22e62
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 LOG_TAG "Minikin"
18
19#include <cutils/log.h>
20#include <stdlib.h>
21#include <stdint.h>
22
23#include "MinikinInternal.h"
24#include <minikin/MinikinFont.h>
25#include <minikin/AnalyzeStyle.h>
26#include <minikin/FontFamily.h>
27#include <UniquePtr.h>
28
29using std::vector;
30
31namespace android {
32
33// Parse bcp-47 language identifier into internal structure
34FontLanguage::FontLanguage(const char* buf, size_t size) {
35    uint32_t bits = 0;
36    size_t i;
37    for (i = 0; i < size && buf[i] != '-' && buf[i] != '_'; i++) {
38        uint16_t c = buf[i];
39        if (c == '-' || c == '_') break;
40    }
41    if (i == 2) {
42        bits = (uint8_t(buf[0]) << 8) | uint8_t(buf[1]);
43    }
44    size_t next;
45    for (i++; i < size; i = next + 1) {
46        for (next = i; next < size; next++) {
47            uint16_t c = buf[next];
48            if (c == '-' || c == '_') break;
49        }
50        if (next - i == 4 && buf[i] == 'H' && buf[i+1] == 'a' && buf[i+2] == 'n') {
51            if (buf[i+3] == 's') {
52                bits |= kHansFlag;
53            } else if (buf[i+3] == 't') {
54                bits |= kHantFlag;
55            }
56        }
57        // TODO: this might be a good place to infer script from country (zh_TW -> Hant),
58        // but perhaps it's up to the client to do that, before passing a string.
59    }
60    mBits = bits;
61}
62
63int FontLanguage::match(const FontLanguage other) const {
64    int result = 0;
65    if ((mBits & kBaseLangMask) == (other.mBits & kBaseLangMask)) {
66        result++;
67        if ((mBits & kScriptMask) != 0 && (mBits & kScriptMask) == (other.mBits & kScriptMask)) {
68            result++;
69        }
70    }
71    return result;
72}
73
74FontFamily::~FontFamily() {
75    for (size_t i = 0; i < mFonts.size(); i++) {
76        mFonts[i].typeface->UnrefLocked();
77    }
78}
79
80bool FontFamily::addFont(MinikinFont* typeface) {
81    AutoMutex _l(gMinikinLock);
82    const uint32_t os2Tag = MinikinFont::MakeTag('O', 'S', '/', '2');
83    size_t os2Size = 0;
84    bool ok = typeface->GetTable(os2Tag, NULL, &os2Size);
85    if (!ok) return false;
86    UniquePtr<uint8_t[]> os2Data(new uint8_t[os2Size]);
87    ok = typeface->GetTable(os2Tag, os2Data.get(), &os2Size);
88    if (!ok) return false;
89    int weight;
90    bool italic;
91    if (analyzeStyle(os2Data.get(), os2Size, &weight, &italic)) {
92        //ALOGD("analyzed weight = %d, italic = %s", weight, italic ? "true" : "false");
93        FontStyle style(weight, italic);
94        addFontLocked(typeface, style);
95        return true;
96    } else {
97        ALOGD("failed to analyze style");
98    }
99    return false;
100}
101
102void FontFamily::addFont(MinikinFont* typeface, FontStyle style) {
103    AutoMutex _l(gMinikinLock);
104    addFontLocked(typeface, style);
105}
106
107void FontFamily::addFontLocked(MinikinFont* typeface, FontStyle style) {    typeface->RefLocked();
108    mFonts.push_back(Font(typeface, style));
109}
110
111// Compute a matching metric between two styles - 0 is an exact match
112int computeMatch(FontStyle style1, FontStyle style2) {
113    if (style1 == style2) return 0;
114    int score = abs(style1.getWeight() - style2.getWeight());
115    if (style1.getItalic() != style2.getItalic()) {
116        score += 2;
117    }
118    return score;
119}
120
121MinikinFont* FontFamily::getClosestMatch(FontStyle style) const {
122    const Font* bestFont = NULL;
123    int bestMatch = 0;
124    for (size_t i = 0; i < mFonts.size(); i++) {
125        const Font& font = mFonts[i];
126        int match = computeMatch(font.style, style);
127        if (i == 0 || match < bestMatch) {
128            bestFont = &font;
129            bestMatch = match;
130        }
131    }
132    return bestFont == NULL ? NULL : bestFont->typeface;
133}
134
135size_t FontFamily::getNumFonts() const {
136    return mFonts.size();
137}
138
139MinikinFont* FontFamily::getFont(size_t index) const {
140    return mFonts[index].typeface;
141}
142
143FontStyle FontFamily::getStyle(size_t index) const {
144    return mFonts[index].style;
145}
146
147}  // namespace android
148