fm-vector 0.1.0

A notation program's PDF read from its vectors: every notehead, stem, beam and barline where the engraver put it, read into .fmn and compiled to MusicXML, with the words from the PDF's text. No model, no server: it runs in the browser.
Documentation
//! LilyPond's music font (Emmentaler, and feta before it) read as SMuFL. Its glyphs have no code
//! points: a PDF numbers them as it meets them and tells them apart by name (`names.rs`), and the
//! names are the same in every size of the font and every version of LilyPond since 2.6
//! (`noteheads.s2` a black notehead, `rests.3` an eighth rest, `flags.u3` an eighth's flag on an
//! up stem), but for the accidentals, which older versions number by quarter tones
//! (`accidentals.2` a sharp, `accidentals.M2` a flat).
//!
//! The origins are SMuFL's (a notehead's at its left on its line, a flag's at the stem's end, a
//! clef's on its line), and the font's size is four staff spaces as SMuFL's is. Three things
//! differ. The digits of a time signature and of fingering are one set of glyphs, told apart by
//! their size. The dot beside a note and the dots of a repeat sign are one glyph, told apart by
//! place. And the 8 under a tenor's treble clef is a letter of the text font (`page.rs` reads all
//! three).

use std::collections::HashMap;

use crate::legacy::Sym;
use crate::names;

/// A digit, to be told by its size: a time signature's as large as the noteheads, fingering's
/// smaller. The digit is added to this.
pub const DIGIT: u32 = 0x10;
/// A dot, to be told by its place: a repeat sign's where it is one of a pair beside no note,
/// else an augmentation dot.
pub const DOT: u32 = 1;

/// Whether a font is LilyPond's music font, by its name.
pub fn is_font(name: &str) -> bool {
    let n = name.to_ascii_lowercase();
    n.contains("emmentaler") || n.contains("feta")
}

/// The SMuFL glyph of a glyph of LilyPond's font, by its name. None for one the reader has no
/// use for (a brace, a bracket's tip).
pub fn glyph(name: &str) -> Option<Sym> {
    const DIGITS: [&str; 10] = [
        "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
    ];
    if let Some(d) = DIGITS.iter().position(|&d| d == name) {
        return sym(DIGIT + d as u32);
    }
    let code = match name {
        "noteheads.sM1" => 0xE0A0,
        "noteheads.s0" => 0xE0A2,
        "noteheads.s1" => 0xE0A3,
        "noteheads.s2" => 0xE0A4,
        "noteheads.s0cross" => 0xE0A7,
        "noteheads.s1cross" => 0xE0A8,
        "noteheads.s2cross" => 0xE0A9,
        // rests: the whole rest hangs from its origin and the half rest sits on it, as SMuFL's
        // do; `o` is the one with a ledger line, outside the staff
        "rests.M1" | "rests.M1o" => 0xE4E2,
        "rests.0" | "rests.0o" => 0xE4E3,
        "rests.1" | "rests.1o" => 0xE4E4,
        "rests.2" | "rests.2classical" => 0xE4E5,
        "rests.3" => 0xE4E6,
        "rests.4" => 0xE4E7,
        "rests.5" => 0xE4E8,
        "rests.6" => 0xE4E9,
        "flags.u3" => 0xE240,
        "flags.d3" => 0xE241,
        "flags.u4" => 0xE242,
        "flags.d4" => 0xE243,
        "flags.u5" => 0xE244,
        "flags.d5" => 0xE245,
        "flags.u6" => 0xE246,
        "flags.d6" => 0xE247,
        "accidentals.flat" | "accidentals.M2" => 0xE260,
        "accidentals.natural" | "accidentals.0" => 0xE261,
        "accidentals.sharp" | "accidentals.2" => 0xE262,
        "accidentals.doublesharp" | "accidentals.4" => 0xE263,
        "accidentals.flatflat" | "accidentals.M4" => 0xE264,
        "accidentals.leftparen" => 0xE26A,
        "accidentals.rightparen" => 0xE26B,
        "clefs.G" => 0xE050,
        "clefs.G_change" => 0xE07A,
        "clefs.F" => 0xE062,
        "clefs.F_change" => 0xE07C,
        "clefs.C" => 0xE05C,
        "clefs.C_change" => 0xE07B,
        "clefs.percussion" | "clefs.percussion_change" => 0xE069,
        "dots.dot" => DOT,
        "timesig.C44" => 0xE08A,
        "timesig.C22" => 0xE08B,
        "scripts.sforzato" => 0xE4A0,
        "scripts.staccato" => 0xE4A2,
        "scripts.tenuto" => 0xE4A4,
        "scripts.ustaccatissimo" | "scripts.dstaccatissimo" => 0xE4A6,
        "scripts.umarcato" => 0xE4AC,
        "scripts.dmarcato" => 0xE4AD,
        "scripts.ufermata" => 0xE4C0,
        "scripts.dfermata" => 0xE4C1,
        "scripts.trill" => 0xE566,
        "scripts.turn" => 0xE567,
        "scripts.prall" => 0xE56C,
        "scripts.mordent" => 0xE56D,
        "scripts.rcomma" => 0xE4CE,
        "scripts.segno" => 0xE047,
        "scripts.coda" => 0xE048,
        "pedal.Ped" => 0xE650,
        "pedal.*" => 0xE655,
        // dynamics, a letter a glyph
        "p" => 0xE520,
        "m" => 0xE521,
        "f" => 0xE522,
        "r" => 0xE523,
        "s" => 0xE524,
        "z" => 0xE525,
        _ => return None,
    };
    sym(code)
}

/// Every glyph of a PDF's LilyPond fonts, by the interpreter's key for it: what it is, or None
/// for one the reader has no use for.
pub fn glyphs(pdf: &hayro_syntax::Pdf) -> HashMap<u128, Option<Sym>> {
    names::glyph_names(pdf, is_font)
        .into_iter()
        .map(|(key, n)| (key, glyph(&n.glyph)))
        .collect()
}

fn sym(code: u32) -> Option<Sym> {
    Some(Sym {
        code,
        scale: 1.0,
        rise: 0.0,
        shift: 0.0,
    })
}

/// A digit of the font, `size` large, on a page whose noteheads are `heads` large: a time
/// signature's digit, or (smaller) fingering. And how far above its origin SMuFL's is, in ems: the
/// font's digits stand on their origin (a time signature's upper number on the middle line),
/// where SMuFL's time signature digits are centred on theirs.
pub fn digit(d: u32, size: f64, heads: f64) -> (u32, f64) {
    if heads > 0.0 && size < 0.8 * heads {
        (0xED10 + d, 0.0)
    } else {
        (0xE080 + d, 0.25)
    }
}

/// Whether two dots (x, y) are a repeat sign's pair: one over the other, a staff space apart.
pub fn repeat_pair(a: (f64, f64), b: (f64, f64), sp: f64) -> bool {
    (a.0 - b.0).abs() < 0.1 * sp && ((a.1 - b.1).abs() - sp).abs() < 0.15 * sp
}

/// The clef a clef with an 8 set by it is: the 8's baseline `below` the clef's origin by so many
/// staff spaces (above it: negative). None where the 8 stands neither under nor over the clef.
pub fn clef_with_8(clef: u32, below: f64) -> Option<u32> {
    match clef {
        // a treble clef hangs 2.6 spaces under its line and stands 4.4 over it
        0xE050 | 0xE07A if (2.6..6.0).contains(&below) => Some(0xE052),
        0xE050 | 0xE07A if (-7.5..-4.2).contains(&below) => Some(0xE053),
        // a bass clef hangs 2.6 spaces under its line and stands 1 over it
        0xE062 | 0xE07C if (2.6..6.0).contains(&below) => Some(0xE064),
        0xE062 | 0xE07C if (-4.0..-1.0).contains(&below) => Some(0xE065),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_font_is_known_by_its_name_in_every_size() {
        assert!(is_font("Emmentaler-20"));
        assert!(is_font("emmentaler-11"));
        assert!(is_font("feta-alphabet18"));
        assert!(is_font("feta20"));
        assert!(!is_font("CenturySchL-Roma"));
        assert!(!is_font("Aybabtu-Regular"));
    }

    #[test]
    fn a_glyph_is_known_by_its_name() {
        let g = |n: &str| glyph(n).map(|s| s.code);
        assert_eq!(g("noteheads.s2"), Some(0xE0A4));
        assert_eq!(g("noteheads.s1"), Some(0xE0A3));
        assert_eq!(g("rests.3"), Some(0xE4E6));
        assert_eq!(g("flags.d4"), Some(0xE243));
        assert_eq!(g("clefs.F_change"), Some(0xE07C));
        // the accidentals by name, and as older versions number them
        assert_eq!(g("accidentals.sharp"), Some(0xE262));
        assert_eq!(g("accidentals.2"), Some(0xE262));
        assert_eq!(g("accidentals.M2"), Some(0xE260));
        assert_eq!(g("accidentals.0"), Some(0xE261));
        // a dynamic's letter, a digit, the dot
        assert_eq!(g("f"), Some(0xE522));
        assert_eq!(g("four"), Some(DIGIT + 4));
        assert_eq!(g("dots.dot"), Some(DOT));
        // a brace is nothing to the reader, nor a notehead of a shape it does not read
        assert_eq!(g("brace184"), None);
        assert_eq!(g("noteheads.s2slash"), None);
    }

    #[test]
    fn a_digit_is_a_time_signature_s_or_fingering_by_its_size() {
        assert_eq!(digit(4, 20.0, 20.0), (0xE084, 0.25));
        assert_eq!(digit(4, 11.2, 20.0), (0xED14, 0.0));
        // with no noteheads to measure by, a time signature's
        assert_eq!(digit(3, 11.0, 0.0).0, 0xE083);
    }

    #[test]
    fn two_dots_one_over_the_other_are_a_repeat_sign_s() {
        assert!(repeat_pair((100.0, 50.0), (100.0, 55.0), 5.0));
        assert!(repeat_pair((100.0, 55.0), (100.2, 50.1), 5.0));
        // a dot is no pair with itself, nor with one beside it or two spaces off
        assert!(!repeat_pair((100.0, 50.0), (100.0, 50.0), 5.0));
        assert!(!repeat_pair((100.0, 50.0), (103.0, 55.0), 5.0));
        assert!(!repeat_pair((100.0, 50.0), (100.0, 60.0), 5.0));
    }

    #[test]
    fn an_8_under_a_treble_clef_makes_it_a_tenor_s() {
        assert_eq!(clef_with_8(0xE050, 4.0), Some(0xE052));
        assert_eq!(clef_with_8(0xE050, -5.0), Some(0xE053));
        assert_eq!(clef_with_8(0xE062, 3.5), Some(0xE064));
        assert_eq!(clef_with_8(0xE062, -2.0), Some(0xE065));
        // an 8 level with the clef is not its
        assert_eq!(clef_with_8(0xE050, 0.5), None);
        assert_eq!(clef_with_8(0xE0A4, 4.0), None);
    }
}