Start route-D pages on newpage breaks

This commit is contained in:
gloamxun committed 2026-10-23 14:32:27 +08:00
1 parent 4111080422
commit 247cbf2b9e
5 files changed
+107 -12

No files matched your search

+26 -4
View File
@@ -601,6 +601,9 @@ fn default_insertion_multiplier() -> u32 {
pub struct Document {
pub units: Vec<Unit>,
pub page_goal: i64,
/// Unit indices that start on a fresh page (TeX `\newpage` forced breaks).
#[serde(default)]
pub page_breaks: Vec<usize>,
/// TeX `\paperwidth`: width of the output page.
#[serde(default = "default_paper_width")]
pub paper_width: i64,
@@ -744,6 +747,7 @@ impl Document {
Self {
units,
page_goal,
page_breaks: Vec::new(),
paper_width: default_paper_width(),
paper_height: default_paper_height(),
top_skip: 0,
@@ -865,6 +869,11 @@ fn document_nodes(
let mut items = Vec::new();
let mut previous_paragraph = false;
for (index, unit) in document.units.iter().enumerate() {
if document.page_breaks.binary_search(&index).is_ok() {
nodes.push(VerticalNode::Penalty(-10_000));
owners.push(index);
items.push(VerticalItem::Penalty(-10_000));
}
if unit.space_before > 0 {
nodes.push(VerticalNode::Glue(VerticalGlue::new(
unit.space_before,
@@ -1373,6 +1382,13 @@ impl Engine {
used = document.top_skip;
insertion_height = 0;
}
if document.page_breaks.binary_search(&unit_index).is_ok() && !current.items.is_empty()
{
output.shipout(&mut current, pages.len() + 1);
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
checkpoints.push(Checkpoint {
unit_index,
page_index: pages.len(),
@@ -2417,10 +2433,11 @@ impl Engine {
.sum();
// A paragraph is rebuilt line by line, so it may fill the remaining
// space of the checkpoint page instead of being pushed to a new one.
let splits = matches!(
next.units[start_index].items.first(),
Some(VerticalItem::Paragraph(_))
);
let splits = next.page_breaks.binary_search(&start_index).is_ok()
|| matches!(
next.units[start_index].items.first(),
Some(VerticalItem::Paragraph(_))
);
if !splits
&& checkpoint.used + edited_height > next.page_goal
&& !checkpoint.page_items.is_empty()
@@ -2444,6 +2461,11 @@ impl Engine {
let mut checkpoints = self.layout.checkpoints[..start_index].to_vec();
for (index, unit) in next.units.iter().enumerate().skip(start_index) {
if next.page_breaks.binary_search(&index).is_ok() && !current.items.is_empty() {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
}
checkpoints.push(Checkpoint {
unit_index: index,
page_index: pages.len(),