Files

77 lines
4.4 KiB
Python

# EOT fixtures (tools/fixtures.py): no EOT tool is packaged in nixpkgs, so Embedded OpenType files are built here per
# the W3C EOT submission (versions 1.0, 2.1 plain and XOR-obfuscated, 2.2 with an empty signature and EUDC data, one
# MTX-flagged file) around TrueType / CFF fonts (DejaVu Sans Mono from nixpkgs#dejavu_fonts, Source Sans 3 OTF from
# nixpkgs#source-sans); the header fields come from fontTools, and the inner font's facts (units per em, glyph count,
# cmap glyph ids, advances) from fontTools too.
# tests/gen/eot.luau { cases = { { name, data, version, xor, family, style, fullName, weight, fsType, upem,
# numGlyphs, glyphs = { { cp, gid, advance } } } }, mtx }
import json, os, struct, subprocess
CODE = r'''
import json, sys
from fontTools.ttLib import TTFont
out = []
for p in json.load(sys.stdin):
f = TTFont(p)
os2, head, name = f["OS/2"], f["head"], f["name"]
cmap = f.getBestCmap()
glyphs = []
order = f.getGlyphOrder()
for cp in (65, 97, 48, 0x20AC, 0x3A9):
if cp in cmap:
g = cmap[cp]
glyphs.append({"cp": cp, "gid": order.index(g), "advance": f["hmtx"][g][0]})
out.append({"panose": bytes([getattr(os2.panose, k) for k in ("bFamilyType", "bSerifStyle", "bWeight", "bProportion", "bContrast", "bStrokeVariation", "bArmStyle", "bLetterForm", "bMidline", "bXHeight")]).hex(),
"weight": os2.usWeightClass, "fsType": os2.fsType, "italic": os2.fsSelection & 1,
"ranges": [os2.ulUnicodeRange1, os2.ulUnicodeRange2, os2.ulUnicodeRange3, os2.ulUnicodeRange4],
"codepages": [getattr(os2, "ulCodePageRange1", 0), getattr(os2, "ulCodePageRange2", 0)],
"checksum": head.checkSumAdjustment, "upem": head.unitsPerEm, "numGlyphs": len(order),
"family": name.getDebugName(1) or "", "style": name.getDebugName(2) or "", "version": name.getDebugName(5) or "",
"full": name.getDebugName(4) or "", "glyphs": glyphs})
print(json.dumps(out))
'''
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 utf16(s):
return s.encode("utf-16-le")
def eot(data, facts, version, xor=False, flags=0):
hdr = struct.pack("<IIII", 0, len(data), version, flags | (0x10000000 if xor else 0))
hdr += bytes.fromhex(facts["panose"]) + struct.pack("<BBIHH", 1, facts["italic"], facts["weight"], facts["fsType"], 0x504C)
hdr += struct.pack("<4I", *facts["ranges"]) + struct.pack("<2I", *facts["codepages"]) + struct.pack("<I", facts["checksum"]) + b"\0" * 16
for s in (facts["family"], facts["style"], facts["version"], facts["full"]):
u = utf16(s)
hdr += struct.pack("<HH", 0, len(u)) + u
if version >= 0x00020001:
u = utf16("https://example.com/")
hdr += struct.pack("<HH", 0, len(u)) + u
if version == 0x00020002:
hdr += struct.pack("<IIHH", 0x12345678, 0, 0, 0) + struct.pack("<II", 0, 4) + b"EUDC"
body = bytes(b ^ 0x50 for b in data) if xor else data
out = hdr + body
return struct.pack("<I", len(out)) + out[4:]
def generate(ctx):
dejavu = os.path.join(store("dejavu_fonts"), "share", "fonts", "truetype", "DejaVuSansMono.ttf")
otf = os.path.join(store("source-sans"), "share", "fonts", "opentype", "SourceSans3-Regular.otf")
facts = json.loads(ctx.python(["fonttools"], CODE, input=json.dumps([dejavu, otf]).encode()))
cases = []
for path, f in zip((dejavu, otf), facts):
data = open(path, "rb").read()
base = os.path.basename(path)
for label, ver, xor in (("1.0", 0x00010000, False), ("2.1", 0x00020001, False), ("2.1 xor", 0x00020001, True), ("2.2", 0x00020002, False)):
if base.endswith(".otf") and label != "2.1":
continue
cases.append({"name": "%s %s" % (base, label), "data": eot(data, f, ver, xor), "version": ver, "xor": xor,
"family": f["family"], "style": f["style"], "fullName": f["full"], "weight": f["weight"],
"fsType": f["fsType"], "upem": f["upem"], "numGlyphs": f["numGlyphs"], "glyphs": f["glyphs"],
"fontCrc": __import__("zlib").crc32(data)})
mtx = eot(open(dejavu, "rb").read()[:1000], facts[0], 0x00020001, flags=0x4)
ctx.emit("eot", {"cases": cases, "mtx": mtx})