Skip to main content

harfrust/hb/ot/gsub/
single.rs

1use crate::hb::ot_layout_gsubgpos::OT::hb_ot_apply_context_t;
2use crate::hb::ot_layout_gsubgpos::{Apply, WouldApply, WouldApplyContext};
3use read_fonts::tables::gsub::{SingleSubstFormat1, SingleSubstFormat2};
4
5impl WouldApply for SingleSubstFormat1<'_> {
6    fn would_apply(&self, ctx: &WouldApplyContext) -> bool {
7        let gid = ctx.glyphs[0];
8        ctx.glyphs.len() == 1 && self.coverage().is_ok_and(|cov| cov.get(gid).is_some())
9    }
10}
11
12impl Apply for SingleSubstFormat1<'_> {
13    fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
14        let glyph = ctx.buffer.cur(0).as_glyph();
15        self.coverage().ok()?.get(glyph)?;
16        let subst = (glyph.to_u32() as i32 + self.delta_glyph_id() as i32) as u16;
17        ctx.replace_glyph(subst.into());
18        Some(())
19    }
20}
21
22impl WouldApply for SingleSubstFormat2<'_> {
23    fn would_apply(&self, ctx: &WouldApplyContext) -> bool {
24        ctx.glyphs.len() == 1
25            && self
26                .coverage()
27                .is_ok_and(|cov| cov.get(ctx.glyphs[0]).is_some())
28    }
29}
30
31impl Apply for SingleSubstFormat2<'_> {
32    fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
33        let glyph = ctx.buffer.cur(0).as_glyph();
34        let index = self.coverage().ok()?.get(glyph)? as usize;
35        let subst = self.substitute_glyph_ids().get(index)?.get().to_u16();
36        ctx.replace_glyph(subst.into());
37        Some(())
38    }
39}