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
//! MuseScore's own music font before it took up SMuFL (MScore: MuseScore 1 and 2), read as
//! SMuFL. The font is LilyPond's of its day (feta) with its glyphs set in a row from U+E100 in
//! feta's own order: rests, accidentals, the dot, noteheads, scripts, flags, clefs. Every entry
//! here was either seen in the library's PDFs' glyphs or lies in a run of that order between two
//! that were (the 32nd's flags between the 16th's and the next kind's first; the marcato between
//! the portato and the breath mark). The glyphs' origins are feta's, which are SMuFL's
//! (`lilypond.rs`), and its digits stand on their origin.
//!
//! MuseScore 2 gave the font a few SMuFL code points of its time besides (a repeat sign's dot at
//! U+E043, a staccato dot at U+E4A3), and set its dynamics in a second font (MScoreText) that is
//! SMuFL's throughout; MuseScore 1 set them as letters of the font at another size (MScore1-20).

use crate::legacy::Sym;
use crate::lilypond;

/// Which of MuseScore's old fonts a font is, by its name.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Font {
    /// MuseScore 1's music font (MScore-20): its digits stand on their origin, as feta's do.
    One,
    /// MuseScore 2's (MScore): its digits are centred on their origin, as SMuFL's are.
    Two,
    /// MuseScore 1's font of dynamics and of the notes and accidentals set among words
    /// (MScore1-20): only its dynamics' letters are read.
    Letters,
}

/// The font a name is of: None for another font, and for MScoreText, which is SMuFL's.
pub fn font_by_name(name: &str) -> Option<Font> {
    let n = crate::names::untagged(name).to_ascii_lowercase();
    let rest = n.strip_prefix("mscore")?;
    let rest = rest.trim_end_matches("regular").replace('-', "");
    match rest.as_str() {
        "20" => Some(Font::One),
        "" => Some(Font::Two),
        "120" => Some(Font::Letters),
        _ => None,
    }
}

/// The font's black notehead.
pub const HEAD: char = '\u{E12D}';

/// How large the font's em is drawn next to its size, by the black notehead's advance in ems
/// (0.329 as the font is made): 1 unless the PDF holds the glyphs at another scale.
pub fn em_of(head_advance: Option<f64>) -> f64 {
    match head_advance {
        Some(a) if a > 0.0 && (a / 0.329 - 1.0).abs() > 0.05 => a / 0.329,
        _ => 1.0,
    }
}

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

/// A dynamic's letter, where the font sets them as letters.
fn letter(uni: u32) -> Option<Sym> {
    sym(match char::from_u32(uni)? {
        'p' => 0xE520,
        'm' => 0xE521,
        'f' => 0xE522,
        'r' => 0xE523,
        's' => 0xE524,
        'z' => 0xE525,
        _ => return None,
    })
}

/// The SMuFL glyph of a glyph of the font, by its Unicode.
pub fn glyph(font: Font, uni: u32) -> Option<Sym> {
    if font == Font::Letters {
        return letter(uni);
    }
    let code = match uni {
        // a time signature's digits: MuseScore 1's stand on their origin, where SMuFL's are
        // centred on it
        0x30..=0x39 => {
            return Some(Sym {
                code: 0xE080 + (uni - 0x30),
                scale: 1.0,
                rise: if font == Font::One { 0.25 } else { 0.0 },
                shift: 0.0,
            })
        }
        // rests: whole, half, the two with a ledger line, breve; quarter (and its old form),
        // eighth to 64th
        0xE100 | 0xE102 => 0xE4E3,
        0xE101 | 0xE103 => 0xE4E4,
        0xE106 => 0xE4E2,
        0xE107 | 0xE108 => 0xE4E5,
        0xE109 => 0xE4E6,
        0xE10A => 0xE4E7,
        0xE10B => 0xE4E8,
        0xE10C => 0xE4E9,
        0xE10E => 0xE262,
        0xE113 => 0xE261,
        0xE114 => 0xE260,
        0xE11A => 0xE264,
        0xE11C => 0xE263,
        0xE11D => 0xE26B,
        0xE11E => 0xE26A,
        // the dot beside a note, and (MuseScore 1) of a repeat sign
        0xE127 => lilypond::DOT,
        0xE12A => 0xE0A0,
        0xE12B => 0xE0A2,
        0xE12C => 0xE0A3,
        0xE12D => 0xE0A4,
        // scripts: fermatas, accent, staccato, staccatissimo, tenuto, marcato, turn, trill,
        // segno, coda, breath
        0xE158 => 0xE4C0,
        0xE159 => 0xE4C1,
        0xE161 => 0xE4A0,
        0xE163 => 0xE4A2,
        0xE164 | 0xE165 => 0xE4A6,
        0xE166 => 0xE4A4,
        0xE169 => 0xE4AC,
        0xE16A => 0xE4AD,
        0xE170 => 0xE567,
        0xE171 => 0xE566,
        0xE177 => 0xE047,
        0xE178 => 0xE048,
        0xE17A => 0xE4CE,
        // flags: up from the eighth's, down from the eighth's (two grace flags between)
        0xE190..=0xE193 => 0xE240 + 2 * (uni - 0xE190),
        0xE194 => 0xE241,
        0xE197..=0xE199 => 0xE243 + 2 * (uni - 0xE197),
        // clefs, each with its smaller form for a change
        0xE19A => 0xE05C,
        0xE19B => 0xE07B,
        0xE19C => 0xE062,
        0xE19D => 0xE07C,
        0xE19E => 0xE050,
        0xE19F => 0xE07A,
        0xE1A0 => 0xE069,
        // MuseScore 2's additions: a treble clef with an 8 under it and over it, common and cut
        // time, and what it already set at SMuFL's code points
        0xE1D7 => 0xE052,
        0xE1D8 => 0xE053,
        0xE09A => 0xE08A,
        0xE09B => 0xE08B,
        0xE043 | 0xE4A0 | 0xE4A3 => uni,
        _ => return None,
    };
    sym(code)
}

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

    #[test]
    fn the_font_is_known_by_its_name() {
        assert_eq!(font_by_name("MScore"), Some(Font::Two));
        assert_eq!(font_by_name("BLCXAY+MScore-20"), Some(Font::One));
        assert_eq!(font_by_name("MScore20"), Some(Font::One));
        assert_eq!(font_by_name("MScoreRegular"), Some(Font::Two));
        assert_eq!(font_by_name("MScore120"), Some(Font::Letters));
        // MScoreText is SMuFL's, and Bravura is no font of these
        assert_eq!(font_by_name("MScoreText"), None);
        assert_eq!(font_by_name("BravuraText"), None);
    }

    #[test]
    fn its_glyphs_are_feta_s_in_a_row() {
        let g = |u: u32| glyph(Font::Two, u).map(|s| s.code);
        assert_eq!(g(0xE12D), Some(0xE0A4));
        assert_eq!(g(0xE12C), Some(0xE0A3));
        assert_eq!(g(0xE12B), Some(0xE0A2));
        assert_eq!(g(0xE107), Some(0xE4E5));
        assert_eq!(g(0xE109), Some(0xE4E6));
        assert_eq!(g(0xE10E), Some(0xE262));
        assert_eq!(g(0xE114), Some(0xE260));
        // flags up and down, the grace flags between them unread
        assert_eq!(g(0xE190), Some(0xE240));
        assert_eq!(g(0xE191), Some(0xE242));
        assert_eq!(g(0xE192), Some(0xE244));
        assert_eq!(g(0xE194), Some(0xE241));
        assert_eq!(g(0xE195), None);
        assert_eq!(g(0xE197), Some(0xE243));
        assert_eq!(g(0xE198), Some(0xE245));
        assert_eq!(g(0xE19C), Some(0xE062));
        assert_eq!(g(0xE19E), Some(0xE050));
        assert_eq!(g(0xE1D7), Some(0xE052));
        assert_eq!(g(0xE127), Some(lilypond::DOT));
        // a time signature's digit, raised to SMuFL's origin
        let four = glyph(Font::One, 0x34).unwrap();
        assert_eq!((four.code, four.rise), (0xE084, 0.25));
        assert_eq!(glyph(Font::Two, 0x34).unwrap().rise, 0.0);
        // what MuseScore 2 already set as SMuFL
        assert_eq!(g(0xE043), Some(0xE043));
        // a font held at a third of its size
        assert!((em_of(Some(0.112)) - 0.34).abs() < 0.01);
        assert_eq!(em_of(Some(0.326)), 1.0);
        assert_eq!(em_of(None), 1.0);
        // the font of letters: a dynamic's letter and nothing else
        assert_eq!(
            glyph(Font::Letters, 'f' as u32).map(|s| s.code),
            Some(0xE522)
        );
        assert_eq!(glyph(Font::Letters, 0xE10D), None);
        assert_eq!(glyph(Font::One, 'f' as u32), None);
    }
}