diff --git a/src/hb-cff2-interp-cs.hh b/src/hb-cff2-interp-cs.hh index 2b705d49f..a04732c9a 100644 --- a/src/hb-cff2-interp-cs.hh +++ b/src/hb-cff2-interp-cs.hh @@ -204,6 +204,9 @@ struct cff2_cs_interp_env_t : cs_interp_env_t /* Subsetter instancing can defer rounding until absolute points are known. */ bool round_blends = true; + /* Some analysis passes need only blend presence, not symbolic deltas. */ + bool preserve_blends = true; + protected: const int *coords; unsigned int num_coords; @@ -222,6 +225,9 @@ struct cff2_cs_interp_env_t : cs_interp_env_t template , PARAM>> struct cff2_cs_opset_t : cs_opset_t, PARAM, PATH> { + /* Retained subroutine collection needs only blend identity. */ + static constexpr bool preserve_blends = false; + static void process_op (op_code_t op, cff2_cs_interp_env_t &env, PARAM& param) { switch (op) { @@ -269,8 +275,20 @@ struct cff2_cs_opset_t : cs_opset_t, PAR { double v = arg.to_real () + env.blend_deltas (blends); arg.set_real (env.round_blends ? round (v) : v); + return; } - else if (unlikely (arg.blending ())) + + /* Without coordinates, the flat delta vector cannot represent a + * product of region scalars. Do not discard a delta's own blend. */ + if (OPSET::preserve_blends && env.preserve_blends) + for (const ELEM &delta : blends) + if (unlikely (delta.blending ())) + { + env.set_error (); + return; + } + + if (unlikely (arg.blending ())) { /* A blend result used as an operand of a later blend: the value is * default + inner deltas + outer deltas, so accumulate the deltas. */ diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc index 70b6b735c..3aa392b10 100644 --- a/src/hb-subset-cff2.cc +++ b/src/hb-subset-cff2.cc @@ -329,6 +329,8 @@ struct cff2_usage_param_t struct cff2_cs_opset_usage_t : cff2_cs_opset_t { + static constexpr bool preserve_blends = true; + static void flush_args_and_op (op_code_t op, cff2_cs_interp_env_t &env, cff2_usage_param_t& param) { param.has_flex1 |= op == OpCode_flex1; @@ -426,6 +428,8 @@ struct cff2_top_dict_op_serializer_t : cff_top_dict_op_serializer_t<> struct cff2_cs_opset_flatten_t : cff2_cs_opset_t { + static constexpr bool preserve_blends = true; + static void flush_args_and_op (op_code_t op, cff2_cs_interp_env_t &env, flatten_param_t& param) { bool skip_command = false; @@ -1224,6 +1228,7 @@ struct cff2_subset_plan used_fds.add (fd); cff2_cs_interp_env_t env (str, acc, fd); + env.preserve_blends = partial_instancing; cs_interpreter_t, cff2_cs_opset_usage_t, cff2_usage_param_t> interp (env); cff2_usage_param_t param; param.instancing = partial_instancing ? &instancing : nullptr; @@ -2024,6 +2029,7 @@ OT::cff2::accelerator_subset_t::subset (hb_subset_context_t *c) const if (unlikely (!cff2_plan.create (*this, c->plan))) { + /* A planning failure is not an intentionally empty outline table. */ c->serializer->err (HB_SERIALIZE_ERROR_OTHER); return false; } diff --git a/test/api/fonts/cff2-nested-delta.otf b/test/api/fonts/cff2-nested-delta.otf new file mode 100644 index 000000000..9b41367a2 Binary files /dev/null and b/test/api/fonts/cff2-nested-delta.otf differ diff --git a/test/api/generate-cff2-nested-delta-font.py b/test/api/generate-cff2-nested-delta-font.py new file mode 100644 index 000000000..269f8f588 --- /dev/null +++ b/test/api/generate-cff2-nested-delta-font.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +"""Generate nested-default and nested-delta CFF2 regression charstrings.""" +from pathlib import Path + +from fontTools.fontBuilder import FontBuilder +from fontTools.misc.psCharStrings import T2CharString + +builder = FontBuilder(1000, isTTF=False) +builder.font.recalcBBoxes = builder.font.recalcTimestamp = False +builder.font["head"].created = builder.font["head"].modified = 2082844800 +glyphs = [".notdef", "linear", "product"] +builder.setupGlyphOrder(glyphs) +builder.setupCharacterMap({65: "linear", 66: "product"}) +builder.setupHorizontalMetrics({glyph: (500, 0) for glyph in glyphs}) +builder.setupHorizontalHeader(ascent=800, descent=-200) +builder.setupNameTable({"familyName": "Nested CFF2 delta", "styleName": "Regular"}) +builder.setupOS2() +builder.setupPost() +builder.setupFvar([("wght", 0, 0, 1, "Weight"), ("wdth", 0, 0, 1, "Width")], []) +move = [0, 0, "rmoveto"] +finish = ["hlineto", 100, "vlineto", -100, "hlineto", -100, "vlineto"] +programs = { + ".notdef": [], + # 100 + 20*wght + 30*wdth: a nested default remains linear. + "linear": move + [100, 20, 0, 1, "blend", 0, 30, 1, "blend"] + finish, + # 100 + (10 + 20*wght)*wdth: a nested delta requires a product. + "product": move + [100, 0, 10, 20, 0, 1, "blend", 1, "blend"] + finish, +} +builder.setupCFF2( + {glyph: T2CharString(program=program) for glyph, program in programs.items()}, + regions=[{"wght": (0, 1, 1)}, {"wdth": (0, 1, 1)}], +) +builder.save(Path(__file__).parent / "fonts" / "cff2-nested-delta.otf") diff --git a/test/api/test-instance-cff2.c b/test/api/test-instance-cff2.c index d62c1dc00..c22a3b80f 100644 --- a/test/api/test-instance-cff2.c +++ b/test/api/test-instance-cff2.c @@ -308,6 +308,57 @@ test_instance_cff2_absolute_rounding (void) +static void +test_instance_cff2_nested_delta (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/cff2-nested-delta.otf"); + for (unsigned mode = 0; mode < 5; mode++) + { + g_test_message ("nested delta mode %u", mode); + hb_set_t *codepoints = hb_set_create (); + hb_set_add (codepoints, mode == 4 ? 65 : 66); + hb_subset_input_t *input = hb_subset_test_create_input (codepoints); + hb_set_destroy (codepoints); + if (mode == 1 || mode == 2 || mode == 4) + g_assert_true (hb_subset_input_pin_axis_location (input, face, + HB_TAG ('w', 'g', 'h', 't'), 0.25f)); + if (mode == 2) + g_assert_true (hb_subset_input_pin_axis_location (input, face, + HB_TAG ('w', 'd', 't', 'h'), 1)); + if (mode == 3) + hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_DESUBROUTINIZE); + + hb_face_t *subset = hb_subset_or_fail (face, input); + hb_subset_input_destroy (input); + if (mode == 1 || mode == 3) + { + /* The flat symbolic representation cannot retain a variable delta. + * Fail the operation instead of changing the outline or dropping CFF2. */ + g_assert_null (subset); + continue; + } + g_assert_nonnull (subset); + hb_blob_t *cff2 = hb_face_reference_table (subset, HB_TAG ('C', 'F', 'F', '2')); + g_assert_cmpuint (hb_blob_get_length (cff2), >, 0); + hb_blob_destroy (cff2); + hb_font_t *font = hb_font_create (subset); + hb_variation_t variations[] = { + {HB_TAG ('w', 'g', 'h', 't'), 0.25f}, + {HB_TAG ('w', 'd', 't', 'h'), 1} + }; + hb_font_set_variations (font, variations, G_N_ELEMENTS (variations)); + hb_codepoint_t glyph; + hb_glyph_extents_t extents; + g_assert_true (hb_font_get_nominal_glyph (font, mode == 4 ? 65 : 66, &glyph)); + g_assert_true (hb_font_get_glyph_extents (font, glyph, &extents)); + g_assert_cmpint (extents.width, ==, mode == 4 ? 135 : 115); + hb_font_destroy (font); + hb_face_destroy (subset); + } + hb_face_destroy (face); +} + + int main (int argc, char **argv) { @@ -318,6 +369,7 @@ main (int argc, char **argv) hb_test_add (test_instance_cff2_empty_bounds); hb_test_add (test_instance_cff2_large_store); hb_test_add (test_instance_cff2_absolute_rounding); + hb_test_add (test_instance_cff2_nested_delta); return hb_test_run(); }