-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
127 lines (115 loc) · 4.31 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
#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p "python3.withPackages (ps: with ps; [ ])"
import json
import os
import re
weight_map = {
"ExtraLight": 200,
"Light": 300,
"Regular": 400,
"Medium": 500,
"SemiBold": 600,
"Bold": 700,
"Heavy": 900,
}
font_map = {
"思源宋体": {
"name": "SourceHanSerifSC-VF",
"path": "./source-han-serif/Variable/TTF/SourceHanSerifSC-VF.ttf",
"type": "file",
"weight": "100 200 300 400 500 600 700 800 900",
},
"更纱黑体": {"name": "SarasaUiSC", "path": "./sarasa-ui", "type": "dir"},
"霞骛新晰黑": {
"name": "LXGWNewXiHei",
"path": "./LXGWNeoXiHei.ttf",
"type": "file",
},
"霞骛文楷": {"name": "LXGWWenKai", "path": "./LXGWWenKai", "type": "dir"},
"新晰黑 Code": {
"name": "NeoXiHeiCode-Regular",
"path": "./NeoXiHeiCode-Regular.ttf",
"type": "file",
},
"黑体": {"name": "sans", "type": "builtin"},
"宋体": {"name": "serif", "type": "builtin"},
"默认字体": {"name": "", "type": "builtin"},
}
def process(dir):
paths: list[str] = []
for item in os.scandir(dir):
if not item.is_file() or not (
item.name.endswith(".otf") or item.name.endswith(".ttf")
):
continue
name = item.name.split(".")[0]
print(f"Processing {name}...")
weight_name = name.split("-")[-1].replace("Italic", "")
if weight_name not in weight_map:
print(f"Unknown weight: {weight_name}")
weight_name = "Regular"
weight = weight_map[weight_name]
print(f"{{ name = {name}\tweight = {weight}\n }}")
os.system(
f"cn-font-split -i {item.path} --font-family {name.split("-")[0]} --font-weight {weight} --out-dir ./result/{name}"
)
paths.append(name)
return paths
class FontMapItem:
fontFamily: str
paths: list[str]
def __init__(self, paths: list[str], font_family: str):
self.paths = paths
self.fontFamily = font_family
class FontMapItemEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, FontMapItem):
return o.__dict__
return super().default(o)
if __name__ == "__main__":
path_map: dict[str, FontMapItem] = {}
for name, info in font_map.items():
if info["type"] == "builtin":
path_map[name] = FontMapItem(paths=[], font_family=info["name"])
elif info["type"] == "file":
outdir = f'./result/{info["name"]}'
os.system(
f'cn-font-split -i {info["path"]} --font-weight "{info["weight"] if "weight" in info else "400"}" --out-dir {outdir}'
)
css = open(f"{outdir}/result.css", "r", encoding="utf-8").read()
font_family = re.search(r"(?<=font-family:\").*(?=\";)", css).group()
path_map[name] = FontMapItem(paths=[info["name"]], font_family=font_family)
elif info["type"] == "dir":
result = process(info["path"])
for item in result:
if item.find("Mono") == -1:
if name in path_map:
path_map[name].paths.append(item)
else:
css = open(
f"./result/{item}/result.css", "r", encoding="utf-8"
).read()
font_family = re.search(
r"(?<=font-family:\").*(?=\";)", css
).group()
path_map[name] = FontMapItem(
paths=[item], font_family=font_family
)
elif f"{name} Mono" in path_map:
path_map[f"{name} Mono"].paths.append(item)
else:
css = open(
f"./result/{item}/result.css", "r", encoding="utf-8"
).read()
font_family = re.search(
r"(?<=font-family:\").*(?=\";)", css
).group()
path_map[f"{name} Mono"] = FontMapItem(
paths=[item], font_family=font_family
)
json.dump(
path_map,
open("./result/path_map.json", "w"),
ensure_ascii=False,
cls=FontMapItemEncoder,
)