-
Notifications
You must be signed in to change notification settings - Fork 61
/
generate_fonts.py
97 lines (83 loc) · 2.79 KB
/
generate_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
#!/usr/bin/env python3
# Generate CSS files for various Nerd Fonts.
import base64
import json
import sys
import pathlib
import urllib.error
import urllib.request
CSS_TEMPLATE_REGULAR = """\
@font-face {{
font-family: "{name}";
font-style: normal;
font-weight: 400;
src: url(data:font/{extension};charset-utf-8;base64,{b64_data});
}}
"""
CSS_TEMPLATE_BOLD = """\
@font-face {{
font-family: "{name}";
font-style: normal;
font-weight: 700;
src: url(data:font/{extension};charset-utf-8;base64,{b64_data});
}}
"""
CSS_TEMPLATE_ITALIC = """\
@font-face {{
font-family: "{name}";
font-style: italic;
font-weight: 400;
src: url(data:font/{extension};charset-utf-8;base64,{b64_data});
}}
"""
CSS_TEMPLATE_BOLD_ITALIC = """\
@font-face {{
font-family: "{name}";
font-style: italic;
font-weight: 700;
src: url(data:font/{extension};charset-utf-8;base64,{b64_data});
}}
"""
def download_as_base64(url):
# 'https://github.com/ryanoasis/nerd-fonts/raw/master/patched-fonts/{font}/{regular}'
print(url)
req = urllib.request.Request(url)
with urllib.request.urlopen(req) as r:
assert r.getcode() == 200
data = r.read()
return base64.b64encode(data).decode('utf-8')
def generate_css(fonts, base_url, is_nerd_font):
for font, paths in fonts.items():
name = paths.get('name', font)
if is_nerd_font:
name = name + " Nerd Font"
filename = '%s.css' % name
if not 'regular' in paths:
print('Incomplete: ' + filename)
continue
else:
print('Creating: ' + filename)
extension = pathlib.Path(paths['regular']).suffix.replace('.', '')
with open(filename, 'w') as fd:
for (weight, template) in (('regular', CSS_TEMPLATE_REGULAR),
('bold', CSS_TEMPLATE_BOLD),
('italic', CSS_TEMPLATE_ITALIC),
('bold_italic', CSS_TEMPLATE_BOLD_ITALIC)):
if not weight in paths:
continue
params = urllib.parse.quote('%s/%s' % (font, paths[weight]))
b64_data = download_as_base64(
base_url + '/%s' % (params),
)
fd.write(template.format(name = name, b64_data = b64_data, extension = extension))
def unpatched_fonts():
with open('fonts-unpatched.json', 'r') as fd:
fonts = json.load(fd)
generate_css(fonts, 'https://github.com/ryanoasis/nerd-fonts/raw/master/src/unpatched-fonts', False)
def patched_fonts():
with open('fonts-patched.json', 'r') as fd:
fonts = json.load(fd)
generate_css(fonts, 'https://github.com/ryanoasis/nerd-fonts/raw/master/patched-fonts', True)
if __name__ == '__main__':
unpatched_fonts()
# patched_fonts()