diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index ec5afd810..02294f904 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -441,8 +441,7 @@ struct parsed_cs_op_t : op_str_t bool is_hinting () const { return hinting_flag; } void set_hinting () { hinting_flag = true; } - /* The layout of this struct is designed to fit within the - * padding of op_str_t! */ + /* Metadata for retained charstring tokens. */ protected: bool hinting_flag = false; @@ -467,6 +466,7 @@ struct parsed_cs_str_t : parsed_values_t void add_op (op_code_t op, const byte_str_ref_t& str_ref) { if (is_parsed ()) return; + last_op_ = op; if (coalescing_) { /* Do not record individual tokens; only note their boundaries. @@ -482,10 +482,20 @@ struct parsed_cs_str_t : parsed_values_t SUPER::add_op (op, str_ref); } + bool has_immediate_subr_number () const + { + if (is_parsed ()) return true; + op_code_t op = coalescing_ ? last_op_ : + values.length ? values.tail ().op : OpCode_Invalid; + return op == OpCode_shortint || op == OpCode_fixedcs || + (OpCode_OneByteIntFirst <= op && op <= OpCode_TwoByteNegInt3); + } + void add_call_op (op_code_t op, const byte_str_ref_t& str_ref, unsigned int subr_num) { if (is_parsed ()) return; has_calls_ = true; + last_op_ = op; if (coalescing_) { @@ -601,6 +611,7 @@ struct parsed_cs_str_t : parsed_values_t * hint analysis. */ bool coalescing_ : 1; /* End offsets of the last two tokens seen (coalescing mode). */ + op_code_t last_op_ = OpCode_Invalid; unsigned penultimate_end_ = 0; unsigned last_end_ = 0; op_code_t prefix_op_; @@ -825,8 +836,9 @@ struct subr_subsetter_t * Phase #3 walks charstrings/subroutines forward then backward (hence parsing required), * because we can't tell if a number belongs to a hint op until we see the first moveto. * - * Assumption: a callsubr/callgsubr operator must immediately follow a (biased) subroutine number - * within the same charstring/subroutine, e.g., not split across a charstring and a subroutine. + * Retained calls require an immediately preceding subroutine-number token + * within the same program. Caller-supplied operands are rejected; use + * desubroutinization for those programs. */ bool subset (void) { diff --git a/src/hb-subset-cff1.cc b/src/hb-subset-cff1.cc index 505c4bdc5..dd64312c7 100644 --- a/src/hb-subset-cff1.cc +++ b/src/hb-subset-cff1.cc @@ -348,6 +348,11 @@ struct cff1_cs_opset_subr_subset_t : cff1_cs_opset_thas_immediate_subr_number ())) + { + env.set_error (); + return; + } byte_str_ref_t str_ref = env.str_ref; env.call_subr (subrs, type); param.current_parsed_str->add_call_op (op, str_ref, env.context.subr_num); @@ -1009,6 +1014,7 @@ OT::cff1::accelerator_subset_t::subset (hb_subset_context_t *c) const if (unlikely (!cff_plan.create (*this, c->plan))) { DEBUG_MSG(SUBSET, nullptr, "Failed to generate a cff subsetting plan."); + c->serializer->err (HB_SERIALIZE_ERROR_OTHER); return false; } diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc index 3e3f7f913..70b6b735c 100644 --- a/src/hb-subset-cff2.cc +++ b/src/hb-subset-cff2.cc @@ -815,6 +815,11 @@ struct cff2_cs_opset_subr_subset_t : cff2_cs_opset_t &env, subr_subset_param_t& param, cff2_biased_subrs_t& subrs, hb_set_t *closure) { + if (unlikely (!param.current_parsed_str->has_immediate_subr_number ())) + { + env.set_error (); + return; + } byte_str_ref_t str_ref = env.str_ref; env.call_subr (subrs, type); param.current_parsed_str->add_call_op (op, str_ref, env.context.subr_num); @@ -2017,7 +2022,11 @@ OT::cff2::accelerator_subset_t::subset (hb_subset_context_t *c) const { cff2_subset_plan cff2_plan; - if (unlikely (!cff2_plan.create (*this, c->plan))) return false; + if (unlikely (!cff2_plan.create (*this, c->plan))) + { + c->serializer->err (HB_SERIALIZE_ERROR_OTHER); + return false; + } // If instantiating (pinned) and downgrade flag is set, convert to CFF1 if (cff2_plan.pinned && (c->plan->flags & HB_SUBSET_FLAGS_DOWNGRADE_CFF2)) diff --git a/test/api/fonts/cff1_caller_subr.otf b/test/api/fonts/cff1_caller_subr.otf new file mode 100644 index 000000000..0b24987de Binary files /dev/null and b/test/api/fonts/cff1_caller_subr.otf differ diff --git a/test/api/fonts/cff2_caller_subr.otf b/test/api/fonts/cff2_caller_subr.otf new file mode 100644 index 000000000..ebafc6511 Binary files /dev/null and b/test/api/fonts/cff2_caller_subr.otf differ diff --git a/test/api/generate-cff-subset-regression-fonts.py b/test/api/generate-cff-subset-regression-fonts.py new file mode 100644 index 000000000..d627fd1ca --- /dev/null +++ b/test/api/generate-cff-subset-regression-fonts.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +"""Generate small synthetic CFF regression fonts (requires fontTools).""" +from pathlib import Path +from fontTools.fontBuilder import FontBuilder +from fontTools.misc.psCharStrings import T2CharString +from fontTools.cffLib import SubrsIndex, FDSelect + +OUT = Path(__file__).parent / "fonts" + + +def builder(): + f = FontBuilder(1000, isTTF=False) + f.font.recalcBBoxes = False + f.font.recalcTimestamp = False + f.font["head"].created = f.font["head"].modified = 2082844800 + gs = [".notdef", "A", "B"] + f.setupGlyphOrder(gs) + f.setupCharacterMap({65: "A", 66: "B"}) + f.setupHorizontalMetrics({g: (500, 0) for g in gs}) + f.setupHorizontalHeader(ascent=800, descent=-200) + f.setupNameTable({"familyName": "CFF regression", "styleName": "Regular"}) + f.setupOS2() + f.setupPost() + return f + + +for cff2 in [False, True]: + for kind in ["caller-dependent"]: + f = builder() + end = [] if cff2 else ["endchar"] + ret = [] if cff2 else ["return"] + # An unused first subroutine forces renumbering of caller-supplied IDs. + if kind == "caller-dependent": + progs = [ + ret, + [10, 0, "rlineto"] + ret, + [20, 0, "rlineto"] + ret, + ["callsubr"] + ret, + ] + glyphs = { + ".notdef": end, + "A": [0, 0, "rmoveto", -106, -104, "callsubr"] + end, + "B": [0, 0, "rmoveto", -105, -104, "callsubr"] + end, + } + elif kind == "shared-hints": + progs = [[0, 10] + ret] + glyphs = { + ".notdef": end, + "A": [-107, "callsubr", "hstem", 0, 0, "rmoveto", 10, 0, "rlineto"] + + end, + "B": [-107, "callsubr", "rmoveto", 20, 0, "rlineto"] + end, + } + cs = {g: T2CharString(program=p) for g, p in glyphs.items()} + if cff2: + f.setupCFF2(cs) + top = f.font["CFF2"].cff.topDictIndex[0] + private = top.FDArray[0].Private + else: + f.setupCFF("CFFRegression", {}, cs, {}) + top = f.font["CFF "].cff.topDictIndex[0] + private = top.Private + private.Subrs = SubrsIndex() + for p in progs: + private.Subrs.append( + T2CharString(program=p, private=private, globalSubrs=top.GlobalSubrs) + ) + f.save( + OUT + / ( + ("cff2" if cff2 else "cff1") + + ( + "_caller_subr.otf" + if kind == "caller-dependent" + else "_shared_hint_operands.otf" + ) + ) + ) +# Multiple blend groups accumulate fourteen live results, not 574 operands. +f = builder() +f.setupFvar([("wght", 100, 400, 900, "Weight")], []) +f.setupCFF2( + { + g: T2CharString( + program=[0, 0, "rmoveto", 10, 0, "rlineto"] if g != ".notdef" else [] + ) + for g in [".notdef", "A", "B"] + }, + regions=[{"wght": (0, 1, 1)}] * 40, +) +private = f.font["CFF2"].cff.topDictIndex[0].FDArray[0].Private +private.BlueValues = [[i] + [0] * 40 for i in range(14)] +f.save(OUT / "cff2_long_private_blends.otf") diff --git a/test/api/test-subset-cff1.c b/test/api/test-subset-cff1.c index 88c79bbb9..fb130950f 100644 --- a/test/api/test-subset-cff1.c +++ b/test/api/test-subset-cff1.c @@ -412,11 +412,42 @@ test_subset_cff1_iftb_requirements (void) #endif +/* Caller-dependent subroutine numbers cannot be remapped at a shared offset. */ +static void +test_subset_cff1_caller_supplied_subr (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/cff1_caller_subr.otf"); + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add_range (hb_subset_input_unicode_set (input), 'A', 'B'); + for (unsigned i = 0; i < 2; i++) + { + hb_subset_input_set_flags (input, i ? HB_SUBSET_FLAGS_NO_HINTING : HB_SUBSET_FLAGS_DEFAULT); + hb_face_t *subset = hb_subset_or_fail (face, input); + g_assert_null (subset); + } + /* Explicit flattening does not remap caller-supplied operands. */ + hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_DESUBROUTINIZE); + hb_face_t *subset = hb_subset_or_fail (face, input); + g_assert_nonnull (subset); + hb_font_t *font = hb_font_create (subset); + hb_glyph_extents_t extents; + g_assert_true (hb_font_get_glyph_extents (font, 1, &extents)); + g_assert_cmpint (extents.width, ==, 10); + g_assert_true (hb_font_get_glyph_extents (font, 2, &extents)); + g_assert_cmpint (extents.width, ==, 20); + hb_font_destroy (font); + hb_face_destroy (subset); + hb_subset_input_destroy (input); + hb_face_destroy (face); +} + int main (int argc, char **argv) { hb_test_init (&argc, &argv); + hb_test_add (test_subset_cff1_caller_supplied_subr); + hb_test_add (test_subset_cff1_noop); hb_test_add (test_subset_cff1); hb_test_add (test_subset_cff1_strip_hints); diff --git a/test/api/test-subset-cff2.c b/test/api/test-subset-cff2.c index 6af90e79c..bc3dc8ae6 100644 --- a/test/api/test-subset-cff2.c +++ b/test/api/test-subset-cff2.c @@ -188,6 +188,35 @@ test_subset_cff2_iftb_requirements (void) } #endif +/* Caller-dependent subroutine numbers cannot be remapped at a shared offset. */ +static void +test_subset_cff2_caller_supplied_subr (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/cff2_caller_subr.otf"); + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add_range (hb_subset_input_unicode_set (input), 'A', 'B'); + for (unsigned i = 0; i < 2; i++) + { + hb_subset_input_set_flags (input, i ? HB_SUBSET_FLAGS_NO_HINTING : HB_SUBSET_FLAGS_DEFAULT); + hb_face_t *subset = hb_subset_or_fail (face, input); + g_assert_null (subset); + } + /* Explicit flattening does not remap caller-supplied operands. */ + hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_DESUBROUTINIZE); + hb_face_t *subset = hb_subset_or_fail (face, input); + g_assert_nonnull (subset); + hb_font_t *font = hb_font_create (subset); + hb_glyph_extents_t extents; + g_assert_true (hb_font_get_glyph_extents (font, 1, &extents)); + g_assert_cmpint (extents.width, ==, 10); + g_assert_true (hb_font_get_glyph_extents (font, 2, &extents)); + g_assert_cmpint (extents.width, ==, 20); + hb_font_destroy (font); + hb_face_destroy (subset); + hb_subset_input_destroy (input); + hb_face_destroy (face); +} + static void test_subset_cff2_long_private_blends (void) { @@ -226,6 +255,7 @@ main (int argc, char **argv) { hb_test_init (&argc, &argv); + hb_test_add (test_subset_cff2_caller_supplied_subr); hb_test_add (test_subset_cff2_long_private_blends); hb_test_add (test_subset_cff2_noop);