From 3d6677f05cd9debf15ac871c59ea9ce0ab222cff Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 1 Oct 2026 19:25:17 -0600 Subject: [PATCH] [subset] Preserve LookupVariationRecord tables Subset FeatureVariations 1.1 lookup records while remapping feature and lookup indices. Preserve all condition formats and remap variation indices used by value conditions. Keep features with lookup variations distinct during feature deduplication, since equal default lookup lists do not imply equal variable behavior. Tested with the full Meson test suite and a targeted HB_NO_VAR build. Assisted-by: OpenAI Codex --- src/hb-ot-layout-common.hh | 145 ++++++++++++++++++++++++++++++++--- src/hb-subset-plan-layout.cc | 6 ++ test/api/test-subset.c | 43 +++++++++++ 3 files changed, 185 insertions(+), 9 deletions(-) diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 0c88fb7be..490839f22 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -4330,8 +4330,23 @@ struct ConditionValue bool subset (hb_subset_context_t *c) const { TRACE_SUBSET (this); - // TODO(subset) - return_trace (false); + auto *out = c->serializer->embed (this); + if (unlikely (!out)) return_trace (false); + + if (varIdx == VarIdx::NO_VARIATION) + return_trace (true); + + hb_pair_t *new_varidx_delta; + if (!c->plan->layout_variation_idx_delta_map.has (varIdx, &new_varidx_delta)) + return_trace (false); + + if (unlikely (!c->serializer->check_assign ( + out->defaultValue, + defaultValue + hb_second (*new_varidx_delta), + HB_SERIALIZE_ERROR_INT_OVERFLOW))) + return_trace (false); + out->varIdx = hb_first (*new_varidx_delta); + return_trace (true); } private: @@ -4375,8 +4390,15 @@ struct ConditionAnd bool subset (hb_subset_context_t *c) const { TRACE_SUBSET (this); - // TODO(subset) - return_trace (false); + auto *out = c->serializer->start_embed (this); + if (unlikely (!out || !c->serializer->extend_min (out))) + return_trace (false); + + for (const auto &offset : conditions) + if (unlikely (!subset_offset_array (c, out->conditions, this) (offset))) + return_trace (false); + + return_trace (true); } private: @@ -4423,8 +4445,15 @@ struct ConditionOr bool subset (hb_subset_context_t *c) const { TRACE_SUBSET (this); - // TODO(subset) - return_trace (false); + auto *out = c->serializer->start_embed (this); + if (unlikely (!out || !c->serializer->extend_min (out))) + return_trace (false); + + for (const auto &offset : conditions) + if (unlikely (!subset_offset_array (c, out->conditions, this) (offset))) + return_trace (false); + + return_trace (true); } private: @@ -4471,8 +4500,9 @@ struct ConditionNegate bool subset (hb_subset_context_t *c) const { TRACE_SUBSET (this); - // TODO(subset) - return_trace (false); + auto *out = c->serializer->embed (this); + if (unlikely (!out)) return_trace (false); + return_trace (out->condition.serialize_subset (c, condition, this)); } private: @@ -5079,6 +5109,23 @@ struct LookupIndexList bool intersects_lookup_indexes (const hb_map_t *lookup_indexes) const { return lookupIndices.intersects (lookup_indexes); } + bool subset (hb_subset_context_t *c, + hb_subset_layout_context_t *l) const + { + TRACE_SUBSET (this); + auto *out = c->serializer->start_embed (this); + if (unlikely (!out || !c->serializer->extend_min (out))) + return_trace (false); + + auto it = + + hb_iter (lookupIndices) + | hb_filter (l->lookup_index_map) + | hb_map (l->lookup_index_map) + ; + out->lookupIndices.serialize (c->serializer, l, it); + return_trace (bool (out->lookupIndices)); + } + bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); @@ -5115,6 +5162,19 @@ struct LookupConditionRecord c->layout_variation_indices->err (); } + bool subset (hb_subset_context_t *c, + hb_subset_layout_context_t *l, + const void *base) const + { + TRACE_SUBSET (this); + auto *out = c->serializer->embed (this); + if (unlikely (!out)) return_trace (false); + + if (unlikely (!out->condition.serialize_subset (c, condition, base))) + return_trace (false); + return_trace (out->lookupIndices.serialize_subset (c, lookupIndices, base, l)); + } + bool sanitize (hb_sanitize_context_t *c, const void *base) const { TRACE_SANITIZE (this); @@ -5164,6 +5224,30 @@ struct FeatureLookupsTable record.collect_variation_indices (c, this); } + bool subset (hb_subset_context_t *c, + hb_subset_layout_context_t *l) const + { + TRACE_SUBSET (this); + auto *out = c->serializer->start_embed (this); + if (unlikely (!out || !c->serializer->extend_min (out))) + return_trace (false); + + out->version.major = version.major; + out->version.minor = version.minor; + out->flags = flags; + + for (const LookupConditionRecord &record : records) + { + auto snap = c->serializer->snapshot (); + if (record.subset (c, l, this)) + out->records.len++; + else + c->serializer->revert (snap); + } + + return_trace (true); + } + bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); @@ -5206,6 +5290,24 @@ struct LookupVariationRecord const void *base) const { (base+featureLookups).collect_variation_indices (c); } + bool subset (hb_subset_layout_context_t *l, const void *base) const + { + TRACE_SUBSET (this); + uint32_t *new_feature_index; + if (!l->feature_map_w_duplicates->has (featureIndex, &new_feature_index)) + return_trace (false); + + hb_subset_context_t *c = l->subset_context; + auto *out = c->serializer->embed (this); + if (unlikely (!out || + !c->serializer->check_assign (out->featureIndex, + *new_feature_index, + HB_SERIALIZE_ERROR_INT_OVERFLOW))) + return_trace (false); + + return_trace (out->featureLookups.serialize_subset (c, featureLookups, base, l)); + } + bool sanitize (hb_sanitize_context_t *c, const void *base) const { TRACE_SANITIZE (this); @@ -5359,6 +5461,18 @@ struct FeatureVariations record.collect_variation_indices (c, this); } + bool has_lookup_variations (unsigned feature_index) const + { + for (const LookupVariationRecord &record : get_lookup_variation_records ()) + { + if (record.featureIndex == feature_index) + return true; + if (record.featureIndex > feature_index) + break; + } + return false; + } + bool subset (hb_subset_context_t *c, hb_subset_layout_context_t *l) const { @@ -5394,7 +5508,20 @@ struct FeatureVariations subset_record_array (l, &(out->varRecords), this, insert_catch_all_record) (varRecords[0]); } - return_trace (bool (out->varRecords)); + Array32Of *out_lookup_records = nullptr; + if (version.minor >= 1) + { + out_lookup_records = &StructAfter> (out->varRecords); + if (unlikely (!c->serializer->extend_min (out_lookup_records))) + return_trace (false); + + + get_lookup_variation_records ().iter () + | hb_apply (subset_record_array (l, out_lookup_records, this)) + ; + } + + return_trace (bool (out->varRecords) || + (out_lookup_records && bool (*out_lookup_records))); } bool sanitize (hb_sanitize_context_t *c) const diff --git a/src/hb-subset-plan-layout.cc b/src/hb-subset-plan-layout.cc index d58e994bc..25512f404 100644 --- a/src/hb-subset-plan-layout.cc +++ b/src/hb-subset-plan-layout.cc @@ -206,6 +206,12 @@ _GSUBGPOS_find_duplicate_features (const OT::GSUBGPOS &g, hb_set_t* same_tag_features = unique_features.get (t); for (unsigned other_f_index : same_tag_features->iter ()) { +#ifndef HB_NO_VAR + if (g.get_feature_variations ().has_lookup_variations (i) || + g.get_feature_variations ().has_lookup_variations (other_f_index)) + continue; +#endif + const OT::Feature* f = &(g.get_feature (i)); const OT::Feature **p = nullptr; if (feature_substitutes_map->has (i, &p)) diff --git a/test/api/test-subset.c b/test/api/test-subset.c index 37ce10d6f..6b075c603 100644 --- a/test/api/test-subset.c +++ b/test/api/test-subset.c @@ -92,6 +92,46 @@ test_subset_crash (void) hb_face_destroy (face); } +#ifndef HB_NO_VAR +static void +test_subset_lookup_variations (void) +{ + hb_face_t *face = hb_test_open_font_file ( + "../shape/data/in-house/fonts/48b61f685ab1b3a3690fe4fea46ffc73dac3c301.ttf"); + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + + hb_set_add_range (hb_subset_input_unicode_set (input), 'A', 'F'); + hb_set_t *features = hb_subset_input_set (input, HB_SUBSET_SETS_LAYOUT_FEATURE_TAG); + hb_set_clear (features); + hb_set_add (features, HB_TAG ('l', 'i', 'g', 'a')); + + hb_face_t *subset = hb_subset_or_fail (face, input); + g_assert_nonnull (subset); + + hb_font_t *font = hb_font_create (subset); + int coords[] = {13107}; /* 0.8 */ + hb_font_set_var_coords_normalized (font, coords, 1); + + hb_buffer_t *buffer = hb_buffer_create (); + hb_buffer_add_utf8 (buffer, "ABCDEF", -1, 0, -1); + hb_buffer_guess_segment_properties (buffer); + hb_shape (font, buffer, NULL, 0); + + unsigned length; + const hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &length); + const hb_codepoint_t expected[] = {2, 4, 6, 8, 9, 10}; + g_assert_cmpuint (length, ==, G_N_ELEMENTS (expected)); + for (unsigned i = 0; i < length; i++) + g_assert_cmpuint (info[i].codepoint, ==, expected[i]); + + hb_buffer_destroy (buffer); + hb_font_destroy (font); + hb_face_destroy (subset); + hb_subset_input_destroy (input); + hb_face_destroy (face); +} +#endif + static void test_subset_set_flags (void) { @@ -640,6 +680,9 @@ main (int argc, char **argv) hb_test_add (test_subset_32_tables); hb_test_add (test_subset_no_inf_loop); hb_test_add (test_subset_crash); +#ifndef HB_NO_VAR + hb_test_add (test_subset_lookup_variations); +#endif hb_test_add (test_subset_set_flags); hb_test_add (test_subset_sets); hb_test_add (test_subset_plan);