Files
rengine/scripts/fetch_report_fonts.py
T
Yogesh OjhaandClaude Opus 5 2cb8d73280 style: strip explanatory comments across the codebase
The comment rule in CLAUDE.md is one line maximum, never narration: no
multi-line blocks, no module-header explainers, no sentences that explain or
justify the code to the reader. The tree had drifted a long way from it, and
most of the drift is mine.

Removed:
  232  lines of multi-line comment blocks
  203  single-line comments that argued instead of stating
  154  comments joined by so/because/otherwise/rather-than
  143  narrating comments in the frontend
  112  test docstrings (the test name already says what it asserts)
   25  test module docstrings and 25 decorative section dividers

Collapsed:
  324  multi-line docstrings to one factual line
  271  one-line docstrings that had a justifying clause bolted on

Kept: functional directives, section dividers, short field labels, alembic
revision headers, and the eight FastAPI route docstrings that render in /docs.
Where a fact was load-bearing it is in CLAUDE.md, which is where it belongs.

284 python tests and 34 frontend tests pass, ruff and ruff-format clean across
ten packages, svelte-check 0 errors, prettier and eslint clean, app.main imports.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013dKUV96WkQQETZBMoAG98C
2026-09-11 14:11:23 +05:30

130 lines
4.7 KiB
Python

#!/usr/bin/env python3
"""Vendor the report fonts. Run on a checkout; the files are committed."""
from __future__ import annotations
import argparse
import io
import re
import sys
import urllib.request
from pathlib import Path
from fontTools import subset
from fontTools.ttLib import TTFont
DEFAULT_ROOT = Path(__file__).resolve().parent.parent / "reports" / "assets" / "fonts"
API = "https://fonts.googleapis.com/css2"
UA = (
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"
)
WIDE = "latin,latin-ext,greek,cyrillic"
LATIN = "latin,latin-ext"
MONO = "latin"
# what to keep after the download, per subset choice
UNICODES = {
WIDE: "U+0000-024F,U+0259,U+0300-036F,U+0370-03FF,U+0400-04FF,U+1E00-1EFF,"
"U+2000-206F,U+2070-209F,U+20A0-20CF,U+2100-214F,U+2190-21FF,U+2200-22FF,U+25A0-25FF,U+2E00-2E7F",
LATIN: "U+0000-024F,U+0259,U+0300-036F,U+1E00-1EFF,U+2000-206F,U+2070-209F,"
"U+20A0-20CF,U+2100-214F,U+2190-21FF,U+2200-22FF,U+25A0-25FF,U+2E00-2E7F",
MONO: "U+0000-024F,U+0259,U+0300-036F,U+2000-206F,U+2070-209F,U+20A0-20CF,"
"U+2100-214F,U+2190-21FF,U+2200-22FF,U+25A0-25FF",
}
FAMILIES: dict[str, tuple[str, tuple[int, ...], bool, str]] = {
"inter": ("Inter", (400, 500, 600, 700), False, WIDE),
"space-grotesk": ("Space Grotesk", (400, 500, 700), False, LATIN),
"ibm-plex-sans": ("IBM Plex Sans", (400, 600, 700), True, WIDE),
"source-serif": ("Source Serif 4", (400, 600, 700), True, WIDE),
"jetbrains-mono": ("JetBrains Mono", (400, 700), False, MONO),
"ibm-plex-mono": ("IBM Plex Mono", (400, 600), False, MONO),
}
FACE_RE = re.compile(r"@font-face\s*\{(.*?)\}", re.S)
def _fetch(url: str) -> bytes:
request = urllib.request.Request(url, headers={"User-Agent": UA}) # noqa: S310
with urllib.request.urlopen(request, timeout=60) as response: # noqa: S310
return response.read()
def _spec(name: str, weights: tuple[int, ...], italic: bool) -> str:
family = name.replace(" ", "+")
if italic:
pairs = [f"0,{w}" for w in weights] + [f"1,{w}" for w in weights]
return f"family={family}:ital,wght@{';'.join(pairs)}"
return f"family={family}:wght@{';'.join(str(w) for w in weights)}"
def _trim(raw: bytes, unicodes: str) -> bytes:
"""Keep the ranges a report can print, and write one woff2."""
font = TTFont(io.BytesIO(raw))
options = subset.Options()
options.layout_features = ["*"]
options.name_IDs = ["*"]
options.notdef_outline = True
options.recalc_bounds = True
options.drop_tables = ["FFTM"]
subsetter = subset.Subsetter(options=options)
subsetter.populate(unicodes=subset.parse_unicodes(unicodes))
subsetter.subset(font)
font.flavor = "woff2"
out = io.BytesIO()
font.save(out)
return out.getvalue()
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--root", type=Path, default=DEFAULT_ROOT)
root = parser.parse_args().root
root.mkdir(parents=True, exist_ok=True)
for stale in root.glob("*.woff2"):
stale.unlink()
total = 0
lines: list[str] = []
for key, (name, weights, italic, subsets) in FAMILIES.items():
url = f"{API}?{_spec(name, weights, italic)}&subset={subsets}&display=swap"
try:
css = _fetch(url).decode("utf-8")
except OSError as exc:
print(f" {key}: unavailable ({exc})")
continue
faces = 0
for block in FACE_RE.findall(css):
src = re.search(r"url\((https://[^)]+)\)", block)
weight = re.search(r"font-weight:\s*(\d+)", block)
style = re.search(r"font-style:\s*(\w+)", block)
if not src or not weight:
continue
if "unicode-range" in block:
print(f" {key}: the API split this family; refusing a split face")
return 1
slant = (style.group(1) if style else "normal") == "italic"
filename = f"{key}-{weight.group(1)}{'-italic' if slant else ''}.woff2"
data = _trim(_fetch(src.group(1)), UNICODES[subsets])
(root / filename).write_bytes(data)
total += len(data)
faces += 1
lines.append(
f"@font-face{{font-family:'{name}';"
f"font-style:{'italic' if slant else 'normal'};"
f"font-weight:{weight.group(1)};font-display:swap;"
f"src:url('fonts/{filename}') format('woff2')}}"
)
print(f" {key}: {faces} faces")
(root.parent / "fonts.css").write_text("\n".join(lines) + "\n", encoding="utf-8")
print(f"{len(lines)} faces, {total / 1024:.0f} KB")
return 0
if __name__ == "__main__":
sys.exit(main())