#!/usr/bin/env python3 """Write internal/fonts/testdata/harfbuzz.json: how HarfBuzz itself, the shaper every browser uses, sets a list of words in every face Docs ships. internal/fonts' TestShapingMatchesHarfBuzz holds the Go shaper (the PDF's) to these numbers, so a shaper that kerns or ligates differently from the screen fails in CI rather than in someone's PDF. go-text/typesetting v0.3.5 did: it applied none of Inter's kerning. /tmp/fontvenv/bin/pip install uharfbuzz==0.57.0 /tmp/fontvenv/bin/python -I scripts/harfbuzz-golden.py """ import json import os import uharfbuzz as hb ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) FILES = os.path.join(ROOT, "internal", "fonts", "files") MANIFEST = os.path.join(ROOT, "internal", "fonts", "fonts.json") OUT = os.path.join(ROOT, "internal", "fonts", "testdata", "harfbuzz.json") WORDS = ["AVOWED", "TOYOTA", "Yolanda", "Tomorrow,", "office", "affidavit", "efficiently", "waffled,", "fluffy", "Wolverhampton", "to", "Ava", "“quoted”", "café", "Te", "LT.", "Vivian"] def main(): manifest = json.load(open(MANIFEST, encoding="utf-8")) out = {"comment": "Generated by scripts/harfbuzz-golden.py: each word's advance in font units, as HarfBuzz sets it.", "faces": {}} for fam in manifest["families"]: for style, base in fam["styles"].items(): path = os.path.join(FILES, fam["dir"], base + ".ttf") font = hb.Font(hb.Face(hb.Blob.from_file_path(path))) words = {} for w in WORDS: buf = hb.Buffer() buf.add_str(w) buf.guess_segment_properties() buf.language = "en" hb.shape(font, buf, {}) words[w] = sum(p.x_advance for p in buf.glyph_positions) out["faces"][fam["dir"] + "/" + base + ".ttf"] = words with open(OUT, "w", encoding="utf-8") as f: json.dump(out, f, indent=1, ensure_ascii=False, sort_keys=True) f.write("\n") if __name__ == "__main__": main()