-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathbuild-fonts.py
executable file
·100 lines (72 loc) · 2.79 KB
/
build-fonts.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
#!/usr/bin/env python3
import re
import json
import base64
__field_def_pattern__ = r'^([a-z-]+):\s*(.*);$'
def read_file(file_path):
with open(file_path, 'r') as f:
return f.readlines()
def font_format(ext):
if ext == 'ttf':
return 'truetype'
return ext
def convert_font_url(font_path_prefix, text):
if not text.startswith('url('):
return text
bracket_idx = text.index(')')
sub_path = text[4:bracket_idx]
if sub_path[0] == '\'' or sub_path[0] == '"':
sub_path = sub_path[1:]
if sub_path[-1] == '\'' or sub_path[-1] == '"':
sub_path = sub_path[0:len(sub_path)-1]
font_path = font_path_prefix + sub_path
ext = font_path[font_path.rfind('.')+1:]
font_path = font_path[:font_path.rfind('.')] + '.' + ext
with open(font_path, 'rb') as f:
data = f.read()
encoded = base64.b64encode(data).decode("utf-8")
return f'url(\'data:font/{ext};base64,{encoded}\') format(\'{font_format(ext)}\')'
def extract_font_name(font_family):
return re.sub('[\'\"]', '', font_family['font-family'])
if __name__ == '__main__':
font_files = [
'assets/custom-fonts/fonts.css',
'assets/katex/katex.css'
]
font_families = []
next_family = None
for font_file_path in font_files:
font_css_lines = read_file(font_file_path)
font_path_prefix = font_file_path[0:font_file_path.rfind('/')+1]
for raw_line in font_css_lines:
line = raw_line.strip()
if line == '@font-face {':
next_family = {}
font_families.append(next_family)
elif not next_family is None:
match = re.search(__field_def_pattern__, line)
if not match is None:
field = match.group(1)
value = match.group(2)
if field == 'src':
next_family[field] = convert_font_url(font_path_prefix, value)
else:
next_family[field] = value
elif line == '}':
next_family = None
font_mapping = dict()
for font_family in font_families:
font_name = extract_font_name(font_family)
font_file_name = re.sub('[^0-9a-zA-Z]+', '_', font_name.lower().strip()) + '.css'
file_path = 'assets/custom-fonts/embedded/' + font_file_name
font_mapping[font_name] = '/' + file_path
with open(file_path, 'w+') as f:
f.write('@font-face {\n')
for k, v in font_family.items():
f.write(f' {k}: {v};\n')
f.write('}\n\n')
s = json.dumps(font_mapping, indent=4)
with open('src/ui/scheme/FontMapping.js', 'w+') as f:
f.write('export const fontMapping = ')
f.write(s)
f.write(';\n')