# Font reference data (run by tools/fixtures/fonts.py inside a nix python with fonttools, freetype-py, uharfbuzz and # brotli). Reads a JSON list of {name, path | b64} on stdin; writes JSON on stdout: per font the bytes, the metrics, # names, cmap samples (fontTools), advances / side bearings / control boxes / areas of sample glyphs (fontTools pens), # kerning of sample pairs (HarfBuzz GPOS 'kern', or FreeType for 'kern' tables), FreeType renders (no hinting) of # sample glyphs at a few sizes, and the WOFF / WOFF2 versions (fontTools). import base64, io, json, sys import freetype import uharfbuzz as hb from fontTools.pens.areaPen import AreaPen from fontTools.pens.boundsPen import ControlBoundsPen from fontTools.ttLib import TTFont, newTable from fontTools.ttLib.tables._k_e_r_n import KernTable_format_0 SAMPLE_TEXT = "AVTWYLPFoaey.,'\"-0147" SIZES = [11, 24, 47] def b64(b): return base64.b64encode(b).decode() def load(spec): if "path" in spec: return open(spec["path"], "rb").read() return base64.b64decode(spec["b64"]) def with_kern_table(data): """a copy with a 'kern' format 0 table of a few pairs (none of the sample fonts has one)""" f = TTFont(io.BytesIO(data)) cmap = f.getBestCmap() order = f.getGlyphOrder() pairs = {} letters = "AVTWYLoaey" for i, a in enumerate(letters): for j, b in enumerate(letters): if ord(a) in cmap and ord(b) in cmap and (i * 7 + j * 3) % 4 == 0: pairs[(cmap[ord(a)], cmap[ord(b)])] = -((i * 13 + j * 5) % 90) - 10 k = newTable("kern") k.version = 0 st = KernTable_format_0() st.version, st.coverage, st.format = 0, 1, 0 st.kernTable = pairs k.kernTables = [st] f["kern"] = k if "GPOS" in f: del f["GPOS"] out = io.BytesIO() f.save(out) return out.getvalue() def collection(datas): from fontTools.ttLib.ttCollection import TTCollection c = TTCollection() c.fonts = [TTFont(io.BytesIO(d)) for d in datas] out = io.BytesIO() c.save(out) return out.getvalue() def reference(name, data, index=0): f = TTFont(io.BytesIO(data), fontNumber=index) gs = f.getGlyphSet() order = f.getGlyphOrder() head, hhea, maxp = f["head"], f["hhea"], f["maxp"] names = f["name"] def nm(i): r = names.getName(i, 3, 1, 0x409) or names.getName(i, 3, 1) or names.getName(i, 1, 0, 0) or names.getName(i, 0, 3) return r.toUnicode() if r else "" cmap = f.getBestCmap() or {} cps = sorted(cmap) sample_cps = cps[:: max(1, len(cps) // 150)] + [0x10FFFF, 0xFFFF, 0x7F, 0x9999] cm = [[cp, f.getGlyphID(cmap[cp]) if cp in cmap else 0] for cp in sample_cps] # glyphs: every glyph for small fonts, else a sample (plus the sample text's) gids = list(range(len(order))) if len(order) <= 400 else list(range(0, len(order), len(order) // 300)) for ch in SAMPLE_TEXT: if ord(ch) in cmap: gids.append(f.getGlyphID(cmap[ord(ch)])) gids = sorted(set(gids)) hmtx = f["hmtx"] glyphs = [] for g in gids: gn = order[g] adv, lsb = hmtx[gn] bp = ControlBoundsPen(gs) gs[gn].draw(bp) ap = AreaPen(gs) gs[gn].draw(ap) e = {"gid": g, "adv": adv, "lsb": lsb, "area": float(ap.value)} if bp.bounds: e["box"] = [float(v) for v in bp.bounds] glyphs.append(e) # kerning kern = [] letters = [c for c in SAMPLE_TEXT if ord(c) in cmap] if "kern" in f and "GPOS" not in f: face = freetype.Face(io.BytesIO(data), index) for a in letters: for b in letters: ga, gb = f.getGlyphID(cmap[ord(a)]), f.getGlyphID(cmap[ord(b)]) v = face.get_kerning(ga, gb, freetype.FT_KERNING_UNSCALED) kern.append([ga, gb, v.x]) elif "GPOS" in f: blob = hb.Blob(data) hface = hb.Face(blob, index) hfont = hb.Font(hface) feats = {"kern": True, "liga": False, "clig": False, "calt": False, "mark": False, "mkmk": False, "ccmp": False, "locl": False, "rlig": False, "dist": False, "curs": False} for a in letters: for b in letters: buf = hb.Buffer() buf.add_str(a + b) buf.guess_segment_properties() hb.shape(hfont, buf, feats) ga, gb = buf.glyph_infos[0].codepoint, buf.glyph_infos[1].codepoint adv = buf.glyph_positions[0].x_advance - hmtx[order[ga]][0] kern.append([ga, gb, adv]) # FreeType renders face = freetype.Face(io.BytesIO(data), index) renders = [] rg = [f.getGlyphID(cmap[ord(c)]) for c in "AgQ@&e%" if ord(c) in cmap] for px in SIZES: face.set_pixel_sizes(0, px) for g in rg: face.load_glyph(g, freetype.FT_LOAD_NO_HINTING | freetype.FT_LOAD_NO_BITMAP) face.glyph.render(freetype.FT_RENDER_MODE_NORMAL) bm = face.glyph.bitmap rows = [] for y in range(bm.rows): rows.append(bytes(bm.buffer[y * bm.pitch: y * bm.pitch + bm.width])) renders.append({"gid": g, "px": px, "w": bm.width, "h": bm.rows, "left": face.glyph.bitmap_left, "top": face.glyph.bitmap_top, "pixels": b64(b"".join(rows))}) os2 = f["OS/2"] if "OS/2" in f else None return { "name": name, "data": b64(data), "index": index, "upem": head.unitsPerEm, "ascent": hhea.ascent, "descent": hhea.descent, "lineGap": hhea.lineGap, "numGlyphs": maxp.numGlyphs, "family": nm(16) or nm(1), "style": nm(17) or nm(2), "fullName": nm(4), "weight": os2.usWeightClass if os2 else 0, "cff": "CFF " in f, "cmap": cm, "glyphs": glyphs, "kern": kern, "renders": renders, } def flavors(data): out = {} for fl in ("woff", "woff2"): f = TTFont(io.BytesIO(data)) f.flavor = fl b = io.BytesIO() f.save(b) out[fl] = b64(b.getvalue()) return out def main(): specs = json.load(sys.stdin) fonts = {} for s in specs: fonts[s["name"]] = load(s) res = {"fonts": [], "woff": []} for name, data in fonts.items(): res["fonts"].append(reference(name, data)) kd = with_kern_table(fonts["ProggyClean"] if "ProggyClean" in fonts else next(iter(fonts.values()))) res["fonts"].append(reference("kern-table", kd)) names = [n for n in ("Karla-Regular", "Cousine-Regular") if n in fonts] if len(names) == 2: ttc = collection([fonts[n] for n in names]) r = reference("collection-1", ttc, 1) res["fonts"].append(r) res["ttc"] = {"data": b64(ttc), "count": 2, "second": names[1]} # woff / woff2 of every font (the reference values are the sfnt's own) for name, data in fonts.items(): fl = flavors(data) res["woff"].append({"name": name, "woff": fl["woff"], "woff2": fl["woff2"]}) json.dump(res, sys.stdout) main()