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 000000000..af9548104 Binary files /dev/null and b/test/api/fonts/colr-large-scale.ttf differ diff --git a/test/api/fonts/colr-maximum-scale.ttf b/test/api/fonts/colr-maximum-scale.ttf new file mode 100644 index 000000000..cd52df1a5 Binary files /dev/null and b/test/api/fonts/colr-maximum-scale.ttf differ diff --git a/test/api/fonts/generate-colr-large-scale-font.py b/test/api/fonts/generate-colr-large-scale-font.py new file mode 100644 index 000000000..0af1881c0 --- /dev/null +++ b/test/api/fonts/generate-colr-large-scale-font.py @@ -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") diff --git a/test/api/test-subset-colr.c b/test/api/test-subset-colr.c index 134f9bf26..8a3095d88 100644 --- a/test/api/test-subset-colr.c +++ b/test/api/test-subset-colr.c @@ -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 *