f-office-fonts 0.3.0

Font database, metrics and outlines.
Documentation
use super::{Ligature, Lookup, Lookups};

/// A lookup that joins each of `pairs`, a first glyph, a second one and their ligature.
fn lookup(pairs: &[(u32, u32, u32)]) -> Lookup {
    let mut map = Lookup::new();
    for &(first, second, glyph) in pairs {
        map.entry(first).or_default().push((vec![second], glyph));
    }
    map
}

/// Two lookups: 1 and 2 give 3, then two of 3 give 4.
fn two_lookups() -> Lookups {
    Lookups(vec![lookup(&[(1, 2, 3)]), lookup(&[(3, 3, 4)])])
}

#[test]
fn a_later_lookup_joins_the_ligatures_of_an_earlier_one() {
    // The second lookup takes the first two ligatures of 3, and the third one stays, as its
    // next glyph is 7.
    assert_eq!(
        two_lookups().apply(&[1, 2, 1, 2, 1, 2, 7]),
        [
            Ligature {
                start: 0,
                len: 4,
                glyph: 4,
            },
            Ligature {
                start: 4,
                len: 2,
                glyph: 3,
            },
        ]
    );
    // A ligature is not joined again by the lookup that made it.
    let again = Lookups(vec![lookup(&[(1, 1, 1)])]);
    assert_eq!(
        again.apply(&[1, 1, 1]),
        [Ligature {
            start: 0,
            len: 2,
            glyph: 1,
        }]
    );
}

#[test]
fn joins_the_ligatures_of_a_long_run_in_one_walk_of_each_lookup() {
    // A run of 10^6 glyphs with 500,000 ligatures in the first lookup: a pass that moves the
    // later glyphs for each ligature moves about 2.5 * 10^11 of them and does not end in time.
    let (sender, receiver) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        let glyphs: Vec<u32> = [1, 2].repeat(500_000);
        let _ = sender.send(two_lookups().apply(&glyphs));
    });
    let found = receiver
        .recv_timeout(std::time::Duration::from_secs(30))
        .expect("the ligatures of the run take too long");
    assert_eq!(found.len(), 250_000);
    assert!(found.iter().enumerate().all(|(n, ligature)| *ligature
        == Ligature {
            start: 4 * n,
            len: 4,
            glyph: 4,
        }));
}