diff --git a/src/OT/glyf/glyf.hh b/src/OT/glyf/glyf.hh index 3a9916150..ce6ab83df 100644 --- a/src/OT/glyf/glyf.hh +++ b/src/OT/glyf/glyf.hh @@ -641,6 +641,7 @@ struct glyf_accelerator_t } unsigned int get_num_glyphs () const { return num_glyphs; } + unsigned int get_table_length () const { return glyf_table.get_length (); } hb_blob_t *reference_glyf_table () const { return hb_blob_reference (glyf_table.get_blob ()); } diff --git a/src/hb-limits.hh b/src/hb-limits.hh index c19b1c1e1..af6a54465 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -112,6 +112,14 @@ #define HB_MAX_COMPOSITE_OPERATIONS_PER_GLYPH 64 #endif +/* Source bytes of all the glyphs the subsetter retains, as a multiple of + * the glyf table length; exceeding it fails the plan. A monotonic loca + * never needs more than one; a loca that aliases the same bytes to many + * glyph IDs makes trimming and hint dropping cost that many table scans. */ +#ifndef HB_SUBSET_GLYF_MAX_BYTES_FACTOR +#define HB_SUBSET_GLYF_MAX_BYTES_FACTOR 16 +#endif + #ifndef HB_SVG_MAX_PATH_SEGMENTS #define HB_SVG_MAX_PATH_SEGMENTS 262144 #endif diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index 7dd6908ca..849dde150 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -391,6 +391,7 @@ _glyf_add_gid_and_children (const OT::glyf_accelerator_t &glyf, hb_codepoint_t gid, hb_set_t *gids_to_retain, int operation_count, + int64_t &bytes_budget, unsigned depth = 0) { /* Check if is already visited */ @@ -398,17 +399,24 @@ _glyf_add_gid_and_children (const OT::glyf_accelerator_t &glyf, gids_to_retain->add (gid); + auto glyph = glyf.glyph_for_gid (gid); + + /* Every retained glyph is walked here and again when glyf is subset. + * Charge its untrimmed bytes before the operation count can bail out, + * so that a glyph retained past that point is still paid for. */ + if (unlikely (!hb_budget_spend (bytes_budget, HB_BUDGET_1, glyph.get_bytes ().length))) + return operation_count; + if (unlikely (depth++ > HB_MAX_NESTING_LEVEL)) return operation_count; if (unlikely (--operation_count < 0)) return operation_count; - auto glyph = glyf.glyph_for_gid (gid); - for (auto &item : glyph.get_composite_iterator ()) operation_count = _glyf_add_gid_and_children (glyf, item.get_gid (glyph.is_extended ()), gids_to_retain, operation_count, + bytes_budget, depth); return operation_count; @@ -494,9 +502,19 @@ _populate_gids_to_retain (hb_subset_plan_t* plan, /* Populate a full set of glyphs to retain by adding all referenced * composite glyphs. */ if (glyf.has_data ()) + { + /* With a monotonic loca the retained glyphs add up to at most the + * table; loca need not be monotonic though, and any number of glyph + * IDs may alias the same bytes, each of them trimmed separately. */ + int64_t bytes_budget = (int64_t) glyf.get_table_length () * + HB_SUBSET_GLYF_MAX_BYTES_FACTOR; for (hb_codepoint_t gid : cur_glyphset) _glyf_add_gid_and_children (glyf, gid, &plan->_glyphset, - cur_glyphset.get_population () * HB_MAX_COMPOSITE_OPERATIONS_PER_GLYPH); + cur_glyphset.get_population () * HB_MAX_COMPOSITE_OPERATIONS_PER_GLYPH, + bytes_budget); + if (unlikely (!plan->check_success (bytes_budget >= 0))) + return; + } else plan->_glyphset.union_ (cur_glyphset); #ifndef HB_NO_SUBSET_CFF diff --git a/test/api/test-subset-glyf.c b/test/api/test-subset-glyf.c index da403846c..bde57feef 100644 --- a/test/api/test-subset-glyf.c +++ b/test/api/test-subset-glyf.c @@ -624,6 +624,50 @@ test_subset_glyf_recompute_default_instance_bounds (void) hb_face_destroy (source); } +static void +test_subset_glyf_aliased_loca (void) +{ + /* A loca of 0, 16, 0, 16, ...: every even glyph is the same 16 bytes and + * every odd one starts after its end, so reads as empty. */ + enum { num_glyphs = 101 }; + static const char maxp_data[] = {0x00, 0x00, 0x50, 0x00, 0x00, num_glyphs}; + static const char head_data[54] = { + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x5F, 0x0F, 0x3C, (char) 0xF5, 0x00, 0x00, 0x03, (char) 0xE8, + }; + static const char glyf_data[16] = { + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x31, + }; + char loca_data[2 * (num_glyphs + 1)] = {0}; + for (unsigned i = 1; i <= num_glyphs; i += 2) + loca_data[2 * i + 1] = sizeof (glyf_data) / 2; + + hb_face_t *face = hb_face_builder_create (); + HB_FACE_ADD_TABLE (face, "maxp", maxp_data); + HB_FACE_ADD_TABLE (face, "head", head_data); + HB_FACE_ADD_TABLE (face, "glyf", glyf_data); + HB_FACE_ADD_TABLE (face, "loca", loca_data); + + /* A few glyphs sharing the same bytes are each copied out. */ + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add_range (hb_subset_input_glyph_set (input), 0, 4); + hb_face_t *subset = hb_subset_test_create_subset (face, input); + hb_blob_t *blob = hb_face_reference_table (subset, HB_TAG ('g','l','y','f')); + g_assert_cmpuint (hb_blob_get_length (blob), ==, 2 * sizeof (glyf_data)); + hb_blob_destroy (blob); + hb_face_destroy (subset); + + /* Enough of them to add up to many times the table are rejected. */ + input = hb_subset_input_create_or_fail (); + hb_set_add_range (hb_subset_input_glyph_set (input), 0, num_glyphs - 1); + g_assert_null (hb_subset_plan_create_or_fail (face, input)); + g_assert_null (hb_subset_or_fail (face, input)); + hb_subset_input_destroy (input); + + hb_face_destroy (face); +} + int main (int argc, char **argv) { @@ -647,6 +691,7 @@ main (int argc, char **argv) hb_test_add (test_subset_glyf_retain_gids_truncates); hb_test_add (test_subset_glyf_zero_contour_instructions); hb_test_add (test_subset_glyf_recompute_default_instance_bounds); + hb_test_add (test_subset_glyf_aliased_loca); #ifdef HB_EXPERIMENTAL_API hb_test_add (test_subset_glyf_iftb_requirements);