From 87454172a813fd2e0491a6e9f9d0afd0bf19d46d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 15 Oct 2026 11:52:19 -0600 Subject: [PATCH 1/9] [ot] Share mark attachment positioning across table widths MarkArray::apply instantiates the same positioning and attachment-chain updates for each mark-array and anchor-matrix representation. Resolve the format-specific anchors in the wrapper and pass them to a common helper. Preserve the anchor checks, buffer flags, messages, and attachment overflow behavior. With GCC 15.3 on x86-64 without LTO, this removes 1576 bytes of core code/read-only data at -O3 and 648 bytes at -Os. Validated with the full Meson suite (292 passed, one skipped) and sampled shaping benchmarks. Assisted-by: OpenAI Codex --- src/OT/Layout/GPOS/MarkArray.hh | 95 ++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 43 deletions(-) diff --git a/src/OT/Layout/GPOS/MarkArray.hh b/src/OT/Layout/GPOS/MarkArray.hh index 04a14ae9f..af840c806 100644 --- a/src/OT/Layout/GPOS/MarkArray.hh +++ b/src/OT/Layout/GPOS/MarkArray.hh @@ -31,6 +31,57 @@ resolve_cross_offset (const hb_glyph_position_t *pos, return offset; } +static inline bool +apply_mark_attachment (hb_ot_apply_context_t *c, + const Anchor &mark_anchor, + const Anchor &glyph_anchor, + unsigned glyph_pos) +{ + hb_buffer_t *buffer = c->buffer; + float mark_x, mark_y, base_x, base_y; + + buffer->unsafe_to_break (glyph_pos, buffer->idx + 1); + mark_anchor.get_anchor (c, buffer->cur().codepoint, &mark_x, &mark_y); + glyph_anchor.get_anchor (c, buffer->info[glyph_pos].codepoint, &base_x, &base_y); + + if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ()) + { + c->buffer->message (c->font, + "attaching mark glyph at %u to glyph at %u", + c->buffer->idx, glyph_pos); + } + + hb_position_t base_offset = resolve_cross_offset (buffer->pos, buffer->len, glyph_pos, + buffer->props.direction); + + hb_glyph_position_t &mark = buffer->cur_pos(); + mark.attach_chain() = (int) glyph_pos - (int) buffer->idx; + if (mark.attach_chain() != (int) glyph_pos - (int) buffer->idx) + { + mark.attach_chain() = 0; + goto overflow; + } + mark.attach_type() = ATTACH_TYPE_MARK; + mark.x_offset = roundf (base_x - mark_x); + mark.y_offset = roundf (base_y - mark_y); + if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction)) + mark.y_offset = hb_saturate_add (mark.y_offset, base_offset); + else + mark.x_offset = hb_saturate_add (mark.x_offset, base_offset); + buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT; + + if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ()) + { + c->buffer->message (c->font, + "attached mark glyph at %u to glyph at %u", + c->buffer->idx, glyph_pos); + } + +overflow: + buffer->idx++; + return true; +} + template struct MarkArray : Types::template ArrayOf> /* Array of MarkRecords--in Coverage order */ @@ -50,7 +101,6 @@ struct MarkArray : Types::template ArrayOf> unsigned int glyph_pos) const { TRACE_APPLY (this); - hb_buffer_t *buffer = c->buffer; const MarkRecord &record = Super::operator[](mark_index); unsigned int mark_class = record.klass; @@ -61,48 +111,7 @@ struct MarkArray : Types::template ArrayOf> * return false such that the subsequent subtables have a chance at it. */ if (unlikely (!found)) return_trace (false); - float mark_x, mark_y, base_x, base_y; - - buffer->unsafe_to_break (glyph_pos, buffer->idx + 1); - mark_anchor.get_anchor (c, buffer->cur().codepoint, &mark_x, &mark_y); - glyph_anchor.get_anchor (c, buffer->info[glyph_pos].codepoint, &base_x, &base_y); - - if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ()) - { - c->buffer->message (c->font, - "attaching mark glyph at %u to glyph at %u", - c->buffer->idx, glyph_pos); - } - - hb_position_t base_offset = resolve_cross_offset (buffer->pos, buffer->len, glyph_pos, - buffer->props.direction); - - hb_glyph_position_t &mark = buffer->cur_pos(); - mark.attach_chain() = (int) glyph_pos - (int) buffer->idx; - if (mark.attach_chain() != (int) glyph_pos - (int) buffer->idx) - { - mark.attach_chain() = 0; - goto overflow; - } - mark.attach_type() = ATTACH_TYPE_MARK; - mark.x_offset = roundf (base_x - mark_x); - mark.y_offset = roundf (base_y - mark_y); - if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction)) - mark.y_offset = hb_saturate_add (mark.y_offset, base_offset); - else - mark.x_offset = hb_saturate_add (mark.x_offset, base_offset); - buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT; - - if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ()) - { - c->buffer->message (c->font, - "attached mark glyph at %u to glyph at %u", - c->buffer->idx, glyph_pos); - } - - overflow: - buffer->idx++; - return_trace (true); + return_trace (apply_mark_attachment (c, mark_anchor, glyph_anchor, glyph_pos)); } template Date: Thu, 15 Oct 2026 11:55:19 -0600 Subject: [PATCH 2/9] [ot] Share cursive attachment positioning across table widths CursivePosFormat1_2 duplicates the direction-dependent positioning and attachment-chain updates for legacy and beyond-64k entry/exit records. Keep record access and anchor validation in the format-specific wrapper, then pass the resolved anchors to a common attachment helper. Preserve all direction cases, chain reversal, overflow handling, and messages. With GCC 15.3 on x86-64 without LTO, this removes another 2760 bytes of core code/read-only data at -O3 and 864 bytes at -Os. Validated with the full Meson suite (292 passed, one skipped) and sampled shaping benchmarks. Assisted-by: OpenAI Codex --- src/OT/Layout/GPOS/CursivePosFormat1.hh | 244 +++++++++++++----------- 1 file changed, 128 insertions(+), 116 deletions(-) diff --git a/src/OT/Layout/GPOS/CursivePosFormat1.hh b/src/OT/Layout/GPOS/CursivePosFormat1.hh index 1505f30dd..e37d240b5 100644 --- a/src/OT/Layout/GPOS/CursivePosFormat1.hh +++ b/src/OT/Layout/GPOS/CursivePosFormat1.hh @@ -100,6 +100,130 @@ reverse_cursive_minor_offset (hb_glyph_position_t *pos, } +static inline bool +apply_cursive_attachment (hb_ot_apply_context_t *c, + const Anchor &exit_anchor, + const Anchor &entry_anchor, + unsigned i) +{ + hb_buffer_t *buffer = c->buffer; + unsigned int j = buffer->idx; + + if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ()) + { + c->buffer->message (c->font, + "cursive attaching glyph at %u to glyph at %u", + i, j); + } + + buffer->unsafe_to_break (i, j + 1); + float entry_x, entry_y, exit_x, exit_y; + exit_anchor.get_anchor (c, buffer->info[i].codepoint, &exit_x, &exit_y); + entry_anchor.get_anchor (c, buffer->info[j].codepoint, &entry_x, &entry_y); + + hb_glyph_position_t *pos = buffer->pos; + + hb_position_t d; + /* Main-direction adjustment */ + switch (c->direction) { + case HB_DIRECTION_LTR: + pos[i].x_advance = hb_saturate_add ((hb_position_t) roundf (exit_x), pos[i].x_offset); + + d = hb_saturate_add ((hb_position_t) roundf (entry_x), pos[j].x_offset); + pos[j].x_advance = hb_saturate_sub (pos[j].x_advance, d); + pos[j].x_offset = hb_saturate_sub (pos[j].x_offset, d); + break; + case HB_DIRECTION_RTL: + d = hb_saturate_add ((hb_position_t) roundf (exit_x), pos[i].x_offset); + pos[i].x_advance = hb_saturate_sub (pos[i].x_advance, d); + pos[i].x_offset = hb_saturate_sub (pos[i].x_offset, d); + + pos[j].x_advance = hb_saturate_add ((hb_position_t) roundf (entry_x), pos[j].x_offset); + break; + case HB_DIRECTION_TTB: + pos[i].y_advance = hb_saturate_add ((hb_position_t) roundf (exit_y), pos[i].y_offset); + + d = hb_saturate_add ((hb_position_t) roundf (entry_y), pos[j].y_offset); + pos[j].y_advance = hb_saturate_sub (pos[j].y_advance, d); + pos[j].y_offset = hb_saturate_sub (pos[j].y_offset, d); + break; + case HB_DIRECTION_BTT: + d = hb_saturate_add ((hb_position_t) roundf (exit_y), pos[i].y_offset); + pos[i].y_advance = hb_saturate_sub (pos[i].y_advance, d); + pos[i].y_offset = hb_saturate_sub (pos[i].y_offset, d); + + pos[j].y_advance = (hb_position_t) roundf (entry_y); + break; + case HB_DIRECTION_INVALID: + default: + break; + } + + /* Cross-direction adjustment */ + + /* We attach child to parent (think graph theory and rooted trees whereas + * the root stays on baseline and each node aligns itself against its + * parent. + * + * Optimize things for the case of RightToLeft, as that's most common in + * Arabic. */ + unsigned int child = i; + unsigned int parent = j; + hb_position_t x_offset = (hb_position_t) roundf (entry_x - exit_x); + hb_position_t y_offset = (hb_position_t) roundf (entry_y - exit_y); + if (!(c->lookup_props & LookupFlag::RightToLeft)) + { + unsigned int k = child; + child = parent; + parent = k; + x_offset = -x_offset; + y_offset = -y_offset; + } + + /* If child was already connected to someone else, walk through its old + * chain and reverse the link direction, such that the whole tree of its + * previous connection now attaches to new parent. Watch out for case + * where new parent is on the path from old chain... + */ + reverse_cursive_minor_offset (pos, buffer->len, child, c->direction, parent); + + pos[child].attach_chain() = (int) parent - (int) child; + if (pos[child].attach_chain() != (int) parent - (int) child) + { + pos[child].attach_chain() = 0; + goto overflow; + } + pos[child].attach_type() = ATTACH_TYPE_CURSIVE; + buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT; + if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction))) + pos[child].y_offset = y_offset; + else + pos[child].x_offset = x_offset; + + /* If parent was attached to child, separate them. + * https://github.com/harfbuzz/harfbuzz/issues/2469 + */ + if (unlikely (pos[parent].attach_chain() == -pos[child].attach_chain())) + { + pos[parent].attach_chain() = 0; + if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction))) + pos[parent].y_offset = 0; + else + pos[parent].x_offset = 0; + } + + if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ()) + { + c->buffer->message (c->font, + "cursive attached glyph at %u to glyph at %u", + i, j); + } + +overflow: + buffer->idx++; + return true; +} + template struct CursivePosFormat1_2 { @@ -176,122 +300,10 @@ struct CursivePosFormat1_2 } hb_barrier (); - unsigned int i = skippy_iter.idx; - unsigned int j = buffer->idx; - - if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ()) - { - c->buffer->message (c->font, - "cursive attaching glyph at %u to glyph at %u", - i, j); - } - - buffer->unsafe_to_break (i, j + 1); - float entry_x, entry_y, exit_x, exit_y; - (this+prev_record.exitAnchor).get_anchor (c, buffer->info[i].codepoint, &exit_x, &exit_y); - (this+this_record.entryAnchor).get_anchor (c, buffer->info[j].codepoint, &entry_x, &entry_y); - - hb_glyph_position_t *pos = buffer->pos; - - hb_position_t d; - /* Main-direction adjustment */ - switch (c->direction) { - case HB_DIRECTION_LTR: - pos[i].x_advance = hb_saturate_add ((hb_position_t) roundf (exit_x), pos[i].x_offset); - - d = hb_saturate_add ((hb_position_t) roundf (entry_x), pos[j].x_offset); - pos[j].x_advance = hb_saturate_sub (pos[j].x_advance, d); - pos[j].x_offset = hb_saturate_sub (pos[j].x_offset, d); - break; - case HB_DIRECTION_RTL: - d = hb_saturate_add ((hb_position_t) roundf (exit_x), pos[i].x_offset); - pos[i].x_advance = hb_saturate_sub (pos[i].x_advance, d); - pos[i].x_offset = hb_saturate_sub (pos[i].x_offset, d); - - pos[j].x_advance = hb_saturate_add ((hb_position_t) roundf (entry_x), pos[j].x_offset); - break; - case HB_DIRECTION_TTB: - pos[i].y_advance = hb_saturate_add ((hb_position_t) roundf (exit_y), pos[i].y_offset); - - d = hb_saturate_add ((hb_position_t) roundf (entry_y), pos[j].y_offset); - pos[j].y_advance = hb_saturate_sub (pos[j].y_advance, d); - pos[j].y_offset = hb_saturate_sub (pos[j].y_offset, d); - break; - case HB_DIRECTION_BTT: - d = hb_saturate_add ((hb_position_t) roundf (exit_y), pos[i].y_offset); - pos[i].y_advance = hb_saturate_sub (pos[i].y_advance, d); - pos[i].y_offset = hb_saturate_sub (pos[i].y_offset, d); - - pos[j].y_advance = (hb_position_t) roundf (entry_y); - break; - case HB_DIRECTION_INVALID: - default: - break; - } - - /* Cross-direction adjustment */ - - /* We attach child to parent (think graph theory and rooted trees whereas - * the root stays on baseline and each node aligns itself against its - * parent. - * - * Optimize things for the case of RightToLeft, as that's most common in - * Arabic. */ - unsigned int child = i; - unsigned int parent = j; - hb_position_t x_offset = (hb_position_t) roundf (entry_x - exit_x); - hb_position_t y_offset = (hb_position_t) roundf (entry_y - exit_y); - if (!(c->lookup_props & LookupFlag::RightToLeft)) - { - unsigned int k = child; - child = parent; - parent = k; - x_offset = -x_offset; - y_offset = -y_offset; - } - - /* If child was already connected to someone else, walk through its old - * chain and reverse the link direction, such that the whole tree of its - * previous connection now attaches to new parent. Watch out for case - * where new parent is on the path from old chain... - */ - reverse_cursive_minor_offset (pos, buffer->len, child, c->direction, parent); - - pos[child].attach_chain() = (int) parent - (int) child; - if (pos[child].attach_chain() != (int) parent - (int) child) - { - pos[child].attach_chain() = 0; - goto overflow; - } - pos[child].attach_type() = ATTACH_TYPE_CURSIVE; - buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT; - if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction))) - pos[child].y_offset = y_offset; - else - pos[child].x_offset = x_offset; - - /* If parent was attached to child, separate them. - * https://github.com/harfbuzz/harfbuzz/issues/2469 - */ - if (unlikely (pos[parent].attach_chain() == -pos[child].attach_chain())) - { - pos[parent].attach_chain() = 0; - if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction))) - pos[parent].y_offset = 0; - else - pos[parent].x_offset = 0; - } - - if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ()) - { - c->buffer->message (c->font, - "cursive attached glyph at %u to glyph at %u", - i, j); - } - - overflow: - buffer->idx++; - return_trace (true); + return_trace (apply_cursive_attachment (c, + this+prev_record.exitAnchor, + this+this_record.entryAnchor, + skippy_iter.idx)); } template Date: Thu, 15 Oct 2026 11:59:38 -0600 Subject: [PATCH 3/9] [subset] Share mark-class remapping across record widths Mark classes remain 16-bit values in both mark-record representations; only the record stride and array count differ. Decode those in the wrapper and share the class collection and remapping work. Give the common helper hidden inline linkage so its body is shared across translation units as well as record formats. Class ordering, existing mappings, and allocation behavior are preserved. With GCC 15.3 on x86-64 without LTO, this removes 1152 bytes of subset code/read-only data at -O3 and 1440 bytes at -Os. Validated with the full Meson suite (292 passed, one skipped) and subset benchmarks. Assisted-by: OpenAI Codex --- src/OT/Layout/GPOS/MarkArray.hh | 38 ++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/OT/Layout/GPOS/MarkArray.hh b/src/OT/Layout/GPOS/MarkArray.hh index af840c806..fecb47f5a 100644 --- a/src/OT/Layout/GPOS/MarkArray.hh +++ b/src/OT/Layout/GPOS/MarkArray.hh @@ -147,21 +147,22 @@ struct MarkArray : Types::template ArrayOf> } }; -template HB_INTERNAL inline -void Markclass_closure_and_remap_indexes (const Coverage &mark_coverage, - const MarkArray &mark_array, - const hb_set_t &glyphset, - hb_map_t* klass_mapping /* INOUT */) +void Markclass_closure_and_remap_indexes (const Coverage &mark_coverage, + const void *mark_records, + unsigned count, + unsigned record_size, + const hb_set_t &glyphset, + hb_map_t *klass_mapping) { hb_set_t orig_classes; - - + hb_zip (mark_coverage, mark_array) - | hb_filter (glyphset, hb_first) - | hb_map (hb_second) - | hb_map (&MarkRecord::get_class) - | hb_sink (orig_classes) - ; + for (const auto _ : hb_zip (mark_coverage, hb_range (count))) + if (glyphset.has (_.first)) + { + const auto *klass = reinterpret_cast + ((const char *) mark_records + _.second * record_size); + orig_classes.add (*klass); + } unsigned idx = 0; for (auto klass : orig_classes.iter ()) @@ -172,6 +173,19 @@ void Markclass_closure_and_remap_indexes (const Coverage &mark_coverage, } } +template +HB_INTERNAL inline +void Markclass_closure_and_remap_indexes (const Coverage &mark_coverage, + const MarkArray &mark_array, + const hb_set_t &glyphset, + hb_map_t* klass_mapping /* INOUT */) +{ + Markclass_closure_and_remap_indexes (mark_coverage, + mark_array.arrayZ, mark_array.len, + MarkRecord::static_size, + glyphset, klass_mapping); +} + } } } From dafbe9c4bbd06a669a3f034fb8e96d3037f743af Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 15 Oct 2026 12:08:37 -0600 Subject: [PATCH 4/9] [subset] Share SinglePos serializers across source formats SinglePos output serializers only need the source value format and ValueBase pointer, but carried the full source lookup type through several template layers. The per-glyph mapper also had distinct lambda types for legacy and beyond-64k source tables. Pass the source format and base directly, and use a common mapper with the same reference captures as the original lambda. Output formats, device offsets, value copying, and allocation behavior are preserved. With GCC 15.3 on x86-64 without LTO, this removes 18080 bytes of subset code/read-only data at -O3 and 9472 bytes at -Os. Validated with the full Meson suite (292 passed, one skipped) and subset benchmarks. Assisted-by: OpenAI Codex --- src/OT/Layout/GPOS/Common.hh | 8 +++++-- src/OT/Layout/GPOS/SinglePos.hh | 19 ++++++++------- src/OT/Layout/GPOS/SinglePosFormat1.hh | 8 +++---- src/OT/Layout/GPOS/SinglePosFormat2.hh | 33 ++++++++++++++++++-------- 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/OT/Layout/GPOS/Common.hh b/src/OT/Layout/GPOS/Common.hh index 696d25d75..701b1896b 100644 --- a/src/OT/Layout/GPOS/Common.hh +++ b/src/OT/Layout/GPOS/Common.hh @@ -18,9 +18,13 @@ enum attach_type_t { #define attach_type() var.u8[2] /* attachment type */ /* Note! if attach_chain() is zero, the value of attach_type() is irrelevant. */ -template +struct ValueBase; +struct ValueFormat; + +template static void SinglePos_serialize (hb_serialize_context_t *c, - const SrcLookup *src, + const ValueBase *src, + ValueFormat src_format, Iterator it, const hb_hashmap_t> *layout_variation_idx_delta_map, unsigned new_format); diff --git a/src/OT/Layout/GPOS/SinglePos.hh b/src/OT/Layout/GPOS/SinglePos.hh index 31c2b4881..b500d6a3b 100644 --- a/src/OT/Layout/GPOS/SinglePos.hh +++ b/src/OT/Layout/GPOS/SinglePos.hh @@ -37,10 +37,10 @@ struct SinglePos } template void serialize (hb_serialize_context_t *c, - const SrcLookup* src, + const ValueBase* src, + ValueFormat src_format, Iterator glyph_val_iter_pairs, const hb_hashmap_t> *layout_variation_idx_delta_map, unsigned newFormat) @@ -63,26 +63,26 @@ struct SinglePos u.format.v = format; switch (u.format.v) { case 1: hb_barrier (); u.format1.serialize (c, - src, + src, src_format, glyph_val_iter_pairs, new_format, layout_variation_idx_delta_map); return; case 2: hb_barrier (); u.format2.serialize (c, - src, + src, src_format, glyph_val_iter_pairs, new_format, layout_variation_idx_delta_map); return; #ifndef HB_NO_BEYOND_64K case 3: u.format3.serialize (c, - src, + src, src_format, glyph_val_iter_pairs, new_format, layout_variation_idx_delta_map); return; case 4: u.format4.serialize (c, - src, + src, src_format, glyph_val_iter_pairs, new_format, layout_variation_idx_delta_map); @@ -110,14 +110,15 @@ struct SinglePos }; -template +template static void SinglePos_serialize (hb_serialize_context_t *c, - const SrcLookup *src, + const ValueBase *src, + ValueFormat src_format, Iterator it, const hb_hashmap_t> *layout_variation_idx_delta_map, unsigned new_format) -{ c->start_embed ()->serialize (c, src, it, layout_variation_idx_delta_map, new_format); } +{ c->start_embed ()->serialize (c, src, src_format, it, layout_variation_idx_delta_map, new_format); } } diff --git a/src/OT/Layout/GPOS/SinglePosFormat1.hh b/src/OT/Layout/GPOS/SinglePosFormat1.hh index 05d5e4d5f..9bae02dd4 100644 --- a/src/OT/Layout/GPOS/SinglePosFormat1.hh +++ b/src/OT/Layout/GPOS/SinglePosFormat1.hh @@ -110,10 +110,10 @@ struct SinglePosFormat1_3 : ValueBase } template void serialize (hb_serialize_context_t *c, - const SrcLookup *src, + const ValueBase *src, + ValueFormat src_format, Iterator it, ValueFormat newFormat, const hb_hashmap_t> *layout_variation_idx_delta_map) @@ -125,7 +125,7 @@ struct SinglePosFormat1_3 : ValueBase for (const hb_array_t& _ : + it | hb_map (hb_second)) { - src->get_value_format ().copy_values (c, newFormat, src, &_, layout_variation_idx_delta_map); + src_format.copy_values (c, newFormat, src, &_, layout_variation_idx_delta_map); // Only serialize the first entry in the iterator, the rest are assumed to // be the same. break; @@ -179,7 +179,7 @@ struct SinglePosFormat1_3 : ValueBase ; bool ret = bool (it); - SinglePos_serialize (c->serializer, this, it, &c->plan->layout_variation_idx_delta_map, new_format); + SinglePos_serialize (c->serializer, this, valueFormat, it, &c->plan->layout_variation_idx_delta_map, new_format); return_trace (ret); } }; diff --git a/src/OT/Layout/GPOS/SinglePosFormat2.hh b/src/OT/Layout/GPOS/SinglePosFormat2.hh index 5af4361d4..7cf00926b 100644 --- a/src/OT/Layout/GPOS/SinglePosFormat2.hh +++ b/src/OT/Layout/GPOS/SinglePosFormat2.hh @@ -7,6 +7,22 @@ namespace OT { namespace Layout { namespace GPOS_impl { +/* Keep the iterator type independent of the source table width so + * the output serializers can share their instantiations. */ +struct single_pos_value_mapper_t +{ + const hb_map_t &glyph_map; + const hb_array_t &values; + const unsigned &sub_length; + + hb_pair_t> + operator () (const hb_pair_t &_) const + { + return hb_pair (glyph_map[_.first], + values.sub_array (_.second * sub_length, sub_length)); + } +}; + template struct SinglePosFormat2_4 : ValueBase { @@ -118,10 +134,10 @@ struct SinglePosFormat2_4 : ValueBase template void serialize (hb_serialize_context_t *c, - const SrcLookup *src, + const ValueBase *src, + ValueFormat src_format, Iterator it, ValueFormat newFormat, const hb_hashmap_t> *layout_variation_idx_delta_map) @@ -134,7 +150,7 @@ struct SinglePosFormat2_4 : ValueBase + it | hb_map (hb_second) | hb_apply ([&] (hb_array_t _) - { src->get_value_format ().copy_values (c, newFormat, src, &_, layout_variation_idx_delta_map); }) + { src_format.copy_values (c, newFormat, src, &_, layout_variation_idx_delta_map); }) ; auto glyphs = @@ -188,12 +204,9 @@ struct SinglePosFormat2_4 : ValueBase auto it = + hb_zip (this+coverage, hb_range ((unsigned) valueCount)) | hb_filter (glyphset, hb_first) - | hb_map_retains_sorting ([&] (const hb_pair_t& _) - { - return hb_pair (glyph_map[_.first], - values_array.sub_array (_.second * sub_length, - sub_length)); - }) + | hb_map_retains_sorting (single_pos_value_mapper_t {glyph_map, + values_array, + sub_length}) ; unsigned new_format = compute_effective_format (c->plan->source, it, @@ -202,7 +215,7 @@ struct SinglePosFormat2_4 : ValueBase c->plan->has_gdef_varstore, &c->plan->layout_variation_idx_delta_map); bool ret = bool (it); - SinglePos_serialize (c->serializer, this, it, &c->plan->layout_variation_idx_delta_map, new_format); + SinglePos_serialize (c->serializer, this, valueFormat, it, &c->plan->layout_variation_idx_delta_map, new_format); return_trace (ret); } }; From e6827ea9da29adb9b286b4bcd1e1bb7cec6368c7 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 15 Oct 2026 12:13:07 -0600 Subject: [PATCH 5/9] [subset] Share delta substitution subset serialization SingleSubstFormat1_3 constructs equivalent remapping iterators in each source-format specialization, giving the output and coverage serializers distinct iterator types and duplicating their bodies. Move the subset preparation into a common helper taking the decoded coverage, delta, and glyph mask. Preserve the glyph intersection, modular delta calculation, remapping, and serializer failure behavior. With GCC 15.3 on x86-64 without LTO, this removes 11896 bytes of subset code/read-only data at -O3 and 5776 bytes at -Os. Validated with the full Meson suite (292 passed, one skipped) and subset benchmarks. Assisted-by: OpenAI Codex --- src/OT/Layout/GSUB/SingleSubstFormat1.hh | 50 +++++++++++++----------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/src/OT/Layout/GSUB/SingleSubstFormat1.hh b/src/OT/Layout/GSUB/SingleSubstFormat1.hh index 5cdbb2d68..14d93b809 100644 --- a/src/OT/Layout/GSUB/SingleSubstFormat1.hh +++ b/src/OT/Layout/GSUB/SingleSubstFormat1.hh @@ -7,6 +7,33 @@ namespace OT { namespace Layout { namespace GSUB_impl { +static inline bool +subset_single_subst (hb_subset_context_t *c, + const Coverage &coverage, + hb_codepoint_t d, + hb_codepoint_t mask) +{ + const hb_set_t &glyphset = *c->plan->glyphset_gsub (); + const hb_map_t &glyph_map = *c->plan->glyph_map; + + hb_set_t intersection; + coverage.intersect_set (glyphset, intersection); + + auto it = + + hb_iter (intersection) + | hb_map_retains_sorting ([d, mask] (hb_codepoint_t g) { + return hb_codepoint_pair_t (g, + (g + d) & mask); }) + | hb_filter (glyphset, hb_second) + | hb_map_retains_sorting ([&] (hb_codepoint_pair_t p) -> hb_codepoint_pair_t + { return hb_pair (glyph_map[p.first], glyph_map[p.second]); }) + ; + + bool ret = bool (it); + SingleSubst_serialize (c->serializer, it); + return ret; +} + template struct SingleSubstFormat1_3 { @@ -203,28 +230,7 @@ struct SingleSubstFormat1_3 bool subset (hb_subset_context_t *c) const { TRACE_SUBSET (this); - const hb_set_t &glyphset = *c->plan->glyphset_gsub (); - const hb_map_t &glyph_map = *c->plan->glyph_map; - - hb_codepoint_t d = deltaGlyphID; - hb_codepoint_t mask = get_mask (); - - hb_set_t intersection; - (this+coverage).intersect_set (glyphset, intersection); - - auto it = - + hb_iter (intersection) - | hb_map_retains_sorting ([d, mask] (hb_codepoint_t g) { - return hb_codepoint_pair_t (g, - (g + d) & mask); }) - | hb_filter (glyphset, hb_second) - | hb_map_retains_sorting ([&] (hb_codepoint_pair_t p) -> hb_codepoint_pair_t - { return hb_pair (glyph_map[p.first], glyph_map[p.second]); }) - ; - - bool ret = bool (it); - SingleSubst_serialize (c->serializer, it); - return_trace (ret); + return_trace (subset_single_subst (c, this+coverage, deltaGlyphID, get_mask ())); } }; From 3e831689a955b67dd73b0874e3c8cdaf626814f2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 15 Oct 2026 12:18:30 -0600 Subject: [PATCH 6/9] [ot] Share SinglePos optical-bound context setup SinglePos position_single duplicates the temporary apply context and value application for each source representation. Keep coverage and value-index checks in the wrappers and share the context setup through ValueFormat, passing the original value base explicitly. With GCC 15.3 on x86-64 without LTO, this removes 2088 bytes of core code/read-only data at -O3 and 616 bytes at -Os. Optical-bound benchmarks show no consistent slowdown across the four source formats. Validated with the full Meson suite (292 passed, one skipped) and synthetic optical-bound checks for all directions, 16/24-bit glyph IDs, missing glyphs, and device adjustments. Assisted-by: OpenAI Codex --- src/OT/Layout/GPOS/SinglePosFormat1.hh | 8 ++------ src/OT/Layout/GPOS/SinglePosFormat2.hh | 10 ++-------- src/OT/Layout/GPOS/ValueFormat.hh | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/OT/Layout/GPOS/SinglePosFormat1.hh b/src/OT/Layout/GPOS/SinglePosFormat1.hh index 9bae02dd4..daf276cdf 100644 --- a/src/OT/Layout/GPOS/SinglePosFormat1.hh +++ b/src/OT/Layout/GPOS/SinglePosFormat1.hh @@ -100,12 +100,8 @@ struct SinglePosFormat1_3 : ValueBase unsigned int index = (this+coverage).get_coverage (gid); if (likely (index == NOT_COVERED)) return false; - /* This is ugly... */ - hb_buffer_t buffer {}; - buffer.props.direction = direction; - OT::hb_ot_apply_context_t c (1, font, &buffer, table_blob); - - valueFormat.apply_value (&c, this, values, pos); + valueFormat.position_single (font, table_blob, direction, this, + values, pos); return true; } diff --git a/src/OT/Layout/GPOS/SinglePosFormat2.hh b/src/OT/Layout/GPOS/SinglePosFormat2.hh index 7cf00926b..c84ae5a47 100644 --- a/src/OT/Layout/GPOS/SinglePosFormat2.hh +++ b/src/OT/Layout/GPOS/SinglePosFormat2.hh @@ -121,14 +121,8 @@ struct SinglePosFormat2_4 : ValueBase if (likely (index == NOT_COVERED)) return false; if (unlikely (index >= valueCount)) return false; - /* This is ugly... */ - hb_buffer_t buffer {}; - buffer.props.direction = direction; - OT::hb_ot_apply_context_t c (1, font, &buffer, table_blob); - - valueFormat.apply_value (&c, this, - &values[index * valueFormat.get_len ()], - pos); + valueFormat.position_single (font, table_blob, direction, this, + &values[index * valueFormat.get_len ()], pos); return true; } diff --git a/src/OT/Layout/GPOS/ValueFormat.hh b/src/OT/Layout/GPOS/ValueFormat.hh index 08e55cbc6..1357afb49 100644 --- a/src/OT/Layout/GPOS/ValueFormat.hh +++ b/src/OT/Layout/GPOS/ValueFormat.hh @@ -157,6 +157,21 @@ struct ValueFormat : HBUINT16 return ret; } + void position_single (hb_font_t *font, + hb_blob_t *table_blob, + hb_direction_t direction, + const ValueBase *base, + const Value *values, + hb_glyph_position_t &pos) const + { + /* This is ugly... */ + hb_buffer_t buffer {}; + buffer.props.direction = direction; + hb_ot_apply_context_t c (1, font, &buffer, table_blob); + + apply_value (&c, base, values, pos); + } + unsigned int get_effective_format (const Value *values, bool strip_hints, bool strip_empty, const ValueBase *base, const hb_hashmap_t> *varidx_delta_map) const { From ffa5d3faf13e8a713834b33a857c266dacd58b04 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 15 Oct 2026 12:23:41 -0600 Subject: [PATCH 7/9] [subset] Share retained mark-anchor index collection Mark-to-base and mark-to-mark variation collection construct the same row-major anchor-index lists, but do so inside each table-width template. Pass the decoded row and class counts to a common collector. Preserve coverage filtering, class selection, index order, and the existing vector allocation behavior. Anchor access remains in the format-specific wrappers. With GCC 15.3 on x86-64 without LTO, this removes 2896 bytes of subset code/read-only data at -O3 and 2152 bytes at -Os. Validated with the full Meson suite (292 passed, one skipped) and variable-font subset benchmarks. Assisted-by: OpenAI Codex --- src/OT/Layout/GPOS/AnchorMatrix.hh | 15 +++++++++++++++ src/OT/Layout/GPOS/MarkBasePosFormat1.hh | 18 +++--------------- src/OT/Layout/GPOS/MarkMarkPosFormat1.hh | 18 +++--------------- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/src/OT/Layout/GPOS/AnchorMatrix.hh b/src/OT/Layout/GPOS/AnchorMatrix.hh index 939442046..7d213ba29 100644 --- a/src/OT/Layout/GPOS/AnchorMatrix.hh +++ b/src/OT/Layout/GPOS/AnchorMatrix.hh @@ -5,6 +5,21 @@ namespace OT { namespace Layout { namespace GPOS_impl { +HB_INTERNAL inline +void collect_anchor_indexes (const Coverage &coverage, + unsigned rows, + unsigned class_count, + const hb_set_t &glyphset, + const hb_map_t &klass_mapping, + hb_sorted_vector_t &indexes) +{ + for (const auto _ : hb_zip (coverage, hb_range (rows))) + if (glyphset.has (_.first)) + for (unsigned col = 0; col < class_count; col++) + if (klass_mapping.has (col)) + indexes.push (_.second * class_count + col); +} + template struct AnchorMatrix { diff --git a/src/OT/Layout/GPOS/MarkBasePosFormat1.hh b/src/OT/Layout/GPOS/MarkBasePosFormat1.hh index 9f8dc7354..0bf5b03f0 100644 --- a/src/OT/Layout/GPOS/MarkBasePosFormat1.hh +++ b/src/OT/Layout/GPOS/MarkBasePosFormat1.hh @@ -58,22 +58,10 @@ struct MarkBasePosFormat1_2 hb_map_t klass_mapping; Markclass_closure_and_remap_indexes (this+markCoverage, this+markArray, *c->glyph_set, &klass_mapping); - unsigned basecount = (this+baseArray).rows; - auto base_iter = - + hb_zip (this+baseCoverage, hb_range (basecount)) - | hb_filter (c->glyph_set, hb_first) - | hb_map (hb_second) - ; - hb_sorted_vector_t base_indexes; - for (const unsigned row : base_iter) - { - + hb_range ((unsigned) classCount) - | hb_filter (klass_mapping) - | hb_map ([&] (const unsigned col) { return row * (unsigned) classCount + col; }) - | hb_sink (base_indexes) - ; - } + collect_anchor_indexes (this+baseCoverage, (this+baseArray).rows, + classCount, *c->glyph_set, klass_mapping, + base_indexes); (this+baseArray).collect_variation_indices (c, base_indexes.iter ()); } diff --git a/src/OT/Layout/GPOS/MarkMarkPosFormat1.hh b/src/OT/Layout/GPOS/MarkMarkPosFormat1.hh index 9b546f3bb..4ef32f3c9 100644 --- a/src/OT/Layout/GPOS/MarkMarkPosFormat1.hh +++ b/src/OT/Layout/GPOS/MarkMarkPosFormat1.hh @@ -60,22 +60,10 @@ struct MarkMarkPosFormat1_2 hb_map_t klass_mapping; Markclass_closure_and_remap_indexes (this+mark1Coverage, this+mark1Array, *c->glyph_set, &klass_mapping); - unsigned mark2_count = (this+mark2Array).rows; - auto mark2_iter = - + hb_zip (this+mark2Coverage, hb_range (mark2_count)) - | hb_filter (c->glyph_set, hb_first) - | hb_map (hb_second) - ; - hb_sorted_vector_t mark2_indexes; - for (const unsigned row : mark2_iter) - { - + hb_range ((unsigned) classCount) - | hb_filter (klass_mapping) - | hb_map ([&] (const unsigned col) { return row * (unsigned) classCount + col; }) - | hb_sink (mark2_indexes) - ; - } + collect_anchor_indexes (this+mark2Coverage, (this+mark2Array).rows, + classCount, *c->glyph_set, klass_mapping, + mark2_indexes); (this+mark2Array).collect_variation_indices (c, mark2_indexes.iter ()); } From f2c944c7c93c86e4e96766c0c9c987cc01d918c2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 15 Oct 2026 12:27:50 -0600 Subject: [PATCH 8/9] [ot] Share delta substitution glyph traversal across widths Closure, dependency extraction, and glyph collection for delta substitutions only need a decoded coverage, delta, and glyph mask. Share those traversals instead of instantiating them for each source representation. Keep the closure population and overlapping-range guards, dependency bounds filtering, traversal order, and allocation behavior unchanged. With GCC 15.3 on x86-64 without LTO, this removes 1112 bytes of core and 952 bytes of subset code/read-only data at -O3, and 608/712 bytes at -Os. Validated with the full Meson suite (292 passed, one skipped), C++11 syntax checking, and subset benchmarks. Assisted-by: OpenAI Codex --- src/OT/Layout/GSUB/SingleSubstFormat1.hh | 113 +++++++++++++---------- 1 file changed, 65 insertions(+), 48 deletions(-) diff --git a/src/OT/Layout/GSUB/SingleSubstFormat1.hh b/src/OT/Layout/GSUB/SingleSubstFormat1.hh index 14d93b809..9683dc4f6 100644 --- a/src/OT/Layout/GSUB/SingleSubstFormat1.hh +++ b/src/OT/Layout/GSUB/SingleSubstFormat1.hh @@ -34,6 +34,68 @@ subset_single_subst (hb_subset_context_t *c, return ret; } +HB_INTERNAL inline +void single_subst_depend (hb_depend_context_t *c, + const Coverage &coverage, + hb_codepoint_t d, hb_codepoint_t mask) +{ + unsigned num_glyphs = c->face->get_num_glyphs (); + + // Filter coverage by active glyphs (like closure does) + hb_set_t intersection; + coverage.intersect_set (c->parent_active_glyphs (), intersection); + + + hb_iter (intersection) + | hb_map ([&] (hb_codepoint_t _) { return hb_codepoint_pair_t (_, (_ + d) & mask); }) + | hb_filter ([num_glyphs] (const hb_codepoint_pair_t &_) { return _.second < num_glyphs; }) + | hb_apply ([&] (const hb_codepoint_pair_t &_) { c->depend_data->add_gsub_lookup(_.first, c->lookup_index, _.second); }) + ; +} + +HB_INTERNAL inline +void single_subst_closure (hb_closure_context_t *c, + const Coverage &coverage, + hb_codepoint_t d, hb_codepoint_t mask) +{ + /* Help fuzzer avoid this function as much. */ + unsigned pop = coverage.get_population (); + if (pop >= mask) + return; + + hb_set_t intersection; + coverage.intersect_set (c->parent_active_glyphs (), intersection); + + /* In degenerate fuzzer-found fonts, but not real fonts, + * this table can keep adding new glyphs in each round of closure. + * Refuse to close-over, if it maps glyph range to overlapping range. */ + hb_codepoint_t min_before = intersection.get_min (); + hb_codepoint_t max_before = intersection.get_max (); + hb_codepoint_t min_after = (min_before + d) & mask; + hb_codepoint_t max_after = (max_before + d) & mask; + if (intersection.get_population () == max_before - min_before + 1 && + ((min_before <= min_after && min_after <= max_before) || + (min_before <= max_after && max_after <= max_before))) + return; + + + hb_iter (intersection) + | hb_map ([d, mask] (hb_codepoint_t g) { return (g + d) & mask; }) + | hb_sink (c->output) + ; +} + +HB_INTERNAL inline +void single_subst_collect_glyphs (hb_collect_glyphs_context_t *c, + const Coverage &coverage, + hb_codepoint_t d, hb_codepoint_t mask) +{ + if (unlikely (!coverage.collect_coverage (c->input))) return; + + + hb_iter (coverage) + | hb_map ([d, mask] (hb_codepoint_t g) { return (g + d) & mask; }) + | hb_sink (c->output) + ; +} + template struct SingleSubstFormat1_3 { @@ -74,64 +136,19 @@ struct SingleSubstFormat1_3 void depend (hb_depend_context_t *c) const { - hb_codepoint_t d = deltaGlyphID; - hb_codepoint_t mask = get_mask (); - unsigned num_glyphs = c->face->get_num_glyphs (); - - // Filter coverage by active glyphs (like closure does) - hb_set_t intersection; - (this+coverage).intersect_set (c->parent_active_glyphs (), intersection); - - + hb_iter (intersection) - | hb_map ([&] (hb_codepoint_t _) { return hb_codepoint_pair_t (_, (_ + d) & mask); }) - | hb_filter ([num_glyphs] (const hb_codepoint_pair_t &_) { return _.second < num_glyphs; }) - | hb_apply ([&] (const hb_codepoint_pair_t &_) { c->depend_data->add_gsub_lookup(_.first, c->lookup_index, _.second); }) - ; + single_subst_depend (c, this+coverage, deltaGlyphID, get_mask ()); } void closure (hb_closure_context_t *c) const { - hb_codepoint_t d = deltaGlyphID; - hb_codepoint_t mask = get_mask (); - - /* Help fuzzer avoid this function as much. */ - unsigned pop = (this+coverage).get_population (); - if (pop >= mask) - return; - - hb_set_t intersection; - (this+coverage).intersect_set (c->parent_active_glyphs (), intersection); - - /* In degenerate fuzzer-found fonts, but not real fonts, - * this table can keep adding new glyphs in each round of closure. - * Refuse to close-over, if it maps glyph range to overlapping range. */ - hb_codepoint_t min_before = intersection.get_min (); - hb_codepoint_t max_before = intersection.get_max (); - hb_codepoint_t min_after = (min_before + d) & mask; - hb_codepoint_t max_after = (max_before + d) & mask; - if (intersection.get_population () == max_before - min_before + 1 && - ((min_before <= min_after && min_after <= max_before) || - (min_before <= max_after && max_after <= max_before))) - return; - - + hb_iter (intersection) - | hb_map ([d, mask] (hb_codepoint_t g) { return (g + d) & mask; }) - | hb_sink (c->output) - ; + single_subst_closure (c, this+coverage, deltaGlyphID, get_mask ()); } void closure_lookups (hb_closure_lookups_context_t *c) const {} void collect_glyphs (hb_collect_glyphs_context_t *c) const { - if (unlikely (!(this+coverage).collect_coverage (c->input))) return; - hb_codepoint_t d = deltaGlyphID; - hb_codepoint_t mask = get_mask (); - - + hb_iter (this+coverage) - | hb_map ([d, mask] (hb_codepoint_t g) { return (g + d) & mask; }) - | hb_sink (c->output) - ; + single_subst_collect_glyphs (c, this+coverage, deltaGlyphID, get_mask ()); } const Coverage &get_coverage () const { return this+coverage; } From 570c92b993b10626c5b9af70d0d4a28bb9a7aba9 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 15 Oct 2026 12:32:45 -0600 Subject: [PATCH 9/9] [subset] Share pair-position variation-index collection Resolve the table-width-dependent PairPos fields before collecting variation indices. Share the class sets and matrix traversal between the small and beyond-64k formats while retaining the original value base for device offsets. Saves 3328 bytes at -O3 and 2312 bytes at -Os in the subset library with GCC 15.3 without LTO. The full test suite passes, including the HB_NO_BEYOND_64K build; variable-font subset timings remain close. Assisted-by: OpenAI Codex --- src/OT/Layout/GPOS/PairPosFormat2.hh | 109 ++++++++++++++++----------- 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/src/OT/Layout/GPOS/PairPosFormat2.hh b/src/OT/Layout/GPOS/PairPosFormat2.hh index eb7573dd2..fa07a2f45 100644 --- a/src/OT/Layout/GPOS/PairPosFormat2.hh +++ b/src/OT/Layout/GPOS/PairPosFormat2.hh @@ -7,6 +7,66 @@ namespace OT { namespace Layout { namespace GPOS_impl { +HB_INTERNAL inline +void collect_pair_variation_indices (hb_collect_variation_indices_context_t *c, + const ValueBase *base, + const Coverage &coverage, + const ClassDef &class_def1, + const ClassDef &class_def2, + const ValueFormat &valueFormat1, + const ValueFormat &valueFormat2, + unsigned class1_count, + unsigned class2_count, + const ValueRecord &values) +{ + if (!coverage.intersects (c->glyph_set) || + !class_def2.intersects (c->glyph_set)) return; + if ((!valueFormat1.has_device ()) && (!valueFormat2.has_device ())) return; + + hb_set_t klass1_glyphs, klass2_glyphs; + if (!class_def1.collect_coverage (&klass1_glyphs)) return; + if (!class_def2.collect_coverage (&klass2_glyphs)) return; + + hb_set_t class1_set, class2_set; + for (const unsigned cp : + c->glyph_set->iter () | hb_filter (coverage)) + { + if (!klass1_glyphs.has (cp)) class1_set.add (0); + else + { + unsigned klass1 = class_def1.get (cp); + class1_set.add (klass1); + } + } + + class2_set.add (0); + for (const unsigned cp : + c->glyph_set->iter () | hb_filter (klass2_glyphs)) + { + unsigned klass2 = class_def2.get (cp); + class2_set.add (klass2); + } + + if (class1_set.is_empty () + || class2_set.is_empty () + || (class2_set.get_population() == 1 && class2_set.has(0))) + return; + + unsigned len1 = valueFormat1.get_len (); + unsigned len2 = valueFormat2.get_len (); + const hb_array_t values_array = values.as_array ((unsigned)class1_count * (unsigned) class2_count * (len1 + len2)); + for (const unsigned class1_idx : class1_set.iter ()) + { + for (const unsigned class2_idx : class2_set.iter ()) + { + unsigned start_offset = (class1_idx * (unsigned) class2_count + class2_idx) * (len1 + len2); + if (valueFormat1.has_device ()) + valueFormat1.collect_variation_indices (c, base, values_array.sub_array (start_offset, len1)); + + if (valueFormat2.has_device ()) + valueFormat2.collect_variation_indices (c, base, values_array.sub_array (start_offset+len1, len2)); + } + } +} + template struct PairPosFormat2_4 : ValueBase { @@ -68,51 +128,10 @@ struct PairPosFormat2_4 : ValueBase void closure_lookups (hb_closure_lookups_context_t *c) const {} void collect_variation_indices (hb_collect_variation_indices_context_t *c) const { - if (!intersects (c->glyph_set)) return; - if ((!valueFormat1.has_device ()) && (!valueFormat2.has_device ())) return; - - hb_set_t klass1_glyphs, klass2_glyphs; - if (!(this+classDef1).collect_coverage (&klass1_glyphs)) return; - if (!(this+classDef2).collect_coverage (&klass2_glyphs)) return; - - hb_set_t class1_set, class2_set; - for (const unsigned cp : + c->glyph_set->iter () | hb_filter (this + coverage)) - { - if (!klass1_glyphs.has (cp)) class1_set.add (0); - else - { - unsigned klass1 = (this+classDef1).get (cp); - class1_set.add (klass1); - } - } - - class2_set.add (0); - for (const unsigned cp : + c->glyph_set->iter () | hb_filter (klass2_glyphs)) - { - unsigned klass2 = (this+classDef2).get (cp); - class2_set.add (klass2); - } - - if (class1_set.is_empty () - || class2_set.is_empty () - || (class2_set.get_population() == 1 && class2_set.has(0))) - return; - - unsigned len1 = valueFormat1.get_len (); - unsigned len2 = valueFormat2.get_len (); - const hb_array_t values_array = values.as_array ((unsigned)class1Count * (unsigned) class2Count * (len1 + len2)); - for (const unsigned class1_idx : class1_set.iter ()) - { - for (const unsigned class2_idx : class2_set.iter ()) - { - unsigned start_offset = (class1_idx * (unsigned) class2Count + class2_idx) * (len1 + len2); - if (valueFormat1.has_device ()) - valueFormat1.collect_variation_indices (c, this, values_array.sub_array (start_offset, len1)); - - if (valueFormat2.has_device ()) - valueFormat2.collect_variation_indices (c, this, values_array.sub_array (start_offset+len1, len2)); - } - } + collect_pair_variation_indices (c, this, this+coverage, + this+classDef1, this+classDef2, + valueFormat1, valueFormat2, + class1Count, class2Count, values); } void collect_glyphs (hb_collect_glyphs_context_t *c) const