Emit multi-page route-D PDFs
This commit is contained in:
1 parent
b6fab85f61
commit
7dd3961e21
2 files changed
+56
-18
No files matched your search
@@ -1322,6 +1322,17 @@ mod tests {
|
||||
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]
|
||||
fn display_list_writes_a_minimal_pdf() {
|
||||
let document = Document::new(400, vec![Unit::paragraph(1, "hello")]);
|
||||
|
||||
@@ -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
|
||||
//! rectangles for rules. It proves the display-list -> PDF path before the
|
||||
//! real NFSS/TFM font backend and XObject support arrive.
|
||||
//! Base-14 Times-Roman text and filled rectangles only. This is the
|
||||
//! structural backend: multi-page output is supported; TFM/NFSS fonts,
|
||||
//! XObjects and `\special` are later milestones.
|
||||
|
||||
use crate::render::{DisplayItem, DisplayList};
|
||||
|
||||
@@ -24,10 +24,7 @@ fn pt(value: i64) -> String {
|
||||
format!("{:.3}", value as f64 / 65_536.0)
|
||||
}
|
||||
|
||||
/// Write one page. The media box is at least US Letter.
|
||||
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);
|
||||
fn page_content(list: &DisplayList, page_height: i64, height: i64) -> String {
|
||||
let mut content = String::from("q\n0 0 0 rg\n");
|
||||
for item in &list.items {
|
||||
match item {
|
||||
@@ -57,22 +54,47 @@ pub fn display_list_to_pdf(list: &DisplayList, page_height: i64) -> Vec<u8> {
|
||||
}
|
||||
}
|
||||
content.push_str("Q\n");
|
||||
let _ = page_height;
|
||||
content
|
||||
}
|
||||
|
||||
let objects = vec![
|
||||
"<< /Type /Catalog /Pages 2 0 R >>".to_string(),
|
||||
"<< /Type /Pages /Kids [4 0 R] /Count 1 >>".to_string(),
|
||||
"<< /Type /Font /Subtype /Type1 /BaseFont /Times-Roman >>".to_string(),
|
||||
format!(
|
||||
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {} {}] /Resources << /Font << /F1 3 0 R >> >> /Contents 5 0 R >>",
|
||||
/// Write several display lists as a multi-page PDF.
|
||||
pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8> {
|
||||
let height = page_height.max(792 * 65_536);
|
||||
let width = pages
|
||||
.iter()
|
||||
.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(height)
|
||||
),
|
||||
format!(
|
||||
));
|
||||
let content = page_content(list, page_height, height);
|
||||
objects.push(format!(
|
||||
"<< /Length {} >>\nstream\n{}endstream",
|
||||
content.len(),
|
||||
content
|
||||
),
|
||||
];
|
||||
));
|
||||
}
|
||||
objects[1] = format!(
|
||||
"<< /Type /Pages /Kids [{}] /Count {} >>",
|
||||
kids.join(" "),
|
||||
pages.len()
|
||||
);
|
||||
|
||||
let mut pdf = String::from("%PDF-1.4\n");
|
||||
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()
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
Reference in new issue
Block a user