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
//! Whose stem a line beside a notehead is, where the notes are set so close that a head touches
//! the stem of the note before or after it.
//!
//! A stem stands at its heads' right and runs up from the lowest of them, or at their left and
//! runs down from the highest: the end its heads are at, and the side they are on, say which. A
//! head on the stem's other side is its chord's only as a second set across the stem (or a
//! unison beside it), next to a head on the stem's own side; any other head there is the next
//! note's, set close.

use crate::page::{Glyph, VLine};

/// How near a head's edge its stem stands, in staff spaces.
const AT: f64 = 0.25;
/// How near a stem's end its outermost head's middle is, in staff spaces.
const END: f64 = 0.6;
/// Heads this near in height are a second or a unison, in staff spaces.
const SECOND: f64 = 0.75;

/// Whether the stem `s`, which stands at an edge of the head `h`, is another note's and none of
/// `h`'s: it runs up from a head at its foot on its left and `h` stands to its right, or it
/// hangs down from a head at its top on its right and `h` stands to its left, with no head of
/// the stem's own side within a second of `h` (`heads`: the bar's, `h` among them); and `h` has
/// a stem of its own among `stems`, up from its right or down from its left.
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) {
        // an up-stem: a head at its foot, on its left
        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) {
        // a down-stem: a head at its top, on its right
        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() {
        // b1 stemmed up to a beam, and b2 an octave over it set so close that its left edge
        // touches b1's stem: the stem runs past it above and below
        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));
        // (and its own stem, at its right, is its own)
        assert!(!of_another(&high, &highs, &heads, &stems, 1.0));
        // (a head with no stem of its own is left to the one it touches)
        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() {
        // stems down: the next note's stem hangs from its head, higher, and touches this
        // head's right edge on its way down
        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() {
        // c and d on one up-stem, the d set to the stem's right
        let (c, d) = (head(0.0, 10.0), head(1.2, 9.5));
        let s = stem(1.2, 5.0, 10.0);
        // (each with a stem it might be thought to have of its own)
        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));
        // a chord with a second high up it: the stem's foot is at the low head, far below
        let low = head(0.0, 13.0);
        assert!(!of_another(
            &d,
            &stem(1.2, 5.0, 13.0),
            &[low, c, d],
            &all,
            1.0
        ));
        // two voices in unison either side of a stem
        let u = head(1.2, 10.0);
        assert!(!of_another(&u, &s, &[c, u], &all, 1.0));
        // a down-stem's chord with its second: the lower head set to the stem's left
        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() {
        // one head, a stem up from its right and a stem down from its left; the down-stem's
        // chord has a lower head too
        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));
    }
}