[subset] Preserve overflowing static COLRv1 scales

Assisted-by: Codex
This commit is contained in:
Behdad Esfahbod committed 2026-10-10 19:28:00 -06:00
1 parent d89fa94461
commit 3ffb7fe26c
4 files changed
+237

No files matched your search

Binary file not shown.
@@ -0,0 +1,56 @@
"""Generate COLRv1 scale variations that exceed F2Dot14 after instancing."""
from pathlib import Path
from fontTools.fontBuilder import FontBuilder
from fontTools.colorLib.builder import buildCOLR, buildCPAL
from fontTools.pens.ttGlyphPen import TTGlyphPen
from fontTools.varLib.builder import buildVarRegionList, buildVarData, buildVarStore
fb = FontBuilder(1000, isTTF=True)
names = [".notdef"] + [f"scale{i}" for i in range(16)]
fb.setupGlyphOrder(names)
fb.setupCharacterMap({0x61 + i: name for i, name in enumerate(names[1:])})
glyphs = {}
for name in names:
pen = TTGlyphPen(None)
glyphs[name] = pen.glyph()
fb.setupGlyf(glyphs)
fb.setupHorizontalMetrics({name: (1000, 0) for name in names})
fb.setupHorizontalHeader(ascent=800, descent=-200)
fb.setupNameTable({"familyName": "COLR Wide Scale", "styleName": "Regular"})
fb.setupOS2(sTypoAscender=800, sTypoDescender=-200, usWinAscent=800, usWinDescent=200)
fb.setupPost()
fb.setupFvar([("wght", 0, 0, 1, "Weight"), ("wdth", 0, 0, 1, "Width")], [])
rows = []
paints = {}
for i, name in enumerate(names[1:]):
format = (17, 19, 21, 23)[i % 4]
sign = 1 if i % 8 < 4 else -1
paint = dict(
Format=format,
Paint=dict(Format=2, PaletteIndex=0, Alpha=1),
VarIndexBase=len(rows),
)
if format in (17, 19):
paint.update(scaleX=sign * 1.5, scaleY=-sign * 1.5)
deltas = [sign * 16384, -sign * 16384, 3, -5]
else:
paint.update(scale=sign * 1.5)
deltas = [sign * 16384, 3, -5, 0]
if format in (19, 23):
paint.update(centerX=37 if i < 8 else 30000, centerY=-29 if i < 8 else -30000)
rows.extend([delta] for delta in deltas)
paints[name] = paint
regions = buildVarRegionList([{"wght": (0, 1, 1)}], ["wght", "wdth"])
store = buildVarStore(regions, [buildVarData([0], rows)])
fb.font["COLR"] = buildCOLR(
paints,
version=1,
glyphMap=fb.font.getReverseGlyphMap(),
varStore=store,
clipBoxes={name: (0, 0, 1000, 1000) for name in names[1:]},
)
fb.font["CPAL"] = buildCPAL([[(1, 0, 0, 1)]])
fb.font.recalcTimestamp = False
fb.font["head"].created = fb.font["head"].modified = 2082844800
fb.save(Path(__file__).parent / "colr-wide-scale.ttf")
+81
View File
@@ -106,6 +106,86 @@ test_subset_colr_keep_no_colr_glyph (void)
hb_face_destroy (face);
}
static unsigned
read_uint_be (const unsigned char *p, unsigned count)
{
unsigned value = 0;
for (unsigned i = 0; i < count; i++)
value = (value << 8) | p[i];
return value;
}
static void
test_subset_colr_wide_scales (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/colr-wide-scale.ttf");
for (unsigned composed = 0; composed < 2; composed++)
{
hb_face_t *source = hb_face_reference (face);
if (composed)
{
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_add_range (hb_subset_input_glyph_set (input), 0, 16);
g_assert_true (hb_subset_input_pin_axis_location (input, source, HB_TAG ('w','d','t','h'), 0));
hb_face_t *partial = hb_subset_test_create_subset (source, input);
hb_face_destroy (source);
source = partial;
}
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_add_range (hb_subset_input_glyph_set (input), 0, 16);
g_assert_true (hb_subset_input_pin_axis_location (input, source, HB_TAG ('w','g','h','t'), 1));
if (!composed)
g_assert_true (hb_subset_input_pin_axis_location (input, source, HB_TAG ('w','d','t','h'), 0));
hb_face_t *subset = hb_subset_test_create_subset (source, input);
hb_blob_t *blob = hb_face_reference_table (subset, HB_TAG ('C','O','L','R'));
unsigned length;
const unsigned char *data = (const unsigned char *) hb_blob_get_data (blob, &length);
g_assert_cmpuint (length, >=, 34);
unsigned list = read_uint_be (data + 14, 4);
g_assert_cmpuint (list + 4 + 16 * 6, <=, length);
g_assert_cmpuint (read_uint_be (data + list, 4), ==, 16);
for (unsigned i = 0; i < 16; i++)
{
unsigned paint = list + read_uint_be (data + list + 4 + i * 6 + 2, 4);
g_assert_cmpuint (paint + 7, <=, length);
g_assert_cmpuint (data[paint], ==, 12); /* PaintTransform */
float sx = i % 8 < 4 ? 2.5f : -2.5f;
float sy = i % 4 < 2 ? -sx : sx;
float cx = i % 2 ? (i < 8 ? 40 : 30003) : 0;
float cy = i % 2 ? (i < 8 ? -34 : -30005) : 0;
float xx = 1, yy = 1, dx = 0, dy = 0;
unsigned depth = 0;
while (data[paint] == 12)
{
g_assert_cmpuint (paint + 7, <=, length);
unsigned transform = paint + read_uint_be (data + paint + 4, 3);
g_assert_cmpuint (transform + 24, <=, length);
float values[6];
for (unsigned j = 0; j < 6; j++)
values[j] = (int32_t) read_uint_be (data + transform + j * 4, 4) / 65536.f;
g_assert_cmpfloat (values[1], ==, 0);
g_assert_cmpfloat (values[2], ==, 0);
dx += xx * values[4];
dy += yy * values[5];
xx *= values[0];
yy *= values[3];
paint += read_uint_be (data + paint + 1, 3);
g_assert_cmpuint (paint, <, length);
g_assert_cmpuint (++depth, <=, 5);
}
g_assert_cmpuint (data[paint], ==, 2); /* PaintSolid */
g_assert_cmpfloat (xx, ==, sx);
g_assert_cmpfloat (yy, ==, sy);
g_assert_cmpfloat (dx, ==, cx * (1 - sx));
g_assert_cmpfloat (dy, ==, cy * (1 - sy));
}
hb_blob_destroy (blob);
hb_face_destroy (subset);
hb_face_destroy (source);
}
hb_face_destroy (face);
}
int
main (int argc, char **argv)
{
@@ -115,6 +195,7 @@ main (int argc, char **argv)
hb_test_add (test_subset_colr_keep_one_colr_glyph);
hb_test_add (test_subset_colr_keep_mixed_glyph);
hb_test_add (test_subset_colr_keep_no_colr_glyph);
hb_test_add (test_subset_colr_wide_scales);
return hb_test_run();
}