From f5ce0905545f46d705d1a0b7bfc4127dda59a58c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 20 Aug 2026 05:25:15 -0600 Subject: [PATCH] [subset] Remap ClassDef glyphs through the flat array Extend the flat old-to-new gid array introduced for Coverage::subset to ClassDef subsetting: add a map_gsub_glyph() helper on the plan and use it in ClassDefFormat1/2::subset in place of per-glyph glyph_map_gsub hash lookups. The use_class_zero glyph counts switch from the map to the equivalent glyphset_gsub set. Tested with meson test (all tests pass). Co-Authored-By: Claude Fable 5 --- src/OT/Layout/Common/Coverage.hh | 8 ++++---- src/hb-ot-layout-common.hh | 15 +++++++-------- src/hb-subset-plan.hh | 12 ++++++++++++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/OT/Layout/Common/Coverage.hh b/src/OT/Layout/Common/Coverage.hh index 7b6e8867d..492d09b3a 100644 --- a/src/OT/Layout/Common/Coverage.hh +++ b/src/OT/Layout/Common/Coverage.hh @@ -189,12 +189,12 @@ struct Coverage bool subset (hb_subset_context_t *c) const { TRACE_SUBSET (this); - const hb_vector_t &glyph_map = c->plan->glyph_map_gsub_flat; + const hb_subset_plan_t *plan = c->plan; auto it = + iter () - | hb_take (c->plan->source->get_num_glyphs ()) - | hb_map_retains_sorting ([&glyph_map] (hb_codepoint_t g) { - return g < glyph_map.length ? glyph_map.arrayZ[g] : HB_MAP_VALUE_INVALID; + | hb_take (plan->source->get_num_glyphs ()) + | hb_map_retains_sorting ([plan] (hb_codepoint_t g) { + return plan->map_gsub_glyph (g); }) | hb_filter ([] (hb_codepoint_t glyph) { return glyph != HB_MAP_VALUE_INVALID; }) ; diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 0927f7047..45a257eb7 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -1551,7 +1551,6 @@ struct ClassDefFormat1_3 const Coverage* glyph_filter = nullptr) const { TRACE_SUBSET (this); - const hb_map_t &glyph_map = c->plan->glyph_map_gsub; hb_sorted_vector_t glyph_and_klass; hb_set_t orig_klasses; @@ -1561,7 +1560,7 @@ struct ClassDefFormat1_3 for (const hb_codepoint_t gid : + hb_range (start, end)) { - hb_codepoint_t new_gid = glyph_map[gid]; + hb_codepoint_t new_gid = c->plan->map_gsub_glyph (gid); if (new_gid == HB_MAP_VALUE_INVALID) continue; if (glyph_filter && !glyph_filter->has(gid)) continue; @@ -1574,9 +1573,10 @@ struct ClassDefFormat1_3 if (use_class_zero) { + const hb_set_t& glyphset = *c->plan->glyphset_gsub (); unsigned glyph_count = glyph_filter - ? hb_len (hb_iter (glyph_map.keys()) | hb_filter (glyph_filter)) - : glyph_map.get_population (); + ? hb_len (hb_iter (glyphset) | hb_filter (glyph_filter)) + : glyphset.get_population (); use_class_zero = glyph_count <= glyph_and_klass.length; } if (!ClassDef_remap_and_serialize (c->serializer, @@ -1810,7 +1810,6 @@ struct ClassDefFormat2_4 const Coverage* glyph_filter = nullptr) const { TRACE_SUBSET (this); - const hb_map_t &glyph_map = c->plan->glyph_map_gsub; const hb_set_t &glyph_set = *c->plan->glyphset_gsub (); hb_sorted_vector_t glyph_and_klass; @@ -1823,7 +1822,7 @@ struct ClassDefFormat2_4 { unsigned klass = get_class (g); if (!klass) continue; - hb_codepoint_t new_gid = glyph_map[g]; + hb_codepoint_t new_gid = c->plan->map_gsub_glyph (g); if (new_gid == HB_MAP_VALUE_INVALID) continue; if (glyph_filter && !glyph_filter->has (g)) continue; glyph_and_klass.push (hb_pair (new_gid, klass)); @@ -1841,7 +1840,7 @@ struct ClassDefFormat2_4 hb_codepoint_t end = hb_min (range.last + 1, num_source_glyphs); for (hb_codepoint_t g = start; g < end; g++) { - hb_codepoint_t new_gid = glyph_map[g]; + hb_codepoint_t new_gid = c->plan->map_gsub_glyph (g); if (new_gid == HB_MAP_VALUE_INVALID) continue; if (glyph_filter && !glyph_filter->has (g)) continue; @@ -1854,7 +1853,7 @@ struct ClassDefFormat2_4 const hb_set_t& glyphset = *c->plan->glyphset_gsub (); unsigned glyph_count = glyph_filter ? hb_len (hb_iter (glyphset) | hb_filter (glyph_filter)) - : glyph_map.get_population (); + : glyphset.get_population (); use_class_zero = use_class_zero && glyph_count <= glyph_and_klass.length; if (!ClassDef_remap_and_serialize (c->serializer, orig_klasses, diff --git a/src/hb-subset-plan.hh b/src/hb-subset-plan.hh index 9baf3d82e..a84a305fe 100644 --- a/src/hb-subset-plan.hh +++ b/src/hb-subset-plan.hh @@ -241,6 +241,18 @@ struct hb_subset_plan_t return &_glyphset_gsub; } + /* + * Old-to-new glyph id mapping for GSUB glyphs, through the flat + * array; returns HB_MAP_VALUE_INVALID for glyphs not retained. + */ + inline hb_codepoint_t + map_gsub_glyph (hb_codepoint_t old_gid) const + { + return old_gid < glyph_map_gsub_flat.length + ? glyph_map_gsub_flat.arrayZ[old_gid] + : HB_MAP_VALUE_INVALID; + } + /* * The total number of output glyphs in the final subset. */