77 lines · 2.8 KB · PythonRaw
#!/usr/bin/env python3"""Cut the static Libre Caslon Text the invitation face bundles.`Font.custom` cannot set a variation axis, so the invitation's title serif is astatic instance cut from the variable font: LibreCaslonText-Regular wght 400 an invitation's title ("Dinner at Ananya's")The script face beside it (`MsMadi-Regular`, the place written as a signature)is static already and is bundled as google/fonts ships it.Needs fonttools (`python3 -m venv .venv && .venv/bin/pip install fonttools`). python3 scripts/gen-fonts/cut_libre_caslon.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 instance was cut from.SOURCE = ( "https://raw.githubusercontent.com/google/fonts/ea37b132420c876be7220094ea6374be0efb23e3/" "ofl/librecaslontext/LibreCaslonText%5Bwght%5D.ttf")REPO = Path(__file__).resolve().parents[2]DEFAULT_OUT = REPO / "apps/loom/Sources/CottonPaper/Resources/Fonts"# (PostScript name, family, style, wght)CUTS = [ ("LibreCaslonText-Regular", "Libre Caslon Text", "Regular", 400),]def cut(variable: Path, out_dir: Path, ps: str, family: str, style: str, wght: float) -> Path: font = TTFont(variable) inst = instantiateVariableFont(font, {"wght": wght}, 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};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()) / "LibreCaslonText.ttf" urllib.request.urlretrieve(SOURCE, variable) for ps, family, style, wght in CUTS: print(cut(variable, out_dir, ps, family, style, wght))if __name__ == "__main__": main()