-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
import-font.cpp
315 lines (274 loc) · 11.9 KB
/
import-font.cpp
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
#include "import-font.h"
#include <cstring>
#include <vector>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#ifndef MSDFGEN_DISABLE_VARIABLE_FONTS
#include FT_MULTIPLE_MASTERS_H
#endif
namespace msdfgen {
#define F16DOT16_TO_DOUBLE(x) (1/65536.*double(x))
#define DOUBLE_TO_F16DOT16(x) FT_Fixed(65536.*x)
class FreetypeHandle {
friend FreetypeHandle *initializeFreetype();
friend void deinitializeFreetype(FreetypeHandle *library);
friend FontHandle *loadFont(FreetypeHandle *library, const char *filename);
friend FontHandle *loadFontData(FreetypeHandle *library, const byte *data, int length);
#ifndef MSDFGEN_DISABLE_VARIABLE_FONTS
friend bool setFontVariationAxis(FreetypeHandle *library, FontHandle *font, const char *name, double coordinate);
friend bool listFontVariationAxes(std::vector<FontVariationAxis> &axes, FreetypeHandle *library, FontHandle *font);
#endif
FT_Library library;
};
class FontHandle {
friend FontHandle *adoptFreetypeFont(FT_Face ftFace);
friend FontHandle *loadFont(FreetypeHandle *library, const char *filename);
friend FontHandle *loadFontData(FreetypeHandle *library, const byte *data, int length);
friend void destroyFont(FontHandle *font);
friend bool getFontMetrics(FontMetrics &metrics, FontHandle *font, FontCoordinateScaling coordinateScaling);
friend bool getFontWhitespaceWidth(double &spaceAdvance, double &tabAdvance, FontHandle *font, FontCoordinateScaling coordinateScaling);
friend bool getGlyphCount(unsigned &output, FontHandle *font);
friend bool getGlyphIndex(GlyphIndex &glyphIndex, FontHandle *font, unicode_t unicode);
friend bool loadGlyph(Shape &output, FontHandle *font, GlyphIndex glyphIndex, FontCoordinateScaling coordinateScaling, double *outAdvance);
friend bool loadGlyph(Shape &output, FontHandle *font, unicode_t unicode, FontCoordinateScaling coordinateScaling, double *outAdvance);
friend bool getKerning(double &output, FontHandle *font, GlyphIndex glyphIndex0, GlyphIndex glyphIndex1, FontCoordinateScaling coordinateScaling);
friend bool getKerning(double &output, FontHandle *font, unicode_t unicode0, unicode_t unicode1, FontCoordinateScaling coordinateScaling);
#ifndef MSDFGEN_DISABLE_VARIABLE_FONTS
friend bool setFontVariationAxis(FreetypeHandle *library, FontHandle *font, const char *name, double coordinate);
friend bool listFontVariationAxes(std::vector<FontVariationAxis> &axes, FreetypeHandle *library, FontHandle *font);
#endif
FT_Face face;
bool ownership;
};
struct FtContext {
double scale;
Point2 position;
Shape *shape;
Contour *contour;
};
static Point2 ftPoint2(const FT_Vector &vector, double scale) {
return Point2(scale*vector.x, scale*vector.y);
}
static int ftMoveTo(const FT_Vector *to, void *user) {
FtContext *context = reinterpret_cast<FtContext *>(user);
if (!(context->contour && context->contour->edges.empty()))
context->contour = &context->shape->addContour();
context->position = ftPoint2(*to, context->scale);
return 0;
}
static int ftLineTo(const FT_Vector *to, void *user) {
FtContext *context = reinterpret_cast<FtContext *>(user);
Point2 endpoint = ftPoint2(*to, context->scale);
if (endpoint != context->position) {
context->contour->addEdge(EdgeHolder(context->position, endpoint));
context->position = endpoint;
}
return 0;
}
static int ftConicTo(const FT_Vector *control, const FT_Vector *to, void *user) {
FtContext *context = reinterpret_cast<FtContext *>(user);
Point2 endpoint = ftPoint2(*to, context->scale);
if (endpoint != context->position) {
context->contour->addEdge(EdgeHolder(context->position, ftPoint2(*control, context->scale), endpoint));
context->position = endpoint;
}
return 0;
}
static int ftCubicTo(const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *user) {
FtContext *context = reinterpret_cast<FtContext *>(user);
Point2 endpoint = ftPoint2(*to, context->scale);
if (endpoint != context->position || crossProduct(ftPoint2(*control1, context->scale)-endpoint, ftPoint2(*control2, context->scale)-endpoint)) {
context->contour->addEdge(EdgeHolder(context->position, ftPoint2(*control1, context->scale), ftPoint2(*control2, context->scale), endpoint));
context->position = endpoint;
}
return 0;
}
static double getFontCoordinateScale(const FT_Face &face, FontCoordinateScaling coordinateScaling) {
switch (coordinateScaling) {
case FONT_SCALING_NONE:
return 1;
case FONT_SCALING_EM_NORMALIZED:
return 1./(face->units_per_EM ? face->units_per_EM : 1);
case FONT_SCALING_LEGACY:
return MSDFGEN_LEGACY_FONT_COORDINATE_SCALE;
}
return 1;
}
GlyphIndex::GlyphIndex(unsigned index) : index(index) { }
unsigned GlyphIndex::getIndex() const {
return index;
}
FreetypeHandle *initializeFreetype() {
FreetypeHandle *handle = new FreetypeHandle;
FT_Error error = FT_Init_FreeType(&handle->library);
if (error) {
delete handle;
return NULL;
}
return handle;
}
void deinitializeFreetype(FreetypeHandle *library) {
FT_Done_FreeType(library->library);
delete library;
}
FontHandle *adoptFreetypeFont(FT_Face ftFace) {
FontHandle *handle = new FontHandle;
handle->face = ftFace;
handle->ownership = false;
return handle;
}
FT_Error readFreetypeOutline(Shape &output, FT_Outline *outline, double scale) {
output.contours.clear();
output.inverseYAxis = false;
FtContext context = { };
context.scale = scale;
context.shape = &output;
FT_Outline_Funcs ftFunctions;
ftFunctions.move_to = &ftMoveTo;
ftFunctions.line_to = &ftLineTo;
ftFunctions.conic_to = &ftConicTo;
ftFunctions.cubic_to = &ftCubicTo;
ftFunctions.shift = 0;
ftFunctions.delta = 0;
FT_Error error = FT_Outline_Decompose(outline, &ftFunctions, &context);
if (!output.contours.empty() && output.contours.back().edges.empty())
output.contours.pop_back();
return error;
}
FontHandle *loadFont(FreetypeHandle *library, const char *filename) {
if (!library)
return NULL;
FontHandle *handle = new FontHandle;
FT_Error error = FT_New_Face(library->library, filename, 0, &handle->face);
if (error) {
delete handle;
return NULL;
}
handle->ownership = true;
return handle;
}
FontHandle *loadFontData(FreetypeHandle *library, const byte *data, int length) {
if (!library)
return NULL;
FontHandle *handle = new FontHandle;
FT_Error error = FT_New_Memory_Face(library->library, data, length, 0, &handle->face);
if (error) {
delete handle;
return NULL;
}
handle->ownership = true;
return handle;
}
void destroyFont(FontHandle *font) {
if (font->ownership)
FT_Done_Face(font->face);
delete font;
}
bool getFontMetrics(FontMetrics &metrics, FontHandle *font, FontCoordinateScaling coordinateScaling) {
double scale = getFontCoordinateScale(font->face, coordinateScaling);
metrics.emSize = scale*font->face->units_per_EM;
metrics.ascenderY = scale*font->face->ascender;
metrics.descenderY = scale*font->face->descender;
metrics.lineHeight = scale*font->face->height;
metrics.underlineY = scale*font->face->underline_position;
metrics.underlineThickness = scale*font->face->underline_thickness;
return true;
}
bool getFontWhitespaceWidth(double &spaceAdvance, double &tabAdvance, FontHandle *font, FontCoordinateScaling coordinateScaling) {
double scale = getFontCoordinateScale(font->face, coordinateScaling);
FT_Error error = FT_Load_Char(font->face, ' ', FT_LOAD_NO_SCALE);
if (error)
return false;
spaceAdvance = scale*font->face->glyph->advance.x;
error = FT_Load_Char(font->face, '\t', FT_LOAD_NO_SCALE);
if (error)
return false;
tabAdvance = scale*font->face->glyph->advance.x;
return true;
}
bool getGlyphCount(unsigned &output, FontHandle *font) {
output = (unsigned) font->face->num_glyphs;
return true;
}
bool getGlyphIndex(GlyphIndex &glyphIndex, FontHandle *font, unicode_t unicode) {
glyphIndex = GlyphIndex(FT_Get_Char_Index(font->face, unicode));
return glyphIndex.getIndex() != 0;
}
bool loadGlyph(Shape &output, FontHandle *font, GlyphIndex glyphIndex, FontCoordinateScaling coordinateScaling, double *outAdvance) {
if (!font)
return false;
FT_Error error = FT_Load_Glyph(font->face, glyphIndex.getIndex(), FT_LOAD_NO_SCALE);
if (error)
return false;
double scale = getFontCoordinateScale(font->face, coordinateScaling);
if (outAdvance)
*outAdvance = scale*font->face->glyph->advance.x;
return !readFreetypeOutline(output, &font->face->glyph->outline, scale);
}
bool loadGlyph(Shape &output, FontHandle *font, unicode_t unicode, FontCoordinateScaling coordinateScaling, double *outAdvance) {
return loadGlyph(output, font, GlyphIndex(FT_Get_Char_Index(font->face, unicode)), coordinateScaling, outAdvance);
}
bool loadGlyph(Shape &output, FontHandle *font, GlyphIndex glyphIndex, double *outAdvance) {
return loadGlyph(output, font, glyphIndex, FONT_SCALING_LEGACY, outAdvance);
}
bool loadGlyph(Shape &output, FontHandle *font, unicode_t unicode, double *outAdvance) {
return loadGlyph(output, font, unicode, FONT_SCALING_LEGACY, outAdvance);
}
bool getKerning(double &output, FontHandle *font, GlyphIndex glyphIndex0, GlyphIndex glyphIndex1, FontCoordinateScaling coordinateScaling) {
FT_Vector kerning;
if (FT_Get_Kerning(font->face, glyphIndex0.getIndex(), glyphIndex1.getIndex(), FT_KERNING_UNSCALED, &kerning)) {
output = 0;
return false;
}
output = getFontCoordinateScale(font->face, coordinateScaling)*kerning.x;
return true;
}
bool getKerning(double &output, FontHandle *font, unicode_t unicode0, unicode_t unicode1, FontCoordinateScaling coordinateScaling) {
return getKerning(output, font, GlyphIndex(FT_Get_Char_Index(font->face, unicode0)), GlyphIndex(FT_Get_Char_Index(font->face, unicode1)), coordinateScaling);
}
#ifndef MSDFGEN_DISABLE_VARIABLE_FONTS
bool setFontVariationAxis(FreetypeHandle *library, FontHandle *font, const char *name, double coordinate) {
bool success = false;
if (font->face->face_flags&FT_FACE_FLAG_MULTIPLE_MASTERS) {
FT_MM_Var *master = NULL;
if (FT_Get_MM_Var(font->face, &master))
return false;
if (master && master->num_axis) {
std::vector<FT_Fixed> coords(master->num_axis);
if (!FT_Get_Var_Design_Coordinates(font->face, FT_UInt(coords.size()), &coords[0])) {
for (FT_UInt i = 0; i < master->num_axis; ++i) {
if (!strcmp(name, master->axis[i].name)) {
coords[i] = DOUBLE_TO_F16DOT16(coordinate);
success = true;
break;
}
}
}
if (FT_Set_Var_Design_Coordinates(font->face, FT_UInt(coords.size()), &coords[0]))
success = false;
}
FT_Done_MM_Var(library->library, master);
}
return success;
}
bool listFontVariationAxes(std::vector<FontVariationAxis> &axes, FreetypeHandle *library, FontHandle *font) {
if (font->face->face_flags&FT_FACE_FLAG_MULTIPLE_MASTERS) {
FT_MM_Var *master = NULL;
if (FT_Get_MM_Var(font->face, &master))
return false;
axes.resize(master->num_axis);
for (FT_UInt i = 0; i < master->num_axis; ++i) {
FontVariationAxis &axis = axes[i];
axis.name = master->axis[i].name;
axis.minValue = F16DOT16_TO_DOUBLE(master->axis[i].minimum);
axis.maxValue = F16DOT16_TO_DOUBLE(master->axis[i].maximum);
axis.defaultValue = F16DOT16_TO_DOUBLE(master->axis[i].def);
}
FT_Done_MM_Var(library->library, master);
return true;
}
return false;
}
#endif
}