forked from microsoft/cascadia-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
317 lines (248 loc) · 10.3 KB
/
build.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
from pathlib import Path
import argparse
import os
import fontmake.instantiator
import fontTools.designspaceLib
import ufo2ft
import ufoLib2
import vttLib
import sys
import subprocess
from statmake import lib, classes
INPUT_DIR = Path("sources")
OUTPUT_DIR = Path("build")
VTT_DATA_FILE = INPUT_DIR / "vtt_data" / "CascadiaCode.ttx"
def step_set_font_name(n):
def _set(instance):
instance.info.familyName = n
# We have to change the style map family name because that's what
# Windows uses to map Bold/Regular/Medium/etc. fonts
instance.info.styleMapFamilyName = n
return _set
def step_merge_glyphs_from_ufo(path):
def _merge(instance):
ufo = ufoLib2.Font.open(path)
print(f"[{instance.info.familyName} {instance.info.styleName}] Merging {path}")
for glyph in ufo.glyphOrder:
if glyph not in instance.glyphOrder:
instance.addGlyph(ufo[glyph])
return _merge
def step_set_feature_file(n):
fea = n.read_text()
def _set(instance):
instance.features.text = fea
return _set
def set_font_metaData(font, sort):
font.info.versionMajor = 2007
font.info.versionMinor = 1
font.info.openTypeOS2Panose = [2, 11, 6, 9, 2, 0, 0, 2, 0, 4]
font.info.openTypeOS2TypoAscender = 1900
font.info.openTypeOS2TypoDescender = -480
font.info.openTypeOS2TypoLineGap = 0
font.info.openTypeHheaAscender = font.info.openTypeOS2TypoAscender
font.info.openTypeHheaDescender = font.info.openTypeOS2TypoDescender
font.info.openTypeHheaLineGap = font.info.openTypeOS2TypoLineGap
font.info.openTypeOS2WinAscent = 2226
font.info.openTypeOS2WinDescent = abs(font.info.openTypeOS2TypoDescender)
if sort != "otf":
font.info.openTypeGaspRangeRecords =[
{
"rangeMaxPPEM" : 9,
"rangeGaspBehavior" : [1,3]
},
{
"rangeMaxPPEM" : 50,
"rangeGaspBehavior" : [0,1,2,3]
},
{
"rangeMaxPPEM" : 65535,
"rangeGaspBehavior" : [1,3]
},
]
def build_font_instance(generator, instance_descriptor, *steps):
for format in ["otf"]: # removed TTF from here
instance = generator.generate_instance(instance_descriptor)
for step in steps:
step(instance)
set_font_metaData(instance, format)
familyName = instance.info.familyName
fontName = familyName +" "+instance.info.styleName
file_stem = instance.info.familyName.replace(" ", "")
file_path = (OUTPUT_DIR / str(file_stem+"-"+instance.info.styleName)).with_suffix(f".{format}")
print(f"[{fontName}] Compiling")
if format == "ttf":
instance_font = ufo2ft.compileTTF(instance, removeOverlaps=True, inplace=True)
else:
# Do not optimize, because we have to do it again after autohinting.
instance_font = ufo2ft.compileOTF(
instance,
removeOverlaps=True,
inplace=True,
optimizeCFF=ufo2ft.CFFOptimization.NONE
)
if format == "ttf":
print(f"[{fontName}] Merging VTT")
vttLib.transfer.merge_from_file(instance_font, VTT_DATA_FILE)
print(f"[{fontName}] Saving")
instance_font.save(file_path)
print(f"[{fontName}] Done: {file_path}")
def build_variable_fonts(designspace, *steps):
sourceFonts = [ufoLib2.Font.open(INPUT_DIR / designspace.sources[0].filename), ufoLib2.Font.open(INPUT_DIR / designspace.sources[1].filename), ufoLib2.Font.open(INPUT_DIR / designspace.sources[2].filename)]
for source in sourceFonts:
set_font_metaData(source, "var")
designspace.sources[0].font = sourceFonts[0] #ExtraLight
designspace.sources[1].font = sourceFonts[1] #Regular
designspace.sources[2].font = sourceFonts[2] #Bold
for font in sourceFonts:
for step in steps:
step(font)
familyName = sourceFonts[1].info.familyName
file_stem = sourceFonts[1].info.familyName.replace(" ", "")
file_path = (OUTPUT_DIR / file_stem).with_suffix(f".ttf")
print(f"[{familyName}] Compiling")
varFont = ufo2ft.compileVariableTTF(designspace)
print(f"[{familyName}] Adding STAT table")
styleSpace = classes.Stylespace.from_file(INPUT_DIR / "STAT.plist")
lib.apply_stylespace_to_variable_font(styleSpace,varFont,{})
print(f"[{familyName}] Merging VTT")
vttLib.transfer.merge_from_file(varFont, VTT_DATA_FILE)
print(f"[{familyName}] Saving")
varFont.save(file_path)
print(f"[{familyName}] Done: {file_path}")
print(f"[{familyName}] Compiling CFF2")
file_path_cff2 = (OUTPUT_DIR / file_stem).with_suffix(f".otf")
#Do not optimize, because we have to do it again after autohinting.
varFontCFF2 = ufo2ft.compileVariableCFF2(designspace,
inplace=True,
useProductionNames=True,
optimizeCFF=ufo2ft.CFFOptimization.NONE,
)
print(f"[{familyName}] Adding STAT table")
styleSpace = classes.Stylespace.from_file(INPUT_DIR / "STAT.plist")
lib.apply_stylespace_to_variable_font(styleSpace,varFontCFF2,{})
print(f"[{familyName}] Saving")
varFontCFF2.save(file_path_cff2)
print(f"[{familyName}] Done: {file_path_cff2}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="build some fonts")
parser.add_argument("-N", "--no-nerdfonts", default=False, action="store_true")
parser.add_argument("-P", "--no-powerline", default=False, action="store_true")
parser.add_argument("-M", "--no-mono", default=False, action="store_true")
parser.add_argument("-S", "--static-fonts", default=False, action="store_true")
args = parser.parse_args()
# 1. Load Designspace and filter out instances that are marked as non-exportable.
designspace = fontTools.designspaceLib.DesignSpaceDocument.fromfile(
INPUT_DIR / "CascadiaCode.designspace"
)
designspace.instances = [
s
for s in designspace.instances
if s.lib.get("com.schriftgestaltung.export", True)
]
# 2. Prepare masters.
generator = fontmake.instantiator.Instantiator.from_designspace(designspace)
OUTPUT_DIR.mkdir(exist_ok=True)
step_merge_pl = step_merge_glyphs_from_ufo(
INPUT_DIR / "nerdfonts" / "NerdfontsPL.ufo"
)
nf_path = INPUT_DIR / "nerdfonts" / "NerdfontsNF.ufo"
if not nf_path.exists():
args.no_nerdfonts = True # No NF = don't try to build those fonts.
step_merge_nf = None
if not args.no_nerdfonts:
step_merge_nf = step_merge_glyphs_from_ufo(
INPUT_DIR / "nerdfonts" / "NerdfontsNF.ufo"
)
if args.static_fonts == True:
for instance_descriptor in designspace.instances:
build_font_instance(
generator,
instance_descriptor,
step_set_feature_file(INPUT_DIR / "features" / "features_code.fea"),
)
if not args.no_mono:
build_font_instance(
generator,
instance_descriptor,
step_set_font_name("Cascadia Mono"),
step_set_feature_file(INPUT_DIR / "features" / "features_mono.fea"),
)
if not args.no_powerline:
build_font_instance(
generator,
instance_descriptor,
step_set_font_name("Cascadia Code PL"),
step_set_feature_file(INPUT_DIR / "features" / "features_code_PL.fea"),
step_merge_pl,
)
if not args.no_mono:
build_font_instance(
generator,
instance_descriptor,
step_set_font_name("Cascadia Mono PL"),
step_set_feature_file(INPUT_DIR / "features" / "features_mono_PL.fea"),
step_merge_pl,
)
if not args.no_nerdfonts:
build_font_instance(
generator,
instance_descriptor,
step_set_font_name("Cascadia Code NF"),
step_merge_nf,
)
if not args.no_mono:
build_font_instance(
generator,
instance_descriptor,
step_set_font_name("Cascadia Mono NF"),
step_remove_ligatures,
step_merge_nf,
)
print ("*** *** *** Building Variable Fonts *** *** ***")
build_variable_fonts(
designspace,
step_set_feature_file(INPUT_DIR / "features" / "features_code.fea"),
)
if not args.no_mono:
build_variable_fonts(
designspace,
step_set_font_name("Cascadia Mono"),
step_set_feature_file(INPUT_DIR / "features" / "features_mono.fea"),
)
if not args.no_powerline:
build_variable_fonts(
designspace,
step_set_font_name("Cascadia Code PL"),
step_set_feature_file(INPUT_DIR / "features" / "features_code_PL.fea"),
step_merge_pl,
)
if not args.no_mono:
build_variable_fonts(
designspace,
step_set_font_name("Cascadia Mono PL"),
step_set_feature_file(INPUT_DIR / "features" / "features_mono_PL.fea"),
step_merge_pl,
)
if not args.no_nerdfonts:
build_variable_fonts(
designspace,
step_set_font_name("Cascadia Code NF"),
step_merge_nf,
)
if not args.no_mono:
build_variable_fonts(
designspace,
step_set_font_name("Cascadia Mono NF"),
step_remove_ligatures,
step_merge_nf,
)
otfs = list(Path("build").glob("*.otf"))
if otfs:
for otf in otfs:
path = os.fspath(otf)
print(f"Autohinting {path}")
subprocess.check_call(["psautohint", "--log", "build/log.txt", path])
print(f"Compressing {path}")
subprocess.check_call(["python", "-m", "cffsubr", "-i", path])
print("All done")
print("*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***")