-
Notifications
You must be signed in to change notification settings - Fork 829
/
Copy pathget-fonts.py
executable file
·187 lines (164 loc) · 5.32 KB
/
get-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
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
#!/usr/bin/env python3
# This script downloads several Noto fonts from https://github.com/notofonts/noto-fonts
# That repo was archived in 2023 and is no longer updated.
# Additional fonts can be found on https://notofonts.github.io
import os
import requests
import tempfile
import shutil
import warnings
import zipfile
FONTDIR = os.environ.get("FONTDIR", "./fonts")
try:
os.mkdir(FONTDIR)
except FileExistsError:
warnings.warn("Font directory already exists")
# Fonts to download in regular, bold, and italic
REGULAR_BOLD_ITALIC = ["NotoSans"]
# Fonts to download in regular and bold
REGULAR_BOLD = [
"NotoSansAdlamUnjoined",
"NotoSansArabicUI",
"NotoSansArmenian",
"NotoSansBalinese",
"NotoSansBamum",
"NotoSansBengaliUI",
"NotoSansCanadianAboriginal",
"NotoSansCham",
"NotoSansCherokee",
"NotoSansDevanagariUI",
"NotoSansEthiopic",
"NotoSansGeorgian",
"NotoSansGujaratiUI",
"NotoSansGurmukhiUI",
"NotoSansHebrew",
"NotoSansJavanese",
"NotoSansKannadaUI",
"NotoSansKayahLi",
"NotoSansKhmerUI",
"NotoSansLaoUI",
"NotoSansLisu",
"NotoSansMalayalamUI",
"NotoSansMyanmarUI",
"NotoSansOlChiki",
"NotoSansOriyaUI",
"NotoSansSinhalaUI",
"NotoSansSundanese",
"NotoSansSymbols",
"NotoSansTaiTham",
"NotoSansTamilUI",
"NotoSansTeluguUI",
"NotoSansThaana",
"NotoSansThaiUI",
"NotoSerifTibetan",
]
# Fonts to download regular and black, but no bold
REGULAR_BLACK = ["NotoSansSyriac"]
# Fonts to download only regular
REGULAR = [
"NotoSansBatak",
"NotoSansBuginese",
"NotoSansBuhid",
"NotoSansChakma",
"NotoSansCoptic",
"NotoSansHanunoo",
"NotoSansLepcha",
"NotoSansLimbu",
"NotoSansMandaic",
"NotoSansMongolian",
"NotoSansNewTaiLue",
"NotoSansNKo",
"NotoSansOsage",
"NotoSansOsmanya",
"NotoSansSamaritan",
"NotoSansSaurashtra",
"NotoSansShavian",
"NotoSansSymbols2",
"NotoSansTagalog",
"NotoSansTagbanwa",
"NotoSansTaiLe",
"NotoSansTaiViet",
"NotoSansTifinagh",
"NotoSansVai",
"NotoSansYi",
]
# Attempt to download the font from repos in this order
def findFontUrls(fontName, modifier):
return [
f"https://github.com/notofonts/noto-fonts/raw/main/hinted/ttf/{fontName}/{fontName}-{modifier}.ttf",
# currently only sourcing from one repo
]
def downloadToFile(urls, destination, dir=FONTDIR):
headers = {"User-Agent": "get-fonts.py/osm-carto"}
try:
r = requests.get(urls[0], headers=headers)
if r.status_code != 200:
if len(urls) > 1:
warnings.warn(f"Failed to download {urls[0]}, retrying with next font source")
downloadToFile(urls[1:], destination, dir=dir)
else:
raise Exception
with open(os.path.join(dir, destination), "wb") as f:
f.write(r.content)
except:
raise Exception(f"Failed to download {urls}")
for font in REGULAR_BOLD + REGULAR_BOLD_ITALIC + REGULAR_BLACK + REGULAR:
regularFontUrls = findFontUrls(font, "Regular")
downloadToFile(regularFontUrls, f"{font}-Regular.ttf")
if (font in REGULAR_BOLD) or (font in REGULAR_BOLD_ITALIC):
boldFontUrls = findFontUrls(font, "Bold")
downloadToFile(boldFontUrls, f"{font}-Bold.ttf")
if font in REGULAR_BOLD_ITALIC:
italicFontUrls = findFontUrls(font, "Italic")
downloadToFile(italicFontUrls, f"{font}-Italic.ttf")
if font in REGULAR_BLACK:
blackFontUrls = findFontUrls(font, "Black")
downloadToFile(blackFontUrls, f"{font}-Black.ttf")
# Other noto fonts which don't follow the URL pattern above
# CJK fonts
downloadToFile(
[
"https://github.com/notofonts/noto-cjk/raw/main/Sans/OTF/Japanese/NotoSansCJKjp-Regular.otf"
],
"NotoSansCJKjp-Regular.otf",
)
downloadToFile(
[
"https://github.com/notofonts/noto-cjk/raw/main/Sans/OTF/Japanese/NotoSansCJKjp-Bold.otf"
],
"NotoSansCJKjp-Bold.otf",
)
# Fonts in zipfiles need a temporary directory
TMPDIR = tempfile.mkdtemp(prefix="get-fonts.")
# Noto Emoji B&W isn't available as a separate download, so we need to download the package and unzip it
downloadToFile(
["https://archive.org/download/noto-emoji/Noto_Emoji.zip"],
"Noto_Emoji.zip",
dir=TMPDIR,
)
emojiPath = os.path.join(TMPDIR, "Noto_Emoji.zip")
emojiExtract = ["NotoEmoji-Regular.ttf", "NotoEmoji-Bold.ttf"]
with zipfile.ZipFile(emojiPath, "r") as zip_ref:
for file in emojiExtract:
source = zip_ref.getinfo(f"static/{file}")
zip_ref.extract(source, FONTDIR)
# move from FONTDIR/static/x to overwrite FONTDIR/x
unzipSrc = os.path.join(FONTDIR, file)
if os.path.exists(unzipSrc):
os.remove(unzipSrc)
shutil.move(os.path.join(FONTDIR, "static", file), FONTDIR)
downloadToFile(
["https://mirrors.dotsrc.org/osdn/hanazono-font/68253/hanazono-20170904.zip"],
"hanazono.zip",
dir=TMPDIR,
)
hanazonoPath = os.path.join(TMPDIR, "hanazono.zip")
with zipfile.ZipFile(hanazonoPath, "r") as zip_ref:
for file in ["HanaMinA.ttf", "HanaMinB.ttf"]:
source = zip_ref.getinfo(file)
zip_ref.extract(source, FONTDIR)
# clean up tmp directories
shutil.rmtree(TMPDIR)
fontdir_static = os.path.join(FONTDIR, "static")
if os.path.exists(fontdir_static):
shutil.rmtree(fontdir_static)