#!/usr/bin/env python3 """Write umtool/report-to-video/intro-glyphs.mjs: Archivo's letters as outlines, for the brand intro's site swap (`render.intro.site`, chrome-intro.mjs). The intro draws the lockup's own glyph paths (brands/archilyzer-media.json, from common/lib/brandMediaGlyphs.ts), which hold only the letters of `Archi`, `lyzer` and `MEDIA`. A site's name ("Jera", "Hasana", "Rekieta") and the URLs under it need any letter, so this table carries every character of CHARSET at the three weights the swap draws: the wordmark's lead (720) and suffix (380), and the URL line (600), all at wdth 118 -- the same instances and the same conventions as gen-media-glyphs.py (font units, y down, baseline 0), so a swapped "Jer" sits on the same baseline as the "Archi" it replaces and is drawn by the same bytes. Pair kerning is recorded per weight for every pair of CHARSET it is non-zero for. python3 umtool/report-to-video/fonts/gen-intro-glyphs.py # rewrite the module python3 umtool/report-to-video/fonts/gen-intro-glyphs.py --stdout # print it Needs fontTools (>= 4.40). The module records the version it was made with. """ import hashlib import importlib.util import json import os import sys import fontTools from fontTools.ttLib import TTFont from fontTools.varLib import instancer HERE = os.path.dirname(os.path.abspath(__file__)) REPO = os.path.normpath(os.path.join(HERE, "..", "..", "..")) FONT = os.path.join(HERE, "Archivo[wdth,wght].ttf") OUT = os.path.join(os.path.dirname(HERE), "intro-glyphs.mjs") # The helpers that make the lockup's own outlines, so the two tables cannot drift # (loaded by path, and without leaving a __pycache__ in common/bin). sys.dont_write_bytecode = True _spec = importlib.util.spec_from_file_location("gen_media_glyphs", os.path.join(REPO, "common", "bin", "gen-media-glyphs.py")) _gmg = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(_gmg) WDTH = _gmg.WDTH WEIGHTS = (720, 380, 600) CHARSET = ( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789" " .-_/:&'" ) def weight_data(font_path, wght): inst = instancer.instantiateVariableFont(TTFont(font_path), {"wght": wght, "wdth": WDTH}) cmap = inst.getBestCmap() hmtx = inst["hmtx"] glyphset = inst.getGlyphSet() glyphs = {} names = {} for ch in CHARSET: name = cmap.get(ord(ch)) if name is None: continue g = inst["glyf"][name] g.recalcBounds(inst["glyf"]) top, bottom = (-g.yMax, -g.yMin) if g.numberOfContours else (0, 0) left, right = (g.xMin, g.xMax) if g.numberOfContours else (0, 0) names[ch] = name glyphs[ch] = { "a": hmtx[name][0], "l": left, "r": right, "t": top, "b": bottom, "d": _gmg.glyph_path(glyphset, name) if g.numberOfContours else "", } kern = {} chars = list(names) for a in chars: for b in chars: k = _gmg.pair_kerning(inst, names[a], names[b]) if k: kern[a + b] = k return {"glyphs": glyphs, "kern": kern, "upem": inst["head"].unitsPerEm, "cap": inst["OS/2"].sCapHeight} def render(font_path): sha = hashlib.sha256(open(font_path, "rb").read()).hexdigest() data = {w: weight_data(font_path, w) for w in WEIGHTS} upem = {d["upem"] for d in data.values()} cap = {d["cap"] for d in data.values()} assert len(upem) == 1 and len(cap) == 1 out = [ "// GENERATED by umtool/report-to-video/fonts/gen-intro-glyphs.py -- DO NOT EDIT. Re-run it:", "// python3 umtool/report-to-video/fonts/gen-intro-glyphs.py", "//", "// Archivo's letters as outlines for the brand intro's site swap (render.intro.site):", f"// Archivo[wdth,wght].ttf at wdth {WDTH}, one table per weight. Font units, y down,", "// baseline 0 (the lockup's convention). Per glyph: a = advance, l/r/t/b = ink box,", "// d = path. kern[\"xy\"] = the GPOS pair adjustment of x then y, where non-zero.", "", "export const INTRO_GLYPH_SOURCE = Object.freeze({", ' font: "Archivo[wdth,wght].ttf",', f' sha256: "{sha}",', f" wdth: {WDTH},", f' fontTools: "{fontTools.version}",', "});", "", f"export const INTRO_GLYPH_UPEM = {upem.pop()};", f"export const INTRO_GLYPH_CAP = {cap.pop()};", "", "export const INTRO_GLYPHS = {", ] for w in WEIGHTS: d = data[w] out.append(f" {w}: {{") out.append(" glyphs: {") for ch, g in d["glyphs"].items(): out.append(f" {json.dumps(ch)}: {json.dumps(g, separators=(',', ':'))},") out.append(" },") out.append(f" kern: {json.dumps(d['kern'], separators=(',', ':'))},") out.append(" },") out.append("};") out.append("") return "\n".join(out) def main(): text = render(FONT) if "--stdout" in sys.argv: sys.stdout.write(text) return with open(OUT, "w", encoding="utf-8") as f: f.write(text) print(f"wrote {os.path.relpath(OUT, REPO)} ({len(text)} bytes)", file=sys.stderr) if __name__ == "__main__": main()