summaryrefslogtreecommitdiff
path: root/tests/unittest/FontFileParserTest.cpp
blob: d960217e4a8ed7913b0fbf64c7bfc29d4caddd6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (C) 2021 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 "minikin/FontFileParser.h"

#include <gtest/gtest.h>

#include "FontTestUtils.h"
#include "FreeTypeMinikinFontForTest.h"
#include "PathUtils.h"

namespace minikin {
namespace {

static size_t writeU16(uint16_t x, uint8_t* out, size_t offset) {
    out[offset] = x >> 8;
    out[offset + 1] = x;
    return offset + 2;
}

static size_t writeU32(uint32_t x, uint8_t* out, size_t offset) {
    out[offset] = x >> 24;
    out[offset + 1] = x >> 16;
    out[offset + 2] = x >> 8;
    out[offset + 3] = x;
    return offset + 4;
}

class TestableFontFileParser : public FontFileParser {
public:
    using FontFileParser::analyzeFontRevision;
    using FontFileParser::checkPSName;
};

// Returns valid head table contents.
static std::vector<uint8_t> buildHeadTable(uint32_t fontRevision) {
    std::vector<uint8_t> out(46);
    size_t head = writeU16(1, out.data(), 0);         // major version
    head = writeU16(0, out.data(), head);             // minor version
    head = writeU32(fontRevision, out.data(), head);  // fontRevision
    head = writeU32(0xB1B0AFBA, out.data(), head);    // checksum. (random value)
    head = writeU32(0x5F0F3CF5, out.data(), head);    // magicNumber
    head = writeU16(0, out.data(), head);             // flasgs
    head = writeU16(1024, out.data(), head);          // unitsPerEm
    head = writeU32(123457890, out.data(), head);     // created (random value)
    head = writeU32(123457890, out.data(), head);     // modified (random value)
    head = writeU16(0, out.data(), head);             // xMin
    head = writeU16(100, out.data(), head);           // yMin
    head = writeU16(1024, out.data(), head);          // xMax
    head = writeU16(2048, out.data(), head);          // yMax
    head = writeU16(0, out.data(), head);             // macStyle
    head = writeU16(10, out.data(), head);            // lowestRecPPEM
    head = writeU16(1, out.data(), head);             // fontDirectionHint
    head = writeU16(1, out.data(), head);             // indexToLocFormat
    head = writeU16(0, out.data(), head);             // glyphDataFormat;

    return out;
}

TEST(FontFileParserTest, analyzeFontRevision) {
    uint32_t rev = 0x12345678;
    std::vector<uint8_t> head = buildHeadTable(rev);

    uint32_t out = 0;
    EXPECT_TRUE(TestableFontFileParser::analyzeFontRevision(head.data(), head.size(), &out));
    EXPECT_EQ(rev, out);
}

TEST(FontFileParserTest, headInvalidLength) {
    uint32_t rev = 0x12345678;
    std::vector<uint8_t> head = buildHeadTable(rev);

    uint32_t out = 0;
    EXPECT_FALSE(TestableFontFileParser::analyzeFontRevision(head.data(), 6, &out));
}

TEST(FontFileParserTest, parseFontForRev) {
    auto minikinFont = std::make_shared<FreeTypeMinikinFontForTest>(getTestFontPath("Ascii.ttf"));
    auto parser = FontFileParser(minikinFont->GetFontData(), minikinFont->GetFontSize(), 0);

    auto revision = parser.getFontRevision();
    EXPECT_TRUE(revision.has_value());
    EXPECT_EQ(0x00010000u, revision.value());
}

TEST(FontFileParser, checkPSName) {
    EXPECT_TRUE(TestableFontFileParser::checkPSName("Roboto-Regular"));
    EXPECT_TRUE(TestableFontFileParser::checkPSName("NotoColorEmoji"));

    // Space character is not allowed.
    EXPECT_FALSE(TestableFontFileParser::checkPSName("Roboto Regular"));
    EXPECT_FALSE(TestableFontFileParser::checkPSName("Noto Color Emoji"));

    // parens are not not allowed.
    EXPECT_FALSE(TestableFontFileParser::checkPSName("Roboto (Regular)"));
    EXPECT_FALSE(TestableFontFileParser::checkPSName("Noto <Color> {Emoji}"));

    // control characters are not allowed
    EXPECT_FALSE(TestableFontFileParser::checkPSName("Roboto-Regular\b"));
    EXPECT_FALSE(TestableFontFileParser::checkPSName("NotoColorEmoji\t"));

    // Up to 63 character is allowed.
    EXPECT_FALSE(TestableFontFileParser::checkPSName(std::string(64, 'a')));

    // Only printable ASCII is allowed.
    EXPECT_FALSE(TestableFontFileParser::checkPSName("ろぼとふぉんと"));
}

TEST(FontFileParserTest, parseFontForPSName) {
    auto minikinFont = std::make_shared<FreeTypeMinikinFontForTest>(getTestFontPath("Ascii.ttf"));
    auto parser = FontFileParser(minikinFont->GetFontData(), minikinFont->GetFontSize(), 0);

    auto psName = parser.getPostScriptName();
    EXPECT_TRUE(psName.has_value());
    EXPECT_EQ("SampleFont-Regular", psName.value());
}

}  // namespace
}  // namespace minikin