-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsetter_tool.py
119 lines (96 loc) · 3.57 KB
/
subsetter_tool.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
#!/usr/local/bin/python3 -tt
# -*- coding: utf-8 -*-
from typing import Literal
import argparse
from fontTools import subset
from pathlib import Path
import pyperclip as pc
import sys
from subsetter import font_face, hash
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def get_subset_font_path(input_path: Path, hash: str, format: str) -> Path:
input_filename = Path(input_path.name).stem
output_filename_str = "{}__subset_{}.{}".format(input_filename, hash, format)
return Path(".").joinpath(output_filename_str)
def write_subset_font(
input_path: Path,
text: str,
hash: str,
format: Literal["ttf", "woff", "woff2"],
):
try:
output_path = get_subset_font_path(input_path, hash, format)
except TypeError as e:
print("\n[ Error ] {}".format(e))
sys.exit(1)
if format == "woff2" or format == "woff":
options = subset.Options(flavor=format)
else:
options = subset.Options()
try:
font = subset.load_font(input_path, options)
except FileNotFoundError:
message = "\n[ Error ] Missing input font file `{}`".format(input_path)
eprint(message)
raise FileNotFoundError(message)
fonttools_subsetter = subset.Subsetter(options)
fonttools_subsetter.populate(text=text)
fonttools_subsetter.subset(font)
try:
subset.save_font(font, output_path, options)
except PermissionError:
message = f"\n[ Error ] Unable to write font file `{output_path}`. Check the "
"path exists with write permissions."
eprint(message)
raise PermissionError(message)
print("Wrote {} subset to {}".format(format, str(output_path)))
return str(output_path)
def write_subset_font_file_for_format(font_file_path_str: str, text: str, hash: str):
extension = Path(font_file_path_str).suffix
format = extension[1:]
if format == "woff2" or format == "woff" or format == "ttf":
try:
subset_file_path = write_subset_font(
Path(font_file_path_str), text, hash, format
)
except FileNotFoundError:
sys.exit(1)
except PermissionError:
sys.exit(1)
return subset_file_path
print("Unrecognised file format for font file {}".format(font_file_path_str))
return ""
def main():
# parse CLI arguments
parser = argparse.ArgumentParser(
prog="subsetter", description="Generate font subset CSS and font files."
)
parser.add_argument("-f", "--family", help="Font family name.")
parser.add_argument("-t", "--text", help="Text fragment to create subset for.")
parser.add_argument(
"-w", "--weight", type=int, help="Font weight to generate @fontface CSS for."
)
parser.add_argument(
"font_files", nargs="*", help="Input woff2, woff and ttf font files"
)
family = parser.parse_args().family
text = parser.parse_args().text
weight_int = parser.parse_args().weight
# generate subsets for requested formats
hash_value = hash(text)
font_file_path_list = parser.parse_args().font_files
subset_file_path_list = []
for font_file_path_str in font_file_path_list:
subset_file_path = write_subset_font_file_for_format(
font_file_path_str, text, hash_value
)
if len(subset_file_path) > 0:
subset_file_path_list.append(subset_file_path)
# generate CSS
css = font_face(family, weight_int, text, subset_file_path_list)
print(css)
pc.copy(css)
print("Copied @fontface CSS to clipboard")
if __name__ == "__main__":
main()