[subset] Preserve COLR scales beyond Fixed storage

Fully pinned scale deltas can overflow Fixed as well as F2DOT14.
Represent them as bounded affine factors instead of saturating a single
transform. Factor axes independently and preserve fractional bits with
a dyadic pivot. Keep centered translations outside the factors.

Add binary regressions for large scales and composed instances.
All 254 configured Meson tests pass.

Assisted-by: Codex
This commit is contained in:
Behdad Esfahbod committed 2026-10-10 20:33:28 -06:00
1 parent 7ef0b43143
commit 32ce2bccab
5 files changed
+224 -66

No files matched your search

Binary file not shown.
Binary file not shown.
@@ -0,0 +1,69 @@
"""Generate COLRv1 scale variations that exceed Fixed 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 Large 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 * 2147459072, -sign * 2147459072, 3, -5]
else:
paint.update(scale=sign * 1.5)
deltas = [sign * 2147459072, 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-large-scale.ttf")
# A valid near-maximum LONG_WORDS row can repeat the same region index.
# One uncentered paint suffices to exercise both signs without a large fixture.
count = 32766
items = [[2147483647] * count, [-2147483647] * count]
fb.font["COLR"] = buildCOLR(
{names[1]: paints[names[1]]},
version=1,
glyphMap=fb.font.getReverseGlyphMap(),
varStore=buildVarStore(regions, [buildVarData([0] * count, items)]),
clipBoxes={names[1]: (0, 0, 1000, 1000)},
)
fb.save(Path(__file__).parent / "colr-maximum-scale.ttf")
+66 -57
View File
@@ -118,72 +118,81 @@ read_uint_be (const unsigned char *p, unsigned count)
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++)
const char *files[] = {"fonts/colr-wide-scale.ttf", "fonts/colr-large-scale.ttf", "fonts/colr-maximum-scale.ttf"};
for (unsigned file = 0; file < G_N_ELEMENTS (files); file++)
{
hb_face_t *source = hb_face_reference (face);
if (composed)
hb_face_t *face = hb_test_open_font_file (files[file]);
for (unsigned composed = 0; composed < (file == 2 ? 1 : 3); 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, composed == 1 ? HB_TAG ('w','d','t','h') : HB_TAG ('w','g','h','t'), composed == 1 ? 0 : 1));
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','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_true (hb_subset_input_pin_axis_location (input, source, composed == 2 ? HB_TAG ('w','d','t','h') : HB_TAG ('w','g','h','t'), composed == 2 ? 0 : 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);
unsigned count = file == 2 ? 1 : 16;
g_assert_cmpuint (list + 4 + count * 6, <=, length);
g_assert_cmpuint (read_uint_be (data + list, 4), ==, count);
for (unsigned i = 0; i < count; i++)
{
unsigned paint = list + read_uint_be (data + list + 4 + i * 6 + 2, 4);
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], ==, 12); /* PaintTransform */
double magnitude = file == 2 ? 4294705152. : file ? 131072 : 2.5;
double sx = i % 8 < 4 ? magnitude : -magnitude;
double sy = i % 4 < 2 ? -sx : sx;
double cx = i % 2 ? (i < 8 ? 40 : 30003) : 0;
double cy = i % 2 ? (i < 8 ? -34 : -30005) : 0;
double xx = 1, yx = 0, xy = 0, 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);
double values[6];
for (unsigned j = 0; j < 6; j++)
values[j] = (int32_t) read_uint_be (data + transform + j * 4, 4) / 65536.;
dx += xx * values[4] + xy * values[5];
dy += yx * values[4] + yy * values[5];
double next_xx = xx * values[0] + xy * values[1];
double next_yx = yx * values[0] + yy * values[1];
double next_xy = xx * values[2] + xy * values[3];
double next_yy = yx * values[2] + yy * values[3];
xx = next_xx; yx = next_yx; xy = next_xy; yy = next_yy;
paint += read_uint_be (data + paint + 1, 3);
g_assert_cmpuint (paint, <, length);
g_assert_cmpuint (++depth, <=, 20);
}
g_assert_cmpuint (data[paint], ==, 2); /* PaintSolid */
g_assert_cmpfloat (xy, ==, 0);
g_assert_cmpfloat (yx, ==, 0);
g_assert_cmpfloat (xx, ==, sx);
g_assert_cmpfloat (yy, ==, sy);
g_assert_cmpfloat (dx, ==, cx * (1 - sx));
g_assert_cmpfloat (dy, ==, cy * (1 - sy));
}
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_blob_destroy (blob);
hb_face_destroy (subset);
hb_face_destroy (source);
hb_face_destroy (face);
}
hb_face_destroy (face);
}
static hb_face_t *