diff --git a/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh b/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh index a0b47a7ba..de4f4bc93 100644 --- a/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh +++ b/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh @@ -75,11 +75,56 @@ struct ReverseChainSingleSubstFormat1 const auto &lookahead = StructAfter (backtrack); const auto &substitute = StructAfter (lookahead); + /* Build context set from backtrack and lookahead coverages. + * ReverseChainSingleSubst has contextual requirements - the substitution + * only fires when backtrack and lookahead glyphs are present. */ + hb_set_t context_glyphs; + + /* Add glyphs from backtrack coverages */ + unsigned int count = backtrack.len; + for (unsigned int i = 0; i < count; i++) + { + hb_set_t back_glyphs; + (this+backtrack[i]).intersect_set (*c->glyphs, back_glyphs); + if (back_glyphs.get_population () == 1) + context_glyphs.add (back_glyphs.get_min ()); + else if (back_glyphs.get_population () > 1) + { + hb_codepoint_t set_idx = c->depend_data->find_or_create_set (back_glyphs); + context_glyphs.add (0x80000000 | set_idx); + } + } + + /* Add glyphs from lookahead coverages */ + count = lookahead.len; + for (unsigned int i = 0; i < count; i++) + { + hb_set_t look_glyphs; + (this+lookahead[i]).intersect_set (*c->glyphs, look_glyphs); + if (look_glyphs.get_population () == 1) + context_glyphs.add (look_glyphs.get_min ()); + else if (look_glyphs.get_population () > 1) + { + hb_codepoint_t set_idx = c->depend_data->find_or_create_set (look_glyphs); + context_glyphs.add (0x80000000 | set_idx); + } + } + + /* Allocate context set and save/restore around edge creation */ + hb_codepoint_t context_set_idx = context_glyphs.is_empty () + ? HB_CODEPOINT_INVALID + : c->depend_data->find_or_create_set (context_glyphs); + + hb_codepoint_t saved_context = c->depend_data->current_context_set_index; + c->depend_data->current_context_set_index = context_set_idx; + + hb_zip (this+coverage, substitute) | hb_filter (c->parent_active_glyphs (), hb_first) | hb_apply ([&] (const hb_codepoint_pair_t &_) { c->depend_data->add_gsub_lookup (_.first, c->lookup_index, _.second); }) ; + c->depend_data->current_context_set_index = saved_context; + return true; } #endif diff --git a/src/hb-depend-data.hh b/src/hb-depend-data.hh index 5591303a8..4ecd4b8fb 100644 --- a/src/hb-depend-data.hh +++ b/src/hb-depend-data.hh @@ -37,6 +37,7 @@ * Edge flags for hb_depend_data_record_t */ #define HB_DEPEND_EDGE_FLAG_FROM_CONTEXT_POSITION 0x01 +#define HB_DEPEND_EDGE_FLAG_FROM_NESTED_CONTEXT 0x02 /** * hb_depend_data_record_t: diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index 23f2a7e16..bb5a0f1bb 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -1943,17 +1943,33 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c, ? HB_CODEPOINT_INVALID : c->depend_data->find_or_create_set (position_context); + /* Save outer context to restore later - this preserves the outer context + * across all rules when a contextual lookup calls another contextual lookup. */ + hb_codepoint_t saved_context_set_index = c->depend_data->current_context_set_index; + uint8_t saved_edge_flags = c->depend_data->current_edge_flags; + + /* Detect nested contextual: if we already have a context set, we're inside + * an outer contextual rule that called this one. The outer context will be + * lost, so edges created here may over-approximate. */ + bool nested_contextual = (saved_context_set_index != HB_CODEPOINT_INVALID); + /* Set for this position's edges */ c->depend_data->current_context_set_index = context_set_idx; - /* Mark edges from non-zero positions in multi-position rules as potentially - * consuming intermediate glyphs. These edges are risky because their source - * glyphs are contextual requirements that might be produced elsewhere and - * immediately consumed here. */ + /* Mark edges in multi-position rules as potentially over-approximate. + * For seqIndex > 0: the source glyph might be produced by earlier lookups. + * For seqIndex == 0 with inputCount > 1: context glyphs from other input + * positions might be produced, satisfying the context check but not the + * actual positional requirements. + * In both cases, the edge is "risky" - the rule might not fire even when + * all required glyphs are in the closure. */ c->depend_data->current_edge_flags = 0; - if (seqIndex > 0 && inputCount > 1) { + if (inputCount > 1) { c->depend_data->current_edge_flags |= HB_DEPEND_EDGE_FLAG_FROM_CONTEXT_POSITION; } + if (nested_contextual) { + c->depend_data->current_edge_flags |= HB_DEPEND_EDGE_FLAG_FROM_NESTED_CONTEXT; + } bool has_pos_glyphs = false; @@ -2006,9 +2022,10 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c, c->recurse (lookupRecord[i].lookupListIndex, &covered_seq_indicies, seqIndex, endIndex); c->pop_cur_done_glyphs (); - /* Clear context and flags after this position */ - c->depend_data->current_context_set_index = HB_CODEPOINT_INVALID; - c->depend_data->current_edge_flags = 0; + /* Restore outer context after this position - this ensures the outer context + * persists across multiple rules when processing nested contextual lookups. */ + c->depend_data->current_context_set_index = saved_context_set_index; + c->depend_data->current_edge_flags = saved_edge_flags; } } #endif @@ -3525,21 +3542,23 @@ static inline bool chain_context_intersects (const hb_set_t *glyphs, const HBUINT lookahead[], ChainContextClosureLookupContext &lookup_context) { - return array_is_subset_of (glyphs, + bool bt_ok = array_is_subset_of (glyphs, backtrackCount, backtrack, lookup_context.funcs.intersects, lookup_context.intersects_data[0], - lookup_context.intersects_cache[0]) - && array_is_subset_of (glyphs, + lookup_context.intersects_cache[0]); + bool in_ok = array_is_subset_of (glyphs, inputCount ? inputCount - 1 : 0, input, lookup_context.funcs.intersects, lookup_context.intersects_data[1], - lookup_context.intersects_cache[1]) - && array_is_subset_of (glyphs, + lookup_context.intersects_cache[1]); + bool la_ok = array_is_subset_of (glyphs, lookaheadCount, lookahead, lookup_context.funcs.intersects, lookup_context.intersects_data[2], lookup_context.intersects_cache[2]); + + return bt_ok && in_ok && la_ok; } template diff --git a/test/fuzzing/hb-depend-closure-parity.cc b/test/fuzzing/hb-depend-closure-parity.cc index b5d2229aa..d0a18a24d 100644 --- a/test/fuzzing/hb-depend-closure-parity.cc +++ b/test/fuzzing/hb-depend-closure-parity.cc @@ -817,8 +817,9 @@ process_edges_for_tables (hb_depend_t *depend, /* Non-ligature dependency: add immediately */ if (!hb_set_has (glyphs, dependent)) { - /* Track if we hit a flagged edge (from contextual position) */ - if (hit_flagged_edge && (flags & 0x01)) + /* Track if we hit a flagged edge (from contextual position or nested context) + * Flag values: 0x01 = FROM_CONTEXT_POSITION, 0x02 = FROM_NESTED_CONTEXT */ + if (hit_flagged_edge && (flags & 0x03)) *hit_flagged_edge = true; if (dependent == debug_gid) @@ -910,6 +911,7 @@ compute_depend_closure (hb_depend_t *depend, hb_set_t *glyphs, bool skip_gsub, while (hb_set_next (deferred_ligatures, &lig_glyph)) { bool any_set_satisfied = false; + uint8_t satisfied_edge_flags = 0; /* Track flags from the edge that satisfied */ /* Scan through glyphs in closure to find ligature sets */ hb_codepoint_t src_glyph = HB_SET_VALUE_INVALID; @@ -928,6 +930,10 @@ compute_depend_closure (hb_depend_t *depend, hb_set_t *glyphs, bool skip_gsub, { if (dep == lig_glyph && ligset != HB_CODEPOINT_INVALID && ttag == HB_OT_TAG_GSUB) { + /* Must also check feature is active (same as non-ligature edge processing) */ + if (active_features && !hb_set_has (active_features, ltag)) + continue; + /* Check if this ligature set is satisfied */ hb_set_clear (ligature_set_glyphs); hb_depend_get_set_from_index (depend, ligset, ligature_set_glyphs); @@ -935,6 +941,7 @@ compute_depend_closure (hb_depend_t *depend, hb_set_t *glyphs, bool skip_gsub, if (hb_set_is_subset (ligature_set_glyphs, glyphs)) { any_set_satisfied = true; + satisfied_edge_flags = fls; /* Remember flags from this edge */ break; } } @@ -964,8 +971,13 @@ compute_depend_closure (hb_depend_t *depend, hb_set_t *glyphs, bool skip_gsub, if (!skip_ligature && !hb_set_has (glyphs, lig_glyph)) { + /* Check if this ligature edge has flags (from contextual position or nested context) */ + if (hit_flagged_edge && (satisfied_edge_flags & 0x03)) + *hit_flagged_edge = true; + if (trace_closure) - DEBUG_MSG_DEPEND("Adding GSUB ligature %u (components satisfied)", lig_glyph); + DEBUG_MSG_DEPEND("Adding GSUB ligature %u (components satisfied, flags=0x%02x)", + lig_glyph, satisfied_edge_flags); hb_set_add (glyphs, lig_glyph); lig_made_progress = true; } @@ -1016,12 +1028,14 @@ compute_depend_closure (hb_depend_t *depend, hb_set_t *glyphs, bool skip_gsub, hb_set_get_population (glyphs)); } - /* Stage 4: glyf/CFF closure */ + /* Stage 4: glyf/CFF closure + * Note: cmap edges (UVS) are NOT included here because: + * 1. Subset doesn't follow UVS for glyph-based closure + * 2. UVS is about Unicode→glyph mapping, not glyph→glyph rendering deps */ { hb_set_t *glyf_filter = hb_set_create (); hb_set_add (glyf_filter, HB_TAG('g','l','y','f')); hb_set_add (glyf_filter, HB_TAG('C','F','F',' ')); - hb_set_add (glyf_filter, HB_TAG('c','m','a','p')); /* Include cmap for UVS */ hb_set_union (to_process, glyphs); process_edges_for_tables (depend, glyphs, to_process, deferred_ligatures,