Files
harfbuzz/test/api/generate-cff-encoding-glyph-count-fonts.py
Behdad Esfahbod eb9dc91893 [subset] Bound CFF1 encoding lookups by the glyph count (#6543)
An explicit glyph map can create empty output glyphs beyond the source
font's glyph count. When preserving codes for gaps in the mapping, the
subsetter queries those nonexistent source glyphs. Excess entries in a
malformed custom encoding can then give the new empty glyphs codes.

Return CFF_UNDEF_CODE for glyphs outside the source font before reading
the encoding. Add format 0 and format 1 regression cases comparing
ordinary and explicitly remapped subsets against bounded encodings,
with a generator for the small synthetic fonts.

Both new cases fail before the fix. All 275 tests pass with
meson test -C build --print-errorlogs --num-processes 12.

Related: https://github.com/googlefonts/fontations/pull/2398
Assisted-by: Codex
2026-11-01 14:14:09 -07:00

74 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Generate CFF encodings with and without excess codes (requires fontTools)."""
from pathlib import Path
from struct import pack
from fontTools.fontBuilder import FontBuilder
from fontTools.ttLib.tables.DefaultTable import DefaultTable
OUT = Path(__file__).parent / "fonts"
def index(items):
if not items:
return b"\0\0"
offsets = [1]
for item in items:
offsets.append(offsets[-1] + len(item))
assert offsets[-1] < 256
return pack(">H", len(items)) + b"\1" + bytes(offsets) + b"".join(items)
for fmt in (0, 1):
for excess in (False, True):
f = FontBuilder(1000, isTTF=False)
f.font.sfntVersion = "OTTO"
f.font.recalcBBoxes = False
f.font.recalcTimestamp = False
f.font["head"].created = f.font["head"].modified = 2082844800
glyphs = [".notdef", "A", "B"]
f.setupGlyphOrder(glyphs)
f.setupCharacterMap({65: "A", 66: "B"})
f.setupHorizontalMetrics({g: (500, 0) for g in glyphs})
f.setupHorizontalHeader(ascent=800, descent=-200)
f.setupNameTable({"familyName": "CFF encoding regression", "styleName": "Regular"})
f.setupOS2()
f.setupPost()
# Write the CFF directly so the encoding can describe nonexistent glyphs.
name = index([b"CFFEncodingRegression"])
charset = b"\0\0\x22\0\x23" # Format 0: SIDs 34 (A), 35 (B).
if fmt == 0:
encoding = bytes([0, 4, 65, 66, 67, 68] if excess else [0, 2, 65, 66])
else:
encoding = bytes([1, 1, 65, 3 if excess else 1])
charstrings = index([b"\x0e"] * 3) # Three empty glyphs (endchar).
# Three long-integer operands and their operators make an 18-byte DICT.
charset_offset = 4 + len(name) + len(index([bytes(18)])) + 4
encoding_offset = charset_offset + len(charset)
charstrings_offset = encoding_offset + len(encoding)
top_dict = b"".join(
b"\x1d" + pack(">I", offset) + bytes([op])
for offset, op in (
(charset_offset, 15),
(encoding_offset, 16),
(charstrings_offset, 17),
)
)
cff = DefaultTable("CFF ")
cff.data = (
b"\1\0\4\4"
+ name
+ index([top_dict])
+ index([]) # String INDEX.
+ index([]) # Global Subr INDEX.
+ charset
+ encoding
+ charstrings
)
f.font["CFF "] = cff
suffix = "excess" if excess else "bounded"
f.save(OUT / f"cff1_encoding{fmt}_{suffix}.otf")