From c752b532bde2bc2acf3c7b50b9c317056bc222dc Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 3 Jul 2026 18:57:00 -0600 Subject: [PATCH] [subset] Instance self-contained pinned axes for avar2 A pinned axis whose final coordinate is CONSTANT over the retained box -- its varIdx is NO_VARIATION_INDEX, or its avar2 delta row is constant (zero-width delta interval, detected with the same plan-time interval arithmetic as the reachable ranges) -- is "self-contained": there is no reason to keep it in the instance. Remove it from fvar and avar entirely and bake its constant contribution into the variation tables, mirroring fontTools' selfContainedAxes handling on its avar2-instancing branch. The key move fits fontTools' second instancing pass into HarfBuzz's single-pass architecture: the plan presents the self-contained pins to every other table as an ORDINARY partial instancing in old final-coordinate space. plan->axes_location and plan->normalized_coords now hold only those pins (empty when there are none), while the intermediate-space ranges that avar itself needs move to a new plan->avar2_axes_location. With that, the blanket "normalized_coords && !has_avar2" instancing skips added earlier reduce to plain "normalized_coords" checks: under avar2 with no self-contained axes the tables take their non-instancing paths exactly as before, and with self-contained axes the standard instancing machinery pins exactly those axes -- correct, because their final coordinates are constants in the space the variation tables live in. This effectively reverts the special-casing from "[subset] Skip glyf instancing and hmtx MVAR for avar2", "[subset] Skip remaining instancing paths for avar2" and "[subset] Skip GPOS value format optimization for avar2" in favor of the emptiness of the plan's instancing state. cvar and MVAR also instance (rather than pass through) when self-contained axes exist. avar-side changes: _subset_avar2 skips dropped axes for privatization and offset compensation, writes the DeltaSetIndexMap over the retained axes only, and rebases the IVS with avar2_axes_location; SegmentMaps::subset picks the right location map; fvar InstanceRecords drop the removed axes' coordinates. The avar2 variation culling now also hooks the decompiled instancing paths (tuple_variations_t::cull_unreachable, called from item_variations_t::instantiate and gvar's glyph_variations_t), since self-contained axes route those tables through instantiation, where the byte-level culling paths don't apply. Under non-avar2, all of this is a no-op (has_avar2 was always false in the flipped guards). Co-Authored-By: Claude Opus 4.8 --- src/OT/Color/COLR/COLR.hh | 8 +- src/OT/Layout/GDEF/GDEF.hh | 2 +- src/OT/Layout/GPOS/PairPosFormat1.hh | 2 +- src/OT/Layout/GPOS/PairPosFormat2.hh | 2 +- src/OT/Layout/GPOS/SinglePosFormat1.hh | 2 +- src/OT/Layout/GPOS/SinglePosFormat2.hh | 2 +- src/OT/glyf/glyf-helpers.hh | 2 +- src/OT/glyf/glyf.hh | 15 ++- src/hb-ot-head-table.hh | 2 +- src/hb-ot-hmtx-table.hh | 2 +- src/hb-ot-layout-base-table.hh | 2 +- src/hb-ot-maxp-table.hh | 2 +- src/hb-ot-os2-table.hh | 2 +- src/hb-ot-post-table.hh | 2 +- src/hb-ot-var-avar-table.hh | 31 ++++-- src/hb-ot-var-common.hh | 20 ++++ src/hb-ot-var-fvar-table.hh | 5 +- src/hb-ot-var-gvar-table.hh | 5 +- src/hb-ot-var-hvar-table.hh | 2 +- src/hb-subset-cff2.cc | 2 +- src/hb-subset-plan-member-list.hh | 11 ++ src/hb-subset-plan-var.cc | 141 +++++++++++++++++-------- src/hb-subset-table-var.cc | 6 +- src/hb-subset.cc | 2 +- 24 files changed, 198 insertions(+), 74 deletions(-) diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index 4445cf4a9..4c43436e7 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -2437,7 +2437,7 @@ struct COLR return_trace (true); const ItemVariationStore& var_store = this+varStore; - if (c->plan->normalized_coords && !c->plan->has_avar2) + if (c->plan->normalized_coords) { item_variations_t item_vars; /* turn off varstore optimization when varIdxMap is null, so we maintain @@ -2595,9 +2595,9 @@ struct COLR * after instancing */ if (!subset_varstore (c, colr_prime)) return_trace (false); - ItemVarStoreInstancer instancer (c->plan->has_avar2 ? nullptr : get_var_store_ptr (), - c->plan->has_avar2 ? nullptr : get_delta_set_index_map_ptr (), - c->plan->has_avar2 ? hb_array () : c->plan->normalized_coords.as_array ()); + ItemVarStoreInstancer instancer (c->plan->normalized_coords ? get_var_store_ptr () : nullptr, + c->plan->normalized_coords ? get_delta_set_index_map_ptr () : nullptr, + c->plan->normalized_coords.as_array ()); if (!colr_prime->baseGlyphList.serialize_subset (c, baseGlyphList, this, instancer)) return_trace (false); diff --git a/src/OT/Layout/GDEF/GDEF.hh b/src/OT/Layout/GDEF/GDEF.hh index c4b6e1f0f..e48354670 100644 --- a/src/OT/Layout/GDEF/GDEF.hh +++ b/src/OT/Layout/GDEF/GDEF.hh @@ -678,7 +678,7 @@ struct GDEFVersion1_2 if (unlikely (!c->serializer->embed (varStore))) return_trace (false); if (c->plan->all_axes_pinned) out->varStore = 0; - else if (c->plan->normalized_coords && !c->plan->has_avar2) + else if (c->plan->normalized_coords) { if (varStore) { diff --git a/src/OT/Layout/GPOS/PairPosFormat1.hh b/src/OT/Layout/GPOS/PairPosFormat1.hh index 6bb5bbdc3..a8ded1e75 100644 --- a/src/OT/Layout/GPOS/PairPosFormat1.hh +++ b/src/OT/Layout/GPOS/PairPosFormat1.hh @@ -156,7 +156,7 @@ struct PairPosFormat1_3 hb_pair_t newFormats = hb_pair (valueFormat[0], valueFormat[1]); - if (c->plan->normalized_coords && !c->plan->has_avar2) + if (c->plan->normalized_coords) { /* all device flags will be dropped when full instancing, no need to strip * hints, also do not strip emtpy cause we don't compute the new default diff --git a/src/OT/Layout/GPOS/PairPosFormat2.hh b/src/OT/Layout/GPOS/PairPosFormat2.hh index 85bca2a0c..e7320b932 100644 --- a/src/OT/Layout/GPOS/PairPosFormat2.hh +++ b/src/OT/Layout/GPOS/PairPosFormat2.hh @@ -312,7 +312,7 @@ struct PairPosFormat2_4 : ValueBase hb_pair_t newFormats = hb_pair (valueFormat1, valueFormat2); - if (c->plan->normalized_coords && !c->plan->has_avar2) + if (c->plan->normalized_coords) { /* in case of full instancing, all var device flags will be dropped so no * need to strip hints here */ diff --git a/src/OT/Layout/GPOS/SinglePosFormat1.hh b/src/OT/Layout/GPOS/SinglePosFormat1.hh index 4238a17d5..4792a4974 100644 --- a/src/OT/Layout/GPOS/SinglePosFormat1.hh +++ b/src/OT/Layout/GPOS/SinglePosFormat1.hh @@ -149,7 +149,7 @@ struct SinglePosFormat1 : ValueBase unsigned new_format = valueFormat; - if (c->plan->normalized_coords && !c->plan->has_avar2) + if (c->plan->normalized_coords) { new_format = valueFormat.get_effective_format (values.arrayZ, false, false, this, &c->plan->layout_variation_idx_delta_map); } diff --git a/src/OT/Layout/GPOS/SinglePosFormat2.hh b/src/OT/Layout/GPOS/SinglePosFormat2.hh index 97ecd286a..dc92f6a52 100644 --- a/src/OT/Layout/GPOS/SinglePosFormat2.hh +++ b/src/OT/Layout/GPOS/SinglePosFormat2.hh @@ -195,7 +195,7 @@ struct SinglePosFormat2 : ValueBase ; unsigned new_format = compute_effective_format (c->plan->source, it, - bool (c->plan->normalized_coords) && !c->plan->has_avar2, + bool (c->plan->normalized_coords), bool (c->plan->flags & HB_SUBSET_FLAGS_NO_HINTING), c->plan->has_gdef_varstore, &c->plan->layout_variation_idx_delta_map); diff --git a/src/OT/glyf/glyf-helpers.hh b/src/OT/glyf/glyf-helpers.hh index f3418c4cc..f157bf002 100644 --- a/src/OT/glyf/glyf-helpers.hh +++ b/src/OT/glyf/glyf-helpers.hh @@ -64,7 +64,7 @@ _add_head_and_set_loca_version (hb_subset_plan_t *plan, bool use_short_loca) head *head_prime = (head *) hb_blob_get_data_writable (head_prime_blob, nullptr); head_prime->indexToLocFormat = use_short_loca ? 0 : 1; - if (plan->normalized_coords && !plan->has_avar2) + if (plan->normalized_coords) { head_prime->xMin = plan->head_maxp_info.xMin; head_prime->xMax = plan->head_maxp_info.xMax; diff --git a/src/OT/glyf/glyf.hh b/src/OT/glyf/glyf.hh index 1a99a133a..985d3bb84 100644 --- a/src/OT/glyf/glyf.hh +++ b/src/OT/glyf/glyf.hh @@ -86,7 +86,7 @@ struct glyf } hb_font_t *font = nullptr; - if (c->plan->normalized_coords && !c->plan->has_avar2) + if (c->plan->normalized_coords) { font = _create_font_for_instancing (c->plan); if (unlikely (!font)) @@ -614,6 +614,19 @@ glyf::_create_font_for_instancing (const hb_subset_plan_t *plan) const hb_font_t *font = hb_font_create (plan->source); if (unlikely (font == hb_font_get_empty ())) return nullptr; +#ifndef HB_NO_VAR + if (plan->has_avar2) + { + /* Under avar2, instancing applies only to the self-contained pins, + * whose constant final coordinates the plan holds in normalized_coords. + * Setting user-space variations would run the full avar2 mapping and + * bake contributions that remain live in the instance's variations. */ + hb_font_set_var_coords_normalized (font, plan->normalized_coords.arrayZ, + plan->normalized_coords.length); + return font; + } +#endif + hb_vector_t vars; if (unlikely (!vars.alloc (plan->user_axes_location.get_population (), true))) { diff --git a/src/hb-ot-head-table.hh b/src/hb-ot-head-table.hh index be2d0a813..4cb6c15c6 100644 --- a/src/hb-ot-head-table.hh +++ b/src/hb-ot-head-table.hh @@ -66,7 +66,7 @@ struct head head *out = c->serializer->embed (this); if (unlikely (!out)) return_trace (false); - if (c->plan->normalized_coords && !c->plan->has_avar2) + if (c->plan->normalized_coords) { if (unlikely (!c->serializer->check_assign (out->xMin, c->plan->head_maxp_info.xMin, HB_SERIALIZE_ERROR_INT_OVERFLOW))) diff --git a/src/hb-ot-hmtx-table.hh b/src/hb-ot-hmtx-table.hh index 48b41157f..99ea38042 100644 --- a/src/hb-ot-hmtx-table.hh +++ b/src/hb-ot-hmtx-table.hh @@ -89,7 +89,7 @@ struct hmtxvmtx c->serializer->check_assign (table->numberOfLongMetrics, num_hmetrics, HB_SERIALIZE_ERROR_INT_OVERFLOW); #ifndef HB_NO_VAR - if (c->plan->normalized_coords && !c->plan->has_avar2) + if (c->plan->normalized_coords) { auto &MVAR = *c->plan->source->table.MVAR; if (T::is_horizontal) diff --git a/src/hb-ot-layout-base-table.hh b/src/hb-ot-layout-base-table.hh index b25231836..2470e7e2f 100644 --- a/src/hb-ot-layout-base-table.hh +++ b/src/hb-ot-layout-base-table.hh @@ -726,7 +726,7 @@ struct BASE TRACE_SUBSET (this); if (!c->serializer->allocate_size> (Offset32To::static_size)) return_trace (false); - if (!c->plan->normalized_coords || c->plan->has_avar2) + if (!c->plan->normalized_coords) return_trace (out->varStore.serialize_subset (c, varStore, this, c->plan->base_varstore_inner_maps.as_array ())); if (c->plan->all_axes_pinned) diff --git a/src/hb-ot-maxp-table.hh b/src/hb-ot-maxp-table.hh index 13d711212..8f000526b 100644 --- a/src/hb-ot-maxp-table.hh +++ b/src/hb-ot-maxp-table.hh @@ -111,7 +111,7 @@ struct maxp if (c->plan->flags & HB_SUBSET_FLAGS_NO_HINTING) drop_hint_fields (dest_v1); - if (c->plan->normalized_coords && !c->plan->has_avar2) + if (c->plan->normalized_coords) instancing_update_fields (c->plan->head_maxp_info, dest_v1); } diff --git a/src/hb-ot-os2-table.hh b/src/hb-ot-os2-table.hh index 1915c9a03..db8fd4d87 100644 --- a/src/hb-ot-os2-table.hh +++ b/src/hb-ot-os2-table.hh @@ -233,7 +233,7 @@ struct OS2 if (unlikely (!os2_prime)) return_trace (false); #ifndef HB_NO_VAR - if (c->plan->normalized_coords && !c->plan->has_avar2) + if (c->plan->normalized_coords) { auto &MVAR = *c->plan->source->table.MVAR; auto *table = os2_prime; diff --git a/src/hb-ot-post-table.hh b/src/hb-ot-post-table.hh index b78e85e0e..4536c7738 100644 --- a/src/hb-ot-post-table.hh +++ b/src/hb-ot-post-table.hh @@ -103,7 +103,7 @@ struct post return_trace (false); #ifndef HB_NO_VAR - if (c->plan->normalized_coords && !c->plan->has_avar2) + if (c->plan->normalized_coords) { auto &MVAR = *c->plan->source->table.MVAR; auto *table = post_prime; diff --git a/src/hb-ot-var-avar-table.hh b/src/hb-ot-var-avar-table.hh index ef95a731d..b798a8e27 100644 --- a/src/hb-ot-var-avar-table.hh +++ b/src/hb-ot-var-avar-table.hh @@ -264,9 +264,14 @@ struct SegmentMaps : Array16Of * We should instead keep the design coords in the shape plan and use * those. unmap_axis_range needs to be killed. */ - /* avar mapped normalized axis range*/ + /* avar mapped normalized axis range. Under avar2, axes_location holds + * only the self-contained pins for the other tables; avar itself uses + * the intermediate-space ranges of all restricted axes. */ + const auto &axes_location = c->plan->has_avar2 + ? c->plan->avar2_axes_location + : c->plan->axes_location; Triple *axis_range; - if (!c->plan->axes_location.has (axis_tag, &axis_range)) + if (!axes_location.has (axis_tag, &axis_range)) return c->serializer->embed (*this); TripleDistances *axis_triple_distances; @@ -767,7 +772,7 @@ struct avar item_variations_t item_vars; if (!item_vars.create_from_item_varstore (var_store, c->plan->axes_old_index_tag_map)) return false; - if (!item_vars.instantiate_tuple_vars_no_region_build (c->plan->axes_location, + if (!item_vars.instantiate_tuple_vars_no_region_build (c->plan->avar2_axes_location, c->plan->axes_triple_distances)) return false; @@ -793,6 +798,7 @@ struct avar hb_hashmap_t varidx_ref_count; for (unsigned i = 0; i < axisCount; i++) { + if (!c->plan->axes_index_map.has (i)) continue; /* self-contained: dropped */ uint32_t varidx = new_varidx_mapping[i]; if (varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX) continue; unsigned *count; @@ -806,7 +812,8 @@ struct avar hb_tag_t *axis_tag_ptr; /* Only axes that will receive offset compensation (restricted or * pinned) can contaminate a shared row. */ - if (!c->plan->axes_old_index_tag_map.has (i, &axis_tag_ptr) || + if (!c->plan->axes_index_map.has (i) || + !c->plan->axes_old_index_tag_map.has (i, &axis_tag_ptr) || !c->plan->user_axes_location.has (*axis_tag_ptr)) continue; uint32_t varidx = new_varidx_mapping[i]; @@ -833,6 +840,11 @@ struct avar continue; hb_tag_t axis_tag = *axis_tag_ptr; + /* Self-contained pinned axes are removed from fvar/avar; their + * contribution is baked into the variation tables instead. */ + if (!c->plan->axes_index_map.has (i)) + continue; + Triple *new_user; if (c->plan->user_axes_location.has (axis_tag, &new_user)) { @@ -1057,7 +1069,10 @@ struct avar * We serialize each sub-object via push/pop_pack, write the tail * struct inline, and link offsets to the avar table start. */ - /* Serialize DeltaSetIndexMap (push a new object) */ + /* Serialize DeltaSetIndexMap (push a new object). Entries cover the + * RETAINED axes only (self-contained pinned axes are removed from + * fvar), in their retained order. */ + unsigned retained_axis_count = c->plan->axes_index_map.get_population (); hb_serialize_context_t::objidx_t packed_map; { /* Compute width and inner_bit_count for the mapping. @@ -1067,6 +1082,7 @@ struct avar unsigned max_outer = 0, max_inner = 0; for (unsigned i = 0; i < axisCount; i++) { + if (!c->plan->axes_index_map.has (i)) continue; uint32_t varidx = new_varidx_mapping[i]; if (varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX) { @@ -1111,13 +1127,14 @@ struct avar auto *map_count_field = c->serializer->allocate_size (HBUINT16::static_size); if (unlikely (!map_count_field)) { c->serializer->pop_discard (); return false; } - *map_count_field = axisCount; + *map_count_field = retained_axis_count; - HBUINT8 *p = c->serializer->allocate_size (width * axisCount); + HBUINT8 *p = c->serializer->allocate_size (width * retained_axis_count); if (unlikely (!p)) { c->serializer->pop_discard (); return false; } for (unsigned i = 0; i < axisCount; i++) { + if (!c->plan->axes_index_map.has (i)) continue; uint32_t varidx = new_varidx_mapping[i]; unsigned o = varidx >> 16; unsigned in = varidx & 0xFFFF; diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index e6504671f..30600f2a3 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1305,6 +1305,22 @@ struct TupleVariationData } public: + /* avar2 partial-instancing culling: drop tuples whose region a partial + * instance can never reach (hb_subset_plan_t::avar2_reachable_ranges). */ + void cull_unreachable (const hb_hashmap_t &reachable_ranges) + { + unsigned j = 0; + for (unsigned i = 0; i < tuple_vars.length; i++) + { + if (_hb_avar2_region_is_dead (tuple_vars.arrayZ[i].axis_tuples, reachable_ranges)) + continue; + if (i != j) + tuple_vars.arrayZ[j] = std::move (tuple_vars.arrayZ[i]); + j++; + } + tuple_vars.shrink (j); + } + bool instantiate (const hb_hashmap_t& normalized_axes_location, const hb_hashmap_t& axes_triple_distances, optimize_scratch_t &scratch, @@ -1839,6 +1855,10 @@ struct item_variations_t { if (!create_from_item_varstore (varStore, plan->axes_old_index_tag_map, inner_maps)) return false; + /* avar2 partial instancing: cull unreachable regions. */ + if (plan->has_avar2 && plan->avar2_reachable_ranges.get_population ()) + for (tuple_variations_t& tuple_vars : vars) + tuple_vars.cull_unreachable (plan->avar2_reachable_ranges); if (!instantiate_tuple_vars (plan->axes_location, plan->axes_triple_distances)) return false; return as_item_varstore (optimize, use_no_variation_idx); diff --git a/src/hb-ot-var-fvar-table.hh b/src/hb-ot-var-fvar-table.hh index 6cd80082c..c98d78c7b 100644 --- a/src/hb-ot-var-fvar-table.hh +++ b/src/hb-ot-var-fvar-table.hh @@ -107,8 +107,9 @@ struct InstanceRecord if (!axis_coord_pinned_or_within_axis_range (coords, i, *axis_limit)) return_trace (false); - //skip pinned axis (but not for avar2, where pinned axes stay in fvar) - if (axis_limit->is_point () && !c->plan->has_avar2) + //skip axes dropped from fvar (pinned; or self-contained under avar2, + //where other pinned axes stay in fvar as hidden) + if (axis_limit->is_point () && !c->plan->axes_index_map.has (i)) continue; } diff --git a/src/hb-ot-var-gvar-table.hh b/src/hb-ot-var-gvar-table.hh index de75ecf04..ec1de51df 100644 --- a/src/hb-ot-var-gvar-table.hh +++ b/src/hb-ot-var-gvar-table.hh @@ -150,6 +150,9 @@ struct glyph_variations_t contour_point_vector_t *all_points; if (!plan->new_gid_contour_points_map.has (new_gid, &all_points)) return false; + /* avar2 partial instancing: cull unreachable tuples. */ + if (plan->has_avar2 && plan->avar2_reachable_ranges.get_population ()) + glyph_variations[i].cull_unreachable (plan->avar2_reachable_ranges); if (!glyph_variations[i].instantiate (plan->axes_location, plan->axes_triple_distances, scratch, &pool, all_points, iup_optimize)) return false; } @@ -428,7 +431,7 @@ struct gvar_GVAR if (c->plan->all_axes_pinned) return_trace (false); - if (c->plan->normalized_coords && !c->plan->has_avar2) + if (c->plan->normalized_coords) return_trace (instantiate (c)); unsigned glyph_count = version.to_int () ? c->plan->source->get_num_glyphs () : 0; diff --git a/src/hb-ot-var-hvar-table.hh b/src/hb-ot-var-hvar-table.hh index c0501bb5e..d32f00a40 100644 --- a/src/hb-ot-var-hvar-table.hh +++ b/src/hb-ot-var-hvar-table.hh @@ -369,7 +369,7 @@ struct HVARVVAR out->version.major = 1; out->version.minor = 0; - if (c->plan->normalized_coords && !c->plan->has_avar2) + if (c->plan->normalized_coords) { item_variations_t item_vars; if (!item_vars.instantiate (this+varStore, c->plan, diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc index e6881ddc9..262eeb1ab 100644 --- a/src/hb-subset-cff2.cc +++ b/src/hb-subset-cff2.cc @@ -502,7 +502,7 @@ struct cff2_subset_plan orig_fdcount = acc.fdArray->count; drop_hints = plan->flags & HB_SUBSET_FLAGS_NO_HINTING; - pinned = (bool) plan->normalized_coords && !plan->has_avar2; + pinned = (bool) plan->normalized_coords; normalized_coords = plan->normalized_coords; head_maxp_info = plan->head_maxp_info; hmtx_map = &plan->hmtx_map; diff --git a/src/hb-subset-plan-member-list.hh b/src/hb-subset-plan-member-list.hh index 581a57341..d6e73fb84 100644 --- a/src/hb-subset-plan-member-list.hh +++ b/src/hb-subset-plan-member-list.hh @@ -143,6 +143,17 @@ HB_SUBSET_PLAN_MEMBER (hb_hashmap_t E(), old_intermediates) //variation tuples/regions that a partial instance can never reach. HB_SUBSET_PLAN_MEMBER (hb_hashmap_t E(), avar2_reachable_ranges) +//avar2: intermediate-space (post-avar-v1) axis ranges of all restricted +//axes, used by avar itself for segment-map renormalization and IVS +//rebasing. Under avar2, axes_location instead holds only the +//self-contained pins (final space) for the other tables to instance. +HB_SUBSET_PLAN_MEMBER (hb_hashmap_t E(), avar2_axes_location) + +//avar2: self-contained pinned axes -> constant old-space final coord. +//These axes are removed from fvar/avar entirely; their contribution is +//baked into the variation tables like an ordinary pin at that coord. +HB_SUBSET_PLAN_MEMBER (hb_hashmap_t E(), avar2_self_contained) + //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 1c44834e8..79c6a462b 100644 --- a/src/hb-subset-plan-var.cc +++ b/src/hb-subset-plan-var.cc @@ -136,39 +136,62 @@ if (!box.has (tag, &identity)) return; double dmin = 0.0, dmax = 0.0; + bool bounded = true; 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++) + if (outer >= var_store->get_sub_table_count ()) bounded = false; + else { - 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; } + const OT::VarData &vd = var_store->get_sub_table (outer); + if (inner >= vd.get_item_count ()) bounded = false; + else + { + 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 && bounded; 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) { bounded = false; break; } + 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; } + } + } } } + if (!bounded) + continue; /* malformed row: no constraint, not self-contained */ + + /* A pinned axis whose delta is constant over the box is self-contained: + * its final coordinate is a constant. It can be removed from fvar/avar + * and its contribution baked into the variation tables like an ordinary + * pin at that coordinate (in old final space). */ + Triple *user; + if (dmin == dmax && + plan->user_axes_location.has (tag, &user) && user->is_point ()) + { + double v = hb_clamp (identity->first + dmin / 16384.0, -1.0, +1.0); + v = (double) roundf ((float) (v * 16384.0)) / 16384.0; + if (!plan->avar2_self_contained.set (tag, v)) return; + continue; /* axis is dropped; no region can reference it afterwards */ + } /* Pad by one F2Dot14 unit against rounding differences with the * runtime's fixed-point evaluation. */ @@ -189,6 +212,17 @@ { hb_font_t *font = hb_font_create (plan->source); + if (plan->has_avar2) + { + /* Under avar2, instancing applies only to the self-contained pins, + * whose constant final coordinates the plan holds in normalized_coords. + * Setting user-space variations would run the full avar2 mapping and + * bake contributions that remain live in the instance's variations. */ + hb_font_set_var_coords_normalized (font, plan->normalized_coords.arrayZ, + plan->normalized_coords.length); + return font; + } + hb_vector_t vars; if (!vars.alloc (plan->user_axes_location.get_population ())) { hb_font_destroy (font); @@ -400,39 +434,62 @@ normalize_axes_location (hb_face_t *face, hb_subset_plan_t *plan) hb_tag_t axis_tag = _.second.get_axis_tag (); if (plan->user_axes_location.has (axis_tag)) { - /* Store intermediate-space coords for offset compensation */ + /* Store intermediate-space coords for offset compensation and + * for avar's own segment-map renormalization and IVS rebasing. */ plan->old_intermediates.set (axis_tag, Triple ((double) normalized_mins[i], (double) normalized_defaults[i], (double) normalized_maxs[i])); - /* axes_location is also in intermediate space for avar2 */ - plan->axes_location.set (axis_tag, Triple ((double) normalized_mins[i], - (double) normalized_defaults[i], - (double) normalized_maxs[i])); - - float normalized_default = normalized_defaults[i]; - if (normalized_default == -0.f) - normalized_default = 0.f; - if (normalized_default != 0.f) - plan->pinned_at_default = false; - - plan->normalized_coords[i] = roundf (normalized_default * 16384.f); + plan->avar2_axes_location.set (axis_tag, Triple ((double) normalized_mins[i], + (double) normalized_defaults[i], + (double) normalized_maxs[i])); } } - /* For avar2, keep ALL axes in axes_index_map (including pinned ones), - * so pinned axes remain in fvar as hidden. */ + /* Reachable-range computation also detects self-contained pinned + * axes: pinned axes whose final coordinate is constant. */ + _compute_avar2_reachable_ranges (plan, axes, avar_table); + + /* Keep all axes in fvar (pinned ones as hidden), EXCEPT self-contained + * pinned axes, which are removed entirely. */ plan->axes_index_map.reset (); plan->axis_tags.reset (); + unsigned retained_axis_idx = 0; for (const auto& _ : + hb_enumerate (axes)) { unsigned i = _.first; hb_tag_t axis_tag = _.second.get_axis_tag (); - plan->axes_index_map.set (i, (unsigned) _.first); + if (plan->avar2_self_contained.has (axis_tag)) + continue; + plan->axes_index_map.set (i, retained_axis_idx++); plan->axis_tags.push (axis_tag); } plan->all_axes_pinned = false; - _compute_avar2_reachable_ranges (plan, axes, avar_table); + /* The other tables see ONLY the self-contained pins, as an ordinary + * partial instancing in old final-coordinate space; the standard + * instancing machinery bakes those axes' contributions in. The + * remaining restriction is carried entirely by avar. */ + plan->axes_location.reset (); + if (plan->avar2_self_contained.get_population ()) + { + bool sc_pinned_at_default = true; + for (auto _ : plan->avar2_self_contained) + { + plan->axes_location.set (_.first, Triple (_.second, _.second, _.second)); + if (_.second != 0.0) + sc_pinned_at_default = false; + } + for (const auto& _ : + hb_enumerate (axes)) + { + double *v; + plan->normalized_coords[_.first] = + plan->avar2_self_contained.has (_.second.get_axis_tag (), &v) + ? (int) roundf ((float) (*v * 16384.0)) : 0; + } + plan->pinned_at_default = sc_pinned_at_default; + } + else + plan->normalized_coords.shrink (0); } else { diff --git a/src/hb-subset-table-var.cc b/src/hb-subset-table-var.cc index 570a33bcf..2aa2ad565 100644 --- a/src/hb-subset-table-var.cc +++ b/src/hb-subset-table-var.cc @@ -30,7 +30,8 @@ bool _hb_subset_table_var (hb_subset_plan_t *plan, hb_vector_t &buf, hb_t 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) + if (plan->user_axes_location.is_empty () || + (plan->has_avar2 && !plan->normalized_coords)) *success = _hb_subset_table_passthrough (plan, tag); else *success = _hb_subset_table (plan, buf); @@ -38,7 +39,8 @@ bool _hb_subset_table_var (hb_subset_plan_t *plan, hb_vector_t &buf, hb_t 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) + if (plan->user_axes_location.is_empty () || + (plan->has_avar2 && !plan->normalized_coords)) *success = _hb_subset_table_passthrough (plan, tag); else *success = _hb_subset_table (plan, buf); diff --git a/src/hb-subset.cc b/src/hb-subset.cc index 91314d610..d3f97f4bb 100644 --- a/src/hb-subset.cc +++ b/src/hb-subset.cc @@ -295,7 +295,7 @@ _subset_table (hb_subset_plan_t *plan, case HB_TAG('c','v','t',' '): #ifndef HB_NO_VAR if (_is_table_present (plan->source, HB_TAG('c','v','a','r')) && - plan->normalized_coords && !plan->pinned_at_default && !plan->has_avar2) + plan->normalized_coords && !plan->pinned_at_default) { auto &cvar = *plan->source->table.cvar; return OT::cvar::add_cvt_and_apply_deltas (plan, cvar.get_tuple_var_data (), &cvar);