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

1use crate::hb::ot_layout_gsubgpos::OT::hb_ot_apply_context_t;
2use crate::hb::ot_layout_gsubgpos::{Apply, WouldApply, WouldApplyContext};
3use ttf_parser::gsub::SingleSubstitution;
4use ttf_parser::GlyphId;
5
6// SingleSubstFormat1::would_apply
7// SingleSubstFormat2::would_apply
8impl WouldApply for SingleSubstitution<'_> {
9    fn would_apply(&self, ctx: &WouldApplyContext) -> bool {
10        ctx.glyphs.len() == 1 && self.coverage().get(ctx.glyphs[0]).is_some()
11    }
12}
13
14// SingleSubstFormat1::apply
15// SingleSubstFormat2::apply
16impl Apply for SingleSubstitution<'_> {
17    fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
18        let glyph = ctx.buffer.cur(0).as_glyph();
19        let subst = match *self {
20            Self::Format1 { coverage, delta } => {
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,
28                substitutes,
29            } => {
30                let index = coverage.get(glyph)?;
31                substitutes.get(index)?
32            }
33        };
34
35        ctx.replace_glyph(subst);
36        Some(())
37    }
38}