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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/*
Copyright (C) 2026 Carl Hetherington <cth@carlh.net>
This file is part of libttf.
libttf is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
libttf is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libttf. If not, see <http://www.gnu.org/licenses/>.
*/
#define BOOST_TEST_MODULE libttf_test
#include "collection.h"
#include "font.h"
#include "name_table.h"
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/test/unit_test.hpp>
#include <fstream>
#include <vector>
void
check_file(boost::filesystem::path ref, boost::filesystem::path check)
{
uintmax_t size = boost::filesystem::file_size(ref);
BOOST_CHECK_EQUAL(size, boost::filesystem::file_size(check));
boost::filesystem::ifstream ref_file(ref, std::ios::binary);
BOOST_REQUIRE(ref_file.good());
boost::filesystem::ifstream check_file(check, std::ios::binary);
BOOST_REQUIRE(check_file.good());
int constexpr buffer_size = 65536;
std::vector<uint8_t> ref_buffer(buffer_size);
std::vector<uint8_t> check_buffer(buffer_size);
uintmax_t pos = 0;
while (pos < size) {
uintmax_t this_time = std::min(uintmax_t(buffer_size), size - pos);
ref_file.read(reinterpret_cast<char*>(ref_buffer.data()), this_time);
BOOST_CHECK(ref_file);
check_file.read(reinterpret_cast<char*>(check_buffer.data()), this_time);
BOOST_CHECK(check_file);
if (memcmp(ref_buffer.data(), check_buffer.data(), this_time) != 0) {
for (int i = 0; i < buffer_size; ++i) {
if (ref_buffer[i] != check_buffer[i]) {
BOOST_CHECK_MESSAGE (
false,
"File " << check << " differs from reference " << ref << " at offset " << (pos + i)
);
break;
}
}
break;
}
pos += this_time;
}
}
BOOST_AUTO_TEST_CASE(passthrough_test)
{
ttf::Font font("test/data/LiberationSans-Regular.ttf");
font.write("build/test/passthrough_test.ttf");
check_file("test/data/LiberationSans-Regular.ttf", "build/test/passthrough_test.ttf");
}
BOOST_AUTO_TEST_CASE(get_names_test)
{
ttf::Font font("test/data/LiberationSans-Regular.ttf");
auto name_table = font.name_table();
BOOST_REQUIRE(name_table);
auto font_family = name_table->names(ttf::NameTable::Identifier::FONT_FAMILY);
BOOST_REQUIRE_EQUAL(font_family.size(), 2U);
BOOST_CHECK_EQUAL(boost::variant2::get<0>(font_family[1]), "Liberation Sans");
auto full_name = name_table->names(ttf::NameTable::Identifier::FULL_NAME);
BOOST_REQUIRE_EQUAL(full_name.size(), 2U);
BOOST_CHECK_EQUAL(boost::variant2::get<0>(full_name[1]), "Liberation Sans");
auto postscript_name = name_table->names(ttf::NameTable::Identifier::POSTSCRIPT_NAME);
BOOST_REQUIRE_EQUAL(postscript_name.size(), 2U);
BOOST_CHECK_EQUAL(boost::variant2::get<0>(postscript_name[1]), "LiberationSans");
}
BOOST_AUTO_TEST_CASE(set_names_test)
{
ttf::Font font("test/data/LiberationSans-Regular.ttf");
auto name_table = font.name_table();
BOOST_REQUIRE(name_table);
name_table->set_names(ttf::NameTable::Identifier::FONT_FAMILY, "Adams", L"Adams");
name_table->set_names(ttf::NameTable::Identifier::FULL_NAME, "Frobozz", L"Frobozz");
name_table->set_names(ttf::NameTable::Identifier::POSTSCRIPT_NAME, "Pat", L"Pat");
font.write("build/test/set_names_test.ttf");
ttf::Font loaded("build/test/set_names_test.ttf");
auto loaded_name_table = loaded.name_table();
auto font_family = loaded_name_table->names(ttf::NameTable::Identifier::FONT_FAMILY);
BOOST_CHECK_EQUAL(font_family.size(), 2U);
BOOST_CHECK_EQUAL(boost::variant2::get<0>(font_family[1]), "Adams");
BOOST_CHECK(boost::variant2::get<1>(font_family[0]) == L"Adams");
auto full_name = loaded_name_table->names(ttf::NameTable::Identifier::FULL_NAME);
BOOST_CHECK_EQUAL(full_name.size(), 2U);
BOOST_CHECK_EQUAL(boost::variant2::get<0>(full_name[1]), "Frobozz");
BOOST_CHECK(boost::variant2::get<1>(full_name[0]) == L"Frobozz");
auto postscript_name = loaded_name_table->names(ttf::NameTable::Identifier::POSTSCRIPT_NAME);
BOOST_CHECK_EQUAL(postscript_name.size(), 2U);
BOOST_CHECK_EQUAL(boost::variant2::get<0>(postscript_name[1]), "Pat");
BOOST_CHECK(boost::variant2::get<1>(postscript_name[0]) == L"Pat");
}
BOOST_AUTO_TEST_CASE(set_names_test2)
{
ttf::Font font("test/data/539c-fffe-768a-d2c7");
auto name_table = font.name_table();
BOOST_REQUIRE(name_table);
name_table->set_names(ttf::NameTable::Identifier::FONT_FAMILY, "Arial_0", L"Arial_0");
auto font_family = name_table->names(ttf::NameTable::Identifier::FONT_FAMILY);
BOOST_REQUIRE_EQUAL(font_family.size(), 3U);
BOOST_CHECK(boost::variant2::get<1>(font_family[0]) == L"Arial_0");
BOOST_CHECK(boost::variant2::get<1>(font_family[1]) == L"Arial_0");
BOOST_CHECK_EQUAL(boost::variant2::get<0>(font_family[2]), "Arial_0");
}
BOOST_AUTO_TEST_CASE(load_ttc_file)
{
BOOST_CHECK_THROW(ttf::Font("test/data/8fb0935f-28cd-4d8f-9ee9-85897c600364"), ttf::InvalidScalerType);
ttf::Collection collection("test/data/8fb0935f-28cd-4d8f-9ee9-85897c600364");
auto fonts = collection.fonts();
BOOST_CHECK_EQUAL(fonts.size(), 9U);
BOOST_REQUIRE(fonts[0].name_table());
auto const& table = fonts[0].name_table();
auto const font_family = table->names(ttf::NameTable::Identifier::FONT_FAMILY);
BOOST_REQUIRE_EQUAL(font_family.size(), 2U);
BOOST_CHECK_EQUAL(boost::variant2::get<0>(font_family[1]), "Arial Hebrew");
}
|