forked from 15-466/15-466-f22-base4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-PathFont-font.py
executable file
·560 lines (481 loc) · 13.7 KB
/
make-PathFont-font.py
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/usr/bin/env python3
#
# Based on code from Chesskoban (c) 2017-2019 Jim McCann;
# this adapted-for-15-466 code is released into the public domain.
#
import xml.parsers.expat
import re
import math
import sys
insvg = 'PathFont-font.svg'
hppname = 'PathFont-font.hpp'
cppname = hppname[0:-4] + ".cpp"
fontname = 'font'
xmlparser = xml.parsers.expat.ParserCreate()
def mul(A,B):
if len(A) == 6 and len(B) == 6:
return (
A[0] * B[0] + A[2] * B[1] + A[4] * 0.0,
A[1] * B[0] + A[3] * B[1] + A[5] * 0.0,
A[0] * B[2] + A[2] * B[3] + A[4] * 0.0,
A[1] * B[2] + A[3] * B[3] + A[5] * 0.0,
A[0] * B[4] + A[2] * B[5] + A[4] * 1.0,
A[1] * B[4] + A[3] * B[5] + A[5] * 1.0
)
elif len(A) == 6 and len(B) == 2:
return (
A[0] * B[0] + A[2] * B[1] + A[4] * 1.0,
A[1] * B[0] + A[3] * B[1] + A[5] * 1.0
)
else:
assert(False) #"Invalid multiply arg lengths " + str(len(A)) + " and " + str(len(B)))
def inv(A):
assert(len(A) == 6)
a = A[0]
b = A[2]
c = A[1]
d = A[3]
x = A[4]
y = A[5]
invdet = 1.0 / (a*d - c*b)
a *= invdet
b *= invdet
c *= invdet
d *= invdet
B = ( d, -c, -b, a, -(d * x + -b * y), -(-c * x + a * y) )
P = mul(A,B)
#print("Matrix: " + repr(A) + " / Inverse: " + repr(B) + " / Product: " + repr(P))
return B
def parse_transform(transform):
print('parsing: ' + transform)
#transform is a list of zero or more commands
wsp = '[\x20\x09\x0D\x0A]'
comma_wsp = '[\x20\x09\x0D\x0A,]'
number = r'[+-]?[0-9]*\.?[0-9]+(?:[eE][+-]?[0-9]+)?'
first = True
xf = (1,0, 0,1, 0,0)
while True:
if first: ws = wsp + '*'
else: ws = comma_wsp + '+'
first = False
m = re.match(
ws
+ r'(matrix|translate|scale|rotate|skewX|skewY)'
+ wsp + '*'
+ r'\('
+ wsp + '*'
+ '(' + number + '(:?' + comma_wsp + '+' + number + ')*' + ')'
+ wsp + '*'
+ r'\)'
, transform)
if m == None: break
op = m.group(1)
args = list(map(float, re.split(comma_wsp + '+', m.group(2))))
#print("op: " + op)
#print("args: " + repr(args))
mat = (1,0, 0,1, 0,0)
if op == 'matrix':
if len(args) == 6:
mat = tuple(args)
else:
print("WARNING: ignoring matrix with args " + repr(args) + " (non-length-6)")
elif op == 'translate':
if len(args) == 2:
mat = (1,0, 0,1, args[0], args[1])
elif len(args) == 1:
mat = (1,0, 0,1, args[0], 0)
else:
print("WARNING: ignoring translate with args " + repr(args) + " (non-length-6)")
elif op == 'scale':
if len(args) == 2:
mat = (args[0],0, 0,args[1], 0,0)
elif len(args) == 1:
mat = (args[0],0, 0,args[0], 0,0)
else:
print("WARNING: ignoring scale with args " + repr(args) + " (non-length-6)")
elif op == 'rotate':
if len(args) == 3 or len(args) == 1:
cos = math.cos(math.pi * args[0] / 180.0)
sin = math.sin(math.pi * args[0] / 180.0)
mat = (cos,sin, -sin,cos, 0,0)
if len(args) == 3:
mat = mul( (1,0, 0,1, args[1], args[2]),
mul( mat, (1,0, 0,1, -args[1],-args[2]) ) )
else:
print("WARNING: ignoring rotate with args " + repr(args) + " (non-length-6)")
elif op == 'skewX':
if len(args) == 1:
tan = math.tan(math.pi * args[0] / 180.0)
mat = (1,0, tan,1, 0,0)
else:
print("WARNING: ignoring skewX with args " + repr(args) + " (non-length-6)")
elif op == 'skewY':
if len(args) == 1:
tan = math.tan(math.pi * args[0] / 180.0)
mat = (1,tan, 0,1, 0,0)
else:
print("WARNING: ignoring skewY with args " + repr(args) + " (non-length-6)")
else:
assert(False) # "shouldn't have an op named '" + op + "'"
xf = mul(xf, mat)
transform = transform[m.end():]
if not re.fullmatch(wsp + '*', transform):
print("WARNING: ignoring trailing transform information '" + transform + "'")
return xf
def parse_path(data):
idx = 0
def peek():
nonlocal idx, data
if idx < len(data): return data[idx]
else: return 'EOF'
def get():
nonlocal idx, data
assert(idx < len(data))
idx += 1
return data[idx-1]
#divide path into command groups by implementing the grammar from the SVG spec:
def skip_wsp_star():
while peek() in '\x20\x09\x0D\x0A':
get()
def read_number():
if peek() not in '+-0123456789.': return None
num = ''
if peek() in '+-': num += get()
while peek() in '0123456789': num += get()
if peek() == '.':
num += get()
while peek() in '0123456789': num += get()
if peek() in 'eE':
num += get()
if peek() in '+-': num += get()
while peek() in '0123456789': num += get()
return float(num)
def skip_comma_wsp():
skip_wsp_star()
if peek() == ',':
get()
skip_wsp_star()
def read_coordinate_pair():
x = read_number()
if x == None: return None
skip_comma_wsp()
y = read_number()
assert(y != None)
return (x, y)
def read_moveto():
cmd = get()
assert(cmd in 'mM')
skip_wsp_star()
args = []
while True:
pair = read_coordinate_pair()
if pair == None:
assert(len(args) > 0)
break
args.append(pair)
skip_comma_wsp()
return (cmd, args)
def read_drawto():
if peek() not in 'ZzLlHhVvCcSsQqTtAa': return None
cmd = get()
args = []
if cmd in 'Zz':
pass #no args
elif cmd in 'LlTt':
skip_wsp_star()
while True:
a = read_coordinate_pair()
if a == None:
assert(len(args) > 0)
break
args.append(a)
skip_comma_wsp()
elif cmd in 'HhVv':
skip_wsp_star()
while True:
a = read_number()
if a == None:
assert(len(args) > 0)
break
args.append(a)
skip_comma_wsp()
elif cmd in 'Cc':
skip_wsp_star()
while True:
a = read_coordinate_pair()
if a == None:
assert(len(args) > 0)
break
args.append(a)
skip_comma_wsp()
a = read_coordinate_pair()
assert(a != None)
args.append(a)
skip_comma_wsp()
a = read_coordinate_pair()
assert(a != None)
skip_comma_wsp()
args.append(a)
elif cmd in 'SsQq':
skip_wsp_star()
while True:
a = read_coordinate_pair()
if a == None:
assert(len(args) > 0)
break
args.append(a)
skip_comma_wsp()
a = read_coordinate_pair()
assert(a != None)
args.append(a)
elif cmd in 'Aa':
skip_wsp_star()
while True:
rx = read_nonnegative_number()
if rx == None:
assert(len(args) > 0)
break
skip_comma_wsp()
ry = read_nonnegative_number()
assert(ry != None)
skip_comma_wsp()
x_axis_rotation = read_number()
assert(x_axis_rotation != None)
skip_comma_wsp()
large_arc_flag = read_flag()
assert(large_arc_flag != None)
skip_comma_wsp()
sweep_flag = read_flag()
assert(sweep_flag != None)
skip_comma_wsp()
x = read_number()
assert(x != None)
skip_comma_wsp()
y = read_number()
assert(y != None)
skip_comma_wsp()
args.append((rx, ry, x_axis_rotation, large_arc_flag, sweep_flag, x, y))
else:
assert(False)
return (cmd, args)
cmds = []
while True:
skip_wsp_star()
if peek() == 'EOF': break
moveto = read_moveto()
assert(moveto)
cmds.append(moveto)
while True:
skip_wsp_star()
drawto = read_drawto()
if drawto == None: break
cmds.append(drawto)
return cmds
glyphs = dict()
class Glyph:
def __init__(self, name):
self.accum = []
self.box_accum = []
self.box_xf = (1,0, 0,1, 0,0)
self.name = name
xf_stack = [(1,0, 0,1, 0,0)]
glyph_stack = [None]
accum_stack = [None]
def start_element(name, attrs):
#handle transform:
xf = (1,0, 0,1, 0,0)
if 'transform' in attrs: xf = parse_transform(attrs['transform'])
xf_stack.append(mul(xf_stack[-1], xf))
#handle label:
label = ''
if 'inkscape:label' in attrs: label = attrs['inkscape:label']
accum = accum_stack[-1]
glyph = glyph_stack[-1]
if label.startswith('glyph:'):
if glyph != None: print("WARNING: nested glyphs.")
#start a new glyph:
glyph = Glyph(label[len('glyph:'):])
accum = glyph.accum
elif label.startswith('box:'):
if glyph == None:
print("WARNING: box outside of glyph.")
else:
#switch to box accumulator:
if len(glyph.box_accum) != 0: print("WARNING: multiple boxes in glyph.")
accum = glyph.box_accum
glyph.box_xf = xf
accum_stack.append(accum)
glyph_stack.append(glyph)
d = None
if name == 'path' and 'd' in attrs:
d = attrs['d']
elif name == 'rect':
x = float(attrs['x'])
y = float(attrs['y'])
width = float(attrs['width'])
height = float(attrs['height'])
d = ( 'M' + str(x) + ' ' + str(y) +
'L' + str(x+width) + ' ' + str(y) +
'L' + str(x+width) + ' ' + str(y+height) +
'L' + str(x) + ' ' + str(y+height) +
'Z' )
if d != None and accum != None:
#print(d)
accum.append( (xf, parse_path(d) ) )
def end_element(name):
glyph = glyph_stack[-1]
xf_stack.pop()
accum_stack.pop()
glyph_stack.pop()
if glyph != None and glyph_stack[-1] == None:
glyphs[glyph.name] = glyph
#TODO: grab glyph path from accum
xmlparser.StartElementHandler = start_element
xmlparser.EndElementHandler = end_element
xmlparser.ParseFile(open(insvg, 'rb'))
def cmds_to_lines(xf, cmds):
lines = []
def approx_bezier(a,b,c,d):
nonlocal lines
perp = (d[1] - a[1], -(d[0] - a[0]))
amt = max(
abs(perp[0] * (b[0]-a[0]) + perp[1] * (b[1]-a[1])),
abs(perp[0] * (c[0]-a[0]) + perp[1] * (c[1]-a[1]))
) / math.sqrt(perp[0]*perp[0] + perp[1]*perp[1])
if amt > 0.02:
ab = (0.5 * (a[0] + b[0]), 0.5 * (a[1] + b[1]))
bc = (0.5 * (b[0] + c[0]), 0.5 * (b[1] + c[1]))
cd = (0.5 * (c[0] + d[0]), 0.5 * (c[1] + d[1]))
abc = (0.5 * (ab[0] + bc[0]), 0.5 * (ab[1] + bc[1]))
bcd = (0.5 * (bc[0] + cd[0]), 0.5 * (bc[1] + cd[1]))
abcd = (0.5 * (abc[0] + bcd[0]), 0.5 * (abc[1] + bcd[1]))
approx_bezier(a, ab, abc, abcd)
approx_bezier(abcd, bcd, cd, d)
else:
lines += [a,b, b,c, c,d]
start = None
prev = (0.0, 0.0)
prev_cp = (0.0, 0.0)
for ca in cmds:
cmd = ca[0]
args = ca[1]
if cmd in 'Mm':
start = None
for pt in args:
if cmd == 'm': pt = (prev[0] + pt[0], prev[1] + pt[1])
if start == None:
start = pt
prev_cp = prev = pt
if prev != pt: lines += [mul(xf, prev), mul(xf, pt)]
prev_cp = prev = pt
elif cmd in 'Ll':
for pt in args:
if cmd == 'l': pt = (prev[0] + pt[0], prev[1] + pt[1])
if prev != pt: lines += [mul(xf, prev), mul(xf, pt)]
prev_cp = prev = pt
elif cmd in 'Cc':
for i in range(0, len(args), 3):
p1 = args[i]
p2 = args[i+1]
p3 = args[i+2]
if cmd == 'c':
p1 = (prev[0] + p1[0], prev[1] + p1[1])
p2 = (prev[0] + p2[0], prev[1] + p2[1])
p3 = (prev[0] + p3[0], prev[1] + p3[1])
approx_bezier(mul(xf, prev), mul(xf, p1), mul(xf, p2), mul(xf, p3))
prev_cp = p2
prev = p3
elif cmd in 'Zz':
pt = start
if prev != pt: lines += [mul(xf, prev), mul(xf, pt)]
prev = pt
else:
print("Skipping unimplemented command '" + cmd + "'")
return lines
def accum_to_lines(accum, outer_xf):
lines = []
for xc in accum:
xf = xc[0]
cmds = xc[1]
lines += cmds_to_lines(mul(outer_xf, xf), cmds)
return lines
out_glyphs = 0
out_glyph_widths = []
out_glyph_char_starts = []
out_chars = []
out_glyph_coord_starts = []
out_coords = []
for char_glyph in sorted(glyphs.items()):
glyph = char_glyph[1]
print("--- '" + glyph.name + "' ---")
inv_box_xf = inv(glyph.box_xf)
print(" Box has " + str(len(glyph.box_accum)) + " paths.")
box_lines = accum_to_lines(glyph.box_accum, inv_box_xf)
inf = float('inf')
box_min = ( inf, inf)
box_max = (-inf, -inf)
for coord in box_lines:
box_min = ( min(box_min[0], coord[0]), min(box_min[1], coord[1]) )
box_max = ( max(box_max[0], coord[0]), max(box_max[1], coord[1]) )
print(" Box Bounds: " + repr(box_min) + " " + repr(box_max))
scale = 1.0 / (box_max[1] - box_min[1])
width = scale * (box_max[0] - box_min[0])
glyph_xf = mul(inv_box_xf, (scale,0, 0,-scale, scale * -box_min[0],-scale * -box_max[1]) )
print(" Glyph has " + str(len(glyph.accum)) + " paths.")
glyph_lines = accum_to_lines(glyph.accum, glyph_xf)
inf = float('inf')
glyph_min = ( inf, inf)
glyph_max = (-inf, -inf)
for coord in glyph_lines:
glyph_min = ( min(glyph_min[0], coord[0]), min(glyph_min[1], coord[1]) )
glyph_max = ( max(glyph_max[0], coord[0]), max(glyph_max[1], coord[1]) )
print(" Glyph Bounds: " + repr(glyph_min) + " " + repr(glyph_max))
print(" Glyph Width: " + str(width))
out_glyphs += 1
out_glyph_widths += [width]
out_glyph_char_starts += [len(out_chars)]
out_chars += list(glyph.name.encode('utf8'))
out_glyph_coord_starts += [len(out_coords)]
for pair in glyph_lines:
out_coords += list(pair)
print("Font covers: " + ", ".join(map(lambda x: "'" + x + "'", sorted(glyphs.keys()))))
missing = []
for m in range(0x20, 0x7f):
c = chr(m)
if c not in glyphs:
missing.append(c)
print("Font misses: " + ", ".join(map(lambda x: "'" + x + "'", missing)))
print("Writing PathFont '" + fontname + "' to '" + cppname + "'")
cppfile = open(cppname, 'wb')
def w(s):
cppfile.write(s.encode('utf8'))
def wd(d,fs, wrap):
w('\t\t')
for i in range(0,len(d)):
if i != 0: w(',')
if i != 0 and i % wrap == 0: w('\n\t\t')
elif i != 0: w(' ')
w(fs.format(d[i]))
w('\n')
w('//automatically generated\n')
w('#include \"PathFont.hpp\"\n')
w('namespace {\n')
w('\tconstexpr const uint32_t font_glyphs = ' + str(out_glyphs) + ';\n')
w('\tconstexpr const float font_glyph_widths[font_glyphs] = {\n')
wd(out_glyph_widths, "{:.6f}f", 6)
w('\t};\n')
w('\tconstexpr const uint32_t font_glyph_char_starts[font_glyphs+1] = {\n')
wd(out_glyph_char_starts + [len(out_chars)], "{}", 12)
w('\t};\n')
w('\tconstexpr const uint8_t font_chars[' + str(len(out_chars)) + '] = {\n')
wd(out_chars, "{}", 12)
w('\t};\n')
w('\tconstexpr const uint32_t font_glyph_coord_starts[font_glyphs+1] = {\n')
wd(out_glyph_coord_starts + [len(out_coords)], "{}", 12)
w('\t};\n')
w('\tconstexpr const float font_coords[' + str(len(out_coords)) + '] = {\n')
wd(out_coords, "{:.6f}f", 6)
w('\t};\n')
w('}\n')
w('PathFont PathFont::font(font_glyphs, font_glyph_widths, font_glyph_char_starts, font_chars, font_glyph_coord_starts, font_coords);\n')
cppfile.close()