rustybuzz/hb/ot/layout/GSUB/
reverse_chain_single_subst.rs

1use crate::hb::ot_layout::MAX_NESTING_LEVEL;
2use crate::hb::ot_layout_gsubgpos::OT::hb_ot_apply_context_t;
3use crate::hb::ot_layout_gsubgpos::{
4    match_backtrack, match_lookahead, Apply, WouldApply, WouldApplyContext,
5};
6use ttf_parser::gsub::ReverseChainSingleSubstitution;
7
8// ReverseChainSingleSubstFormat1::would_apply
9impl WouldApply for ReverseChainSingleSubstitution<'_> {
10    fn would_apply(&self, ctx: &WouldApplyContext) -> bool {
11        ctx.glyphs.len() == 1 && self.coverage.get(ctx.glyphs[0]).is_some()
12    }
13}
14
15// ReverseChainSingleSubstFormat1::apply
16impl Apply for ReverseChainSingleSubstitution<'_> {
17    fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
18        // No chaining to this type.
19        if ctx.nesting_level_left != MAX_NESTING_LEVEL {
20            return None;
21        }
22
23        let glyph = ctx.buffer.cur(0).as_glyph();
24        let index = self.coverage.get(glyph)?;
25        if index >= self.substitutes.len() {
26            return None;
27        }
28
29        let subst = self.substitutes.get(index)?;
30
31        let f1 = |glyph, num_items| {
32            let index = self.backtrack_coverages.len() - num_items;
33            let value = self.backtrack_coverages.get(index).unwrap();
34            value.contains(glyph)
35        };
36
37        let f2 = |glyph, num_items| {
38            let index = self.lookahead_coverages.len() - num_items;
39            let value = self.lookahead_coverages.get(index).unwrap();
40            value.contains(glyph)
41        };
42
43        let mut start_index = 0;
44        let mut end_index = 0;
45
46        if match_backtrack(ctx, self.backtrack_coverages.len(), &f1, &mut start_index) {
47            if match_lookahead(
48                ctx,
49                self.lookahead_coverages.len(),
50                &f2,
51                ctx.buffer.idx + 1,
52                &mut end_index,
53            ) {
54                ctx.buffer
55                    .unsafe_to_break_from_outbuffer(Some(start_index), Some(end_index));
56                ctx.replace_glyph_inplace(subst);
57
58                // Note: We DON'T decrease buffer.idx.  The main loop does it
59                // for us.  This is useful for preventing surprises if someone
60                // calls us through a Context lookup.
61                return Some(());
62            }
63        }
64
65        ctx.buffer
66            .unsafe_to_concat_from_outbuffer(Some(start_index), Some(end_index));
67        return None;
68    }
69}