kakemono 0.24.2

Convert PDF, Word, Excel, PowerPoint and EPUB documents and web pages to HTML, Markdown, EPUB, LaTeX and JSON in Rust
Documentation
//! Items sorted by a coordinate (a baseline, mostly), to find those within
//! a band of it without looking at every item of the page: a rule that
//! compares each line, or each glyph, with its neighbours stays near
//! linear on a page of tens of thousands of them.

use std::cmp::Ordering;

/// How much wider than asked a band is, as a share of its width and in
/// points: a rounding margin, so that an item right at its edge is kept
/// whichever way its distance rounds (the caller still tests each item).
const SLACK_RATIO: f32 = 1e-3;
/// See [`SLACK_RATIO`].
const SLACK_PT: f32 = 1e-2;

/// The indexes of some items sorted by a coordinate (those with the same
/// coordinate in their own order), with that coordinate.
pub(crate) struct Band {
    /// The indexes, by coordinate.
    order: Vec<usize>,
    /// The coordinate of each, in that order.
    keys: Vec<f32>,
}

impl Band {
    /// The items whose coordinates are `keys`, in their order.
    pub(crate) fn new(keys: impl IntoIterator<Item = f32>) -> Self {
        let keys: Vec<f32> = keys.into_iter().collect();
        let mut order: Vec<usize> = (0..keys.len()).collect();
        order.sort_by(|&a, &b| keys[a].total_cmp(&keys[b]));
        let keys = order.iter().map(|&index| keys[index]).collect();
        Self { order, keys }
    }

    /// The indexes of the items whose coordinate may lie from `low` to
    /// `high`, sorted by coordinate: a little more on each side, so the
    /// caller tests each; all of them when the band is not a number.
    pub(crate) fn within(&self, low: f32, high: f32) -> &[usize] {
        let margin = (high - low).abs() * SLACK_RATIO + SLACK_PT;
        let (low, high) = (low - margin, high + margin);
        if !matches!(low.partial_cmp(&high), Some(Ordering::Less | Ordering::Equal)) {
            return &self.order;
        }
        let start = self.keys.partition_point(|key| key.total_cmp(&low).is_lt());
        let end = self.keys.partition_point(|key| key.total_cmp(&high).is_le());
        &self.order[start..end.max(start)]
    }

    /// The indexes of the items whose coordinate may lie within `reach` of
    /// `at`, as [`Band::within`] gives them.
    pub(crate) fn around(&self, at: f32, reach: f32) -> &[usize] {
        self.within(at - reach, at + reach)
    }
}

#[cfg(test)]
mod tests {
    use super::Band;

    #[test]
    fn a_band_holds_the_items_within_it_by_coordinate_then_order() {
        let band = Band::new([5.0, 1.0, 3.0, 1.0, 9.0]);
        assert_eq!(band.within(1.0, 3.0), &[1, 3, 2]);
        assert_eq!(band.around(5.0, 0.5), &[0]);
        assert!(band.within(6.0, 8.0).is_empty());
        // A band that is not a number holds every item.
        assert_eq!(band.within(f32::NAN, 1.0).len(), 5);
        // An edge that rounds either way is kept.
        assert_eq!(band.around(0.1 + 0.2, 4.7), &[1, 3, 2, 0]);
    }
}