#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Role {
Main(Family),
Special,
Marks,
Companion,
Unread,
MScore(crate::mscore::Font, f64),
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Family {
Opus,
Maestro,
Doremi,
Capella,
Either,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Decoding {
Code,
Private,
Windows,
Mac,
Shape,
}
pub const DECODINGS: [Decoding; 4] = [
Decoding::Private,
Decoding::Code,
Decoding::Mac,
Decoding::Windows,
];
pub const DOT_BY_PLACE: u32 = 0;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Sym {
pub code: u32,
pub scale: f64,
pub rise: f64,
pub shift: f64,
}
fn sym(code: u32) -> Option<Sym> {
Some(Sym {
code,
scale: 1.0,
rise: 0.0,
shift: 0.0,
})
}
fn risen(code: u32, rise: f64) -> Option<Sym> {
Some(Sym {
code,
scale: 1.0,
rise,
shift: 0.0,
})
}
fn small(code: u32) -> Option<Sym> {
Some(Sym {
code,
scale: 0.6,
rise: 0.0,
shift: 0.0,
})
}
impl Decoding {
pub fn byte(self, code: u32, uni: u32) -> Option<u8> {
match self {
Decoding::Code => u8::try_from(code).ok(),
Decoding::Private => (0xF000..=0xF0FF)
.contains(&uni)
.then(|| (uni - 0xF000) as u8),
Decoding::Windows => windows_byte(uni),
Decoding::Mac => mac_byte(uni),
Decoding::Shape => None,
}
}
}
fn windows_byte(uni: u32) -> Option<u8> {
const HIGH: [u32; 32] = [
0x20AC, 0, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039,
0x0152, 0, 0x017D, 0, 0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC,
0x2122, 0x0161, 0x203A, 0x0153, 0, 0x017E, 0x0178,
];
if let Some(k) = HIGH.iter().position(|&u| u != 0 && u == uni) {
return Some(0x80 + k as u8);
}
(uni < 0x100 && !(0x80..0xA0).contains(&uni)).then_some(uni as u8)
}
fn mac_byte(uni: u32) -> Option<u8> {
const HIGH: [u32; 128] = [
0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, 0x00E0, 0x00E2, 0x00E4,
0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8, 0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF,
0x00F1, 0x00F3, 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC, 0x2020,
0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, 0x00AE, 0x00A9, 0x2122, 0x00B4,
0x00A8, 0x2260, 0x00C6, 0x00D8, 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202,
0x2211, 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8, 0x00BF, 0x00A1,
0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, 0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3,
0x00D5, 0x0152, 0x0153, 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA,
0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02, 0x2021, 0x00B7, 0x201A,
0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC,
0x00D3, 0x00D4, 0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, 0x00AF,
0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7,
];
if uni < 0x80 {
return Some(uni as u8);
}
HIGH.iter().position(|&u| u == uni).map(|k| 0x80 + k as u8)
}
pub fn is_head(byte: u8) -> bool {
matches!(byte, 0xCF | 0xFA | 0x77)
}
pub fn role_by_name(name: &str) -> Option<Role> {
let name = name.split_once('+').map_or(name, |(tag, rest)| {
if tag.len() == 6 && tag.chars().all(|c| c.is_ascii_uppercase()) {
rest
} else {
name
}
});
let n = name.to_ascii_lowercase();
let has = |s: &str| n.contains(s);
let sibelius =
has("opus") || has("helsinki") || has("inkpen") || has("reprise") || has("schottmusic");
let finale = has("maestro")
|| has("petrucci")
|| has("engraver")
|| has("sonata")
|| has("jazzfont")
|| has("broadwaycopyist");
if sibelius {
return Some(if has("chord") {
return None;
} else if has("special") {
Role::Special
} else if has("text") || has("script") {
Role::Marks
} else if has("figured") || has("metronome") || has("roman") || has("percussion") {
Role::Unread
} else {
Role::Main(Family::Opus)
});
}
if finale {
if has("text") || has("times") || has("lyric") {
return None;
}
return Some(Role::Main(Family::Maestro));
}
if has("doremi") {
return Some(Role::Main(Family::Doremi));
}
None
}
pub fn main_glyph(family: Family, byte: u8, tall: bool) -> Option<Sym> {
use Family::*;
if family == Capella {
return capella_glyph(byte);
}
let own = match (family, byte) {
(Doremi, 0x47) => sym(0xE050),
(Doremi, 0x46) => sym(0xE062),
(Doremi, 0x4F) => sym(0xE08B),
(Doremi, 0x3B) => sym(0xE4C0),
(Doremi, 0xAB) => sym(0xE4E6),
(Doremi, 0xEA) => sym(0xE4E7),
(Doremi, 0x00 | 0x21) => sym(0xE242),
(Doremi, 0x66) => sym(0xE522),
(Doremi, 0x6D) => sym(0xE521),
(Doremi, 0x70) => sym(0xE520),
(Doremi, 0x26 | 0x3F | 0x43) => return None,
(Maestro | Either, 0x66) if tall => sym(0xE522),
(Maestro | Either, 0x70) if tall => sym(0xE520),
(Maestro | Either, 0x46) => sym(0xE52D),
(Maestro | Either, 0x50) => sym(0xE52C),
(Maestro | Either, 0xC4) => sym(0xE52F),
(Maestro | Either, 0xB9) => sym(0xE52B),
(Maestro | Either, 0xEC) => sym(0xE530),
(Maestro | Either, 0xB8) => sym(0xE52A),
(Maestro | Either, 0xA7) => sym(0xE539),
(Maestro | Either, 0x53) => sym(0xE536),
(Maestro | Either, 0x5A) => sym(0xE535),
(Maestro | Either, 0xD9) => sym(0xE566),
(Maestro | Either, 0x2C) => sym(0xE4CE),
(Maestro | Either, 0x41) if tall => sym(0xE260),
(Maestro | Either, 0x4E) if tall => sym(0xE261),
(Maestro, 0x61) if tall => sym(0xE262),
(Maestro | Either, 0x8D) => sym(0xE53B),
(Maestro | Either, 0xAF) => sym(0xE529),
(Maestro | Either, 0x56) if tall => sym(0xE052),
(Maestro | Either, 0xFB) => risen(0xE250, 0.25),
(Maestro | Either, 0xF0) => risen(0xE251, -0.25),
(Opus | Either, 0x4B) if tall => sym(0xE250),
(Opus | Either, 0xEF) if tall => sym(0xE251),
_ => None,
};
if own.is_some() {
return own;
}
match byte {
0x77 => sym(0xE0A2),
0xFA => sym(0xE0A3),
0xCF => sym(0xE0A4),
0xC0 => sym(0xE0A9),
0x26 => sym(0xE050),
0x3F => sym(0xE062),
0x42 => sym(0xE05C),
0x62 => sym(0xE260),
0x6E => sym(0xE261),
0x23 => sym(0xE262),
0xDC => sym(0xE263),
0xBA => sym(0xE264),
0x6A => risen(0xE240, 0.25),
0x4A => risen(0xE241, -0.25),
0x72 => risen(0xE242, 0.25),
0x52 => risen(0xE243, -0.25),
0xB7 => risen(0xE4E3, 0.25),
0xEE => sym(0xE4E4),
0xCE => sym(0xE4E5),
0xE4 => sym(0xE4E6),
0xC5 => sym(0xE4E7),
0xA8 => sym(0xE4E8),
0xF4 => sym(0xE4E9),
0x2E => sym(DOT_BY_PLACE),
0x3E => sym(0xE4A0),
0x2D => sym(0xE4A4),
0x5E => sym(0xE4AC),
0x76 => sym(0xE4AD),
0x55 => sym(0xE4C0),
0x75 => sym(0xE4C1),
0x30..=0x39 => sym(0xE080 + (byte - 0x30) as u32),
0x63 => sym(0xE08A),
0x43 => sym(0xE08B),
0xA1 => sym(0xE650),
0x2A => sym(0xE655),
0x25 => sym(0xE047),
0xDE => sym(0xE048),
_ => None,
}
}
pub fn is_capella_head(byte: u8) -> bool {
matches!(byte, 0xE3..=0xE5)
}
fn capella_glyph(byte: u8) -> Option<Sym> {
match byte {
0xE3 => sym(0xE0A2),
0xE4 => sym(0xE0A3),
0xE5 => sym(0xE0A4),
0x41 => sym(0xE050),
0x42 => sym(0xE07A),
0x45 => sym(0xE062),
0x46 => sym(0xE07C),
0x51 => sym(0xE260),
0x52 => sym(0xE261),
0x53 => sym(0xE262),
0x54 => sym(0xE263),
0xE6 => sym(0xE240),
0xE7 => sym(0xE242),
0xE8 => sym(0xE244),
0xEA => sym(0xE241),
0xEB => sym(0xE243),
0xEC => sym(0xE245),
0x49 => risen(0xE4E3, 0.25),
0x4A => sym(0xE4E4),
0x4B => sym(0xE4E5),
0x4C => sym(0xE4E6),
0x4D => sym(0xE4E7),
0x2E => Some(Sym {
code: DOT_BY_PLACE,
scale: 1.0,
rise: 0.0,
shift: 0.354,
}),
0xCA => sym(0xE4A0),
0x75 => sym(0xE4C0),
0x6B => sym(0xE4C1),
0x77 => sym(0xE567),
0x6C => sym(0xE56C),
0xAD => sym(0xE63C),
0x79 => sym(0xE047),
0x30..=0x39 => sym(0xE080 + (byte - 0x30) as u32),
0x3A => sym(0xE08A),
0x3B => sym(0xE08B),
0x66 => sym(0xE522),
0x67 => sym(0xE52F),
0x6A => sym(0xE52D),
0x70 => sym(0xE520),
0x71 => sym(0xE52B),
0x73 => sym(0xE536),
0x7C => sym(0xE534),
0xF7..=0xFF => sym(0xE880 + (byte - 0xF6) as u32),
_ => None,
}
}
pub fn merged_glyph(family: Family, byte: u8, high: f64) -> Option<Sym> {
let tall = high > 0.4;
let opus = matches!(family, Family::Opus | Family::Either);
let dot = matches!(byte, 0xAA | 0x99 | 0x00 | 0x24) && high < 0.15;
let small_head = matches!(byte, 0x65 | 0x66 | 0x45 | 0x46) && high < 0.3;
if opus && (dot || small_head) {
return special_glyph(byte, tall);
}
main_glyph(family, byte, tall)
}
pub fn special_glyph(byte: u8, tall: bool) -> Option<Sym> {
match byte {
0xAA | 0x99 | 0x00 | 0x24 => sym(DOT_BY_PLACE),
0x65 | 0x66 if !tall => small(0xE0A4),
0x45 | 0x46 if !tall => small(0xE0A3),
0x21 | 0xA1 | 0xB0 => small(0xE240),
0x22 | 0xA2 => small(0xE241),
_ => None,
}
}
pub fn companion_glyph(byte: u8, high: f64) -> Option<Sym> {
let tall = high > 0.4;
let dot = matches!(byte, 0xAA | 0x99 | 0x00 | 0x24) && high < 0.15;
let small_head = matches!(byte, 0x65 | 0x66 | 0x45 | 0x46) && high < 0.3;
if dot || small_head {
return special_glyph(byte, tall);
}
marks_glyph(byte, tall)
}
pub fn marks_glyph(byte: u8, tall: bool) -> Option<Sym> {
match byte {
0x70 => sym(0xE520),
0x6D => sym(0xE521),
0x66 if tall => sym(0xE522),
0x72 => sym(0xE523),
0x73 => sym(0xE524),
0x7A => sym(0xE525),
0x30..=0x39 => sym(0xE880 + (byte - 0x30) as u32),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_font_is_known_by_its_name() {
assert_eq!(role_by_name("ABCDEF+Opus"), Some(Role::Main(Family::Opus)));
assert_eq!(role_by_name("OpusStd"), Some(Role::Main(Family::Opus)));
assert_eq!(role_by_name("OpusSpecialStd"), Some(Role::Special));
assert_eq!(role_by_name("OpusText"), Some(Role::Marks));
assert_eq!(role_by_name("OpusChords"), None);
assert_eq!(role_by_name("Maestro"), Some(Role::Main(Family::Maestro)));
assert_eq!(role_by_name("EngraverTextT"), None);
assert_eq!(role_by_name("Doremi"), Some(Role::Main(Family::Doremi)));
assert_eq!(role_by_name("TimesNewRomanPSMT"), None);
assert_eq!(role_by_name("CIDFont+F1"), None);
}
#[test]
fn a_32nd_s_second_flag_is_a_glyph_of_its_own() {
let g = |f: Family, b: u8| main_glyph(f, b, true).map(|s| s.code);
assert_eq!(g(Family::Opus, 0x4B), Some(0xE250));
assert_eq!(g(Family::Opus, 0xEF), Some(0xE251));
assert_eq!(g(Family::Maestro, 0xFB), Some(0xE250));
assert_eq!(g(Family::Maestro, 0xF0), Some(0xE251));
assert_eq!(g(Family::Maestro, 0x4B), None);
assert_eq!(g(Family::Maestro, 0x56), Some(0xE052));
assert_eq!(g(Family::Maestro, 0x41), Some(0xE260));
}
#[test]
fn a_type_1_group_holds_the_music_font_and_its_companion() {
let dot = merged_glyph(Family::Either, 0xAA, 0.09).unwrap();
assert_eq!(dot.code, DOT_BY_PLACE);
assert!(merged_glyph(Family::Either, 0xAA, 0.3).is_none());
assert_eq!(merged_glyph(Family::Either, 0x46, 0.2).unwrap().scale, 0.6);
assert_eq!(
merged_glyph(Family::Either, 0x46, 0.6).unwrap().code,
0xE52D
);
assert_eq!(
merged_glyph(Family::Either, 0xCF, 0.26).unwrap().code,
0xE0A4
);
assert!(merged_glyph(Family::Doremi, 0xAA, 0.09).is_none());
}
#[test]
fn a_companion_with_no_name_is_read_as_either() {
assert_eq!(companion_glyph(0xAA, 0.09).unwrap().code, DOT_BY_PLACE);
assert_eq!(companion_glyph(0x66, 0.26).unwrap().scale, 0.6);
assert_eq!(companion_glyph(0x66, 1.1).unwrap().code, 0xE522);
assert_eq!(companion_glyph(0x33, 0.68).unwrap().code, 0xE883);
assert!(companion_glyph(0xAA, 0.5).is_none());
assert!(companion_glyph(0x21, 0.42).is_none());
}
#[test]
fn capella_s_layout_is_its_own() {
let g = |b: u8| main_glyph(Family::Capella, b, false).map(|s| s.code);
assert_eq!(g(0xE5), Some(0xE0A4));
assert_eq!(g(0x41), Some(0xE050));
assert_eq!(g(0x46), Some(0xE07C));
assert_eq!(g(0x53), Some(0xE262));
assert_eq!(g(0xE8), Some(0xE244));
assert_eq!(g(0xEC), Some(0xE245));
assert_eq!(g(0x6A), Some(0xE52D));
assert_eq!(g(0xF9), Some(0xE883));
assert_eq!(g(0xCF), None);
assert_eq!(g(0x4E), None);
let dot = main_glyph(Family::Capella, 0x2E, false).unwrap();
assert_eq!((dot.code, dot.shift), (DOT_BY_PLACE, 0.354));
}
#[test]
fn a_byte_is_found_from_the_code_or_the_unicode() {
assert_eq!(Decoding::Private.byte(53, 0xF0CF), Some(0xCF));
assert_eq!(Decoding::Code.byte(0xCF, 0x00CF), Some(0xCF));
assert_eq!(Decoding::Windows.byte(53, 0x00CF), Some(0xCF));
assert_eq!(Decoding::Mac.byte(53, 0x0153), Some(0xCF));
assert_eq!(Decoding::Windows.byte(53, 0x0153), Some(0x9C));
assert_eq!(Decoding::Private.byte(53, 0x0153), None);
}
#[test]
fn the_families_share_their_noteheads_and_differ_in_their_clefs() {
for f in [
Family::Opus,
Family::Maestro,
Family::Doremi,
Family::Either,
] {
assert_eq!(main_glyph(f, 0xCF, false).unwrap().code, 0xE0A4);
assert_eq!(main_glyph(f, 0xFA, false).unwrap().code, 0xE0A3);
}
assert_eq!(main_glyph(Family::Opus, 0x26, true).unwrap().code, 0xE050);
assert_eq!(main_glyph(Family::Doremi, 0x47, true).unwrap().code, 0xE050);
assert!(main_glyph(Family::Doremi, 0x26, true).is_none());
assert_eq!(main_glyph(Family::Doremi, 0x46, true).unwrap().code, 0xE062);
assert_eq!(
main_glyph(Family::Maestro, 0x46, true).unwrap().code,
0xE52D
);
assert_eq!(special_glyph(0x66, false).unwrap().scale, 0.6);
assert!(special_glyph(0x66, true).is_none());
assert_eq!(marks_glyph(0x66, true).unwrap().code, 0xE522);
}
}