75 lines
2.3 KiB
Rust
75 lines
2.3 KiB
Rust
//! Owned leader glue. The pattern is kept once even when it paints many times.
|
|
use super::*;
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
|
pub enum LeaderMode {
|
|
Aligned,
|
|
Centered,
|
|
Expanded,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
|
pub enum LeaderPattern {
|
|
Box(Box<NativeBox>),
|
|
Rule(NativeRule),
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
|
pub struct LeaderGlue {
|
|
pub glue: Glue,
|
|
pub mode: LeaderMode,
|
|
pub pattern: LeaderPattern,
|
|
}
|
|
|
|
impl LeaderGlue {
|
|
pub(super) fn pattern_dimensions(&self) -> (i64, i64, i64) {
|
|
match &self.pattern {
|
|
LeaderPattern::Box(b) => (b.width, b.height - b.shift, b.depth + b.shift),
|
|
LeaderPattern::Rule(r) => r.dimensions(),
|
|
}
|
|
}
|
|
|
|
pub(super) fn positions(
|
|
&self,
|
|
origin: i64,
|
|
start: i64,
|
|
available: i64,
|
|
tile: i64,
|
|
) -> impl Iterator<Item = i64> {
|
|
let (first, stride, count) = if tile <= 0 || available <= 0 {
|
|
(start, 0, 0)
|
|
} else {
|
|
// TeX includes 10 sp in its fit check to absorb glue rounding.
|
|
// The caller still advances by the original adjusted glue length.
|
|
let length = available + 10;
|
|
let copies = length / tile;
|
|
let remaining = length % tile;
|
|
let (offset, gap) = match self.mode {
|
|
LeaderMode::Aligned => ((origin - start).rem_euclid(tile), 0),
|
|
LeaderMode::Centered => (remaining / 2, 0),
|
|
LeaderMode::Expanded => {
|
|
let gap = remaining / (copies + 1);
|
|
((remaining - (copies - 1) * gap) / 2, gap)
|
|
}
|
|
};
|
|
let stride = tile + gap;
|
|
let count = if length - offset >= tile {
|
|
1 + (length - offset - tile) / stride
|
|
} else {
|
|
0
|
|
};
|
|
(start + offset, stride, count)
|
|
};
|
|
(0..count).map(move |copy| first + copy * stride)
|
|
}
|
|
}
|
|
|
|
impl LeaderPattern {
|
|
pub(in crate::execution) fn node_count(&self) -> usize {
|
|
match self {
|
|
Self::Box(b) => b.node_count(),
|
|
Self::Rule(_) => 1,
|
|
}
|
|
}
|
|
}
|