Cotton code
84 lines · 3.3 KB · PythonRaw
#!/usr/bin/env python3"""Cut the two static Fraunces instances the paper kit bundles.`Font.custom` cannot set a variation axis, so every Fraunces the app draws is astatic instance cut from the variable font at a fixed optical size and weight:  FrauncesTitle-SemiBold  opsz 48, wght 580  page titles ("Todos", "Cotton")  FrauncesText-Regular    opsz 18, wght 450  section headings, object titles, namesSOFT 0 and WONK 0 on both: the leaning "wonky" n/m/h the variable font defaultsto read as a quirk at card size, and the pictures carry none.Why two cuts and not one: the redesign's pictures set page titles in a heavierserif at display size and everything on paper in a book-weight text serif. Onehigh-contrast display instance at every size (the earlier FrauncesDisplay-SemiBold,opsz 144) was the single largest gap between the built screens and the pictures.Needs fonttools (`python3 -m venv .venv && .venv/bin/pip install fonttools`).    python3 scripts/gen-fonts/cut_fraunces.py [<variable.ttf>] [<out-dir>]With no variable font given it downloads the pinned google/fonts file."""import sysimport tempfileimport urllib.requestfrom pathlib import Pathfrom fontTools.ttLib import TTFontfrom fontTools.varLib.instancer import instantiateVariableFont# google/fonts at the commit the bundled instances were cut from.SOURCE = (    "https://github.com/google/fonts/raw/4024282d9b0cffcdb8e3024560862746178d741f/"    "ofl/fraunces/Fraunces%5BSOFT,WONK,opsz,wght%5D.ttf")REPO = Path(__file__).resolve().parents[2]DEFAULT_OUT = REPO / "apps/loom/Sources/CottonPaper/Resources/Fonts"# (PostScript name, family, style, opsz, wght)CUTS = [    ("FrauncesTitle-SemiBold", "Fraunces Title", "SemiBold", 48, 580),    ("FrauncesText-Regular", "Fraunces Text", "Regular", 18, 450),]def cut(variable: Path, out_dir: Path, ps: str, family: str, style: str, opsz: float, wght: float) -> Path:    font = TTFont(variable)    inst = instantiateVariableFont(font, {"opsz": opsz, "wght": wght, "SOFT": 0, "WONK": 0}, updateFontNames=False)    names = inst["name"]    # Drop the variable font's typographic / variation names so CoreText sees one    # family with one style, registered under the PostScript name the app asks for.    for rec in list(names.names):        if rec.nameID in (16, 17, 21, 22, 25) or rec.nameID >= 256:            names.removeNames(nameID=rec.nameID)    for platform, encoding, language in ((3, 1, 0x409), (1, 0, 0)):        names.setName(family, 1, platform, encoding, language)        names.setName(style, 2, platform, encoding, language)        names.setName(f"{ps};opsz={opsz:g};wght={wght:g}", 3, platform, encoding, language)        names.setName(f"{family} {style}", 4, platform, encoding, language)        names.setName(ps, 6, platform, encoding, language)    inst["OS/2"].usWeightClass = int(wght)    if "STAT" in inst:        del inst["STAT"]    path = out_dir / f"{ps}.ttf"    inst.save(path)    return pathdef main() -> None:    out_dir = Path(sys.argv[2]) if len(sys.argv) > 2 else DEFAULT_OUT    if len(sys.argv) > 1:        variable = Path(sys.argv[1])    else:        variable = Path(tempfile.mkdtemp()) / "Fraunces.ttf"        urllib.request.urlretrieve(SOURCE, variable)    for ps, family, style, opsz, wght in CUTS:        print(cut(variable, out_dir, ps, family, style, opsz, wght))if __name__ == "__main__":    main()