allsorts-azul 0.16.2

Azul's fork of the allsorts font parser / shaping engine / subsetter. Adds pixel-snap hinting fixes + assorted bug fixes to YesLogic's upstream. Intended to be upstreamed — use the official `allsorts` crate if you can.
Documentation
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
//! Calculate glyph positions.
//!
//! [GlyphLayout] is used to obtain the positions for a collection of shaped glyphs. The position
//! for each glyph includes its horizontal and vertical advance as well as any `(x, y)` offset from
//! the origin. Horizontal layout in left-to-right and right-to-left directions is supported. Basic
//! (but incomplete) support for vertical text is present too.
//!
//! The position of a series of glyphs is determined from an initial pen position, which is
//! incremented by the advance of each glyph as they are processed. The position of a particular
//! glyph is the current pen position plus `x_offset` and `y_offset`.

use crate::context::Glyph;
use crate::error::ParseError;
use crate::gpos::{Info, Placement};
use crate::tables::FontTableProvider;
use crate::unicode::codepoint::is_upright_char;
use crate::Font;

/// Used to calculate the position of shaped glyphs.
pub struct GlyphLayout<'f, 'i, T>
where
    T: FontTableProvider,
{
    font: &'f mut Font<T>,
    infos: &'i [Info],
    direction: TextDirection,
    vertical: bool,
}

/// The position and advance of a glyph.
#[derive(Copy, Clone, Eq, Debug, Default)]
pub struct GlyphPosition {
    /// Horizontal advance
    pub hori_advance: i32,
    /// Vertical advance
    pub vert_advance: i32,
    /// Offset in the X (horizontal) direction of this glyph
    pub x_offset: i32,
    /// Offset in the Y (vertical) direction of this glyph
    pub y_offset: i32,
    cursive_attachment: Option<u16>,
}

/// The horizontal text layout direction.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum TextDirection {
    LeftToRight,
    RightToLeft,
}

impl<'f, 'i, T: FontTableProvider> GlyphLayout<'f, 'i, T> {
    /// Construct a new `GlyphLayout` instance.
    ///
    /// **Arguments**
    ///
    /// * `font` — the font that the glyphs belong to.
    /// * `infos` — the shaped glyphs to lay out.
    /// * `direction` — the horizontal text layout direction.
    /// * `vertical` — `true` if the text is being laid out top to bottom.
    pub fn new(
        font: &'f mut Font<T>,
        infos: &'i [Info],
        direction: TextDirection,
        vertical: bool,
    ) -> Self {
        GlyphLayout {
            font,
            infos,
            direction,
            vertical,
        }
    }

    /// Retrieve the glyphs positions.
    pub fn glyph_positions(&mut self) -> Result<Vec<GlyphPosition>, ParseError> {
        let mut has_marks = false;
        let mut has_cursive_connection = false;
        let mut positions = vec![GlyphPosition::default(); self.infos.len()];

        for (i, info) in self.infos.iter().enumerate() {
            let (hori_advance, vert_advance) = glyph_advance(self.font, info, self.vertical)?;
            match info.placement {
                Placement::None => positions[i].update(hori_advance, vert_advance, 0, 0),
                Placement::Distance(dx, dy) => {
                    positions[i].update(hori_advance, vert_advance, dx, dy)
                }
                Placement::MarkAnchor(base_index, base_anchor, mark_anchor) => {
                    has_marks = true;
                    match self.infos.get(base_index) {
                        Some(base_info) => {
                            let (dx, dy) = match base_info.placement {
                                Placement::Distance(dx, dy) => (dx, dy),
                                _ => (0, 0),
                            };
                            let offset_x = i32::from(base_anchor.x) - i32::from(mark_anchor.x) + dx;
                            let offset_y = i32::from(base_anchor.y) - i32::from(mark_anchor.y) + dy;
                            positions[i].update(hori_advance, vert_advance, offset_x, offset_y);
                        }
                        None => {
                            return Err(ParseError::BadIndex);
                        }
                    }
                }
                Placement::MarkOverprint(base_index) => {
                    has_marks = true;
                    positions[i].update_advance(0, 0);
                    self.infos.get(base_index).ok_or(ParseError::BadIndex)?;
                }
                Placement::CursiveAnchor(exit_glyph_index, _, _, _) => {
                    has_cursive_connection = true;
                    // Validate index
                    self.infos
                        .get(exit_glyph_index)
                        .ok_or(ParseError::BadIndex)?;

                    // Link to exit glyph
                    positions[exit_glyph_index].cursive_attachment = Some(u16::try_from(i)?);
                    let new_glyph = GlyphPosition {
                        hori_advance,
                        vert_advance,
                        ..positions[i]
                    };
                    positions[i] = new_glyph;
                }
            };
        }

        if has_cursive_connection {
            // Now that we know all base glyphs are positioned we do a second pass to apply
            // cursive attachment adjustments
            self.adjust_cursive_connections(&mut positions);
        }

        if has_marks {
            // Now that cursive connected glyphs are positioned, ensure marks are positioned on their
            // base properly.
            self.position_marks(&mut positions);
        }

        Ok(positions)
    }

    fn adjust_cursive_connections(&self, positions: &mut [GlyphPosition]) {
        for (i, info) in self.infos.iter().enumerate() {
            match info.placement {
                Placement::None
                | Placement::Distance(_, _)
                | Placement::MarkAnchor(_, _, _)
                | Placement::MarkOverprint(_) => {}
                Placement::CursiveAnchor(
                    exit_glyph_index,
                    rtl_flag,
                    exit_glyph_anchor,
                    entry_glyph_anchor,
                ) => {
                    // Anchor alignment can result in horizontal or vertical positioning adjustments,
                    // or both. Note that the positioning effects in the text-layout direction
                    // (horizontal, for horizontal layout) work differently than for the cross-stream
                    // direction (vertical, in horizontal layout):
                    //
                    // * For adjustments in the line-layout direction, the layout engine adjusts the
                    //   advance of the first glyph (in logical order). This effectively moves the
                    //   second glyph relative to the first so that the anchors are aligned in that
                    //   direction.
                    // * For the cross-stream direction, placement of one glyph is adjusted to make
                    //   the anchors align. Which glyph is adjusted is determined by the RIGHT_TO_LEFT
                    //   flag in the parent lookup table: if the RIGHT_TO_LEFT flag is clear, the
                    //   second glyph is adjusted to align anchors with the first glyph; if the
                    //   RIGHT_TO_LEFT flag is set, the first glyph is adjusted to align anchors with
                    //   the second glyph.
                    //
                    // https://docs.microsoft.com/en-us/typography/opentype/spec/gpos#lookup-type-3-cursive-attachment-positioning-subtable

                    // First glyph in logical order is the one with the lower index
                    let (first_glyph_index, second_glyph_index) = if i < exit_glyph_index {
                        (i, exit_glyph_index)
                    } else {
                        (exit_glyph_index, i)
                    };

                    // Line-layout direction
                    // TODO: Handle vertical text
                    match self.direction {
                        TextDirection::LeftToRight => {
                            positions[first_glyph_index].hori_advance =
                                i32::from(entry_glyph_anchor.x)
                        }
                        TextDirection::RightToLeft => {
                            positions[first_glyph_index].hori_advance +=
                                i32::from(entry_glyph_anchor.x)
                        }
                    }

                    // Cross-stream direction
                    let dy = i32::from(exit_glyph_anchor.y) - i32::from(entry_glyph_anchor.y);
                    if rtl_flag {
                        positions[first_glyph_index].y_offset +=
                            dy + positions[second_glyph_index].y_offset;
                        if let Some(linked_index) = positions[first_glyph_index].cursive_attachment
                        {
                            adjust_cursive_chain(
                                dy,
                                self.direction,
                                usize::from(linked_index),
                                self.infos,
                                positions,
                            );
                        }
                    } else {
                        positions[second_glyph_index].y_offset +=
                            dy + positions[first_glyph_index].y_offset;
                        if let Some(linked_index) = positions[second_glyph_index].cursive_attachment
                        {
                            adjust_cursive_chain(
                                dy,
                                self.direction,
                                usize::from(linked_index),
                                self.infos,
                                positions,
                            );
                        }
                    }
                }
            }
        }
    }

    fn position_marks(&self, positions: &mut [GlyphPosition]) {
        for (i, info) in self.infos.iter().enumerate() {
            match info.placement {
                Placement::None
                | Placement::Distance(_, _)
                | Placement::CursiveAnchor(_, _, _, _) => {}
                Placement::MarkAnchor(base_index, _, _) => {
                    let base_pos = positions[base_index];
                    let (hori_advance_offset, vert_advance_offset) = match self.direction {
                        TextDirection::LeftToRight => sum_advance(positions.get(base_index..i)),
                        TextDirection::RightToLeft => sum_advance(positions.get(i..base_index)),
                    };

                    // Add the x & y offset of the base glyph to the mark
                    let position = &mut positions[i];
                    position.x_offset += base_pos.x_offset;
                    position.y_offset += base_pos.y_offset;

                    // Shift the mark back the advance of the base glyph and glyphs leading to it
                    // so that it is positioned above it
                    match self.direction {
                        TextDirection::LeftToRight => {
                            position.x_offset -= hori_advance_offset;
                            position.y_offset -= vert_advance_offset;
                        }
                        TextDirection::RightToLeft => {
                            position.x_offset += hori_advance_offset;
                            position.y_offset += vert_advance_offset;
                        }
                    }
                }
                Placement::MarkOverprint(base_index) => {
                    let base_pos = positions[base_index];
                    let position = &mut positions[i];
                    position.x_offset = base_pos.x_offset;
                    position.y_offset = base_pos.y_offset;
                }
            }
        }
    }
}

impl GlyphPosition {
    pub const fn new(hori_advance: i32, vert_advance: i32, x_offset: i32, y_offset: i32) -> Self {
        GlyphPosition {
            hori_advance,
            vert_advance,
            x_offset,
            y_offset,
            cursive_attachment: None,
        }
    }

    pub fn update(&mut self, hori_advance: i32, vert_advance: i32, x_offset: i32, y_offset: i32) {
        self.hori_advance = hori_advance;
        self.vert_advance = vert_advance;
        self.x_offset = x_offset;
        self.y_offset = y_offset;
    }

    pub fn update_advance(&mut self, hori_advance: i32, vert_advance: i32) {
        self.hori_advance = hori_advance;
        self.vert_advance = vert_advance;
    }
}

impl PartialEq for GlyphPosition {
    fn eq(&self, other: &Self) -> bool {
        self.hori_advance == other.hori_advance
            && self.vert_advance == other.vert_advance
            && self.x_offset == other.x_offset
            && self.y_offset == other.y_offset
    }
}

fn adjust_cursive_chain(
    delta: i32,
    direction: TextDirection,
    index: usize,
    infos: &[Info],
    positions: &mut [GlyphPosition],
) {
    let position = &mut positions[index];
    position.y_offset += delta;
    if let Some(next_index) = position.cursive_attachment {
        // TODO: prevent cycles
        adjust_cursive_chain(delta, direction, usize::from(next_index), infos, positions)
    }
}

fn sum_advance(positions: Option<&[GlyphPosition]>) -> (i32, i32) {
    positions.map_or((0, 0), |p| {
        p.iter().fold((0, 0), |(hori, vert), &pos| {
            (hori + pos.hori_advance, vert + pos.vert_advance)
        })
    })
}

fn glyph_advance<T: FontTableProvider>(
    font: &mut Font<T>,
    info: &Info,
    vertical: bool,
) -> Result<(i32, i32), ParseError> {
    let advance = if vertical && is_upright_glyph(info) {
        font.vertical_advance(info.get_glyph_index())
            .map(i32::from)
            .unwrap_or_else(|| {
                i32::from(font.hhea_table.ascender) - i32::from(font.hhea_table.descender)
            })
            + i32::from(info.kerning)
    } else {
        font.horizontal_advance(info.get_glyph_index())
            .map(i32::from)
            .ok_or(ParseError::MissingValue)?
            + i32::from(info.kerning)
    };
    Ok(if vertical { (0, advance) } else { (advance, 0) })
}

fn is_upright_glyph(info: &Info) -> bool {
    info.glyph.is_vert_alt()
        || info
            .glyph
            .unicodes
            .first()
            .is_some_and(|&ch| is_upright_char(ch))
}

#[cfg(test)]
mod tests {
    use std::error::Error;
    use std::path::Path;

    use super::*;
    use crate::binary::read::ReadScope;
    use crate::font::MatchingPresentation;
    use crate::font_data::FontData;
    use crate::gsub::{FeatureMask, Features};
    use crate::tag;
    use crate::tests::read_fixture;

    fn get_positions(
        text: &str,
        font: &str,
        script: u32,
        lang: u32,
        direction: TextDirection,
    ) -> Result<Vec<GlyphPosition>, Box<dyn Error>> {
        get_positions_with_gpos_features(
            text,
            font,
            script,
            lang,
            &Features::Mask(FeatureMask::default()),
            direction,
            false,
        )
    }

    fn get_positions_with_gpos_features(
        text: &str,
        font: &str,
        script: u32,
        lang: u32,
        features: &Features,
        direction: TextDirection,
        vertical: bool,
    ) -> Result<Vec<GlyphPosition>, Box<dyn Error>> {
        let path = Path::new("tests/fonts").join(font);
        let data = read_fixture(&path);
        let scope = ReadScope::new(&data);
        let font_file = scope.read::<FontData<'_>>()?;
        let provider = font_file.table_provider(0)?;
        let mut font = Font::new(provider)?;

        // Map text to glyphs and then apply font shaping
        let glyphs = font.map_glyphs(text, script, MatchingPresentation::NotRequired);
        let infos = font
            .shape(glyphs, script, Some(lang), features, None, true)
            .map_err(|(err, _info)| err)?;

        let mut layout = GlyphLayout::new(&mut font, &infos, direction, vertical);
        layout.glyph_positions().map_err(|err| err.into())
    }

    #[test]
    fn ltr_kerning() -> Result<(), Box<dyn Error>> {
        let script = tag::LATN;
        let lang = tag!(b"ENG ");
        // V gets kerned closer to A in AV
        let positions = get_positions(
            "AV AA",
            "opentype/Klei.otf",
            script,
            lang,
            TextDirection::LeftToRight,
        )?;
        let expected = &[
            GlyphPosition {
                hori_advance: 597,
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 758,
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 280,
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 777,
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 777,
                ..Default::default()
            },
        ];
        assert_eq!(positions, expected);
        Ok(())
    }

    #[test]
    fn ltr_mark_attach() -> Result<(), Box<dyn Error>> {
        let script = tag::KNDA;
        let lang = tag!(b"KAN ");
        // U+0CBC is a mark on U+0C9F
        let positions = get_positions(
            "\u{0C9F}\u{0CBC}",
            "noto/NotoSansKannada-Regular.ttf",
            script,
            lang,
            TextDirection::LeftToRight,
        )?;
        let expected = &[
            GlyphPosition {
                hori_advance: 1669,
                ..Default::default()
            },
            GlyphPosition {
                x_offset: -260,
                ..Default::default()
            },
        ];
        assert_eq!(positions, expected);
        Ok(())
    }

    #[test]
    fn ltr_attach_distance() -> Result<(), Box<dyn Error>> {
        let script = tag!(b"latn");
        let lang = tag!(b"ENG ");
        let features = Features::Mask(FeatureMask::default() | FeatureMask::FRAC);
        // '⁄' is U+2044 FRACTION SLASH, which when the `frac` GPOS feature is enabled is
        // positioned to be under the previous character and above the next.
        let positions = get_positions_with_gpos_features(
            "1⁄99",
            "opentype/SourceCodePro-Regular.otf",
            script,
            lang,
            &features,
            TextDirection::LeftToRight,
            false,
        )?;
        let expected = &[
            GlyphPosition {
                hori_advance: 600,
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 0,
                x_offset: -300,
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 600,
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 600,
                ..Default::default()
            },
        ];
        assert_eq!(positions, expected);
        Ok(())
    }

    #[test]
    fn ltr_mark_overprint() -> Result<(), Box<dyn Error>> {
        let script = tag::LATN;
        let lang = tag!(b"ENG ");
        // TerminusTTF does not have GPOS or GSUB tables. As a result it hits the fallback mark
        // handling code that results in characters belonging to the Nonspacing Mark General
        // Category to be Mark::Overprint. Combining characters are examples of such characters.
        // This test is 'a' followed by COMBINING TILDE.
        let positions = get_positions(
            "a\u{0303}",
            "opentype/TerminusTTF-4.47.0.ttf",
            script,
            lang,
            TextDirection::LeftToRight,
        )?;
        let expected = &[
            GlyphPosition {
                hori_advance: 500,
                ..Default::default()
            },
            GlyphPosition::default(),
        ];
        assert_eq!(positions, expected);
        Ok(())
    }

    #[test]
    fn ltr_cursive() -> Result<(), Box<dyn Error>> {
        let script = tag::KNDA;
        let lang = tag!(b"KAN ");
        // Text is RTL Arabic with cursive connections
        let positions = get_positions(
            "ಇನ್ಫ್ಲೆಕ್ಷನ್",
            "noto/NotoSansKannada-Regular.ttf",
            script,
            lang,
            TextDirection::LeftToRight,
        )?;
        // [8=0+1457|256=1+1456|118=1+346|335=1+791|282=7+1176|186=10+2096]
        let expected = &[
            GlyphPosition {
                hori_advance: 1457,
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 1456,
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 346, // This glyph's advance gets adjusted to align with the next one
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 791,
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 1176,
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 2096,
                ..Default::default()
            },
        ];
        assert_eq!(positions, expected);
        Ok(())
    }

    #[test]
    fn rtl_cursive() -> Result<(), Box<dyn Error>> {
        let script = tag::ARAB;
        let lang = tag!(b"URD ");
        // Text is RTL Arabic with cursive connections
        let positions = get_positions(
            "لسان",
            "arabic/NafeesNastaleeq.ttf",
            script,
            lang,
            TextDirection::RightToLeft,
        )?;
        let expected = &[
            GlyphPosition {
                hori_advance: 391,
                y_offset: -409,
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 989,
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 213,
                ..Default::default()
            },
            GlyphPosition {
                hori_advance: 1561,
                ..Default::default()
            },
        ];
        assert_eq!(positions, expected);
        Ok(())
    }
}