rustybuzz/hb/
ot_layout_gpos_table.rs

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
use ttf_parser::gpos::*;
use ttf_parser::opentype_layout::LookupIndex;
use ttf_parser::GlyphId;

use super::buffer::*;
use super::hb_font_t;
use super::ot_layout::*;
use super::ot_layout_common::{lookup_flags, PositioningLookup, PositioningTable};
use super::ot_layout_gsubgpos::{skipping_iterator_t, Apply, OT::hb_ot_apply_context_t};
use super::ot_shape_plan::hb_ot_shape_plan_t;
use crate::Direction;

pub fn position(plan: &hb_ot_shape_plan_t, face: &hb_font_t, buffer: &mut hb_buffer_t) {
    apply_layout_table(plan, face, buffer, face.gpos.as_ref());
}

trait ValueRecordExt {
    fn is_empty(&self) -> bool;
    fn apply(&self, ctx: &mut hb_ot_apply_context_t, idx: usize) -> bool;
    fn apply_to_pos(&self, ctx: &mut hb_ot_apply_context_t, pos: &mut GlyphPosition) -> bool;
}

impl ValueRecordExt for ValueRecord<'_> {
    fn is_empty(&self) -> bool {
        self.x_placement == 0
            && self.y_placement == 0
            && self.x_advance == 0
            && self.y_advance == 0
            && self.x_placement_device.is_none()
            && self.y_placement_device.is_none()
            && self.x_advance_device.is_none()
            && self.y_advance_device.is_none()
    }

    fn apply(&self, ctx: &mut hb_ot_apply_context_t, idx: usize) -> bool {
        let mut pos = ctx.buffer.pos[idx];
        let worked = self.apply_to_pos(ctx, &mut pos);
        ctx.buffer.pos[idx] = pos;
        worked
    }

    fn apply_to_pos(&self, ctx: &mut hb_ot_apply_context_t, pos: &mut GlyphPosition) -> bool {
        let horizontal = ctx.buffer.direction.is_horizontal();
        let mut worked = false;

        if self.x_placement != 0 {
            pos.x_offset += i32::from(self.x_placement);
            worked = true;
        }

        if self.y_placement != 0 {
            pos.y_offset += i32::from(self.y_placement);
            worked = true;
        }

        if self.x_advance != 0 && horizontal {
            pos.x_advance += i32::from(self.x_advance);
            worked = true;
        }

        if self.y_advance != 0 && !horizontal {
            // y_advance values grow downward but font-space grows upward, hence negation
            pos.y_advance -= i32::from(self.y_advance);
            worked = true;
        }

        {
            let (ppem_x, ppem_y) = ctx.face.pixels_per_em().unwrap_or((0, 0));
            let coords = ctx.face.ttfp_face.variation_coordinates().len();
            let use_x_device = ppem_x != 0 || coords != 0;
            let use_y_device = ppem_y != 0 || coords != 0;

            if use_x_device {
                if let Some(device) = self.x_placement_device {
                    pos.x_offset += device.get_x_delta(ctx.face).unwrap_or(0);
                    worked = true; // TODO: even when 0?
                }
            }

            if use_y_device {
                if let Some(device) = self.y_placement_device {
                    pos.y_offset += device.get_y_delta(ctx.face).unwrap_or(0);
                    worked = true;
                }
            }

            if horizontal && use_x_device {
                if let Some(device) = self.x_advance_device {
                    pos.x_advance += device.get_x_delta(ctx.face).unwrap_or(0);
                    worked = true;
                }
            }

            if !horizontal && use_y_device {
                if let Some(device) = self.y_advance_device {
                    // y_advance values grow downward but face-space grows upward, hence negation
                    pos.y_advance -= device.get_y_delta(ctx.face).unwrap_or(0);
                    worked = true;
                }
            }
        }

        worked
    }
}

trait AnchorExt {
    fn get(&self, face: &hb_font_t) -> (i32, i32);
}

impl AnchorExt for Anchor<'_> {
    fn get(&self, face: &hb_font_t) -> (i32, i32) {
        let mut x = i32::from(self.x);
        let mut y = i32::from(self.y);

        if self.x_device.is_some() || self.y_device.is_some() {
            let (ppem_x, ppem_y) = face.pixels_per_em().unwrap_or((0, 0));
            let coords = face.ttfp_face.variation_coordinates().len();

            if let Some(device) = self.x_device {
                if ppem_x != 0 || coords != 0 {
                    x += device.get_x_delta(face).unwrap_or(0);
                }
            }

            if let Some(device) = self.y_device {
                if ppem_y != 0 || coords != 0 {
                    y += device.get_y_delta(face).unwrap_or(0);
                }
            }
        }

        (x, y)
    }
}

impl<'a> LayoutTable for PositioningTable<'a> {
    const INDEX: TableIndex = TableIndex::GPOS;
    const IN_PLACE: bool = true;

    type Lookup = PositioningLookup<'a>;

    fn get_lookup(&self, index: LookupIndex) -> Option<&Self::Lookup> {
        self.lookups.get(usize::from(index))
    }
}

impl LayoutLookup for PositioningLookup<'_> {
    fn props(&self) -> u32 {
        self.props
    }

    fn is_reverse(&self) -> bool {
        false
    }

    fn covers(&self, glyph: GlyphId) -> bool {
        self.coverage.contains(glyph)
    }
}

impl Apply for PositioningLookup<'_> {
    fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
        if self.covers(ctx.buffer.cur(0).as_glyph()) {
            for subtable in &self.subtables {
                if subtable.apply(ctx).is_some() {
                    return Some(());
                }
            }
        }

        None
    }
}

impl Apply for SingleAdjustment<'_> {
    fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
        let glyph = ctx.buffer.cur(0).as_glyph();
        let record = match self {
            Self::Format1 { coverage, value } => {
                coverage.get(glyph)?;
                *value
            }
            Self::Format2 { coverage, values } => {
                let index = coverage.get(glyph)?;
                values.get(index)?
            }
        };
        record.apply(ctx, ctx.buffer.idx);
        ctx.buffer.idx += 1;
        Some(())
    }
}

impl Apply for PairAdjustment<'_> {
    fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
        let first_glyph = ctx.buffer.cur(0).as_glyph();
        let first_glyph_coverage_index = self.coverage().get(first_glyph)?;

        let mut iter = skipping_iterator_t::new(ctx, ctx.buffer.idx, 1, false);

        let mut unsafe_to = 0;
        if !iter.next(Some(&mut unsafe_to)) {
            ctx.buffer
                .unsafe_to_concat(Some(ctx.buffer.idx), Some(unsafe_to));
            return None;
        }

        let second_glyph_index = iter.index();
        let second_glyph = ctx.buffer.info[second_glyph_index].as_glyph();

        let finish = |ctx: &mut hb_ot_apply_context_t, has_record2| {
            ctx.buffer.idx = second_glyph_index;

            if has_record2 {
                ctx.buffer.idx += 1;
            }

            Some(())
        };

        let boring = |ctx: &mut hb_ot_apply_context_t, has_record2| {
            ctx.buffer
                .unsafe_to_concat(Some(ctx.buffer.idx), Some(second_glyph_index + 1));
            finish(ctx, has_record2)
        };

        let success = |ctx: &mut hb_ot_apply_context_t, flag1, flag2, has_record2| {
            if flag1 || flag2 {
                ctx.buffer
                    .unsafe_to_break(Some(ctx.buffer.idx), Some(second_glyph_index + 1));
                finish(ctx, has_record2)
            } else {
                boring(ctx, has_record2)
            }
        };

        let bail = |ctx: &mut hb_ot_apply_context_t, records: (ValueRecord, ValueRecord)| {
            let flag1 = records.0.apply(ctx, ctx.buffer.idx);
            let flag2 = records.1.apply(ctx, second_glyph_index);

            let has_record2 = !records.1.is_empty();
            success(ctx, flag1, flag2, has_record2)
        };

        let records = match self {
            Self::Format1 { sets, .. } => {
                sets.get(first_glyph_coverage_index)?.get(second_glyph)?
            }
            Self::Format2 {
                classes, matrix, ..
            } => {
                let classes = (classes.0.get(first_glyph), classes.1.get(second_glyph));

                let records = match matrix.get(classes) {
                    Some(v) => v,
                    None => {
                        ctx.buffer
                            .unsafe_to_concat(Some(ctx.buffer.idx), Some(iter.index() + 1));
                        return None;
                    }
                };

                return bail(ctx, records);
            }
        };

        bail(ctx, records)
    }
}

impl Apply for CursiveAdjustment<'_> {
    fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
        let this = ctx.buffer.cur(0).as_glyph();

        let index_this = self.coverage.get(this)?;
        let entry_this = self.sets.entry(index_this)?;

        let mut iter = skipping_iterator_t::new(ctx, ctx.buffer.idx, 1, false);

        let mut unsafe_from = 0;
        if !iter.prev(Some(&mut unsafe_from)) {
            ctx.buffer
                .unsafe_to_concat_from_outbuffer(Some(unsafe_from), Some(ctx.buffer.idx + 1));
            return None;
        }

        let i = iter.index();
        let prev = ctx.buffer.info[i].as_glyph();
        let index_prev = self.coverage.get(prev)?;
        let Some(exit_prev) = self.sets.exit(index_prev) else {
            ctx.buffer
                .unsafe_to_concat_from_outbuffer(Some(iter.index()), Some(ctx.buffer.idx + 1));
            return None;
        };

        let (exit_x, exit_y) = exit_prev.get(ctx.face);
        let (entry_x, entry_y) = entry_this.get(ctx.face);

        let direction = ctx.buffer.direction;
        let j = ctx.buffer.idx;
        ctx.buffer.unsafe_to_break(Some(i), Some(j));

        let pos = &mut ctx.buffer.pos;
        match direction {
            Direction::LeftToRight => {
                pos[i].x_advance = exit_x + pos[i].x_offset;
                let d = entry_x + pos[j].x_offset;
                pos[j].x_advance -= d;
                pos[j].x_offset -= d;
            }
            Direction::RightToLeft => {
                let d = exit_x + pos[i].x_offset;
                pos[i].x_advance -= d;
                pos[i].x_offset -= d;
                pos[j].x_advance = entry_x + pos[j].x_offset;
            }
            Direction::TopToBottom => {
                pos[i].y_advance = exit_y + pos[i].y_offset;
                let d = entry_y + pos[j].y_offset;
                pos[j].y_advance -= d;
                pos[j].y_offset -= d;
            }
            Direction::BottomToTop => {
                let d = exit_y + pos[i].y_offset;
                pos[i].y_advance -= d;
                pos[i].y_offset -= d;
                pos[j].y_advance = entry_y;
            }
            Direction::Invalid => {}
        }

        // Cross-direction adjustment

        // We attach child to parent (think graph theory and rooted trees whereas
        // the root stays on baseline and each node aligns itself against its
        // parent.
        //
        // Optimize things for the case of RightToLeft, as that's most common in
        // Arabic.
        let mut child = i;
        let mut parent = j;
        let mut x_offset = entry_x - exit_x;
        let mut y_offset = entry_y - exit_y;

        // Low bits are lookup flags, so we want to truncate.
        if ctx.lookup_props as u16 & lookup_flags::RIGHT_TO_LEFT == 0 {
            core::mem::swap(&mut child, &mut parent);
            x_offset = -x_offset;
            y_offset = -y_offset;
        }

        // If child was already connected to someone else, walk through its old
        // chain and reverse the link direction, such that the whole tree of its
        // previous connection now attaches to new parent.  Watch out for case
        // where new parent is on the path from old chain...
        reverse_cursive_minor_offset(pos, child, direction, parent);

        pos[child].set_attach_type(attach_type::CURSIVE);
        pos[child].set_attach_chain((parent as isize - child as isize) as i16);

        ctx.buffer.scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
        if direction.is_horizontal() {
            pos[child].y_offset = y_offset;
        } else {
            pos[child].x_offset = x_offset;
        }

        // If parent was attached to child, separate them.
        // https://github.com/harfbuzz/harfbuzz/issues/2469
        if pos[parent].attach_chain() == -pos[child].attach_chain() {
            pos[parent].set_attach_chain(0);
        }

        ctx.buffer.idx += 1;
        Some(())
    }
}

impl Apply for MarkToBaseAdjustment<'_> {
    fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
        let buffer = &ctx.buffer;
        let mark_glyph = ctx.buffer.cur(0).as_glyph();
        let mark_index = self.mark_coverage.get(mark_glyph)?;

        // Now we search backwards for a non-mark glyph
        let mut iter = skipping_iterator_t::new(ctx, buffer.idx, 1, false);
        iter.set_lookup_props(u32::from(lookup_flags::IGNORE_MARKS));

        let info = &buffer.info;
        loop {
            let mut unsafe_from = 0;
            if !iter.prev(Some(&mut unsafe_from)) {
                ctx.buffer
                    .unsafe_to_concat_from_outbuffer(Some(unsafe_from), Some(ctx.buffer.idx + 1));
                return None;
            }

            // We only want to attach to the first of a MultipleSubst sequence.
            // https://github.com/harfbuzz/harfbuzz/issues/740
            // Reject others...
            // ...but stop if we find a mark in the MultipleSubst sequence:
            // https://github.com/harfbuzz/harfbuzz/issues/1020
            let idx = iter.index();
            if !_hb_glyph_info_multiplied(&info[idx])
                || _hb_glyph_info_get_lig_comp(&info[idx]) == 0
                || idx == 0
                || _hb_glyph_info_is_mark(&info[idx - 1])
                || _hb_glyph_info_get_lig_id(&info[idx])
                    != _hb_glyph_info_get_lig_id(&info[idx - 1])
                || _hb_glyph_info_get_lig_comp(&info[idx])
                    != _hb_glyph_info_get_lig_comp(&info[idx - 1]) + 1
            {
                break;
            }
            iter.reject();
        }

        // Checking that matched glyph is actually a base glyph by GDEF is too strong; disabled

        let iter_idx = iter.index();
        let base_glyph = info[iter_idx].as_glyph();
        let Some(base_index) = self.base_coverage.get(base_glyph) else {
            ctx.buffer
                .unsafe_to_concat_from_outbuffer(Some(iter_idx), Some(buffer.idx + 1));
            return None;
        };

        self.marks
            .apply(ctx, self.anchors, mark_index, base_index, iter_idx)
    }
}

impl Apply for MarkToLigatureAdjustment<'_> {
    fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
        let buffer = &ctx.buffer;
        let mark_glyph = ctx.buffer.cur(0).as_glyph();
        let mark_index = self.mark_coverage.get(mark_glyph)?;

        // Now we search backwards for a non-mark glyph
        let mut iter = skipping_iterator_t::new(ctx, buffer.idx, 1, false);
        iter.set_lookup_props(u32::from(lookup_flags::IGNORE_MARKS));

        let mut unsafe_from = 0;
        if !iter.prev(Some(&mut unsafe_from)) {
            ctx.buffer
                .unsafe_to_concat_from_outbuffer(Some(unsafe_from), Some(ctx.buffer.idx + 1));
            return None;
        }

        // Checking that matched glyph is actually a ligature by GDEF is too strong; disabled

        let iter_idx = iter.index();
        let lig_glyph = buffer.info[iter_idx].as_glyph();
        let Some(lig_index) = self.ligature_coverage.get(lig_glyph) else {
            ctx.buffer
                .unsafe_to_concat_from_outbuffer(Some(iter_idx), Some(buffer.idx + 1));
            return None;
        };
        let lig_attach = self.ligature_array.get(lig_index)?;

        // Find component to attach to
        let comp_count = lig_attach.rows;
        if comp_count == 0 {
            ctx.buffer
                .unsafe_to_concat_from_outbuffer(Some(iter_idx), Some(buffer.idx + 1));
            return None;
        }

        // We must now check whether the ligature ID of the current mark glyph
        // is identical to the ligature ID of the found ligature.  If yes, we
        // can directly use the component index.  If not, we attach the mark
        // glyph to the last component of the ligature.
        let lig_id = _hb_glyph_info_get_lig_id(&buffer.info[iter_idx]);
        let mark_id = _hb_glyph_info_get_lig_id(&buffer.cur(0));
        let mark_comp = u16::from(_hb_glyph_info_get_lig_comp(buffer.cur(0)));
        let matches = lig_id != 0 && lig_id == mark_id && mark_comp > 0;
        let comp_index = if matches {
            mark_comp.min(comp_count)
        } else {
            comp_count
        } - 1;

        self.marks
            .apply(ctx, lig_attach, mark_index, comp_index, iter_idx)
    }
}

impl Apply for MarkToMarkAdjustment<'_> {
    fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
        let buffer = &ctx.buffer;
        let mark1_glyph = ctx.buffer.cur(0).as_glyph();
        let mark1_index = self.mark1_coverage.get(mark1_glyph)?;

        // Now we search backwards for a suitable mark glyph until a non-mark glyph
        let mut iter = skipping_iterator_t::new(ctx, buffer.idx, 1, false);
        iter.set_lookup_props(ctx.lookup_props & !u32::from(lookup_flags::IGNORE_FLAGS));

        let mut unsafe_from = 0;
        if !iter.prev(Some(&mut unsafe_from)) {
            ctx.buffer
                .unsafe_to_concat_from_outbuffer(Some(unsafe_from), Some(ctx.buffer.idx + 1));
            return None;
        }

        let iter_idx = iter.index();
        if !_hb_glyph_info_is_mark(&buffer.info[iter_idx]) {
            ctx.buffer
                .unsafe_to_concat_from_outbuffer(Some(iter_idx), Some(buffer.idx + 1));
            return None;
        }

        let id1 = _hb_glyph_info_get_lig_id(buffer.cur(0));
        let id2 = _hb_glyph_info_get_lig_id(&buffer.info[iter_idx]);
        let comp1 = _hb_glyph_info_get_lig_comp(buffer.cur(0));
        let comp2 = _hb_glyph_info_get_lig_comp(&buffer.info[iter_idx]);

        let matches = if id1 == id2 {
            // Marks belonging to the same base
            // or marks belonging to the same ligature component.
            id1 == 0 || comp1 == comp2
        } else {
            // If ligature ids don't match, it may be the case that one of the marks
            // itself is a ligature.  In which case match.
            (id1 > 0 && comp1 == 0) || (id2 > 0 && comp2 == 0)
        };

        if !matches {
            ctx.buffer
                .unsafe_to_concat_from_outbuffer(Some(iter_idx), Some(buffer.idx + 1));
            return None;
        }

        let mark2_glyph = buffer.info[iter_idx].as_glyph();
        let mark2_index = self.mark2_coverage.get(mark2_glyph)?;

        self.marks
            .apply(ctx, self.mark2_matrix, mark1_index, mark2_index, iter_idx)
    }
}

trait MarkArrayExt {
    fn apply(
        &self,
        ctx: &mut hb_ot_apply_context_t,
        anchors: AnchorMatrix,
        mark_index: u16,
        glyph_index: u16,
        glyph_pos: usize,
    ) -> Option<()>;
}

impl MarkArrayExt for MarkArray<'_> {
    fn apply(
        &self,
        ctx: &mut hb_ot_apply_context_t,
        anchors: AnchorMatrix,
        mark_index: u16,
        glyph_index: u16,
        glyph_pos: usize,
    ) -> Option<()> {
        // If this subtable doesn't have an anchor for this base and this class
        // return `None` such that the subsequent subtables have a chance at it.
        let (mark_class, mark_anchor) = self.get(mark_index)?;
        let base_anchor = anchors.get(glyph_index, mark_class)?;

        let (mark_x, mark_y) = mark_anchor.get(ctx.face);
        let (base_x, base_y) = base_anchor.get(ctx.face);

        ctx.buffer
            .unsafe_to_break(Some(glyph_pos), Some(ctx.buffer.idx + 1));

        let idx = ctx.buffer.idx;
        let pos = ctx.buffer.cur_pos_mut();
        pos.x_offset = base_x - mark_x;
        pos.y_offset = base_y - mark_y;
        pos.set_attach_type(attach_type::MARK);
        pos.set_attach_chain((glyph_pos as isize - idx as isize) as i16);

        ctx.buffer.scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
        ctx.buffer.idx += 1;

        Some(())
    }
}

pub mod attach_type {
    pub const MARK: u8 = 1;
    pub const CURSIVE: u8 = 2;
}

/// Just like TryFrom<N>, but for numeric types not supported by the Rust's std.
pub trait TryNumFrom<T>: Sized {
    /// Casts between numeric types.
    fn try_num_from(_: T) -> Option<Self>;
}

impl TryNumFrom<f32> for i32 {
    #[inline]
    fn try_num_from(v: f32) -> Option<Self> {
        // Based on https://github.com/rust-num/num-traits/blob/master/src/cast.rs

        // Float as int truncates toward zero, so we want to allow values
        // in the exclusive range `(MIN-1, MAX+1)`.

        // We can't represent `MIN-1` exactly, but there's no fractional part
        // at this magnitude, so we can just use a `MIN` inclusive boundary.
        const MIN: f32 = core::i32::MIN as f32;
        // We can't represent `MAX` exactly, but it will round up to exactly
        // `MAX+1` (a power of two) when we cast it.
        const MAX_P1: f32 = core::i32::MAX as f32;
        if v >= MIN && v < MAX_P1 {
            Some(v as i32)
        } else {
            None
        }
    }
}

trait DeviceExt {
    fn get_x_delta(&self, face: &hb_font_t) -> Option<i32>;
    fn get_y_delta(&self, face: &hb_font_t) -> Option<i32>;
}

impl DeviceExt for Device<'_> {
    fn get_x_delta(&self, face: &hb_font_t) -> Option<i32> {
        match self {
            Device::Hinting(hinting) => hinting.x_delta(face.units_per_em, face.pixels_per_em()),
            Device::Variation(variation) => face
                .tables()
                .gdef?
                .glyph_variation_delta(
                    variation.outer_index,
                    variation.inner_index,
                    face.variation_coordinates(),
                )
                .and_then(|float| i32::try_num_from(super::round(float))),
        }
    }

    fn get_y_delta(&self, face: &hb_font_t) -> Option<i32> {
        match self {
            Device::Hinting(hinting) => hinting.y_delta(face.units_per_em, face.pixels_per_em()),
            Device::Variation(variation) => face
                .tables()
                .gdef?
                .glyph_variation_delta(
                    variation.outer_index,
                    variation.inner_index,
                    face.variation_coordinates(),
                )
                .and_then(|float| i32::try_num_from(super::round(float))),
        }
    }
}

impl Apply for PositioningSubtable<'_> {
    fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
        match self {
            Self::Single(t) => t.apply(ctx),
            Self::Pair(t) => t.apply(ctx),
            Self::Cursive(t) => t.apply(ctx),
            Self::MarkToBase(t) => t.apply(ctx),
            Self::MarkToLigature(t) => t.apply(ctx),
            Self::MarkToMark(t) => t.apply(ctx),
            Self::Context(t) => t.apply(ctx),
            Self::ChainContext(t) => t.apply(ctx),
        }
    }
}

fn reverse_cursive_minor_offset(
    pos: &mut [GlyphPosition],
    i: usize,
    direction: Direction,
    new_parent: usize,
) {
    let chain = pos[i].attach_chain();
    let attach_type = pos[i].attach_type();
    if chain == 0 || attach_type & attach_type::CURSIVE == 0 {
        return;
    }

    pos[i].set_attach_chain(0);

    // Stop if we see new parent in the chain.
    let j = (i as isize + isize::from(chain)) as _;
    if j == new_parent {
        return;
    }

    reverse_cursive_minor_offset(pos, j, direction, new_parent);

    if direction.is_horizontal() {
        pos[j].y_offset = -pos[i].y_offset;
    } else {
        pos[j].x_offset = -pos[i].x_offset;
    }

    pos[j].set_attach_chain(-chain);
    pos[j].set_attach_type(attach_type);
}

fn propagate_attachment_offsets(
    pos: &mut [GlyphPosition],
    len: usize,
    i: usize,
    direction: Direction,
) {
    // Adjusts offsets of attached glyphs (both cursive and mark) to accumulate
    // offset of glyph they are attached to.
    let chain = pos[i].attach_chain();
    let kind = pos[i].attach_type();
    if chain == 0 {
        return;
    }

    pos[i].set_attach_chain(0);

    let j = (i as isize + isize::from(chain)) as _;
    if j >= len {
        return;
    }

    propagate_attachment_offsets(pos, len, j, direction);

    match kind {
        attach_type::MARK => {
            pos[i].x_offset += pos[j].x_offset;
            pos[i].y_offset += pos[j].y_offset;

            assert!(j < i);
            if direction.is_forward() {
                for k in j..i {
                    pos[i].x_offset -= pos[k].x_advance;
                    pos[i].y_offset -= pos[k].y_advance;
                }
            } else {
                for k in j + 1..i + 1 {
                    pos[i].x_offset += pos[k].x_advance;
                    pos[i].y_offset += pos[k].y_advance;
                }
            }
        }
        attach_type::CURSIVE => {
            if direction.is_horizontal() {
                pos[i].y_offset += pos[j].y_offset;
            } else {
                pos[i].x_offset += pos[j].x_offset;
            }
        }
        _ => {}
    }
}

pub mod GPOS {
    use super::*;

    pub fn position_start(_: &hb_font_t, buffer: &mut hb_buffer_t) {
        let len = buffer.len;
        for pos in &mut buffer.pos[..len] {
            pos.set_attach_chain(0);
            pos.set_attach_type(0);
        }
    }

    pub fn position_finish_advances(_: &hb_font_t, _: &mut hb_buffer_t) {}

    pub fn position_finish_offsets(_: &hb_font_t, buffer: &mut hb_buffer_t) {
        let len = buffer.len;
        let direction = buffer.direction;

        // Handle attachments
        if buffer.scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT != 0 {
            for i in 0..len {
                propagate_attachment_offsets(&mut buffer.pos, len, i, direction);
            }
        }
    }
}