1 | use crate::hb::ot_layout_gsubgpos::Apply; |
2 | use crate::hb::ot_layout_gsubgpos::OT::hb_ot_apply_context_t; |
3 | use crate::hb::ot_map::hb_ot_map_t; |
4 | use core::convert::TryFrom; |
5 | use ttf_parser::gsub::AlternateSet; |
6 | |
7 | impl Apply for AlternateSet<'_> { |
8 | fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> { |
9 | let len = self.alternates.len(); |
10 | if len == 0 { |
11 | return None; |
12 | } |
13 | |
14 | let glyph_mask = ctx.buffer.cur(0).mask; |
15 | |
16 | // Note: This breaks badly if two features enabled this lookup together. |
17 | let shift = ctx.lookup_mask().trailing_zeros(); |
18 | let mut alt_index = (ctx.lookup_mask() & glyph_mask) >> shift; |
19 | |
20 | // If alt_index is MAX_VALUE, randomize feature if it is the rand feature. |
21 | if alt_index == hb_ot_map_t::MAX_VALUE && ctx.random { |
22 | // Maybe we can do better than unsafe-to-break all; but since we are |
23 | // changing random state, it would be hard to track that. Good 'nough. |
24 | ctx.buffer.unsafe_to_break(Some(0), Some(ctx.buffer.len)); |
25 | alt_index = ctx.random_number() % u32::from(len) + 1; |
26 | } |
27 | |
28 | let idx = u16::try_from(alt_index).ok()?.checked_sub(1)?; |
29 | ctx.replace_glyph(self.alternates.get(idx)?); |
30 | |
31 | Some(()) |
32 | } |
33 | } |
34 | |