| 1 | #!/usr/bin/env python3 |
| 2 | """Write internal/fonts/testdata/harfbuzz.json: how HarfBuzz itself, the |
| 3 | shaper every browser uses, sets a list of words in every face Docs ships. |
| 4 | |
| 5 | internal/fonts' TestShapingMatchesHarfBuzz holds the Go shaper (the PDF's) |
| 6 | to these numbers, so a shaper that kerns or ligates differently from the |
| 7 | screen fails in CI rather than in someone's PDF. go-text/typesetting |
| 8 | v0.3.5 did: it applied none of Inter's kerning. |
| 9 | |
| 10 | /tmp/fontvenv/bin/pip install uharfbuzz==0.57.0 |
| 11 | /tmp/fontvenv/bin/python -I scripts/harfbuzz-golden.py |
| 12 | """ |
| 13 | |
| 14 | import json |
| 15 | import os |
| 16 | |
| 17 | import uharfbuzz as hb |
| 18 | |
| 19 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 20 | FILES = os.path.join(ROOT, "internal", "fonts", "files") |
| 21 | MANIFEST = os.path.join(ROOT, "internal", "fonts", "fonts.json") |
| 22 | OUT = os.path.join(ROOT, "internal", "fonts", "testdata", "harfbuzz.json") |
| 23 | |
| 24 | WORDS = ["AVOWED", "TOYOTA", "Yolanda", "Tomorrow,", "office", "affidavit", "efficiently", "waffled,", |
| 25 | "fluffy", "Wolverhampton", "to", "Ava", "“quoted”", "café", "Te", "LT.", "Vivian"] |
| 26 | |
| 27 | |
| 28 | def main(): |
| 29 | manifest = json.load(open(MANIFEST, encoding="utf-8")) |
| 30 | out = {"comment": "Generated by scripts/harfbuzz-golden.py: each word's advance in font units, as HarfBuzz sets it.", "faces": {}} |
| 31 | for fam in manifest["families"]: |
| 32 | for style, base in fam["styles"].items(): |
| 33 | path = os.path.join(FILES, fam["dir"], base + ".ttf") |
| 34 | font = hb.Font(hb.Face(hb.Blob.from_file_path(path))) |
| 35 | words = {} |
| 36 | for w in WORDS: |
| 37 | buf = hb.Buffer() |
| 38 | buf.add_str(w) |
| 39 | buf.guess_segment_properties() |
| 40 | buf.language = "en" |
| 41 | hb.shape(font, buf, {}) |
| 42 | words[w] = sum(p.x_advance for p in buf.glyph_positions) |
| 43 | out["faces"][fam["dir"] + "/" + base + ".ttf"] = words |
| 44 | with open(OUT, "w", encoding="utf-8") as f: |
| 45 | json.dump(out, f, indent=1, ensure_ascii=False, sort_keys=True) |
| 46 | f.write("\n") |
| 47 | |
| 48 | |
| 49 | if __name__ == "__main__": |
| 50 | main() |
| 51 | |