FontFamily.cpp revision bcc3dc5a2591a95a57e379e27cbad69c18e91e67
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#include <utils/UniquePtr.h>
23#include <minikin/MinikinFont.h>
24#include <minikin/AnalyzeStyle.h>
25#include <minikin/FontFamily.h>
26
27using std::vector;
28
29namespace android {
30
31bool FontFamily::addFont(MinikinFont* typeface) {
32    const uint32_t os2Tag = MinikinFont::MakeTag('O', 'S', '/', '2');
33    size_t os2Size = 0;
34    bool ok = typeface->GetTable(os2Tag, NULL, &os2Size);
35    if (!ok) return false;
36    UniquePtr<uint8_t[]> os2Data(new uint8_t[os2Size]);
37    ok = typeface->GetTable(os2Tag, os2Data.get(), &os2Size);
38    if (!ok) return false;
39    int weight;
40    bool italic;
41    if (analyzeStyle(os2Data.get(), os2Size, &weight, &italic)) {
42        //ALOGD("analyzed weight = %d, italic = %s", weight, italic ? "true" : "false");
43        FontStyle style(weight, italic);
44        addFont(typeface, style);
45        return true;
46    } else {
47        ALOGD("failed to analyze style");
48    }
49    return false;
50}
51
52void FontFamily::addFont(MinikinFont* typeface, FontStyle style) {
53    mFonts.push_back(Font(typeface, style));
54    ALOGD("added font, mFonts.size() = %d", mFonts.size());
55}
56
57// Compute a matching metric between two styles - 0 is an exact match
58int computeMatch(FontStyle style1, FontStyle style2) {
59    if (style1 == style2) return 0;
60    int score = abs(style1.getWeight() - style2.getWeight());
61    if (style1.getItalic() != style2.getItalic()) {
62        score += 2;
63    }
64    return score;
65}
66
67MinikinFont* FontFamily::getClosestMatch(FontStyle style) const {
68    const Font* bestFont = NULL;
69    int bestMatch = 0;
70    for (size_t i = 0; i < mFonts.size(); i++) {
71        const Font& font = mFonts[i];
72        int match = computeMatch(font.style, style);
73        if (i == 0 || match < bestMatch) {
74            bestFont = &font;
75            bestMatch = match;
76        }
77    }
78    return bestFont == NULL ? NULL : bestFont->typeface;
79}
80
81size_t FontFamily::getNumFonts() const {
82    return mFonts.size();
83}
84
85MinikinFont* FontFamily::getFont(size_t index) const {
86    return mFonts[index].typeface;
87}
88
89FontStyle FontFamily::getStyle(size_t index) const {
90    return mFonts[index].style;
91}
92
93}  // namespace android
94