From 32ce2bccab0bd39711abcb658efc830a58e63ca6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 10 Oct 2026 20:32:13 -0600 Subject: [PATCH] [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 --- src/OT/Color/COLR/COLR.hh | 98 ++++++++++++-- test/api/fonts/colr-large-scale.ttf | Bin 0 -> 1696 bytes test/api/fonts/colr-maximum-scale.ttf | Bin 0 -> 328812 bytes .../fonts/generate-colr-large-scale-font.py | 69 ++++++++++ test/api/test-subset-colr.c | 123 ++++++++++-------- 5 files changed, 224 insertions(+), 66 deletions(-) create mode 100644 test/api/fonts/colr-large-scale.ttf create mode 100644 test/api/fonts/colr-maximum-scale.ttf create mode 100644 test/api/fonts/generate-colr-large-scale-font.py diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index 76ed6de90..823267b76 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -1193,9 +1193,9 @@ struct PaintTranslate }; template -static bool serialize_scale_transform (hb_subset_context_t *c, - float sx, float sy, float dx, float dy, - const Source &source) +static bool serialize_scale_matrix (hb_subset_context_t *c, + float xx, float yx, float xy, float yy, float dx, float dy, + const Source &source) { auto *out = c->serializer->allocate_size> (7); if (unlikely (!out)) return false; @@ -1208,10 +1208,10 @@ static bool serialize_scale_transform (hb_subset_context_t *c, c->serializer->pop_discard (); return false; } - transform->xx.set_float (sx); - transform->yx.set_float (0); - transform->xy.set_float (0); - transform->yy.set_float (sy); + transform->xx.set_float (xx); + transform->yx.set_float (yx); + transform->xy.set_float (xy); + transform->yy.set_float (yy); transform->dx.set_float (dx); transform->dy.set_float (dy); c->serializer->add_link (out->transform, c->serializer->pop_pack ()); @@ -1225,6 +1225,83 @@ static bool serialize_scale_transform (hb_subset_context_t *c, return ret; } +template +static bool serialize_scale_transform (hb_subset_context_t *c, + float sx, float sy, float dx, float dy, + const Source &source) +{ + return serialize_scale_matrix (c, sx, 0, 0, sy, dx, dy, source); +} + +struct colr_scale_matrix_t +{ + float xx, yx, xy, yy; +}; + +static bool append_scale_matrices (hb_vector_t &matrices, + float value, bool y_axis) +{ + auto append = [&] (colr_scale_matrix_t matrix) { + if (y_axis) matrix = {matrix.yy, matrix.xy, matrix.yx, matrix.xx}; + if (matrix.xx != 1 || matrix.yx != 0 || matrix.xy != 0 || matrix.yy != 1) + matrices.push (matrix); + }; + if (value >= -32768 && value < 32768) + append ({value, 0, 0, 1}); + else + { + float scaled = value; + int shifts = 0; + while (fabsf (scaled) >= 268435456.f) + { + scaled /= 2; + if (++shifts > 16) return false; + } + float unit = ldexpf (1.f, -hb_max (0, shifts - 2)); + float k = 16384; + float m = truncf (scaled / k); + float r = (scaled - k * m) / unit; + float x = truncf (r / m); + k += x * unit; + r -= x * m; + /* Eliminating the -unit pivot gives diag(value, 1). The pivot retains + * fractional bits even when the scale itself needs downscaling. */ + append ({ldexpf (1.f, shifts), 0, 0, 1 / unit}); + append ({1, 0, k, 1}); + append ({unit, 0, 0, 1}); + append ({k, -unit, r, m}); + append ({1 / unit, 0, 0, 1}); + append ({1, 0, m, 1}); + append ({unit, 0, 0, 1}); + append ({0, 1, -1, 0}); + } + return !matrices.in_error (); +} + +template +static bool serialize_scale_matrices (hb_subset_context_t *c, + hb_array_t matrices, + const Source &source) +{ + if (!matrices) return source (); + const auto &matrix = matrices[0]; + auto child = [&] { return serialize_scale_matrices (c, matrices.sub_array (1), source); }; + return serialize_scale_matrix (c, matrix.xx, matrix.yx, matrix.xy, matrix.yy, 0, 0, child); +} + +template +static bool serialize_scale_graph (hb_subset_context_t *c, float sx, float sy, + const Source &source) +{ + if (sx >= -32768 && sx < 32768 && sy >= -32768 && sy < 32768) + return serialize_scale_transform (c, sx, sy, 0, 0, source); + hb_vector_t matrices; + if (!matrices.alloc (16) || + !append_scale_matrices (matrices, sx, false) || + !append_scale_matrices (matrices, sy, true)) return false; + return serialize_scale_matrices (c, matrices.as_array (), source); +} + template static bool subset_scale_as_transform (hb_subset_context_t *c, const Base *base, @@ -1236,8 +1313,11 @@ static bool subset_scale_as_transform (hb_subset_context_t *c, if (src.is_null ()) return false; auto source = [&] { return c->dispatch (base+src, instancer); }; float dx = cx * (1 - sx), dy = cy * (1 - sy); - if (dx >= -32768 && dx < 32768 && dy >= -32768 && dy < 32768) + if (sx >= -32768 && sx < 32768 && sy >= -32768 && sy < 32768 && + dx >= -32768 && dx < 32768 && dy >= -32768 && dy < 32768) return serialize_scale_transform (c, sx, sy, dx, dy, source); + if (cx == 0 && cy == 0) + return serialize_scale_graph (c, sx, sy, source); // Preserve the center as separate translations when the combined matrix // exceeds Fixed. Splitting also makes the inverse of -32768 representable. @@ -1248,7 +1328,7 @@ static bool subset_scale_as_transform (hb_subset_context_t *c, return serialize_scale_transform (c, 1, 1, -cx / 2, -cy / 2, inner); }; auto scale = [&] { - return serialize_scale_transform (c, sx, sy, 0, 0, inverse); + return serialize_scale_graph (c, sx, sy, inverse); }; auto outer = [&] { return serialize_scale_transform (c, 1, 1, cx / 2, cy / 2, scale); diff --git a/test/api/fonts/colr-large-scale.ttf b/test/api/fonts/colr-large-scale.ttf new file mode 100644 index 0000000000000000000000000000000000000000..af95481045dc1b8412f72c20598e1ed8a640c113 GIT binary patch literal 1696 zcmah}O=uHA6#iy6P5;t1Z6ocW!cto4K`?F8TA^Tn=tTl8sSrWIENPl8q&B4Kk06?J zPX#@A@DRb1CqWPDK?Hm7P^3pek%E_=L`3o6A%1UX8{-=FEj#b~=FKF1sTHH2|h@hSd&#k3x zVbn7R46NGQd33>Ge1hD%X0K*Ge@`$sL->AvZM~R$Gxd!52gsj8_Qv-@_v6j09h2W+ z>e+7$J}NPSV=tIy%FM1&V_Fr?smHTVW~w#i_Hx#g#8U(q zqGl#_+<%&yn72-pnjyiSCPqzfSu_*Wv{&bvtueY8*AOHATUT-~rKK}8w ooLJ65PKsX&;QnKTg?VBI|3N&#EnZ-Wxh63V-;(=$Y`RbV1SmTs`Tzg` literal 0 HcmV?d00001 diff --git a/test/api/fonts/colr-maximum-scale.ttf b/test/api/fonts/colr-maximum-scale.ttf new file mode 100644 index 0000000000000000000000000000000000000000..cd52df1a5ffad0fb439c6955092f1849cef74fc7 GIT binary patch literal 328812 zcmeIwPiq`y9KiA4vzzpvX`7aa9u$^P&-PDRD=4;>-U2C!5D$V`lI?CFX+yfTRfP5> z_!8q`#!^K0+nA2EYd5Yg=Gobpp6cazdTHhL^VhFl|1F}tpX;^t?fy>8#N%B5nCpd` zAN6;CdGb@0*DvJy@^I_pn>pszsn=yR=x?m{KYx<*`ua-E;{^?tp|&(G%W%y$3&PMnTTp1+*y-mU)j;MYHv-^=s$`um;RcgCwfT=^=W ze=65sNB!IK{mF|j?|gXjt>2^D-2KznzId2tVsEVHxpnJ1PqzF_w^!voo9pvfeWz!) zGC6W;|Ipqi_Yc(HRX)p#^^^SH_P4vglUshEA3Nu;IM=jr7*m9KieQ&qX|S5@`sAHOkA{O5KgZnmE(X=JZaCl2Ro@{4kJC|+vkC0o2& zKgHD1JkpE#cHW5-@oqEE>u2IdJ1=o8KBz_Z7W=!yL2qTfzcpAM4DW9BcUK3S!_jzk zb7MS;Yq1oIv7C2Y%x1eWj6u#2jzXE=S7Ia;&^iX3Ft$qMR(V<=, 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 *