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