-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate_font.py
executable file
·151 lines (132 loc) · 4.29 KB
/
generate_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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# generate_font.py
# generates FONTLOG.txt, OpenType font, and the font metadata JSON
import json
import sys
import fontforge
def validateGlyph(glyph):
result = g.validate(True)
report = ''
if result & 0x1 or True:
if result & 0x2:
report += '- OPEN CONTOUR'
if result & 0x4:
report += '- INTERSECTION'
if result & 0x8:
report += '- WRONG DIRECTION'
if result & 0x20:
report += '- missing extrema'
if result & 0x80:
report += '- too many points'
# if result & 0x200:
# report += '- invalid glyph name'
if result & 0x40000:
report += '- points too far apart'
if result & 0x200000:
report += '- DUPLICATE GLYPH NAME'
if result & 0x400000:
report += '- DUPLICATE UNICODE POINT'
if report != '':
print('%s (%#0.2x) issues: %s' %
(g.glyphname, glyph.unicode, report))
return report
nargs = len(sys.argv)
fontFileName = ''
fontDir = ''
outDir = ''
if nargs != 2:
fontFileName = 'Leipzig.sfd'
path = fontFileName
if fontFileName == '':
fontFileName = sys.argv[1]
path = fontFileName
try:
font = fontforge.open(path)
except EnvironmentError:
print("Can't open font file %s" % fontFileName)
sys.exit(1)
# generating OpenType font:
print("Generating OpenType font")
font.generate(font.fontname+'.otf', '', 'opentype')
print("Generating TrueType font")
font.generate(font.fontname+'.ttf')
print("Generating WOFF2 font")
font.generate(font.fontname+'.woff2')
print("Generating SVG font")
font.generate(font.fontname+'.svg')
metadata = {'fontName': font.fontname, 'fontVersion': font.version}
# extracting font log
print("Extracting font log")
fontlog = open('FONTLOG.txt', 'w+')
n = fontlog.write(font.fontlog)
fontlog.close()
# Leipzig's engraving defaults:
engravingDefaults = {
'arrowShaftThickness': 0.16,
'barlineSeparation': 0.4,
'beamSpacing': 0.25,
'beamThickness': 0.5,
'bracketThickness': 0.5,
'dashedBarlineDashLength': 0.5,
'dashedBarlineGapLength': 0.25,
'dashedBarlineThickness': 0.16,
'hairpinThickness': 0.16,
'legerLineExtension': 0.27,
'legerLineThickness': 0.16,
'lyricLineThickness': 0.16,
'octaveLineThickness': 0.16,
'pedalLineThickness': 0.16,
'repeatBarlineDotSeparation': 0.18,
'repeatEndingLineThickness': 0.16,
'slurEndpointThickness': 0.1,
'slurMidpointThickness': 0.22,
'staffLineThickness': 0.08,
'stemThickness': 0.076,
'subBracketThickness': 0.16,
'textEnclosureThickness': 0.16,
'thickBarlineThickness': 0.5,
'thinBarlineThickness': 0.15,
'tieEndpointThickness': 0.1,
'tieMidpointThickness': 0.22,
'tupletBracketThickness': 0.16,
'hBarThickness': 1.0
}
metadata["engravingDefaults"] = engravingDefaults
with open('glyphnames.json') as smuflGlyphnamesFile:
smuflGlyphnames = json.load(smuflGlyphnamesFile)
codepoint_to_name = { data['codepoint']: name for name, data in smuflGlyphnames.items() }
glyphBBoxes = dict()
glyphsWithAnchors = dict()
count = 0
undefCount = 0
for glyph in font:
g = font[glyph]
try:
g.glyphname = codepoint_to_name["U+" + hex(g.unicode)[2:].upper()]
except KeyError:
# glyph not in SMuFL range
continue
if g.unicode != -1 and g.unicode > 31:
count += 1
validateGlyph(g)
g.addExtrema()
bbox = [round(bb / 250., 4) for bb in g.boundingBox()]
bBoxSW = (bbox[0], bbox[1])
bBoxNE = (bbox[2], bbox[3])
glyphBBoxes[g.glyphname] = {'bBoxNE': bBoxNE, 'bBoxSW': bBoxSW}
if len(g.anchorPoints) > 0:
anchors = dict()
for item in g.anchorPoints:
anchors[item[0]] = (round(item[2] / 250., 4),
round(item[3] / 250., 4))
glyphsWithAnchors[g.glyphname] = anchors
elif g.unicode == -1:
undefCount += 1
metadata["glyphBBoxes"] = glyphBBoxes
metadata["glyphsWithAnchors"] = glyphsWithAnchors
output = json.dumps(metadata, sort_keys=True, indent=4, separators=(',', ': '))
jsonFileName = outDir + font.fontname.lower() + "_metadata.json"
with open(jsonFileName, "w") as outfile:
outfile.write(output)
font.close()