Add a TeX page builder over vertical lists
This commit is contained in:
1 parent
49bd9b63e6
commit
d4a9c6b4fd
6 files changed
+397
-7
No files matched your search
@@ -0,0 +1,371 @@
|
||||
//! TeX-style page building over an explicit vertical list.
|
||||
//!
|
||||
//! This module is the foundation for the TeX page builder described by P2 of
|
||||
//! `docs/ROUTE_D_PLAN.md`. Instead of stopping at the first box that does not
|
||||
//! fit, it scores every candidate breakpoint with TeX's page badness and
|
||||
//! penalty cost, so a fuller page can win over a much emptier one. Floats and
|
||||
//! insertion classes are still handled by the incremental builder in
|
||||
//! `Engine`; wiring this module into `Document` is the next P2 step.
|
||||
|
||||
use crate::Mark;
|
||||
use std::cmp::Reverse;
|
||||
|
||||
/// Vertical glue with the stretch and shrink TeX uses to set a page.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct VerticalGlue {
|
||||
pub width: i64,
|
||||
pub stretch: i64,
|
||||
pub shrink: i64,
|
||||
}
|
||||
|
||||
impl VerticalGlue {
|
||||
pub const fn new(width: i64, stretch: i64, shrink: i64) -> Self {
|
||||
Self {
|
||||
width,
|
||||
stretch,
|
||||
shrink,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Material that can appear in a vertical list handed to the page builder.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum VerticalNode {
|
||||
Box { height: i64, depth: i64 },
|
||||
Glue(VerticalGlue),
|
||||
Penalty(i64),
|
||||
Mark(Mark),
|
||||
}
|
||||
|
||||
/// One page chosen by [`PageBuilder::build`].
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct PageFragment {
|
||||
/// Index of the first node that belongs to this page.
|
||||
pub start: usize,
|
||||
/// Index one past the last node that belongs to this page.
|
||||
pub end: usize,
|
||||
/// TeX's page cost for the breakpoint: badness plus penalty.
|
||||
pub cost: i64,
|
||||
/// Badness of the page height against the page goal.
|
||||
pub badness: i64,
|
||||
/// Height of the page box after glue setting.
|
||||
pub height: i64,
|
||||
/// Depth of the page box, capped at `max_depth`.
|
||||
pub depth: i64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct Candidate {
|
||||
cost: i64,
|
||||
badness: i64,
|
||||
height: i64,
|
||||
depth: i64,
|
||||
shrink: i64,
|
||||
end: usize,
|
||||
next: usize,
|
||||
}
|
||||
|
||||
/// TeX page builder parameters (`\vsize`, `\topskip`, `\maxdepth`).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct PageBuilder {
|
||||
pub page_goal: i64,
|
||||
pub top_skip: i64,
|
||||
pub max_depth: i64,
|
||||
}
|
||||
|
||||
impl PageBuilder {
|
||||
pub const fn new(page_goal: i64) -> Self {
|
||||
Self {
|
||||
page_goal,
|
||||
top_skip: 0,
|
||||
max_depth: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Split `nodes` into pages, choosing the cheapest legal break each time.
|
||||
pub fn build(&self, nodes: &[VerticalNode]) -> Vec<PageFragment> {
|
||||
let mut pages = Vec::new();
|
||||
let mut start = self.skip_leading(nodes, 0);
|
||||
while start < nodes.len() {
|
||||
let Some((fragment, next)) = self.best_fragment(nodes, start) else {
|
||||
break;
|
||||
};
|
||||
let next = next.max(start + 1);
|
||||
pages.push(fragment);
|
||||
start = self.skip_leading(nodes, next);
|
||||
}
|
||||
pages
|
||||
}
|
||||
|
||||
fn skip_leading(&self, nodes: &[VerticalNode], mut index: usize) -> usize {
|
||||
while index < nodes.len() {
|
||||
match nodes[index] {
|
||||
VerticalNode::Glue(_) | VerticalNode::Penalty(_) => index += 1,
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
index
|
||||
}
|
||||
|
||||
fn best_fragment(&self, nodes: &[VerticalNode], start: usize) -> Option<(PageFragment, usize)> {
|
||||
let mut height = 0_i64;
|
||||
let mut stretch = 0_i64;
|
||||
let mut shrink = 0_i64;
|
||||
let mut depth = 0_i64;
|
||||
let mut first_box = true;
|
||||
let mut candidates: Vec<Candidate> = Vec::new();
|
||||
|
||||
for index in start..nodes.len() {
|
||||
match &nodes[index] {
|
||||
VerticalNode::Box {
|
||||
height: box_height,
|
||||
depth: box_depth,
|
||||
} => {
|
||||
let contribution = if first_box {
|
||||
(*box_height).max(self.top_skip)
|
||||
} else {
|
||||
*box_height
|
||||
};
|
||||
height += contribution;
|
||||
depth = *box_depth;
|
||||
first_box = false;
|
||||
}
|
||||
VerticalNode::Glue(glue) => {
|
||||
if index > start {
|
||||
candidates.push(self.candidate(height, depth, stretch, shrink, 0, index));
|
||||
}
|
||||
height += glue.width;
|
||||
stretch += glue.stretch.max(0);
|
||||
shrink += glue.shrink.max(0);
|
||||
}
|
||||
VerticalNode::Penalty(penalty) => {
|
||||
if *penalty <= -10_000 {
|
||||
let end = index.max(start);
|
||||
let badness = self.badness(height, stretch, shrink);
|
||||
let fragment =
|
||||
self.fragment(start, end, height, depth, badness, badness + penalty);
|
||||
return Some((fragment, index + 1));
|
||||
}
|
||||
if index > start {
|
||||
candidates
|
||||
.push(self.candidate(height, depth, stretch, shrink, *penalty, index));
|
||||
}
|
||||
}
|
||||
VerticalNode::Mark(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
candidates.push(self.candidate(height, depth, stretch, shrink, 0, nodes.len()));
|
||||
let best = self.select(candidates)?;
|
||||
let fragment = self.fragment(
|
||||
start,
|
||||
best.end,
|
||||
best.height,
|
||||
best.depth,
|
||||
best.badness,
|
||||
best.cost,
|
||||
);
|
||||
Some((fragment, best.next))
|
||||
}
|
||||
|
||||
fn candidate(
|
||||
&self,
|
||||
height: i64,
|
||||
depth: i64,
|
||||
stretch: i64,
|
||||
shrink: i64,
|
||||
penalty: i64,
|
||||
end: usize,
|
||||
) -> Candidate {
|
||||
let depth = depth.max(0);
|
||||
let (height, depth) = if depth > self.max_depth {
|
||||
(height + depth - self.max_depth, self.max_depth)
|
||||
} else {
|
||||
(height, depth)
|
||||
};
|
||||
let badness = self.badness(height, stretch, shrink);
|
||||
Candidate {
|
||||
cost: badness + penalty,
|
||||
badness,
|
||||
height,
|
||||
depth,
|
||||
shrink,
|
||||
end,
|
||||
next: end + 1,
|
||||
}
|
||||
}
|
||||
|
||||
fn badness(&self, height: i64, stretch: i64, shrink: i64) -> i64 {
|
||||
if height == self.page_goal {
|
||||
return 0;
|
||||
}
|
||||
let (shortfall, slack) = if height < self.page_goal {
|
||||
(self.page_goal - height, stretch)
|
||||
} else {
|
||||
(height - self.page_goal, shrink)
|
||||
};
|
||||
if slack <= 0 {
|
||||
return 10_000;
|
||||
}
|
||||
let ratio = shortfall as f64 / slack as f64;
|
||||
let badness = 100.0 * ratio * ratio * ratio;
|
||||
if badness >= 10_000.0 {
|
||||
10_000
|
||||
} else {
|
||||
badness.round() as i64
|
||||
}
|
||||
}
|
||||
|
||||
fn fragment(
|
||||
&self,
|
||||
start: usize,
|
||||
end: usize,
|
||||
height: i64,
|
||||
depth: i64,
|
||||
badness: i64,
|
||||
cost: i64,
|
||||
) -> PageFragment {
|
||||
PageFragment {
|
||||
start,
|
||||
end,
|
||||
cost,
|
||||
badness,
|
||||
height,
|
||||
depth,
|
||||
}
|
||||
}
|
||||
|
||||
fn select(&self, candidates: Vec<Candidate>) -> Option<Candidate> {
|
||||
let feasible: Vec<Candidate> = candidates
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|candidate| {
|
||||
candidate.height <= self.page_goal
|
||||
|| candidate.height - self.page_goal <= candidate.shrink
|
||||
})
|
||||
.collect();
|
||||
let pool = if feasible.is_empty() {
|
||||
candidates
|
||||
} else {
|
||||
feasible
|
||||
};
|
||||
pool.into_iter()
|
||||
.min_by_key(|candidate| (candidate.cost, Reverse(candidate.height)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn box_(height: i64, depth: i64) -> VerticalNode {
|
||||
VerticalNode::Box { height, depth }
|
||||
}
|
||||
|
||||
fn glue(width: i64, stretch: i64, shrink: i64) -> VerticalNode {
|
||||
VerticalNode::Glue(VerticalGlue::new(width, stretch, shrink))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn breaks_before_the_page_overflows() {
|
||||
let builder = PageBuilder::new(100);
|
||||
let nodes = vec![
|
||||
box_(40, 0),
|
||||
glue(0, 10, 0),
|
||||
box_(40, 0),
|
||||
glue(0, 10, 0),
|
||||
box_(40, 0),
|
||||
];
|
||||
let pages = builder.build(&nodes);
|
||||
assert_eq!(
|
||||
pages.iter().map(|page| page.height).collect::<Vec<_>>(),
|
||||
vec![80, 40]
|
||||
);
|
||||
assert_eq!(pages[0].badness, 800);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glue_stretch_can_fill_the_page_exactly() {
|
||||
let builder = PageBuilder::new(100);
|
||||
let nodes = vec![box_(50, 0), glue(0, 10, 0), box_(50, 0)];
|
||||
let pages = builder.build(&nodes);
|
||||
assert_eq!(pages.len(), 1);
|
||||
assert_eq!(pages[0].height, 100);
|
||||
assert_eq!(pages[0].badness, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forced_penalty_breaks_the_page() {
|
||||
let builder = PageBuilder::new(100);
|
||||
let nodes = vec![box_(30, 0), VerticalNode::Penalty(-10_000), box_(30, 0)];
|
||||
let pages = builder.build(&nodes);
|
||||
assert_eq!(
|
||||
pages.iter().map(|page| page.height).collect::<Vec<_>>(),
|
||||
vec![30, 30]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn penalty_can_move_the_break_to_an_earlier_page() {
|
||||
let nodes = |penalty| {
|
||||
vec![
|
||||
box_(40, 0),
|
||||
glue(0, 10, 0),
|
||||
box_(40, 0),
|
||||
VerticalNode::Penalty(penalty),
|
||||
box_(40, 0),
|
||||
]
|
||||
};
|
||||
let calm = PageBuilder::new(100).build(&nodes(0));
|
||||
assert_eq!(
|
||||
calm.iter().map(|page| page.height).collect::<Vec<_>>(),
|
||||
vec![80, 40]
|
||||
);
|
||||
let penalized = PageBuilder::new(100).build(&nodes(9_500));
|
||||
assert_eq!(
|
||||
penalized.iter().map(|page| page.height).collect::<Vec<_>>(),
|
||||
vec![40, 80]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn top_skip_sets_the_first_box_height() {
|
||||
let mut builder = PageBuilder::new(100);
|
||||
builder.top_skip = 10;
|
||||
let nodes = vec![box_(5, 0), glue(0, 0, 0), box_(5, 0)];
|
||||
let pages = builder.build(&nodes);
|
||||
assert_eq!(pages.len(), 1);
|
||||
assert_eq!(pages[0].height, 15);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn max_depth_moves_excess_depth_into_the_height() {
|
||||
let mut builder = PageBuilder::new(100);
|
||||
builder.max_depth = 4;
|
||||
let pages = builder.build(&[box_(90, 10)]);
|
||||
assert_eq!(pages[0].height, 96);
|
||||
assert_eq!(pages[0].depth, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn marks_do_not_change_page_heights() {
|
||||
let builder = PageBuilder::new(100);
|
||||
let nodes = vec![
|
||||
VerticalNode::Mark(Mark {
|
||||
class: 0,
|
||||
text: "intro".to_owned(),
|
||||
}),
|
||||
box_(30, 0),
|
||||
VerticalNode::Mark(Mark {
|
||||
class: 0,
|
||||
text: "next".to_owned(),
|
||||
}),
|
||||
glue(0, 0, 0),
|
||||
box_(30, 0),
|
||||
];
|
||||
let pages = builder.build(&nodes);
|
||||
assert_eq!(pages.len(), 1);
|
||||
assert_eq!(pages[0].height, 60);
|
||||
assert_eq!((pages[0].start, pages[0].end), (0, 5));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user