-
Notifications
You must be signed in to change notification settings - Fork 71
/
outline.cc
372 lines (318 loc) · 9.08 KB
/
outline.cc
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <fstream>
#include <streambuf>
#include <benchmark/benchmark.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#include FT_MULTIPLE_MASTERS_H
#define TTFP_VARIABLE_FONTS
#include <ttfparser.h>
#define STB_TRUETYPE_IMPLEMENTATION
#include "stb_truetype.h"
namespace FT {
struct Outliner
{
static int moveToFn(const FT_Vector *, void *user)
{
auto self = static_cast<Outliner *>(user);
self->counter += 1;
return 0;
}
static int lineToFn(const FT_Vector *, void *user)
{
auto self = static_cast<Outliner *>(user);
self->counter += 1;
return 0;
}
static int quadToFn(const FT_Vector *, const FT_Vector *, void *user)
{
auto self = static_cast<Outliner *>(user);
self->counter += 1;
return 0;
}
static int cubicToFn(const FT_Vector *, const FT_Vector *, const FT_Vector *, void *user)
{
auto self = static_cast<Outliner *>(user);
self->counter += 1;
return 0;
}
uint32_t counter = 0;
};
class Font
{
public:
Font(const std::string &path, const uint32_t index = 0)
{
if (FT_Init_FreeType(&m_library)) {
throw "failed to init FreeType";
}
std::ifstream s(path);
std::vector<char> data((std::istreambuf_iterator<char>(s)),
std::istreambuf_iterator<char>());
m_fontData = std::move(data);
if (FT_New_Memory_Face(m_library, (FT_Byte*)m_fontData.data(), m_fontData.size(), index, &m_face)) {
throw "failed to open a font";
}
}
~Font()
{
if (m_face) {
FT_Done_Face(m_face);
}
FT_Done_FreeType(m_library);
}
uint16_t numberOfGlyphs() const
{
return (uint16_t)m_face->num_glyphs;
}
void setVariations()
{
FT_Fixed coords[1] = {500 * 65536L};
if (FT_Set_Var_Design_Coordinates(m_face, 1, coords)) {
throw "failed to set variations";
}
}
uint32_t outline(const uint16_t gid) const
{
if (FT_Load_Glyph(m_face, gid, FT_LOAD_NO_SCALE)) {
throw "failed to load a glyph";
}
Outliner outliner;
FT_Outline_Funcs funcs;
funcs.move_to = outliner.moveToFn;
funcs.line_to = outliner.lineToFn;
funcs.conic_to = outliner.quadToFn;
funcs.cubic_to = outliner.cubicToFn;
funcs.shift = 0;
funcs.delta = 0;
if (FT_Outline_Decompose(&m_face->glyph->outline, &funcs, &outliner)) {
throw "failed to outline a glyph";
}
return outliner.counter;
}
private:
std::vector<char> m_fontData;
FT_Library m_library = nullptr;
FT_Face m_face = nullptr;
};
}
namespace STB {
class Font
{
public:
Font(const std::string &path, const uint32_t index = 0)
{
std::ifstream s(path);
std::vector<char> data((std::istreambuf_iterator<char>(s)),
std::istreambuf_iterator<char>());
m_fontData = std::move(data);
if (!stbtt_InitFont(&m_font, (const uint8_t *)m_fontData.data(), 0)) {
throw "failed to open a font";
}
}
uint16_t numberOfGlyphs() const
{
return (uint16_t)m_font.numGlyphs;
}
uint32_t outline(const uint16_t gid) const
{
stbtt_vertex *vertices;
const auto num_verts = stbtt_GetGlyphShape(&m_font, gid, &vertices);
stbtt_FreeShape(&m_font, vertices);
return num_verts;
}
private:
std::vector<char> m_fontData;
stbtt_fontinfo m_font;
};
}
namespace TTFP {
struct Outliner
{
static void moveToFn(float x, float y, void *user)
{
auto self = static_cast<Outliner *>(user);
self->counter += 1;
}
static void lineToFn(float x, float y, void *user)
{
auto self = static_cast<Outliner *>(user);
self->counter += 1;
}
static void quadToFn(float x1, float y1, float x, float y, void *user)
{
auto self = static_cast<Outliner *>(user);
self->counter += 1;
}
static void curveToFn(float x1, float y1, float x2, float y2, float x, float y, void *user)
{
auto self = static_cast<Outliner *>(user);
self->counter += 1;
}
static void closePathFn(void *user)
{
auto self = static_cast<Outliner *>(user);
self->counter += 1;
}
uint32_t counter = 0;
};
class Font
{
public:
Font(const std::string &path, const uint32_t index = 0)
{
std::ifstream s(path);
std::vector<char> data((std::istreambuf_iterator<char>(s)),
std::istreambuf_iterator<char>());
m_fontData = std::move(data);
m_face = (ttfp_face*)malloc(ttfp_face_size_of());
if (!ttfp_face_init(m_fontData.data(), m_fontData.size(), index, m_face)) {
free(m_face);
throw "failed to parse a font";
}
}
~Font()
{
if (m_face) {
free(m_face);
}
}
uint16_t numberOfGlyphs() const
{
return ttfp_get_number_of_glyphs(m_face);
}
void setVariations()
{
if (!ttfp_set_variation(m_face, TTFP_TAG('w', 'g', 'h', 't'), 500)) {
throw "failed to set variations";
}
}
uint32_t outline(const uint16_t gid) const
{
Outliner outliner;
ttfp_outline_builder builder;
builder.move_to = outliner.moveToFn;
builder.line_to = outliner.lineToFn;
builder.quad_to = outliner.quadToFn;
builder.curve_to = outliner.curveToFn;
builder.close_path = outliner.closePathFn;
ttfp_rect bbox;
ttfp_outline_glyph(m_face, builder, &outliner, gid, &bbox);
return outliner.counter;
}
private:
std::vector<char> m_fontData;
ttfp_face *m_face = nullptr;
};
}
static void freetype_outline_glyf(benchmark::State &state)
{
FT::Font font("../fonts/SourceSansPro-Regular.ttf", 0);
for (auto _ : state) {
for (uint i = 0; i < font.numberOfGlyphs(); i++) {
font.outline(i);
}
}
}
BENCHMARK(freetype_outline_glyf);
static void freetype_outline_gvar(benchmark::State &state)
{
FT::Font font("../fonts/SourceSansVariable-Roman.ttf", 0);
font.setVariations();
for (auto _ : state) {
for (uint i = 0; i < font.numberOfGlyphs(); i++) {
font.outline(i);
}
}
}
BENCHMARK(freetype_outline_gvar);
static void freetype_outline_cff(benchmark::State &state)
{
FT::Font font("../fonts/SourceSansPro-Regular.otf", 0);
for (auto _ : state) {
for (uint i = 0; i < font.numberOfGlyphs(); i++) {
font.outline(i);
}
}
}
BENCHMARK(freetype_outline_cff);
static void freetype_outline_cff2(benchmark::State &state)
{
FT::Font font("../fonts/SourceSansVariable-Roman.otf", 0);
font.setVariations();
for (auto _ : state) {
for (uint i = 0; i < font.numberOfGlyphs(); i++) {
font.outline(i);
}
}
}
BENCHMARK(freetype_outline_cff2);
static void stb_truetype_outline_glyf(benchmark::State &state)
{
STB::Font font("../fonts/SourceSansPro-Regular.ttf", 0);
const auto numberOfGlyphs = font.numberOfGlyphs();
for (auto _ : state) {
for (uint i = 0; i < numberOfGlyphs; i++) {
font.outline(i);
}
}
}
BENCHMARK(stb_truetype_outline_glyf);
static void stb_truetype_outline_cff(benchmark::State &state)
{
STB::Font font("../fonts/SourceSansPro-Regular.otf", 0);
const auto numberOfGlyphs = font.numberOfGlyphs();
for (auto _ : state) {
for (uint i = 0; i < numberOfGlyphs; i++) {
font.outline(i);
}
}
}
BENCHMARK(stb_truetype_outline_cff);
static void ttf_parser_outline_glyf(benchmark::State &state)
{
TTFP::Font font("../fonts/SourceSansPro-Regular.ttf", 0);
const auto numberOfGlyphs = font.numberOfGlyphs();
for (auto _ : state) {
for (uint i = 0; i < numberOfGlyphs; i++) {
font.outline(i);
}
}
}
BENCHMARK(ttf_parser_outline_glyf);
static void ttf_parser_outline_gvar(benchmark::State &state)
{
TTFP::Font font("../fonts/SourceSansVariable-Roman.ttf", 0);
font.setVariations();
const auto numberOfGlyphs = font.numberOfGlyphs();
for (auto _ : state) {
for (uint i = 0; i < numberOfGlyphs; i++) {
font.outline(i);
}
}
}
BENCHMARK(ttf_parser_outline_gvar);
static void ttf_parser_outline_cff(benchmark::State &state)
{
TTFP::Font font("../fonts/SourceSansPro-Regular.otf", 0);
const auto numberOfGlyphs = font.numberOfGlyphs();
for (auto _ : state) {
for (uint i = 0; i < numberOfGlyphs; i++) {
font.outline(i);
}
}
}
BENCHMARK(ttf_parser_outline_cff);
static void ttf_parser_outline_cff2(benchmark::State &state)
{
TTFP::Font font("../fonts/SourceSansVariable-Roman.otf", 0);
font.setVariations();
const auto numberOfGlyphs = font.numberOfGlyphs();
for (auto _ : state) {
for (uint i = 0; i < numberOfGlyphs; i++) {
font.outline(i);
}
}
}
BENCHMARK(ttf_parser_outline_cff2);
BENCHMARK_MAIN();