From dae2c8bbbd492cdf97136affd2d4192f0327b437 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 14 Aug 2025 22:26:40 -0600 Subject: [PATCH] [ligature] Add a set-digest on the second component (#5464) --- src/OT/Layout/GSUB/Ligature.hh | 6 ++++++ src/OT/Layout/GSUB/LigatureSet.hh | 22 ++++++++++++++++------ src/OT/Layout/GSUB/LigatureSubstFormat1.hh | 11 ++++++++++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/OT/Layout/GSUB/Ligature.hh b/src/OT/Layout/GSUB/Ligature.hh index e0ec82a23..fe112ef38 100644 --- a/src/OT/Layout/GSUB/Ligature.hh +++ b/src/OT/Layout/GSUB/Ligature.hh @@ -44,6 +44,12 @@ struct Ligature c->output->add (ligGlyph); } + template + void collect_second (set_t &s) const + { + s.add (component[1]); // This adds codepoint 0 if component array is empty. + } + bool would_apply (hb_would_apply_context_t *c) const { if (c->len != component.lenP1) diff --git a/src/OT/Layout/GSUB/LigatureSet.hh b/src/OT/Layout/GSUB/LigatureSet.hh index 059ad20a0..9d933f16b 100644 --- a/src/OT/Layout/GSUB/LigatureSet.hh +++ b/src/OT/Layout/GSUB/LigatureSet.hh @@ -62,6 +62,15 @@ struct LigatureSet ; } + template + void collect_seconds (set_t &s) const + { + + hb_iter (ligature) + | hb_map (hb_add (this)) + | hb_apply ([&s] (const Ligature &_) { _.collect_second (s); }) + ; + } + bool would_apply (hb_would_apply_context_t *c) const { return @@ -72,7 +81,7 @@ struct LigatureSet ; } - bool apply (hb_ot_apply_context_t *c) const + bool apply (hb_ot_apply_context_t *c, const hb_set_digest_t *seconds = nullptr) const { TRACE_APPLY (this); @@ -91,7 +100,7 @@ struct LigatureSet return_trace (false); } - /* This version is optimized for speed by matching the first component + /* This version is optimized for speed by matching the second component * of the ligature here, instead of calling into the ligation code. * * This is replicated in ChainRuleSet and RuleSet. */ @@ -101,11 +110,11 @@ struct LigatureSet skippy_iter.set_match_func (match_always, nullptr); skippy_iter.set_glyph_data ((HBUINT16 *) nullptr); unsigned unsafe_to; - hb_codepoint_t first = (unsigned) -1; + hb_codepoint_t second = (unsigned) -1; bool matched = skippy_iter.next (&unsafe_to); if (likely (matched)) { - first = c->buffer->info[skippy_iter.idx].codepoint; + second = c->buffer->info[skippy_iter.idx].codepoint; unsafe_to = skippy_iter.idx + 1; if (skippy_iter.may_skip (c->buffer->info[skippy_iter.idx])) @@ -118,13 +127,14 @@ struct LigatureSet else goto slow; + if (seconds && !seconds->may_have (second)) + return_trace (false); bool unsafe_to_concat = false; - for (unsigned int i = 0; i < num_ligs; i++) { const auto &lig = this+ligature.arrayZ[i]; if (unlikely (lig.component.lenP1 <= 1) || - lig.component.arrayZ[0] == first) + lig.component.arrayZ[0] == second) { if (lig.apply (c)) { diff --git a/src/OT/Layout/GSUB/LigatureSubstFormat1.hh b/src/OT/Layout/GSUB/LigatureSubstFormat1.hh index 6003c8a54..a84cde1c9 100644 --- a/src/OT/Layout/GSUB/LigatureSubstFormat1.hh +++ b/src/OT/Layout/GSUB/LigatureSubstFormat1.hh @@ -81,6 +81,7 @@ struct LigatureSubstFormat1_2 struct external_cache_t { hb_ot_layout_mapping_cache_t coverage; + hb_set_digest_t seconds; }; void *external_cache_create () const { @@ -88,6 +89,12 @@ struct LigatureSubstFormat1_2 if (likely (cache)) { cache->coverage.clear (); + + cache->seconds.init (); + + hb_iter (ligatureSet) + | hb_map (hb_add (this)) + | hb_apply ([cache] (const LigatureSet &_) { _.collect_seconds (cache->seconds); }) + ; } return cache; } @@ -99,14 +106,16 @@ struct LigatureSubstFormat1_2 #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE external_cache_t *cache = (external_cache_t *) external_cache; + const hb_set_digest_t *seconds = cache ? &cache->seconds : nullptr; unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint, cache ? &cache->coverage : nullptr); #else + const hb_set_digest_t *seconds = nullptr; unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint); #endif if (index == NOT_COVERED) return_trace (false); const auto &lig_set = this+ligatureSet[index]; - return_trace (lig_set.apply (c)); + return_trace (lig_set.apply (c, seconds)); } bool serialize (hb_serialize_context_t *c,