diff --git a/src/hb-limits.hh b/src/hb-limits.hh index bce927573..430b633c9 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -298,6 +298,21 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) #endif +/* Instancing one whole gvar or cvar table in the subsetter, in points. + * Every tuple is expanded to one entry per point of its glyph (per cvt + * value), however few of them it refers to; a tuple takes a dozen bytes + * and a run of repeated glyf points a fraction of a byte each, so the + * size of the font does not bound the product. The budget is this many + * points per byte of the table, but no less than the minimum; exceeding + * it fails the table. */ +#ifndef HB_BUDGET_SUBSET_TUPLE_POINTS_FACTOR +#define HB_BUDGET_SUBSET_TUPLE_POINTS_FACTOR 64 +#endif +#ifndef HB_BUDGET_SUBSET_TUPLE_POINTS_MIN +#define HB_BUDGET_SUBSET_TUPLE_POINTS_MIN ((int64_t) 1 << 20) +#endif + + /* The instancer rebuilds an ItemVariationStore from a dense delta matrix, * one int per (row, region) pair. In the font a row only pays for the * regions of its own VarData, so a small font can ask for a huge matrix. diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 7ef927fbe..af93e77e7 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1027,7 +1027,8 @@ struct TupleVariationData const hb_vector_t &shared_indices, const hb_array_t shared_tuples, hb_alloc_pool_t *pool = nullptr, - bool is_composite_glyph = false) + bool is_composite_glyph = false, + int64_t *budget = nullptr) { hb_vector_t private_indices; hb_vector_t deltas_x; @@ -1066,6 +1067,11 @@ struct TupleVariationData return false; } + /* Every tuple is expanded to point_count entries, however few + * points it names; charge that to the table budget, if any. */ + if (budget && unlikely (!hb_budget_spend (*budget, HB_BUDGET_1, point_count))) + return false; + tuple_delta_t var; var.axis_tuples = std::move (axis_tuples); if (unlikely (!var.indices.allocate_from_pool (pool, point_count) || @@ -1753,7 +1759,8 @@ struct TupleVariationData const hb_array_t shared_tuples, tuple_variations_t& tuple_variations, /* OUT */ hb_alloc_pool_t *pool = nullptr, - bool is_composite_glyph = false) const + bool is_composite_glyph = false, + int64_t *budget = nullptr) const { return tuple_variations.create_from_tuple_var_data (iterator, tupleVarCount, point_count, is_gvar, @@ -1761,7 +1768,8 @@ struct TupleVariationData shared_indices, shared_tuples, pool, - is_composite_glyph); + is_composite_glyph, + budget); } bool serialize (hb_serialize_context_t *c, diff --git a/src/hb-ot-var-cvar-table.hh b/src/hb-ot-var-cvar-table.hh index 1e7532bca..d4099dad3 100644 --- a/src/hb-ot-var-cvar-table.hh +++ b/src/hb-ot-var-cvar-table.hh @@ -58,7 +58,8 @@ struct cvar hb_blob_t *blob, bool is_gvar, const hb_map_t *axes_old_index_tag_map, - TupleVariationData<>::tuple_variations_t& tuple_variations /* OUT */) const + TupleVariationData<>::tuple_variations_t& tuple_variations, /* OUT */ + int64_t *budget = nullptr) const { hb_vector_t shared_indices; TupleVariationData<>::tuple_iterator_t iterator; @@ -71,7 +72,9 @@ struct cvar axes_old_index_tag_map, shared_indices, hb_array (), - tuple_variations); + tuple_variations, + nullptr, false, + budget); } static bool calculate_cvt_deltas (unsigned axis_count, @@ -152,10 +155,13 @@ struct cvar unsigned point_count = hb_blob_get_length (cvt_blob) / FWORD::static_size; hb_blob_destroy (cvt_blob); + int64_t budget = hb_max ((int64_t) c->source_blob->length * HB_BUDGET_SUBSET_TUPLE_POINTS_FACTOR, + HB_BUDGET_SUBSET_TUPLE_POINTS_MIN); if (!decompile_tuple_variations (axis_count, point_count, c->source_blob, false, &(c->plan->axes_old_index_tag_map), - tuple_variations)) + tuple_variations, + &budget)) return_trace (false); optimize_scratch_t scratch; diff --git a/src/hb-ot-var-gvar-table.hh b/src/hb-ot-var-gvar-table.hh index 8316cf18d..43d28484e 100644 --- a/src/hb-ot-var-gvar-table.hh +++ b/src/hb-ot-var-gvar-table.hh @@ -76,6 +76,9 @@ struct glyph_variations_t hb_alloc_pool_t pool; + /* points all tuples of this table may still be expanded to */ + int64_t budget = 0; + public: unsigned compiled_shared_tuples_count () const { return shared_tuples_count; } @@ -92,11 +95,15 @@ struct glyph_variations_t bool create_from_glyphs_var_data (unsigned axis_count, const hb_array_t shared_tuples, const hb_subset_plan_t *plan, - const hb_hashmap_t& new_gid_var_data_map) + const hb_hashmap_t& new_gid_var_data_map, + unsigned table_length) { if (unlikely (!glyph_variations.alloc_exact (plan->new_to_old_gid_list.length))) return false; + budget = hb_max ((int64_t) table_length * HB_BUDGET_SUBSET_TUPLE_POINTS_FACTOR, + HB_BUDGET_SUBSET_TUPLE_POINTS_MIN); + auto it = hb_iter (plan->new_to_old_gid_list); for (auto &_ : it) { @@ -132,7 +139,8 @@ struct glyph_variations_t shared_indices, shared_tuples, tuple_vars, /* OUT */ &pool, - is_composite_glyph)) + is_composite_glyph, + &budget)) return false; glyph_variations.push (std::move (tuple_vars)); } @@ -363,7 +371,8 @@ struct gvar_GVAR if (new_gid_var_data_map.in_error ()) return false; hb_array_t shared_tuples = (this+sharedTuples).as_array ((unsigned) sharedTupleCount * (unsigned) axisCount); - return glyph_vars.create_from_glyphs_var_data (axisCount, shared_tuples, c->plan, new_gid_var_data_map); + return glyph_vars.create_from_glyphs_var_data (axisCount, shared_tuples, c->plan, new_gid_var_data_map, + c->source_blob->length); } template::tuple_variations_t &tuple_variations, + int64_t *budget) +{ + const OT::cvar* cvar_table = reinterpret_cast (cvar_data); + hb_map_t axis_idx_tag_map; + axis_idx_tag_map.set (0, HB_TAG ('w', 'g', 'h', 't')); + + hb_blob_t *blob = hb_blob_create ((const char *) cvar_data, sizeof (cvar_data), + HB_MEMORY_MODE_READONLY, nullptr, nullptr); + bool result = cvar_table->decompile_tuple_variations (1, 65, blob, false, &axis_idx_tag_map, + tuple_variations, budget); + hb_blob_destroy (blob); + return result; +} + +static void +test_tuple_points_budget () +{ + /* Each tuple is expanded to 65 points; the decode charges them. */ + { + OT::TupleVariationData<>::tuple_variations_t tuple_variations; + int64_t budget = 2 * 65 - 1; + hb_always_assert (!decompile_cvar_with_budget (tuple_variations, &budget)); + } + + OT::TupleVariationData<>::tuple_variations_t tuple_variations; + int64_t budget = 2 * 65; + hb_always_assert (decompile_cvar_with_budget (tuple_variations, &budget)); + hb_always_assert (tuple_variations.tuple_vars.length == 2); + hb_always_assert (budget == 0); +} + int main (int argc, char **argv) { @@ -366,4 +399,5 @@ main (int argc, char **argv) test_tuple_values_decompile_trunc (); test_shared_tuple_scalar_cache (); test_decompile_cvar (); + test_tuple_points_budget (); }