diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 00ade4327..bce927573 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -285,6 +285,19 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) #endif +/* One instancing session: all the tuple variations of one table. + * Limiting an axis splits every tuple that uses it into up to six, so + * tuples multiply with each limited axis. The copies are charged to a + * budget that starts at MIN and earns FACTOR times each source tuple, + * in tuple_delta_t::get_cost () units of roughly eight bytes. */ +#ifndef HB_BUDGET_INSTANCER_FACTOR +#define HB_BUDGET_INSTANCER_FACTOR 64 +#endif +#ifndef HB_BUDGET_INSTANCER_MIN +#define HB_BUDGET_INSTANCER_MIN ((int64_t) 1 << 22) +#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 150c0635e..7ef927fbe 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -271,6 +271,9 @@ struct optimize_scratch_t hb_vector_t point_data; hb_vector_t deltas_data; hb_vector_t rounded_deltas; + + /* What splitting tuples may still cost; see HB_BUDGET_INSTANCER_*. */ + int64_t budget = HB_BUDGET_INSTANCER_MIN; }; struct tuple_delta_t @@ -333,6 +336,10 @@ struct tuple_delta_t compiled_interm_coords.duplicate_vector_from_pool (pool, o.compiled_interm_coords); } + /* Budget cost of a copy of this tuple. */ + unsigned get_cost () const + { return HB_BUDGET_32 + HB_BUDGET_8 * axis_tuples.get_population () + HB_BUDGET_1 * indices.length; } + void remove_axis (hb_tag_t axis_tag) { axis_tuples.del (axis_tag); } @@ -1153,8 +1160,13 @@ struct TupleVariationData private: bool change_tuple_variations_axis_limits (const hb_hashmap_t& normalized_axes_location, const hb_hashmap_t& axes_triple_distances, + int64_t &budget, hb_alloc_pool_t *pool = nullptr) { + /* Splitting below may grow the tuples this much. */ + for (const tuple_delta_t& var : tuple_vars) + budget += (int64_t) var.get_cost () * HB_BUDGET_INSTANCER_FACTOR; + /* sort axis_tag/axis_limits, make result deterministic */ hb_vector_t axis_tags; if (!axis_tags.alloc (normalized_axes_location.get_population ())) @@ -1184,6 +1196,9 @@ struct TupleVariationData if (unlikely (out.in_error ())) return false; if (!out) continue; + if (unlikely (!hb_budget_spend (budget, out.arrayZ[0].get_cost (), out.length - 1))) + return false; + unsigned new_len = new_vars.length + out.length; if (unlikely (!new_vars.alloc (new_len, false))) @@ -1343,7 +1358,7 @@ struct TupleVariationData bool optimize = false) { if (!tuple_vars) return true; - if (!change_tuple_variations_axis_limits (normalized_axes_location, axes_triple_distances, pool)) + if (!change_tuple_variations_axis_limits (normalized_axes_location, axes_triple_distances, scratch.budget, pool)) return false; /* compute inferred deltas only for gvar */ if (contour_points) diff --git a/src/test-item-varstore.cc b/src/test-item-varstore.cc index 0e8cb92c6..46e12beb4 100644 --- a/src/test-item-varstore.cc +++ b/src/test-item-varstore.cc @@ -598,6 +598,24 @@ test_delta_matrix_limits () hb_always_assert (!item_vars.as_item_varstore ()); } +static void +test_tuple_split_budget () +{ + /* A tent inside the new range with the new default on its slope splits + * into six, so the region below would become 6^16 tuples. */ + hb_hashmap_t region, locations; + hb_hashmap_t distances; + for (unsigned i = 0; i < 16; i++) + { + region.set (HB_TAG ('A','A','A','A') + i, Triple (0.3, 0.5, 0.7)); + locations.set (HB_TAG ('A','A','A','A') + i, Triple (0.2, 0.45, 0.8)); + } + OT::item_variations_t item_vars; + unsigned outer = item_vars.add_vardata (1); + item_vars.add_tuple (outer, std::move (region), 0, 100, 1); + hb_always_assert (!item_vars.instantiate_tuple_vars (locations, distances)); +} + int main (int argc, char **argv) { @@ -616,4 +634,5 @@ main (int argc, char **argv) test_condition_budget (); test_repeated_region_index (); test_delta_matrix_limits (); + test_tuple_split_budget (); }