Skip to main content

harfrust/ot/gpos/
mod.rs

1//! OpenType GPOS lookups.
2
3use crate::buffer::*;
4use crate::hint::Likely;
5use crate::ot::layout::*;
6use crate::plan::ShapePlan;
7use crate::Direction;
8use crate::ShaperFont;
9
10pub fn position(plan: &ShapePlan, font: &ShaperFont<'_, '_>, buffer: &mut Buffer) {
11    #[cfg(feature = "tracing")]
12    let tag = crate::ot::map::MessageTag(plan.ot_map.chosen_script(LayoutTableKind::Gpos));
13    if !buffer_message!(buffer, font, "start table GPOS script tag '{tag:#}'") {
14        return;
15    }
16    let table = font.layout().ot.gpos();
17    apply_layout_table(plan, font, buffer, table.as_ref());
18    buffer_message!(buffer, font, "end table GPOS script tag '{tag:#}'");
19}
20
21pub mod attach_type {
22    pub const MARK: u8 = 1;
23    pub const CURSIVE: u8 = 2;
24}
25
26/// See <https://github.com/harfbuzz/harfbuzz/blob/7d936359a27abb2d7cb14ecc102463bb15c11843/src/OT/Layout/GPOS/GPOS.hh#L75>
27fn propagate_attachment_offsets(
28    pos: &mut [GlyphPosition],
29    len: usize,
30    i: usize,
31    direction: Direction,
32    nesting_level: usize,
33) -> Option<()> {
34    // Adjusts offsets of attached glyphs (both cursive and mark) to accumulate
35    // offset of glyph they are attached to.
36    let this = pos.get_mut(i).likely()?;
37    let chain = this.attach_chain();
38    let kind = this.attach_type();
39
40    this.set_attach_chain(0);
41
42    let j = (i as isize + isize::from(chain)) as usize;
43    if j >= len {
44        return Some(());
45    }
46
47    if nesting_level == 0 {
48        return Some(());
49    }
50
51    if pos.get(j).likely()?.attach_chain() != 0 {
52        // A failure further up the chain still accumulates this glyph below.
53        propagate_attachment_offsets(pos, len, j, direction, nesting_level - 1);
54    }
55
56    // Read only after the recursion, which updates the parent's offsets.
57    let parent = *pos.get(j).likely()?;
58    let mut this = *pos.get(i).likely()?;
59    match kind {
60        attach_type::MARK => {
61            if direction.is_horizontal() {
62                this.x_offset = this.x_offset.saturating_add(parent.x_offset);
63            } else {
64                this.y_offset = this.y_offset.saturating_add(parent.y_offset);
65            }
66
67            // i is the position of the mark; j is the base.
68            if j < i {
69                // This is the common case: mark follows base.
70                // And currently the only way in OpenType.
71                if direction.is_forward() {
72                    for k in pos.get(j..i).likely()? {
73                        this.x_offset = this.x_offset.saturating_sub(k.x_advance);
74                        this.y_offset = this.y_offset.saturating_sub(k.y_advance);
75                    }
76                } else {
77                    for k in pos.get(j + 1..i + 1).likely()? {
78                        this.x_offset = this.x_offset.saturating_add(k.x_advance);
79                        this.y_offset = this.y_offset.saturating_add(k.y_advance);
80                    }
81                }
82            } else {
83                // This can happen with `kerx`: a mark attaching
84                // to a base after it in the logical order.
85                if direction.is_forward() {
86                    for k in pos.get(i..j).likely()? {
87                        this.x_offset = this.x_offset.saturating_add(k.x_advance);
88                        this.y_offset = this.y_offset.saturating_add(k.y_advance);
89                    }
90                } else {
91                    for k in pos.get(i + 1..j + 1).likely()? {
92                        this.x_offset = this.x_offset.saturating_sub(k.x_advance);
93                        this.y_offset = this.y_offset.saturating_sub(k.y_advance);
94                    }
95                }
96            }
97        }
98        attach_type::CURSIVE => {
99            if direction.is_horizontal() {
100                this.y_offset = this.y_offset.saturating_add(parent.y_offset);
101            } else {
102                this.x_offset = this.x_offset.saturating_add(parent.x_offset);
103            }
104        }
105        _ => {}
106    }
107    *pos.get_mut(i).likely()? = this;
108    Some(())
109}
110
111pub fn position_start(buffer: &mut Buffer) {
112    let len = buffer.len;
113    for pos in buffer.pos.get_mut(..len).unwrap_or_default() {
114        pos.set_attach_chain(0);
115        pos.set_attach_type(0);
116    }
117}
118
119pub fn position_finish_advances(_: &mut Buffer) {
120    //buffer.assert_gsubgpos_vars();
121}
122
123pub fn position_finish_offsets(buffer: &mut Buffer) {
124    buffer.assert_gsubgpos_vars();
125
126    let len = buffer.len;
127    let direction = buffer.direction;
128
129    // Handle attachments
130    if buffer.scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT != 0 {
131        let pos = &mut buffer.pos;
132        // https://github.com/harfbuzz/harfbuzz/issues/5514
133        if direction.is_forward() {
134            for i in 0..len {
135                if pos.get(i).is_some_and(|p| p.attach_chain() != 0) {
136                    propagate_attachment_offsets(pos, len, i, direction, MAX_NESTING_LEVEL);
137                }
138            }
139        } else {
140            for i in (0..len).rev() {
141                if pos.get(i).is_some_and(|p| p.attach_chain() != 0) {
142                    propagate_attachment_offsets(pos, len, i, direction, MAX_NESTING_LEVEL);
143                }
144            }
145        }
146    }
147}
148
149#[cfg(test)]
150mod tests {
151    use super::*;
152
153    #[test]
154    fn propagate_attachment_offsets_saturates_deep_mark_chains() {
155        let len = MAX_NESTING_LEVEL + 2;
156        let mut pos = vec![GlyphPosition::default(); len];
157
158        for i in 1..len {
159            pos[i].x_offset = 40_000_000;
160            pos[i].y_offset = 40_000_000;
161            pos[i].set_attach_type(attach_type::MARK);
162            pos[i].set_attach_chain(-1);
163        }
164
165        propagate_attachment_offsets(
166            &mut pos,
167            len,
168            len - 1,
169            Direction::LeftToRight,
170            MAX_NESTING_LEVEL,
171        );
172
173        assert_eq!(pos[len - 1].x_offset, i32::MAX);
174        assert_eq!(pos[len - 1].y_offset, 40_000_000);
175    }
176}