FontFamily.cpp revision ecc2d34ac23a497988f21e5f415b53c007b9d8c5
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 <minikin/MinikinFont.h>
23#include <minikin/AnalyzeStyle.h>
24#include <minikin/FontFamily.h>
25#include <UniquePtr.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}
55
56// Compute a matching metric between two styles - 0 is an exact match
57int computeMatch(FontStyle style1, FontStyle style2) {
58    if (style1 == style2) return 0;
59    int score = abs(style1.getWeight() - style2.getWeight());
60    if (style1.getItalic() != style2.getItalic()) {
61        score += 2;
62    }
63    return score;
64}
65
66MinikinFont* FontFamily::getClosestMatch(FontStyle style) const {
67    const Font* bestFont = NULL;
68    int bestMatch = 0;
69    for (size_t i = 0; i < mFonts.size(); i++) {
70        const Font& font = mFonts[i];
71        int match = computeMatch(font.style, style);
72        if (i == 0 || match < bestMatch) {
73            bestFont = &font;
74            bestMatch = match;
75        }
76    }
77    return bestFont == NULL ? NULL : bestFont->typeface;
78}
79
80size_t FontFamily::getNumFonts() const {
81    return mFonts.size();
82}
83
84MinikinFont* FontFamily::getFont(size_t index) const {
85    return mFonts[index].typeface;
86}
87
88FontStyle FontFamily::getStyle(size_t index) const {
89    return mFonts[index].style;
90}
91
92}  // namespace android
93