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
33 namespace minikin {
34
35 static int uniqueId = 0; // TODO: make thread safe if necessary.
36
MinikinFontForTest(const std::string & font_path,int index,const std::vector<FontVariation> & variations)37 MinikinFontForTest::MinikinFontForTest(const std::string& font_path, int index,
38 const std::vector<FontVariation>& variations) :
39 MinikinFont(uniqueId++),
40 mFontPath(font_path),
41 mVariations(variations),
42 mFontIndex(index) {
43 int fd = open(font_path.c_str(), O_RDONLY);
44 LOG_ALWAYS_FATAL_IF(fd == -1);
45 struct stat st = {};
46 LOG_ALWAYS_FATAL_IF(fstat(fd, &st) != 0);
47 mFontSize = st.st_size;
48 mFontData = mmap(NULL, mFontSize, PROT_READ, MAP_SHARED, fd, 0);
49 LOG_ALWAYS_FATAL_IF(mFontData == nullptr);
50 close(fd);
51 }
52
~MinikinFontForTest()53 MinikinFontForTest::~MinikinFontForTest() {
54 munmap(mFontData, mFontSize);
55 }
56
GetHorizontalAdvance(uint32_t,const MinikinPaint &) const57 float MinikinFontForTest::GetHorizontalAdvance(uint32_t /* glyph_id */,
58 const MinikinPaint& /* paint */) const {
59 // TODO: Make mock value configurable if necessary.
60 return 10.0f;
61 }
62
GetBounds(MinikinRect * bounds,uint32_t,const MinikinPaint &) const63 void MinikinFontForTest::GetBounds(MinikinRect* bounds, uint32_t /* glyph_id */,
64 const MinikinPaint& /* paint */) const {
65 // TODO: Make mock values configurable if necessary.
66 bounds->mLeft = 0.0f;
67 bounds->mTop = 0.0f;
68 bounds->mRight = 10.0f;
69 bounds->mBottom = 10.0f;
70 }
71
createFontWithVariation(const std::vector<FontVariation> & variations) const72 std::shared_ptr<MinikinFont> MinikinFontForTest::createFontWithVariation(
73 const std::vector<FontVariation>& variations) const {
74 return std::shared_ptr<MinikinFont>(new MinikinFontForTest(mFontPath, mFontIndex, variations));
75 }
76
77 } // namespace minikin
78