diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 27858b8ba..62a7116b1 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -256,6 +256,13 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) #define HB_BUDGET_SUBSET_GLYF ((int64_t) 1 << 28) #endif +/* The work one table may spend on being subset beyond what is proportional + * to its size. Limiting an axis of an avar2 font is charged the square of + * the axis's avar breakpoints. */ +#ifndef HB_BUDGET_SUBSET_TABLE +#define HB_BUDGET_SUBSET_TABLE ((int64_t) 1 << 30) +#endif + /* The flat pixel-work floor for one raster draw or paint session. Pixel and * outline work use separate live counters, so keep this independent of * the common outline budget above. */ diff --git a/src/hb-ot-var-avar-table.hh b/src/hb-ot-var-avar-table.hh index 42138ea65..7a79521d4 100644 --- a/src/hb-ot-var-avar-table.hh +++ b/src/hb-ot-var-avar-table.hh @@ -937,6 +937,9 @@ struct avar * Track processed (outer,inner) pairs to avoid adding duplicate biases * when multiple axes share the same varIdx. */ hb_set_t processed_varidxes; + /* All axes share the budget. A retry with a larger buffer, which the + * added regions make likely, starts afresh. */ + int64_t estimate_budget = HB_BUDGET_SUBSET_TABLE; for (unsigned i = 0; i < axisCount; i++) { hb_tag_t *axis_tag_ptr; @@ -1033,6 +1036,19 @@ struct avar if (-1.0 < z_old && z_old < 1.0) _avar2_add_knot (knots, z_old, -z_old); + /* From here on every breakpoint, and every sample of the error + * estimator, which takes a few per breakpoint on top of its + * uniform grid, is looked up in the old mapping, the new mapping + * and the knots by linear search. Charge that quadratic work + * before doing it; failing the table fails the subset, which + * beats silently dropping avar from an avar2-instanced font. */ + unsigned breakpoints = old_seg_maps[i]->len + new_mapping.length; + if (unlikely (!c->serializer->check_success ( + hb_budget_spend (estimate_budget, + HB_BUDGET_256 + breakpoints, + HB_BUDGET_8 * breakpoints)))) + return false; + /* Interior avar v1 breakpoints inside the retained range each put * a kink in offset(z). Sampling only {-1, 0, +1, z_old} would * linearly interpolate across those kinks. The instantiated diff --git a/test/api/test-subset-avar2.c b/test/api/test-subset-avar2.c index 23d4749e4..9c051485c 100644 --- a/test/api/test-subset-avar2.c +++ b/test/api/test-subset-avar2.c @@ -684,6 +684,81 @@ test_avar2_restrict_steep_no_varidx (void) hb_face_destroy (face); } +/* Replace the test font's avar with a version 2 table that has no VarIdxMap + * and no VarStore, and gives the first axis, wght, an identity mapping with + * a breakpoint every step F2Dot14 units. */ +typedef struct +{ + hb_face_t *source; + unsigned step; +} breakpoints_t; + +static hb_blob_t * +reference_table_with_breakpoints (hb_face_t *face HB_UNUSED, + hb_tag_t tag, + void *user_data) +{ + const breakpoints_t *breakpoints = (const breakpoints_t *) user_data; + if (tag != HB_TAG ('a','v','a','r')) + return hb_face_reference_table (breakpoints->source, tag); + + unsigned map_count = 2 * 16384 / breakpoints->step + 1; + unsigned length = 8 + (2 + map_count * 4) + 5 * 2 + 8; + unsigned char *data = (unsigned char *) calloc (length, 1); + g_assert_nonnull (data); + + data[1] = 2; /* major version */ + data[7] = 6; /* axisCount */ + data[8] = map_count >> 8; + data[9] = map_count & 0xFF; + for (unsigned i = 0; i < map_count; i++) + { + unsigned char *p = data + 10 + i * 4; + unsigned coord = (unsigned) ((int) (i * breakpoints->step) - 16384); + p[0] = p[2] = (coord >> 8) & 0xFF; + p[1] = p[3] = coord & 0xFF; + } + + return hb_blob_create ((const char *) data, length, + HB_MEMORY_MODE_WRITABLE, data, free); +} + +/* Offset compensation takes time quadratic in the breakpoints of the + * restricted axis. It must cope with hundreds of them, but give up on tens + * of thousands rather than spend a minute. */ +static void +test_avar2_restrict_many_breakpoints (void) +{ + hb_face_t *source = open_original (); + + breakpoints_t many = {source, 64}; + hb_face_t *face = hb_face_create_for_tables (reference_table_with_breakpoints, + &many, NULL); + hb_subset_input_t *input = create_input (); + g_assert_true (hb_subset_input_set_axis_range (input, face, WGHT, 200.f, 700.f, 400.f)); + hb_face_t *inst = instance (face, input); + + for (unsigned i = 0; i < 9; i++) + { + hb_variation_t vars[1] = {{WGHT, lerp (200.f, 700.f, i, 9)}}; + check_same_coords (face, inst, vars, 1, 2); + } + + hb_face_destroy (inst); + hb_face_destroy (face); + + breakpoints_t too_many = {source, 1}; + face = hb_face_create_for_tables (reference_table_with_breakpoints, + &too_many, NULL); + input = create_input (); + g_assert_true (hb_subset_input_set_axis_range (input, face, WGHT, 200.f, 700.f, 400.f)); + g_assert_null (hb_subset_or_fail (face, input)); + + hb_subset_input_destroy (input); + hb_face_destroy (face); + hb_face_destroy (source); +} + int main (int argc, char **argv) { @@ -705,6 +780,7 @@ main (int argc, char **argv) hb_test_add (test_avar2_culling_aliased_gvar_data); hb_test_add (test_avar2_restrict_steep); hb_test_add (test_avar2_restrict_steep_no_varidx); + hb_test_add (test_avar2_restrict_many_breakpoints); return hb_test_run (); }