fm-vector 0.1.0

A notation program's PDF read from its vectors: every notehead, stem, beam and barline where the engraver put it, read into .fmn and compiled to MusicXML, with the words from the PDF's text. No model, no server: it runs in the browser.
Documentation
//! 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);
    }
}