forked from microsoft/cascadia-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
196 lines (151 loc) · 6.03 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
from pathlib import Path
import argparse
import fontmake.instantiator
import fontTools.designspaceLib
import ufo2ft
import ufoLib2
import vttLib
import sys
import subprocess
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}] 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 build_font_instance(generator, instance_descriptor, *steps):
for format in ["ttf","otf"]:
instance = generator.generate_instance(instance_descriptor)
for step in steps:
step(instance)
instance.info.openTypeOS2Panose = [2, 11, 6, 9, 2, 0, 0, 2, 0, 4]
instance.info.openTypeOS2TypoAscender = 1900
instance.info.openTypeOS2TypoDescender = -480
instance.info.openTypeOS2TypoLineGap = 0
instance.info.openTypeHheaAscender = instance.info.openTypeOS2TypoAscender
instance.info.openTypeHheaDescender = instance.info.openTypeOS2TypoDescender
instance.info.openTypeHheaLineGap = instance.info.openTypeOS2TypoLineGap
instance.info.openTypeOS2WinAscent = 2226
instance.info.openTypeOS2WinDescent = 770
if format == "ttf":
instance.info.openTypeGaspRangeRecords =[
{
"rangeMaxPPEM" : 9,
"rangeGaspBehavior" : [1,3]
},
{
"rangeMaxPPEM" : 50,
"rangeGaspBehavior" : [0,1,2,3]
},
{
"rangeMaxPPEM" : 65535,
"rangeGaspBehavior" : [1,3]
},
]
familyName = instance.info.familyName
file_stem = instance.info.familyName.replace(" ", "")
file_path = (OUTPUT_DIR / file_stem).with_suffix(f".{format}")
print(f"[{familyName}] Compiling")
if format == "ttf":
instance_font = ufo2ft.compileTTF(instance, removeOverlaps=True, inplace=True)
else:
instance_font = ufo2ft.compileOTF(instance, removeOverlaps=True, inplace=True)
if format == "ttf":
print(f"[{familyName}] Merging VTT")
vttLib.transfer.merge_from_file(instance_font, VTT_DATA_FILE)
print(f"[{familyName}] Saving")
instance_font.save(file_path)
print(f"[{familyName}] Done: {file_path}")
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")
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-Regular.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_remove_ligatures = step_set_feature_file(
INPUT_DIR / "features" / "features.fea"
)
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"
)
for instance_descriptor in designspace.instances:
build_font_instance(generator, instance_descriptor)
if not args.no_mono:
build_font_instance(
generator,
instance_descriptor,
step_set_font_name("Cascadia Mono"),
step_remove_ligatures,
)
if not args.no_powerline:
build_font_instance(
generator,
instance_descriptor,
step_set_font_name("Cascadia Code PL"),
step_merge_pl,
)
if not args.no_mono:
build_font_instance(
generator,
instance_descriptor,
step_set_font_name("Cascadia Mono PL"),
step_remove_ligatures,
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("Autohinting OTFs")
for file in Path("build").glob("*.otf"):
subprocess.run(['psautohint --log "build/log.txt" '+str(file)], shell=True)
print("All done")
print("*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***")