Emit multi-page route-D PDFs

This commit is contained in:
gloamxun committed 2026-10-20 18:11:06 +08:00
1 parent b6fab85f61
commit 7dd3961e21
2 files changed
+56 -18

No files matched your search

+11
View File
@@ -1322,6 +1322,17 @@ mod tests {
assert_eq!(halign.cell_box(2, 0), Some((30, 110))); assert_eq!(halign.cell_box(2, 0), Some((30, 110)));
} }
#[test]
fn multi_page_pdf_lists_each_page() {
let document = Document::new(400, vec![Unit::paragraph(1, "a"), Unit::paragraph(2, "b")]);
let engine = Engine::layout(&document);
let list = engine.pages()[0].display_list();
let pages = vec![list.clone(), list];
let pdf = crate::pdf::display_lists_to_pdf(&pages, 400 * 65_536);
let text = String::from_utf8_lossy(&pdf);
assert!(text.contains("/Count 2"));
assert_eq!(text.matches("/Type /Page ").count(), 2);
}
#[test] #[test]
fn display_list_writes_a_minimal_pdf() { fn display_list_writes_a_minimal_pdf() {
let document = Document::new(400, vec![Unit::paragraph(1, "hello")]); let document = Document::new(400, vec![Unit::paragraph(1, "hello")]);
+45 -18
View File
@@ -1,8 +1,8 @@
//! Minimal single-page PDF writer for route-D display lists. //! Minimal PDF writer for route-D display lists.
//! //!
//! This is intentionally small: Base-14 Times-Roman for text, filled //! Base-14 Times-Roman text and filled rectangles only. This is the
//! rectangles for rules. It proves the display-list -> PDF path before the //! structural backend: multi-page output is supported; TFM/NFSS fonts,
//! real NFSS/TFM font backend and XObject support arrive. //! XObjects and `\special` are later milestones.
use crate::render::{DisplayItem, DisplayList}; use crate::render::{DisplayItem, DisplayList};
@@ -24,10 +24,7 @@ fn pt(value: i64) -> String {
format!("{:.3}", value as f64 / 65_536.0) format!("{:.3}", value as f64 / 65_536.0)
} }
/// Write one page. The media box is at least US Letter. fn page_content(list: &DisplayList, page_height: i64, height: i64) -> String {
pub fn display_list_to_pdf(list: &DisplayList, page_height: i64) -> Vec<u8> {
let width = list.width.max(612 * 65_536);
let height = page_height.max(792 * 65_536);
let mut content = String::from("q\n0 0 0 rg\n"); let mut content = String::from("q\n0 0 0 rg\n");
for item in &list.items { for item in &list.items {
match item { match item {
@@ -57,22 +54,47 @@ pub fn display_list_to_pdf(list: &DisplayList, page_height: i64) -> Vec<u8> {
} }
} }
content.push_str("Q\n"); content.push_str("Q\n");
let _ = page_height;
content
}
let objects = vec![ /// Write several display lists as a multi-page PDF.
"<< /Type /Catalog /Pages 2 0 R >>".to_string(), pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8> {
"<< /Type /Pages /Kids [4 0 R] /Count 1 >>".to_string(), let height = page_height.max(792 * 65_536);
"<< /Type /Font /Subtype /Type1 /BaseFont /Times-Roman >>".to_string(), let width = pages
format!( .iter()
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {} {}] /Resources << /Font << /F1 3 0 R >> >> /Contents 5 0 R >>", .map(|page| page.width)
.max()
.unwrap_or(612 * 65_536)
.max(612 * 65_536);
let mut objects: Vec<String> = Vec::new();
objects.push("<< /Type /Catalog /Pages 2 0 R >>".to_string());
objects.push(String::new()); // pages object filled below
objects.push("<< /Type /Font /Subtype /Type1 /BaseFont /Times-Roman >>".to_string());
let mut kids = Vec::new();
for list in pages {
let page_id = objects.len() + 1;
let content_id = page_id + 1;
kids.push(format!("{page_id} 0 R"));
objects.push(format!(
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {} {}] /Resources << /Font << /F1 3 0 R >> >> /Contents {content_id} 0 R >>",
pt(width), pt(width),
pt(height) pt(height)
), ));
format!( let content = page_content(list, page_height, height);
objects.push(format!(
"<< /Length {} >>\nstream\n{}endstream", "<< /Length {} >>\nstream\n{}endstream",
content.len(), content.len(),
content content
), ));
]; }
objects[1] = format!(
"<< /Type /Pages /Kids [{}] /Count {} >>",
kids.join(" "),
pages.len()
);
let mut pdf = String::from("%PDF-1.4\n"); let mut pdf = String::from("%PDF-1.4\n");
let mut offsets = Vec::new(); let mut offsets = Vec::new();
@@ -95,3 +117,8 @@ pub fn display_list_to_pdf(list: &DisplayList, page_height: i64) -> Vec<u8> {
)); ));
pdf.into_bytes() pdf.into_bytes()
} }
/// Write one page; kept for the single-page spike and tests.
pub fn display_list_to_pdf(list: &DisplayList, page_height: i64) -> Vec<u8> {
display_lists_to_pdf(std::slice::from_ref(list), page_height)
}