use std::collections::HashMap;
use crate::legacy::Sym;
use crate::names;
pub const DIGIT: u32 = 0x10;
pub const DOT: u32 = 1;
pub fn is_font(name: &str) -> bool {
let n = name.to_ascii_lowercase();
n.contains("emmentaler") || n.contains("feta")
}
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.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,
"p" => 0xE520,
"m" => 0xE521,
"f" => 0xE522,
"r" => 0xE523,
"s" => 0xE524,
"z" => 0xE525,
_ => return None,
};
sym(code)
}
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,
})
}
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)
}
}
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
}
pub fn clef_with_8(clef: u32, below: f64) -> Option<u32> {
match clef {
0xE050 | 0xE07A if (2.6..6.0).contains(&below) => Some(0xE052),
0xE050 | 0xE07A if (-7.5..-4.2).contains(&below) => Some(0xE053),
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));
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));
assert_eq!(g("f"), Some(0xE522));
assert_eq!(g("four"), Some(DIGIT + 4));
assert_eq!(g("dots.dot"), Some(DOT));
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));
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));
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));
assert_eq!(clef_with_8(0xE050, 0.5), None);
assert_eq!(clef_with_8(0xE0A4, 4.0), None);
}
}