Set pages with TeX glue orders
This commit is contained in:
1 parent
00c23a3511
commit
52e3e0647c
5 files changed
+172
-25
No files matched your search
@@ -17,6 +17,10 @@ pub struct VerticalGlue {
|
||||
pub width: i64,
|
||||
pub stretch: i64,
|
||||
pub shrink: i64,
|
||||
/// TeX glue order: 0 is normal, 1 `\vfil`, 2 `\vfill`, 3 `\vfilll`.
|
||||
/// Page setting only uses glue of the highest order that has stretch.
|
||||
#[serde(default)]
|
||||
pub order: u8,
|
||||
}
|
||||
|
||||
impl VerticalGlue {
|
||||
@@ -25,6 +29,17 @@ impl VerticalGlue {
|
||||
width,
|
||||
stretch,
|
||||
shrink,
|
||||
order: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Vertical glue with an explicit TeX order.
|
||||
pub const fn with_order(width: i64, stretch: i64, shrink: i64, order: u8) -> Self {
|
||||
Self {
|
||||
width,
|
||||
stretch,
|
||||
shrink,
|
||||
order,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,8 +106,10 @@ pub struct PageFragment {
|
||||
pub bottom: i64,
|
||||
/// Depth of the page box, capped at `max_depth`.
|
||||
pub depth: i64,
|
||||
/// Total stretch of the vertical glue on the page.
|
||||
/// Total stretch of the vertical glue on the page (highest order only).
|
||||
pub stretch: i64,
|
||||
/// Highest glue order that contributes stretch: 0 normal, 1 `\vfil`, ...
|
||||
pub stretch_order: u8,
|
||||
/// Total shrink of the vertical glue on the page.
|
||||
pub shrink: i64,
|
||||
/// Signed glue-setting amount applied to the page: positive stretch to
|
||||
@@ -110,6 +127,7 @@ struct Candidate {
|
||||
height: i64,
|
||||
depth: i64,
|
||||
stretch: i64,
|
||||
stretch_order: u8,
|
||||
shrink: i64,
|
||||
reserved: i64,
|
||||
bottom: i64,
|
||||
@@ -212,7 +230,7 @@ impl PageBuilder {
|
||||
held: &BTreeMap<u8, i64>,
|
||||
) -> Option<(PageFragment, usize, BTreeMap<u8, i64>)> {
|
||||
let mut height = 0_i64;
|
||||
let mut stretch = 0_i64;
|
||||
let mut stretch_orders = [0_i64; 4];
|
||||
let mut shrink = 0_i64;
|
||||
let mut depth = 0_i64;
|
||||
let mut first_box = true;
|
||||
@@ -224,6 +242,7 @@ impl PageBuilder {
|
||||
let mut candidates: Vec<Candidate> = Vec::new();
|
||||
|
||||
for index in start..nodes.len() {
|
||||
let (stretch, stretch_order) = effective_stretch(&stretch_orders);
|
||||
match &nodes[index] {
|
||||
VerticalNode::Box {
|
||||
height: box_height,
|
||||
@@ -250,7 +269,8 @@ impl PageBuilder {
|
||||
// TeX never places a top float below body material:
|
||||
// the float starts the next page instead.
|
||||
let end = index.max(start);
|
||||
let badness = self.badness(height, reserved, stretch, shrink);
|
||||
let badness =
|
||||
self.badness(height, reserved, stretch, shrink, stretch_order);
|
||||
let fragment = self.fragment(
|
||||
start,
|
||||
end,
|
||||
@@ -258,6 +278,7 @@ impl PageBuilder {
|
||||
depth,
|
||||
stretch,
|
||||
shrink,
|
||||
stretch_order,
|
||||
badness,
|
||||
badness,
|
||||
0,
|
||||
@@ -288,7 +309,8 @@ impl PageBuilder {
|
||||
|| bottom + needed > allowed)
|
||||
{
|
||||
let end = index.max(start);
|
||||
let badness = self.badness(height, reserved + bottom, stretch, shrink);
|
||||
let badness =
|
||||
self.badness(height, reserved + bottom, stretch, shrink, stretch_order);
|
||||
let fragment = self.fragment(
|
||||
start,
|
||||
end,
|
||||
@@ -296,6 +318,7 @@ impl PageBuilder {
|
||||
depth,
|
||||
stretch,
|
||||
shrink,
|
||||
stretch_order,
|
||||
badness,
|
||||
badness,
|
||||
0,
|
||||
@@ -313,20 +336,30 @@ impl PageBuilder {
|
||||
}
|
||||
VerticalNode::Glue(glue) => {
|
||||
if index > start {
|
||||
candidates.push(
|
||||
self.candidate(
|
||||
height, depth, stretch, shrink, reserved, bottom, 0, index,
|
||||
),
|
||||
);
|
||||
candidates.push(self.candidate(
|
||||
height,
|
||||
depth,
|
||||
stretch,
|
||||
shrink,
|
||||
stretch_order,
|
||||
reserved,
|
||||
bottom,
|
||||
0,
|
||||
index,
|
||||
));
|
||||
}
|
||||
height += glue.width;
|
||||
stretch += glue.stretch.max(0);
|
||||
shrink += glue.shrink.max(0);
|
||||
let order = usize::from(glue.order.min(3));
|
||||
stretch_orders[order] += glue.stretch.max(0);
|
||||
if order == 0 {
|
||||
shrink += glue.shrink.max(0);
|
||||
}
|
||||
}
|
||||
VerticalNode::Penalty(penalty) => {
|
||||
if *penalty <= -10_000 {
|
||||
let end = index.max(start);
|
||||
let badness = self.badness(height, reserved, stretch, shrink);
|
||||
let badness =
|
||||
self.badness(height, reserved, stretch, shrink, stretch_order);
|
||||
let fragment = self.fragment(
|
||||
start,
|
||||
end,
|
||||
@@ -334,6 +367,7 @@ impl PageBuilder {
|
||||
depth,
|
||||
stretch,
|
||||
shrink,
|
||||
stretch_order,
|
||||
badness,
|
||||
badness + *penalty,
|
||||
*penalty,
|
||||
@@ -348,7 +382,15 @@ impl PageBuilder {
|
||||
}
|
||||
if index > start {
|
||||
candidates.push(self.candidate(
|
||||
height, depth, stretch, shrink, reserved, bottom, *penalty, index,
|
||||
height,
|
||||
depth,
|
||||
stretch,
|
||||
shrink,
|
||||
stretch_order,
|
||||
reserved,
|
||||
bottom,
|
||||
*penalty,
|
||||
index,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -367,11 +409,13 @@ impl PageBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
let (stretch, stretch_order) = effective_stretch(&stretch_orders);
|
||||
candidates.push(self.candidate(
|
||||
height,
|
||||
depth,
|
||||
stretch,
|
||||
shrink,
|
||||
stretch_order,
|
||||
reserved,
|
||||
bottom,
|
||||
0,
|
||||
@@ -385,6 +429,7 @@ impl PageBuilder {
|
||||
best.depth,
|
||||
best.stretch,
|
||||
best.shrink,
|
||||
best.stretch_order,
|
||||
best.badness,
|
||||
best.cost,
|
||||
best.penalty,
|
||||
@@ -448,6 +493,7 @@ impl PageBuilder {
|
||||
depth: i64,
|
||||
stretch: i64,
|
||||
shrink: i64,
|
||||
stretch_order: u8,
|
||||
reserved: i64,
|
||||
bottom: i64,
|
||||
penalty: i64,
|
||||
@@ -459,7 +505,7 @@ impl PageBuilder {
|
||||
} else {
|
||||
(height, depth)
|
||||
};
|
||||
let badness = self.badness(height, reserved + bottom, stretch, shrink);
|
||||
let badness = self.badness(height, reserved + bottom, stretch, shrink, stretch_order);
|
||||
Candidate {
|
||||
cost: badness + penalty,
|
||||
penalty,
|
||||
@@ -467,6 +513,7 @@ impl PageBuilder {
|
||||
height,
|
||||
depth,
|
||||
stretch,
|
||||
stretch_order,
|
||||
shrink,
|
||||
reserved,
|
||||
bottom,
|
||||
@@ -475,12 +522,24 @@ impl PageBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
fn badness(&self, height: i64, reserved: i64, stretch: i64, shrink: i64) -> i64 {
|
||||
fn badness(
|
||||
&self,
|
||||
height: i64,
|
||||
reserved: i64,
|
||||
stretch: i64,
|
||||
shrink: i64,
|
||||
stretch_order: u8,
|
||||
) -> i64 {
|
||||
let goal = self.page_goal - reserved;
|
||||
if height == goal {
|
||||
return 0;
|
||||
}
|
||||
let (shortfall, slack) = if height < goal {
|
||||
// `\vfil` and higher orders stretch without bound: a page that
|
||||
// stops short of the goal is free when one of them is present.
|
||||
if stretch_order > 0 {
|
||||
return 0;
|
||||
}
|
||||
(goal - height, stretch)
|
||||
} else {
|
||||
(height - goal, shrink)
|
||||
@@ -506,6 +565,7 @@ impl PageBuilder {
|
||||
depth: i64,
|
||||
stretch: i64,
|
||||
shrink: i64,
|
||||
stretch_order: u8,
|
||||
badness: i64,
|
||||
cost: i64,
|
||||
penalty: i64,
|
||||
@@ -535,6 +595,7 @@ impl PageBuilder {
|
||||
bottom,
|
||||
depth,
|
||||
stretch,
|
||||
stretch_order,
|
||||
shrink,
|
||||
set,
|
||||
insertions,
|
||||
@@ -564,6 +625,15 @@ impl PageBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
fn effective_stretch(stretch_orders: &[i64; 4]) -> (i64, u8) {
|
||||
for (order, stretch) in stretch_orders.iter().enumerate().rev() {
|
||||
if *stretch > 0 {
|
||||
return (*stretch, order as u8);
|
||||
}
|
||||
}
|
||||
(0, 0)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -614,7 +684,6 @@ mod tests {
|
||||
assert_eq!(pages[0].badness, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[test]
|
||||
fn flush_bottom_stretches_glue_to_the_goal() {
|
||||
let nodes = vec![box_(40, 0), glue(0, 10, 0), box_(40, 0)];
|
||||
@@ -630,6 +699,24 @@ mod tests {
|
||||
assert_eq!(pages[0].set, 40);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_glue_makes_short_pages_free() {
|
||||
let nodes = vec![
|
||||
box_(100, 0),
|
||||
VerticalNode::Glue(VerticalGlue::with_order(20, 1, 0, 1)),
|
||||
VerticalNode::Penalty(-10_000),
|
||||
];
|
||||
let mut builder = PageBuilder::new(200);
|
||||
builder.ragged_bottom = false;
|
||||
let pages = builder.build(&nodes);
|
||||
assert_eq!(pages.len(), 1);
|
||||
assert_eq!(pages[0].stretch_order, 1);
|
||||
assert_eq!(pages[0].stretch, 1);
|
||||
assert_eq!(pages[0].badness, 0);
|
||||
assert_eq!(pages[0].set, 80);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fragments_report_glue_stretch_and_shrink() {
|
||||
let builder = PageBuilder::new(100);
|
||||
let nodes = vec![box_(40, 0), glue(0, 20, 5), box_(40, 0)];
|
||||
|
||||
Reference in new issue
Block a user