f-office-docx 0.1.0

DOCX model and Word's layout rules.
Documentation
//! Tab stops.

use f_office_units::Length;

use crate::props::{TabKind, TabStop};

/// The tab stops of one paragraph, measured from the left edge of the text column.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Tabs {
    stops: Vec<TabStop>,
    default: Length,
    hanging: Option<Length>,
    indent_after_custom: bool,
}

impl Tabs {
    /// Makes the stops of a paragraph from its custom stops, the default distance and, for a
    /// hanging indent, the left indent, which acts as one more stop.
    #[must_use]
    pub fn new(
        stops: impl IntoIterator<Item = TabStop>,
        default: Length,
        hanging: Option<Length>,
    ) -> Self {
        Self {
            stops: stops.into_iter().collect(),
            default,
            hanging,
            indent_after_custom: false,
        }
    }

    /// Whether `stop` is one of the paragraph's custom stops, and not its hanging indent or a
    /// default stop.
    #[must_use]
    pub fn is_custom(&self, stop: TabStop) -> bool {
        self.stops.contains(&stop)
    }

    /// Returns the stop that a tab starting at `from` moves to: the nearest custom stop or
    /// hanging indent after it, or else the next default stop.
    #[must_use]
    pub fn next(&self, from: Length) -> TabStop {
        self.next_stop(from).0
    }

    /// The stop that a tab at `from` moves to, as [`Tabs::next`] gives it, and whether that
    /// stop is a default stop.
    #[must_use]
    pub fn next_stop(&self, from: Length) -> (TabStop, bool) {
        let custom = self.stops.iter().find(|s| s.position > from).copied();
        let hanging = self.hanging.filter(|h| *h > from).map(|position| TabStop {
            position,
            kind: TabKind::Left,
            leader: None,
        });
        let default = self.default_after(from);
        let nearest = match (custom, hanging) {
            (Some(c), Some(_)) if self.indent_after_custom => Some(c),
            (None, Some(h)) if self.indent_after_custom && default.position < h.position => None,
            (Some(c), Some(h)) => Some(if h.position < c.position { h } else { c }),
            (c, h) => c.or(h),
        };
        nearest.map_or((default, true), |stop| (stop, false))
    }

    /// Puts the hanging indent after the custom stops: a tab then moves to the next custom
    /// stop when there is one, also one beyond the indent, and else to the nearer of the
    /// indent and the next default stop. This is the tab after a list label in a document
    /// with `w:doNotUseIndentAsNumberingTabStop`.
    #[must_use]
    pub const fn with_indent_after_custom_stops(mut self) -> Self {
        self.indent_after_custom = true;
        self
    }

    fn default_after(&self, from: Length) -> TabStop {
        // The count of whole default steps up to `from`, rounded down; the stop is the next one.
        let step = self.default.max(Length::from_units(1));
        let steps = from.units().div_euclid(step.units());
        TabStop {
            position: step * (steps + 1),
            kind: TabKind::Left,
            leader: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Tabs;
    use crate::props::{TabKind, TabStop};
    use f_office_units::Length;

    fn stop(position: i64, kind: TabKind) -> TabStop {
        TabStop {
            position: Length::from_units(position),
            kind,
            leader: None,
        }
    }

    #[test]
    fn custom_stops_come_before_defaults() {
        let tabs = Tabs::new([stop(250, TabKind::Right)], Length::from_units(100), None);
        assert_eq!(tabs.next(Length::from_units(10)).position.units(), 250);
        assert_eq!(tabs.next(Length::from_units(10)).kind, TabKind::Right);
        assert_eq!(tabs.next(Length::from_units(250)).position.units(), 300);
        assert_eq!(tabs.next(Length::from_units(-5)).position.units(), 250);
    }

    #[test]
    fn a_hanging_indent_is_a_stop() {
        let tabs = Tabs::new(
            [stop(500, TabKind::Left)],
            Length::from_units(100),
            Some(Length::from_units(300)),
        );
        assert_eq!(tabs.next(Length::from_units(120)).position.units(), 300);
        assert_eq!(tabs.next(Length::from_units(320)).position.units(), 500);
        let early = Tabs::new(
            [stop(200, TabKind::Left)],
            Length::from_units(100),
            Some(Length::from_units(300)),
        );
        assert_eq!(early.next(Length::from_units(120)).position.units(), 200);
        let none = Tabs::new([], Length::from_units(100), Some(Length::from_units(300)));
        assert_eq!(none.next(Length::from_units(350)).position.units(), 400);
    }

    #[test]
    fn a_hanging_indent_after_the_custom_stops_competes_with_the_default_stops() {
        let tabs = |stops: &[TabStop], default: i64| {
            Tabs::new(
                stops.iter().copied(),
                Length::from_units(default),
                Some(Length::from_units(300)),
            )
            .with_indent_after_custom_stops()
        };
        let next = |tabs: &Tabs, from: i64| tabs.next_stop(Length::from_units(from));
        // A custom stop beyond the indent wins over the indent.
        let custom = tabs(&[stop(500, TabKind::Left)], 100);
        assert_eq!(next(&custom, 220).0.position.units(), 500);
        // Without one, the nearer of the indent and the next default stop wins.
        let (stop_at, is_default) = next(&tabs(&[], 250), 220);
        assert_eq!((stop_at.position.units(), is_default), (250, true));
        let (stop_at, is_default) = next(&tabs(&[], 400), 220);
        assert_eq!((stop_at.position.units(), is_default), (300, false));
    }
}