theater-reader/tools/fetch_fonts.py
agent 2608d10dd2 Add theater reader web app with the 1931 play transcription
Static app (no build step) served by unprivileged nginx in a container:
choose a font, size and theme, pick a character to highlight their lines
and step through them, read in a distraction-free mode, or export a PDF
via the print dialog (one page per original page).

Includes the transcription of typescript pages 5-27 (pages 1-4 and 7
are missing in the original), a parser with node:test tests, and
self-hosted OFL fonts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gVRDXpoukzgMFumoJt
2026-10-29 23:14:19 +00:00

54 lines
2.1 KiB
Python

"""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()