forked from subframe7536/maple-font
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
256 lines (188 loc) · 6.65 KB
/
__init__.py
File metadata and controls
256 lines (188 loc) · 6.65 KB
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
from copy import deepcopy
import json
from source.py.feature import ast
from source.py.feature.base import get_base_feature_cn_only, get_base_features
from source.py.feature.base.lang import get_lang_list
from source.py.feature.calt import get_calt, get_calt_lookup
from source.py.feature.cv import cv96, cv97, cv98, cv99
from source.py.feature.regular import (
cls_var,
cls_hex_letter,
class_list_regular,
cv_list_regular,
ss_list_regular,
)
from source.py.feature.italic import (
class_list_italic,
cv_list_italic,
ss_list_italic,
)
normal_enabled_features = [
"cv01",
"cv02",
"cv33",
"cv34",
"cv35",
"cv36",
"cv61",
"cv62",
"ss05",
"ss06",
"ss07",
"ss08",
]
cv_list_cn = [
cv96.cv96_feat_cn,
cv97.cv97_feat_cn,
cv98.cv98_feat_cn,
cv99.cv99_feat_cn,
]
def generate_fea_string(
is_italic: bool,
is_cn: bool,
is_normal: bool = False,
is_calt: bool = True,
variable_enabled_feature_list: list[str] | None = None,
):
"""
Generates feature string.
For ``variable=True, normal=True``, enabled features are
moved to calt feature to freeze them.
Args:
is_italic (bool): Whether to generate italic features
is_cn (bool): Whether to include Chinese-specific features
is_normal (bool): Whether to generate normal preset
is_calt (bool): Whether to enable calt
variable_enabled_feature_list (list[str]): List of features that
be enabled in variable format
"""
print(
f"Generating feature string with italic={is_italic}, cn={is_cn}, normal={is_normal}, calt={is_calt}, variable={bool(variable_enabled_feature_list)}"
)
class_list = class_list_italic if is_italic else class_list_regular
cv_list = cv_list_italic if is_italic else cv_list_regular
ss_list = ss_list_italic if is_italic else ss_list_regular
if class_list[-2].name != "Var" or class_list[-1].name != "HexLetter":
raise TypeError("Invalid class_list, must ends with [@Var, @HexLetter]")
calt_feat = get_calt(
class_list[-2], class_list[-1], is_italic=is_italic, is_normal=is_normal
)
# clear calt for no ligature
if not is_calt:
calt_feat.content = []
cv_ss_list = deepcopy(cv_list + (cv_list_cn if is_cn else []) + ss_list)
# for variable font, freeze feature by moving it to `calt`
if variable_enabled_feature_list:
extracted_lookup_list = []
for feat in cv_ss_list:
if feat.tag in variable_enabled_feature_list or []:
# prevent features that add ligatures like `ss08`
if not is_calt and feat.has_lookup:
continue
extracted_lookup_list.append(
feat.content
if feat.has_lookup
else [ast.Lookup(f"move_{feat.tag}", None, feat.content)]
)
# cleanup
feat.content = []
calt_feat.content.extend(extracted_lookup_list)
# remove calt if empty, to prevent fonttools warning
if not calt_feat.content:
calt_feat = None
return ast.create(
[
class_list,
get_lang_list(),
get_base_features(calt_feat, is_cn=is_cn),
cv_ss_list,
],
)
def generate_fea_string_cn_only():
return ast.create(
[
get_base_feature_cn_only(),
cv_list_cn,
],
)
def get_all_calt_text():
result = []
for item in ast.recursive_iterate(get_calt_lookup(cls_var, cls_hex_letter, True)):
if isinstance(item, ast.Lookup) and item.desc:
if item.name == "escape":
result.append(item.desc.replace("\\ ", "\\\\ "))
else:
result.append(item.desc)
return "\n".join(result)
zero_desc = "Dot style `0`"
def get_version_info(
features: list[ast.CharacterVariant] | list[ast.StylisticSet],
) -> dict[str, dict[str, str]]:
result = {}
for item in features:
if item.version not in result:
result[item.version] = {}
result[item.version][item.tag] = item.sample
return result
def get_cv_desc():
return "\n".join(
[cv.desc_item() for cv in cv_list_regular] + [f"- [v7.0] zero: {zero_desc}"]
)
def get_cv_version_info() -> dict[str, dict[str, str]]:
return get_version_info(cv_list_regular)
def get_cv_italic_desc():
return "\n".join(
[cv.desc_item() for cv in cv_list_italic if cv.id > 30 and cv.id < 61]
)
def get_cv_italic_version_info() -> dict[str, dict[str, str]]:
return get_version_info([cv for cv in cv_list_italic if cv.id > 30 and cv.id < 61])
def get_cv_cn_desc():
return "\n".join([cv.desc_item() for cv in cv_list_cn])
def get_cv_cn_version_info() -> dict[str, dict[str, str]]:
return get_version_info(cv_list_cn)
def get_ss_desc():
result = {}
for ss in ss_list_regular + ss_list_italic:
if ss.id not in result:
desc = ss.desc_item()
if ss.id == 5:
desc = desc.replace("`\\\\`", "`\\\\\\\\`")
result[ss.id] = desc
return "\n".join(sorted(result.values()))
def get_ss_version_info() -> dict[str, dict[str, str]]:
ss = list({s.tag: s for s in ss_list_regular + ss_list_italic}.values())
return get_version_info(sorted(ss, key=lambda x: x.tag))
__total_feat_list = (
cv_list_regular + cv_list_italic + cv_list_cn + ss_list_regular + ss_list_italic
)
def get_total_feat_dict() -> dict[str, str]:
result = {}
for item in __total_feat_list:
if item.tag not in result:
result[item.tag] = f"[v{item.version}] " + item.desc.replace("`", "'")
result["zero"] = "[v7.0] " + zero_desc.replace("`", "'")
return dict(sorted(result.items()))
def get_total_feat_ts() -> str:
feat_dict = {}
for item in __total_feat_list:
if item.tag not in feat_dict:
feat_dict[item.tag] = item.desc
feat_dict["calt"] = "Default ligatures"
feat_dict["zero"] = zero_desc
feat_dict = dict(sorted(feat_dict.items()))
js_object = "\n"
for key, val in feat_dict.items():
js_object += f" /** {val} */\n {key}: string\n"
return f"""// Auto generated by `python task.py fea`
// @prettier-ignore
/* eslint-disable */
export interface FeatureDescription {{{js_object}}}
export const featureArray = {json.dumps(list(feat_dict.keys()), indent=2)}
export const normalFeatureArray = {json.dumps(normal_enabled_features, indent=2)}
"""
def get_freeze_moving_rules() -> list[str]:
result = set()
for feat in __total_feat_list:
if feat.has_lookup:
result.add(feat.tag)
return list(result)