FontFamily.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 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
33FontFamily::~FontFamily() {
34    for (size_t i = 0; i < mFonts.size(); i++) {
35        mFonts[i].typeface->UnrefLocked();
36    }
37}
38
39bool FontFamily::addFont(MinikinFont* typeface) {
40    AutoMutex _l(gMinikinLock);
41    const uint32_t os2Tag = MinikinFont::MakeTag('O', 'S', '/', '2');
42    size_t os2Size = 0;
43    bool ok = typeface->GetTable(os2Tag, NULL, &os2Size);
44    if (!ok) return false;
45    UniquePtr<uint8_t[]> os2Data(new uint8_t[os2Size]);
46    ok = typeface->GetTable(os2Tag, os2Data.get(), &os2Size);
47    if (!ok) return false;
48    int weight;
49    bool italic;
50    if (analyzeStyle(os2Data.get(), os2Size, &weight, &italic)) {
51        //ALOGD("analyzed weight = %d, italic = %s", weight, italic ? "true" : "false");
52        FontStyle style(weight, italic);
53        addFontLocked(typeface, style);
54        return true;
55    } else {
56        ALOGD("failed to analyze style");
57    }
58    return false;
59}
60
61void FontFamily::addFont(MinikinFont* typeface, FontStyle style) {
62    AutoMutex _l(gMinikinLock);
63    addFontLocked(typeface, style);
64}
65
66void FontFamily::addFontLocked(MinikinFont* typeface, FontStyle style) {    typeface->RefLocked();
67    mFonts.push_back(Font(typeface, style));
68}
69
70// Compute a matching metric between two styles - 0 is an exact match
71int computeMatch(FontStyle style1, FontStyle style2) {
72    if (style1 == style2) return 0;
73    int score = abs(style1.getWeight() - style2.getWeight());
74    if (style1.getItalic() != style2.getItalic()) {
75        score += 2;
76    }
77    return score;
78}
79
80MinikinFont* FontFamily::getClosestMatch(FontStyle style) const {
81    const Font* bestFont = NULL;
82    int bestMatch = 0;
83    for (size_t i = 0; i < mFonts.size(); i++) {
84        const Font& font = mFonts[i];
85        int match = computeMatch(font.style, style);
86        if (i == 0 || match < bestMatch) {
87            bestFont = &font;
88            bestMatch = match;
89        }
90    }
91    return bestFont == NULL ? NULL : bestFont->typeface;
92}
93
94size_t FontFamily::getNumFonts() const {
95    return mFonts.size();
96}
97
98MinikinFont* FontFamily::getFont(size_t index) const {
99    return mFonts[index].typeface;
100}
101
102FontStyle FontFamily::getStyle(size_t index) const {
103    return mFonts[index].style;
104}
105
106}  // namespace android
107