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
//! The SMuFL code points the reader knows (w3c.github.io/smufl): what a notation program's music
//! font draws, by the character it is set as.

use crate::frac::Q;

/// SMuFL glyphs all stand in Unicode's private-use area; a text font's letters never do.
pub fn is_music(c: u32) -> bool {
    (0xE000..=0xF8FF).contains(&c)
}

/// A notehead's length in quarters: double whole, whole, half, black.
pub fn head(c: u32) -> Option<Q> {
    match c {
        0xE0A0 => Some(Q::int(8)),
        0xE0A2 => Some(Q::int(4)),
        0xE0A3 => Some(Q::int(2)),
        0xE0A4 => Some(Q::ONE),
        // Bravura's own alternates, which Dorico sets: the whole, half and black heads
        0xF4BC => Some(Q::int(4)),
        0xF4BD => Some(Q::int(2)),
        0xF4BE => Some(Q::ONE),
        // x heads (spoken or unpitched notes): whole, half and black
        0xE0A7 => Some(Q::int(4)),
        0xE0A8 => Some(Q::int(2)),
        0xE0A9 => Some(Q::ONE),
        _ => None,
    }
}

/// A notehead's shape when it is not the usual one, by the format's name (`-head:x`).
pub fn head_shape(c: u32) -> Option<&'static str> {
    match c {
        0xE0A7..=0xE0A9 => Some("x"),
        _ => None,
    }
}

pub fn rest(c: u32) -> Option<Q> {
    match c {
        0xE4E2 => Some(Q::int(8)),
        0xE4E3 => Some(Q::int(4)),
        0xE4E4 => Some(Q::int(2)),
        0xE4E5 => Some(Q::ONE),
        0xE4E6 => Some(Q::new(1, 2)),
        0xE4E7 => Some(Q::new(1, 4)),
        0xE4E8 => Some(Q::new(1, 8)),
        0xE4E9 => Some(Q::new(1, 16)),
        _ => None,
    }
}

/// A flag's number of beams (up and down flags alike).
pub fn flag(c: u32) -> Option<u32> {
    match c {
        0xE240..=0xE247 => Some((c - 0xE240) / 2 + 1),
        _ => None,
    }
}

/// A further flag set on a stem beside its flag (SMuFL's internal flags): the older fonts draw a
/// 32nd note's flag as a 16th's and one of these, a 64th's with two.
pub fn is_added_flag(c: u32) -> bool {
    c == 0xE250 || c == 0xE251
}

/// An accidental's alteration in semitones.
pub fn accidental(c: u32) -> Option<i32> {
    match c {
        0xE260 => Some(-1),
        0xE261 => Some(0),
        0xE262 => Some(1),
        0xE263 => Some(2),
        0xE264 => Some(-2),
        _ => None,
    }
}

pub fn is_paren(c: u32) -> bool {
    c == 0xE26A || c == 0xE26B
}

pub const DOT: u32 = 0xE1E7;

pub fn clef(c: u32) -> Option<&'static str> {
    match c {
        0xE050 | 0xE07A => Some("treble"),
        // (an octave lower: plain, the old way, with a C clef, or in parentheses, optional)
        0xE052 | 0xE055 | 0xE056 | 0xE057 => Some("treble8"),
        0xE053 => Some("treble8va"),
        0xE062 | 0xE07C => Some("bass"),
        0xE064 => Some("bass8"),
        0xE065 => Some("bass8va"),
        0xE05C | 0xE07B => Some("alto"),
        // (the percussion clefs: the two bars, and the box)
        0xE069 | 0xE06A => Some("percussion"),
        _ => None,
    }
}

/// A clef by its glyph and the staff line it is set on (1: the bottom line; the glyph's own
/// origin is on the line it names: a G clef's on the G line, a C clef's on the C line). A C clef
/// is the soprano, mezzo-soprano, alto or tenor clef by its line; a G clef on the bottom line and
/// an F clef on the middle line are the old French violin and baritone clefs. Off any line (a
/// font that sets its clefs otherwise), each is the usual one.
pub fn clef_on(c: u32, line: f64) -> Option<&'static str> {
    let usual = clef(c)?;
    let on = |n: f64| (line - n).abs() < 0.25;
    Some(match usual {
        "alto" if on(1.0) => "soprano",
        "alto" if on(2.0) => "mezzo",
        "alto" if on(4.0) => "tenor",
        "alto" if on(5.0) => "baritoneC",
        "treble" if on(1.0) => "french",
        "bass" if on(3.0) => "baritone",
        u => u,
    })
}

/// The diatonic step (octave × 7 + letter) of a clef's bottom line.
pub fn clef_bottom(clef: &str) -> i32 {
    let (letter, octave) = match clef {
        // (a percussion staff's notes are written by their places, as a treble clef names them)
        "treble" | "percussion" => (2, 4), // e4
        "treble8" => (2, 3),
        "treble8va" => (2, 5),
        "french" => (4, 4), // g4
        "bass" => (4, 2),   // g2
        "bass8" => (4, 1),
        "bass8va" => (4, 3),
        "baritone" | "baritoneC" => (6, 2), // b2
        "alto" => (3, 3),                   // f3
        "soprano" => (0, 4),                // c4
        "mezzo" => (5, 3),                  // a3
        _ => (1, 3),                        // tenor: d3
    };
    octave * 7 + letter
}

pub fn time_digit(c: u32) -> Option<u32> {
    (0xE080..=0xE089).contains(&c).then(|| c - 0xE080)
}
pub const COMMON: u32 = 0xE08A;
pub const CUT: u32 = 0xE08B;

pub fn is_time(c: u32) -> bool {
    time_digit(c).is_some() || c == COMMON || c == CUT
}

pub fn dynamic_letter(c: u32) -> Option<char> {
    match c {
        0xE520 => Some('p'),
        0xE521 => Some('m'),
        0xE522 => Some('f'),
        0xE523 => Some('r'),
        0xE524 => Some('s'),
        0xE525 => Some('z'),
        0xE526 => Some('n'),
        _ => None,
    }
}

/// A dynamic drawn as one glyph (SMuFL's ligatures), as its letters.
pub fn dynamic_word(c: u32) -> Option<&'static str> {
    Some(match c {
        0xE529 => "pppp",
        0xE52A => "ppp",
        0xE52B => "pp",
        0xE52C => "mp",
        0xE52D => "mf",
        0xE52E => "pf",
        0xE52F => "ff",
        0xE530 => "fff",
        0xE531 => "ffff",
        0xE534 => "fp",
        0xE535 => "fz",
        0xE536 => "sf",
        0xE537 => "sfp",
        0xE538 => "sfpp",
        0xE539 => "sfz",
        0xE53B => "sffz",
        0xE53C => "rf",
        0xE53D => "rfz",
        _ => return None,
    })
}

/// A dynamic's letters, from a letter glyph or a ligature.
pub fn dynamic_text(c: u32) -> Option<String> {
    dynamic_letter(c)
        .map(String::from)
        .or_else(|| dynamic_word(c).map(String::from))
}

/// A fermata as the format writes it: over the note or under it, the angled one (short), the
/// square one (long).
pub fn fermata(c: u32) -> Option<&'static str> {
    match c {
        0xE4C0 => Some("-fermata"),
        0xE4C1 => Some("-fermatabelow"),
        0xE4C4 | 0xE4C5 => Some("-shortfermata"),
        0xE4C6 | 0xE4C7 => Some("-longfermata"),
        _ => None,
    }
}

/// A piece of an arpeggio's wavy line (the line is a chain of them, set upright).
pub fn is_arpeggio(c: u32) -> bool {
    matches!(c, 0xE63C | 0xE63D | 0xE634 | 0xE635 | 0xEAA9..=0xEAAE)
}

/// An articulation as the format writes it.
pub fn articulation(c: u32) -> Option<&'static str> {
    match c {
        0xE4A2 | 0xE4A3 => Some("-."),
        0xE4A0 | 0xE4A1 => Some("->"),
        0xE4A4 | 0xE4A5 => Some("-_"),
        0xE4AC | 0xE4AD => Some("-^"),
        0xE4A6..=0xE4A9 => Some("-'"),
        _ => None,
    }
}

/// An ornament, a breath, an arpeggio or a tremolo's strokes, as the format writes it.
pub fn ornament(c: u32) -> Option<&'static str> {
    match c {
        0xE566 => Some("-tr"),
        0xE567 | 0xE568 => Some("-turn"),
        0xE56C => Some("-prall"),
        0xE56D => Some("-mordent"),
        0xE4CE | 0xE4CF => Some("-breath"),
        0xE63C | 0xE63D | 0xE634 | 0xE635 | 0xEAA9..=0xEAAE => Some("-arp"),
        0xE220 => Some("-trem1"),
        0xE221 => Some("-trem2"),
        0xE222 => Some("-trem3"),
        0xE223 => Some("-trem4"),
        _ => None,
    }
}

/// Repeat dots: two in one glyph, or one each.
pub fn is_repeat_dot(c: u32) -> bool {
    c == 0xE043 || c == 0xE044
}

pub fn is_tuplet_digit(c: u32) -> bool {
    (0xE880..=0xE889).contains(&c)
}

pub const STEPS: [char; 7] = ['c', 'd', 'e', 'f', 'g', 'a', 'b'];

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

    #[test]
    fn bravura_s_own_heads_are_heads() {
        // Dorico sets these alternates in place of the standard noteheads
        assert_eq!(head(0xF4BC), Some(Q::int(4)));
        assert_eq!(head(0xF4BD), Some(Q::int(2)));
        assert_eq!(head(0xF4BE), Some(Q::ONE));
    }

    #[test]
    fn a_treble_clef_with_an_eight_under_it_is_an_octave_down() {
        // the 8 in parentheses (optional) too
        assert_eq!(clef(0xE052), Some("treble8"));
        assert_eq!(clef(0xE057), Some("treble8"));
    }

    #[test]
    fn a_c_clef_is_named_by_the_line_it_is_set_on() {
        assert_eq!(clef_on(0xE05C, 3.0), Some("alto"));
        assert_eq!(clef_on(0xE05C, 4.02), Some("tenor"));
        assert_eq!(clef_on(0xE07B, 3.98), Some("tenor"));
        assert_eq!(clef_on(0xE05C, 1.0), Some("soprano"));
        assert_eq!(clef_on(0xE05C, 2.0), Some("mezzo"));
        // middle c is on the clef's line
        for (name, line) in [("soprano", 1), ("mezzo", 2), ("alto", 3), ("tenor", 4)] {
            assert_eq!(clef_bottom(name) + 2 * (line - 1), 4 * 7, "{name}");
        }
        // off every line (a font that sets it otherwise): the usual clef
        assert_eq!(clef_on(0xE05C, 2.5), Some("alto"));
        assert_eq!(clef_on(0xE050, 2.0), Some("treble"));
        assert_eq!(clef_on(0xE062, 4.0), Some("bass"));
        assert_eq!(clef_on(0xE0A4, 2.0), None);
    }

    #[test]
    fn clefs_are_read_at_their_pitches_by_their_own_names() {
        // a G clef on the bottom line (g4 there), an F clef on the middle line (f3 there)
        assert_eq!(clef_on(0xE050, 1.0), Some("french"));
        assert_eq!(clef_bottom("french"), 4 * 7 + 4);
        assert_eq!(clef_on(0xE062, 3.0), Some("baritone"));
        assert_eq!(clef_bottom("baritone") + 4, 3 * 7 + 3);
        // a G clef with an 8 over it: an octave up
        assert_eq!(clef(0xE053), Some("treble8va"));
        assert_eq!(clef_bottom("treble8va"), clef_bottom("treble") + 7);
        assert_eq!(clef_bottom("bass8"), clef_bottom("bass") - 7);
        // a percussion clef: its notes are places, named as a treble clef would name them
        assert_eq!(clef_on(0xE069, 3.0), Some("percussion"));
        assert_eq!(clef_bottom("percussion"), clef_bottom("treble"));
        let drums = "part D \"Drums\"
time 4/4
clef D percussion
bar 1
D: c5/4 f4 g5-head:x c5
";
        assert!(fm_transcribe::compile(drums)
            .unwrap()
            .musicxml
            .contains("<unpitched>"));
        // and the format has each by the name it is read as
        for name in [
            "treble",
            "treble8",
            "treble8va",
            "bass",
            "bass8",
            "bass8va",
            "alto",
            "tenor",
            "soprano",
            "mezzo",
            "baritoneC",
            "french",
            "baritone",
        ] {
            let text = format!(
                "part V \"V\" voice\ntime 4/4\nclef V {name}\nbar 1\nV: \\clef:{name} c4/1\n"
            );
            fm_transcribe::compile(&text).unwrap();
        }
    }

    #[test]
    fn fermatas_and_arpeggios_by_their_glyphs() {
        assert_eq!(fermata(0xE4C0), Some("-fermata"));
        assert_eq!(fermata(0xE4C1), Some("-fermatabelow"));
        assert_eq!(fermata(0xE4C6), Some("-longfermata"));
        assert_eq!(fermata(0xE4A2), None);
        // the wavy line's pieces, as MuseScore sets them, and the one-glyph signs
        assert!(is_arpeggio(0xEAA9) && is_arpeggio(0xEAAD) && is_arpeggio(0xE63C));
        assert_eq!(ornament(0xEAA9), Some("-arp"));
        assert!(!is_arpeggio(0xEAA4));
        // and the format has each
        for m in ["-fermatabelow", "-shortfermata", "-longfermata", "-arp"] {
            let text = format!("part V \"V\" voice\ntime 4/4\nbar 1\nV: <c4 e4>/1{m}\n");
            fm_transcribe::compile(&text).unwrap();
        }
    }

    #[test]
    fn x_heads_are_heads() {
        assert_eq!(head_shape(0xE0A9), Some("x"));
        assert_eq!(head_shape(0xE0A4), None);
        assert_eq!(head(0xE0A7), Some(Q::int(4)));
        assert_eq!(head(0xE0A8), Some(Q::int(2)));
        assert_eq!(head(0xE0A9), Some(Q::ONE));
    }
}