use crate::page::Glyph;
use crate::smufl;
use crate::staff::Ev;
pub const DOT_UNCLAIMED: &str = "a dot by its notes that none of them took";
pub const DOTS_UNFIT: &str = "dots beside two voices' notes that fit neither";
pub const DOT_EITHER: &str = "a dot that either of two voices' notes could claim";
pub const SIGN_UNCLAIMED: &str = "an accidental by its notes that none of them took";
pub const KEPT: &[&str] = &[
DOT_UNCLAIMED,
SIGN_UNCLAIMED,
DOTS_UNFIT,
PAD_NUMBER,
PAD_SMALL,
TIE_FROM_UNREAD_SIGNED,
SMALL_BY_REST,
SMALL_PADDED,
TREMOLO_UNLIKE,
];
pub fn kept(evs: &[Ev], of_bar: &[&'static str]) -> Option<&'static str> {
evs.iter()
.flat_map(|e| e.doubts.iter().copied())
.chain(of_bar.iter().copied())
.find(|d| KEPT.contains(d))
}
pub fn sure(
evs: &[Ev],
of_bar: &[&'static str],
voices: Result<Vec<Vec<Ev>>, String>,
) -> Result<Vec<Vec<Ev>>, String> {
let Ok(vs) = voices else {
return voices;
};
if !tremolos_paired(&vs) {
return Err(TREMOLO_APART.to_string());
}
let all: Vec<Ev> = evs.iter().chain(vs.iter().flatten()).cloned().collect();
match kept(&all, of_bar) {
Some(d) if vs.iter().flatten().any(|e| e.is_note()) => Err(d.to_string()),
_ => Ok(vs),
}
}
pub const TREMOLO_UNLIKE: &str = "a tremolo's strokes between two notes of different lengths";
pub const TREMOLO_APART: &str =
"a tremolo between two notes that are not one voice's, one after the other";
pub fn tremolos_paired(voices: &[Vec<Ev>]) -> bool {
use crate::staff::Trem;
voices.iter().all(|v| {
let main: Vec<&Ev> = v.iter().filter(|e| e.grace.is_none()).collect();
(0..main.len()).all(|k| match main[k].tremolo {
Some(Trem::Start(_)) => main
.get(k + 1)
.is_some_and(|n| n.is_note() && n.tremolo == Some(Trem::Stop)),
Some(Trem::Stop) => k > 0 && matches!(main[k - 1].tremolo, Some(Trem::Start(_))),
_ => true,
})
})
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Dotted {
pub dots: Option<usize>,
pub taken: Vec<(u64, u64)>,
pub shared: Vec<(u64, u64)>,
pub doubt: Option<&'static str>,
}
type Fit = (f64, Vec<usize>, Vec<(usize, (u64, u64), (u64, u64))>);
fn key(g: &Glyph) -> (u64, u64) {
(g.x0.to_bits(), g.y.to_bits())
}
pub fn dots_by_column(out: &[Ev], heads: &[Glyph], inside: &[Glyph], sp: f64) -> Vec<Dotted> {
let mut said = vec![Dotted::default(); out.len()];
let mut notes: Vec<usize> = (0..out.len())
.filter(|&i| out[i].is_note() && out[i].grace.is_none() && !out[i].heads.is_empty())
.collect();
notes.sort_by(|&a, &b| out[a].left().total_cmp(&out[b].left()));
let mut groups: Vec<(Vec<usize>, f64)> = Vec::new();
for i in notes {
match groups.last_mut() {
Some((g, right)) if out[i].left() < *right + 0.2 * sp => {
g.push(i);
*right = right.max(out[i].right());
}
_ => groups.push((vec![i], out[i].right())),
}
}
for (group, right) in groups {
if group.len() < 2 || group.len() > 5 {
continue;
}
let mine = |h: &Glyph| {
group
.iter()
.any(|&i| out[i].heads.iter().any(|o| key(o) == key(h)))
};
let (hi, lo) = group
.iter()
.flat_map(|&i| out[i].heads.iter())
.fold((f64::MAX, f64::MIN), |a, h| (a.0.min(h.y), a.1.max(h.y)));
let next = |y: f64| {
heads
.iter()
.filter(|h| !mine(h) && h.x0 > right - 0.1 * sp && (h.y - y).abs() < 1.0 * sp)
.map(|h| h.x0)
.fold(1e18, f64::min)
};
let nearest = group
.iter()
.map(|&i| out[i].right())
.fold(f64::MAX, f64::min);
let mut dots: Vec<&Glyph> = inside
.iter()
.filter(|g| {
g.code == smufl::DOT
&& 0.0 < g.x0 - nearest
&& g.x0 - right < 2.6 * sp
&& hi - 0.9 * sp < g.y
&& g.y < lo + 0.9 * sp
&& g.x0 < next(g.y)
})
.collect();
let first = dots.iter().map(|g| g.x0).fold(f64::MAX, f64::min);
let last = dots.iter().map(|g| g.x0).fold(f64::MIN, f64::max);
if dots.is_empty() || first - right > 1.4 * sp || last < right {
continue;
}
dots.sort_by(|a, b| a.y.total_cmp(&b.y));
let n = group.len();
let mut fits: Vec<Fit> = Vec::new();
for choice in 1..3usize.pow(n as u32) {
let many: Vec<usize> = (0..n).map(|k| choice / 3usize.pow(k as u32) % 3).collect();
if (0..n).any(|k| many[k] == 0 && heads_dotted_by_another(out, &group, &many, k)) {
continue;
}
let mut hs: Vec<(usize, f64, f64, (u64, u64))> = Vec::new();
for (k, &i) in group.iter().enumerate() {
let mut at: Vec<(u64, u64)> = out[i].heads.iter().map(key).collect();
at.sort();
at.dedup();
at.retain(|p| !shared_before(out, &group, &many, k, p));
for _ in 0..many[k] {
hs.extend(
at.iter()
.map(|p| (k, f64::from_bits(p.1), out[i].right(), *p)),
);
}
}
if hs.len() != dots.len() {
continue;
}
hs.sort_by(|a, b| a.1.total_cmp(&b.1));
let off: Vec<f64> = hs
.iter()
.zip(&dots)
.map(|(h, d)| (h.1 - d.y).abs())
.collect();
let past = hs.iter().zip(&dots).all(|(h, d)| d.x0 > h.2);
let twice = (0..hs.len()).any(|a| {
(0..a).any(|b| {
(hs[a].0, hs[a].1) == (hs[b].0, hs[b].1)
&& (dots[a].x0 - dots[b].x0).abs() < 0.3 * sp
})
});
let past = past && !twice;
if past && off.iter().all(|&o| o < 0.65 * sp) {
let pairs = hs
.iter()
.zip(&dots)
.map(|(h, d)| (h.0, key(d), h.3))
.collect();
fits.push((off.iter().sum(), many, pairs));
}
}
fits.sort_by(|a, b| a.0.total_cmp(&b.0));
let doubt = match fits.len() {
0 => Some(DOTS_UNFIT),
1 => None,
_ if fits[1].0 - fits[0].0 > 0.4 * sp => None,
_ => Some(DOT_EITHER),
};
if let Some(d) = doubt {
for &i in &group {
said[i].doubt = Some(d);
}
continue;
}
let (_, many, pairs) = &fits[0];
for (k, &i) in group.iter().enumerate() {
said[i].dots = Some(many[k]);
said[i].taken = pairs.iter().filter(|p| p.0 == k).map(|p| p.1).collect();
}
for &(k, d, p) in pairs {
for (o, &j) in group.iter().enumerate() {
if o != k && shared_before(out, &group, many, o, &p) {
said[group[k]].shared.push(d);
said[j].shared.push(d);
said[j].taken.push(d);
}
}
}
}
said
}
fn has_head(e: &Ev, p: &(u64, u64)) -> bool {
e.heads.iter().any(|h| key(h) == *p)
}
fn shared_before(out: &[Ev], group: &[usize], many: &[usize], k: usize, p: &(u64, u64)) -> bool {
black(&out[group[k]], p) && (0..k).any(|o| many[o] == many[k] && has_head(&out[group[o]], p))
}
fn heads_dotted_by_another(out: &[Ev], group: &[usize], many: &[usize], k: usize) -> bool {
let e = &out[group[k]];
!e.heads.is_empty()
&& e.heads.iter().all(|h| {
smufl::head(h.code) == Some(crate::frac::Q::ONE)
&& (0..group.len())
.any(|o| o != k && many[o] > 0 && has_head(&out[group[o]], &key(h)))
})
}
fn black(e: &Ev, p: &(u64, u64)) -> bool {
(e.heads.iter()).any(|h| key(h) == *p && smufl::head(h.code) == Some(crate::frac::Q::ONE))
}
pub fn unclaimed_dots(
out: &mut [Ev],
inside: &[Glyph],
others: &[Glyph],
taken: &[(u64, u64)],
(top, bottom): (f64, f64),
sp: f64,
) {
for g in inside.iter().filter(|g| g.code == smufl::DOT) {
if taken.contains(&key(g)) || g.y < top - 4.0 * sp || g.y > bottom + 4.0 * sp {
continue;
}
let by = |h: &Glyph| {
g.x0 > h.x1 - 0.2 * sp && g.x0 - h.x1 < 3.0 * sp && (h.y - g.y).abs() < 0.8 * sp
};
if others.iter().any(by) {
continue;
}
let near = out
.iter_mut()
.filter(|e| {
let (r, y) = match &e.rest {
crate::staff::Rest::Printed(r) => (r.x1, r.y),
_ => (e.right(), e.y()),
};
let ys = e.heads.iter().map(|h| h.y).chain([y]);
g.x0 > r - 0.2 * sp
&& g.x0 - r < 3.0 * sp
&& ys.into_iter().any(|y| (y - g.y).abs() < 1.2 * sp)
})
.min_by(|a, b| (g.x0 - a.x).abs().total_cmp(&(g.x0 - b.x).abs()));
if let Some(e) = near {
e.doubts.push(DOT_UNCLAIMED);
}
}
}
pub fn unclaimed_signs(out: &mut [Ev], left: &[Glyph], sp: f64) {
for g in left {
let near = out.iter_mut().find(|e| {
e.heads.iter().any(|h| {
h.x0 - g.x1 > -0.2 * sp && h.x0 - g.x1 < 4.0 * sp && (h.y - g.y).abs() < 1.2 * sp
})
});
if let Some(e) = near {
e.doubts.push(SIGN_UNCLAIMED);
}
}
}
pub const PAD_UNCONFIRMED: &str =
"a note after time the reader filled in, where no other note's column says it starts";
pub const PAD_BESIDE: &str =
"a note after time the reader filled in, set clear of the note it is said to start with";
pub const PAD_ANY: &str = "time the reader filled in";
pub const PAD_INNER: &str = "time the reader filled in between a voice's notes";
pub const SMALL_TIMED: &str = "small notes read as taking time";
pub const SMALL_GRACE: &str =
"small notes read as grace notes, with stems of their own and no slash";
pub const PAD_TWICE: &str = "a voice the reader filled in time for in two places";
pub const SMALL_APART: &str =
"a small note read as a grace note that stands clear of the next note";
pub const SMALL_BY_REST: &str = "a small note read as a grace note, at a rest's place";
pub const SMALL_PADDED: &str =
"small notes after the bar's last note, read as taking time the reader filled in before them";
pub const PAD_NUMBER: &str =
"time the reader filled in, by a tuplet's number no notes were given to";
pub const PAD_SMALL: &str =
"time the reader filled in between a voice's notes, by small notes read as grace notes";
pub const SHARED_HEAD: &str = "a head two stems share";
pub const NUMBER_LOOSE: &str = "a tuplet's number no notes were given to";
pub const VOICES_MANY: &str = "more than two voices";
pub const INK_STEM_LEFT: &str = "a stem left bare by the head at its end";
pub const INK_STEM_BARE: &str = "a stem no head hangs from";
pub const INK_BEAM: &str = "a beam across the staff's stems that no note took";
pub const INK_FLAG: &str = "a flag by no note's stem";
pub fn of_bar(
evs: &[Vec<Ev>],
voices: &[Result<Vec<Vec<Ev>>, String>],
sp: f64,
) -> Vec<Vec<&'static str>> {
use crate::columns::{column_x, Mark};
use crate::frac::Q;
use crate::staff::Rest;
let marked: Vec<Vec<(Mark, bool, usize)>> = voices
.iter()
.map(|v| {
let mut out = Vec::new();
for (vi, voice) in v.iter().flatten().enumerate() {
let (mut t, mut after) = (Q::ZERO, false);
for e in voice {
after |= e.rest == Rest::Blank;
if e.is_note() && e.grace.is_none() {
let m = Mark {
x: column_x(e),
at: t,
play: 0.0,
wide: false,
sign: false,
};
out.push((m, after, vi));
}
t += e.takes();
}
}
out
})
.collect();
let mut all = Vec::new();
for (i, v) in voices.iter().enumerate() {
let mut ds: Vec<&'static str> = Vec::new();
let Ok(vs) = v else {
all.push(ds);
continue;
};
let blank = |v: &Vec<Ev>| v.iter().any(|e| e.rest == Rest::Blank);
if vs.iter().any(blank) {
ds.push(PAD_ANY);
}
let inner = vs.iter().any(|v| {
let first = v.iter().position(|e| e.is_note());
let last = v.iter().rposition(|e| e.is_note());
match (first, last) {
(Some(a), Some(b)) => v[a..b].iter().any(|e| e.rest == Rest::Blank),
_ => false,
}
});
if inner {
ds.push(PAD_INNER);
}
let sure = |x: f64, reach: f64| {
marked
.iter()
.flatten()
.filter(|(m, after, _)| !after && (m.x - x).abs() <= reach * sp)
.min_by(|a, b| (a.0.x - x).abs().total_cmp(&(b.0.x - x).abs()))
.map(|(m, _, _)| m.at)
};
for (m, after, vi) in &marked[i] {
if !after {
continue;
}
if sure(m.x, 0.7) != Some(m.at) {
ds.push(PAD_UNCONFIRMED);
let clear = marked[i]
.iter()
.any(|(o, oa, ov)| !oa && ov != vi && o.at == m.at && m.x - o.x > 0.7 * sp);
if clear {
ds.push(PAD_BESIDE);
}
}
}
let small = |e: &Ev| !e.heads.is_empty() && e.heads.iter().all(|h| h.size < 0.8 * 4.0 * sp);
if evs[i].iter().any(|e| small(e) && e.grace.is_none()) {
ds.push(SMALL_TIMED);
}
if evs[i]
.iter()
.any(|e| small(e) && e.grace == Some(false) && e.stem.is_some())
{
ds.push(SMALL_GRACE);
}
let keys: Vec<Vec<(u64, u64)>> = evs[i]
.iter()
.map(|e| e.heads.iter().map(key).collect())
.collect();
let shared = (0..keys.len()).any(|a| {
(0..a).any(|b| {
evs[i][a].dur != evs[i][b].dur && keys[a].iter().any(|k| keys[b].contains(k))
})
});
if shared {
ds.push(SHARED_HEAD);
}
if evs[i]
.iter()
.any(|e| e.loose.iter().any(|l| l.3) && e.tuplets.is_empty())
{
ds.push(NUMBER_LOOSE);
}
if vs.len() > 2 {
ds.push(VOICES_MANY);
}
let runs = |v: &Vec<Ev>| {
let mut n = 0;
let mut was = false;
for e in v {
let b = e.rest == Rest::Blank;
if b && !was {
n += 1;
}
was = b;
}
n
};
if vs
.iter()
.any(|v| runs(v) >= 2 && v.iter().any(|e| e.is_note()))
{
ds.push(PAD_TWICE);
}
let apart = evs[i].iter().any(|e| {
let next = evs[i]
.iter()
.filter(|o| o.is_note() && o.grace.is_none() && o.left() > e.left() + 0.2 * sp)
.map(|o| o.left())
.fold(f64::MAX, f64::min);
small(e) && e.grace == Some(false) && e.stem.is_some() && next - e.right() > 2.0 * sp
});
if apart {
ds.push(SMALL_APART);
}
let lone = |e: &&Ev| small(e) && e.grace == Some(false) && e.stem.is_some();
let by_rest = |e: &Ev| {
evs[i].iter().any(|o| match &o.rest {
Rest::Printed(g) => (g.x0 - e.left()).abs() < 0.7 * sp && g.size > 0.8 * 4.0 * sp,
_ => false,
})
};
if vs.iter().flatten().filter(lone).any(by_rest) {
ds.push(SMALL_BY_REST);
}
let last_full = (evs[i].iter())
.filter(|e| e.is_note() && !small(e))
.map(|e| e.left())
.fold(f64::MIN, f64::max);
let small_padded = last_full > f64::MIN
&& vs.iter().any(|v| {
let mut notes = v.iter().filter(|e| e.is_note() && e.grace.is_none());
v.iter().any(|e| e.rest == Rest::Blank)
&& !v.iter().any(|e| matches!(e.rest, Rest::Printed(_)))
&& v.iter().any(|e| e.is_note())
&& notes.all(|e| small(e) && e.left() > last_full + 0.6 * sp)
});
if small_padded {
ds.push(SMALL_PADDED);
}
if ds.contains(&PAD_ANY) && ds.contains(&NUMBER_LOOSE) {
ds.push(PAD_NUMBER);
}
if ds.contains(&PAD_TWICE) && ds.contains(&PAD_INNER) && ds.contains(&SMALL_GRACE) {
ds.push(PAD_SMALL);
}
ds.sort();
ds.dedup();
all.push(ds);
}
all
}
pub const TIE_IN_UNCARRIED: &str = "a tie from the system before whose note was not found";
pub const TIE_FAILED: &str = "a curve between two notes at one height that was not read as a tie";
pub const TIE_NO_START: &str = "a curve like a tie whose first note was not found";
pub const TIE_DROPPED: &str = "a tie with no note found to land on";
pub const TIE_ENDS: &str = "a tie ends by it";
pub const TIE_FROM_UNREAD: &str = "a tie from a bar that was not read";
pub const TIE_FROM_UNREAD_SIGNED: &str =
"a tie from a bar that was not read, which has an accidental on its place";
pub const TIE_PARTIAL: &str = "a chord some of whose ties were not read";
pub fn of_curve(
incoming: Option<&dyn Fn(i32) -> bool>,
start: Option<&Ev>,
end: &Ev,
cy: f64,
sp: f64,
) -> Option<&'static str> {
let k = ends_by(end, cy, sp)?;
let h = &end.heads[k];
match (incoming, start) {
(Some(carried), _) => (!carried(end.place_of[k])).then_some(TIE_IN_UNCARRIED),
(None, Some(s)) => {
let same = s.heads.iter().any(|o| (o.y - h.y).abs() < 0.2 * sp);
(same && s.x < end.x - 0.5 * sp).then_some(TIE_FAILED)
}
(None, None) => Some(TIE_NO_START),
}
}
pub fn tie_dropped(bar: &mut Result<Vec<Vec<Ev>>, String>) {
let first = bar
.iter_mut()
.flatten()
.flatten()
.find(|e| e.is_note() && e.grace.is_none());
if let Some(e) = first {
e.doubts.push(TIE_DROPPED);
}
}
pub fn ends_by(end: &Ev, cy: f64, sp: f64) -> Option<usize> {
let (k, h) = end
.heads
.iter()
.enumerate()
.min_by(|a, b| (a.1.y - cy).abs().total_cmp(&(b.1.y - cy).abs()))?;
((h.y - cy).abs() <= 1.5 * sp && !end.accs.contains_key(end.place_of.get(k)?)).then_some(k)
}
pub fn in_force(
printed: &[(crate::frac::Q, i32, i32)],
tied_in: &std::collections::BTreeMap<i32, i32>,
) -> std::collections::BTreeMap<i32, i32> {
let mut left = tied_in.clone();
left.extend(printed.iter().map(|&(_, p, a)| (p, a)));
left
}
#[cfg(test)]
mod tests {
use super::*;
use crate::frac::Q;
use crate::staff::Rest;
const SP: f64 = 10.0;
fn glyph(code: u32, x: f64, y: f64) -> Glyph {
Glyph {
code,
x0: x,
x1: x + if code == smufl::DOT { 4.0 } else { 12.0 },
y,
size: 40.0,
}
}
fn head(x: f64, y: f64) -> Glyph {
glyph(0xE0A4, x, y)
}
fn dot(x: f64, y: f64) -> Glyph {
glyph(smufl::DOT, x, y)
}
fn chord(x: f64, ys: &[f64], up: bool) -> Ev {
let mut e = Ev::new(ys.iter().map(|&y| head(x, y)).collect(), None, Some(up));
e.dur = Q::ONE;
e.place_of = ys.iter().map(|y| 40 - (*y / 5.0) as i32).collect();
e.places = e.place_of.clone();
e
}
fn said(out: &[Ev], dots: &[Glyph]) -> Vec<Dotted> {
let heads: Vec<Glyph> = out.iter().flat_map(|e| e.heads.clone()).collect();
dots_by_column(out, &heads, dots, SP)
}
#[test]
fn a_dot_past_another_voice_s_head_is_its_note_s() {
let out = [
chord(100.0, &[20.0, 40.0], false),
chord(113.0, &[15.0, 50.0], true),
];
let dots = [
dot(130.0, 15.0),
dot(130.0, 20.0),
dot(130.0, 40.0),
dot(130.0, 45.0),
];
let s = said(&out, &dots);
assert_eq!((s[0].dots, s[1].dots), (Some(1), Some(1)));
assert_eq!(s[0].taken.len(), 2);
let out = [chord(100.0, &[25.0], false), chord(113.0, &[20.0], true)];
let s = said(&out, &[dot(130.0, 20.0)]);
assert_eq!((s[0].dots, s[1].dots), (Some(0), Some(1)));
}
#[test]
fn dots_one_after_the_other_are_one_note_s_and_one_over_the_other_two_notes() {
let out = [chord(100.0, &[20.0], false), chord(113.0, &[20.0], true)];
let s = said(&out, &[dot(130.0, 20.0), dot(130.0, 25.0)]);
assert_eq!((s[0].dots, s[1].dots), (Some(1), Some(1)));
let s = said(&out, &[dot(130.0, 20.0)]);
assert_eq!((s[0].dots, s[0].doubt), (None, Some(DOT_EITHER)));
let s = said(
&out,
&[dot(130.0, 15.0), dot(130.0, 20.0), dot(130.0, 25.0)],
);
assert_eq!(s[1].doubt, Some(DOTS_UNFIT));
}
#[test]
fn a_black_head_two_stems_share_has_its_dot_for_both_notes() {
let down = chord(100.0, &[20.0, 30.0], false);
let up = chord(100.0, &[20.0], true);
let dots = [dot(116.0, 20.0), dot(116.0, 30.0)];
let s = said(&[down.clone(), up.clone()], &dots);
assert_eq!((s[0].dots, s[1].dots), (Some(1), Some(1)));
assert_eq!((s[0].shared.len(), s[1].shared.len()), (1, 1));
assert_eq!(s[0].shared, s[1].shared);
let hollow = |mut e: Ev| {
e.heads.iter_mut().for_each(|h| h.code = 0xE0A3);
e
};
let s = said(&[hollow(down), hollow(up)], &dots);
assert_eq!((s[0].dots, s[1].dots), (Some(1), Some(0)));
assert!(s[0].shared.is_empty());
}
#[test]
fn a_chord_clear_of_the_other_voice_s_keeps_its_dots_by_its_own_heads() {
let mut wide = chord(100.0, &[10.0, 15.0], true);
wide.heads[1].x0 = 112.0;
wide.heads[1].x1 = 124.0;
let out = [chord(100.0, &[60.0], false), wide];
let s = said(
&out,
&[dot(116.0, 60.0), dot(128.0, 10.0), dot(128.0, 20.0)],
);
assert_eq!((s[0].dots, s[1].dots), (Some(1), Some(1)));
}
#[test]
fn a_dot_or_an_accidental_that_no_note_took_is_a_doubt_of_the_note_by_it() {
let mut out = vec![chord(100.0, &[20.0], true), chord(200.0, &[20.0], true)];
let dots = [dot(118.0, 20.0)];
unclaimed_dots(&mut out, &dots, &[], &[], (0.0, 40.0), SP);
assert_eq!(out[0].doubts, vec![DOT_UNCLAIMED]);
assert!(out[1].doubts.is_empty());
let mut out = vec![chord(100.0, &[20.0], true)];
unclaimed_dots(&mut out, &dots, &[], &[key(&dots[0])], (0.0, 40.0), SP);
unclaimed_dots(&mut out, &dots, &[head(104.0, 22.0)], &[], (0.0, 40.0), SP);
assert!(out[0].doubts.is_empty());
unclaimed_signs(&mut out, &[glyph(0xE262, 80.0, 25.0)], SP);
assert_eq!(out[0].doubts, vec![SIGN_UNCLAIMED]);
assert_eq!(kept(&out, &[]), Some(SIGN_UNCLAIMED));
assert!(sure(&out, &[], Ok(vec![out.clone()])).is_err());
assert!(sure(&out, &[], Ok(vec![vec![Ev::spacer(Q::int(4), 0.0)]])).is_ok());
}
#[test]
fn blank_time_by_a_tuplet_s_number_no_notes_took_is_a_doubt() {
let mut a = chord(100.0, &[20.0], true);
a.loose.push((3, 2, 100.0, true));
let evs = vec![vec![a.clone()]];
let padded = vec![Ok(vec![vec![a.clone(), Ev::spacer(Q::int(3), 150.0)]])];
assert!(of_bar(&evs, &padded, SP)[0].contains(&PAD_NUMBER));
let full = vec![Ok(vec![vec![a.clone()]])];
assert!(!of_bar(&evs, &full, SP)[0].contains(&PAD_NUMBER));
let mut small = chord(120.0, &[20.0], true);
small.heads[0].size = 26.0;
small.grace = Some(false);
small.stem = Some(crate::page::VLine {
x: 132.0,
y0: -10.0,
y1: 20.0,
w: 1.0,
});
let b = chord(160.0, &[20.0], true);
let blank = || Ev::spacer(Q::ONE, 140.0);
let evs = vec![vec![a.clone(), small.clone(), b.clone()]];
let read = vec![Ok(vec![vec![
a.clone(),
small.clone(),
blank(),
b.clone(),
blank(),
]])];
assert!(of_bar(&evs, &read, SP)[0].contains(&PAD_SMALL));
let once = vec![Ok(vec![vec![a.clone(), small.clone(), b.clone(), blank()]])];
assert!(!of_bar(&evs, &once, SP)[0].contains(&PAD_SMALL));
let mut rest = Ev::new(Vec::new(), None, None);
rest.rest = Rest::Printed(glyph(0xE4E6, 122.0, 10.0));
let evs = vec![vec![a.clone(), rest.clone(), small.clone(), b.clone()]];
let read = vec![Ok(vec![vec![a.clone(), rest, small.clone(), b.clone()]])];
assert!(of_bar(&evs, &read, SP)[0].contains(&SMALL_BY_REST));
assert!(KEPT.contains(&SMALL_BY_REST));
let mut timed = small.clone();
timed.grace = None;
let read = vec![Ok(vec![vec![a.clone(), timed, b.clone()]])];
assert!(!of_bar(&evs, &read, SP)[0].contains(&SMALL_BY_REST));
let evs = vec![vec![a.clone(), small.clone(), b.clone()]];
let read = vec![Ok(vec![vec![a.clone(), small.clone(), b.clone()]])];
assert!(!of_bar(&evs, &read, SP)[0].contains(&SMALL_BY_REST));
let mut last = small.clone();
last.grace = None;
last.heads[0].x0 += 80.0;
last.heads[0].x1 += 80.0;
let evs = vec![vec![a.clone(), b.clone(), last.clone()]];
let read = vec![Ok(vec![
vec![a.clone(), b.clone()],
vec![blank(), last.clone()],
])];
assert!(of_bar(&evs, &read, SP)[0].contains(&SMALL_PADDED));
assert!(KEPT.contains(&SMALL_PADDED));
let mut under = last.clone();
under.heads[0].x0 = b.heads[0].x0;
under.heads[0].x1 = b.heads[0].x1;
let evs_under = vec![vec![a.clone(), b.clone(), under.clone()]];
let read = vec![Ok(vec![vec![a.clone(), b.clone()], vec![blank(), under]])];
assert!(!of_bar(&evs_under, &read, SP)[0].contains(&SMALL_PADDED));
let read = vec![Ok(vec![vec![a, b], vec![last]])];
assert!(!of_bar(&evs, &read, SP)[0].contains(&SMALL_PADDED));
assert_eq!(Ev::spacer(Q::ONE, 0.0).rest, Rest::Blank);
}
#[test]
fn a_tie_s_end_is_by_a_head_with_no_accidental_and_takes_the_bar_before_s_alteration() {
let mut e = chord(100.0, &[20.0, 40.0], true);
assert_eq!(ends_by(&e, 42.0, SP), Some(1));
assert_eq!(ends_by(&e, 70.0, SP), None);
e.accs.insert(e.place_of[1], (1, false));
assert_eq!(ends_by(&e, 42.0, SP), None);
let printed = [(Q::ZERO, 30, 1), (Q::ONE, 30, 0), (Q::ONE, 32, -1)];
let tied = [(28, 1), (32, 1)].into_iter().collect();
let left = in_force(&printed, &tied);
assert_eq!((left[&30], left[&32], left[&28]), (0, -1, 1));
let e = chord(100.0, &[20.0], true);
let none = |_: i32| false;
assert_eq!(
of_curve(Some(&none), None, &e, 21.0, SP),
Some(TIE_IN_UNCARRIED)
);
let earlier = chord(40.0, &[20.0], true);
assert_eq!(
of_curve(None, Some(&earlier), &e, 21.0, SP),
Some(TIE_FAILED)
);
let mut bar = Ok(vec![vec![e]]);
tie_dropped(&mut bar);
assert_eq!(bar.unwrap()[0][0].doubts, vec![TIE_DROPPED]);
}
#[test]
fn a_tremolo_between_notes_the_voices_set_apart_leaves_the_bar_unread() {
use crate::staff::Trem;
let mut a = chord(100.0, &[20.0], true);
a.tremolo = Some(Trem::Start(3));
let mut b = chord(160.0, &[30.0], true);
b.tremolo = Some(Trem::Stop);
let mut rest = chord(160.0, &[30.0], true);
rest.rest = Rest::Blank;
assert!(tremolos_paired(&[vec![a.clone(), b.clone()]]));
assert!(!tremolos_paired(&[vec![a.clone()], vec![b.clone()]]));
assert!(!tremolos_paired(&[vec![
a.clone(),
rest.clone(),
b.clone()
]]));
let said = sure(&[], &[], Ok(vec![vec![a, rest]]));
assert_eq!(said.err(), Some(TREMOLO_APART.to_string()));
}
}