MinikinFontForTest.cpp revision 1c7b0261542f8a54e7b3e52236070f62d073e17f
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#define LOG_TAG "Minikin"
18
19#include "MinikinFontForTest.h"
20
21#include <minikin/MinikinFont.h>
22
23#include <fcntl.h>
24#include <sys/mman.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <unistd.h>
28
29#include <string>
30
31#include <log/log.h>
32
33namespace minikin {
34
35static int uniqueId = 0;  // TODO: make thread safe if necessary.
36
37MinikinFontForTest::MinikinFontForTest(const std::string& font_path, int index) :
38        MinikinFont(uniqueId++),
39        mFontPath(font_path),
40        mFontIndex(index) {
41    int fd = open(font_path.c_str(), O_RDONLY);
42    LOG_ALWAYS_FATAL_IF(fd == -1);
43    struct stat st = {};
44    LOG_ALWAYS_FATAL_IF(fstat(fd, &st) != 0);
45    mFontSize = st.st_size;
46    mFontData = mmap(NULL, mFontSize, PROT_READ, MAP_SHARED, fd, 0);
47    LOG_ALWAYS_FATAL_IF(mFontData == nullptr);
48    close(fd);
49}
50
51MinikinFontForTest::~MinikinFontForTest() {
52    munmap(mFontData, mFontSize);
53}
54
55float MinikinFontForTest::GetHorizontalAdvance(uint32_t /* glyph_id */,
56        const MinikinPaint& /* paint */) const {
57    // TODO: Make mock value configurable if necessary.
58    return 10.0f;
59}
60
61void MinikinFontForTest::GetBounds(MinikinRect* bounds, uint32_t /* glyph_id */,
62        const MinikinPaint& /* paint */) const {
63    // TODO: Make mock values configurable if necessary.
64    bounds->mLeft = 0.0f;
65    bounds->mTop = 0.0f;
66    bounds->mRight = 10.0f;
67    bounds->mBottom = 10.0f;
68}
69
70}  // namespace minikin
71