Advance native LaTeX execution and preview

This commit is contained in:
gloamxun committed 2026-11-18 22:17:21 +08:00
1 parent 91f9d76b16
commit 2741546ca8
414 files changed
+99872 -933

No files matched your search

+122 -34
View File
@@ -353,6 +353,7 @@ impl PageBuilder {
float_total += 1;
}
let mut stopped_at_break = false;
for index in start..nodes.len() {
let (stretch, stretch_order) = effective_stretch(&stretch_orders);
match &nodes[index] {
@@ -404,11 +405,9 @@ impl PageBuilder {
} else {
*box_height
};
// TeX keeps at most \maxdepth of a box descent below the
// page; deeper material is added to the page height as the
// box is contributed. A following box replaces the current
// page depth, so earlier kept descents are discarded.
height += contribution + (*box_depth - self.max_depth).max(0);
// Consume the previous box's pending depth exactly once.
// The current box leaves at most \maxdepth below the page.
height += depth + contribution + (*box_depth - self.max_depth).max(0);
depth = (*box_depth).min(self.max_depth);
first_box = false;
has_body = true;
@@ -536,8 +535,11 @@ impl PageBuilder {
// non-discardable item: glue after glue is not a legal
// breakpoint, and the glue at a break is discarded rather
// than counted towards the page height.
let legal_break =
index > start && !matches!(nodes[index - 1], VerticalNode::Glue(_));
let legal_break = index > start
&& !matches!(
nodes[index - 1],
VerticalNode::Glue(_) | VerticalNode::Penalty(_)
);
if legal_break {
candidates.push(self.candidate(
height,
@@ -550,8 +552,13 @@ impl PageBuilder {
0,
index,
));
if candidates.last().unwrap().cost == 1_073_741_823 {
stopped_at_break = true;
break;
}
}
height += glue.width;
height += depth + glue.width;
depth = 0;
let order = usize::from(glue.order.min(3));
stretch_orders[order] += glue.stretch.max(0);
if order == 0 {
@@ -560,32 +567,21 @@ impl PageBuilder {
}
VerticalNode::Penalty(penalty) => {
if *penalty <= -10_000 {
let end = index.max(start);
let badness =
self.badness(height, reserved, stretch, shrink, stretch_order);
let fragment = self.fragment(
start,
end,
candidates.push(self.candidate(
height,
depth,
stretch,
shrink,
stretch_order,
badness,
badness + *penalty,
*penalty,
reserved,
bottom,
order_stretches(&nodes[start..end]),
self.insertion_space(nodes, start, end, held),
bottom_floats.clone(),
);
return Some((
fragment,
index + 1,
self.insertion_overflow(nodes, start, end, held),
bottom_floats.clone(),
deferred_floats.clone(),
*penalty,
index,
));
// fire_up uses the best candidate seen so far. An
// overfull forced point must not discard an earlier fit.
stopped_at_break = true;
break;
}
// TeX treats a penalty of 10000 or more as no break at
// all; only the forced (<= -10000) case above is stronger.
@@ -601,6 +597,10 @@ impl PageBuilder {
*penalty,
index,
));
if candidates.last().unwrap().cost == 1_073_741_823 {
stopped_at_break = true;
break;
}
}
}
VerticalNode::Insertion {
@@ -624,7 +624,8 @@ impl PageBuilder {
}
let (stretch, stretch_order) = effective_stretch(&stretch_orders);
candidates.push(self.candidate(
let final_page_fits = !stopped_at_break && height + reserved + bottom <= self.page_goal;
let mut final_break = self.candidate(
height,
depth,
stretch,
@@ -634,9 +635,19 @@ impl PageBuilder {
bottom,
0,
nodes.len(),
));
);
// TeX ends the job with infinite fill and a forced break. Without
// that terminator a negative section penalty can split even a short
// final page, and flushbottom stretches its body instead of the fill.
if final_page_fits {
final_break.cost = -10_000;
final_break.badness = 0;
}
if !stopped_at_break {
candidates.push(final_break);
}
let best = self.select(candidates)?;
let fragment = self.fragment(
let mut fragment = self.fragment(
start,
best.end,
best.height,
@@ -652,6 +663,9 @@ impl PageBuilder {
self.insertion_space(nodes, start, best.end, held),
bottom_floats.clone(),
);
if final_page_fits && best.end == nodes.len() {
fragment.set = 0;
}
Some((
fragment,
best.next,
@@ -725,7 +739,17 @@ impl PageBuilder {
};
let badness = self.badness(height, reserved + bottom, stretch, shrink, stretch_order);
Candidate {
cost: badness + penalty,
// TeX build_page: saturated underfull badness has the fixed
// deplorable cost; a finite penalty cannot distinguish such pages.
cost: if height + reserved + bottom - self.page_goal > shrink {
1_073_741_823 // TeX awful_bad, which triggers fire_up.
} else if penalty <= -10_000 {
penalty
} else if badness >= 10_000 {
100_000
} else {
badness + penalty
},
penalty,
badness,
height,
@@ -792,7 +816,13 @@ impl PageBuilder {
insertions: Vec<InsertionSpace>,
bottom_floats: Vec<i64>,
) -> PageFragment {
let reserved: i64 = insertions.iter().map(|space| space.height).sum();
// The candidate already reserved each insertion class's \skip as
// well as its material. Use the same reservation for glue setting;
// otherwise footnote pages are stretched into their separator space.
let reserved: i64 = insertions
.iter()
.map(|space| space.height + self.insertion_skip(space.class))
.sum();
let target = (self.page_goal - bottom - reserved).max(0);
let set = if self.ragged_bottom {
0
@@ -978,7 +1008,12 @@ mod tests {
#[test]
fn flush_bottom_stretches_glue_to_the_goal() {
let nodes = vec![box_(40, 0), glue(0, 10, 0), box_(40, 0)];
let nodes = vec![
box_(40, 0),
glue(0, 10, 0),
box_(40, 0),
VerticalNode::Penalty(-10_000),
];
let mut builder = PageBuilder::new(120);
assert_eq!(
builder.build(&nodes)[0].set,
@@ -991,6 +1026,57 @@ mod tests {
assert_eq!(pages[0].set, 40);
}
#[test]
fn insertion_separator_is_reserved_when_setting_page_glue() {
let mut builder = PageBuilder::new(120);
builder.ragged_bottom = false;
builder.insertion_skips = vec![(0, 10)];
let pages = builder.build(&[
box_(40, 0),
glue(0, 10, 0),
VerticalNode::Insertion {
class: 0,
height: 10,
},
box_(40, 0),
VerticalNode::Penalty(-10_000),
]);
assert_eq!(pages.len(), 1);
assert_eq!(
pages[0].set, 20,
"120 - 80 body - 10 insertion - 10 separator"
);
}
#[test]
fn short_final_page_uses_terminal_fill_instead_of_an_earlier_section_break() {
let mut builder = PageBuilder::new(120);
builder.ragged_bottom = false;
let pages = builder.build(&[
box_(20, 0),
VerticalNode::Penalty(-300),
glue(0, 1, 0),
box_(20, 0),
]);
assert_eq!(pages.len(), 1);
assert_eq!(
pages[0].set, 0,
"terminal fill must not stretch ordinary body glue"
);
}
#[test]
fn glue_cannot_bypass_a_no_break_penalty() {
let pages = PageBuilder::new(100).build(&[
box_(70, 0),
VerticalNode::Penalty(10_000),
glue(0, 0, 0),
box_(70, 0),
]);
assert_eq!(pages.len(), 1);
assert_eq!(pages[0].height, 140);
}
#[test]
fn fill_glue_makes_short_pages_free() {
let nodes = vec![
@@ -1050,7 +1136,9 @@ mod tests {
let nodes = |penalty| {
vec![
box_(40, 0),
glue(0, 10, 0),
glue(0, 20, 0),
box_(0, 0),
VerticalNode::Penalty(0),
box_(40, 0),
VerticalNode::Penalty(penalty),
box_(40, 0),