Files
rosetta/tools/fixtures/fonts.py
T

38 lines
2.1 KiB
Python

# Font fixtures (tools/fixtures.py): the imgui-luau fonts (TrueType: Cousine, DroidSans, Karla, Proggy*, Roboto, read
# from ~/dev/luau/imgui/imgui-luau/fonts or $IMGUI_FONTS; skipped if missing) and Source Sans 3 (CFF OpenType, nixpkgs
# source-sans), plus a copy of ProggyClean with a 'kern' table and a two-font collection, both made with fontTools.
# Reference values come from tools/fixtures/_font_reference.py: fontTools (metrics, names, cmap, hmtx, control boxes,
# areas, WOFF / WOFF2), HarfBuzz (GPOS kerning), FreeType (kern tables, unhinted renders).
# tests/gen/fonts.luau { fonts = { { name, data, upem, ..., cmap, glyphs, kern, renders } }, woff = { ... }, ttc }
import base64, glob, json, os, subprocess
INPUTS = ["tools/fixtures/_font_reference.py"]
FONTS = os.environ.get("IMGUI_FONTS", os.path.expanduser("~/dev/luau/imgui/imgui-luau/fonts"))
PICK = ["Cousine-Regular", "DroidSans", "Karla-Regular", "ProggyClean", "Roboto-Medium"]
def generate(ctx):
specs = []
for name in PICK:
p = os.path.join(FONTS, name + ".ttf")
if os.path.exists(p):
specs.append({"name": name, "b64": base64.b64encode(open(p, "rb").read()).decode()})
out = subprocess.run(["nix", "build", "--no-link", "--print-out-paths", "nixpkgs#source-sans"], capture_output=True,
check=True, text=True).stdout.strip().split("\n")[-1]
otf = os.path.join(out, "share", "fonts", "opentype", "SourceSans3-Regular.otf")
specs.append({"name": "SourceSans3-Regular", "b64": base64.b64encode(open(otf, "rb").read()).decode()})
code = open(os.path.join(ctx.root, "tools", "fixtures", "_font_reference.py")).read()
raw = ctx.python(["fonttools", "freetype-py", "uharfbuzz", "brotli"], code, input=json.dumps(specs).encode())
res = json.loads(raw)
d = base64.b64decode
for f in res["fonts"]:
f["data"] = d(f["data"])
for r in f["renders"]:
r["pixels"] = d(r["pixels"])
for w in res["woff"]:
w["woff"], w["woff2"], w["woff2h"] = d(w["woff"]), d(w["woff2"]), d(w["woff2h"])
if "ttc" in res:
res["ttc"]["data"] = d(res["ttc"]["data"])
ctx.emit("fonts", res)