diff --git a/src/hb-limits.hh b/src/hb-limits.hh index b18e4e64c..27858b8ba 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -107,6 +107,14 @@ #define HB_VAR_COMPOSITE_MAX_AXES 4096 #endif +/* Region axes copied into tuples when one ItemVariationStore is converted + * for instancing, summed over its VarData subtables. Every region a + * VarData references gets its own copy of that region's axes, and the + * font pays a few bytes per reference however many axes the region has. */ +#ifndef HB_VARSTORE_MAX_TUPLE_AXES +#define HB_VARSTORE_MAX_TUPLE_AXES (1u << 20) +#endif + #ifndef HB_GLYF_MAX_POINTS #define HB_GLYF_MAX_POINTS 200000 #endif diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 92d582841..ed3cfa941 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1088,6 +1088,7 @@ struct TupleVariationData const hb_vector_t>& regions, const hb_map_t& axes_old_index_tag_map, unsigned& item_count, + unsigned& num_tuple_axes, const hb_inc_bimap_t* inner_map = nullptr) { /* NULL offset, to keep original varidx valid, just return */ @@ -1102,8 +1103,31 @@ struct TupleVariationData unsigned row_size = var_data.get_row_size (); const HBUINT8 *delta_bytes = var_data.get_delta_bytes (); + /* region index -> index of its tuple in tuple_vars */ + hb_map_t region_tuple_map; for (unsigned r = 0; r < num_regions; r++) { + unsigned region_index = var_data.get_region_index (r); + if (region_index >= regions.length) return false; + + /* A region the VarData references again adds its deltas to the + * tuple it already has, as merge_tuple_variations () would do, + * instead of getting another copy of the region's axes. */ + unsigned *tuple_index; + if (region_tuple_map.has (region_index, &tuple_index)) + { + tuple_delta_t& tuple = tuple_vars.arrayZ[*tuple_index]; + for (unsigned i = 0; i < item_count; i++) + tuple.deltas_x.arrayZ[i] += var_data.get_item_delta_fast (inner_map ? inner_map->backward (i) : i, + r, delta_bytes, row_size); + continue; + } + + /* Each tuple copies its region's axes; charge them before copying. */ + const hb_hashmap_t& region = regions.arrayZ[region_index]; + num_tuple_axes += region.get_population (); + if (unlikely (num_tuple_axes > HB_VARSTORE_MAX_TUPLE_AXES)) return false; + /* In VarData, deltas are organized in rows, convert them into * column(region) based tuples, resize deltas_x first */ tuple_delta_t tuple; @@ -1118,10 +1142,9 @@ struct TupleVariationData r, delta_bytes, row_size); } - unsigned region_index = var_data.get_region_index (r); - if (region_index >= regions.length) return false; - tuple.axis_tuples = regions.arrayZ[region_index]; + tuple.axis_tuples = region; + if (unlikely (!region_tuple_map.set (region_index, tuple_vars.length))) return false; tuple_vars.push (std::move (tuple)); } return !tuple_vars.in_error (); @@ -2005,6 +2028,7 @@ struct item_variations_t if (!vars.alloc (num_var_data) || !var_data_num_rows.alloc (num_var_data)) return false; + unsigned num_tuple_axes = 0; for (unsigned i = 0; i < num_var_data; i++) { if (inner_maps && !inner_maps.arrayZ[i].get_population ()) @@ -2015,6 +2039,7 @@ struct item_variations_t orig_region_list, axes_old_index_tag_map, item_count, + num_tuple_axes, inner_maps ? &(inner_maps.arrayZ[i]) : nullptr)) return false; diff --git a/src/test-item-varstore.cc b/src/test-item-varstore.cc index 7e0ca2eed..80497ef4a 100644 --- a/src/test-item-varstore.cc +++ b/src/test-item-varstore.cc @@ -457,6 +457,53 @@ test_condition_budget () hb_always_assert (budget < 0); } +static void +test_repeated_region_index () +{ + /* One axis, two regions, and a VarData whose region indices are {0, 1, 0}: + * the repeated region's two delta columns must be summed into one tuple. */ + const char store_data[] = + "\x00\x01\x00\x00\x00\x0c\x00\x01\x00\x00\x00\x1c" // ItemVariationStore + "\x00\x01\x00\x02" // VarRegionList: 1 axis, 2 regions + "\x00\x00\x40\x00\x40\x00" // region 0: (0, 1, 1) + "\xc0\x00\xc0\x00\x00\x00" // region 1: (-1, -1, 0) + "\x00\x02\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00" // VarData: 2 items, regions {0, 1, 0} + "\x01\x02\x03" // item 0 + "\x0a\x14\x1e"; // item 1 + const auto &store = *reinterpret_cast (store_data); + hb_map_t axis_map; + axis_map.set (0, HB_TAG ('T','E','S','T')); + + hb_vector_t> regions; + hb_always_assert (store.get_region_list ().get_var_regions (axis_map, regions)); + OT::tuple_variations_t tuples; + unsigned item_count = 0, num_tuple_axes = 0; + hb_always_assert (tuples.create_from_item_var_data (store.get_sub_table (0), regions, axis_map, + item_count, num_tuple_axes)); + hb_always_assert (item_count == 2 && num_tuple_axes == 2); + hb_always_assert (tuples.tuple_vars.length == 2); + hb_always_assert (tuples.tuple_vars[0].deltas_x[0] == 4.f && tuples.tuple_vars[0].deltas_x[1] == 40.f); + hb_always_assert (tuples.tuple_vars[1].deltas_x[0] == 2.f && tuples.tuple_vars[1].deltas_x[1] == 20.f); + + /* The region axes copied for one store are capped. */ + OT::tuple_variations_t capped; + num_tuple_axes = HB_VARSTORE_MAX_TUPLE_AXES - 1; + hb_always_assert (!capped.create_from_item_var_data (store.get_sub_table (0), regions, axis_map, + item_count, num_tuple_axes)); + + OT::item_variations_t item_vars; + hb_always_assert (item_vars.create_from_item_varstore (store, axis_map)); + hb_hashmap_t locations; + hb_hashmap_t distances; + hb_always_assert (item_vars.instantiate_tuple_vars (locations, distances)); + hb_always_assert (item_vars.as_item_varstore (false)); + hb_always_assert (item_vars.get_region_list ().length == 2); + const auto &items = item_vars.get_vardata_encodings ()[0].items; + hb_always_assert (items.length == 2); + hb_always_assert ((*items[0])[0] == 4 && (*items[0])[1] == 2); + hb_always_assert ((*items[1])[0] == 40 && (*items[1])[1] == 20); +} + static void test_failed_encoding () { @@ -530,4 +577,5 @@ main (int argc, char **argv) test_condition_delta_precision (); test_multi_instancer_budget (); test_condition_budget (); + test_repeated_region_index (); }