-
Notifications
You must be signed in to change notification settings - Fork 48
/
font_to_svg.hpp
457 lines (401 loc) · 14.1 KB
/
font_to_svg.hpp
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// font_to_svg.hpp - Read Font in TrueType (R) format, write SVG
// Copyright Don Bright 2013 <[email protected]>
/*
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
License based on zlib license, by Jean-loup Gailly and Mark Adler
*/
/*
This program reads a TTF (TrueType (R)) file and outputs an SVG path.
See these sites for more info.
Basic Terms: http://www.freetype.org/freetype2/docs/glyphs/glyphs-3.html
FType + outlines: http://www.freetype.org/freetype2/docs/reference/ft2-outline_processinhtml
FType + contours: http://www.freetype.org/freetype2/docs/glyphs/glyphs-6.html
TType contours: https://developer.apple.com/fonts/TTRefMan/RM01/Chap1.html
TType contours2: http://www.truetype-typography.com/ttoutln.htm
Non-zero winding rule: http://en.wikipedia.org/wiki/Nonzero-rule
SVG paths: http://www.w3schools.com/svg/svg_path.asp
SVG paths + nonzero: http://www.w3.org/TR/SVG/paintinhtml#FillProperties
TrueType is a trademark of Apple Inc. Use of this mark does not imply
endorsement.
*/
#ifndef __font_to_svg_h__
#define __font_to_svg_h__
#include <ft2build.h>
#include FT_FREETYPE_H
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
namespace font2svg {
std::stringstream debug;
FT_Vector halfway_between( FT_Vector p1, FT_Vector p2 )
{
FT_Vector newv;
newv.x = p1.x + (p2.x-p1.x)/2.0;
newv.y = p1.y + (p2.y-p1.y)/2.0;
return newv;
}
class ttf_file
{
public:
std::string filename;
FT_Library library;
FT_Face face;
FT_Error error;
ttf_file()
{
filename = std::string("");
}
ttf_file( std::string fname )
{
filename = fname;
error = FT_Init_FreeType( &library );
debug << "Init error code: " << error;
// Load a typeface
error = FT_New_Face( library, filename.c_str(), 0, &face );
debug << "\nFace load error code: " << error;
debug << "\nfont filename: " << filename;
if (error) {
std::cerr << "problem loading file " << filename << "\n";
exit(1);
}
debug << "\nFamily Name: " << face->family_name;
debug << "\nStyle Name: " << face->style_name;
debug << "\nNumber of faces: " << face->num_faces;
debug << "\nNumber of glyphs: " << face->num_glyphs;
}
void free()
{
debug << "\n<!--";
error = FT_Done_Face( face );
debug << "\nFree face. error code: " << error;
error = FT_Done_FreeType( library );
debug << "\nFree library. error code: " << error;
debug << "\n-->\n";
}
};
/* Draw the outline of the font as svg.
There are three main components.
1. the points
2. the 'tags' for the points
3. the contour indexes (that define which points belong to which contour)
*/
std::string do_outline(std::vector<FT_Vector> points, std::vector<char> tags, std::vector<short> contours)
{
std::stringstream debug, svg;
std::cout << "<!-- do outline -->\n";
if (points.size()==0) return "<!-- font had 0 points -->";
if (contours.size()==0) return "<!-- font had 0 contours -->";
svg.str("");
svg << "\n\n <!-- draw actual outline using lines and Bezier curves-->";
svg << "\n <path fill='black' stroke='black'"
<< " fill-opacity='0.45' "
<< " stroke-width='2' "
<< " d='";
/* tag bit 1 indicates whether its a control point on a bez curve
or not. two consecutive control points imply another point halfway
between them */
// Step 1. move to starting point (M x-coord y-coord )
// Step 2. decide whether to draw a line or a bezier curve or to move
// Step 3. for bezier: Q control-point-x control-point-y,
// destination-x, destination-y
// for line: L x-coord, y-coord
// for move: M x-coord, y-coord
int contour_starti = 0;
int contour_endi = 0;
for ( int i = 0 ; i < contours.size() ; i++ ) {
contour_endi = contours.at(i);
debug << "new contour starting. startpt index, endpt index:";
debug << contour_starti << "," << contour_endi << "\n";
int offset = contour_starti;
int npts = contour_endi - contour_starti + 1;
debug << "number of points in this contour: " << npts << "\n";
debug << "moving to first pt " << points[offset].x << "," << points[offset].y << "\n";
svg << "\n M " << points[contour_starti].x << "," << points[contour_starti].y << "\n";
debug << "listing pts: [this pt index][isctrl] <next pt index><isctrl> [x,y] <nx,ny>\n";
for ( int j = 0; j < npts; j++ ) {
int thisi = j%npts + offset;
int nexti = (j+1)%npts + offset;
int nextnexti = (j+2)%npts + offset;
int x = points[thisi].x;
int y = points[thisi].y;
int nx = points[nexti].x;
int ny = points[nexti].y;
int nnx = points[nextnexti].x;
int nny = points[nextnexti].y;
bool this_tagbit1 = (tags[ thisi ] & 1);
bool next_tagbit1 = (tags[ nexti ] & 1);
bool nextnext_tagbit1 = (tags[ nextnexti ] & 1);
bool this_isctl = !this_tagbit1;
bool next_isctl = !next_tagbit1;
bool nextnext_isctl = !nextnext_tagbit1;
debug << " [" << thisi << "]";
debug << "[" << !this_tagbit1 << "]";
debug << " <" << nexti << ">";
debug << "<" << !next_tagbit1 << ">";
debug << " <<" << nextnexti << ">>";
debug << "<<" << !nextnext_tagbit1 << ">>";
debug << " [" << x << "," << y << "]";
debug << " <" << nx << "," << ny << ">";
debug << " <<" << nnx << "," << nny << ">>";
debug << "\n";
if (this_isctl && next_isctl) {
debug << " two adjacent ctl pts. adding point halfway between " << thisi << " and " << nexti << ":";
debug << " reseting x and y to ";
x = (x + nx) / 2;
y = (y + ny) / 2;
this_isctl = false;
debug << " [" << x << "," << y <<"]\n";
if (j==0) {
debug << "first pt in contour was ctrl pt. moving to non-ctrl pt\n";
svg << " M " << x << "," << y << "\n";
}
}
if (!this_isctl && next_isctl && !nextnext_isctl) {
svg << " Q " << nx << "," << ny << " " << nnx << "," << nny << "\n";
debug << " bezier to " << nnx << "," << nny << " ctlx, ctly: " << nx << "," << ny << "\n";
} else if (!this_isctl && next_isctl && nextnext_isctl) {
debug << " two ctl pts coming. adding point halfway between " << nexti << " and " << nextnexti << ":";
debug << " reseting nnx and nny to halfway pt";
nnx = (nx + nnx) / 2;
nny = (ny + nny) / 2;
svg << " Q " << nx << "," << ny << " " << nnx << "," << nny << "\n";
debug << " bezier to " << nnx << "," << nny << " ctlx, ctly: " << nx << "," << ny << "\n";
} else if (!this_isctl && !next_isctl) {
svg << " L " << nx << "," << ny << "\n";
debug << " line to " << nx << "," << ny << "\n";
} else if (this_isctl && !next_isctl) {
debug << " this is ctrl pt. skipping to " << nx << "," << ny << "\n";
}
}
contour_starti = contour_endi+1;
svg << " Z\n";
}
svg << "\n '/>";
std::cout << "\n<!--\n" << debug.str() << " \n-->\n";
return svg.str();
}
class glyph
{
public:
int codepoint;
FT_GlyphSlot slot;
FT_Error error;
FT_Outline ftoutline;
FT_Glyph_Metrics gm;
FT_Face face;
ttf_file file;
FT_Vector* ftpoints;
char* tags;
short* contours;
std::stringstream debug, tmp;
int bbwidth, bbheight;
glyph( ttf_file &f, std::string unicode_str )
{
file = f;
init( unicode_str );
}
glyph( const char * filename, std::string unicode_str )
{
this->file = ttf_file( std::string(filename) );
init( unicode_str );
}
glyph( const char * filename, const char * unicode_c_str )
{
this->file = ttf_file( std::string(filename) );
init( std::string(unicode_c_str) );
}
void free()
{
file.free();
}
void init( std::string unicode_s )
{
face = file.face;
codepoint = strtol( unicode_s.c_str() , NULL, 0 );
// Load the Glyph into the face's Glyph Slot + print details
FT_UInt glyph_index = FT_Get_Char_Index( face, codepoint );
debug << "<!--\nUnicode requested: " << unicode_s;
debug << " (decimal: " << codepoint << " hex: 0x"
<< std::hex << codepoint << std::dec << ")";
debug << "\nGlyph index for unicode: " << glyph_index;
error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
debug << "\nLoad Glyph into Face's glyph slot. error code: " << error;
slot = face->glyph;
ftoutline = slot->outline;
char glyph_name[1024];
FT_Get_Glyph_Name( face, glyph_index, glyph_name, 1024 );
gm = slot->metrics;
debug << "\nGlyph Name: " << glyph_name;
debug << "\nGlyph Width: " << gm.width
<< " Height: " << gm.height
<< " Hor. Advance: " << gm.horiAdvance
<< " Vert. Advance: " << gm.vertAdvance;
// Print outline details, taken from the glyph in the slot.
debug << "\nNum points: " << ftoutline.n_points;
debug << "\nNum contours: " << ftoutline.n_contours;
debug << "\nContour endpoint index values:";
for ( int i = 0 ; i < ftoutline.n_contours ; i++ ) debug << " " << ftoutline.contours[i];
debug << "\n-->\n";
// Invert y coordinates (SVG = neg at top, TType = neg at bottom)
ftpoints = ftoutline.points;
for ( int i = 0 ; i < ftoutline.n_points ; i++ )
ftpoints[i].y *= -1;
bbheight = face->bbox.yMax - face->bbox.yMin;
bbwidth = face->bbox.xMax - face->bbox.xMin;
tags = ftoutline.tags;
contours = ftoutline.contours;
std::cout << debug.str();
}
std::string svgheader() {
tmp.str("");
tmp << "\n<svg width='" << bbwidth << "px'"
<< " height='" << bbheight << "px'"
<< " xmlns='http://www.w3.org/2000/svg' version='1.1'>";
return tmp.str();
}
std::string svgborder() {
tmp.str("");
tmp << "\n\n <!-- draw border -->";
tmp << "\n <rect fill='none' stroke='black'"
<< " width='" << bbwidth - 1 << "'"
<< " height='" << bbheight - 1 << "'/>";
return tmp.str();
}
std::string svgtransform() {
// TrueType points are not in the range usually visible by SVG.
// they often have negative numbers etc. So.. here we
// 'transform' to make visible.
//
// note also that y coords of all points have been flipped during
// init() so that SVG Y positive = Truetype Y positive
tmp.str("");
tmp << "\n\n <!-- make sure glyph is visible within svg window -->";
int yadj = gm.horiBearingY + gm.vertBearingY + 100;
int xadj = 100;
tmp << "\n <g fill-rule='nonzero' "
<< " transform='translate(" << xadj << " " << yadj << ")'"
<< ">";
return tmp.str();
}
std::string axes() {
tmp.str("");
tmp << "\n\n <!-- draw axes --> ";
tmp << "\n <path stroke='blue' stroke-dasharray='5,5' d='"
<< " M" << -bbwidth << "," << 0
<< " L" << bbwidth << "," << 0
<< " M" << 0 << "," << -bbheight
<< " L" << 0 << "," << bbheight
<< " '/>";
return tmp.str();
}
std::string typography_box() {
tmp.str("");
tmp << "\n\n <!-- draw bearing + advance box --> ";
int x1 = 0;
int x2 = gm.horiAdvance;
int y1 = -gm.vertBearingY-gm.height;
int y2 = y1 + gm.vertAdvance;
tmp << "\n <path stroke='blue' fill='none' stroke-dasharray='10,16' d='"
<< " M" << x1 << "," << y1
<< " M" << x1 << "," << y2
<< " L" << x2 << "," << y2
<< " L" << x2 << "," << y1
<< " L" << x1 << "," << y1
<< " '/>";
return tmp.str();
}
std::string points() {
tmp.str("");
tmp << "\n\n <!-- draw points as circles -->";
for ( int i = 0 ; i < ftoutline.n_points ; i++ ) {
bool this_is_ctrl_pt = !(tags[i] & 1);
bool next_is_ctrl_pt = !(tags[(i+1)%ftoutline.n_points] & 1);
int x = ftpoints[i].x;
int y = ftpoints[i].y;
int nx = ftpoints[(i+1)%ftoutline.n_points].x;
int ny = ftpoints[(i+1)%ftoutline.n_points].y;
int radius = 5;
if ( i == 0 ) radius = 10;
std::string color;
if (this_is_ctrl_pt) color = "none"; else color = "blue";
if (this_is_ctrl_pt && next_is_ctrl_pt) {
tmp << "\n <!-- halfway pt between 2 ctrl pts -->";
tmp << "<circle"
<< " fill='" << "blue" << "'"
<< " stroke='black'"
<< " cx='" << (x+nx)/2 << "' cy='" << (y+ny)/2 << "'"
<< " r='" << 2 << "'"
<< "/>";
};
tmp << "\n <!--" << i << "-->";
tmp << "<circle"
<< " fill='" << color << "'"
<< " stroke='black'"
<< " cx='" << ftpoints[i].x << "' cy='" << ftpoints[i].y << "'"
<< " r='" << radius << "'"
<< "/>";
}
return tmp.str();
}
std::string pointlines() {
tmp.str("");
tmp << "\n\n <!-- draw straight lines between points -->";
tmp << "\n <path fill='none' stroke='green' d='";
tmp << "\n M " << ftpoints[0].x << "," << ftpoints[0].y << "\n";
tmp << "\n '/>";
for ( int i = 0 ; i < ftoutline.n_points-1 ; i++ ) {
std::string dash_mod("");
for (int j = 0 ; j < ftoutline.n_contours; j++ ) {
if (i==contours[j])
dash_mod = " stroke-dasharray='3'";
}
tmp << "\n <path fill='none' stroke='green'";
tmp << dash_mod;
tmp << " d='";
tmp << " M " << ftpoints[i].x << "," << ftpoints[i].y;
tmp << " L " << ftpoints[(i+1)%ftoutline.n_points].x << "," << ftpoints[(i+1)%ftoutline.n_points].y;
tmp << "\n '/>";
}
return tmp.str();
}
std::string labelpts() {
tmp.str("");
for ( int i = 0 ; i < ftoutline.n_points ; i++ ) {
tmp << "\n <g font-family='SVGFreeSansASCII,sans-serif' font-size='10'>\n";
tmp << " <text id='revision'";
tmp << " x='" << ftpoints[i].x + 5 << "'";
tmp << " y='" << ftpoints[i].y - 5 << "'";
tmp << " stroke='none' fill='darkgreen'>\n";
tmp << " " << ftpoints[i].x << "," << ftpoints[i].y;
tmp << " </text>\n";
tmp << " </g>\n";
}
return tmp.str();
}
std::string outline() {
std::vector<FT_Vector> pointsv(ftpoints,ftpoints+ftoutline.n_points);
std::vector<char> tagsv(tags,tags+ftoutline.n_points);
std::vector<short> contoursv(contours,contours+ftoutline.n_contours);
return do_outline(pointsv, tagsv, contoursv);
}
std::string svgfooter() {
tmp.str("");
tmp << "\n </g>\n</svg>\n";
return tmp.str();
}
};
} // namespace
#endif