1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! Two voices' notes at one place set side by side (a unison split: one voice's head moved past
//! the other's), each with an accidental of its own: the engraver sets the two accidentals in
//! the order of their notes, the left one the left note's and the right one, by the heads, the
//! right note's (Gould, "Behind Bars": an altered unison).
use crate::page::Glyph;
use crate::smufl;
use crate::staff::Ev;
/// The accidentals of split unisons, given out in the order of their heads: each such pair of
/// heads takes the two signs at its height before it, and they are taken out of `accs`. The
/// signs as (event, head, sign).
pub fn split_unison_signs(
out: &[Ev],
accs: &mut Vec<Glyph>,
sp: f64,
) -> Vec<(usize, usize, Glyph)> {
let mut given = Vec::new();
for i in 0..out.len() {
for j in 0..out.len() {
if i == j || !out[i].is_note() || !out[j].is_note() {
continue;
}
for (a, ha) in out[i].heads.iter().enumerate() {
for (b, hb) in out[j].heads.iter().enumerate() {
let side = hb.x0 - ha.x0;
if (ha.y - hb.y).abs() >= 0.2 * sp || side < 0.5 * sp || side > 2.5 * sp {
continue;
}
// the signs at their height before the left head
let mut signs: Vec<usize> = (0..accs.len())
.filter(|&k| {
(accs[k].y - ha.y).abs() < 0.3 * sp
&& ha.x0 - accs[k].x1 >= -0.1 * sp
&& ha.x0 - accs[k].x1 < 4.0 * sp
})
.collect();
if signs.len() != 2 {
continue;
}
signs.sort_by(|&p, &q| accs[p].x1.total_cmp(&accs[q].x1));
let (left, right) = (accs[signs[0]], accs[signs[1]]);
// (side by side, the second just after the first: one column each)
if right.x0 < left.x1 - 0.1 * sp || right.x0 - left.x1 > 1.0 * sp {
continue;
}
if smufl::accidental(left.code) == smufl::accidental(right.code) {
continue;
}
accs.remove(signs[0].max(signs[1]));
accs.remove(signs[0].min(signs[1]));
given.push((i, a, left));
given.push((j, b, right));
}
}
}
}
given
}
#[cfg(test)]
mod tests {
use super::*;
use crate::frac::Q;
fn glyph(code: u32, x: f64, y: f64) -> Glyph {
Glyph {
code,
x0: x,
x1: x + 4.0,
y,
size: 20.0,
}
}
fn note(x: f64, y: f64, up: bool) -> Ev {
let mut e = Ev::new(vec![glyph(0xE0A4, x, y)], None, Some(up));
e.dur = Q::ONE;
e
}
#[test]
fn a_split_unison_s_signs_go_in_the_order_of_its_heads() {
// a flat then a natural before two heads at one place, the second set past the first
let out = [note(110.0, 20.0, true), note(116.0, 20.0, false)];
let flat = glyph(0xE260, 101.0, 20.0);
let natural = glyph(0xE261, 105.5, 20.0);
let mut accs = vec![natural, flat];
let given = split_unison_signs(&out, &mut accs, 5.0);
assert!(accs.is_empty());
assert_eq!(given.len(), 2);
assert!(given.contains(&(0, 0, flat)));
assert!(given.contains(&(1, 0, natural)));
// one sign only: left to the heads' own reading
let mut accs = vec![natural];
assert!(split_unison_signs(&out, &mut accs, 5.0).is_empty());
assert_eq!(accs.len(), 1);
}
}