rustybuzz/hb/
ot_layout_gsub_table.rs
1use ttf_parser::gsub::*;
2use ttf_parser::opentype_layout::LookupIndex;
3use ttf_parser::GlyphId;
4
5use super::buffer::hb_buffer_t;
6use super::hb_font_t;
7use super::ot_layout::*;
8use super::ot_layout_common::{SubstLookup, SubstitutionTable};
9use super::ot_layout_gsubgpos::*;
10use super::ot_shape_plan::hb_ot_shape_plan_t;
11use OT::hb_ot_apply_context_t;
12
13pub fn substitute(plan: &hb_ot_shape_plan_t, face: &hb_font_t, buffer: &mut hb_buffer_t) {
14 apply_layout_table(plan, face, buffer, face.gsub.as_ref());
15}
16
17impl<'a> LayoutTable for SubstitutionTable<'a> {
18 const INDEX: TableIndex = TableIndex::GSUB;
19 const IN_PLACE: bool = false;
20
21 type Lookup = SubstLookup<'a>;
22
23 fn get_lookup(&self, index: LookupIndex) -> Option<&Self::Lookup> {
24 self.lookups.get(usize::from(index))
25 }
26}
27
28impl LayoutLookup for SubstLookup<'_> {
29 fn props(&self) -> u32 {
30 self.props
31 }
32
33 fn is_reverse(&self) -> bool {
34 self.reverse
35 }
36
37 fn covers(&self, glyph: GlyphId) -> bool {
38 self.coverage.contains(glyph)
39 }
40}
41
42impl WouldApply for SubstLookup<'_> {
43 fn would_apply(&self, ctx: &WouldApplyContext) -> bool {
44 self.covers(ctx.glyphs[0])
45 && self
46 .subtables
47 .iter()
48 .any(|subtable| subtable.would_apply(ctx))
49 }
50}
51
52impl Apply for SubstLookup<'_> {
53 fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
54 if self.covers(ctx.buffer.cur(0).as_glyph()) {
55 for subtable in &self.subtables {
56 if subtable.apply(ctx).is_some() {
57 return Some(());
58 }
59 }
60 }
61
62 None
63 }
64}
65
66impl WouldApply for SubstitutionSubtable<'_> {
67 fn would_apply(&self, ctx: &WouldApplyContext) -> bool {
68 match self {
69 Self::Single(t) => t.would_apply(ctx),
70 Self::Multiple(t) => t.would_apply(ctx),
71 Self::Alternate(t) => t.would_apply(ctx),
72 Self::Ligature(t) => t.would_apply(ctx),
73 Self::Context(t) => t.would_apply(ctx),
74 Self::ChainContext(t) => t.would_apply(ctx),
75 Self::ReverseChainSingle(t) => t.would_apply(ctx),
76 }
77 }
78}
79
80impl Apply for SubstitutionSubtable<'_> {
81 fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
82 match self {
83 Self::Single(t) => t.apply(ctx),
84 Self::Multiple(t) => t.apply(ctx),
85 Self::Alternate(t) => t.apply(ctx),
86 Self::Ligature(t) => t.apply(ctx),
87 Self::Context(t) => t.apply(ctx),
88 Self::ChainContext(t) => t.apply(ctx),
89 Self::ReverseChainSingle(t) => t.apply(ctx),
90 }
91 }
92}