diff --git a/src/hb-depend-data.hh b/src/hb-depend-data.hh index eace0461b..a88f287d3 100644 --- a/src/hb-depend-data.hh +++ b/src/hb-depend-data.hh @@ -218,7 +218,14 @@ struct hb_depend_data_t return nullptr; } - bool get_glyph_entry (hb_codepoint_t gid, hb_codepoint_t index, + unsigned int get_glyph_entry_count (hb_codepoint_t gid) const + { + if (gid < glyph_dependencies.length) + return glyph_dependencies[gid].dependencies.length; + return 0; + } + + bool get_glyph_entry (hb_codepoint_t gid, unsigned int index, hb_tag_t *table_tag, hb_codepoint_t *dependent, hb_tag_t *layout_tag, hb_codepoint_t *ligature_set, hb_codepoint_t *context_set, uint8_t *flags) diff --git a/src/hb-depend.cc b/src/hb-depend.cc index cf49a30dd..0d5291ac9 100644 --- a/src/hb-depend.cc +++ b/src/hb-depend.cc @@ -144,37 +144,52 @@ hb_subset_depend_from_face_or_fail (hb_face_t *face) * hb_subset_depend_lookup_glyph: * @depend: depend object * @gid: GID to retrieve dependencies from - * @index: The index of the entry to retrieve - * @entry: (out): Filled with the dependency edge data + * @start_offset: offset of first entry to retrieve + * @entry_count: (inout) (optional): Input = number of entries to fill; output = number + * actually filled. Pass NULL to query total count without filling. + * @entries: (out) (optional) (array length=entry_count): Array to fill with dependency + * edge data. May be NULL if @entry_count is also NULL. * - * Get the values associated with a dependency entry for a glyph. - * Dependencies are indexed sequentially starting from 0. + * Retrieve dependency edges for a glyph. Follows the standard HarfBuzz array-getter + * pattern: always returns the total number of edges for @gid regardless of + * @start_offset or @entry_count. * - * Example: + * Example (iterate all entries): * ```c - * hb_codepoint_t index = 0; - * hb_subset_depend_entry_t entry; - * - * while (hb_subset_depend_lookup_glyph (depend, gid, index++, &entry)) { - * // Process dependency via entry.table_tag, entry.dependent, etc. + * unsigned int total = hb_subset_depend_lookup_glyph (depend, gid, 0, NULL, NULL); + * for (unsigned int i = 0; i < total; i++) { + * hb_subset_depend_entry_t entry; + * unsigned int count = 1; + * hb_subset_depend_lookup_glyph (depend, gid, i, &count, &entry); + * // Process entry.table_tag, entry.dependent, etc. * } * ``` * - * Return value: true if there is such an entry, false otherwise + * Return value: Total number of dependency edges for @gid. * * XSince: REPLACEME **/ -hb_bool_t +unsigned int hb_subset_depend_lookup_glyph (hb_subset_depend_t *depend, hb_codepoint_t gid, - hb_codepoint_t index, - hb_subset_depend_entry_t *entry) + unsigned int start_offset, + unsigned int *entry_count, + hb_subset_depend_entry_t *entries) { - return depend->get_glyph_entry (gid, index, - &entry->table_tag, &entry->dependent, - &entry->layout_tag, &entry->ligature_set_index, - &entry->context_set_index, - (uint8_t *) &entry->flags); + unsigned int total = depend->data.get_glyph_entry_count (gid); + if (entry_count) + { + unsigned int count = hb_min (*entry_count, + start_offset < total ? total - start_offset : 0u); + for (unsigned int i = 0; i < count; i++) + depend->data.get_glyph_entry (gid, start_offset + i, + &entries[i].table_tag, &entries[i].dependent, + &entries[i].layout_tag, &entries[i].ligature_set_index, + &entries[i].context_set_index, + (uint8_t *) &entries[i].flags); + *entry_count = count; + } + return total; } /** diff --git a/src/hb-depend.h b/src/hb-depend.h index 4f17da833..e984fba8a 100644 --- a/src/hb-depend.h +++ b/src/hb-depend.h @@ -37,12 +37,27 @@ HB_BEGIN_DECLS /** * hb_subset_depend_edge_flags_t: * @HB_SUBSET_DEPEND_EDGE_FLAG_NONE: No flags set. - * @HB_SUBSET_DEPEND_EDGE_FLAG_FROM_CONTEXT_POSITION: Edge created from a - * multi-position contextual rule. May cause expected over-approximation. - * @HB_SUBSET_DEPEND_EDGE_FLAG_FROM_NESTED_CONTEXT: Edge from a lookup - * called within another contextual lookup. May over-approximate. + * @HB_SUBSET_DEPEND_EDGE_FLAG_FROM_CONTEXT_POSITION: Edge from a multi-position + * contextual rule (Context or ChainContext with inputCount > 1). Depend + * extraction records edges based on what glyphs could statically be at each + * position according to input coverage or class. However, at runtime lookups + * within the rule are applied sequentially: a lookup at an earlier position + * may transform the glyph at a later position, and two lookups at the same + * position may interact such that one produces a glyph that another + * immediately consumes as an "intermediate". A glyph that matches the static + * coverage may therefore not persist at that position when the rule actually + * fires, so this edge may not trigger during closure. + * @HB_SUBSET_DEPEND_EDGE_FLAG_FROM_NESTED_CONTEXT: Edge from a lookup invoked + * within another contextual lookup. The outer context's requirements are not + * propagated to this edge, so the edge may fire even when those requirements + * are not met. * - * Flags for dependency edges returned by hb_subset_depend_lookup_glyph(). + * Flags on dependency edges returned by hb_subset_depend_lookup_glyph() that + * mark edges which may produce expected over-approximation when computing + * closure via the depend graph, relative to + * hb_ot_layout_lookups_substitute_closure(). These flags help distinguish + * known limitations of static dependency analysis (expected over-approximation) + * from bugs (unexpected over-approximation). * * XSince: REPLACEME */ @@ -97,11 +112,12 @@ typedef struct { HB_EXTERN hb_subset_depend_t * hb_subset_depend_from_face_or_fail (hb_face_t *face); -HB_EXTERN hb_bool_t +HB_EXTERN unsigned int hb_subset_depend_lookup_glyph (hb_subset_depend_t *depend, hb_codepoint_t gid, - hb_codepoint_t index, - hb_subset_depend_entry_t *entry /* OUT */); + unsigned int start_offset, + unsigned int *entry_count, /* IN/OUT */ + hb_subset_depend_entry_t *entries /* OUT */); HB_EXTERN hb_bool_t hb_subset_depend_lookup_set (hb_subset_depend_t *depend, diff --git a/src/hb-depend.hh b/src/hb-depend.hh index 23ed4fbdf..aef790528 100644 --- a/src/hb-depend.hh +++ b/src/hb-depend.hh @@ -55,14 +55,6 @@ struct hb_subset_depend_t bool in_error () const { return !successful; } - bool get_glyph_entry(hb_codepoint_t gid, hb_codepoint_t index, - hb_tag_t *table_tag, hb_codepoint_t *dependent, - hb_tag_t *layout_tag, hb_codepoint_t *ligature_set, - hb_codepoint_t *context_set, uint8_t *flags) - { - return data.get_glyph_entry (gid, index, table_tag, dependent, layout_tag, - ligature_set, context_set, flags); - } const hb_set_t *get_set_from_index (hb_codepoint_t index) { return data.get_set_from_index (index); diff --git a/test/api/test-ot-depend.c b/test/api/test-ot-depend.c index 47058c0ec..5f03e036a 100644 --- a/test/api/test-ot-depend.c +++ b/test/api/test-ot-depend.c @@ -244,12 +244,12 @@ find_dependency (hb_subset_depend_t *depend, hb_tag_t filter_layout_tag = (out_layout_tag && *out_layout_tag != HB_TAG_NONE) ? *out_layout_tag : HB_TAG_NONE; - for (hb_codepoint_t index = 0; ; index++) + unsigned int total = hb_subset_depend_lookup_glyph (depend, source_glyph, 0, NULL, NULL); + for (unsigned int index = 0; index < total; index++) { hb_subset_depend_entry_t entry; - hb_bool_t found = hb_subset_depend_lookup_glyph (depend, source_glyph, index, &entry); - if (!found) - break; + unsigned int count = 1; + hb_subset_depend_lookup_glyph (depend, source_glyph, index, &count, &entry); if (entry.table_tag == expected_table_tag && entry.dependent == target_glyph) { diff --git a/test/fuzzing/hb-depend-closure-parity.cc b/test/fuzzing/hb-depend-closure-parity.cc index 4af031790..a2c60578a 100644 --- a/test/fuzzing/hb-depend-closure-parity.cc +++ b/test/fuzzing/hb-depend-closure-parity.cc @@ -747,11 +747,12 @@ process_edges_for_tables (hb_subset_depend_t *depend, if (gid == debug_gid) DEBUG_MSG_DEPEND("Processing glyph %u", gid); - hb_codepoint_t index = 0; hb_subset_depend_entry_t entry; - - while (hb_subset_depend_lookup_glyph (depend, gid, index++, &entry)) + unsigned int total = hb_subset_depend_lookup_glyph (depend, gid, 0, nullptr, nullptr); + for (unsigned int idx = 0; idx < total; idx++) { + unsigned int count = 1; + hb_subset_depend_lookup_glyph (depend, gid, idx, &count, &entry); hb_tag_t table_tag = entry.table_tag; hb_codepoint_t dependent = entry.dependent; hb_tag_t layout_tag = entry.layout_tag; @@ -1578,13 +1579,7 @@ extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) unsigned num_glyphs = hb_face_get_glyph_count (face); for (unsigned gid = 0; gid < num_glyphs; gid++) { - hb_codepoint_t idx = 0; - hb_subset_depend_entry_t entry; - - while (hb_subset_depend_lookup_glyph (depend, gid, idx++, &entry)) - { - total_edges++; - } + total_edges += hb_subset_depend_lookup_glyph (depend, gid, 0, nullptr, nullptr); } /* Estimate average edges traversed per test (roughly 0.25% of total edges) */ diff --git a/test/fuzzing/hb-depend-fuzzer.cc b/test/fuzzing/hb-depend-fuzzer.cc index 34242a2fc..9a524c25d 100644 --- a/test/fuzzing/hb-depend-fuzzer.cc +++ b/test/fuzzing/hb-depend-fuzzer.cc @@ -24,13 +24,14 @@ extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) for (unsigned int gid = 0; gid < num_glyphs && gid < 100; gid++) { - hb_codepoint_t index = 0; - hb_subset_depend_entry_t entry; - // Iterate through up to 10 entries per glyph - while (index < 10 && - hb_subset_depend_lookup_glyph (depend, gid, index++, &entry)) + unsigned int total = hb_subset_depend_lookup_glyph (depend, gid, 0, nullptr, nullptr); + unsigned int limit = total < 10u ? total : 10u; + for (unsigned int index = 0; index < limit; index++) { + hb_subset_depend_entry_t entry; + unsigned int count = 1; + hb_subset_depend_lookup_glyph (depend, gid, index, &count, &entry); // Access basic edge data (void) entry.table_tag; (void) entry.dependent;