use crate::page::{Glyph, VLine};
const AT: f64 = 0.25;
const END: f64 = 0.6;
const SECOND: f64 = 0.75;
pub(crate) fn of_another(h: &Glyph, s: &VLine, heads: &[Glyph], stems: &[VLine], sp: f64) -> bool {
let own = stems.iter().any(|o| {
(o.x != s.x || o.y0 != s.y0)
&& (((o.x - h.x1).abs() < AT * sp && o.y0 < h.y - 0.3 * sp && h.y < o.y1 + END * sp)
|| ((o.x - h.x0).abs() < AT * sp && o.y1 > h.y + 0.3 * sp && o.y0 - END * sp < h.y))
});
if !own {
return false;
}
let same = |g: &Glyph| g.x0 == h.x0 && g.y == h.y && g.code == h.code;
let left_of = |g: &Glyph| (s.x - g.x1).abs() < AT * sp;
let right_of = |g: &Glyph| (s.x - g.x0).abs() < AT * sp;
let others = || heads.iter().filter(|g| !same(g));
let near = |g: &Glyph| (g.y - h.y).abs() < SECOND * sp;
if right_of(h) && !left_of(h) {
others().any(|g| left_of(g) && (g.y - s.y1).abs() < END * sp)
&& !others().any(|g| left_of(g) && near(g))
} else if left_of(h) && !right_of(h) {
others().any(|g| right_of(g) && (g.y - s.y0).abs() < END * sp)
&& !others().any(|g| right_of(g) && near(g))
} else {
false
}
}
#[cfg(test)]
mod tests {
use super::*;
fn head(x0: f64, y: f64) -> Glyph {
Glyph {
code: 0xE0A4,
x0,
x1: x0 + 1.2,
y,
size: 4.0,
}
}
fn stem(x: f64, y0: f64, y1: f64) -> VLine {
VLine { x, y0, y1, w: 0.1 }
}
#[test]
fn a_head_touching_the_stem_of_the_note_before_it_is_not_on_that_stem() {
let (low, high) = (head(0.0, 10.0), head(1.25, 6.5));
let heads = [low, high];
let lows = stem(1.2, 1.0, 10.0);
let highs = stem(2.45, 1.0, 6.5);
let stems = [lows, highs];
assert!(of_another(&high, &lows, &heads, &stems, 1.0));
assert!(!of_another(&low, &lows, &heads, &stems, 1.0));
assert!(!of_another(&high, &highs, &heads, &stems, 1.0));
assert!(!of_another(&high, &lows, &heads, &[lows], 1.0));
}
#[test]
fn a_head_touching_the_stem_of_the_note_after_it_is_not_on_that_stem() {
let (first, next) = (head(0.0, 10.0), head(1.25, 6.5));
let heads = [first, next];
let nexts = stem(1.25, 6.5, 14.0);
let stems = [stem(0.0, 10.0, 14.0), nexts];
assert!(of_another(&first, &nexts, &heads, &stems, 1.0));
assert!(!of_another(&next, &nexts, &heads, &stems, 1.0));
}
#[test]
fn a_second_or_a_unison_across_the_stem_is_the_stem_s_own() {
let (c, d) = (head(0.0, 10.0), head(1.2, 9.5));
let s = stem(1.2, 5.0, 10.0);
let all = [
s,
stem(2.4, 5.0, 9.5),
stem(1.2, 10.0, 14.0),
stem(0.0, 9.5, 14.0),
];
assert!(!of_another(&d, &s, &[c, d], &all, 1.0));
let low = head(0.0, 13.0);
assert!(!of_another(
&d,
&stem(1.2, 5.0, 13.0),
&[low, c, d],
&all,
1.0
));
let u = head(1.2, 10.0);
assert!(!of_another(&u, &s, &[c, u], &all, 1.0));
let (e, low_d) = (head(1.2, 9.0), head(0.0, 9.5));
assert!(!of_another(
&low_d,
&stem(1.2, 9.0, 13.0),
&[e, low_d],
&all,
1.0
));
}
#[test]
fn a_head_two_voices_stem_is_on_both_its_stems() {
let (shared, under) = (head(0.0, 10.0), head(0.0, 12.0));
let heads = [shared, under];
let (up, down) = (stem(1.2, 6.0, 10.0), stem(0.0, 10.0, 16.0));
let stems = [up, down];
assert!(!of_another(&shared, &up, &heads, &stems, 1.0));
assert!(!of_another(&shared, &down, &heads, &stems, 1.0));
assert!(!of_another(&under, &down, &heads, &stems, 1.0));
}
}