-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert_font.py
More file actions
111 lines (93 loc) · 3.11 KB
/
convert_font.py
File metadata and controls
111 lines (93 loc) · 3.11 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
#!/usr/bin/env python3
import json
import argparse
from pathlib import Path
from fontTools.ttLib import TTFont
from fontTools.varLib.instancer import instantiateVariableFont
CURRENT_DIR = Path(__file__).parent
def convert_variable_font(
font_path: str,
weight: int,
):
print(f"--- Converting font ---")
font_path = Path(font_path)
codepoints_path = font_path.parent / font_path.name.replace(".ttf", ".codepoints")
base_name = "MaterialSymbolsOutlined"
new_base_name = "MaterialSymbolsOutlFILL"
dst_dir = CURRENT_DIR / "src" / "qtmaterialsymbols" / "resources"
static_file = dst_dir / f"{base_name}.ttf"
static_file_filled = dst_dir / f"{base_name}Filled.ttf"
json_file = dst_dir / f"{base_name}.json"
print(f"- Converting variable font")
def _instantiate(axes):
var_font = TTFont(font_path)
static_font = instantiateVariableFont(
var_font,
axes,
static=True,
)
if "STAT" in static_font:
del static_font["STAT"]
return static_font
static_font = _instantiate({"wght": weight})
static_font_filled = _instantiate({"wght": weight, "FILL": 1.0})
# Change family name of filled font
family_base_name = "Material Symbols Outlined"
new_family_base_name = "Material Symbols OutlFILL"
for record in static_font_filled["name"].names:
name_str = record.toUnicode()
if family_base_name in name_str:
new_name = name_str.replace(family_base_name, new_family_base_name)
elif base_name in name_str:
new_name = name_str.replace(base_name, new_base_name)
else:
continue
static_font_filled["name"].setName(
new_name,
record.nameID,
record.platformID,
record.platEncID,
record.langID,
)
static_font.save(static_file)
static_font_filled.save(static_file_filled)
print(f" - Saved static fonts")
# convert code points
codepoints = Path(codepoints_path)
print(f"- Converting codepoints {codepoints.name} to {json_file.name}")
with open(codepoints, "r") as fr:
data = fr.readlines()
cp = {}
for line in data:
try:
name, code = line.split(maxsplit=2)
except ValueError:
continue
cp[name] = int(code, 16)
with open(json_file, "w") as fw:
json.dump(cp, fw, indent=4)
print(f" - Saved {json_file}")
print(f"Done ! {json_file}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Convert a variable font to a static font and codepoints to JSON format",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"-f",
"--font-path",
required=True,
help="Path to the variable font file (.ttf)",
)
parser.add_argument(
"-w",
"--weight",
type=int,
default=400,
help="Font weight value (default: 400)",
)
args = parser.parse_args()
convert_variable_font(
args.font_path,
weight=args.weight,
)