MinikinFontForTest.cpp 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (C) 2015 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.
 */

#include "MinikinFontForTest.h"

#include <minikin/MinikinFont.h>

#include <SkTypeface.h>

#include <cutils/log.h>

S
Seigo Nonaka 已提交
25 26
namespace minikin {

S
Seigo Nonaka 已提交
27 28
// static
MinikinFontForTest* MinikinFontForTest::createFromFile(const std::string& font_path) {
29 30
    sk_sp<SkTypeface> typeface = SkTypeface::MakeFromFile(font_path.c_str());
    MinikinFontForTest* font = new MinikinFontForTest(font_path, std::move(typeface));
S
Seigo Nonaka 已提交
31
    return font;
S
Seigo Nonaka 已提交
32 33
}

S
Seigo Nonaka 已提交
34 35 36
// static
MinikinFontForTest* MinikinFontForTest::createFromFileWithIndex(const std::string& font_path,
        int index) {
37 38
    sk_sp<SkTypeface> typeface = SkTypeface::MakeFromFile(font_path.c_str(), index);
    MinikinFontForTest* font = new MinikinFontForTest(font_path, std::move(typeface));
S
Seigo Nonaka 已提交
39 40 41
    return font;
}

42
MinikinFontForTest::MinikinFontForTest(const std::string& font_path, sk_sp<SkTypeface> typeface) :
S
Seigo Nonaka 已提交
43
        MinikinFont(typeface->uniqueID()),
44
        mTypeface(std::move(typeface)),
S
Seigo Nonaka 已提交
45
        mFontPath(font_path) {
46 47
}

48
float MinikinFontForTest::GetHorizontalAdvance(uint32_t /* glyph_id */,
S
Seigo Nonaka 已提交
49
        const MinikinPaint& /* paint */) const {
50 51 52 53
    LOG_ALWAYS_FATAL("MinikinFontForTest::GetHorizontalAdvance is not yet implemented");
    return 0.0f;
}

S
Seigo Nonaka 已提交
54 55
void MinikinFontForTest::GetBounds(MinikinRect* /* bounds */, uint32_t /* glyph_id */,
        const MinikinPaint& /* paint */) const {
56 57 58
    LOG_ALWAYS_FATAL("MinikinFontForTest::GetBounds is not yet implemented");
}

R
Raph Levien 已提交
59
const void* MinikinFontForTest::GetTable(uint32_t tag, size_t* size,
S
Seigo Nonaka 已提交
60
        MinikinDestroyFunc* destroy) {
R
Raph Levien 已提交
61 62 63 64
    const size_t tableSize = mTypeface->getTableSize(tag);
    *size = tableSize;
    if (tableSize == 0) {
        return nullptr;
65
    }
R
Raph Levien 已提交
66 67 68 69 70 71 72
    void* buf = malloc(tableSize);
    if (buf == nullptr) {
        return nullptr;
    }
    mTypeface->getTableData(tag, 0, tableSize, buf);
    *destroy = free;
    return buf;
73
}
S
Seigo Nonaka 已提交
74 75

}  // namespace minikin