diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index 0429a08fd..4445cf4a9 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -2480,9 +2480,17 @@ struct COLR } else { + /* avar2 partial instancing: cull unreachable regions. */ + hb_set_t dead_regions; + if (c->plan->has_avar2) + var_store.collect_dead_regions (c->plan->axes_old_index_tag_map, + c->plan->avar2_reachable_ranges, + dead_regions); + if (unlikely (!out->varStore.serialize_serialize (c->serializer, &var_store, - c->plan->colrv1_varstore_inner_maps.as_array ()))) + c->plan->colrv1_varstore_inner_maps.as_array (), + dead_regions.get_population () ? &dead_regions : nullptr))) return_trace (false); } diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 09326dac3..b41ebf45a 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -2807,6 +2807,35 @@ struct VarRegionList DEFINE_SIZE_ARRAY (4, axesZ); }; +/* Whether a variation region is unreachable given per-axis reachable + * final-coordinate ranges, in F2Dot14 units (avar2 partial-instancing + * culling; see hb_subset_plan_t::avar2_reachable_ranges). A region is dead + * if some axis's tent evaluates to zero over the axis's entire reachable + * range. Mirrors the runtime evaluation: invalid tents evaluate as + * constant 1 and never kill a region. */ +static inline bool +_hb_avar2_region_is_dead (const hb_hashmap_t &axis_tuples, + const hb_hashmap_t &reachable_ranges) +{ + for (auto _ : axis_tuples) + { + Triple *range; + if (!reachable_ranges.has (_.first, &range)) continue; + const Triple &tent = _.second; + int start = (int) roundf ((float) tent.minimum * 16384.f); + int peak = (int) roundf ((float) tent.middle * 16384.f); + int end = (int) roundf ((float) tent.maximum * 16384.f); + if (!peak) continue; + if (start > peak || peak > end) continue; + if (start < 0 && end > 0) continue; + int lo = (int) roundf ((float) range->minimum * 16384.f); + int hi = (int) roundf ((float) range->maximum * 16384.f); + if (hi <= start || lo >= end) + return true; + } + return false; +} + struct SparseVariationRegion : Array16Of { float evaluate (const int *coords, unsigned int coord_len) const @@ -3047,7 +3076,8 @@ struct VarData bool serialize (hb_serialize_context_t *c, const VarData *src, const hb_inc_bimap_t &inner_map, - const hb_inc_bimap_t ®ion_map) + const hb_inc_bimap_t ®ion_map, + const hb_set_t *culled_regions = nullptr) { TRACE_SERIALIZE (this); if (unlikely (!c->extend_min (this))) return_trace (false); @@ -3092,6 +3122,8 @@ struct VarData bool short_circuit = src_long_words == has_long && src_word_count <= r; delta_sz[r] = kZero; + if (culled_regions && culled_regions->has (src->regionIndices[r])) + continue; /* unreachable region: drop its column */ for (unsigned old_gid : inner_map.keys()) { int32_t delta = src->get_item_delta_fast (old_gid, r, src_delta_bytes, src_row_size); @@ -3400,7 +3432,8 @@ struct ItemVariationStore bool serialize (hb_serialize_context_t *c, const ItemVariationStore *src, - const hb_array_t &inner_maps) + const hb_array_t &inner_maps, + const hb_set_t *culled_regions = nullptr) { TRACE_SERIALIZE (this); #ifdef HB_NO_VAR @@ -3427,6 +3460,9 @@ struct ItemVariationStore region_indices.del_range ((src_regions).regionCount, hb_set_t::INVALID); + if (culled_regions) + region_indices.subtract (*culled_regions); + /* TODO use constructor when our data-structures support that. */ hb_inc_bimap_t region_map; + hb_iter (region_indices) @@ -3448,7 +3484,8 @@ struct ItemVariationStore { if (!inner_maps[i].get_population ()) continue; if (unlikely (!dataSets[set_index++] - .serialize_serialize (c, &(src+src->dataSets[i]), inner_maps[i], region_map))) + .serialize_serialize (c, &(src+src->dataSets[i]), inner_maps[i], region_map, + culled_regions))) return_trace (false); } @@ -3480,6 +3517,30 @@ struct ItemVariationStore return_trace (out); } + /* Collect regions that a partial avar2 instance can never reach, per the + * plan's reachable final-coordinate ranges. */ + bool collect_dead_regions (const hb_map_t &axes_old_index_tag_map, + const hb_hashmap_t &reachable_ranges, + hb_set_t &dead_regions /* OUT */) const + { +#ifndef HB_NO_VAR + if (!reachable_ranges.get_population ()) return true; + const VarRegionList ®ion_list = this+regions; + unsigned count = region_list.regionCount; + for (unsigned i = 0; i < count; i++) + { + hb_hashmap_t axis_tuples; + if (!region_list.get_var_region (i, axes_old_index_tag_map, axis_tuples)) + return false; + if (_hb_avar2_region_is_dead (axis_tuples, reachable_ranges)) + dead_regions.add (i); + } + return !dead_regions.in_error (); +#else + return true; +#endif + } + bool subset (hb_subset_context_t *c, const hb_array_t &inner_maps) const { TRACE_SUBSET (this); @@ -3490,7 +3551,15 @@ struct ItemVariationStore ItemVariationStore *varstore_prime = c->serializer->start_embed (); if (unlikely (!varstore_prime)) return_trace (false); - varstore_prime->serialize (c->serializer, this, inner_maps); + /* avar2 partial instancing: cull unreachable regions. */ + hb_set_t dead_regions; + if (c->plan->has_avar2) + collect_dead_regions (c->plan->axes_old_index_tag_map, + c->plan->avar2_reachable_ranges, + dead_regions); + + varstore_prime->serialize (c->serializer, this, inner_maps, + dead_regions.get_population () ? &dead_regions : nullptr); return_trace ( !c->serializer->in_error() diff --git a/src/hb-ot-var-avar-table.hh b/src/hb-ot-var-avar-table.hh index 636610361..ef95a731d 100644 --- a/src/hb-ot-var-avar-table.hh +++ b/src/hb-ot-var-avar-table.hh @@ -552,6 +552,24 @@ struct avar bool has_v2_data () const { return version.major > 1; } + /* Resolve the avar2 VarStore and VarIdxMap, for the subset planner. */ + bool get_v2_store_and_map (const ItemVariationStore **store, + const DeltaSetIndexMap **varidx_map) const + { +#ifndef HB_NO_AVAR2 + if (version.major < 2) return false; + const SegmentMaps *map = &firstAxisSegmentMaps; + for (unsigned i = 0; i < axisCount; i++) + map = &StructAfter (*map); + const auto &v2 = * (const avarV2Tail *) map; + *store = &(this+v2.varStore); + *varidx_map = &(this+v2.varIdxMap); + return true; +#else + return false; +#endif + } + /* Apply avar v1 segment maps only, deliberately omitting avar v2 processing. * Used during avar2 partial instancing to compute intermediate-space coords. */ bool map_coords_v1_only (float *coords, unsigned int coords_length) const diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 75b7565a0..e6504671f 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1526,6 +1526,105 @@ struct TupleVariationData bool has_shared_point_numbers () const { return tupleVarCount.has_shared_point_numbers (); } + /* avar2 partial-instancing culling: rewrite this tuple variation data, + * dropping TupleVariations whose region a partial instance can never + * reach (see hb_subset_plan_t::avar2_reachable_ranges). On success with + * *changed set, out contains the rewritten data — possibly empty, when + * every tuple died. Keeps the original data (*changed stays false) when + * nothing is culled or the data is malformed. Returns false only on + * allocation failure. */ + bool cull_tuple_variations (hb_bytes_t var_data_bytes, + unsigned axis_count, + hb_array_t shared_tuples, + const hb_map_t *axes_old_index_tag_map, + const hb_hashmap_t &reachable_ranges, + hb_vector_t &out /* OUT */, + bool *changed /* OUT */) const + { + *changed = false; + + hb_vector_t shared_indices; + tuple_iterator_t iterator; + if (!get_tuple_iterator (var_data_bytes, axis_count, this, + shared_indices, &iterator)) + return true; /* no tuples or malformed: keep original */ + + const char *bytes_start = var_data_bytes.arrayZ; + const char *bytes_end = bytes_start + var_data_bytes.length; + const char *serialized_base = (const char *) &(this+data); + const char *shared_points_end = (const char *) iterator.get_serialized_data (); + if (unlikely (serialized_base < bytes_start || serialized_base > bytes_end || + shared_points_end < serialized_base || shared_points_end > bytes_end)) + return true; + + struct kept_tuple_t + { + const char *header; + unsigned header_size; + const char *data; + unsigned data_size; + }; + hb_vector_t kept; + unsigned total = 0; + unsigned kept_headers_size = 0, kept_data_size = 0; + do + { + const TupleVariationHeader *header = iterator.current_tuple; + unsigned header_size = header->get_size (axis_count * 2); + unsigned data_size = header->get_data_size (); + const char *tuple_data = (const char *) iterator.get_serialized_data (); + if (unlikely (tuple_data < serialized_base || + data_size > (unsigned) (bytes_end - tuple_data))) + return true; /* malformed: keep original */ + + hb_hashmap_t axis_tuples; + if (!header->unpack_axis_tuples (axis_count, shared_tuples, + axes_old_index_tag_map, axis_tuples)) + return true; + + total++; + if (!_hb_avar2_region_is_dead (axis_tuples, reachable_ranges)) + { + kept.push (kept_tuple_t {(const char *) header, header_size, + tuple_data, data_size}); + kept_headers_size += header_size; + kept_data_size += data_size; + } + } while (iterator.move_to_next ()); + + if (unlikely (kept.in_error ())) return false; + if (kept.length == total) return true; /* nothing to cull */ + + *changed = true; + if (!kept.length) return true; /* everything culled: no variation data */ + + unsigned shared_points_size = shared_points_end - serialized_base; + unsigned data_offset = min_size + kept_headers_size; + unsigned new_size = data_offset + shared_points_size + kept_data_size; + if (unlikely (!out.resize (new_size))) + return false; + + TupleVariationData *out_data = (TupleVariationData *) out.arrayZ; + out_data->tupleVarCount = (uint16_t) (kept.length | + (has_shared_point_numbers () ? 0x8000u : 0u)); + out_data->data = data_offset; + + char *p = out.arrayZ + min_size; + for (const auto &t : kept) + { + hb_memcpy (p, t.header, t.header_size); + p += t.header_size; + } + hb_memcpy (p, serialized_base, shared_points_size); + p += shared_points_size; + for (const auto &t : kept) + { + hb_memcpy (p, t.data, t.data_size); + p += t.data_size; + } + return true; + } + static bool decompile_points (const HBUINT8 *&p /* IN/OUT */, hb_vector_t &points /* OUT */, const HBUINT8 *end) diff --git a/src/hb-ot-var-fvar-table.hh b/src/hb-ot-var-fvar-table.hh index f22d573bc..6cd80082c 100644 --- a/src/hb-ot-var-fvar-table.hh +++ b/src/hb-ot-var-fvar-table.hh @@ -179,6 +179,8 @@ struct AxisRecord hb_tag_t get_axis_tag () const { return axisTag; } + bool is_hidden () const { return flags & AXIS_FLAG_HIDDEN; } + float normalize_axis_value (float v) const { float min_value, default_value, max_value; diff --git a/src/hb-ot-var-gvar-table.hh b/src/hb-ot-var-gvar-table.hh index aaed36641..de75ecf04 100644 --- a/src/hb-ot-var-gvar-table.hh +++ b/src/hb-ot-var-gvar-table.hh @@ -444,6 +444,36 @@ struct gvar_GVAR unsigned int num_glyphs = c->plan->num_output_glyphs (); out->glyphCountX = hb_min (0xFFFFu, num_glyphs); + /* avar2 partial instancing: cull tuple variations whose region falls + * outside the reachable old-space final-coord ranges. Rewritten glyph + * data is cached per new gid; untouched glyphs are copied verbatim. */ + hb_vector_t> culled_vars; + hb_set_t culled_gids; + if (c->plan->has_avar2 && c->plan->avar2_reachable_ranges.get_population ()) + { + hb_array_t shared_tuples_array = + (this+sharedTuples).as_array ((unsigned) sharedTupleCount * (unsigned) axisCount); + if (unlikely (!culled_vars.resize (num_glyphs))) return_trace (false); + auto cull_it = hb_iter (c->plan->new_to_old_gid_list); + if (cull_it->first == 0 && !(c->plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE)) + cull_it++; + for (auto &_ : cull_it) + { + hb_bytes_t var_data_bytes = get_glyph_var_data_bytes (c->source_blob, glyph_count, _.second); + if (var_data_bytes.length < GlyphVariationData::min_size) continue; + bool changed = false; + if (unlikely (!var_data_bytes.as () + ->cull_tuple_variations (var_data_bytes, axisCount, + shared_tuples_array, + &c->plan->axes_old_index_tag_map, + c->plan->avar2_reachable_ranges, + culled_vars[_.first], &changed))) + return_trace (false); + if (changed) culled_gids.add (_.first); + } + if (unlikely (culled_gids.in_error ())) return_trace (false); + } + auto it = hb_iter (c->plan->new_to_old_gid_list); if (it->first == 0 && !(c->plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE)) it++; @@ -452,7 +482,9 @@ struct gvar_GVAR for (auto &_ : it) { hb_codepoint_t old_gid = _.second; - unsigned glyph_data_size = get_glyph_var_data_bytes (c->source_blob, glyph_count, old_gid).length; + unsigned glyph_data_size = culled_gids.has (_.first) + ? culled_vars[_.first].length + : get_glyph_var_data_bytes (c->source_blob, glyph_count, old_gid).length; if (glyph_data_size % 2) { glyph_data_size++; @@ -524,9 +556,12 @@ struct gvar_GVAR for (; last < gid; last++) ((HBUINT16 *) subset_offsets)[last] = glyph_offset / 2; - hb_bytes_t var_data_bytes = get_glyph_var_data_bytes (c->source_blob, - glyph_count, - old_gid); + hb_bytes_t var_data_bytes = culled_gids.has (gid) + ? hb_bytes_t (culled_vars[gid].arrayZ, + culled_vars[gid].length) + : get_glyph_var_data_bytes (c->source_blob, + glyph_count, + old_gid); hb_memcpy (subset_data, var_data_bytes.arrayZ, var_data_bytes.length); unsigned glyph_data_size = var_data_bytes.length; diff --git a/src/hb-ot-var-hvar-table.hh b/src/hb-ot-var-hvar-table.hh index c45d4ef31..c0501bb5e 100644 --- a/src/hb-ot-var-hvar-table.hh +++ b/src/hb-ot-var-hvar-table.hh @@ -394,10 +394,18 @@ struct HVARVVAR } else { + /* avar2 partial instancing: cull unreachable regions. */ + hb_set_t dead_regions; + if (c->plan->has_avar2) + hvar_plan.var_store->collect_dead_regions (c->plan->axes_old_index_tag_map, + c->plan->avar2_reachable_ranges, + dead_regions); + if (unlikely (!out->varStore .serialize_serialize (c->serializer, hvar_plan.var_store, - hvar_plan.inner_maps.as_array ()))) + hvar_plan.inner_maps.as_array (), + dead_regions.get_population () ? &dead_regions : nullptr))) return_trace (false); } diff --git a/src/hb-subset-plan-member-list.hh b/src/hb-subset-plan-member-list.hh index be4541378..581a57341 100644 --- a/src/hb-subset-plan-member-list.hh +++ b/src/hb-subset-plan-member-list.hh @@ -137,6 +137,12 @@ HB_SUBSET_PLAN_MEMBER (hb_map_t, axes_index_map) //old intermediate coords (post-fvar, post-avar-v1, pre-avar-v2) at new min/default/max HB_SUBSET_PLAN_MEMBER (hb_hashmap_t E(), old_intermediates) +//avar2: reachable old-space final-coord range per axis, as Triple +//(minimum, unused middle, maximum), F2Dot14-quantized. Only axes with a +//constraining range (narrower than [-1, +1]) have entries. Used to cull +//variation tuples/regions that a partial instance can never reach. +HB_SUBSET_PLAN_MEMBER (hb_hashmap_t E(), avar2_reachable_ranges) + //axis_index->axis_tag mapping in fvar axis array HB_SUBSET_PLAN_MEMBER (hb_map_t, axes_old_index_tag_map) //vector of retained axis tags in the order of axes given in the 'fvar' table diff --git a/src/hb-subset-plan-var.cc b/src/hb-subset-plan-var.cc index 6885722ce..1c44834e8 100644 --- a/src/hb-subset-plan-var.cc +++ b/src/hb-subset-plan-var.cc @@ -55,6 +55,134 @@ } } + /* Evaluate a variation-region tent at v, mirroring VarRegionAxis::evaluate + * (invalid regions evaluate as constant 1). */ + static double + _tent_eval (const Triple &tent, double v) + { + double start = tent.minimum, peak = tent.middle, end = tent.maximum; + if (unlikely (start > peak || peak > end)) return 1.0; + if (unlikely (start < 0.0 && end > 0.0 && peak != 0.0)) return 1.0; + if (peak == 0.0 || v == peak) return 1.0; + if (v <= start || end <= v) return 0.0; + if (v < peak) return (v - start) / (peak - start); + return (end - v) / (end - peak); + } + + /* Range of a tent's scalar over an input interval [lo, hi]. The tent is + * unimodal, so the extremes are at the interval endpoints, plus the peak + * when it falls inside. */ + static void + _tent_value_range (const Triple &tent, double lo, double hi, + double *tmin, double *tmax) + { + double a = _tent_eval (tent, lo); + double b = _tent_eval (tent, hi); + *tmin = hb_min (a, b); + *tmax = hb_max (a, b); + if (lo <= tent.middle && tent.middle <= hi) + *tmax = hb_max (*tmax, 1.0); + } + + /* Compute conservative reachable old-space final-coord ranges per axis for + * avar2 partial instancing. With offset compensation, the instanced font's + * final coordinates equal the original font's over the retained user box, + * so the reachable range of axis i's final coordinate is the range of + * final_i = intermediate_i + delta_i(intermediates) + * over the box of retained old-intermediate ranges: restricted axes span + * their retained [a, b], free public axes [-1, +1], pinned axes sit at + * their d_i, and hidden (private) axes at 0. Interval arithmetic over the + * avar2 VarStore regions bounds delta_i. Mirrors fontTools' + * _computeReachableRangesForAvar2, but computed from the original store at + * plan time (the same quantity fontTools bounds via getExtremes on the + * instanced store). Only constraining ranges (narrower than [-1, +1]) are + * recorded. */ + static void + _compute_avar2_reachable_ranges (hb_subset_plan_t *plan, + hb_array_t axes, + const OT::avar *avar_table) + { + const OT::ItemVariationStore *var_store; + const OT::DeltaSetIndexMap *varidx_map; + if (!avar_table->get_v2_store_and_map (&var_store, &varidx_map)) + return; + + /* Old-intermediate input box per axis. */ + hb_hashmap_t> box; + for (const auto &axis : axes) + { + hb_tag_t tag = axis.get_axis_tag (); + double lo = -1.0, hi = +1.0; + Triple *old_int; + if (plan->user_axes_location.has (tag)) + { + if (!plan->old_intermediates.has (tag, &old_int)) return; + lo = hb_min (old_int->minimum, old_int->maximum); + hi = hb_max (old_int->minimum, old_int->maximum); + } + else if (axis.is_hidden ()) + lo = hi = 0.0; /* private axis: intermediate is always 0 */ + if (!box.set (tag, hb_pair (lo, hi))) return; + } + + hb_vector_t> regions; + if (!var_store->get_region_list ().get_var_regions (plan->axes_old_index_tag_map, regions)) + return; + + for (unsigned i = 0; i < axes.length; i++) + { + hb_tag_t tag = axes[i].get_axis_tag (); + hb_pair_t *identity; + if (!box.has (tag, &identity)) return; + + double dmin = 0.0, dmax = 0.0; + uint32_t varidx = varidx_map->map (i); + if (varidx != HB_OT_LAYOUT_NO_VARIATIONS_INDEX) + { + unsigned outer = varidx >> 16; + unsigned inner = varidx & 0xFFFF; + if (outer >= var_store->get_sub_table_count ()) continue; + const OT::VarData &vd = var_store->get_sub_table (outer); + if (inner >= vd.get_item_count ()) continue; + const OT::HBUINT8 *delta_bytes = vd.get_delta_bytes (); + unsigned row_size = vd.get_row_size (); + unsigned region_count = vd.get_region_index_count (); + for (unsigned r = 0; r < region_count; r++) + { + int delta = vd.get_item_delta_fast (inner, r, delta_bytes, row_size); + if (!delta) continue; + unsigned region_idx = vd.get_region_index (r); + if (region_idx >= regions.length) continue; + double smin = 1.0, smax = 1.0; + for (const auto &_ : regions[region_idx]) + { + hb_pair_t *input; + double blo = -1.0, bhi = +1.0; + if (box.has (_.first, &input)) { blo = input->first; bhi = input->second; } + double tmin, tmax; + _tent_value_range (_.second, blo, bhi, &tmin, &tmax); + smin *= tmin; + smax *= tmax; + if (smax == 0.0) break; + } + if (delta > 0) { dmin += smin * delta; dmax += smax * delta; } + else { dmin += smax * delta; dmax += smin * delta; } + } + } + + /* Pad by one F2Dot14 unit against rounding differences with the + * runtime's fixed-point evaluation. */ + double lo = hb_clamp (identity->first + dmin / 16384.0 - 1.0 / 16384.0, -1.0, +1.0); + double hi = hb_clamp (identity->second + dmax / 16384.0 + 1.0 / 16384.0, -1.0, +1.0); + lo = floor (lo * 16384.0) / 16384.0; + hi = ceil (hi * 16384.0) / 16384.0; + if (lo <= -1.0 && hi >= +1.0) + continue; /* not constraining */ + if (!plan->avar2_reachable_ranges.set (tag, Triple (lo, hb_clamp (0.0, lo, hi), hi))) + return; + } + } + #ifndef HB_NO_OT_FONT_CFF static inline hb_font_t* _get_hb_font_with_variations (const hb_subset_plan_t *plan) @@ -303,6 +431,8 @@ normalize_axes_location (hb_face_t *face, hb_subset_plan_t *plan) plan->axis_tags.push (axis_tag); } plan->all_axes_pinned = false; + + _compute_avar2_reachable_ranges (plan, axes, avar_table); } else { diff --git a/src/hb-subset-table-var.cc b/src/hb-subset-table-var.cc index 6cc4758eb..570a33bcf 100644 --- a/src/hb-subset-table-var.cc +++ b/src/hb-subset-table-var.cc @@ -28,12 +28,16 @@ bool _hb_subset_table_var (hb_subset_plan_t *plan, hb_vector_t &buf, hb_t *success = _hb_subset_table (plan, buf); return true; case HB_TAG('c','v','a','r'): + /* TODO: For avar2 partial instancing, cull unreachable tuple + * variations (avar2_reachable_ranges) instead of passing through. */ if (plan->user_axes_location.is_empty () || plan->has_avar2) *success = _hb_subset_table_passthrough (plan, tag); else *success = _hb_subset_table (plan, buf); return true; case HB_TAG('M','V','A','R'): + /* TODO: For avar2 partial instancing, cull unreachable VarStore + * regions (avar2_reachable_ranges) instead of passing through. */ if (plan->user_axes_location.is_empty () || plan->has_avar2) *success = _hb_subset_table_passthrough (plan, tag); else