-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
fonttools_script.py
279 lines (240 loc) · 8.4 KB
/
fonttools_script.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
#!/bin/env python3
import configparser
import glob
import os
import sys
import xml.etree.ElementTree as ET
from pathlib import Path
from fontTools import merge, ttLib, ttx
from ttfautohint import options, ttfautohint
# iniファイルを読み込む
settings = configparser.ConfigParser()
settings.read("build.ini", encoding="utf-8")
FONT_NAME = settings.get("DEFAULT", "FONT_NAME")
FONTFORGE_PREFIX = settings.get("DEFAULT", "FONTFORGE_PREFIX")
FONTTOOLS_PREFIX = settings.get("DEFAULT", "FONTTOOLS_PREFIX")
BUILD_FONTS_DIR = settings.get("DEFAULT", "BUILD_FONTS_DIR")
HALF_WIDTH_12 = int(settings.get("DEFAULT", "HALF_WIDTH_12"))
FULL_WIDTH_35 = int(settings.get("DEFAULT", "FULL_WIDTH_35"))
WIDTH_35_STR = settings.get("DEFAULT", "WIDTH_35_STR")
CONSOLE_STR = settings.get("DEFAULT", "CONSOLE_STR")
def main():
# 第一引数を取得
# 特定のバリエーションのみを処理するための指定
specific_variant = sys.argv[1] if len(sys.argv) > 1 else None
edit_fonts(specific_variant)
def edit_fonts(specific_variant: str):
"""フォントを編集する"""
if specific_variant is None:
specific_variant = ""
# ファイルをパターンで指定
file_pattern = f"{FONTFORGE_PREFIX}{FONT_NAME}{specific_variant}*-eng.ttf"
filenames = glob.glob(f"{BUILD_FONTS_DIR}/{file_pattern}")
# ファイルが見つからない場合はエラー
if len(filenames) == 0:
print(f"Error: {file_pattern} not found")
return
paths = [Path(f) for f in filenames]
for path in paths:
print(f"edit {str(path)}")
style = path.stem.split("-")[1]
variant = path.stem.split("-")[0].replace(f"{FONTFORGE_PREFIX}{FONT_NAME}", "")
add_hinting(str(path), str(path).replace(".ttf", "-hinted.ttf"), variant, style)
merge_fonts(style, variant)
fix_font_tables(style, variant)
# 一時ファイルを削除
# スタイル部分以降はワイルドカードで指定
for filename in glob.glob(
f"{BUILD_FONTS_DIR}/{FONTTOOLS_PREFIX}{FONT_NAME}{specific_variant}*"
):
os.remove(filename)
for filename in glob.glob(
f"{BUILD_FONTS_DIR}/{FONTFORGE_PREFIX}{FONT_NAME}{specific_variant}*"
):
os.remove(filename)
def add_hinting(input_font_path, output_font_path, variant, style):
"""フォントにヒンティングを付ける"""
if "Italic" not in style:
width_variant = "35" if WIDTH_35_STR in variant else "normal"
ctrl_file = [
"-m",
f"hinting_post_process/{width_variant}-{style}-ctrl.txt",
]
else:
ctrl_file = []
args = ctrl_file + [
"-l",
"6",
"-r",
"45",
"-D",
"latn",
"-f",
"none",
"-S",
"-W",
"-X",
"13-",
"-I",
input_font_path,
output_font_path,
]
options_ = options.parse_args(args)
print("exec hinting", options_)
ttfautohint(**options_)
def merge_fonts(style, variant):
"""フォントを結合する"""
eng_font_path = f"{BUILD_FONTS_DIR}/{FONTFORGE_PREFIX}{FONT_NAME}{variant}-{style}-eng-hinted.ttf"
jp_font_path = (
f"{BUILD_FONTS_DIR}/{FONTFORGE_PREFIX}{FONT_NAME}{variant}-{style}-jp.ttf"
)
# vhea, vmtxテーブルを削除
jp_font_object = ttLib.TTFont(jp_font_path)
if "vhea" in jp_font_object:
del jp_font_object["vhea"]
if "vmtx" in jp_font_object:
del jp_font_object["vmtx"]
jp_font_object.save(jp_font_path)
# フォントを結合
merger = merge.Merger()
merged_font = merger.merge([eng_font_path, jp_font_path])
merged_font.save(
f"{BUILD_FONTS_DIR}/{FONTTOOLS_PREFIX}{FONT_NAME}{variant}-{style}_merged.ttf"
)
def fix_font_tables(style, variant):
"""フォントテーブルを編集する"""
input_font_name = f"{FONTTOOLS_PREFIX}{FONT_NAME}{variant}-{style}_merged.ttf"
output_name_base = f"{FONTTOOLS_PREFIX}{FONT_NAME}{variant}-{style}"
completed_name_base = f"{FONT_NAME.replace(' ', '')}{variant}-{style}"
# OS/2, post テーブルのみのttxファイルを出力
xml = dump_ttx(input_font_name, output_name_base)
# OS/2 テーブルを編集
fix_os2_table(xml, style, flag_35=WIDTH_35_STR in variant)
# post テーブルを編集
fix_post_table(xml, flag_35=WIDTH_35_STR in variant)
# name テーブルを編集
fix_name_table(xml)
# ttxファイルを上書き保存
xml.write(
f"{BUILD_FONTS_DIR}/{output_name_base}.ttx",
encoding="utf-8",
xml_declaration=True,
)
# ttxファイルをttfファイルに適用
ttx.main(
[
"-o",
f"{BUILD_FONTS_DIR}/{output_name_base}_os2_post.ttf",
"-m",
f"{BUILD_FONTS_DIR}/{input_font_name}",
f"{BUILD_FONTS_DIR}/{output_name_base}.ttx",
]
)
# ファイル名を変更
os.rename(
f"{BUILD_FONTS_DIR}/{output_name_base}_os2_post.ttf",
f"{BUILD_FONTS_DIR}/{completed_name_base}.ttf",
)
def dump_ttx(input_name_base, output_name_base) -> ET:
"""OS/2, post テーブルのみのttxファイルを出力"""
ttx.main(
[
"-t",
"OS/2",
"-t",
"post",
"-t",
"name",
"-f",
"-o",
f"{BUILD_FONTS_DIR}/{output_name_base}.ttx",
f"{BUILD_FONTS_DIR}/{input_name_base}",
]
)
return ET.parse(f"{BUILD_FONTS_DIR}/{output_name_base}.ttx")
def fix_os2_table(xml: ET, style: str, flag_35: bool = False):
"""OS/2 テーブルを編集する"""
# xAvgCharWidthを編集
# タグ形式: <xAvgCharWidth value="1000"/>
if flag_35:
x_avg_char_width = FULL_WIDTH_35
else:
x_avg_char_width = HALF_WIDTH_12
xml.find("OS_2/xAvgCharWidth").set("value", str(x_avg_char_width))
# fsSelectionを編集
# タグ形式: <fsSelection value="00000000 11000000" />
# スタイルに応じたビットを立てる
fs_selection = None
if style == "Regular":
fs_selection = "00000001 01000000"
elif style == "Italic":
fs_selection = "00000001 00000001"
elif style == "Bold":
fs_selection = "00000001 00100000"
elif style == "BoldItalic":
fs_selection = "00000001 00100001"
if fs_selection is not None:
xml.find("OS_2/fsSelection").set("value", fs_selection)
# panoseを編集
# タグ形式:
# <panose>
# <bFamilyType value="2" />
# <bSerifStyle value="11" />
# <bWeight value="6" />
# <bProportion value="9" />
# <bContrast value="6" />
# <bStrokeVariation value="3" />
# <bArmStyle value="0" />
# <bLetterForm value="2" />
# <bMidline value="0" />
# <bXHeight value="4" />
# </panose>
if style == "Regular" or style == "Italic":
bWeight = 5
else:
bWeight = 8
if flag_35:
panose = {
"bFamilyType": 2,
"bSerifStyle": 11,
"bWeight": bWeight,
"bProportion": 3,
"bContrast": 5,
"bStrokeVariation": 2,
"bArmStyle": 3,
"bLetterForm": 0,
"bMidline": 2,
"bXHeight": 3,
}
else:
panose = {
"bFamilyType": 2,
"bSerifStyle": 11,
"bWeight": bWeight,
"bProportion": 9,
"bContrast": 5,
"bStrokeVariation": 2,
"bArmStyle": 3,
"bLetterForm": 0,
"bMidline": 2,
"bXHeight": 3,
}
for key, value in panose.items():
xml.find(f"OS_2/panose/{key}").set("value", str(value))
def fix_post_table(xml: ET, flag_35):
"""post テーブルを編集する"""
# isFixedPitchを編集
# タグ形式: <isFixedPitch value="0"/>
is_fixed_pitch = 0 if flag_35 else 1
xml.find("post/isFixedPitch").set("value", str(is_fixed_pitch))
def fix_name_table(xml: ET):
"""name テーブルを編集する
何故か謎の内容の著作権フィールドが含まれてしまうので、削除する。
"""
# タグ形式: <namerecord nameID="0" platformID="1" platEncID="0" langID="0x0" unicode="True">COPYLIGHT</namerecord>
parent = xml.find("name")
for element in parent.findall("namerecord[@nameID='0']"):
if "PlemolJP" not in element.text:
parent.remove(element)
if __name__ == "__main__":
main()