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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
//! Glyph positioning (`gpos`) implementation.
//!
//! > The Glyph Positioning table (GPOS) provides precise control over glyph placement for
//! > sophisticated text layout and rendering in each script and language system that a font
//! > supports.
//!
//! — <https://docs.microsoft.com/en-us/typography/opentype/spec/gpos>

use crate::context::{ContextLookupHelper, Glyph, LookupFlag, MatchType};
use crate::error::ParseError;
use crate::gdef::gdef_is_mark;
use crate::gsub::{FeatureInfo, Features, RawGlyph};
use crate::layout::{
    chain_context_lookup_info, context_lookup_info, Adjust, Anchor, ChainContextLookup,
    ContextLookup, CursivePos, GDEFTable, LangSys, LayoutCache, LayoutTable, LookupList,
    MarkBasePos, MarkLigPos, PairPos, PosLookup, SinglePos, ValueRecord, GPOS,
};
use crate::scripts;
use crate::scripts::ScriptType;
use crate::tag;

use itertools::Itertools;
use tinyvec::tiny_vec;
use unicode_general_category::GeneralCategory;

type PosContext<'a> = ContextLookupHelper<'a, GPOS>;

/// Apply glyph positioning rules to glyph `Info`.
pub fn apply(
    gpos_cache: &LayoutCache<GPOS>,
    opt_gdef_table: Option<&GDEFTable>,
    kerning: bool,
    features: &Features,
    script_tag: u32,
    opt_lang_tag: Option<u32>,
    infos: &mut [Info],
) -> Result<(), ParseError> {
    let gpos_table = &gpos_cache.layout_table;
    let script_type = ScriptType::from(script_tag);

    let script = match script_type {
        ScriptType::Indic => {
            let indic2_tag = scripts::indic::indic2_tag(script_tag);
            match gpos_table.find_script(indic2_tag)? {
                Some(script) => script,
                None => match gpos_table.find_script_or_default(script_tag)? {
                    Some(script) => script,
                    None => return Ok(()),
                },
            }
        }
        _ => match gpos_table.find_script_or_default(script_tag)? {
            Some(script) => script,
            None => return Ok(()),
        },
    };

    let langsys = match script.find_langsys_or_default(opt_lang_tag)? {
        Some(langsys) => langsys,
        None => return Ok(()),
    };

    let base_features: &[u32] = match script_type {
        ScriptType::Arabic => &[tag::CURS, tag::KERN, tag::MARK, tag::MKMK],
        ScriptType::Indic => &[
            tag::ABVM,
            tag::BLWM,
            tag::DIST,
            tag::KERN,
            tag::MARK,
            tag::MKMK,
        ],
        ScriptType::Khmer => &[tag::ABVM, tag::BLWM, tag::DIST, tag::MARK, tag::MKMK],
        ScriptType::Syriac => &[tag::CURS, tag::KERN, tag::MARK, tag::MKMK],
        ScriptType::ThaiLao => &[tag::KERN, tag::MARK, tag::MKMK],
        ScriptType::Default if kerning => &[tag::DIST, tag::KERN, tag::MARK, tag::MKMK],
        ScriptType::Default => &[tag::DIST, tag::MARK, tag::MKMK],
    };

    apply_features(
        gpos_cache,
        gpos_table,
        opt_gdef_table,
        langsys,
        base_features.iter().map(|&feature_tag| FeatureInfo {
            feature_tag,
            alternate: None,
        }),
        infos,
    )?;
    match features {
        Features::Custom(custom) => apply_features(
            gpos_cache,
            gpos_table,
            opt_gdef_table,
            langsys,
            custom.iter().copied(),
            infos,
        ),
        Features::Mask(mask) => apply_features(
            gpos_cache,
            gpos_table,
            opt_gdef_table,
            langsys,
            mask.iter(),
            infos,
        ),
    }
}

/// Apply glyph positioning using specified OpenType features.
///
/// Generally use `gpos::apply`, which will enable features based on script and language. Use
/// this method if you need more low-level control over the enabled features.
pub fn apply_features(
    gpos_cache: &LayoutCache<GPOS>,
    gpos_table: &LayoutTable<GPOS>,
    opt_gdef_table: Option<&GDEFTable>,
    langsys: &LangSys,
    features: impl Iterator<Item = FeatureInfo>,
    infos: &mut [Info],
) -> Result<(), ParseError> {
    let mut lookup_indices = tiny_vec!([u16; 128]);
    for feature in features {
        if let Some(feature_table) =
            gpos_table.find_langsys_feature(langsys, feature.feature_tag)?
        {
            // Sort and remove duplicates
            lookup_indices.clear();
            lookup_indices.extend_from_slice(&feature_table.lookup_indices);
            lookup_indices.sort_unstable();
            for lookup_index in lookup_indices.iter().copied().dedup().map(usize::from) {
                gpos_apply_lookup(gpos_cache, gpos_table, opt_gdef_table, lookup_index, infos)?;
            }
        }
    }
    Ok(())
}

/// Apply basic mark processing when there is no `gpos` table available.
///
/// Call this method when there is no `LayoutCache<GPOS>` available for this font.
pub fn apply_fallback(infos: &mut [Info]) {
    for info in infos.iter_mut() {
        if !info.is_mark && unicodes_are_marks(&info.glyph.unicodes) {
            info.is_mark = true;
        }
    }
    let mut base_index = 0;
    for (i, info) in infos.iter_mut().enumerate().skip(1) {
        if info.is_mark {
            info.placement = Placement::MarkOverprint(base_index);
        } else {
            base_index = i;
        }
    }
}

fn unicodes_are_marks(unicodes: &[char]) -> bool {
    unicodes
        .iter()
        .copied()
        .map(unicode_general_category::get_general_category)
        .all(|cat| cat == GeneralCategory::NonspacingMark)
}

fn gpos_apply_lookup(
    gpos_cache: &LayoutCache<GPOS>,
    gpos_table: &LayoutTable<GPOS>,
    opt_gdef_table: Option<&GDEFTable>,
    lookup_index: usize,
    infos: &mut [Info],
) -> Result<(), ParseError> {
    if let Some(ref lookup_list) = gpos_table.opt_lookup_list {
        let lookup = lookup_list.lookup_cache_gpos(gpos_cache, lookup_index)?;
        let match_type = MatchType::from_lookup_flag(lookup.lookup_flag);
        match lookup.lookup_subtables {
            PosLookup::SinglePos(ref subtables) => {
                forall_glyphs_match(match_type, opt_gdef_table, infos, |i, infos| {
                    singlepos(subtables, &mut infos[i])
                })
            }
            PosLookup::PairPos(ref subtables) => {
                // Spec suggests that the lookup will only be applied to the second glyph if it was
                // not repositioned, ie. if the value_format is zero, but applying the lookup
                // regardless does not break any test cases.
                forall_glyph_pairs_match(match_type, opt_gdef_table, infos, |i1, i2, infos| {
                    pairpos(subtables, i1, i2, infos)
                })
            }
            PosLookup::CursivePos(ref subtables) => forall_glyph_pairs_match(
                MatchType::ignore_marks(),
                opt_gdef_table,
                infos,
                |i1, i2, infos| cursivepos(subtables, i1, i2, lookup.lookup_flag, infos),
            ),
            PosLookup::MarkBasePos(ref subtables) => {
                forall_base_mark_glyph_pairs(infos, |i1, i2, infos| {
                    markbasepos(subtables, i1, i2, infos)
                })
            }
            PosLookup::MarkLigPos(ref subtables) => {
                forall_base_mark_glyph_pairs(infos, |i1, i2, infos| {
                    markligpos(subtables, i1, i2, infos)
                })
            }
            PosLookup::MarkMarkPos(ref subtables) => {
                forall_mark_mark_glyph_pairs(infos, |i1, i2, infos| {
                    markmarkpos(subtables, i1, i2, infos)
                })
            }
            PosLookup::ContextPos(ref subtables) => {
                forall_glyphs_match(match_type, opt_gdef_table, infos, |i, infos| {
                    contextpos(
                        gpos_cache,
                        lookup_list,
                        opt_gdef_table,
                        match_type,
                        subtables,
                        i,
                        infos,
                    )
                })
            }
            PosLookup::ChainContextPos(ref subtables) => {
                forall_glyphs_match(match_type, opt_gdef_table, infos, |i, infos| {
                    chaincontextpos(
                        gpos_cache,
                        lookup_list,
                        opt_gdef_table,
                        match_type,
                        subtables,
                        i,
                        infos,
                    )
                })
            }
        }
    } else {
        Ok(())
    }
}

fn gpos_lookup_singlepos(
    subtables: &[SinglePos],
    glyph_index: u16,
) -> Result<ValueRecord, ParseError> {
    for singlepos in subtables {
        if let Some(val) = singlepos.apply(glyph_index)? {
            return Ok(Some(val));
        }
    }
    Ok(None)
}

fn gpos_lookup_pairpos(
    subtables: &[PairPos],
    glyph_index1: u16,
    glyph_index2: u16,
) -> Result<Option<(ValueRecord, ValueRecord)>, ParseError> {
    for pairpos in subtables {
        if let Some((val1, val2)) = pairpos.apply(glyph_index1, glyph_index2)? {
            return Ok(Some((val1, val2)));
        }
    }
    Ok(None)
}

fn gpos_lookup_cursivepos(
    subtables: &[CursivePos],
    glyph_index1: u16,
    glyph_index2: u16,
) -> Result<Option<(Anchor, Anchor)>, ParseError> {
    for cursivepos in subtables {
        if let Some((an1, an2)) = cursivepos.apply(glyph_index1, glyph_index2)? {
            return Ok(Some((an1, an2)));
        }
    }
    Ok(None)
}

fn gpos_lookup_markbasepos(
    subtables: &[MarkBasePos],
    glyph_index1: u16,
    glyph_index2: u16,
) -> Result<Option<(Anchor, Anchor)>, ParseError> {
    for markbasepos in subtables {
        if let Some((an1, an2)) = markbasepos.apply(glyph_index1, glyph_index2)? {
            return Ok(Some((an1, an2)));
        }
    }
    Ok(None)
}

fn gpos_lookup_markligpos(
    subtables: &[MarkLigPos],
    glyph_index1: u16,
    glyph_index2: u16,
    liga_component_index: u16,
) -> Result<Option<(Anchor, Anchor)>, ParseError> {
    for markligpos in subtables {
        if let Some((an1, an2)) = markligpos.apply(
            glyph_index1,
            glyph_index2,
            usize::from(liga_component_index),
        )? {
            return Ok(Some((an1, an2)));
        }
    }
    Ok(None)
}

fn gpos_lookup_markmarkpos(
    subtables: &[MarkBasePos],
    glyph_index1: u16,
    glyph_index2: u16,
) -> Result<Option<(Anchor, Anchor)>, ParseError> {
    for markmarkpos in subtables {
        if let Some((an1, an2)) = markmarkpos.apply(glyph_index1, glyph_index2)? {
            return Ok(Some((an1, an2)));
        }
    }
    Ok(None)
}

fn gpos_lookup_contextpos<'a>(
    opt_gdef_table: Option<&GDEFTable>,
    match_type: MatchType,
    subtables: &'a [ContextLookup<GPOS>],
    glyph_index: u16,
    i: usize,
    infos: &mut [Info],
) -> Result<Option<Box<PosContext<'a>>>, ParseError> {
    for context_lookup in subtables {
        if let Some(context) = context_lookup_info(context_lookup, glyph_index, |context| {
            context.matches(opt_gdef_table, match_type, infos, i)
        })? {
            return Ok(Some(context));
        }
    }
    Ok(None)
}

fn gpos_lookup_chaincontextpos<'a>(
    opt_gdef_table: Option<&GDEFTable>,
    match_type: MatchType,
    subtables: &'a [ChainContextLookup<GPOS>],
    glyph_index: u16,
    i: usize,
    infos: &mut [Info],
) -> Result<Option<Box<PosContext<'a>>>, ParseError> {
    for chain_context_lookup in subtables {
        if let Some(context) =
            chain_context_lookup_info(chain_context_lookup, glyph_index, |context| {
                context.matches(opt_gdef_table, match_type, infos, i)
            })?
        {
            return Ok(Some(context));
        }
    }
    Ok(None)
}

/// Adjustment to the placement of a glyph as a result of kerning and
/// placement of an attachment relative to a base glyph.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Placement {
    None,
    /// Placement offset by distance delta.
    ///
    /// Fields
    /// (delta x, delta y)
    Distance(i32, i32),
    /// An anchored mark.
    ///
    /// This is a mark where its anchor is aligned with the base glyph anchor.
    ///
    /// Fields:
    /// (base glyph index in `Vec<Info>`, base glyph anchor, mark anchor)
    MarkAnchor(usize, Anchor, Anchor),
    /// An overprint mark.
    ///
    /// This mark is shown at the same position as the base glyph.
    ///
    /// Fields:
    /// (base glyph index in `Vec<Info>`)
    MarkOverprint(usize),
    /// Cursive anchored placement.
    ///
    /// Fields:
    /// * exit glyph index in `Vec<Info>`,
    /// * [RIGHT_TO_LEFT flag from lookup table](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#lookupFlags_1),
    /// * exit glyph anchor,
    /// * entry glyph anchor
    ///
    /// <https://docs.microsoft.com/en-us/typography/opentype/spec/gpos#lookup-type-3-cursive-attachment-positioning-subtable>
    CursiveAnchor(usize, bool, Anchor, Anchor),
}

impl Placement {
    fn combine_distance(&mut self, x2: i32, y2: i32) {
        use Placement::*;

        *self = match *self {
            None | MarkOverprint(_) => Distance(x2, y2),
            // FIXME HarfBuzz also updates cursive anchors
            // but we haven't found any fonts that test this codepath yet.
            CursiveAnchor(..) => Distance(x2, y2),
            Distance(x1, y1) => Distance(x1 + x2, y1 + y2),
            MarkAnchor(i, an1, an2) => {
                let x = an1.x + (x2 as i16);
                let y = an1.y + (y2 as i16);
                MarkAnchor(i, Anchor { x, y }, an2)
            }
        }
    }
}

/// A positioned glyph.
///
/// This struct is the output of applying glyph positioning (`gpos`). It contains the glyph
/// and information about how it should be positioned.
///
/// For more information about glyph placement refer to the OpenType documentation:
/// <https://docs.microsoft.com/en-us/typography/opentype/spec/gpos#positioning-glyphs-with-opentype>
#[derive(Clone, Debug)]
pub struct Info {
    /// The glyph.
    pub glyph: RawGlyph<()>,
    /// An offset from the horizontal glyph advance position for this glyph.
    pub kerning: i16,
    /// When not `Placement::None` indicates that this glyph should be placed according to
    /// the variant.
    pub placement: Placement,
    is_mark: bool,
}

impl Glyph for Info {
    fn get_glyph_index(&self) -> u16 {
        self.glyph.glyph_index
    }
}

impl Info {
    pub fn init_from_glyphs(
        opt_gdef_table: Option<&GDEFTable>,
        glyphs: Vec<RawGlyph<()>>,
    ) -> Vec<Info> {
        let mut infos = Vec::with_capacity(glyphs.len());
        for glyph in glyphs {
            let is_mark = gdef_is_mark(opt_gdef_table, glyph.glyph_index);
            let info = Info {
                glyph,
                kerning: 0,
                placement: Placement::None,
                is_mark,
            };
            infos.push(info);
        }
        infos
    }
}

impl Adjust {
    fn apply(&self, info: &mut Info) {
        if self.x_placement == 0 && self.y_placement == 0 {
            if self.x_advance != 0 && self.y_advance == 0 {
                info.kerning += self.x_advance;
            } else if self.y_advance != 0 {
                // error: y_advance non-zero
            } else {
                // both zero, do nothing
            }
        } else if self.y_advance == 0 {
            info.placement
                .combine_distance(i32::from(self.x_placement), i32::from(self.y_placement));
            if self.x_advance != 0 {
                info.kerning += self.x_advance;
            }
        } else {
            // error: y_advance non-zero
        }
    }
}

fn forall_glyphs_match(
    match_type: MatchType,
    opt_gdef_table: Option<&GDEFTable>,
    infos: &mut [Info],
    f: impl Fn(usize, &mut [Info]) -> Result<(), ParseError>,
) -> Result<(), ParseError> {
    for i in 0..infos.len() {
        if match_type.match_glyph(opt_gdef_table, &infos[i]) {
            f(i, infos)?;
        }
    }
    Ok(())
}

fn forall_glyph_pairs_match(
    match_type: MatchType,
    opt_gdef_table: Option<&GDEFTable>,
    infos: &mut [Info],
    f: impl Fn(usize, usize, &mut [Info]) -> Result<(), ParseError>,
) -> Result<(), ParseError> {
    if let Some(mut i1) = match_type.find_first(opt_gdef_table, infos) {
        while let Some(i2) = match_type.find_next(opt_gdef_table, infos, i1) {
            f(i1, i2, infos)?;
            i1 = i2;
        }
    }
    Ok(())
}

fn forall_base_mark_glyph_pairs(
    infos: &mut [Info],
    f: impl Fn(usize, usize, &mut [Info]) -> Result<(), ParseError>,
) -> Result<(), ParseError> {
    let mut i = 0;
    'outer: while i + 1 < infos.len() {
        if !infos[i].is_mark {
            for j in i + 1..infos.len() {
                f(i, j, infos)?;
                if !infos[j].is_mark {
                    i = j;
                    continue 'outer;
                }
            }
        }
        i += 1;
    }
    Ok(())
}

fn forall_mark_mark_glyph_pairs(
    infos: &mut [Info],
    f: impl Fn(usize, usize, &mut [Info]) -> Result<(), ParseError>,
) -> Result<(), ParseError> {
    let mut start = 0;
    'outer: loop {
        let mut i = start;
        while i + 1 < infos.len() {
            if infos[i].is_mark {
                for j in i + 1..infos.len() {
                    f(i, j, infos)?;
                    if !infos[j].is_mark {
                        start = i + 1;
                        continue 'outer;
                    }
                }
            }
            i += 1;
        }
        break;
    }
    Ok(())
}

fn singlepos(subtables: &[SinglePos], i: &mut Info) -> Result<(), ParseError> {
    let glyph_index = i.glyph.glyph_index;
    if let Some(adj) = gpos_lookup_singlepos(subtables, glyph_index)? {
        adj.apply(i);
    }
    Ok(())
}

fn pairpos(
    subtables: &[PairPos],
    i1: usize,
    i2: usize,
    infos: &mut [Info],
) -> Result<(), ParseError> {
    match gpos_lookup_pairpos(
        subtables,
        infos[i1].glyph.glyph_index,
        infos[i2].glyph.glyph_index,
    )? {
        Some((opt_adj1, opt_adj2)) => {
            if let Some(adj1) = opt_adj1 {
                adj1.apply(&mut infos[i1]);
            }
            if let Some(adj2) = opt_adj2 {
                adj2.apply(&mut infos[i2]);
            }
            Ok(())
        }
        None => Ok(()),
    }
}

fn cursivepos(
    subtables: &[CursivePos],
    i1: usize,
    i2: usize,
    lookup_flag: LookupFlag,
    infos: &mut [Info],
) -> Result<(), ParseError> {
    match gpos_lookup_cursivepos(
        subtables,
        infos[i1].glyph.glyph_index,
        infos[i2].glyph.glyph_index,
    )? {
        Some((anchor1, anchor2)) => {
            infos[i1].placement =
                Placement::CursiveAnchor(i2, lookup_flag.get_rtl(), anchor2, anchor1);
            Ok(())
        }
        None => Ok(()),
    }
}

fn markbasepos(
    subtables: &[MarkBasePos],
    i1: usize,
    i2: usize,
    infos: &mut [Info],
) -> Result<(), ParseError> {
    match gpos_lookup_markbasepos(
        subtables,
        infos[i1].glyph.glyph_index,
        infos[i2].glyph.glyph_index,
    )? {
        Some((anchor1, anchor2)) => {
            infos[i2].placement = Placement::MarkAnchor(i1, anchor1, anchor2);
            infos[i2].is_mark = true;
            Ok(())
        }
        None => Ok(()),
    }
}

fn markligpos(
    subtables: &[MarkLigPos],
    i1: usize,
    i2: usize,
    infos: &mut [Info],
) -> Result<(), ParseError> {
    match gpos_lookup_markligpos(
        subtables,
        infos[i1].glyph.glyph_index,
        infos[i2].glyph.glyph_index,
        infos[i2].glyph.liga_component_pos,
    )? {
        Some((anchor1, anchor2)) => {
            infos[i2].placement = Placement::MarkAnchor(i1, anchor1, anchor2);
            infos[i2].is_mark = true;
            Ok(())
        }
        None => Ok(()),
    }
}

fn markmarkpos(
    subtables: &[MarkBasePos],
    i1: usize,
    i2: usize,
    infos: &mut [Info],
) -> Result<(), ParseError> {
    match gpos_lookup_markmarkpos(
        subtables,
        infos[i1].glyph.glyph_index,
        infos[i2].glyph.glyph_index,
    )? {
        Some((anchor1, anchor2)) => {
            infos[i2].placement = Placement::MarkAnchor(i1, anchor1, anchor2);
            infos[i2].is_mark = true;
            Ok(())
        }
        None => Ok(()),
    }
}

fn contextpos(
    gpos_cache: &LayoutCache<GPOS>,
    lookup_list: &LookupList<GPOS>,
    opt_gdef_table: Option<&GDEFTable>,
    match_type: MatchType,
    subtables: &[ContextLookup<GPOS>],
    i: usize,
    infos: &mut [Info],
) -> Result<(), ParseError> {
    let glyph_index = infos[i].glyph.glyph_index;
    match gpos_lookup_contextpos(opt_gdef_table, match_type, subtables, glyph_index, i, infos)? {
        Some(pos) => apply_pos_context(
            gpos_cache,
            lookup_list,
            opt_gdef_table,
            match_type,
            &pos,
            i,
            infos,
        ),
        None => Ok(()),
    }
}

fn chaincontextpos(
    gpos_cache: &LayoutCache<GPOS>,
    lookup_list: &LookupList<GPOS>,
    opt_gdef_table: Option<&GDEFTable>,
    match_type: MatchType,
    subtables: &[ChainContextLookup<GPOS>],
    i: usize,
    infos: &mut [Info],
) -> Result<(), ParseError> {
    let glyph_index = infos[i].glyph.glyph_index;
    match gpos_lookup_chaincontextpos(opt_gdef_table, match_type, subtables, glyph_index, i, infos)?
    {
        Some(pos) => apply_pos_context(
            gpos_cache,
            lookup_list,
            opt_gdef_table,
            match_type,
            &pos,
            i,
            infos,
        ),
        None => Ok(()),
    }
}

fn apply_pos_context(
    gpos_cache: &LayoutCache<GPOS>,
    lookup_list: &LookupList<GPOS>,
    opt_gdef_table: Option<&GDEFTable>,
    _match_type: MatchType,
    pos: &PosContext<'_>,
    i: usize,
    infos: &mut [Info],
) -> Result<(), ParseError> {
    for (pos_index, pos_lookup_index) in pos.lookup_array {
        apply_pos(
            gpos_cache,
            lookup_list,
            opt_gdef_table,
            usize::from(*pos_index),
            usize::from(*pos_lookup_index),
            infos,
            i,
        )?;
    }
    Ok(())
}

fn apply_pos(
    gpos_cache: &LayoutCache<GPOS>,
    lookup_list: &LookupList<GPOS>,
    opt_gdef_table: Option<&GDEFTable>,
    pos_index: usize,
    lookup_index: usize,
    infos: &mut [Info],
    index: usize,
) -> Result<(), ParseError> {
    let lookup = lookup_list.lookup_cache_gpos(gpos_cache, lookup_index)?;
    let match_type = MatchType::from_lookup_flag(lookup.lookup_flag);
    let i1 = match match_type.find_nth(opt_gdef_table, infos, index, pos_index) {
        Some(index1) => index1,
        None => return Ok(()),
    };
    match lookup.lookup_subtables {
        PosLookup::SinglePos(ref subtables) => singlepos(subtables, &mut infos[i1]),
        PosLookup::PairPos(ref subtables) => {
            if let Some(i2) = match_type.find_next(opt_gdef_table, infos, i1) {
                pairpos(subtables, i1, i2, infos)
            } else {
                Ok(())
            }
        }
        PosLookup::CursivePos(ref subtables) => {
            if let Some(i2) = match_type.find_next(opt_gdef_table, infos, i1) {
                cursivepos(subtables, i1, i2, lookup.lookup_flag, infos)
            } else {
                Ok(())
            }
        }
        PosLookup::MarkBasePos(ref subtables) => {
            // FIXME is this correct?
            if let Some(base_index) = MatchType::ignore_marks().find_prev(opt_gdef_table, infos, i1)
            {
                markbasepos(subtables, base_index, i1, infos)
            } else {
                Ok(())
            }
        }
        PosLookup::MarkLigPos(ref subtables) => {
            // FIXME is this correct?
            if let Some(base_index) = MatchType::ignore_marks().find_prev(opt_gdef_table, infos, i1)
            {
                markligpos(subtables, base_index, i1, infos)
            } else {
                Ok(())
            }
        }
        PosLookup::MarkMarkPos(ref subtables) => {
            // FIXME is this correct?
            if let Some(base_index) = match_type.find_prev(opt_gdef_table, infos, i1) {
                markmarkpos(subtables, base_index, i1, infos)
            } else {
                Ok(())
            }
        }
        PosLookup::ContextPos(ref _subtables) => Ok(()),
        PosLookup::ChainContextPos(ref _subtables) => Ok(()),
    }
}