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(),
+39 -2
View File
@@ -291,16 +291,26 @@ fn route_pages_text_with(
top_skip_pt: i64,
) -> Vec<String> {
let mut units = Vec::new();
for (index, text) in paragraphs.iter().enumerate() {
let mut page_breaks = Vec::new();
for text in paragraphs {
if text.trim() == "\\newpage" || text.trim() == "\\clearpage" {
page_breaks.push(units.len());
continue;
}
let style = ParagraphStyle {
line_width: 345 * PT,
par_indent: 15 * PT,
line_height: 12 * PT,
..ParagraphStyle::default()
};
units.push(Unit::paragraph_with_style(index as u64 + 1, text, style));
units.push(Unit::paragraph_with_style(
units.len() as u64 + 1,
text,
style,
));
}
let mut document = Document::new(page_goal_pt * PT, units);
document.page_breaks = page_breaks;
document.top_skip = top_skip_pt * PT;
document.par_skip = par_skip * PT;
document.text_float_sep = 0;
@@ -659,6 +669,33 @@ fn explicit_sfcode_matches_pdftex() {
});
}
/// `\newpage` forces the next paragraph onto a fresh page; page texts and
/// page count must match pdfLaTeX.
#[test]
fn newpage_matches_pdftex_page_breaks() {
let paragraphs = vec![
"alpha beta gamma delta".to_string(),
"\\newpage".to_string(),
"epsilon zeta eta theta".to_string(),
];
let body = "alpha beta gamma delta\n\n\\newpage\n\nepsilon zeta eta theta\\par\n";
let source = format!(
"\\documentclass[10pt]{{article}}\n\
\\pdfpagewidth=612bp \\pdfpageheight=792bp\n\
\\pagestyle{{empty}}\n\
\\begin{{document}}\n\
\\hsize=345pt \\parindent=15pt \\parskip=0pt \\topskip=12pt \\vsize=550pt\n\
{body}\\end{{document}}\n"
);
let Some(reference) = pdftex_pages(&source) else {
eprintln!("skipping: pdflatex/pdftotext are not installed");
return;
};
let ours = route_pages_text(&paragraphs, 0);
assert_eq!(ours.len(), reference.len(), "newpage page count");
assert_eq!(ours, reference, "newpage page text");
}
#[test]
fn pages_break_like_pdftex_for_single_line_paragraphs() {
let paragraphs: Vec<String> = (0..100)