"""Download the reader's fonts from Google Fonts so the app is self-hosted. Keeps only the latin and latin-ext subsets (enough for Czech), writes the woff2 files plus public/fonts/fonts.css, and fetches each family's OFL licence. Run from the repo root: python3 tools/fetch_fonts.py """ import pathlib import re import urllib.request FAMILIES = { # css family param google/fonts licence dir "Courier+Prime:ital,wght@0,400;0,700;1,400": "courierprime", "Atkinson+Hyperlegible:ital,wght@0,400;0,700;1,400": "atkinsonhyperlegible", "Literata:ital,wght@0,400;0,700;1,400": "literata", "Lexend:wght@400;700": "lexend", } SUBSETS = {"latin", "latin-ext"} UA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/120 Safari/537.36" OUT = pathlib.Path("public/fonts") def get(url): req = urllib.request.Request(url, headers={"User-Agent": UA}) with urllib.request.urlopen(req, timeout=30) as r: return r.read() def main(): OUT.mkdir(parents=True, exist_ok=True) query = "&".join(f"family={f}" for f in FAMILIES) css = get(f"https://fonts.googleapis.com/css2?{query}&display=swap").decode() faces = [] for subset, body in re.findall(r"/\* ([\w-]+) \*/\s*(@font-face \{.*?\})", css, re.S): if subset not in SUBSETS: continue family = re.search(r"font-family: '([^']+)'", body).group(1) style = re.search(r"font-style: (\w+)", body).group(1) weight = re.search(r"font-weight: (\d+)", body).group(1) url = re.search(r"url\((https://[^)]+\.woff2)\)", body).group(1) name = f"{family.lower().replace(' ', '-')}-{subset}-{weight}-{style}.woff2" (OUT / name).write_bytes(get(url)) faces.append(f"/* {subset} */\n" + body.replace(url, name)) (OUT / "fonts.css").write_text("\n".join(faces) + "\n", encoding="utf-8") for d in FAMILIES.values(): lic = get(f"https://raw.githubusercontent.com/google/fonts/main/ofl/{d}/OFL.txt") (OUT / f"OFL-{d}.txt").write_bytes(lic) print(f"{len(faces)} font faces written to {OUT}") if __name__ == "__main__": main()