oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1#!/usr/bin/env python3
2"""Write internal/fonts/testdata/harfbuzz.json: how HarfBuzz itself, the
3shaper every browser uses, sets a list of words in every face Docs ships.
4
5internal/fonts' TestShapingMatchesHarfBuzz holds the Go shaper (the PDF's)
6to these numbers, so a shaper that kerns or ligates differently from the
7screen fails in CI rather than in someone's PDF. go-text/typesetting
8v0.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
14import json
15import os
16
17import uharfbuzz as hb
18
19ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
20FILES = os.path.join(ROOT, "internal", "fonts", "files")
21MANIFEST = os.path.join(ROOT, "internal", "fonts", "fonts.json")
22OUT = os.path.join(ROOT, "internal", "fonts", "testdata", "harfbuzz.json")
23
24WORDS = ["AVOWED", "TOYOTA", "Yolanda", "Tomorrow,", "office", "affidavit", "efficiently", "waffled,",
25 "fluffy", "Wolverhampton", "to", "Ava", "“quoted”", "café", "Te", "LT.", "Vivian"]
26
27
28def 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
49if __name__ == "__main__":
50 main()
51