Emit PDF link annotations from display lists
This commit is contained in:
1 parent
431bbd14d4
commit
da7f580bf0
4 files changed
+103
-2
No files matched your search
@@ -1334,6 +1334,28 @@ mod tests {
|
|||||||
assert_eq!(text.matches("/Type /Page ").count(), 2);
|
assert_eq!(text.matches("/Type /Page ").count(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn link_rectangles_become_pdf_annotations() {
|
||||||
|
use crate::render::DisplayList;
|
||||||
|
let mut list = DisplayList {
|
||||||
|
width: 612 * 65_536,
|
||||||
|
height: 792 * 65_536,
|
||||||
|
items: Vec::new(),
|
||||||
|
images: Vec::new(),
|
||||||
|
};
|
||||||
|
list.link(
|
||||||
|
72 * 65_536,
|
||||||
|
700 * 65_536,
|
||||||
|
100 * 65_536,
|
||||||
|
12 * 65_536,
|
||||||
|
"https://example.com/paper",
|
||||||
|
);
|
||||||
|
let pdf = crate::pdf::display_list_to_pdf(&list, 792 * 65_536);
|
||||||
|
let text = String::from_utf8_lossy(&pdf);
|
||||||
|
assert!(text.contains("/Subtype /Link"));
|
||||||
|
assert!(text.contains("/A << /S /URI /URI (https://example.com/paper) >>"));
|
||||||
|
assert!(text.contains("/Annots ["));
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn image_xobject_is_embedded_and_painted() {
|
fn image_xobject_is_embedded_and_painted() {
|
||||||
use crate::render::{DisplayItem, DisplayList, PdfImage};
|
use crate::render::{DisplayItem, DisplayList, PdfImage};
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ fn page_content(list: &DisplayList, height: i64, image_names: &BTreeMap<usize, S
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
DisplayItem::Link { .. } => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
content.push_str("Q\n");
|
content.push_str("Q\n");
|
||||||
@@ -113,10 +114,38 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
|
|||||||
} else {
|
} else {
|
||||||
format!("<< /Font << /F1 3 0 R >> /XObject << {xobjects} >> >>")
|
format!("<< /Font << /F1 3 0 R >> /XObject << {xobjects} >> >>")
|
||||||
};
|
};
|
||||||
|
let links = list
|
||||||
|
.items
|
||||||
|
.iter()
|
||||||
|
.filter_map(|item| match item {
|
||||||
|
DisplayItem::Link {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
url,
|
||||||
|
} => Some((*x, *y, *width, *height, url.clone())),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
let annotation_base = image_base + used.len();
|
||||||
|
let annots = if links.is_empty() {
|
||||||
|
String::new()
|
||||||
|
} else {
|
||||||
|
let refs = (0..links.len())
|
||||||
|
.map(|index| {
|
||||||
|
format!("/Annots [{}]", (annotation_base + index))
|
||||||
|
.replace("/Annots [", "")
|
||||||
|
.replacen("]", "", 1)
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(" ");
|
||||||
|
format!(" /Annots [{refs}]")
|
||||||
|
};
|
||||||
kids.push(format!("{page_id} 0 R"));
|
kids.push(format!("{page_id} 0 R"));
|
||||||
objects.push(
|
objects.push(
|
||||||
format!(
|
format!(
|
||||||
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {} {}] /Resources {resources} /Contents {content_id} 0 R >>",
|
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {} {}] /Resources {resources}{annots} /Contents {content_id} 0 R >>",
|
||||||
pt(width),
|
pt(width),
|
||||||
pt(height)
|
pt(height)
|
||||||
)
|
)
|
||||||
@@ -150,6 +179,21 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
|
|||||||
object.extend_from_slice(b"\nendstream");
|
object.extend_from_slice(b"\nendstream");
|
||||||
objects.push(object);
|
objects.push(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (index, (x, y, width, link_height, url)) in links.iter().enumerate() {
|
||||||
|
let _ = index;
|
||||||
|
objects.push(
|
||||||
|
format!(
|
||||||
|
"<< /Type /Annot /Subtype /Link /Rect [{:.3} {:.3} {:.3} {:.3}] /Border [0 0 0] /A << /S /URI /URI ({}) >> >>",
|
||||||
|
*x as f64 / 65_536.0,
|
||||||
|
(height as f64 - *y as f64 - *link_height as f64) / 65_536.0,
|
||||||
|
(*x + *width) as f64 / 65_536.0,
|
||||||
|
(height as f64 - *y as f64) / 65_536.0,
|
||||||
|
escape(url)
|
||||||
|
)
|
||||||
|
.into_bytes(),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
objects[1] = format!(
|
objects[1] = format!(
|
||||||
"<< /Type /Pages /Kids [{}] /Count {} >>",
|
"<< /Type /Pages /Kids [{}] /Count {} >>",
|
||||||
|
|||||||
@@ -31,6 +31,13 @@ pub enum DisplayItem {
|
|||||||
height: i64,
|
height: i64,
|
||||||
index: usize,
|
index: usize,
|
||||||
},
|
},
|
||||||
|
Link {
|
||||||
|
x: i64,
|
||||||
|
y: i64,
|
||||||
|
width: i64,
|
||||||
|
height: i64,
|
||||||
|
url: String,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A raw RGB image that can be painted as a PDF XObject.
|
/// A raw RGB image that can be painted as a PDF XObject.
|
||||||
@@ -55,6 +62,16 @@ impl DisplayList {
|
|||||||
self.items.push(item);
|
self.items.push(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn link(&mut self, x: i64, y: i64, width: i64, height: i64, url: &str) {
|
||||||
|
self.items.push(DisplayItem::Link {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
url: url.to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
pub fn add_image(&mut self, image: PdfImage) -> usize {
|
pub fn add_image(&mut self, image: PdfImage) -> usize {
|
||||||
self.images.push(image);
|
self.images.push(image);
|
||||||
self.images.len() - 1
|
self.images.len() - 1
|
||||||
@@ -72,7 +89,7 @@ impl DisplayList {
|
|||||||
*x += dx;
|
*x += dx;
|
||||||
*y += dy;
|
*y += dy;
|
||||||
}
|
}
|
||||||
DisplayItem::Image { x, y, .. } => {
|
DisplayItem::Image { x, y, .. } | DisplayItem::Link { x, y, .. } => {
|
||||||
*x += dx;
|
*x += dx;
|
||||||
*y += dy;
|
*y += dy;
|
||||||
}
|
}
|
||||||
@@ -112,6 +129,22 @@ impl DisplayList {
|
|||||||
"<rect x=\"{x:.3}\" y=\"{y:.3}\" width=\"{width:.3}\" height=\"{height:.3}\" fill=\"black\"/>\n"
|
"<rect x=\"{x:.3}\" y=\"{y:.3}\" width=\"{width:.3}\" height=\"{height:.3}\" fill=\"black\"/>\n"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
DisplayItem::Link {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
let x = *x as f64 / 65_536.0;
|
||||||
|
let y = *y as f64 / 65_536.0;
|
||||||
|
let width = *width as f64 / 65_536.0;
|
||||||
|
let height = *height as f64 / 65_536.0;
|
||||||
|
out.push_str(&format!(
|
||||||
|
"<rect x=\"{x:.3}\" y=\"{y:.3}\" width=\"{width:.3}\" height=\"{height:.3}\" fill=\"none\" stroke=\"#008000\"/>
|
||||||
|
"
|
||||||
|
));
|
||||||
|
}
|
||||||
DisplayItem::Image {
|
DisplayItem::Image {
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
|
|||||||
@@ -253,3 +253,5 @@ Paragraph painter: `Unit::paragraph` now stores `VerticalItem::Paragraph`, and `
|
|||||||
Math painter: stores ; paints symbols, rows, scripts, fractions (with a rule), roots, delimiters, accents, underlines, boxes and stacks. Script/fraction positions are simplified TeX geometry.
|
Math painter: stores ; paints symbols, rows, scripts, fractions (with a rule), roots, delimiters, accents, underlines, boxes and stacks. Script/fraction positions are simplified TeX geometry.
|
||||||
|
|
||||||
PDF writer: `src/pdf.rs` emits a one-page PDF 1.4 for a `DisplayList` (Base-14 Times-Roman text, filled rectangle rules, US Letter media box). It is a structural backend spike; TFM/NFSS fonts, XObjects and multi-page output are still open.
|
PDF writer: `src/pdf.rs` emits a one-page PDF 1.4 for a `DisplayList` (Base-14 Times-Roman text, filled rectangle rules, US Letter media box). It is a structural backend spike; TFM/NFSS fonts, XObjects and multi-page output are still open.
|
||||||
|
|
||||||
|
M6 (partial): display-list Link rectangles become PDF /Annots /Subtype /Link /URI dictionaries. Raw-RGB images are PDF XObjects; /special and embedded font subsets remain.
|
||||||
Reference in new issue
Block a user