diff --git a/src/hb-limits.hh b/src/hb-limits.hh index d39b9cb3e..71523accf 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -322,6 +322,16 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) #endif +/* One avar2 partial-instancing plan, in region tents visited to bound the + * final coordinates each axis can reach. A delta-set row can list a region + * any number of times and axes can share a row, so the table size does not + * bound that work. An axis whose exact bounds do not fit what is left gets + * the looser interval bounds; once exhausted, axes are left unconstrained. */ +#ifndef HB_BUDGET_SUBSET_AVAR2_RANGES +#define HB_BUDGET_SUBSET_AVAR2_RANGES ((int64_t) 1 << 26) +#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-subset-plan-var.cc b/src/hb-subset-plan-var.cc index 4b13b97ff..b05a97ab3 100644 --- a/src/hb-subset-plan-var.cc +++ b/src/hb-subset-plan-var.cc @@ -144,6 +144,7 @@ hb_hashmap_t grid_pos; hb_vector_t> grid_points; + int64_t budget = HB_BUDGET_SUBSET_AVAR2_RANGES; for (unsigned i = 0; i < axes.length; i++) { grid_pos.reset (); @@ -171,6 +172,9 @@ const OT::HBUINT8 *delta_bytes = vd.get_delta_bytes (); unsigned row_size = vd.get_row_size (); unsigned region_count = vd.get_region_index_count (); + /* Out of budget: the remaining axes are left unconstrained. */ + if (unlikely (!hb_budget_spend (budget, HB_BUDGET_1, region_count))) + return true; for (unsigned r = 0; r < region_count; r++) { int delta = vd.get_item_delta_fast (inner, r, delta_bytes, row_size); @@ -217,8 +221,13 @@ if (add_grid_axis (tag) < 0) return false; /* Per active region: (grid axis position, tent) for its valid tents. */ hb_vector_t>> region_tents; + unsigned num_tents = 0; for (const auto &ar : active) { + /* Collecting a tent allocates, and adds breakpoints to sort. */ + if (unlikely (!hb_budget_spend (budget, HB_BUDGET_16, + 1 + regions[ar.second].get_population ()))) + return true; region_tents.push (hb_vector_t> ()); if (region_tents.in_error ()) return false; auto &tents = region_tents[region_tents.length - 1]; @@ -240,6 +249,7 @@ tents.push (hb_pair ((unsigned) pos, tent)); if (points.in_error () || tents.in_error ()) return false; } + num_tents += tents.length; } unsigned grid_size = 1; @@ -256,6 +266,15 @@ grid_size *= points.length; } + /* Every vertex evaluates every tent of every active region. Fall back + * to the interval bounds, without charging, when that does not fit. */ + if (exact) + { + int64_t cost = (int64_t) grid_size * (active.length + num_tents); + if (cost <= budget) budget -= cost; + else exact = false; + } + if (exact) { unsigned target_pos = 0; /* the target axis was added first */