diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index 1aad5394b..5537e7b67 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -872,6 +872,7 @@ hb_set_symmetric_difference hb_set_invert hb_set_is_inverted hb_set_is_equal +hb_set_intersects hb_set_is_subset hb_set_next hb_set_next_range diff --git a/src/OT/Layout/Common/CoverageFormat2.hh b/src/OT/Layout/Common/CoverageFormat2.hh index 19895cabb..02fd29b24 100644 --- a/src/OT/Layout/Common/CoverageFormat2.hh +++ b/src/OT/Layout/Common/CoverageFormat2.hh @@ -139,6 +139,19 @@ struct CoverageFormat2_4 return false; } + static void add_intersected_bits (hb_set_t &set, hb_codepoint_t base, uint64_t bits) + { set.add_bits (base, bits); } + + template + static void add_intersected_bits (IterableOut&& out, hb_codepoint_t base, uint64_t bits) + { + while (bits) + { + out << base + hb_ctz (bits); + bits &= bits - 1; + } + } + template void intersect_set (const hb_set_t &glyphs, IterableOut&& intersect_glyphs) const @@ -151,9 +164,18 @@ struct CoverageFormat2_4 if (unlikely (range.first < last)) break; last = range.last; - for (hb_codepoint_t g = range.first - 1; - glyphs.next (&g) && g <= last;) - intersect_glyphs << g; + hb_codepoint_t cursor = range.first & -hb_bit_page_t::ELT_BITS; + cursor = cursor ? cursor - 1 : HB_SET_VALUE_INVALID; + uint64_t bits; + while (glyphs.next_bits (&cursor, &bits) && cursor <= last) + { + if (range.first > cursor) + bits &= UINT64_MAX << (range.first - cursor); + unsigned int end = last - cursor; + if (end < hb_bit_page_t::ELT_BITS - 1) + bits &= (uint64_t (1) << (end + 1)) - 1; + add_intersected_bits (intersect_glyphs, cursor, bits); + } } } diff --git a/src/OT/Layout/GSUB/SubstLookup.hh b/src/OT/Layout/GSUB/SubstLookup.hh index d3b1d1081..ffa5e20bb 100644 --- a/src/OT/Layout/GSUB/SubstLookup.hh +++ b/src/OT/Layout/GSUB/SubstLookup.hh @@ -211,9 +211,9 @@ struct SubstLookup : Lookup return ret; } - static inline hb_depend_context_t::return_t depend_glyphs_recurse_func (hb_depend_context_t *c, unsigned lookup_index, hb_set_t *covered_seq_indices, unsigned seq_index, unsigned end_index); + static inline hb_depend_context_t::return_t depend_glyphs_recurse_func (hb_depend_context_t *c, unsigned lookup_index, hb_bit_page_t *covered_seq_indices, unsigned seq_index, unsigned end_index); - static inline hb_depend_context_t::return_t dispatch_depend_recurse_func (hb_depend_context_t *c, unsigned lookup_index, hb_set_t *covered_seq_indices, unsigned seq_index, unsigned end_index) + static inline hb_depend_context_t::return_t dispatch_depend_recurse_func (hb_depend_context_t *c, unsigned lookup_index, hb_bit_page_t *covered_seq_indices, unsigned seq_index, unsigned end_index) { return depend_glyphs_recurse_func (c, lookup_index, covered_seq_indices, seq_index, end_index); } diff --git a/src/hb-bit-page.hh b/src/hb-bit-page.hh index 16d5886a2..4087d6eec 100644 --- a/src/hb-bit-page.hh +++ b/src/hb-bit-page.hh @@ -135,7 +135,9 @@ struct hb_bit_page_t } void add (hb_codepoint_t g) { elt (g) |= mask (g); dirty (); } + void add_bits (hb_codepoint_t g, uint64_t bits) { elt (g) |= bits; dirty (); } void del (hb_codepoint_t g) { elt (g) &= ~mask (g); dirty (); } + void del_bits (hb_codepoint_t g, uint64_t bits) { elt (g) &= ~bits; dirty (); } void set (hb_codepoint_t g, bool value) { if (value) add (g); else del (g); } bool get (hb_codepoint_t g) const { return elt (g) & mask (g); } bool may_have (hb_codepoint_t g) const { return get (g); } diff --git a/src/hb-bit-set-invertible.hh b/src/hb-bit-set-invertible.hh index 48b2e2184..089c73c2c 100644 --- a/src/hb-bit-set-invertible.hh +++ b/src/hb-bit-set-invertible.hh @@ -101,9 +101,20 @@ struct hb_bit_set_invertible_t } unsigned int get_population () const { return inverted ? INVALID - s.get_population () : s.get_population (); } + bool get_singleton (hb_codepoint_t *codepoint) const + { + if (likely (!inverted)) + return s.get_singleton (codepoint); + if (s.get_population () != INVALID - 1) + return false; + *codepoint = get_min (); + return *codepoint != INVALID; + } void add (hb_codepoint_t g) { unlikely (inverted) ? s.del (g) : s.add (g); } + void add_bits (hb_codepoint_t g, uint64_t bits) + { unlikely (inverted) ? s.del_bits (g, bits) : s.add_bits (g, bits); } bool add_range (hb_codepoint_t a, hb_codepoint_t b) { return unlikely (inverted) ? ((void) s.del_range (a, b), true) : s.add_range (a, b); } @@ -143,6 +154,24 @@ struct hb_bit_set_invertible_t bool may_intersect (const hb_bit_set_invertible_t &other) const { return inverted || other.inverted || s.intersects (other.s); } + bool intersects (const hb_bit_set_invertible_t &other) const + { + if (likely (!inverted)) + return unlikely (other.inverted) ? !s.is_subset (other.s) + : s.intersects (other.s); + if (likely (!other.inverted)) + return !other.s.is_subset (s); + + unsigned population = s.get_population (); + unsigned other_population = other.s.get_population (); + if ((uint64_t) population + other_population < INVALID) + return true; + + return population >= other_population + ? !hb_all (iter (), other.s) + : !hb_all (other.iter (), s); + } + bool intersects (hb_codepoint_t first, hb_codepoint_t last) const { hb_codepoint_t c = first - 1; @@ -187,8 +216,10 @@ struct hb_bit_set_invertible_t protected: template - void process (const Op& op, const hb_bit_set_invertible_t &other) - { s.process (op, other.s); } + void process (const Op& op, + const hb_bit_set_invertible_t &other, + hb_vector_t *workspace = nullptr) + { s.process (op, other.s, workspace); } public: void union_ (const hb_bit_set_invertible_t &other) { @@ -209,21 +240,22 @@ struct hb_bit_set_invertible_t if (likely (s.successful)) inverted = inverted || other.inverted; } - void intersect (const hb_bit_set_invertible_t &other) + void intersect (const hb_bit_set_invertible_t &other, + hb_vector_t *workspace = nullptr) { if (likely (inverted == other.inverted)) { if (unlikely (inverted)) - process (hb_bitwise_or, other); + process (hb_bitwise_or, other, workspace); else - process (hb_bitwise_and, other); /* Main branch. */ + process (hb_bitwise_and, other, workspace); /* Main branch. */ } else { if (unlikely (inverted)) - process (hb_bitwise_lt, other); + process (hb_bitwise_lt, other, workspace); else - process (hb_bitwise_gt, other); + process (hb_bitwise_gt, other, workspace); } if (likely (s.successful)) inverted = inverted && other.inverted; @@ -280,6 +312,47 @@ struct hb_bit_set_invertible_t *codepoint = v + 1; return *codepoint != INVALID; } + bool next_bits (hb_codepoint_t *codepoint, uint64_t *bits) const + { + if (likely (!inverted)) + return s.next_bits (codepoint, bits); + + hb_codepoint_t base; + if (unlikely (*codepoint == INVALID)) + base = 0; + else + { + base = *codepoint & -hb_bit_page_t::ELT_BITS; + if (unlikely (base >= INVALID - hb_bit_page_t::ELT_BITS + 1)) + goto done; + base += hb_bit_page_t::ELT_BITS; + } + + while (true) + { + hb_codepoint_t cursor = base ? base - hb_bit_page_t::ELT_BITS : INVALID; + uint64_t excluded = 0; + if (!s.next_bits (&cursor, &excluded) || cursor != base) + excluded = 0; + *bits = ~excluded; + bool last = unlikely (base >= INVALID - hb_bit_page_t::ELT_BITS + 1); + if (last) + *bits &= UINT64_MAX >> 1; + if (*bits) + { + *codepoint = base; + return true; + } + if (unlikely (last)) + break; + base += hb_bit_page_t::ELT_BITS; + } + + done: + *codepoint = INVALID; + *bits = 0; + return false; + } bool previous (hb_codepoint_t *codepoint) const { if (likely (!inverted)) @@ -352,27 +425,50 @@ struct hb_bit_set_invertible_t /* * Iterator implementation. + * Mutating the set invalidates its iterators. */ struct iter_t : hb_iter_with_fallback_t { static constexpr bool is_sorted_iterator = true; static constexpr bool has_fast_len = true; iter_t (const hb_bit_set_invertible_t &s_ = Null (hb_bit_set_invertible_t), - bool init = true) : s (&s_), v (INVALID), l(0) + bool init = true) : s (&s_), v (INVALID), remaining (0), + have_len (!init), base (INVALID), bits (0) { if (init) - { - l = s->get_population () + 1; - __next__ (); - } + advance (); } typedef hb_codepoint_t __item_t__; hb_codepoint_t __item__ () const { return v; } bool __more__ () const { return v != INVALID; } - void __next__ () { s->next (&v); if (likely (l)) l--; } - void __prev__ () { s->previous (&v); l++; } - unsigned __len__ () const { return l; } + void __next__ () + { + remaining--; + advance (); + } + void __prev__ () + { + if (s->previous (&v)) + { + sync (); + remaining++; + } + else + { + base = INVALID; + bits = 0; + } + } + unsigned __len__ () const + { + if (unlikely (!have_len)) + { + remaining += s->get_population (); + have_len = true; + } + return remaining; + } iter_t end () const { return iter_t (*s, false); } bool operator != (const iter_t& o) const { return v != o.v; } @@ -380,7 +476,35 @@ struct hb_bit_set_invertible_t protected: const hb_bit_set_invertible_t *s; hb_codepoint_t v; - unsigned l; + mutable unsigned remaining; + mutable bool have_len; + hb_codepoint_t base; + uint64_t bits; + + void advance () + { + if (likely (bits)) + { + v = base + hb_ctz (bits); + bits &= bits - 1; + } + else if (s->next_bits (&base, &bits)) + { + v = base + hb_ctz (bits); + bits &= bits - 1; + } + else + v = INVALID; + } + + void sync () + { + base = v & -hb_bit_page_t::ELT_BITS; + hb_codepoint_t cursor = base ? base - hb_bit_page_t::ELT_BITS : INVALID; + s->next_bits (&cursor, &bits); + unsigned int bit = v & (hb_bit_page_t::ELT_BITS - 1); + bits = bit == hb_bit_page_t::ELT_BITS - 1 ? 0 : bits >> (bit + 1) << (bit + 1); + } }; iter_t iter () const { return iter_t (*this); } operator iter_t () const { return iter (); } diff --git a/src/hb-bit-set.hh b/src/hb-bit-set.hh index 7525e5a57..edf8add28 100644 --- a/src/hb-bit-set.hh +++ b/src/hb-bit-set.hh @@ -161,6 +161,14 @@ struct hb_bit_set_t page_t *page = page_for (g, true); if (unlikely (!page)) return; page->add (g); } + void add_bits (hb_codepoint_t g, uint64_t bits) + { + if (unlikely (!successful) || unlikely (!bits)) return; + assert (!(g & (page_t::ELT_BITS - 1))); + dirty (); + page_t *page = page_for (g, true); if (unlikely (!page)) return; + page->add_bits (g, bits); + } bool add_range (hb_codepoint_t a, hb_codepoint_t b) { if (unlikely (!successful)) return true; /* https://github.com/harfbuzz/harfbuzz/issues/657 */ @@ -291,6 +299,16 @@ struct hb_bit_set_t dirty (); page->del (g); } + void del_bits (hb_codepoint_t g, uint64_t bits) + { + if (unlikely (!successful) || unlikely (!bits)) return; + assert (!(g & (page_t::ELT_BITS - 1))); + page_t *page = page_for (g); + if (!page) + return; + dirty (); + page->del_bits (g, bits); + } private: void del_pages (int ds, int de) @@ -524,7 +542,8 @@ struct hb_bit_set_t void process_ (hb_bit_page_t::vector_t (*op) (const hb_bit_page_t::vector_t &, const hb_bit_page_t::vector_t &), bool passthru_left, bool passthru_right, - const hb_bit_set_t &other) + const hb_bit_set_t &other, + hb_vector_t *workspace = nullptr) { if (unlikely (!successful)) return; @@ -540,8 +559,18 @@ struct hb_bit_set_t // Pre-allocate the workspace that compact() will need so we can bail on allocation failure // before attempting to rewrite the page map. - hb_vector_t compact_workspace; - if (!passthru_left && unlikely (!allocate_compact_workspace (compact_workspace))) return; + hb_vector_t local_workspace; + hb_vector_t &compact_workspace = workspace ? *workspace : local_workspace; + if (!passthru_left) + { + bool allocated = workspace ? compact_workspace.resize (pages.length) + : allocate_compact_workspace (compact_workspace); + if (unlikely (!allocated)) + { + successful = false; + return; + } + } for (; a < na && b < nb; ) { @@ -651,13 +680,17 @@ struct hb_bit_set_t op_ (const hb_bit_page_t::vector_t &a, const hb_bit_page_t::vector_t &b) { return Op{} (a, b); } template - void process (const Op& op, const hb_bit_set_t &other) + void process (const Op& op, + const hb_bit_set_t &other, + hb_vector_t *workspace = nullptr) { - process_ (op_, op (1, 0), op (0, 1), other); + process_ (op_, op (1, 0), op (0, 1), other, workspace); } void union_ (const hb_bit_set_t &other) { process (hb_bitwise_or, other); } - void intersect (const hb_bit_set_t &other) { process (hb_bitwise_and, other); } + void intersect (const hb_bit_set_t &other, + hb_vector_t *workspace = nullptr) + { process (hb_bitwise_and, other, workspace); } void subtract (const hb_bit_set_t &other) { process (hb_bitwise_gt, other); } void symmetric_difference (const hb_bit_set_t &other) { process (hb_bitwise_xor, other); } @@ -708,6 +741,50 @@ struct hb_bit_set_t *codepoint = INVALID; return false; } + bool next_bits (hb_codepoint_t *codepoint, uint64_t *bits) const + { + static_assert (page_t::ELT_BITS == 64, ""); + + unsigned int i = 0; + unsigned int elt = 0; + if (likely (*codepoint != INVALID)) + { + hb_codepoint_t base = *codepoint & -page_t::ELT_BITS; + if (unlikely (base >= INVALID - page_t::ELT_BITS + 1)) + goto done; + base += page_t::ELT_BITS; + + unsigned int major = get_major (base); + i = last_page_lookup; + if (unlikely (i >= page_map.length || page_map.arrayZ[i].major != major)) + { + page_map.bfind (major, &i, HB_NOT_FOUND_STORE_CLOSEST); + if (i >= page_map.length) + goto done; + } + if (page_map.arrayZ[i].major == major) + elt = page_remainder (base) / page_t::ELT_BITS; + } + + for (; i < page_map.length; i++, elt = 0) + { + const page_map_t &map = page_map.arrayZ[i]; + const page_t &page = pages.arrayZ[map.index]; + for (; elt < page_t::len (); elt++) + if (page.v[elt]) + { + *codepoint = major_start (map.major) + elt * page_t::ELT_BITS; + *bits = page.v[elt]; + last_page_lookup = i; + return true; + } + } + + done: + *codepoint = INVALID; + *bits = 0; + return false; + } bool previous (hb_codepoint_t *codepoint) const { if (unlikely (*codepoint == INVALID)) { @@ -883,6 +960,39 @@ struct hb_bit_set_t population = pop; return pop; } + bool get_singleton (hb_codepoint_t *codepoint) const + { + if (has_population ()) + { + if (population != 1) return false; + *codepoint = get_min (); + return true; + } + + hb_codepoint_t singleton = INVALID; + for (const auto &map : page_map) + { + const auto &page = pages.arrayZ[map.index]; + for (unsigned i = 0; i < page_t::len (); i++) + { + page_t::elt_t bits = page.v[i]; + if (!bits) continue; + if (singleton != INVALID || (bits & (bits - 1))) + return false; + singleton = map.major * page_t::PAGE_BITS + + i * page_t::ELT_BITS + hb_ctz (bits); + } + } + + if (singleton == INVALID) + { + population = 0; + return false; + } + population = 1; + *codepoint = singleton; + return true; + } hb_codepoint_t get_min () const { unsigned count = pages.length; diff --git a/src/hb-depend-data.hh b/src/hb-depend-data.hh index 468843018..939e5c73b 100644 --- a/src/hb-depend-data.hh +++ b/src/hb-depend-data.hh @@ -273,8 +273,9 @@ struct hb_depend_data_t * construction is complete, leaving only hb_depend_data_t. * * Temporary state (freed on destruction): - * - lookup_features: Lookup index to feature tag mapping - * - seen_edges: Struct-based edge deduplication table + * - lookup_features: Packed lookup index to feature tag mapping + * - lookup_feature_offsets: Per-lookup offsets into lookup_features + * - edge_slots: Compact edge deduplication table * - set_to_index: Content-based dependency set deduplication map * - free_set_list: Indices of freed sets available for reuse * - current_context_set_index: Context requirements for current rule @@ -455,6 +456,89 @@ struct hb_depend_data_builder_t return find_or_create_context_set (context_elements); } + static uint64_t encode_edge_ref (hb_codepoint_t source, unsigned int index) + { return (uint64_t (source) + 1) << 32 | index; } + + static void decode_edge_ref (uint64_t ref, + hb_codepoint_t *source, + unsigned int *index) + { + *source = (ref >> 32) - 1; + *index = ref; + } + + uint32_t edge_hash (hb_codepoint_t source, const hb_depend_edge_t &record) const + { + hb_depend_edge_key_t key (source, record.table_tag, record.layout_tag, + record.dependent, record.ligature_set, + record.context_set); + return key.hash (); + } + + bool resize_edge_slots () + { + unsigned int new_size = edge_slots.length ? edge_slots.length * 2 : 8; + if (unlikely (new_size < edge_slots.length)) + return fail (); + + hb_vector_t new_slots; + if (unlikely (!new_slots.resize_exact (new_size))) + return fail (); + + for (uint64_t ref : edge_slots) + { + if (!ref) + continue; + + hb_codepoint_t source; + unsigned int index; + decode_edge_ref (ref, &source, &index); + const hb_depend_edge_t &record = data.glyph_dependencies[source].dependencies[index]; + unsigned int slot = edge_hash (source, record) & (new_size - 1); + unsigned int step = 0; + while (new_slots[slot]) + slot = (slot + ++step) & (new_size - 1); + new_slots[slot] = ref; + } + + edge_slots = std::move (new_slots); + return true; + } + + bool add_edge (hb_codepoint_t source, const hb_depend_edge_t &record) + { + /* Store only a source and per-glyph edge index in each bucket. The edge + * itself already lives in the output vector, where it can be compared to + * resolve hash collisions exactly. Adding one to the encoded source + * reserves zero as the empty-bucket value. */ + if (unlikely (!edge_slots.length || + uint64_t (edge_population) * 3 / 2 >= edge_slots.length - 1)) + if (unlikely (!resize_edge_slots ())) + return false; + + unsigned int slot = edge_hash (source, record) & (edge_slots.length - 1); + unsigned int step = 0; + while (uint64_t ref = edge_slots[slot]) + { + hb_codepoint_t existing_source; + unsigned int existing_index; + decode_edge_ref (ref, &existing_source, &existing_index); + if (existing_source == source && + data.glyph_dependencies[source].dependencies[existing_index] == record) + return false; + slot = (slot + ++step) & (edge_slots.length - 1); + } + + auto &dependencies = data.glyph_dependencies[source].dependencies; + unsigned int index = dependencies.length; + if (unlikely (!dependencies.push_or_fail (record))) + return fail (); + + edge_slots[slot] = encode_edge_ref (source, index); + edge_population++; + return true; + } + bool add_depend_layout (hb_codepoint_t target, hb_tag_t table_tag, hb_tag_t layout_tag, hb_codepoint_t dependent, @@ -468,18 +552,8 @@ struct hb_depend_data_builder_t return false; } - hb_depend_edge_key_t key (target, table_tag, layout_tag, dependent, lig_set, context_set); - if (seen_edges.has (key)) - return false; - - if (unlikely (!seen_edges.set (key, true))) - return fail (); - - auto &gdr = data.glyph_dependencies[target]; - if (unlikely (!gdr.dependencies.push_or_fail (table_tag, dependent, layout_tag, - lig_set, context_set, flags))) - return fail (); - return true; + return add_edge (target, hb_depend_edge_t (table_tag, dependent, layout_tag, + lig_set, context_set, flags)); } bool add_gsub_lookup (hb_codepoint_t target, hb_codepoint_t lookup_index, @@ -492,13 +566,68 @@ struct hb_depend_data_builder_t hb_subset_depend_edge_flags_t flags = current_edge_flags; bool any_added = false; - for (auto t : lookup_features[lookup_index]) { + for (uint64_t entry : get_lookup_features (lookup_index)) { + hb_tag_t t = (hb_tag_t) entry; if (add_depend_layout (target, HB_OT_TAG_GSUB, t, dependent, lig_set, context_set, flags)) any_added = true; } return any_added; } + bool init_lookup_features (unsigned lookup_count) + { + return check_success (lookup_feature_offsets.resize (lookup_count + 1)); + } + + bool add_lookup_feature (hb_codepoint_t lookup_index, hb_tag_t feature_tag) + { + if (feature_tag == HB_SET_VALUE_INVALID) + return true; + + if (unlikely (!lookup_feature_offsets.length || + lookup_index >= lookup_feature_offsets.length - 1)) + return fail (); + + uint64_t entry = ((uint64_t) lookup_index << 32) | feature_tag; + return check_success (lookup_features.push_or_fail (entry)); + } + + bool finish_lookup_features () + { + lookup_features.qsort ([] (uint64_t a, uint64_t b) { + return a < b ? -1 : a > b ? 1 : 0; + }); + + unsigned write = 0; + for (uint64_t entry : lookup_features) + if (!write || entry != lookup_features[write - 1]) + lookup_features[write++] = entry; + lookup_features.shrink (write, false); + + unsigned feature_index = 0; + unsigned lookup_count = lookup_feature_offsets.length - 1; + for (unsigned lookup_index = 0; lookup_index < lookup_count; lookup_index++) + { + lookup_feature_offsets[lookup_index] = feature_index; + while (feature_index < lookup_features.length && + lookup_features[feature_index] >> 32 == lookup_index) + feature_index++; + } + lookup_feature_offsets[lookup_count] = feature_index; + return true; + } + + hb_array_t get_lookup_features (hb_codepoint_t lookup_index) const + { + if (unlikely (!lookup_feature_offsets.length || + lookup_index >= lookup_feature_offsets.length - 1)) + return {}; + + unsigned start = lookup_feature_offsets[lookup_index]; + unsigned end = lookup_feature_offsets[lookup_index + 1]; + return lookup_features.as_array ().sub_array (start, end - start); + } + void add_depend (hb_codepoint_t target, hb_tag_t table_tag, hb_codepoint_t dependent, hb_codepoint_t lig_set = HB_CODEPOINT_INVALID, @@ -523,8 +652,10 @@ struct hb_depend_data_builder_t bool successful = true; hb_set_t unicodes; hb_map_t nominal_glyphs; - hb_vector_t lookup_features; - hb_hashmap_t seen_edges; + hb_vector_t lookup_features; + hb_vector_t lookup_feature_offsets; + hb_vector_t edge_slots; + unsigned int edge_population = 0; hb_hashmap_t set_to_index; hb_vector_t free_set_list; hb_codepoint_t current_context_set_index; diff --git a/src/hb-ot-layout-gsub-table.hh b/src/hb-ot-layout-gsub-table.hh index 3a0e2b2b5..cc603e878 100644 --- a/src/hb-ot-layout-gsub-table.hh +++ b/src/hb-ot-layout-gsub-table.hh @@ -58,15 +58,22 @@ template return l.dispatch (c); } -/*static*/ hb_depend_context_t::return_t SubstLookup::depend_glyphs_recurse_func (hb_depend_context_t *c, unsigned lookup_index, hb_set_t *covered_seq_indices, unsigned seq_index, unsigned end_index) +/*static*/ hb_depend_context_t::return_t SubstLookup::depend_glyphs_recurse_func (hb_depend_context_t *c, unsigned lookup_index, hb_bit_page_t *covered_seq_indices, unsigned seq_index, unsigned end_index) { const SubstLookup &l = c->face->table.GSUB.get_relaxed ()->table->get_lookup (lookup_index); /* After a non-1-to-1 lookup (expansion/contraction), subsequent lookups in the same * contextual rule see all glyphs, not position-specific ones. This matches closure behavior. * Mark sequence positions as covered so later lookups use the full glyph set. */ if (l.may_have_non_1to1 ()) - hb_set_add_range (covered_seq_indices, seq_index, end_index); - return l.dispatch (c); + covered_seq_indices->add_range (seq_index, end_index); + + hb_depend_context_t::recurse_key_t key; + if (!c->get_recurse_key (lookup_index, &key)) + return hb_empty_t (); + + hb_depend_context_t::return_t ret = l.dispatch (c); + c->finish_recurse (key); + return ret; } template <> @@ -121,7 +128,7 @@ GSUB_accelerator_t::depend (hb_depend_data_builder_t *builder, hb_face_t *face) return; this->table->get_feature_tags (0, &num_features, feature_tags.arrayZ); - if (!builder->check_success (builder->lookup_features.resize (num_lookups))) + if (!builder->init_lookup_features (num_lookups)) return; hb_vector_t feature_query_v; @@ -144,7 +151,8 @@ GSUB_accelerator_t::depend (hb_depend_data_builder_t *builder, hb_face_t *face) this->table->get_feature (feature_index).add_lookup_indexes_to (&lookup_indexes); for (auto lookup_index : lookup_indexes) - builder->lookup_features[lookup_index].add (ft); + if (unlikely (!builder->add_lookup_feature (lookup_index, ft))) + return; auto &fv = this->table->get_feature_variations (); auto fi_count = fv.record_count (); @@ -158,29 +166,32 @@ GSUB_accelerator_t::depend (hb_depend_data_builder_t *builder, hb_face_t *face) feature_ptr->add_lookup_indexes_to (&lookup_indexes); } for (auto lookup_index : lookup_indexes) - if (!builder->lookup_features[lookup_index].has (ft)) - builder->lookup_features[lookup_index].add (ft); + if (unlikely (!builder->add_lookup_feature (lookup_index, ft))) + return; } } + if (unlikely (!builder->finish_lookup_features ())) + return; + hb_set_t all_glyphs; all_glyphs.add_range (0, face->get_num_glyphs () - 1); hb_depend_context_t c (builder, face, &all_glyphs); - int i = -1; - for (auto &feature_set : builder->lookup_features) + for (unsigned i = 0; i < num_lookups; i++) { - i++; - if (feature_set.is_empty ()) + auto features = builder->get_lookup_features (i); + if (!features) { DEBUG_MSG_LEVEL (DEPEND, nullptr, 1, 0, - "Skipping lookup %d (no features)", i); + "Skipping lookup %u (no features)", i); continue; } DEBUG_MSG_LEVEL (DEPEND, nullptr, 1, 0, - "Processing lookup %d with features:", i); + "Processing lookup %u with features:", i); c.lookup_index = i; + c.reset_recurse_cache (); c.lookups_seen.clear (); c.lookups_seen.add (i); /* Seed for A→B→A cycle detection in recurse(). */ this->table->get_lookup (i).depend (&c); diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index f5ec1dd09..85c81573b 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -382,13 +382,119 @@ struct hb_collect_glyphs_context_t : struct hb_depend_context_t : hb_dispatch_context_t { - typedef return_t (*recurse_func_t) (hb_depend_context_t *c, unsigned lookup_index, hb_set_t *covered_seq_indicies, unsigned seq_index, unsigned end_index); + enum glyph_set_type_t + { + GLYPH_SET_CLASS, + GLYPH_SET_COVERAGE, + GLYPH_SET_SINGLE + }; + + static constexpr hb_codepoint_t SINGLE_GLYPH_ACTIVE_FLAG = 0x80000000u; + + struct glyph_set_cache_key_t + { + glyph_set_cache_key_t () = default; + glyph_set_cache_key_t (const void *data_, unsigned value_, glyph_set_type_t type_) + : data (data_), value (value_), type (type_) {} + + bool operator == (const glyph_set_cache_key_t &o) const + { return data == o.data && value == o.value && type == o.type; } + + uint32_t hash () const + { + uint32_t current = hb_hash ((uintptr_t) data); + current = current * 31 + hb_hash (value); + return current * 31 + hb_hash ((unsigned) type); + } + + const void *data = nullptr; + unsigned value = 0; + glyph_set_type_t type = GLYPH_SET_CLASS; + }; + + struct context_set_cache_key_t + { + context_set_cache_key_t () = default; + context_set_cache_key_t (const void *rule_, + const void *record_, + const void *data_, + hb_codepoint_t active_glyphs_, + unsigned value_, + unsigned format_) + : rule (rule_), record (record_), data (data_), active_glyphs (active_glyphs_), + value (value_), format (format_) {} + + bool operator == (const context_set_cache_key_t &o) const + { + return rule == o.rule && + record == o.record && + data == o.data && + active_glyphs == o.active_glyphs && + value == o.value && + format == o.format; + } + + uint32_t hash () const + { + uint32_t current = hb_hash ((uintptr_t) rule); + current = current * 31 + hb_hash ((uintptr_t) record); + current = current * 31 + hb_hash ((uintptr_t) data); + current = current * 31 + hb_hash (active_glyphs); + current = current * 31 + hb_hash (value); + return current * 31 + hb_hash (format); + } + + const void *rule = nullptr; + const void *record = nullptr; + const void *data = nullptr; + hb_codepoint_t active_glyphs = HB_CODEPOINT_INVALID; + unsigned value = 0; + unsigned format = 0; + }; + + struct recurse_key_t + { + recurse_key_t () = default; + recurse_key_t (unsigned lookup_index_, + hb_codepoint_t active_glyphs_, + hb_codepoint_t recurse_path_, + hb_codepoint_t context_set_) + : lookup_index (lookup_index_), + active_glyphs (active_glyphs_), + recurse_path (recurse_path_), + context_set (context_set_) {} + + bool operator == (const recurse_key_t &o) const + { + return lookup_index == o.lookup_index && + active_glyphs == o.active_glyphs && + recurse_path == o.recurse_path && + context_set == o.context_set; + } + + uint32_t hash () const + { + uint32_t current = 0x84222325; + current = (current ^ hb_hash (lookup_index)) * 16777619; + current = (current ^ hb_hash (active_glyphs)) * 16777619; + current = (current ^ hb_hash (recurse_path)) * 16777619; + current = (current ^ hb_hash (context_set)) * 16777619; + return current; + } + + unsigned lookup_index = 0; + hb_codepoint_t active_glyphs = 0; + hb_codepoint_t recurse_path = 0; + hb_codepoint_t context_set = HB_CODEPOINT_INVALID; + }; + + typedef return_t (*recurse_func_t) (hb_depend_context_t *c, unsigned lookup_index, hb_bit_page_t *covered_seq_indices, unsigned seq_index, unsigned end_index); /* return_t is hb_empty_t — dispatch is purely for side-effects, like hb_closure_context_t. */ template return_t dispatch (const T &obj) { obj.depend (this); return hb_empty_t (); } static return_t default_return_value () { return hb_empty_t (); } - void recurse (unsigned lookup_idx, hb_set_t *covered_seq_indicies, unsigned seq_index, unsigned end_index) + void recurse (unsigned lookup_idx, hb_bit_page_t *covered_seq_indices, unsigned seq_index, unsigned end_index) { /* Cycle detection using call-stack semantics: block re-entry of a * lookup that is currently on the execution stack, but allow it again @@ -409,64 +515,261 @@ struct hb_depend_context_t : * that indirect cycles of the form A→B→A are also caught here. */ if (lookups_seen.has (lookup_idx)) return; lookups_seen.add (lookup_idx); - recurse_func (this, lookup_idx, covered_seq_indicies, seq_index, end_index); + + /* Intern the ordered recursion path instead of copying and interning the + * lookups_seen set. Path identity is more specific than set identity but + * never merges states with different cycle-detection behavior. */ + hb_codepoint_t parent_path = recurse_path; + uint64_t path_key = (uint64_t) parent_path << 32 | lookup_idx; + hb_codepoint_t *path = nullptr; + hb_codepoint_t path_index; + if (!recurse_path_to_index.has (path_key, &path)) + { + path_index = recurse_path_to_index.get_population () + 1; + if (unlikely (!recurse_path_to_index.set (path_key, path_index))) + { + depend_data->fail (); + lookups_seen.del (lookup_idx); + return; + } + } + else + path_index = *path; + recurse_path = path_index; + recurse_func (this, lookup_idx, covered_seq_indices, seq_index, end_index); + recurse_path = parent_path; lookups_seen.del (lookup_idx); } + struct active_glyphs_frame_t + { + hb_set_t glyphs; + hb_codepoint_t active_idx = HB_CODEPOINT_INVALID; + bool use_root = false; + }; + const hb_set_t& parent_active_glyphs () { - if (!active_glyphs_stack) + if (!active_glyphs_stack_depth) return *glyphs; - return active_glyphs_stack.tail (); + const active_glyphs_frame_t &frame = active_glyphs_stack[active_glyphs_stack_depth - 1]; + return frame.use_root ? *glyphs : frame.glyphs; } - hb_set_t* push_cur_active_glyphs () + hb_set_t* push_cur_active_glyphs (hb_codepoint_t active_idx = HB_CODEPOINT_INVALID) { - if (unlikely (!active_glyphs_stack.push_or_fail ())) + if (active_glyphs_stack_depth == active_glyphs_stack.length && + unlikely (!active_glyphs_stack.push_or_fail ())) return nullptr; - return &active_glyphs_stack.tail (); + + active_glyphs_frame_t &frame = active_glyphs_stack[active_glyphs_stack_depth++]; + frame.glyphs.clear (); + frame.active_idx = active_idx; + frame.use_root = false; + return &frame.glyphs; + } + + bool push_root_active_glyphs () + { + if (active_glyphs_stack_depth == active_glyphs_stack.length && + unlikely (!active_glyphs_stack.push_or_fail ())) + return false; + + active_glyphs_stack[active_glyphs_stack_depth++].use_root = true; + return true; } bool pop_cur_done_glyphs () { - if (!active_glyphs_stack) + if (!active_glyphs_stack_depth) return false; - active_glyphs_stack.pop (); + active_glyphs_stack_depth--; return true; } - /* Context information for recording contextual dependencies */ - struct context_info_t { - hb_vector_t backtrack_sets; /* Sets of glyphs in backtrack positions */ - hb_vector_t lookahead_sets; /* Sets of glyphs in lookahead positions */ + bool get_parent_active_glyphs_index (hb_codepoint_t *active_idx) + { + if (!active_glyphs_stack_depth || + active_glyphs_stack[active_glyphs_stack_depth - 1].use_root) + { + *active_idx = HB_CODEPOINT_INVALID; + return true; + } + + active_glyphs_frame_t &frame = active_glyphs_stack[active_glyphs_stack_depth - 1]; + *active_idx = frame.active_idx; + if (*active_idx != HB_CODEPOINT_INVALID) + return true; + + hb_codepoint_t glyph; + if (frame.glyphs.get_singleton (&glyph) && + likely (glyph < SINGLE_GLYPH_ACTIVE_FLAG)) + *active_idx = glyph | SINGLE_GLYPH_ACTIVE_FLAG; + else if (!find_or_create_recurse_set (frame.glyphs, active_idx)) + return false; + + frame.active_idx = *active_idx; + return true; + } + + bool get_context_set_cache_key (const void *rule, + const void *record, + const void *data, + unsigned value, + unsigned format, + context_set_cache_key_t *key) + { + hb_codepoint_t active_idx; + if (!get_parent_active_glyphs_index (&active_idx)) + return false; + *key = context_set_cache_key_t (rule, record, data, active_idx, value, format); + return true; + } + + bool get_recurse_key (unsigned lookup_idx, recurse_key_t *key) + { + /* The active glyphs and in-progress lookup set are part of the state. + * Omitting the latter would make memoization incorrect for cyclic lookup + * graphs whose available recursive paths depend on their ancestry. + * Edge flags are not part of the state: they affect neither traversal nor + * edge identity, so revisiting with different flags can only find edges + * that the deduplication table already contains. */ + hb_codepoint_t active_idx; + if (!get_parent_active_glyphs_index (&active_idx)) + return false; + + *key = recurse_key_t (lookup_idx, + active_idx, + recurse_path, + depend_data->current_context_set_index); + return !completed_recursions.has (*key); + } + + void finish_recurse (const recurse_key_t &key) + { depend_data->check_success (completed_recursions.set (key, true)); } + + void reset_recurse_cache () + { + completed_recursions.clear (); + recurse_path_to_index.clear (); + recurse_path = 0; + } + + bool find_or_create_recurse_set (const hb_set_t &set, hb_codepoint_t *index) + { + hb_codepoint_t *existing = nullptr; + if (recurse_set_to_index.has (&set, &existing)) + { + *index = *existing; + return true; + } + + hb_set_t *copy = hb_object_create (); + if (unlikely (!copy)) + return depend_data->fail (); + copy->set (set); + if (unlikely (copy->in_error ())) + { + hb_set_destroy (copy); + return depend_data->fail (); + } + + *index = recurse_sets.length; + if (unlikely (*index & SINGLE_GLYPH_ACTIVE_FLAG)) + { + hb_set_destroy (copy); + return depend_data->fail (); + } + if (unlikely (!recurse_sets.push_or_fail (hb::unique_ptr {copy}))) + return depend_data->fail (); + if (unlikely (!recurse_set_to_index.set (recurse_sets[*index].get (), *index))) + return depend_data->fail (); + return true; + } + + const hb_set_t *find_or_create_glyph_set (const void *data, + unsigned value, + glyph_set_type_t type); + hb_codepoint_t find_or_create_cached_context_set (const hb_set_t *set); + + struct depend_scratch_t + { + void clear () + { + preliminary_context.clear (); + position_scratch.clear (); + pos_glyphs.clear (); + position_context.clear (); + disjunctive_indices.clear (); + filtered_disjunctive_indices.clear (); + backtrack_sets.clear (); + lookahead_sets.clear (); + input_position_glyphs.clear (); + cached_context_sets.clear (); + compact_workspace.clear (); + } + + hb_set_t preliminary_context; + hb_set_t position_scratch; + hb_set_t pos_glyphs; + hb_set_t position_context; + hb_set_t disjunctive_indices; + hb_set_t filtered_disjunctive_indices; + hb_vector_t backtrack_sets; + hb_vector_t lookahead_sets; + hb_vector_t input_position_glyphs; + hb_vector_t cached_context_sets; + hb_vector_t compact_workspace; + depend_scratch_t *next = nullptr; }; - bool push_context (context_info_t &&ctx) + depend_scratch_t *acquire_depend_scratch () { - return depend_data->check_success (context_stack.push_or_fail (std::move (ctx))); + depend_scratch_t *scratch = depend_scratch_pool; + if (scratch) + depend_scratch_pool = scratch->next; + else + { + scratch = (depend_scratch_t *) hb_malloc (sizeof (depend_scratch_t)); + if (unlikely (!scratch)) + { + depend_data->fail (); + return nullptr; + } + new (scratch) depend_scratch_t (); + } + + scratch->next = nullptr; + scratch->clear (); + return scratch; } - void pop_context () + void release_depend_scratch (depend_scratch_t *scratch) { - if (context_stack.length > 0) - context_stack.resize (context_stack.length - 1); - } - - const context_info_t* current_context () const - { - return context_stack.length > 0 ? &context_stack[context_stack.length - 1] : nullptr; + scratch->next = depend_scratch_pool; + depend_scratch_pool = scratch; } hb_depend_data_builder_t *depend_data; hb_face_t *face; hb_set_t *glyphs; - hb_vector_t active_glyphs_stack; - hb_vector_t context_stack; /* Stack of context information */ + hb_vector_t active_glyphs_stack; + unsigned active_glyphs_stack_depth = 0; recurse_func_t recurse_func; hb_codepoint_t lookup_index = HB_CODEPOINT_INVALID; - hb_set_t lookups_seen; + hb_bit_set_t lookups_seen; + hb_vector_t> recurse_sets; + hb_hashmap_t recurse_set_to_index; + hb_hashmap_t recurse_path_to_index; + hb_codepoint_t recurse_path = 0; + hb_hashmap_t completed_recursions; + hb_vector_t> glyph_sets; + hb_hashmap_t glyph_set_to_index; + hb_hashmap_t cached_context_set_indices; + hb_hashmap_t context_set_cache; + depend_scratch_t *depend_scratch_pool = nullptr; hb_depend_context_t (hb_depend_data_builder_t *depend_data_, hb_face_t *face_, @@ -475,6 +778,16 @@ struct hb_depend_context_t : face(face_), glyphs (glyphs_), recurse_func (nullptr) {} + ~hb_depend_context_t () + { + while (depend_scratch_pool) + { + depend_scratch_t *scratch = depend_scratch_pool; + depend_scratch_pool = scratch->next; + scratch->~depend_scratch_t (); + hb_free (scratch); + } + } void set_recurse_func (recurse_func_t func) { recurse_func = func; } }; @@ -1355,6 +1668,69 @@ static inline void collect_coverage (hb_set_t *glyphs, unsigned value, const voi coverage = value; (data+coverage).collect_coverage (glyphs); } + +inline const hb_set_t * +hb_depend_context_t::find_or_create_glyph_set (const void *data, + unsigned value, + glyph_set_type_t type) +{ + /* Class definitions and coverages are immutable for the lifetime of the + * context, so their expanded glyph sets can be shared across traversals. */ + glyph_set_cache_key_t key (data, value, type); + hb_codepoint_t *existing = nullptr; + if (glyph_set_to_index.has (key, &existing)) + return glyph_sets[*existing].get (); + + hb_set_t *set = hb_object_create (); + if (unlikely (!set)) + { + depend_data->fail (); + return nullptr; + } + + if (type == GLYPH_SET_COVERAGE) + collect_coverage (set, value, data); + else if (type == GLYPH_SET_CLASS) + collect_class (set, value, data); + else + set->add (value); + if (unlikely (set->in_error ())) + { + hb_set_destroy (set); + depend_data->fail (); + return nullptr; + } + + hb_codepoint_t index = glyph_sets.length; + if (unlikely (!glyph_sets.push_or_fail (hb::unique_ptr {set}))) + { + depend_data->fail (); + return nullptr; + } + if (unlikely (!glyph_set_to_index.set (key, index))) + { + depend_data->fail (); + return nullptr; + } + return glyph_sets[index].get (); +} + +inline hb_codepoint_t +hb_depend_context_t::find_or_create_cached_context_set (const hb_set_t *set) +{ + uintptr_t key = (uintptr_t) set; + hb_codepoint_t *existing = nullptr; + if (cached_context_set_indices.has (key, &existing)) + return *existing; + + hb_codepoint_t index = depend_data->find_or_create_context_set (*set); + if (unlikely (index == HB_CODEPOINT_INVALID)) + return HB_CODEPOINT_INVALID; + if (unlikely (!cached_context_set_indices.set (key, index))) + return depend_data->fail_invalid (); + return index; +} + template static inline void collect_array (hb_collect_glyphs_context_t *c HB_UNUSED, hb_set_t *glyphs, @@ -1839,6 +2215,45 @@ static unsigned serialize_lookuprecord_array (hb_serialize_context_t *c, enum ContextFormat { SimpleContext = 1, ClassBasedContext = 2, CoverageBasedContext = 3 }; +static inline const hb_set_t * +depend_position_glyphs (hb_depend_context_t *c, + ContextFormat context_format, + const void *data, + unsigned value, + bool filter_class_by_parent, + hb_set_t *scratch, + hb_vector_t *compact_workspace = nullptr) +{ + switch (context_format) + { + case ContextFormat::SimpleContext: + return c->find_or_create_glyph_set (nullptr, value, + hb_depend_context_t::GLYPH_SET_SINGLE); + case ContextFormat::ClassBasedContext: + { + const hb_set_t *class_set = c->find_or_create_glyph_set ( + data, value, hb_depend_context_t::GLYPH_SET_CLASS); + if (unlikely (!class_set)) + return nullptr; + if (!filter_class_by_parent) + return class_set; + scratch->set (*class_set); + scratch->intersect (c->parent_active_glyphs (), compact_workspace); + break; + } + case ContextFormat::CoverageBasedContext: + return c->find_or_create_glyph_set ( + data, value, hb_depend_context_t::GLYPH_SET_COVERAGE); + } + + if (unlikely (scratch->in_error ())) + { + c->depend_data->fail (); + return nullptr; + } + return scratch; +} + template static void context_closure_recurse_lookups (hb_closure_context_t *c, unsigned inputCount, const HBUINT input[], @@ -1911,18 +2326,59 @@ static void context_closure_recurse_lookups (hb_closure_context_t *c, } } +static bool +context_depend_sets_are_cached (hb_depend_context_t *c, + unsigned input_count, + unsigned lookup_count, + const LookupRecord lookup_records[], + const void *rule, + unsigned value, + ContextFormat context_format, + const void *data, + hb_vector_t *cached_context_sets) +{ + hb_codepoint_t active_idx = HB_CODEPOINT_INVALID; + bool have_active_idx = false; + for (unsigned i = 0; i < lookup_count; i++) + { + if (lookup_records[i].sequenceIndex >= input_count) + continue; + + if (!have_active_idx) + { + if (unlikely (!c->get_parent_active_glyphs_index (&active_idx))) + return false; + have_active_idx = true; + } + + hb_depend_context_t::context_set_cache_key_t key ( + rule, &lookup_records[i], data, active_idx, value, context_format); + hb_codepoint_t *cached_context_set = nullptr; + if (!c->context_set_cache.has (key, &cached_context_set)) + return false; + if (unlikely (!cached_context_sets->push_or_fail (*cached_context_set))) + return c->depend_data->fail (); + } + /* A rule with no valid records has no cache entries, and its preparation + * can still allocate dependency sets in an observable order. */ + return have_active_idx; +} + template static void context_depend_recurse_lookups (hb_depend_context_t *c, unsigned inputCount, const HBUINT input[], unsigned lookupCount, const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */, + const void *rule, unsigned value, ContextFormat context_format, const void *data, intersected_glyphs_func_t intersected_glyphs_func, void *cache, const hb_set_t &preliminary_context, - const hb_vector_t *input_position_glyphs) + const hb_vector_t *input_position_glyphs, + const hb_vector_t *cached_context_sets, + hb_depend_context_t::depend_scratch_t *scratch) { /* For depend graph extraction, we filter active glyphs by InputCoverage constraints * (same as closure), but we do NOT do sequential accumulation of outputs. @@ -1932,105 +2388,122 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c, * - Sequential accumulation: Not done. We want ALL possible edges, so we don't * limit later lookups based on outputs from earlier lookups in the same rule. */ - hb_set_t covered_seq_indicies; - hb_set_t pos_glyphs; + /* HB_MAX_CONTEXT_LENGTH positions fit in one allocation-free bit page. */ + hb_bit_page_t covered_seq_indices; + hb_set_t &pos_glyphs = scratch->pos_glyphs; + hb_set_t &position_context = scratch->position_context; + hb_set_t &disjunctive_indices = scratch->disjunctive_indices; + hb_set_t &filtered_disjunctive_indices = scratch->filtered_disjunctive_indices; + unsigned cached_context_index = 0; for (unsigned int i = 0; i < lookupCount; i++) { unsigned seqIndex = lookupRecord[i].sequenceIndex; if (seqIndex >= inputCount) continue; - /* Build position-specific context_set for this lookup */ - hb_set_t position_context; // Will hold direct glyphs - hb_set_t disjunctive_indices; // Will hold encoded set references + hb_depend_context_t::context_set_cache_key_t context_cache_key; + if (unlikely (!c->get_context_set_cache_key (rule, &lookupRecord[i], data, value, + context_format, &context_cache_key))) + return; - /* Process preliminary_context in one pass */ - hb_codepoint_t elem = HB_SET_VALUE_INVALID; - while (preliminary_context.next (&elem)) { - if (!(elem & HB_DEPEND_CONTEXT_SET_FLAG)) { - /* Direct glyph */ - position_context.add (elem); - } else { - /* Encoded set reference - defer processing until we have all direct requirements */ - disjunctive_indices.add (elem); - } - } - - /* Add singleton glyphs from OTHER input positions */ - for (unsigned j = 0; j < input_position_glyphs->length; j++) { - if (j != seqIndex && (*input_position_glyphs)[j].get_population () == 1) { - position_context.add ((*input_position_glyphs)[j].get_min ()); - } - } - - /* Now position_context has all direct requirements. - * Process deferred encoded sets and filter them. */ - hb_set_t filtered_disjunctive_indices; - elem = HB_SET_VALUE_INVALID; - while (disjunctive_indices.next (&elem)) { - hb_codepoint_t set_idx = elem & 0x7FFFFFFF; - const hb_set_t *original_set = c->depend_data->get_set_from_index (set_idx); - if (unlikely (!original_set)) continue; - - /* Make local copy and subtract direct requirements */ - hb_set_t filtered; - filtered.set (*original_set); - filtered.subtract (position_context); - if (unlikely (filtered.in_error ())) - { - c->depend_data->fail (); - return; - } - - if (filtered == *original_set) { - /* No overlap with position_context - add full requirement */ - filtered_disjunctive_indices.add (elem); - } - /* Otherwise: position_context intersects with original_set. - * Since disjunctive requirements need only ONE glyph from the set, - * and position_context already has a glyph from this set, the - * requirement is satisfied - no need to add anything. */ - } - - /* Process non-singleton input positions */ - for (unsigned j = 0; j < input_position_glyphs->length; j++) { - if (j != seqIndex && (*input_position_glyphs)[j].get_population () > 1) { - hb_set_t filtered; - filtered.set ((*input_position_glyphs)[j]); - filtered.subtract (position_context); - if (unlikely (filtered.in_error ())) - { - c->depend_data->fail (); - return; - } - - if (filtered == (*input_position_glyphs)[j]) { - /* No overlap with position_context - add full requirement */ - hb_codepoint_t idx = c->depend_data->find_or_create_context_set ((*input_position_glyphs)[j]); - if (unlikely (idx == HB_CODEPOINT_INVALID)) - return; - filtered_disjunctive_indices.add (HB_DEPEND_CONTEXT_SET_FLAG | idx); - } - /* Otherwise: position_context intersects, requirement satisfied */ - } - } - - /* Combine direct + disjunctive */ - position_context.union_ (filtered_disjunctive_indices); - if (unlikely (position_context.in_error () || - filtered_disjunctive_indices.in_error ())) + hb_codepoint_t context_set_idx; + hb_codepoint_t *cached_context_set_idx; + if (cached_context_sets) + context_set_idx = (*cached_context_sets)[cached_context_index++]; + else if (c->context_set_cache.has (context_cache_key, &cached_context_set_idx)) + context_set_idx = *cached_context_set_idx; + else { - c->depend_data->fail (); - return; - } + if (unlikely (!input_position_glyphs)) + { + c->depend_data->fail (); + return; + } - /* Allocate final context_set */ - hb_codepoint_t context_set_idx = position_context.is_empty () - ? HB_CODEPOINT_INVALID - : c->depend_data->find_or_create_context_set (position_context); - if (unlikely (!position_context.is_empty () && - context_set_idx == HB_CODEPOINT_INVALID)) - return; + /* Build position-specific context_set for this lookup. */ + position_context.clear (); // Will hold direct glyphs + disjunctive_indices.clear (); // Will hold encoded set references + filtered_disjunctive_indices.clear (); + + /* Process preliminary_context in one pass. */ + hb_codepoint_t elem = HB_SET_VALUE_INVALID; + while (preliminary_context.next (&elem)) { + if (!(elem & HB_DEPEND_CONTEXT_SET_FLAG)) { + /* Direct glyph */ + position_context.add (elem); + } else { + /* Encoded set reference - defer processing until we have all direct requirements */ + disjunctive_indices.add (elem); + } + } + + /* Add singleton glyphs from OTHER input positions. */ + for (unsigned j = 0; j < input_position_glyphs->length; j++) { + if (j != seqIndex && (*input_position_glyphs)[j]->get_population () == 1) { + position_context.add ((*input_position_glyphs)[j]->get_min ()); + } + } + + /* Now position_context has all direct requirements. + * Process deferred encoded sets and filter them. */ + elem = HB_SET_VALUE_INVALID; + while (disjunctive_indices.next (&elem)) { + hb_codepoint_t set_idx = elem & 0x7FFFFFFF; + const hb_set_t *original_set = c->depend_data->get_set_from_index (set_idx); + if (unlikely (!original_set)) continue; + + /* Dependency context sets are never inverted. */ + if (!original_set->intersects (position_context)) { + /* No overlap with position_context - add full requirement */ + filtered_disjunctive_indices.add (elem); + } + /* Otherwise: position_context intersects with original_set. + * Since disjunctive requirements need only ONE glyph from the set, + * and position_context already has a glyph from this set, the + * requirement is satisfied - no need to add anything. */ + } + + /* Process non-singleton input positions. */ + for (unsigned j = 0; j < input_position_glyphs->length; j++) { + if (j != seqIndex && (*input_position_glyphs)[j]->get_population () > 1) { + /* Input-position sets are also never inverted. */ + if (!(*input_position_glyphs)[j]->intersects (position_context)) { + /* No overlap with position_context - add full requirement */ + bool cached_set = context_format == ContextFormat::CoverageBasedContext || + (context_format == ContextFormat::ClassBasedContext && j != 0); + hb_codepoint_t idx = cached_set + ? c->find_or_create_cached_context_set ((*input_position_glyphs)[j]) + : c->depend_data->find_or_create_context_set (*(*input_position_glyphs)[j]); + if (unlikely (idx == HB_CODEPOINT_INVALID)) + return; + filtered_disjunctive_indices.add (HB_DEPEND_CONTEXT_SET_FLAG | idx); + } + /* Otherwise: position_context intersects, requirement satisfied */ + } + } + + /* Combine direct + disjunctive. */ + position_context.union_ (filtered_disjunctive_indices); + if (unlikely (position_context.in_error () || + filtered_disjunctive_indices.in_error ())) + { + c->depend_data->fail (); + return; + } + + context_set_idx = position_context.is_empty () + ? HB_CODEPOINT_INVALID + : c->depend_data->find_or_create_context_set (position_context); + if (unlikely (!position_context.is_empty () && + context_set_idx == HB_CODEPOINT_INVALID)) + return; + + if (unlikely (!c->context_set_cache.set (context_cache_key, context_set_idx))) + { + c->depend_data->fail (); + return; + } + } /* Save outer context to restore after this lookup. When a contextual lookup * calls another contextual lookup, we want each lookup in this rule to see @@ -2064,7 +2537,7 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c, bool has_pos_glyphs = false; - if (!covered_seq_indicies.has (seqIndex)) + if (!covered_seq_indices.has (seqIndex)) { has_pos_glyphs = true; pos_glyphs.clear (); @@ -2096,25 +2569,37 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c, } } - covered_seq_indicies.add (seqIndex); - hb_set_t *cur_active_glyphs = c->push_cur_active_glyphs (); - if (unlikely (!cur_active_glyphs)) + covered_seq_indices.add (seqIndex); + if (has_pos_glyphs) + { + hb_codepoint_t active_idx = HB_CODEPOINT_INVALID; + if (context_format == ContextFormat::SimpleContext) + { + hb_codepoint_t glyph = seqIndex ? pos_glyphs.get_min () : value; + if (likely (glyph < hb_depend_context_t::SINGLE_GLYPH_ACTIVE_FLAG)) + active_idx = glyph | hb_depend_context_t::SINGLE_GLYPH_ACTIVE_FLAG; + } + hb_set_t *cur_active_glyphs = c->push_cur_active_glyphs (active_idx); + if (unlikely (!cur_active_glyphs)) + { + c->depend_data->current_context_set_index = saved_context_set_index; + c->depend_data->current_edge_flags = saved_edge_flags; + return; + } + *cur_active_glyphs = std::move (pos_glyphs); + } + else if (unlikely (!c->push_root_active_glyphs ())) { c->depend_data->current_context_set_index = saved_context_set_index; c->depend_data->current_edge_flags = saved_edge_flags; return; } - if (has_pos_glyphs) { - *cur_active_glyphs = std::move (pos_glyphs); - } else { - *cur_active_glyphs = *c->glyphs; - } unsigned endIndex = inputCount; if (context_format == ContextFormat::CoverageBasedContext) endIndex += 1; - c->recurse (lookupRecord[i].lookupListIndex, &covered_seq_indicies, seqIndex, endIndex); + c->recurse (lookupRecord[i].lookupListIndex, &covered_seq_indices, seqIndex, endIndex); c->pop_cur_done_glyphs (); /* Restore outer context after this lookup, so subsequent lookups in this @@ -2345,6 +2830,7 @@ static inline void context_depend_lookup (hb_depend_context_t *c, const HBUINT input[], /* Array of input values--start with second glyph */ unsigned int lookupCount, const LookupRecord lookupRecord[], + const void *rule, unsigned value, /* Index of first glyph in Coverage or Class value in ClassDef table */ ContextDependLookupContext &lookup_context) { @@ -2353,77 +2839,69 @@ static inline void context_depend_lookup (hb_depend_context_t *c, lookup_context)) return; + hb_depend_context_t::depend_scratch_t *scratch = c->acquire_depend_scratch (); + if (unlikely (!scratch)) + return; + HB_SCOPE_GUARD (c->release_depend_scratch (scratch)); + + bool context_sets_cached = context_depend_sets_are_cached ( + c, inputCount, lookupCount, lookupRecord, rule, value, + lookup_context.context_format, lookup_context.intersects_data, + &scratch->cached_context_sets); + if (unlikely (!c->depend_data->successful)) + return; + /* Build preliminary_context (empty for ContextSubst - no backtrack/lookahead) */ - hb_set_t preliminary_context; + hb_set_t &preliminary_context = scratch->preliminary_context; /* Build glyph sets for ALL input positions */ - hb_vector_t input_position_glyphs; + hb_set_t &position_scratch = scratch->position_scratch; + hb_vector_t &input_position_glyphs = scratch->input_position_glyphs; - /* Position 0 (Coverage glyph) — must be in both the class AND the - * coverage, so intersect with parent_active_glyphs (= coverage ∩ glyphs - * set by the ContextFormat2/3 caller). */ - hb_set_t pos0_glyphs; - switch (lookup_context.context_format) + /* Cached labels no longer need the expanded input-position sets. */ + if (!context_sets_cached) { - case ContextFormat::SimpleContext: - pos0_glyphs.add (value); - break; - case ContextFormat::ClassBasedContext: - collect_class (&pos0_glyphs, value, lookup_context.intersects_data); - pos0_glyphs.intersect (c->parent_active_glyphs ()); - break; - case ContextFormat::CoverageBasedContext: - collect_coverage (&pos0_glyphs, value, lookup_context.intersects_data); - break; - } - if (unlikely (!input_position_glyphs.push_or_fail (pos0_glyphs))) - { - c->depend_data->fail (); - return; - } - - /* Positions 1+ (Input array) */ - for (unsigned i = 0; i < inputCount - 1; i++) - { - hb_set_t pos_glyphs; - switch (lookup_context.context_format) - { - case ContextFormat::SimpleContext: - pos_glyphs.add (input[i]); - break; - case ContextFormat::ClassBasedContext: - collect_class (&pos_glyphs, input[i], lookup_context.intersects_data); - break; - case ContextFormat::CoverageBasedContext: - collect_coverage (&pos_glyphs, input[i], lookup_context.intersects_data); - break; - } - if (unlikely (!input_position_glyphs.push_or_fail (pos_glyphs))) + /* Position 0 (Coverage glyph) — must be in both the class AND the + * coverage, so intersect with parent_active_glyphs (= coverage ∩ glyphs + * set by the ContextFormat2/3 caller). */ + const hb_set_t *pos0_glyphs = depend_position_glyphs ( + c, lookup_context.context_format, lookup_context.intersects_data, value, + true, &position_scratch, &scratch->compact_workspace); + if (unlikely (!pos0_glyphs || + !input_position_glyphs.push_or_fail (pos0_glyphs))) { c->depend_data->fail (); return; } - } - /* Push context before recursing (for backtrack/lookahead tracking - empty for ContextSubst) */ - typename hb_depend_context_t::context_info_t ctx_info; - if (unlikely (!c->push_context (std::move (ctx_info)))) - return; + /* Positions 1+ (Input array) */ + for (unsigned i = 0; i < inputCount - 1; i++) + { + const hb_set_t *pos_glyphs = depend_position_glyphs ( + c, lookup_context.context_format, lookup_context.intersects_data, + input[i], false, &position_scratch); + if (unlikely (!pos_glyphs || + !input_position_glyphs.push_or_fail (pos_glyphs))) + { + c->depend_data->fail (); + return; + } + } + } context_depend_recurse_lookups (c, inputCount, input, lookupCount, lookupRecord, + rule, value, lookup_context.context_format, lookup_context.intersects_data, lookup_context.funcs.intersected_glyphs, lookup_context.intersected_glyphs_cache, preliminary_context, - &input_position_glyphs); - - /* Pop context */ - c->pop_context (); - /* preliminary_context is stack-allocated and will be destroyed automatically */ + context_sets_cached ? nullptr : &input_position_glyphs, + context_sets_cached ? &scratch->cached_context_sets : nullptr, + scratch); } template @@ -2532,6 +3010,7 @@ struct Rule context_depend_lookup (c, inputCount, inputZ.arrayZ, lookupCount, lookupRecord.arrayZ, + this, value, lookup_context); } @@ -3404,6 +3883,7 @@ struct ContextFormat3 context_depend_lookup (c, glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1), lookupCount, lookupRecord, + this, coverageZ[0], lookup_context); c->pop_cur_done_glyphs (); @@ -3669,6 +4149,7 @@ static inline void chain_context_depend_lookup (hb_depend_context_t *c, const HBUINT lookahead[], unsigned int lookupCount, const LookupRecord lookupRecord[], + const void *rule, unsigned value, ChainContextDependLookupContext &lookup_context) { @@ -3679,28 +4160,52 @@ static inline void chain_context_depend_lookup (hb_depend_context_t *c, lookup_context)) return; - /* Build context information - extract backtrack and lookahead glyphs */ - typename hb_depend_context_t::context_info_t ctx_info; + hb_depend_context_t::depend_scratch_t *scratch = c->acquire_depend_scratch (); + if (unlikely (!scratch)) + return; + HB_SCOPE_GUARD (c->release_depend_scratch (scratch)); + + bool context_sets_cached = context_depend_sets_are_cached ( + c, inputCount, lookupCount, lookupRecord, rule, value, + lookup_context.context_format, lookup_context.intersects_data[1], + &scratch->cached_context_sets); + if (unlikely (!c->depend_data->successful)) + return; + if (context_sets_cached) + { + context_depend_recurse_lookups (c, + inputCount, input, + lookupCount, lookupRecord, + rule, + value, + lookup_context.context_format, + lookup_context.intersects_data[1], + lookup_context.funcs.intersected_glyphs, + lookup_context.intersected_glyphs_cache, + scratch->preliminary_context, + nullptr, + &scratch->cached_context_sets, + scratch); + return; + } + + /* Cache misses and rules without valid records still perform the original + * dependency-set interning in its observable order. */ + hb_set_t &position_scratch = scratch->position_scratch; + hb_vector_t &backtrack_sets = scratch->backtrack_sets; + hb_vector_t &lookahead_sets = scratch->lookahead_sets; /* Extract backtrack glyphs */ for (unsigned i = 0; i < backtrackCount; i++) { - hb_set_t pos_glyphs; - switch (lookup_context.context_format) + const hb_set_t *pos_glyphs = depend_position_glyphs ( + c, lookup_context.context_format, lookup_context.intersects_data[0], + backtrack[i], false, &position_scratch); + if (unlikely (!pos_glyphs)) + return; + if (!pos_glyphs->is_empty ()) { - case ContextFormat::SimpleContext: - pos_glyphs.add (backtrack[i]); - break; - case ContextFormat::ClassBasedContext: - collect_class (&pos_glyphs, backtrack[i], lookup_context.intersects_data[0]); - break; - case ContextFormat::CoverageBasedContext: - collect_coverage (&pos_glyphs, backtrack[i], lookup_context.intersects_data[0]); - break; - } - if (!pos_glyphs.is_empty ()) - { - if (unlikely (!ctx_info.backtrack_sets.push_or_fail (pos_glyphs))) + if (unlikely (!backtrack_sets.push_or_fail (pos_glyphs))) { c->depend_data->fail (); return; @@ -3711,22 +4216,14 @@ static inline void chain_context_depend_lookup (hb_depend_context_t *c, /* Extract lookahead glyphs */ for (unsigned i = 0; i < lookaheadCount; i++) { - hb_set_t pos_glyphs; - switch (lookup_context.context_format) + const hb_set_t *pos_glyphs = depend_position_glyphs ( + c, lookup_context.context_format, lookup_context.intersects_data[2], + lookahead[i], false, &position_scratch); + if (unlikely (!pos_glyphs)) + return; + if (!pos_glyphs->is_empty ()) { - case ContextFormat::SimpleContext: - pos_glyphs.add (lookahead[i]); - break; - case ContextFormat::ClassBasedContext: - collect_class (&pos_glyphs, lookahead[i], lookup_context.intersects_data[2]); - break; - case ContextFormat::CoverageBasedContext: - collect_coverage (&pos_glyphs, lookahead[i], lookup_context.intersects_data[2]); - break; - } - if (!pos_glyphs.is_empty ()) - { - if (unlikely (!ctx_info.lookahead_sets.push_or_fail (pos_glyphs))) + if (unlikely (!lookahead_sets.push_or_fail (pos_glyphs))) { c->depend_data->fail (); return; @@ -3735,24 +4232,28 @@ static inline void chain_context_depend_lookup (hb_depend_context_t *c, } /* Build preliminary_context with backtrack + lookahead encoded */ - hb_set_t preliminary_context; - for (const auto &back_set : ctx_info.backtrack_sets) + hb_set_t &preliminary_context = scratch->preliminary_context; + for (const hb_set_t *back_set : backtrack_sets) { - if (back_set.get_population () == 1) { - preliminary_context.add (back_set.get_min ()); // Direct glyph - } else if (back_set.get_population () > 1) { - hb_codepoint_t set_idx = c->depend_data->find_or_create_context_set (back_set); + if (back_set->get_population () == 1) { + preliminary_context.add (back_set->get_min ()); // Direct glyph + } else if (back_set->get_population () > 1) { + hb_codepoint_t set_idx = lookup_context.context_format == ContextFormat::SimpleContext + ? c->depend_data->find_or_create_context_set (*back_set) + : c->find_or_create_cached_context_set (back_set); if (unlikely (set_idx == HB_CODEPOINT_INVALID)) return; preliminary_context.add (HB_DEPEND_CONTEXT_SET_FLAG | set_idx); // Encoded set reference } } - for (const auto &look_set : ctx_info.lookahead_sets) + for (const hb_set_t *look_set : lookahead_sets) { - if (look_set.get_population () == 1) { - preliminary_context.add (look_set.get_min ()); // Direct glyph - } else if (look_set.get_population () > 1) { - hb_codepoint_t set_idx = c->depend_data->find_or_create_context_set (look_set); + if (look_set->get_population () == 1) { + preliminary_context.add (look_set->get_min ()); // Direct glyph + } else if (look_set->get_population () > 1) { + hb_codepoint_t set_idx = lookup_context.context_format == ContextFormat::SimpleContext + ? c->depend_data->find_or_create_context_set (*look_set) + : c->find_or_create_cached_context_set (look_set); if (unlikely (set_idx == HB_CODEPOINT_INVALID)) return; preliminary_context.add (HB_DEPEND_CONTEXT_SET_FLAG | set_idx); // Encoded set reference @@ -3765,71 +4266,50 @@ static inline void chain_context_depend_lookup (hb_depend_context_t *c, } /* Build glyph sets for ALL input positions */ - hb_vector_t input_position_glyphs; + hb_vector_t &input_position_glyphs = scratch->input_position_glyphs; - /* Position 0 (first input coverage/class/glyph) — must be in both the - * class AND the coverage, so intersect with parent_active_glyphs. */ - hb_set_t pos0_glyphs; - switch (lookup_context.context_format) + if (!context_sets_cached) { - case ContextFormat::SimpleContext: - pos0_glyphs.add (value); - break; - case ContextFormat::ClassBasedContext: - collect_class (&pos0_glyphs, value, lookup_context.intersects_data[1]); - pos0_glyphs.intersect (c->parent_active_glyphs ()); - break; - case ContextFormat::CoverageBasedContext: - collect_coverage (&pos0_glyphs, value, lookup_context.intersects_data[1]); - break; - } - if (unlikely (!input_position_glyphs.push_or_fail (pos0_glyphs))) - { - c->depend_data->fail (); - return; - } - - /* Positions 1+ (Input array) */ - for (unsigned i = 0; i < inputCount - 1; i++) - { - hb_set_t pos_glyphs; - switch (lookup_context.context_format) - { - case ContextFormat::SimpleContext: - pos_glyphs.add (input[i]); - break; - case ContextFormat::ClassBasedContext: - collect_class (&pos_glyphs, input[i], lookup_context.intersects_data[1]); - break; - case ContextFormat::CoverageBasedContext: - collect_coverage (&pos_glyphs, input[i], lookup_context.intersects_data[1]); - break; - } - if (unlikely (!input_position_glyphs.push_or_fail (pos_glyphs))) + /* Position 0 (first input coverage/class/glyph) — must be in both the + * class AND the coverage, so intersect with parent_active_glyphs. */ + const hb_set_t *pos0_glyphs = depend_position_glyphs ( + c, lookup_context.context_format, lookup_context.intersects_data[1], value, + true, &position_scratch, &scratch->compact_workspace); + if (unlikely (!pos0_glyphs || + !input_position_glyphs.push_or_fail (pos0_glyphs))) { c->depend_data->fail (); return; } - } - /* Push context before recursing (use move to avoid copy issues) */ - if (unlikely (!c->push_context (std::move (ctx_info)))) - return; + /* Positions 1+ (Input array) */ + for (unsigned i = 0; i < inputCount - 1; i++) + { + const hb_set_t *pos_glyphs = depend_position_glyphs ( + c, lookup_context.context_format, lookup_context.intersects_data[1], + input[i], false, &position_scratch); + if (unlikely (!pos_glyphs || + !input_position_glyphs.push_or_fail (pos_glyphs))) + { + c->depend_data->fail (); + return; + } + } + } context_depend_recurse_lookups (c, inputCount, input, lookupCount, lookupRecord, + rule, value, lookup_context.context_format, lookup_context.intersects_data[1], lookup_context.funcs.intersected_glyphs, lookup_context.intersected_glyphs_cache, preliminary_context, - &input_position_glyphs); - - /* Pop context */ - c->pop_context (); - /* preliminary_context is stack-allocated and will be destroyed automatically */ + context_sets_cached ? nullptr : &input_position_glyphs, + context_sets_cached ? &scratch->cached_context_sets : nullptr, + scratch); } template @@ -3951,6 +4431,7 @@ struct ChainRule input.lenP1, input.arrayZ, lookahead.len, lookahead.arrayZ, lookup.len, lookup.arrayZ, + this, value, lookup_context); } @@ -4975,6 +5456,7 @@ struct ChainContextFormat3 input.len, (const HBUINT16 *) input.arrayZ + 1, lookahead.len, (const HBUINT16 *) lookahead.arrayZ, lookup.len, lookup.arrayZ, + this, input[0], lookup_context); c->pop_cur_done_glyphs (); diff --git a/src/hb-set.cc b/src/hb-set.cc index a9386c5c9..0f3ae7f05 100644 --- a/src/hb-set.cc +++ b/src/hb-set.cc @@ -357,6 +357,24 @@ hb_set_is_equal (const hb_set_t *set, return set->is_equal (*other); } +/** + * hb_set_intersects: + * @set: A set + * @other: Another set + * + * Tests whether @set and @other have any elements in common. + * + * Return value: `true` if the two sets intersect, `false` otherwise. + * + * XSince: REPLACEME + **/ +hb_bool_t +hb_set_intersects (const hb_set_t *set, + const hb_set_t *other) +{ + return set->intersects (*other); +} + /** * hb_set_hash: * @set: A set diff --git a/src/hb-set.h b/src/hb-set.h index 192abf6f6..2f3d670d2 100644 --- a/src/hb-set.h +++ b/src/hb-set.h @@ -131,6 +131,10 @@ HB_EXTERN hb_bool_t hb_set_is_equal (const hb_set_t *set, const hb_set_t *other); +HB_EXTERN hb_bool_t +hb_set_intersects (const hb_set_t *set, + const hb_set_t *other); + HB_EXTERN unsigned int hb_set_hash (const hb_set_t *set); diff --git a/src/hb-set.hh b/src/hb-set.hh index 5cd983c00..b9ef5df23 100644 --- a/src/hb-set.hh +++ b/src/hb-set.hh @@ -87,6 +87,7 @@ struct hb_sparseset_t uint32_t hash () const { return s.hash (); } void add (hb_codepoint_t g) { s.add (g); } + void add_bits (hb_codepoint_t g, uint64_t bits) { s.add_bits (g, bits); } bool add_range (hb_codepoint_t first, hb_codepoint_t last) { return s.add_range (first, last); } template @@ -125,6 +126,9 @@ struct hb_sparseset_t bool may_intersect (const hb_sparseset_t &other) const { return s.may_intersect (other.s); } + bool intersects (const hb_sparseset_t &other) const + { return s.intersects (other.s); } + bool intersects (hb_codepoint_t first, hb_codepoint_t last) const { return s.intersects (first, last); } @@ -137,11 +141,15 @@ struct hb_sparseset_t bool is_subset (const hb_sparseset_t &larger_set) const { return s.is_subset (larger_set.s); } void union_ (const hb_sparseset_t &other) { s.union_ (other.s); } - void intersect (const hb_sparseset_t &other) { s.intersect (other.s); } + void intersect (const hb_sparseset_t &other, + hb_vector_t *workspace = nullptr) + { s.intersect (other.s, workspace); } void subtract (const hb_sparseset_t &other) { s.subtract (other.s); } void symmetric_difference (const hb_sparseset_t &other) { s.symmetric_difference (other.s); } bool next (hb_codepoint_t *codepoint) const { return s.next (codepoint); } + bool next_bits (hb_codepoint_t *codepoint, uint64_t *bits) const + { return s.next_bits (codepoint, bits); } bool previous (hb_codepoint_t *codepoint) const { return s.previous (codepoint); } bool next_range (hb_codepoint_t *first, hb_codepoint_t *last) const { return s.next_range (first, last); } @@ -151,6 +159,7 @@ struct hb_sparseset_t { return s.next_many (codepoint, out, size); } unsigned int get_population () const { return s.get_population (); } + bool get_singleton (hb_codepoint_t *codepoint) const { return s.get_singleton (codepoint); } hb_codepoint_t get_min () const { return s.get_min (); } hb_codepoint_t get_max () const { return s.get_max (); } diff --git a/src/hb-subset-input.cc b/src/hb-subset-input.cc index 117a45fec..c3906fdc1 100644 --- a/src/hb-subset-input.cc +++ b/src/hb-subset-input.cc @@ -806,9 +806,8 @@ _format_tag (hb_tag_t tag, hb_vector_t &out) static void _format_tag_set (const hb_set_t *set, hb_vector_t &out) { - hb_codepoint_t tag = HB_SET_VALUE_INVALID; bool first_tag = true; - while (hb_set_next (set, &tag)) + for (hb_codepoint_t tag : *set) { if (!first_tag) out.push (','); @@ -916,9 +915,8 @@ _format_glyph_map_arg (const hb_map_t &glyph_map, hb_set_t keys; hb_map_keys (&glyph_map, &keys); hb_vector_t pairs; - hb_codepoint_t key = HB_SET_VALUE_INVALID; bool first = true; - while (hb_set_next (&keys, &key)) + for (hb_codepoint_t key : keys) { if (!first) pairs.push (','); diff --git a/src/test-set.cc b/src/test-set.cc index 70d3f6756..01284cb8a 100644 --- a/src/test-set.cc +++ b/src/test-set.cc @@ -137,6 +137,61 @@ main (int argc, char **argv) hb_always_assert (s.has (HB_SET_VALUE_INVALID)); } + /* Test set intersection. */ + { + hb_set_t a {1, 2}; + hb_set_t b {2, 3}; + hb_set_t c {3, 4}; + + hb_always_assert (a.intersects (b)); + hb_always_assert (!a.intersects (c)); + + b.invert (); + hb_always_assert (a.intersects (b)); + hb_always_assert (b.intersects (a)); + + hb_set_t singleton {2}; + hb_always_assert (!singleton.intersects (b)); + hb_always_assert (!b.intersects (singleton)); + + c.invert (); + hb_always_assert (b.intersects (c)); + } + + /* Test singleton detection. */ + { + hb_set_t s; + hb_codepoint_t singleton; + hb_always_assert (!s.get_singleton (&singleton)); + + s.add (63); + hb_always_assert (s.get_singleton (&singleton)); + hb_always_assert (singleton == 63); + + s.add (1024); + hb_always_assert (!s.get_singleton (&singleton)); + s.del (63); + hb_always_assert (s.get_singleton (&singleton)); + hb_always_assert (singleton == 1024); + + s.invert (); + hb_always_assert (!s.get_singleton (&singleton)); + } + + /* Test word-at-a-time insertion. */ + { + hb_set_t s; + s.add_bits (64, uint64_t (1) | (uint64_t (1) << 63)); + hb_always_assert (s.get_population () == 2); + hb_always_assert (s.has (64)); + hb_always_assert (s.has (127)); + + s.invert (); + s.add_bits (64, uint64_t (1) | (uint64_t (1) << 63)); + hb_always_assert (s.has (64)); + hb_always_assert (s.has (127)); + } + /* Adding HB_SET_VALUE_INVALID */ { hb_set_t s; @@ -161,5 +216,70 @@ main (int argc, char **argv) hb_always_assert(s.has(2)); } + /* Test iteration across words and pages. */ + { + hb_set_t s {0, 1, 63, 64, 65, 127, 511, 512, 513, 1024, + HB_SET_VALUE_INVALID - 1}; + hb_codepoint_t expected[] = {0, 1, 63, 64, 65, 127, 511, 512, 513, + 1024, HB_SET_VALUE_INVALID - 1}; + unsigned int i = 0; + for (hb_codepoint_t v : s) + { + hb_always_assert (i < ARRAY_LENGTH (expected)); + hb_always_assert (v == expected[i++]); + } + hb_always_assert (i == ARRAY_LENGTH (expected)); + + auto it = s.iter (); + hb_always_assert (*it == 0); + hb_always_assert (it.len () == ARRAY_LENGTH (expected)); + ++it; + hb_always_assert (*it == 1); + hb_always_assert (it.len () == ARRAY_LENGTH (expected) - 1); + --it; + hb_always_assert (*it == 0); + ++it; + hb_always_assert (*it == 1); + it += 3; + hb_always_assert (*it == 65); + --it; + hb_always_assert (*it == 64); + --it; + hb_always_assert (*it == 63); + ++it; + hb_always_assert (*it == 64); + + it = it.end (); + --it; + hb_always_assert (*it == HB_SET_VALUE_INVALID - 1); + --it; + hb_always_assert (*it == 1024); + ++it; + hb_always_assert (*it == HB_SET_VALUE_INVALID - 1); + + it = s.iter (); + it += 2; + hb_always_assert (it.len () == ARRAY_LENGTH (expected) - 2); + } + + /* Test inverted-set iteration. */ + { + hb_set_t s {0, 1, 63, 64}; + s.invert (); + auto it = s.iter (); + hb_always_assert (*it == 2); + hb_always_assert (it.len () == HB_SET_VALUE_INVALID - 4); + ++it; + hb_always_assert (*it == 3); + --it; + hb_always_assert (*it == 2); + + it = it.end (); + --it; + hb_always_assert (*it == HB_SET_VALUE_INVALID - 1); + --it; + hb_always_assert (*it == HB_SET_VALUE_INVALID - 2); + } + return 0; } diff --git a/test/api/test-set.c b/test/api/test-set.c index 077d63431..9264c6d36 100644 --- a/test/api/test-set.c +++ b/test/api/test-set.c @@ -1024,6 +1024,41 @@ test_set_inverted_equality (void) hb_set_destroy (b); } +static void +test_set_intersects (void) +{ + hb_set_t *a = hb_set_create (); + hb_set_t *b = hb_set_create (); + hb_set_t *c = hb_set_create (); + + hb_set_add (a, 1); + hb_set_add (a, 2); + hb_set_add (b, 2); + hb_set_add (b, 3); + hb_set_add (c, 3); + hb_set_add (c, 4); + + g_assert_true (hb_set_intersects (a, b)); + g_assert_true (hb_set_intersects (b, a)); + g_assert_true (!hb_set_intersects (a, c)); + + hb_set_invert (b); + g_assert_true (hb_set_intersects (a, b)); + g_assert_true (hb_set_intersects (b, a)); + + hb_set_clear (a); + hb_set_add (a, 2); + g_assert_true (!hb_set_intersects (a, b)); + g_assert_true (!hb_set_intersects (b, a)); + + hb_set_invert (c); + g_assert_true (hb_set_intersects (b, c)); + + hb_set_destroy (a); + hb_set_destroy (b); + hb_set_destroy (c); +} + typedef enum { UNION = 0, INTERSECT, @@ -1300,6 +1335,7 @@ main (int argc, char **argv) hb_test_add (test_set_inverted_iteration_next); hb_test_add (test_set_inverted_iteration_prev); hb_test_add (test_set_inverted_equality); + hb_test_add (test_set_intersects); hb_test_add (test_set_inverted_operations); hb_test_add (test_hb_set_add_sorted_array); diff --git a/test/fuzzing/meson.build b/test/fuzzing/meson.build index 9e4f6b970..a84339df8 100644 --- a/test/fuzzing/meson.build +++ b/test/fuzzing/meson.build @@ -126,10 +126,11 @@ subset_fonts_glob = run_command(glob_cmd, meson.current_source_dir() / '..' / 's graphs_glob = run_command(glob_cmd, meson.current_source_dir() / 'graphs', check:true).stdout().strip().split('\n') depend_closure_parity_fonts_glob = [] +depend_closure_parity_noto_nastaliq_fonts = [] foreach font : subset_fonts_glob - # These fonts currently spend tens of seconds or more building the - # contextual GSUB dependency graph before the parity tests start. - if not font.contains('NotoNastaliqUrdu-Bold.ttf') and not font.contains('NotoNastaliqUrdu-Regular.ttf') + if font.contains('NotoNastaliqUrdu-Bold.ttf') or font.contains('NotoNastaliqUrdu-Regular.ttf') + depend_closure_parity_noto_nastaliq_fonts += [font] + else depend_closure_parity_fonts_glob += [font] endif endforeach @@ -250,6 +251,20 @@ if conf.get('HB_EXPERIMENTAL_API', 0) == 1 ) i += 1 endforeach + + foreach font : depend_closure_parity_noto_nastaliq_fonts + font_name = 'regular' + if font.contains('NotoNastaliqUrdu-Bold.ttf') + font_name = 'bold' + endif + test('depend-closure-parity-noto-nastaliq-@0@'.format(font_name), + hb_depend_closure_parity_exe, + args: ['-s', '42', '-n', '16', font], + workdir: meson.current_build_dir() / '..' / '..', + protocol: 'tap', + suite: ['fuzzing'], + ) + endforeach endif i = 0 diff --git a/util/hb-subset-depend.cc b/util/hb-subset-depend.cc index d58fca90d..613fa50c7 100644 --- a/util/hb-subset-depend.cc +++ b/util/hb-subset-depend.cc @@ -38,6 +38,8 @@ struct depend_t : option_parser_t, face_options_t gboolean flagged_only = false; gboolean no_context = false; gboolean no_glyph_names = false; + gboolean compile_only = false; + gboolean quiet = false; char *gids_str = nullptr; void @@ -60,6 +62,10 @@ struct depend_t : option_parser_t, face_options_t "Suppress context set output", nullptr}, {"no-glyph-names", 0, 0, G_OPTION_ARG_NONE, &this->no_glyph_names, "Use numeric glyph IDs instead of names", nullptr}, + {"compile", 0, 0, G_OPTION_ARG_NONE, &this->compile_only, + "Build the dependency graph without retrieving it", nullptr}, + {"quiet", 'q', 0, G_OPTION_ARG_NONE, &this->quiet, + "Retrieve the dependency graph without printing it", nullptr}, {nullptr} }; add_group (depend_entries, @@ -209,7 +215,13 @@ struct depend_t : option_parser_t, face_options_t return 1; } - hb_font_t *font = no_glyph_names ? nullptr : hb_font_create (face); + if (compile_only) + { + hb_subset_depend_destroy (depend); + return 0; + } + + hb_font_t *font = no_glyph_names || quiet ? nullptr : hb_font_create (face); std::set gid_filter; bool filter_gids = gids_str != nullptr; @@ -240,7 +252,7 @@ struct depend_t : option_parser_t, face_options_t entries.push_back (entry); } - if (entries.empty ()) continue; + if (entries.empty () || quiet) continue; if (printed_any) printf ("\n"); printed_any = true;