| 1 | use crate::hb::ot_layout_gsubgpos::OT::hb_ot_apply_context_t; |
| 2 | use crate::hb::ot_layout_gsubgpos::{Apply, WouldApply, WouldApplyContext}; |
| 3 | use ttf_parser::gsub::SingleSubstitution; |
| 4 | use ttf_parser::GlyphId; |
| 5 | |
| 6 | // SingleSubstFormat1::would_apply |
| 7 | // SingleSubstFormat2::would_apply |
| 8 | impl WouldApply for SingleSubstitution<'_> { |
| 9 | fn would_apply(&self, ctx: &WouldApplyContext) -> bool { |
| 10 | ctx.glyphs.len() == 1 && self.coverage().get(glyph:ctx.glyphs[0]).is_some() |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | // SingleSubstFormat1::apply |
| 15 | // SingleSubstFormat2::apply |
| 16 | impl Apply for SingleSubstitution<'_> { |
| 17 | fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> { |
| 18 | let glyph: GlyphId = ctx.buffer.cur(0).as_glyph(); |
| 19 | let subst: GlyphId = match *self { |
| 20 | Self::Format1 { coverage: Coverage<'_>, delta: i16 } => { |
| 21 | coverage.get(glyph)?; |
| 22 | // According to the Adobe Annotated OpenType Suite, result is always |
| 23 | // limited to 16bit, so we explicitly want to truncate. |
| 24 | GlyphId((i32::from(glyph.0) + i32::from(delta)) as u16) |
| 25 | } |
| 26 | Self::Format2 { |
| 27 | coverage: Coverage<'_>, |
| 28 | substitutes: LazyArray16<'_, GlyphId>, |
| 29 | } => { |
| 30 | let index: u16 = coverage.get(glyph)?; |
| 31 | substitutes.get(index)? |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | ctx.replace_glyph(glyph_id:subst); |
| 36 | Some(()) |
| 37 | } |
| 38 | } |
| 39 | |