1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! The [glyph positioning][1].
//!
//! [1]: https://learn.microsoft.com/en-us/typography/opentype/spec/gpos

mod element;

pub use element::*;

use crate::layout::{ChainedContext, Class, Context, Coverage, Directory};
use crate::{Result, Tape, Value, Walue};

/// A glyph positioning.
pub type GlyphPositioning = Directory<Type>;

/// A glyph-positioning type.
#[derive(Clone, Debug)]
pub enum Type {
    SingleAdjustment(SingleAdjustment),
    PairAdjustment(PairAdjustment),
    CursiveAttachment(CursiveAttachment),
    MarkToBaseAttachment(MarkToBaseAttachment),
    MarkToLigatureAttachment(MarkToLigatureAttachment),
    MarkToMarkAttachment(MarkToMarkAttachment),
    ContextualPositioning(Context),
    ChainedContextualPositioning(ChainedContext),
    ExtensionPositioning(ExtensionPositioning),
}

/// A single adjustment.
#[derive(Clone, Debug)]
pub enum SingleAdjustment {
    /// Format 1.
    Format1(SingleAdjustment1),
    /// Format 2.
    Format2(SingleAdjustment2),
}

table! {
    @position
    #[doc = "A single adjustment in format 1."]
    pub SingleAdjustment1 { // SinglePosFormat1
        format          (u16  ), // posFormat
        coverage_offset (u16  ), // coverageOffset
        value_flags     (Flags), // valueFormat

        value (Single) |this, tape, position| { // valueRecord
            tape.take_given((position, this.value_flags))
        },

        coverage (Coverage) |this, tape, position| {
            jump_take!(tape, position, this.coverage_offset)
        },
    }
}

table! {
    @position
    #[doc = "A single adjustment in format 2."]
    pub SingleAdjustment2 { // SinglePosFormat2
        format          (u16  ), // posFormat
        coverage_offset (u16  ), // coverageOffset
        value_flags     (Flags), // valueFormat
        value_count     (u16  ), // valueCount

        values (Vec<Single>) |this, tape, position| { // valueRecords
            (0..this.value_count)
                .map(|_| tape.take_given((position, this.value_flags)))
                .collect()
        },

        coverage (Coverage) |this, tape, position| {
            jump_take!(tape, position, this.coverage_offset)
        },
    }
}

/// A pair adjustment.
#[derive(Clone, Debug)]
pub enum PairAdjustment {
    /// Format 1.
    Format1(PairAdjustment1),
    /// Format 2.
    Format2(PairAdjustment2),
}

table! {
    @position
    #[doc = "A pair adjustment in format 1."]
    pub PairAdjustment1 { // PairPosFormat1
        format          (u16  ), // posFormat
        coverage_offset (u16  ), // coverageOffset
        value1_flags    (Flags), // valueFormat1
        value2_flags    (Flags), // valueFormat2
        rule_count      (u16  ), // pairSetCount

        rule_offsets (Vec<u16>) |this, tape, _| { // pairSetOffsets
            tape.take_given(this.rule_count as usize)
        },

        coverage (Coverage) |this, tape, position| {
            jump_take!(tape, position, this.coverage_offset)
        },

        rules (Vec<Pair1s>) |this, tape, position| {
            jump_take_given!(
                tape,
                position,
                this.rule_count,
                this.rule_offsets,
                (this.value1_flags, this.value2_flags)
            )
        },
    }
}

table! {
    @position
    #[doc = "A pair adjustment in format 2."]
    pub PairAdjustment2 { // PairPosFormat2
        format          (u16  ), // posFormat
        coverage_offset (u16  ), // coverageOffset
        value1_flags    (Flags), // valueFormat1
        value2_flags    (Flags), // valueFormat2
        class1_offset   (u16  ), // classDef1Offset
        class2_offset   (u16  ), // classDef2Offset
        class1_count    (u16  ), // class1Count
        class2_count    (u16  ), // class2Count

        rules (Vec<Pair2s>) |this, tape, position| { // class1Records
            (0..this.class1_count)
                .map(|_| tape.take_given((position, this.class2_count, this.value1_flags, this.value2_flags)))
                .collect()
        },

        coverage (Coverage) |this, tape, position| {
            jump_take!(tape, position, this.coverage_offset)
        },

        class1 (Class) |this, tape, position| {
            jump_take!(tape, position, this.class1_offset)
        },

        class2 (Class) |this, tape, position| {
            jump_take!(tape, position, this.class2_offset)
        },
    }
}

table! {
    @position
    #[doc = "A cursive attachment."]
    pub CursiveAttachment { // CursivePosFormat1
        format           (u16) = { 1 }, // posFormat
        coverage_offset  (u16), // coverageOffset
        connection_count (u16), // entryExitCount

        connections (Vec<Connection>) |this, tape, position| { // entryExitRecords
            (0..this.connection_count)
                .map(|_| tape.take_given(position))
                .collect()
        },

        coverage (Coverage) |this, tape, position| {
            jump_take!(tape, position, this.coverage_offset)
        },
    }
}

table! {
    @position
    #[doc = "A mark-to-base attachment."]
    pub MarkToBaseAttachment { // MarkBasePosFormat1
        format               (u16) = { 1 }, // posFormat
        mark_coverage_offset (u16), // markCoverageOffset
        base_coverage_offset (u16), // baseCoverageOffset
        mark_class_count     (u16), // markClassCount
        mark_offset          (u16), // markArrayOffset
        base_offset          (u16), // baseArrayOffset

        mark_coverage (Coverage) |this, tape, position| {
            jump_take!(tape, position, this.mark_coverage_offset)
        },

        base_coverage (Coverage) |this, tape, position| {
            jump_take!(tape, position, this.base_coverage_offset)
        },

        marks (Mark1s) |this, tape, position| {
            jump_take!(tape, position, this.mark_offset)
        },

        bases (Bases) |this, tape, position| {
            jump_take_given!(tape, position, this.base_offset, this.mark_class_count)
        },
    }
}

table! {
    @position
    #[doc = "A mark-to-ligature attachment."]
    pub MarkToLigatureAttachment { // MarkLigPosFormat1
        format                   (u16) = { 1 }, // posFormat
        mark_coverage_offset     (u16), // markCoverageOffset
        ligature_coverage_offset (u16), // ligatureCoverageOffset
        mark_class_count         (u16), // markClassCount
        mark_offset              (u16), // markArrayOffset
        ligature_offset          (u16), // ligatureArrayOffset

        mark_coverage (Coverage) |this, tape, position| {
            jump_take!(tape, position, this.mark_coverage_offset)
        },

        ligature_coverage (Coverage) |this, tape, position| {
            jump_take!(tape, position, this.ligature_coverage_offset)
        },

        marks (Mark1s) |this, tape, position| {
            jump_take!(tape, position, this.mark_offset)
        },

        ligatures (Ligatures) |this, tape, position| {
            jump_take_given!(tape, position, this.ligature_offset, this.mark_class_count)
        },
    }
}

table! {
    @position
    #[doc = "A mark-to-mark attachment."]
    pub MarkToMarkAttachment { // MarkMarkPosFormat1
        format                (u16) = { 1 }, // posFormat
        mark1_coverage_offset (u16), // mark1CoverageOffset
        mark2_coverage_offset (u16), // mark2CoverageOffset
        mark_class_count      (u16), // markClassCount
        mark1_offset          (u16), // mark1ArrayOffset
        mark2_offset          (u16), // mark2ArrayOffset

        mark1_coverage (Coverage) |this, tape, position| {
            jump_take!(tape, position, this.mark1_coverage_offset)
        },

        mark2_coverage (Coverage) |this, tape, position| {
            jump_take!(tape, position, this.mark2_coverage_offset)
        },

        mark1s (Mark1s) |this, tape, position| {
            jump_take!(tape, position, this.mark1_offset)
        },

        mark2s (Mark2s) |this, tape, position| {
            jump_take_given!(tape, position, this.mark2_offset, this.mark_class_count)
        },
    }
}

table! {
    #[doc = "An extension positioning."]
    pub ExtensionPositioning { // ExtensionPosFormat1
        format (u16) = { 1 }, // posFormat
        r#type (u16), // extensionLookupType
        offset (u32), // extensionOffset
    }
}

impl Walue<'static> for Type {
    type Parameter = u16;

    fn read<T: Tape>(tape: &mut T, r#type: u16) -> Result<Self> {
        Ok(match r#type {
            1 => Self::SingleAdjustment(tape.take()?),
            2 => Self::PairAdjustment(tape.take()?),
            3 => Self::CursiveAttachment(tape.take()?),
            4 => Self::MarkToBaseAttachment(tape.take()?),
            5 => Self::MarkToLigatureAttachment(tape.take()?),
            6 => Self::MarkToMarkAttachment(tape.take()?),
            7 => Self::ContextualPositioning(tape.take()?),
            8 => Self::ChainedContextualPositioning(tape.take()?),
            9 => Self::ExtensionPositioning(tape.take()?),
            value => raise!("found an unknown type of glyph positioning ({value})"),
        })
    }
}

impl Value for SingleAdjustment {
    fn read<T: Tape>(tape: &mut T) -> Result<Self> {
        Ok(match tape.peek::<u16>()? {
            1 => Self::Format1(tape.take()?),
            2 => Self::Format2(tape.take()?),
            value => raise!("found an unknown format of the single adjustment ({value})"),
        })
    }
}

impl Value for PairAdjustment {
    fn read<T: Tape>(tape: &mut T) -> Result<Self> {
        Ok(match tape.peek::<u16>()? {
            1 => Self::Format1(tape.take()?),
            2 => Self::Format2(tape.take()?),
            value => raise!("found an unknown format of the pair adjustment ({value})"),
        })
    }
}