FontFamily.cpp 2.8 KB
Newer Older
R
Raph Levien 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "Minikin"

#include <cutils/log.h>
#include <stdlib.h>
#include <stdint.h>
R
Raph Levien 已提交
22
#include <minikin/MinikinFont.h>
R
Raph Levien 已提交
23 24
#include <minikin/AnalyzeStyle.h>
#include <minikin/FontFamily.h>
K
Kenny Root 已提交
25
#include <UniquePtr.h>
R
Raph Levien 已提交
26 27 28 29 30

using std::vector;

namespace android {

R
Raph Levien 已提交
31 32 33 34 35
bool FontFamily::addFont(MinikinFont* typeface) {
    const uint32_t os2Tag = MinikinFont::MakeTag('O', 'S', '/', '2');
    size_t os2Size = 0;
    bool ok = typeface->GetTable(os2Tag, NULL, &os2Size);
    if (!ok) return false;
R
Raph Levien 已提交
36
    UniquePtr<uint8_t[]> os2Data(new uint8_t[os2Size]);
R
Raph Levien 已提交
37 38
    ok = typeface->GetTable(os2Tag, os2Data.get(), &os2Size);
    if (!ok) return false;
R
Raph Levien 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51
    int weight;
    bool italic;
    if (analyzeStyle(os2Data.get(), os2Size, &weight, &italic)) {
        //ALOGD("analyzed weight = %d, italic = %s", weight, italic ? "true" : "false");
        FontStyle style(weight, italic);
        addFont(typeface, style);
        return true;
    } else {
        ALOGD("failed to analyze style");
    }
    return false;
}

R
Raph Levien 已提交
52
void FontFamily::addFont(MinikinFont* typeface, FontStyle style) {
R
Raph Levien 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66
    mFonts.push_back(Font(typeface, style));
    ALOGD("added font, mFonts.size() = %d", mFonts.size());
}

// Compute a matching metric between two styles - 0 is an exact match
int computeMatch(FontStyle style1, FontStyle style2) {
    if (style1 == style2) return 0;
    int score = abs(style1.getWeight() - style2.getWeight());
    if (style1.getItalic() != style2.getItalic()) {
        score += 2;
    }
    return score;
}

R
Raph Levien 已提交
67
MinikinFont* FontFamily::getClosestMatch(FontStyle style) const {
R
Raph Levien 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    const Font* bestFont = NULL;
    int bestMatch = 0;
    for (size_t i = 0; i < mFonts.size(); i++) {
        const Font& font = mFonts[i];
        int match = computeMatch(font.style, style);
        if (i == 0 || match < bestMatch) {
            bestFont = &font;
            bestMatch = match;
        }
    }
    return bestFont == NULL ? NULL : bestFont->typeface;
}

size_t FontFamily::getNumFonts() const {
    return mFonts.size();
}

R
Raph Levien 已提交
85
MinikinFont* FontFamily::getFont(size_t index) const {
R
Raph Levien 已提交
86 87 88 89 90 91 92 93
    return mFonts[index].typeface;
}

FontStyle FontFamily::getStyle(size_t index) const {
    return mFonts[index].style;
}

}  // namespace android