forked from Chlumsky/msdfgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport-font.cpp
More file actions
162 lines (133 loc) · 4.8 KB
/
import-font.cpp
File metadata and controls
162 lines (133 loc) · 4.8 KB
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
#include "import-font.h"
#include <queue>
namespace msdfgen {
#define REQUIRE(cond) { if (!(cond)) return false; }
class FreetypeHandle {
friend FreetypeHandle * initializeFreetype();
friend void deinitializeFreetype(FreetypeHandle *library);
friend FontHandle * loadFont(FreetypeHandle *library, const char *filename);
FT_Library library;
};
class FontHandle {
friend FontHandle * loadFont(FreetypeHandle *library, const char *filename);
friend void destroyFont(FontHandle *font);
friend bool getFontScale(double &output, FontHandle *font);
friend bool getFontWhitespaceWidth(double &spaceAdvance, double &tabAdvance, FontHandle *font);
friend bool loadGlyph(Shape &output, FontHandle *font, int unicode, double *advance);
friend bool getKerning(double &output, FontHandle *font, int unicode1, int unicode2);
FT_Face face;
};
struct FtContext {
Point2 position;
Shape *shape;
Contour *contour;
};
static Point2 ftPoint2(const FT_Vector &vector) {
return Point2(vector.x/64., vector.y/64.);
}
static int ftMoveTo(const FT_Vector *to, void *user) {
FtContext *context = reinterpret_cast<FtContext *>(user);
context->contour = &context->shape->addContour();
context->position = ftPoint2(*to);
return 0;
}
static int ftLineTo(const FT_Vector *to, void *user) {
FtContext *context = reinterpret_cast<FtContext *>(user);
context->contour->addEdge(new LinearSegment(context->position, ftPoint2(*to)));
context->position = ftPoint2(*to);
return 0;
}
static int ftConicTo(const FT_Vector *control, const FT_Vector *to, void *user) {
FtContext *context = reinterpret_cast<FtContext *>(user);
context->contour->addEdge(new QuadraticSegment(context->position, ftPoint2(*control), ftPoint2(*to)));
context->position = ftPoint2(*to);
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);
context->contour->addEdge(new CubicSegment(context->position, ftPoint2(*control1), ftPoint2(*control2), ftPoint2(*to)));
context->position = ftPoint2(*to);
return 0;
}
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 * 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;
}
return handle;
}
void destroyFont(FontHandle *font) {
FT_Done_Face(font->face);
delete font;
}
bool getFontScale(double &output, FontHandle *font) {
output = font->face->units_per_EM/64.;
return true;
}
bool getFontWhitespaceWidth(double &spaceAdvance, double &tabAdvance, FontHandle *font) {
FT_Error error = FT_Load_Char(font->face, ' ', FT_LOAD_NO_SCALE);
if (error)
return false;
spaceAdvance = font->face->glyph->advance.x/64.;
error = FT_Load_Char(font->face, '\t', FT_LOAD_NO_SCALE);
if (error)
return false;
tabAdvance = font->face->glyph->advance.x/64.;
return true;
}
bool loadGlyph(Shape &output, FT_Face fontFace, int unicode, double *advance) {
FT_Error error = FT_Load_Char(fontFace, unicode, FT_LOAD_NO_SCALE);
if (error)
return false;
output.contours.clear();
output.inverseYAxis = false;
// Don't assume 64 height. Use the ascender/descender here
if (advance)
*advance = static_cast<double>(fontFace->glyph->advance.x)/(fontFace->ascender - fontFace->descender);
FtContext context = { };
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;
error = FT_Outline_Decompose(&fontFace->glyph->outline, &ftFunctions, &context);
if (error)
return false;
return true;
}
bool loadGlyph(Shape &output, FontHandle *font, int unicode, double *advance) {
if (!font->face) {
return false;
}
return loadGlyph(output, font->face, unicode, advance);
}
bool getKerning(double &output, FontHandle *font, int unicode1, int unicode2) {
FT_Vector kerning;
if (FT_Get_Kerning(font->face, FT_Get_Char_Index(font->face, unicode1), FT_Get_Char_Index(font->face, unicode2), FT_KERNING_UNSCALED, &kerning)) {
output = 0;
return false;
}
output = kerning.x/64.;
return true;
}
}