Files
rosetta/tools/fixtures/type1.py
T

80 lines
3.5 KiB
Python

# Type 1 fixtures (tools/fixtures.py): Latin Modern PFB fonts (nixpkgs#lmodern, GUST font licence; read from the nix
# store at fixture time) and a PFA made from one of them by t1utils' `t1ascii`; reference outlines from fontTools
# (t1Lib parses and decrypts, T1CharString draws through ControlBoundsPen / AreaPen: control boxes, areas, advances),
# code points from fontTools.agl, FreeType renders of a few glyphs (PSNR in the test).
# tests/gen/type1.luau { fonts = { { name, data, upem, family, glyphs = { { name, cp?, advance, bounds?, area } },
# renders = { { cp, px, width, height, left, top, pixels } } } } }
import json, os, subprocess
CODE = r'''
import json, sys
from fontTools.t1Lib import T1Font
from fontTools.pens.areaPen import AreaPen
from fontTools.pens.boundsPen import ControlBoundsPen
from fontTools import agl
import freetype
req = json.load(sys.stdin)
out = []
for spec in req:
f = T1Font(spec["path"])
f.parse()
cs = f["CharStrings"]
fi = f.font.get("FontInfo", {})
glyphs = []
for name in spec["names"]:
if name not in cs:
continue
g = cs[name]
bp = ControlBoundsPen(cs)
g.draw(bp)
ap = AreaPen(cs)
g.draw(ap)
e = {"name": name, "advance": g.width, "area": ap.value}
if bp.bounds:
e["bounds"] = list(bp.bounds)
u = agl.toUnicode(name)
if len(u) == 1:
e["cp"] = ord(u)
glyphs.append(e)
face = freetype.Face(spec["path"])
renders = []
for ch in "AgQ&8":
face.set_pixel_sizes(0, 48)
face.load_char(ch, freetype.FT_LOAD_RENDER | freetype.FT_LOAD_NO_HINTING)
b = face.glyph.bitmap
px = bytearray(b.width * b.rows)
for y in range(b.rows):
for x in range(b.width):
px[y * b.width + x] = b.buffer[y * b.pitch + x]
renders.append({"cp": ord(ch), "px": 48, "width": b.width, "height": b.rows, "left": face.glyph.bitmap_left,
"top": face.glyph.bitmap_top, "pixels": px.hex()})
out.append({"family": fi.get("FamilyName", ""), "upem": round(1 / f["FontMatrix"][0]), "glyphs": glyphs, "renders": renders,
"numGlyphs": len(cs.keys())})
print(json.dumps(out))
'''
NAMES = ["A", "B", "O", "Q", "a", "g", "e", "f", "fi", "ffl", "zero", "eight", "ampersand", "Aacute", "eacute", "ccedilla",
"Lslash", "dotlessi", "quotedblleft", "emdash", "germandbls", "AE", "oe", "space", "period", "parenleft", "Ydieresis"]
def store(pkg):
return subprocess.run(["nix", "build", "--no-link", "--print-out-paths", "nixpkgs#" + pkg], capture_output=True, check=True, text=True).stdout.strip().split("\n")[-1]
def generate(ctx):
lm = os.path.join(store("lmodern"), "share", "fonts", "type1", "public", "lm")
t1 = store("t1utils")
paths = [(os.path.join(lm, f), f) for f in ("lmr10.pfb", "lmbx12.pfb", "lmri10.pfb", "lmtt10.pfb")]
import tempfile
tmp = tempfile.mkdtemp(prefix="type1-")
pfa = os.path.join(tmp, "lmr10.pfa")
subprocess.run([os.path.join(t1, "bin", "t1ascii"), paths[0][0], pfa], check=True, capture_output=True)
paths.append((pfa, "lmr10.pfa (t1ascii)"))
refs = json.loads(ctx.python(["fonttools", "freetype-py"], CODE, input=json.dumps([{"path": p, "names": NAMES} for p, _ in paths]).encode()))
fonts = []
for (p, name), ref in zip(paths, refs):
for r in ref["renders"]:
r["pixels"] = bytes.fromhex(r["pixels"])
fonts.append({"name": name, "data": open(p, "rb").read(), **ref})
ctx.emit("type1", {"fonts": fonts})