diff --git a/src/OT/Layout/GSUB/LigatureSet.hh b/src/OT/Layout/GSUB/LigatureSet.hh index 60c262b60..40fd6940b 100644 --- a/src/OT/Layout/GSUB/LigatureSet.hh +++ b/src/OT/Layout/GSUB/LigatureSet.hh @@ -81,6 +81,7 @@ struct LigatureSet bool would_apply (hb_would_apply_context_t *c) const { + if (unlikely (!c->charge_ops (ligature.len))) return false; return + hb_iter (ligature) | hb_map (hb_add (this)) diff --git a/src/OT/Layout/GSUB/SubstLookup.hh b/src/OT/Layout/GSUB/SubstLookup.hh index f05eabab4..cb91b4ab2 100644 --- a/src/OT/Layout/GSUB/SubstLookup.hh +++ b/src/OT/Layout/GSUB/SubstLookup.hh @@ -101,6 +101,7 @@ struct SubstLookup : Lookup { if (unlikely (!c->len)) return false; if (!accel->may_have (c->glyphs[0])) return false; + if (unlikely (!c->charge_ops (1 + get_subtable_count ()))) return false; return dispatch (c); } diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index 2d3d9d851..1890c7499 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -291,19 +291,39 @@ struct hb_would_apply_context_t : static return_t default_return_value () { return false; } bool stop_sublookup_iteration (return_t r) const { return r; } + /* Subtable, rule and ligature offsets can alias each other, so a font can + * declare far more of them than it has bytes. When running as part of + * shaping, charge walking them to the buffer's work budget, the way + * hb_ot_apply_context_t does. */ + bool charge_ops (unsigned int count) + { + if (!buffer) return true; + if (unlikely (buffer->max_ops < (int) count)) + { + buffer->max_ops = -1; + buffer->successful = false; + return false; + } + buffer->max_ops -= (int) count; + return true; + } + hb_face_t *face; const hb_codepoint_t *glyphs; unsigned int len; bool zero_context; + hb_buffer_t *buffer; /* nullptr when called from the public API. */ hb_would_apply_context_t (hb_face_t *face_, const hb_codepoint_t *glyphs_, unsigned int len_, - bool zero_context_) : + bool zero_context_, + hb_buffer_t *buffer_ = nullptr) : face (face_), glyphs (glyphs_), len (len_), - zero_context (zero_context_) {} + zero_context (zero_context_), + buffer (buffer_) {} }; struct hb_collect_glyphs_context_t : @@ -3315,6 +3335,7 @@ struct RuleSetOf bool would_apply (hb_would_apply_context_t *c, const ContextApplyLookupContext &lookup_context) const { + if (unlikely (!c->charge_ops (rule.len))) return false; return + hb_iter (rule) | hb_map (hb_add (this)) @@ -4918,6 +4939,7 @@ struct ChainRuleSetOf bool would_apply (hb_would_apply_context_t *c, const ChainContextApplyLookupContext &lookup_context) const { + if (unlikely (!c->charge_ops (rule.len))) return false; return + hb_iter (rule) | hb_map (hb_add (this)) diff --git a/src/hb-ot-layout.cc b/src/hb-ot-layout.cc index 22e41e24a..8f34090a9 100644 --- a/src/hb-ot-layout.cc +++ b/src/hb-ot-layout.cc @@ -1585,10 +1585,25 @@ hb_ot_layout_lookup_would_substitute (hb_face_t *face, const hb_codepoint_t *glyphs, unsigned int glyphs_length, hb_bool_t zero_context) +{ + return hb_ot_layout_lookup_would_substitute_with_buffer (face, lookup_index, + glyphs, glyphs_length, + (bool) zero_context, + nullptr); +} + +/* Same, but charges the work done to buffer's max_ops when buffer is not nullptr. */ +bool +hb_ot_layout_lookup_would_substitute_with_buffer (hb_face_t *face, + unsigned int lookup_index, + const hb_codepoint_t *glyphs, + unsigned int glyphs_length, + bool zero_context, + hb_buffer_t *buffer) { auto &gsub = face->table.GSUB; if (unlikely (lookup_index >= gsub->lookup_count)) return false; - OT::hb_would_apply_context_t c (face, glyphs, glyphs_length, (bool) zero_context); + OT::hb_would_apply_context_t c (face, glyphs, glyphs_length, zero_context, buffer); const OT::SubstLookup& l = gsub->table->get_lookup (lookup_index); auto *accel = gsub->get_accel (lookup_index); diff --git a/src/hb-ot-layout.hh b/src/hb-ot-layout.hh index c72c157e7..38ad88b3e 100644 --- a/src/hb-ot-layout.hh +++ b/src/hb-ot-layout.hh @@ -68,6 +68,14 @@ hb_ot_layout_table_find_feature (hb_face_t *face, hb_tag_t feature_tag, unsigned int *feature_index); +HB_INTERNAL bool +hb_ot_layout_lookup_would_substitute_with_buffer (hb_face_t *face, + unsigned int lookup_index, + const hb_codepoint_t *glyphs, + unsigned int glyphs_length, + bool zero_context, + hb_buffer_t *buffer); + /* * GDEF diff --git a/src/hb-ot-shaper-indic.cc b/src/hb-ot-shaper-indic.cc index 8501629d7..b935ced6e 100644 --- a/src/hb-ot-shaper-indic.cc +++ b/src/hb-ot-shaper-indic.cc @@ -98,10 +98,14 @@ struct hb_indic_would_substitute_feature_t bool would_substitute (const hb_codepoint_t *glyphs, unsigned int glyphs_count, - hb_face_t *face) const + hb_face_t *face, + hb_buffer_t *buffer) const { + if (unlikely (!buffer->successful)) return false; for (const auto &lookup : lookups) - if (hb_ot_layout_lookup_would_substitute (face, lookup.index, glyphs, glyphs_count, zero_context)) + if (hb_ot_layout_lookup_would_substitute_with_buffer (face, lookup.index, + glyphs, glyphs_count, + zero_context, buffer)) return true; return false; } @@ -357,7 +361,8 @@ static indic_position_t consonant_position_from_face (const indic_shape_plan_t *indic_plan, const hb_codepoint_t consonant, const hb_codepoint_t virama, - hb_face_t *face) + hb_face_t *face, + hb_buffer_t *buffer) { /* For old-spec, the order of glyphs is Consonant,Virama, * whereas for new-spec, it's Virama,Consonant. However, @@ -374,16 +379,16 @@ consonant_position_from_face (const indic_shape_plan_t *indic_plan, * https://github.com/harfbuzz/harfbuzz/issues/1587 */ hb_codepoint_t glyphs[3] = {virama, consonant, virama}; - if (indic_plan->blwf.would_substitute (glyphs , 2, face) || - indic_plan->blwf.would_substitute (glyphs+1, 2, face) || - indic_plan->vatu.would_substitute (glyphs , 2, face) || - indic_plan->vatu.would_substitute (glyphs+1, 2, face)) + if (indic_plan->blwf.would_substitute (glyphs , 2, face, buffer) || + indic_plan->blwf.would_substitute (glyphs+1, 2, face, buffer) || + indic_plan->vatu.would_substitute (glyphs , 2, face, buffer) || + indic_plan->vatu.would_substitute (glyphs+1, 2, face, buffer)) return POS_BELOW_C; - if (indic_plan->pstf.would_substitute (glyphs , 2, face) || - indic_plan->pstf.would_substitute (glyphs+1, 2, face)) + if (indic_plan->pstf.would_substitute (glyphs , 2, face, buffer) || + indic_plan->pstf.would_substitute (glyphs+1, 2, face, buffer)) return POS_POST_C; - if (indic_plan->pref.would_substitute (glyphs , 2, face) || - indic_plan->pref.would_substitute (glyphs+1, 2, face)) + if (indic_plan->pref.would_substitute (glyphs , 2, face, buffer) || + indic_plan->pref.would_substitute (glyphs+1, 2, face, buffer)) return POS_POST_C; return POS_BASE_C; } @@ -445,7 +450,7 @@ update_consonant_positions_indic (const hb_ot_shape_plan_t *plan, if (info[i].indic_position() == POS_BASE_C) { hb_codepoint_t consonant = info[i].codepoint; - info[i].indic_position() = consonant_position_from_face (indic_plan, consonant, virama, face); + info[i].indic_position() = consonant_position_from_face (indic_plan, consonant, virama, face, buffer); } } } @@ -511,9 +516,9 @@ initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan, info[start + 1].codepoint, indic_plan->config->reph_mode == REPH_MODE_EXPLICIT ? info[start + 2].codepoint : 0}; - if (indic_plan->rphf.would_substitute (glyphs, 2, face) || + if (indic_plan->rphf.would_substitute (glyphs, 2, face, buffer) || (indic_plan->config->reph_mode == REPH_MODE_EXPLICIT && - indic_plan->rphf.would_substitute (glyphs, 3, face))) + indic_plan->rphf.would_substitute (glyphs, 3, face, buffer))) { limit += 2; while (limit < end && is_joiner (info[limit])) @@ -898,7 +903,7 @@ initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan, hb_codepoint_t glyphs[2]; for (unsigned int j = 0; j < pref_len; j++) glyphs[j] = info[i + j].codepoint; - if (indic_plan->pref.would_substitute (glyphs, pref_len, face)) + if (indic_plan->pref.would_substitute (glyphs, pref_len, face, buffer)) { for (unsigned int j = 0; j < pref_len; j++) info[i++].mask |= indic_plan->mask_array[INDIC_PREF];