MinikinFontForTest.cpp revision 81c79d6e1e49562c9bc33c14826017dd5e33ebec
1/*
2 * Copyright (C) 2015 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#include "MinikinFontForTest.h"
18
19#include <minikin/MinikinFont.h>
20
21#include <SkTypeface.h>
22
23#include <cutils/log.h>
24
25namespace minikin {
26
27// static
28MinikinFontForTest* MinikinFontForTest::createFromFile(const std::string& font_path) {
29    SkTypeface* typeface = SkTypeface::CreateFromFile(font_path.c_str());
30    MinikinFontForTest* font = new MinikinFontForTest(font_path, typeface);
31    SkSafeUnref(typeface);
32    return font;
33}
34
35MinikinFontForTest::MinikinFontForTest(const std::string& font_path, SkTypeface* typeface) :
36        MinikinFont(typeface->uniqueID()),
37        mTypeface(typeface),
38        mFontPath(font_path) {
39    SkSafeRef(mTypeface);
40}
41
42MinikinFontForTest::~MinikinFontForTest() {
43    SkSafeUnref(mTypeface);
44}
45
46float MinikinFontForTest::GetHorizontalAdvance(uint32_t /* glyph_id */,
47        const MinikinPaint& /* paint */) const {
48    LOG_ALWAYS_FATAL("MinikinFontForTest::GetHorizontalAdvance is not yet implemented");
49    return 0.0f;
50}
51
52void MinikinFontForTest::GetBounds(MinikinRect* /* bounds */, uint32_t /* glyph_id */,
53        const MinikinPaint& /* paint */) const {
54    LOG_ALWAYS_FATAL("MinikinFontForTest::GetBounds is not yet implemented");
55}
56
57const void* MinikinFontForTest::GetTable(uint32_t tag, size_t* size,
58        MinikinDestroyFunc* destroy) {
59    const size_t tableSize = mTypeface->getTableSize(tag);
60    *size = tableSize;
61    if (tableSize == 0) {
62        return nullptr;
63    }
64    void* buf = malloc(tableSize);
65    if (buf == nullptr) {
66        return nullptr;
67    }
68    mTypeface->getTableData(tag, 0, tableSize, buf);
69    *destroy = free;
70    return buf;
71}
72
73}  // namespace minikin
74