-
Notifications
You must be signed in to change notification settings - Fork 72
/
build.py
345 lines (276 loc) · 10.5 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
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
from fontTools.ttLib import TTFont, woff2
from afdko.otf2ttf import otf_to_ttf
from os import path, getcwd, makedirs, listdir, remove, walk
from subprocess import run
from zipfile import ZipFile, ZIP_DEFLATED
from urllib.request import urlopen
from ttfautohint import ttfautohint
from enum import Enum, unique
import shutil
import json
import hashlib
import platform
@unique
class Status(Enum):
DISABLE = "0"
ENABLE = "1"
IGNORE = "2"
# whether to archieve fonts
release_mode = True
# whether to build nerd font
build_nerd_font = True
# whether to clear old build before build new
clear_old_build = True
build_config = {
# font family name
"family_name": "Maple Mono",
# whether to enable font features by default
"freeze_feature_list": {
# ======
# ligatures:
# Status.IGNORE: do nothing
# Status.ENABLE: move font features to default ligature
# Status.DISABLE: remove font features
"ss01": Status.IGNORE, # == === != !==
"ss02": Status.IGNORE, # [info] [trace] [debug] [warn] [error] [fatal] [vite]
"ss03": Status.IGNORE, # __
"ss04": Status.IGNORE, # >= <=
"ss05": Status.IGNORE, # {{ }}
# ======
# character variant:
# Status.IGNORE: do nothing
# Status.ENABLE: enable character variants by default
# Status.DISABLE: remove character variants
"cv01": Status.IGNORE, # @ # $ % & Q -> =>
"cv02": Status.IGNORE, # alt i
"cv03": Status.IGNORE, # alt a
"cv04": Status.IGNORE, # alt @
"zero": Status.IGNORE, # alt 0
# ======
},
# config for nerd font
# total config: generate-nerdfont.{bat/sh}:17
"nerd_font": {
"mono": Status.ENABLE, # whether to use half width icon
"use_hinted": Status.ENABLE, # whether to use hinted ttf to generate Nerd Font patch
},
}
root = getcwd()
ttx_path = path.join(root, "ttx")
output_path = path.join(path.dirname(root), "output")
family_name = build_config["family_name"]
family_name_trim = family_name.replace(" ", "")
if not path.exists(path.join(root, "FontPatcher")):
url = "https://github.com/ryanoasis/nerd-fonts/releases/download/v3.0.2/FontPatcher.zip"
print(f"Font Patcher does not exist, download from {url}")
try:
zip_path = path.join(root, "FontPatcher.zip")
if not path.exists(zip_path):
with urlopen(url) as response, open(zip_path, "wb") as out_file:
shutil.copyfileobj(response, out_file)
with ZipFile(zip_path, "r") as zip_ref:
zip_ref.extractall(path.join(root, "FontPatcher"))
remove(zip_path)
except Exception as e:
print(
f"fail to download Font Patcher, please consider to download it manually, put downloaded 'FontPatcher.zip' in the 'source' folder and run this script again. Error: {e}"
)
exit(1)
def mkdirs(dir):
if not path.exists(dir):
makedirs(dir)
if clear_old_build and path.exists(output_path):
shutil.rmtree(output_path)
mkdirs(path.join(output_path, "otf"))
mkdirs(path.join(output_path, "ttf"))
mkdirs(path.join(output_path, "ttf-autohint"))
mkdirs(path.join(output_path, "woff2"))
def auto_hint(f: str, ttf_path: str):
ttfautohint(
in_file=ttf_path,
out_file=path.join(output_path, "ttf-autohint", f + ".ttf"),
)
def make_sure_eol(file_path: str):
if path.exists(file_path) and file_path.endswith("bat"):
with open(file_path, "r+", encoding="utf-8") as f:
content = f.read()
f.seek(0)
f.write(content.replace(r"(?<!\r)\n", "\r\n"))
f.truncate()
def generate_nerd_font(f: str, f_ttx: str):
if not build_nerd_font:
return
system = platform.uname()[0]
script = path.join(
root, f"generate-nerdfont{'-mac' if 'Darwin' in system else ''}.{'bat' if 'Windows' in system else 'sh'}"
)
make_sure_eol(script)
run(
[
script,
f,
build_config["nerd_font"]["mono"].value,
build_config["nerd_font"]["use_hinted"].value,
]
)
_, sub = f.split("-")
mono = "Mono" if build_config["nerd_font"]["mono"] == Status.ENABLE else ""
nf_path = path.join(
output_path,
"NF",
f"{family_name_trim}NerdFont{mono}-{sub}.ttf",
)
# load font
nf_font = TTFont(nf_path)
def set_name(name: str, id: int):
nf_font["name"].setName(
name, nameID=id, platformID=3, platEncID=1, langID=0x409
)
nf_font["name"].setName(name, nameID=id, platformID=1, platEncID=0, langID=0x0)
def get_name(id: int):
return nf_font["name"].getName(nameID=id, platformID=3, platEncID=1)
def del_name(id: int):
nf_font["name"].removeNames(nameID=id)
# correct names
set_name(f"{family_name} NF", 1)
set_name(sub, 2)
set_name(f"{family_name} NF {sub}; {get_name(5)}", 3)
set_name(f"{family_name} NF {sub}", 4)
set_name(f"{family_name_trim}NF-{sub}", 6)
# remove additional names
del_name(16)
del_name(17)
del_name(18)
del_name(20)
nf_font.importXML(path.join(ttx_path, f_ttx, f_ttx + ".O_S_2f_2.ttx"))
# save font
nf_font.save(path.join(output_path, "NF", f"{family_name_trim}-NF-{sub}.ttf"))
nf_font.close()
# remove original font
remove(nf_path)
print("=== [build start] ===")
conf = json.dumps(
build_config,
default=lambda x: x.name if isinstance(x, Status) else None,
indent=4,
)
print(conf)
for f in listdir(ttx_path):
# load font
font = TTFont()
font.importXML(fileOrPath=path.join(root, "ttx", f, f + ".ttx"))
# check feature list
feature_record = font["GSUB"].table.FeatureList.FeatureRecord
feature_dict = {feature.FeatureTag: feature.Feature for feature in feature_record}
calt_lookup_list = feature_dict.get("calt").LookupListIndex
def replace_glyph(old_key: str, new_key: str):
cff_dict = font["CFF "].cff.values()[0].CharStrings.charStrings
hmtx_dict = font["hmtx"].metrics
if not (
old_key in cff_dict
and old_key in hmtx_dict
and new_key in cff_dict
and new_key in hmtx_dict
):
print(f"{old_key} or {new_key} does not exist")
return
else:
cff_dict[old_key] = cff_dict[new_key]
hmtx_dict[old_key] = hmtx_dict[new_key]
for key, feat in feature_dict.items():
if key == "calt":
continue
status = build_config["freeze_feature_list"][key]
if status == Status.IGNORE:
continue
if status == Status.DISABLE:
# clear lookup list
feat.LookupListIndex = []
elif key.startswith("ss"):
# to freeze styleset, target lookup list should be push into calt's lookup list
calt_lookup_list.extend(feat.LookupListIndex)
else:
# to freeze character variants, apply the replacement of pair that defined in lookup list in cff table and hmtx table
for index in feat.LookupListIndex:
dict = font["GSUB"].table.LookupList.Lookup[index].SubTable[0].mapping
for k, v in dict.items():
replace_glyph(k, v)
# correct names
_, sub = f.split("-")
current_family = f"{family_name_trim}-{sub}"
# correct names
def set_name(name: str, id: int):
font["name"].setName(name, nameID=id, platformID=3, platEncID=1, langID=0x409)
def get_name(id: int):
font["name"].getName(nameID=id, platformID=3, platEncID=1)
set_name(family_name, 1)
set_name(sub, 2)
set_name(f"{family_name} {sub}; {get_name(5)}", 3)
set_name(f"{family_name} {sub}", 4)
set_name(current_family, 6)
otf_path = path.join(output_path, "otf", f"{current_family}.otf")
ttf_path = path.join(output_path, "ttf", f"{current_family}.ttf")
# save otf font
font.save(otf_path)
# save ttf font
otf_to_ttf(font)
font.save(ttf_path)
# auto hint
auto_hint(current_family, ttf_path)
font.close()
# generate nerd font
generate_nerd_font(current_family, f)
# generate woff2
woff2.compress(otf_path, path.join(output_path, "woff2", f"{current_family}.woff2"))
print("generated:", current_family)
# check whether have script to generate sc
sc_path = path.join(
root,
f"generate-sc.bat",
)
if path.exists(sc_path):
make_sure_eol(sc_path)
run([sc_path, family_name])
# compress folder and return sha1
def compress_folder(source_folder_path, target_path):
source_folder_name = path.basename(source_folder_path)
zip_path = path.join(target_path, f"{family_name_trim}-{source_folder_name}.zip")
with ZipFile(zip_path, "w", compression=ZIP_DEFLATED, compresslevel=5) as zip_file:
for root, dirs, files in walk(source_folder_path):
for file in files:
file_path = path.join(root, file)
zip_file.write(file_path, path.relpath(file_path, source_folder_path))
zip_file.close()
sha1 = hashlib.sha1()
with open(zip_path, "rb") as zip_file:
while True:
data = zip_file.read(1024)
if not data:
break
sha1.update(data)
return sha1.hexdigest()
# write config to output path
with open(path.join(output_path, "build-config.json"), "w", encoding="utf-8") as config_file:
config_file.write(conf)
if release_mode:
print("=== [Release Mode] ===")
# archieve fonts
mkdirs(path.join(output_path, "release"))
hash_map = {}
for f in listdir(output_path):
if f == "release" or f.endswith(".json"):
continue
zip_path = path.join(output_path, "release")
target_path = path.join(output_path, f)
hash_map[f] = compress_folder(target_path, zip_path)
# write config
print("archieve:", f)
# write sha1
with open(path.join(output_path, "release", "sha1.json"), "w", encoding="utf-8") as hash_file:
hash_file.write(json.dumps(hash_map, indent=4))
# copy woff
woff2_path = path.join(path.dirname(output_path), "woff2")
if path.exists(woff2_path):
shutil.rmtree(woff2_path)
shutil.copytree(path.join(output_path, "woff2"), woff2_path)
print("copy woff to root")