forked from treeform/icons2font
/
icons2font.py
executable file
·588 lines (476 loc) · 18.9 KB
/
icons2font.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/usr/bin/env python2.7
"""
Terminology:
glyph - the idea and the visual representation of it (both airplane, and
drawing-of-airplane)
Friendly name - name of the SVG file without the extension
glyph name - integer representing a Unicode code point to which a glyph
will be assigned
character - glyph name converted to a str/char data type
"""
import argparse
import json
import logging
import md5
import os
import sys
from collections import OrderedDict
from xml.dom import minidom
import fontforge
log = logging.getLogger(__name__)
DESIGNER_FONT_START_CHAR = "A"
GSIZE = 1400
HEADER = """<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
<svg xmlns="http://www.w3.org/2000/svg">
<metadata></metadata>
<defs>
<font id="{}">
<font-face units-per-em="2048" ascent="1536" descent="-512" />
<missing-glyph horiz-adv-x="512" />
<glyph horiz-adv-x="0" />
<glyph horiz-adv-x="0" />
"""
GLYPH = """<glyph unicode="{glyph_name}" horiz-adv-x="{horz_size}" d="{path}" />\n\n"""
FOOTER = """
</font>
</defs>
</svg>
"""
DOC_HEADER = """
<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="{0}.css">
<style type="text/css">
body {{
font-size: 3em;
color: black;
}}
/* designer font */
@font-face {{
font-family: "{0}-designer";
src: url('{0}-designer.ttf') format('truetype');
font-weight: normal;
font-style: normal;
font-feature-settings: "calt=0,liga=0"
}}
textarea {{
font-family: {0}-designer;
font-size: 3em;
width: 100%;
height: 300px;
}}
</style>
</head>
<body>
<h1>Font: {0}</h1>
"""
DOC_FOOTER = """
<hr>
try out and <a href='{0}-designer.ttf'>download</a> desinger font
<textarea>a b c d</textarea>
</body>
</html>
"""
CSS_HEADER = """@font-face {{
font-family: "{0}";
src: url('{0}.eot?h={1}');
src: url('{0}.eot?h={1}#iefix') format('embedded-opentype'),
url('{0}.ttf?h={1}') format('truetype'),
url('{0}.woff?h={1}') format('woff'),
url('{0}.svg?h={1}') format('svg'),
url('{0}.otf?h={1}') format("opentype");
font-weight: normal;
font-style: normal;
font-feature-settings: "calt=0,liga=0"
}}
[class^="{0}-"], [class*=" {0}-"] {{
font-family: {0};
font-weight: normal;
font-style: normal;
display: inline-block;
text-decoration: inherit;
vertical-align: baseline;
-webkit-font-smoothing: antialiased;
}}
"""
USER_AREA = 0xf000
COMMANDS_ABS = "MZLHVCSQTA"
COMMANDS_REL = COMMANDS_ABS.lower()
COMMANDS = COMMANDS_ABS + COMMANDS_REL
class GlyphNameMapper(object):
def __init__(self, character_mapping):
super(GlyphNameMapper, self).__init__()
self.character_mapping = character_mapping
self.used_values = set(self.character_mapping.viewvalues())
self.new_mappings = OrderedDict()
self.next_glyph_name = ord(max(self.used_values)) if len(self.used_values) else USER_AREA
def advance_to_unused_next_glyph_name(self):
while self.next_glyph_name in self.used_values:
self.next_glyph_name += 1
def get_glyph_name(self, friendly_name):
if friendly_name in self.character_mapping:
return self.character_mapping[friendly_name]
else:
this_name = self.next_glyph_name
self.used_values.add(this_name)
self.advance_to_unused_next_glyph_name()
self.new_mappings[friendly_name] = unichr(this_name)
return self.new_mappings[friendly_name]
def log_new_mappings_if_necessary(self):
if len(self.character_mapping) and len(self.new_mappings):
suggested_additions = '\n'.join([
'"{}": "\\u{:x}",'.format(name, ord(char))
for name, char in self.new_mappings.viewitems()
])
log.info("Character mapping was missing some characters. Suggested additions:\n{}".format(suggested_additions))
def between(a, b, s):
first = s.find(a)
first += len(a)
last = s.find(b, first)
return s[first:last]
def htmlhex(n):
return hex(n).replace("0x","&#x") + ";"
def svg_paths(svg):
xmldoc = minidom.parseString(svg)
paths = []
# view box
for s in xmldoc.getElementsByTagName('svg'):
try:
viewBox = map(float, s.attributes['viewBox'].value.split())
except:
viewBox = [0,0,
float(s.attributes['width'].value),
float(s.attributes['height'].value)]
# width="100" height="100"
for s in xmldoc.getElementsByTagName('path'):
d = s.attributes['d'].value
paths.append(d)
for s in xmldoc.getElementsByTagName('polygon'):
d = s.attributes['points'].value
paths.append("M"+d)
for s in xmldoc.getElementsByTagName('rect'):
try: x = float(s.attributes['x'].value)
except: x = 0
try: y = float(s.attributes['y'].value)
except: y = 0
w = float(s.attributes['width'].value)
h = float(s.attributes['height'].value)
p = ["M",x,y, x+w,y, x+w,y+h, x,y+h, x,y, "Z"]
paths.append(" ".join(map(str,p)))
for s in xmldoc.getElementsByTagName('circle'):
cx = float(s.attributes['cx'].value)
cy = float(s.attributes['cy'].value)
r = float(s.attributes['r'].value)
p =["M", cx-r, cy,
"a", r,r, 0, 1,0, (r*2),0,
"a", r,r, 0, 1,0, -(r*2),0,
"Z"]
paths.append(" ".join(map(str,p)))
x = cx-r
y = cy-r
w = 2*r
h = 2*r
p = ["M",x,y, x+w,y, x+w,y+h, x,y+h, x,y, "Z"]
#paths.append(" ".join(map(str,p)))
return viewBox, paths
def parse_path(path):
commands = []
command = []
word = []
for c in path:
if c in COMMANDS:
if word:
command.append(float("".join(word)))
word = []
if command:
commands.append(command)
command = [c]
elif c in " ,":
if word:
command.append(float("".join(word)))
word = []
elif c in "+-":
if word:
command.append(float("".join(word)))
word = []
word.append(c)
else:
word.append(c)
if word:
command.append(float("".join(word)))
word = []
if command:
commands.append(command)
return commands
def compile_path(commands):
buf = []
for command in commands:
buf.append(command[0])
for n in command[1:]:
buf.append(str(n))
return " ".join(buf)
def do_glyph(data, glyphname, svg, scale=1.0, translate_y=0.0):
""" converts a file into a svg glyph """
local_gsize = GSIZE * scale
viewBox, paths = svg_paths(data)
# font needs to be of one path
path = " ".join(paths)
commands = parse_path(path)
tranx, trany, sizex, sizey = viewBox
tranx = -tranx
trany = -trany
size = max(sizex, sizey)
scale = local_gsize/size
if size - sizey > 0:
trany += (size - sizey)/2
if size - sizex > 0:
tranx += (size - sizex)/2
trany += translate_y
prev_op = None
for command in commands:
op = command[0]
if op in "Aa":
# arcs require special fancy scaling
command[1] *= scale
command[2] *= scale
# presurve flags
command[4] = int(command[4])
command[5] = int(command[5])
# scale the radii
command[6] *= scale
command[7] *= scale
else:
for i,num in enumerate(command):
if num == op: continue
if op in COMMANDS_ABS:
if op == "H":
command[i] *= scale
command[i] += tranx * scale
elif op == "V":
command[i] *= -scale
command[i] += -trany * scale + local_gsize
else:
if i % 2 == 1:
command[i] *= scale
command[i] += tranx * scale
else:
command[i] *= -scale
command[i] += -trany * scale + local_gsize
else:
if op in "h":
command[i] *= scale
elif op in "v":
command[i] *= -scale
else:
if i % 2 == 1:
command[i] *= scale
else:
command[i] *= -scale
# special case for first relative m (its just like abs M)
if op == "m" and prev_op == None:
command[1] += tranx * scale
command[2] += -trany * scale + local_gsize
prev_op = op
path = compile_path(commands)
svg.write(GLYPH.format(glyph_name=glyphname, path=path, horz_size=local_gsize))
def gen_svg_font(
glyph_files, output_path, font_name, get_one_glyph_name,
scale=1.0, translate_y=0.0,
scale_overrides=None, translate_y_overrides=None):
scale_overrides = scale_overrides or {}
translate_y_overrides = translate_y_overrides or {}
svg = open(output_path, 'w')
svg.write(HEADER.format(font_name))
# use the special unicode user area for char encoding
index = 0
for f in glyph_files:
glyph_friendly_name = os.path.splitext(os.path.split(f)[1])[0]
data = open(f).read()
glyph_scale = scale
if glyph_friendly_name in scale_overrides:
glyph_scale = scale_overrides[glyph_friendly_name]
log.debug("Using scale {}={}".format(glyph_friendly_name, glyph_scale))
glyph_translate_y = translate_y
if glyph_friendly_name in translate_y_overrides:
glyph_translate_y = translate_y_overrides[glyph_friendly_name]
log.debug("Using translate Y {}={}".format(glyph_friendly_name, glyph_translate_y))
glyph_name = get_one_glyph_name(glyph_friendly_name, index)
do_glyph(
data, glyph_name, svg, scale=glyph_scale, translate_y=glyph_translate_y)
index += 1
svg.write(FOOTER)
svg.flush()
svg.close()
log.info("Generated {}".format(output_path))
def gen_scss_for_font(glyph_files, output_path, font_name, hash):
scss = open(output_path, 'w')
scss.write(CSS_HEADER.format(font_name, hash))
for index, f in enumerate(glyph_files):
glyph_name = font_name + "-" + f.split("/")[-1].replace(".svg", "")
scss.write(
'.{0}:before {{\n content: "\{1:04x}";\n}}\n'.format(
glyph_name,
USER_AREA + index))
log.info("Generated {}".format(output_path))
def gen_html_for_font(glyph_files, output_path, font_name):
doc = open(output_path,'w')
doc.write(DOC_HEADER.format(font_name))
for index, f in enumerate(glyph_files):
glyph_name = font_name + "-" + f.split("/")[-1].replace(".svg", "")
art_name = chr(ord(DESIGNER_FONT_START_CHAR) + index)
doc.write("<i class='{0}'></i> {0} ({1}) <br/>\n".format(
glyph_name, art_name))
doc.write(DOC_FOOTER.format(font_name))
log.info("Generated {}".format(output_path))
def json_file_arg_type(path):
try:
with open(path, 'r') as f:
return json.load(f)
except IOError:
raise argparse.ArgumentTypeError("File does not exist: {}".format(path))
except ValueError:
raise argparse.ArgumentTypeError("Invalid JSON: {}".format(path))
def parse_args():
"""
All of the boolean options have both an 'on' and an 'off' argument, which
defaults to 'on'. The only reason to have 'on' at all is so that you can
be very explicit about what you want, without worrying about whether the
defaults will change or whether you'll need to override it later.
"""
parser = argparse.ArgumentParser(
description='Convert a folder of SVG files into several font formats')
parser.add_argument(
"input_dir", action="store", help="Path to a folder of SVG files")
parser.add_argument(
"output_dir", action="store", help="Path to a folder to put output files in")
parser.add_argument(
"font_name", default="icon", action="store", help="Output file name *and* font name. Defaults to 'icon'.")
parser.add_argument('--character-mapping', action='store', type=json_file_arg_type, help='JSON file mapping SVG file name to the character its glyph occupies')
parser.add_argument('-v', '--verbose', action='count', help="Add one for info logging, add another for debug logging")
parser.add_argument('--designer', action='store_true', default=True, help='(default) Generate "designer" variant (uses ASCII range instead of user extension range)')
parser.add_argument('--no-designer', action='store_false', dest='designer', help="Don't generate \"designer\" variant")
parser.add_argument('--ignore', action='append', default=[], help="Ignore a glyph. Do not include the .svg extension. Can be used more than once.")
transforms_group = parser.add_argument_group("Transforms", "Translate and scale glyphs")
transforms_group.add_argument("--scale-all", default=1, action='store', type=float, help="Amount by which to scale all glyphs")
transforms_group.add_argument("--translate-y-all", default=0, action='store', type=float, help="Amount by which to offset all glyphs on the Y axis. What are the units? That's for you to find out.")
transforms_group.add_argument(
"--scale-one", nargs=2, metavar=('GLYPH_NAME', 'AMOUNT'), action='append', default=[],
help="'--scale-one airplane 2' would scale the 'airplane' glyph 2x. Can be used more than once. This will _replace_ the global scale value for this glyph.")
transforms_group.add_argument(
"--translate-y-one", nargs=2, metavar=('GLYPH_NAME', 'AMOUNT'), action='append', default=[],
help="'--translate-y-one airplane 5' would move the 'airplane' glyph down 5 units. Can be used more than once. This will _replace_ the global translate-y value for this glyph.")
formats_group = parser.add_argument_group('Formats', 'Enable or disable individual formats. All are enabled by default.')
for fmt in ('scss', 'html', 'woff', 'otf', 'eot'):
formats_group.add_argument('--' + fmt, action='store_true', default=True, help='Generate {} files for fonts'.format(fmt.upper()))
formats_group.add_argument('--no-' + fmt, action='store_false', dest=fmt, help="Don't generate {} files for fonts".format(fmt.upper()))
return parser.parse_args()
def configure_logging(level=0):
log_level = logging.WARNING
if level == 1:
log_level = logging.INFO
elif level > 1:
log_level = logging.DEBUG
logging.basicConfig(format='%(message)s', level=log_level)
def get_glyph_file_paths(input_dir, ignore):
ignore = set(ignore)
glyph_files = []
for f in sorted(os.listdir(input_dir)):
if not f.endswith(".svg"):
continue
glyph_name = os.path.splitext(f)[0]
if glyph_name in ignore:
log.info("Ignoring glyph {}".format(glyph_name))
continue
path = os.path.join(input_dir, f)
glyph_files.append(path)
log.debug("Including file {}".format(path))
return glyph_files
def main():
configure_logging(1) # allow logging during parsing
args = parse_args()
configure_logging(args.verbose)
scale_overrides = {name: float(amt) for (name, amt) in args.scale_one}
translate_y_overrides = {name: float(amt) for (name, amt) in args.translate_y_one}
glyph_name_mapper = GlyphNameMapper(args.character_mapping or {})
log.info("Scale overrides: {}".format(scale_overrides))
log.info("Translate Y overrides: {}".format(translate_y_overrides))
designer_font_name = args.font_name + '-designer'
# make sure output dir exists
try:
os.makedirs(args.output_dir)
except OSError:
pass
glyph_files = get_glyph_file_paths(args.input_dir, args.ignore)
def _get_glyph_name(friendly_name, i):
return htmlhex(ord(glyph_name_mapper.get_glyph_name(friendly_name)))
# generate browser svg font
gen_svg_font(
glyph_files,
os.path.join(args.output_dir, args.font_name + '.svg'),
args.font_name,
get_one_glyph_name=_get_glyph_name,
scale=args.scale_all,
translate_y=args.translate_y_all,
scale_overrides=scale_overrides,
translate_y_overrides=translate_y_overrides
)
if args.scss:
# get file hash
svg_font_hash = md5.new(open(os.path.join(args.output_dir, args.font_name+".svg")).read()).hexdigest()[:5]
# generate scss
gen_scss_for_font(
glyph_files,
os.path.join(args.output_dir, args.font_name + ".scss"),
args.font_name,
svg_font_hash
)
if args.html:
# generate sample html
gen_html_for_font(
glyph_files,
os.path.join(args.output_dir, args.font_name + ".html"),
args.font_name
)
# make ttf, woff, off, and eot browser fonts
font = fontforge.open(os.path.join(args.output_dir, args.font_name + ".svg"))
ttf_path = os.path.join(args.output_dir, args.font_name + ".ttf")
font.generate(ttf_path)
log.info("Generated {}".format(ttf_path))
if args.woff:
path = os.path.join(args.output_dir, args.font_name + ".woff")
font.generate(path)
log.info("Generated {}".format(path))
if args.otf:
path = os.path.join(args.output_dir, args.font_name + ".otf")
font.generate(path)
log.info("Generated {}".format(path))
if args.eot:
eot_path = os.path.join(args.output_dir, args.font_name + '.eot')
os.system("ttf2eot {0} > {1}".format(ttf_path, eot_path))
log.info("Generated {}".format(eot_path))
if args.designer:
def _get_designer_glyph_name(name, i):
return htmlhex(i+ord(DESIGNER_FONT_START_CHAR)),
gen_svg_font(
glyph_files,
os.path.join(args.output_dir, designer_font_name + '.svg'),
designer_font_name,
get_one_glyph_name=_get_designer_glyph_name,
scale=args.scale_all,
translate_y=args.translate_y_all,
scale_overrides=scale_overrides,
translate_y_overrides=translate_y_overrides
)
font = fontforge.open(os.path.join(args.output_dir, designer_font_name + ".svg"))
path = os.path.join(args.output_dir, designer_font_name + ".ttf")
font.generate(path)
log.info("Generated {}".format(path))
glyph_name_mapper.log_new_mappings_if_necessary()
if __name__ == "__main__":
main()