From 95eefa38f6e14da4e1e2e4412fe0fe0107b93bf7 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 4 Feb 2025 19:16:57 +0000 Subject: [PATCH 1/7] [set] Add a brute-force intersects(other_set) --- src/hb-set.hh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/hb-set.hh b/src/hb-set.hh index f6013a414..e78099e34 100644 --- a/src/hb-set.hh +++ b/src/hb-set.hh @@ -120,6 +120,13 @@ struct hb_sparseset_t hb_sparseset_t& operator << (const hb_codepoint_pair_t& range) { add_range (range.first, range.second); return *this; } + bool intersects (const hb_set_t &other) const + { + hb_sparseset_t tmp = *this; + tmp.intersect (other); + return !tmp.is_empty (); + } + bool intersects (hb_codepoint_t first, hb_codepoint_t last) const { return s.intersects (first, last); } From 474bef1cfd8f8c839cfe7e6d1333bbf93057dde4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 4 Feb 2025 19:17:13 +0000 Subject: [PATCH 2/7] [aat] Use exact set for buffer intersection versus subchains --- src/hb-aat-layout-common.hh | 4 ++-- src/hb-aat-layout-morx-table.hh | 24 +++++++++++++----------- src/hb-buffer.hh | 8 +++++++- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/hb-aat-layout-common.hh b/src/hb-aat-layout-common.hh index 5db0d1212..4213ee280 100644 --- a/src/hb-aat-layout-common.hh +++ b/src/hb-aat-layout-common.hh @@ -63,10 +63,10 @@ struct hb_aat_apply_context_t : const ankr *ankr_table; const OT::GDEF *gdef_table; const hb_sorted_vector_t *range_flags = nullptr; - hb_set_digest_t buffer_digest = hb_set_digest_t::full (); + hb_set_t buffer_digest; const hb_set_t *left_set = nullptr; const hb_set_t *right_set = nullptr; - hb_set_digest_t machine_glyph_set = hb_set_digest_t::full (); + const hb_set_t *machine_glyph_set = nullptr; hb_aat_class_cache_t *machine_class_cache = nullptr; hb_mask_t subtable_flags = 0; diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index f5c986349..b2fd47d1f 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -172,7 +172,7 @@ struct RearrangementSubtable StateTableDriver driver (machine, c->face); if (driver.is_idempotent_on_all_out_of_bounds (&dc, c) && - !c->buffer_digest.intersects (c->machine_glyph_set)) + !c->buffer_digest.intersects (*c->machine_glyph_set)) { (void) c->buffer->message (c->font, "skipped chainsubtable because no glyph matches"); return_trace (false); @@ -340,7 +340,7 @@ struct ContextualSubtable StateTableDriver driver (machine, c->face); if (driver.is_idempotent_on_all_out_of_bounds (&dc, c) && - !c->buffer_digest.intersects (c->machine_glyph_set)) + !c->buffer_digest.intersects (*c->machine_glyph_set)) { (void) c->buffer->message (c->font, "skipped chainsubtable because no glyph matches"); return_trace (false); @@ -606,7 +606,7 @@ struct LigatureSubtable StateTableDriver driver (machine, c->face); if (driver.is_idempotent_on_all_out_of_bounds (&dc, c) && - !c->buffer_digest.intersects (c->machine_glyph_set)) + !c->buffer_digest.intersects (*c->machine_glyph_set)) { (void) c->buffer->message (c->font, "skipped chainsubtable because no glyph matches"); return_trace (false); @@ -885,7 +885,7 @@ struct InsertionSubtable StateTableDriver driver (machine, c->face); if (driver.is_idempotent_on_all_out_of_bounds (&dc, c) && - !c->buffer_digest.intersects (c->machine_glyph_set)) + !c->buffer_digest.intersects (*c->machine_glyph_set)) { (void) c->buffer->message (c->font, "skipped chainsubtable because no glyph matches"); return_trace (false); @@ -947,19 +947,21 @@ struct hb_accelerate_subtables_context_t : friend struct hb_aat_layout_lookup_accelerator_t; public: - hb_set_digest_t digest; + hb_set_t glyph_set; mutable hb_aat_class_cache_t class_cache; template auto init_ (const T &obj_, unsigned num_glyphs, hb_priority<1>) HB_AUTO_RETURN - ( - obj_.machine.collect_glyphs (this->digest, num_glyphs) - ) + (( + glyph_set.init (), + obj_.machine.collect_glyphs (glyph_set, num_glyphs) + )) template void init_ (const T &obj_, unsigned num_glyphs, hb_priority<0>) { - digest = digest.full (); + glyph_set.init (); + glyph_set.invert (); } template @@ -1185,7 +1187,7 @@ struct Chain hb_map ([subtable_flags] (const hb_aat_map_t::range_flags_t _) -> bool { return subtable_flags & (_.flags); }))) goto skip; c->subtable_flags = subtable_flags; - c->machine_glyph_set = accel ? accel->subtables[i].digest : hb_set_digest_t::full (); + c->machine_glyph_set = accel ? &accel->subtables[i].glyph_set : nullptr; c->machine_class_cache = accel ? &accel->subtables[i].class_cache : nullptr; if (!(subtable->get_coverage() & ChainSubtable::AllDirections) && @@ -1416,7 +1418,7 @@ struct mortmorx c->buffer->unsafe_to_concat (); - c->buffer_digest = c->buffer->digest (); + c->buffer_digest = c->buffer->set (); c->set_lookup_index (0); const Chain *chain = &firstChain; diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 2a6ad6128..4a4a9631a 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -32,6 +32,7 @@ #include "hb.hh" #include "hb-unicode.hh" +#include "hb-set.hh" #include "hb-set-digest.hh" @@ -194,7 +195,12 @@ struct hb_buffer_t hb_set_digest_t digest () const { hb_set_digest_t d; - d.init (); + d.add_array (&info[0].codepoint, len, sizeof (info[0])); + return d; + } + hb_set_t set () const + { + hb_set_t d; d.add_array (&info[0].codepoint, len, sizeof (info[0])); return d; } From 7862c7c5e16589eba0a1ac468c76102d55e36cef Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 4 Feb 2025 21:37:27 +0000 Subject: [PATCH 3/7] Rename a method again --- src/hb-aat-layout-morx-table.hh | 8 ++++---- src/hb-ot-layout.cc | 2 +- src/hb-set-digest.hh | 2 +- src/hb-set.hh | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index b2fd47d1f..65ed8e6be 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -172,7 +172,7 @@ struct RearrangementSubtable StateTableDriver driver (machine, c->face); if (driver.is_idempotent_on_all_out_of_bounds (&dc, c) && - !c->buffer_digest.intersects (*c->machine_glyph_set)) + !c->buffer_digest.may_intersect (*c->machine_glyph_set)) { (void) c->buffer->message (c->font, "skipped chainsubtable because no glyph matches"); return_trace (false); @@ -340,7 +340,7 @@ struct ContextualSubtable StateTableDriver driver (machine, c->face); if (driver.is_idempotent_on_all_out_of_bounds (&dc, c) && - !c->buffer_digest.intersects (*c->machine_glyph_set)) + !c->buffer_digest.may_intersect (*c->machine_glyph_set)) { (void) c->buffer->message (c->font, "skipped chainsubtable because no glyph matches"); return_trace (false); @@ -606,7 +606,7 @@ struct LigatureSubtable StateTableDriver driver (machine, c->face); if (driver.is_idempotent_on_all_out_of_bounds (&dc, c) && - !c->buffer_digest.intersects (*c->machine_glyph_set)) + !c->buffer_digest.may_intersect (*c->machine_glyph_set)) { (void) c->buffer->message (c->font, "skipped chainsubtable because no glyph matches"); return_trace (false); @@ -885,7 +885,7 @@ struct InsertionSubtable StateTableDriver driver (machine, c->face); if (driver.is_idempotent_on_all_out_of_bounds (&dc, c) && - !c->buffer_digest.intersects (*c->machine_glyph_set)) + !c->buffer_digest.may_intersect (*c->machine_glyph_set)) { (void) c->buffer->message (c->font, "skipped chainsubtable because no glyph matches"); return_trace (false); diff --git a/src/hb-ot-layout.cc b/src/hb-ot-layout.cc index 16f41d108..2ba09809a 100644 --- a/src/hb-ot-layout.cc +++ b/src/hb-ot-layout.cc @@ -2033,7 +2033,7 @@ inline void hb_ot_map_t::apply (const Proxy &proxy, * (plus some past glyphs). * * Only try applying the lookup if there is any overlap. */ - if (accel->digest.intersects (c.digest)) + if (accel->digest.may_intersect (c.digest)) { c.set_lookup_index (lookup_index); c.set_lookup_mask (lookup.mask, false); diff --git a/src/hb-set-digest.hh b/src/hb-set-digest.hh index 429af8092..c1e360b18 100644 --- a/src/hb-set-digest.hh +++ b/src/hb-set-digest.hh @@ -158,7 +158,7 @@ struct hb_set_digest_t return true; } - bool intersects (const hb_set_digest_t &o) const + bool may_intersect (const hb_set_digest_t &o) const { for (unsigned i = 0; i < n; i++) if (!(masks[i] & o.masks[i])) diff --git a/src/hb-set.hh b/src/hb-set.hh index e78099e34..8d0d9692b 100644 --- a/src/hb-set.hh +++ b/src/hb-set.hh @@ -120,7 +120,7 @@ struct hb_sparseset_t hb_sparseset_t& operator << (const hb_codepoint_pair_t& range) { add_range (range.first, range.second); return *this; } - bool intersects (const hb_set_t &other) const + bool may_intersect (const hb_set_t &other) const { hb_sparseset_t tmp = *this; tmp.intersect (other); From a617328005255f9fc2e41ebd743a5994bdb50451 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 4 Feb 2025 22:00:14 +0000 Subject: [PATCH 4/7] [bit-set] Sprinkle a few .arrayZ around All accesses are valid. --- src/hb-bit-set.hh | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/hb-bit-set.hh b/src/hb-bit-set.hh index 5f4c6f0af..032c5b5c4 100644 --- a/src/hb-bit-set.hh +++ b/src/hb-bit-set.hh @@ -297,9 +297,9 @@ struct hb_bit_set_t unsigned int write_index = 0; for (unsigned int i = 0; i < page_map.length; i++) { - int m = (int) page_map[i].major; + int m = (int) page_map.arrayZ[i].major; if (m < ds || de < m) - page_map[write_index++] = page_map[i]; + page_map.arrayZ[write_index++] = page_map.arrayZ[i]; } compact (compact_workspace, write_index); resize (write_index); @@ -389,7 +389,7 @@ struct hb_bit_set_t { if (page_at (a).is_empty ()) { a++; continue; } if (other.page_at (b).is_empty ()) { b++; continue; } - if (page_map[a].major != other.page_map[b].major || + if (page_map.arrayZ[a].major != other.page_map.arrayZ[b].major || !page_at (a).is_equal (other.page_at (b))) return false; a++; @@ -412,8 +412,8 @@ struct hb_bit_set_t uint32_t spi = 0; for (uint32_t lpi = 0; spi < page_map.length && lpi < larger_set.page_map.length; lpi++) { - uint32_t spm = page_map[spi].major; - uint32_t lpm = larger_set.page_map[lpi].major; + uint32_t spm = page_map.arrayZ[spi].major; + uint32_t lpm = larger_set.page_map.arrayZ[lpi].major; auto sp = page_at (spi); if (spm < lpm && !sp.is_empty ()) @@ -503,7 +503,7 @@ struct hb_bit_set_t for (; a < na && b < nb; ) { - if (page_map[a].major == other.page_map[b].major) + if (page_map.arrayZ[a].major == other.page_map.arrayZ[b].major) { if (!passthru_left) { @@ -512,7 +512,7 @@ struct hb_bit_set_t // passthru_left is set since no left side pages will be removed // in that case. if (write_index < a) - page_map[write_index] = page_map[a]; + page_map.arrayZ[write_index] = page_map.arrayZ[a]; write_index++; } @@ -520,7 +520,7 @@ struct hb_bit_set_t a++; b++; } - else if (page_map[a].major < other.page_map[b].major) + else if (page_map.arrayZ[a].major < other.page_map.arrayZ[b].major) { if (passthru_left) count++; @@ -765,8 +765,8 @@ struct hb_bit_set_t unsigned int initial_size = size; for (unsigned int i = start_page; i < page_map.length && size; i++) { - uint32_t base = major_start (page_map[i].major); - unsigned int n = pages[page_map[i].index].write (base, start_page_value, out, size); + uint32_t base = major_start (page_map.arrayZ[i].major); + unsigned int n = pages[page_map.arrayZ[i].index].write (base, start_page_value, out, size); out += n; size -= n; start_page_value = 0; @@ -814,8 +814,8 @@ struct hb_bit_set_t hb_codepoint_t next_value = codepoint + 1; for (unsigned int i=start_page; i= 0; i--) { - const auto& map = page_map[(unsigned) i]; - const auto& page = pages[map.index]; + const auto& map = page_map.arrayZ[(unsigned) i]; + const auto& page = pages.arrayZ[map.index]; if (!page.is_empty ()) return map.major * page_t::PAGE_BITS + page.get_max (); @@ -961,7 +961,7 @@ struct hb_bit_set_t return nullptr; last_page_lookup = i; - return &pages.arrayZ[page_map[i].index]; + return &pages.arrayZ[page_map.arrayZ[i].index]; } page_t &page_at (unsigned int i) { From 255dadc152a967219b447f02bfe95dd9d6bce07c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 4 Feb 2025 22:02:11 +0000 Subject: [PATCH 5/7] [set] Speed up may_intersect() --- src/hb-bit-page.hh | 7 +++++++ src/hb-bit-set-invertible.hh | 3 +++ src/hb-bit-set.hh | 20 ++++++++++++++++++++ src/hb-set.hh | 8 ++------ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/hb-bit-page.hh b/src/hb-bit-page.hh index 869c67895..df08c834e 100644 --- a/src/hb-bit-page.hh +++ b/src/hb-bit-page.hh @@ -227,6 +227,13 @@ struct hb_bit_page_t return false; return true; } + bool may_intersect (const hb_bit_page_t &other) const + { + for (unsigned i = 0; i < len (); i++) + if (v[i] & other.v[i]) + return true; + return false; + } bool operator <= (const hb_bit_page_t &larger_page) const { return is_subset (larger_page); } bool is_subset (const hb_bit_page_t &larger_page) const { diff --git a/src/hb-bit-set-invertible.hh b/src/hb-bit-set-invertible.hh index d5d1326d9..862b1a659 100644 --- a/src/hb-bit-set-invertible.hh +++ b/src/hb-bit-set-invertible.hh @@ -139,6 +139,9 @@ struct hb_bit_set_invertible_t hb_bit_set_invertible_t& operator << (const hb_codepoint_pair_t& range) { add_range (range.first, range.second); return *this; } + bool may_intersect (const hb_bit_set_invertible_t &other) const + { return inverted || other.inverted || s.may_intersect (other.s); } + bool intersects (hb_codepoint_t first, hb_codepoint_t last) const { hb_codepoint_t c = first - 1; diff --git a/src/hb-bit-set.hh b/src/hb-bit-set.hh index 032c5b5c4..c303e2bef 100644 --- a/src/hb-bit-set.hh +++ b/src/hb-bit-set.hh @@ -358,6 +358,26 @@ struct hb_bit_set_t hb_bit_set_t& operator << (const hb_codepoint_pair_t& range) { add_range (range.first, range.second); return *this; } + bool may_intersect (const hb_bit_set_t &other) const + { + unsigned int na = pages.length; + unsigned int nb = other.pages.length; + + unsigned int a = 0, b = 0; + for (; a < na && b < nb; ) + { + if (page_map.arrayZ[a].major == other.page_map.arrayZ[b].major && + page_at (a).may_intersect (other.page_at (b))) + return true; + + if (page_map.arrayZ[a].major < other.page_map.arrayZ[b].major) + a++; + else + b++; + } + return false; + } + bool intersects (hb_codepoint_t first, hb_codepoint_t last) const { hb_codepoint_t c = first - 1; diff --git a/src/hb-set.hh b/src/hb-set.hh index 8d0d9692b..8ea20bb71 100644 --- a/src/hb-set.hh +++ b/src/hb-set.hh @@ -120,12 +120,8 @@ struct hb_sparseset_t hb_sparseset_t& operator << (const hb_codepoint_pair_t& range) { add_range (range.first, range.second); return *this; } - bool may_intersect (const hb_set_t &other) const - { - hb_sparseset_t tmp = *this; - tmp.intersect (other); - return !tmp.is_empty (); - } + bool may_intersect (const hb_sparseset_t &other) const + { return s.may_intersect (other.s); } bool intersects (hb_codepoint_t first, hb_codepoint_t last) const { return s.intersects (first, last); } From 275fb565bb5b66f69426ff85c1b6a364d563abff Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 4 Feb 2025 22:07:48 +0000 Subject: [PATCH 6/7] [morx] Clean up memory --- src/hb-aat-layout-morx-table.hh | 1 + 1 file changed, 1 insertion(+) diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index 65ed8e6be..690d77bcd 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -974,6 +974,7 @@ struct hb_accelerate_subtables_context_t : void fini () { + glyph_set.fini (); } }; From 22a21e81a092904517a5d64d1fb8b30c42a918d0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 4 Feb 2025 22:21:52 +0000 Subject: [PATCH 7/7] [aat] Fix a crash --- src/hb-aat-layout-morx-table.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index 690d77bcd..127d46204 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -1188,7 +1188,7 @@ struct Chain hb_map ([subtable_flags] (const hb_aat_map_t::range_flags_t _) -> bool { return subtable_flags & (_.flags); }))) goto skip; c->subtable_flags = subtable_flags; - c->machine_glyph_set = accel ? &accel->subtables[i].glyph_set : nullptr; + c->machine_glyph_set = accel ? &accel->subtables[i].glyph_set : &Null(hb_set_t); c->machine_class_cache = accel ? &accel->subtables[i].class_cache : nullptr; if (!(subtable->get_coverage() & ChainSubtable::AllDirections) &&