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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//! Which of the staves a system's barlines join are a piano's. A program draws a brace round a
//! piano's two staves (or an organ's, a harp's) and a bracket round a choir's; a choir's barlines
//! often run through all its staves, as a piano's do. So where a system has a brace, two joined
//! staves are one instrument's only when a brace holds them, and the other staves of a joined
//! group are parts of their own (a choir's voices). A brace within a bracket holds a choir's
//! divided voices, and is no piano's. A system with no brace drawn says nothing
//! of it, and its joined staves are taken as before (`layout::shapes_of`).
use crate::page::{Page, Staff};
use crate::shown::{self, Tall};
/// The pairs of neighbouring staves (by index) a brace holds, or None when the system has no
/// brace at all.
pub(crate) fn braced(pg: &Page, staves: &[Staff]) -> Option<Vec<usize>> {
let tall = shown::marks(pg, staves).tall;
let braces: Vec<(usize, usize)> = tall
.iter()
.filter(|t| t.0 == Tall::Brace)
.map(|t| (t.1, t.2))
.collect();
if braces.is_empty() {
return None;
}
// (a brace inside a bracket holds two voices of a choir's divided part, not a piano)
let bracketed = |a: usize, b: usize| {
tall.iter()
.any(|t| t.0 == Tall::Line && t.1 <= a && b <= t.2)
};
Some(
braces
.iter()
.filter(|&&(a, b)| b - a == 1 && !bracketed(a, b))
.map(|(a, _)| *a)
.collect(),
)
}
/// A group of staves the barlines join, as parts: each pair a brace holds (`pairs`: the upper
/// staff of each) one part, every other staff a part of its own.
pub(crate) fn split(group: &[usize], pairs: &[usize]) -> Vec<Vec<usize>> {
let mut out = Vec::new();
let mut k = 0;
while k < group.len() {
let i = group[k];
if k + 1 < group.len() && group[k + 1] == i + 1 && pairs.contains(&i) {
out.push(vec![i, i + 1]);
k += 2;
} else {
out.push(vec![i]);
k += 1;
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::page::{Glyph, VLine};
fn staff(top: f64) -> Staff {
Staff {
lines: [top, top + 1.0, top + 2.0, top + 3.0, top + 4.0],
left: 20.0,
right: 100.0,
breaks: Vec::new(),
}
}
#[test]
fn a_choir_s_joined_staves_are_voices_and_the_braced_two_a_piano() {
// four voices whose barlines run through all four (a bracket at their left), and a
// piano under them with a brace
let staves: Vec<Staff> = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0]
.iter()
.map(|&t| staff(t))
.collect();
let v = |x: f64, y0: f64, y1: f64| VLine { x, y0, y1, w: 0.2 };
let brace = Glyph {
code: 0xE000,
x0: 17.0,
x1: 19.0,
y: 64.0,
size: 14.0,
};
let pg = Page {
vlines: vec![
v(18.0, 10.0, 44.0),
v(60.0, 10.0, 44.0),
v(60.0, 50.0, 64.0),
],
glyphs: vec![brace],
..Page::default()
};
let pairs = braced(&pg, &staves).unwrap();
assert_eq!(pairs, [4]);
assert_eq!(split(&[0, 1, 2, 3], &pairs), [[0], [1], [2], [3]]);
assert_eq!(split(&[4, 5], &pairs), [vec![4, 5]]);
// no brace drawn: nothing said
let bare = Page {
vlines: pg.vlines.clone(),
..Page::default()
};
assert!(braced(&bare, &staves).is_none());
// a choir's divided sopranos, braced inside the choir's bracket: two voices
let mut divided = pg.clone();
divided.glyphs.push(Glyph {
code: 0xE000,
x0: 17.0,
x1: 19.0,
y: 24.0,
size: 14.0,
});
divided.vlines.push(v(15.0, 10.0, 44.0));
assert_eq!(braced(÷d, &staves).unwrap(), [4]);
}
}