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

+36 -1
View File
@@ -504,6 +504,8 @@ pub fn edit_json(source: &str, unit: usize) -> Result<String, String> {
#[derive(Clone, PartialEq, Eq)]
struct SourceParagraph {
text: String,
/// TeX `\newpage` before this paragraph.
page_break: bool,
centered: bool,
right_aligned: bool,
font: ParagraphFont,
@@ -562,7 +564,12 @@ fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
}
let mut result = Vec::new();
let mut pending = (0_i64, 0_i64, 0_i64);
let mut pending_break = false;
for chunk in paragraphs {
if chunk == "\\newpage" || chunk == "\\clearpage" {
pending_break = true;
continue;
}
if let Some(skip) = vertical_skip(chunk) {
pending.0 += skip.0;
pending.1 += skip.1;
@@ -574,6 +581,8 @@ fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
first.space_before += pending.0;
first.space_stretch += pending.1;
first.space_shrink += pending.2;
first.page_break = pending_break;
pending_break = false;
pending = (0, 0, 0);
}
result.extend(blocks);
@@ -620,6 +629,7 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
space_before: 0,
space_stretch: 0,
space_shrink: 0,
page_break: false,
}
})
.collect();
@@ -644,6 +654,7 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
space_before: 0,
space_stretch: 0,
space_shrink: 0,
page_break: false,
}];
}
}
@@ -677,6 +688,7 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
space_before: 0,
space_stretch: 0,
space_shrink: 0,
page_break: false,
}
})
.collect();
@@ -710,6 +722,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
space_before: 0,
space_stretch: 0,
space_shrink: 0,
page_break: false,
};
}
let candidates = [
@@ -730,6 +743,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
space_before: 0,
space_stretch: 0,
space_shrink: 0,
page_break: false,
};
}
}
@@ -744,6 +758,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
space_before: 0,
space_stretch: 0,
space_shrink: 0,
page_break: false,
}
}
@@ -823,7 +838,14 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
unit
})
.collect();
Document::new(700 * SCALE, units)
let mut document = Document::new(700 * SCALE, units);
document.page_breaks = paragraphs
.iter()
.enumerate()
.filter(|(_, paragraph)| paragraph.page_break)
.map(|(index, _)| index)
.collect();
document
}
fn render_json(document: &Document, engine: &Engine, edit: Option<(&[usize], usize)>) -> String {
@@ -1158,4 +1180,17 @@ mod source_tests {
assert_eq!(document.units[3].space_stretch, 2 * SCALE);
assert_eq!(document.units[3].space_shrink, SCALE);
}
#[test]
fn newpage_starts_a_new_page() {
let source = "Alpha beta gamma delta.\n\n\\newpage\n\nEpsilon zeta eta theta.";
let parsed = paragraphs(source).expect("paragraphs");
assert_eq!(parsed.len(), 2);
assert!(!parsed[0].page_break);
assert!(parsed[1].page_break);
let document = build_document(&parsed, &PageGeometry::default());
assert_eq!(document.page_breaks, vec![1]);
let engine = Engine::layout(&document);
assert_eq!(engine.pages().len(), 2);
assert!(engine.pages()[0].items.len() >= 1);
}
}