use f_office_units::Length;
use crate::props::{TabKind, TabStop};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Tabs {
stops: Vec<TabStop>,
default: Length,
hanging: Option<Length>,
indent_after_custom: bool,
truncates_negative_default: bool,
}
impl Tabs {
#[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,
truncates_negative_default: false,
}
}
#[must_use]
pub fn is_custom(&self, stop: TabStop) -> bool {
self.stops.contains(&stop)
}
#[must_use]
pub fn next(&self, from: Length) -> TabStop {
self.next_stop(from).0
}
#[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))
}
#[must_use]
pub const fn with_indent_after_custom_stops(mut self) -> Self {
self.indent_after_custom = true;
self
}
#[must_use]
pub(crate) const fn with_truncated_negative_default_tabs(mut self) -> Self {
self.truncates_negative_default = true;
self
}
pub(crate) fn shift_hanging(&mut self, delta: Length) {
if let Some(hanging) = &mut self.hanging {
*hanging += delta;
}
}
fn default_after(&self, from: Length) -> TabStop {
let step = self.default.max(Length::from_units(1));
let steps = if self.truncates_negative_default && from < Length::ZERO {
from.units() / step.units()
} else {
from.units().div_euclid(step.units()) + 1
};
TabStop {
position: step * steps,
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));
let custom = tabs(&[stop(500, TabKind::Left)], 100);
assert_eq!(next(&custom, 220).0.position.units(), 500);
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));
}
#[test]
fn mode_15_truncates_negative_default_tab_steps_toward_zero() {
let legacy = Tabs::new([], Length::from_units(720), None);
let modern = legacy.clone().with_truncated_negative_default_tabs();
for (from, want_legacy, want_modern) in [
(-1440, -720, -1440),
(-1280, -720, -720),
(-720, 0, -720),
(-667, 0, 0),
(0, 720, 720),
(720, 1440, 1440),
] {
let from = Length::from_units(from);
assert_eq!(
(
legacy.next(from).position.units(),
modern.next(from).position.units(),
),
(want_legacy, want_modern),
);
}
}
}