use crate::legacy::Sym;
use crate::lilypond;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Font {
One,
Two,
Letters,
}
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,
}
}
pub const HEAD: char = '\u{E12D}';
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,
})
}
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,
})
}
pub fn glyph(font: Font, uni: u32) -> Option<Sym> {
if font == Font::Letters {
return letter(uni);
}
let code = match uni {
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,
})
}
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,
0xE127 => lilypond::DOT,
0xE12A => 0xE0A0,
0xE12B => 0xE0A2,
0xE12C => 0xE0A3,
0xE12D => 0xE0A4,
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,
0xE190..=0xE193 => 0xE240 + 2 * (uni - 0xE190),
0xE194 => 0xE241,
0xE197..=0xE199 => 0xE243 + 2 * (uni - 0xE197),
0xE19A => 0xE05C,
0xE19B => 0xE07B,
0xE19C => 0xE062,
0xE19D => 0xE07C,
0xE19E => 0xE050,
0xE19F => 0xE07A,
0xE1A0 => 0xE069,
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));
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));
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));
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);
assert_eq!(g(0xE043), Some(0xE043));
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);
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);
}
}