Ship native PDF annotations with reserved object numbers
This commit is contained in:
1 parent
975dbc8060
commit
de4bd1fa76
14 files changed
+299
-67
No files matched your search
@@ -30,7 +30,7 @@ impl Executor {
|
||||
Pdfsave | Pdfsetmatrix | Pdfrestore | Pdfliteral => {
|
||||
self.scan_pdf_graphics(primitive, token.span, files)?
|
||||
}
|
||||
Pdfstartlink | Pdfendlink | Pdfdest | Pdfrunninglinkoff | Pdfrunninglinkon => {
|
||||
Pdfannot | Pdfstartlink | Pdfendlink | Pdfdest | Pdfrunninglinkoff | Pdfrunninglinkon => {
|
||||
self.scan_pdf_link(primitive, token.span, files)?;
|
||||
}
|
||||
Pdfinterwordspaceon | Pdfinterwordspaceoff | Pdffakespace | Pdfspacefont => {
|
||||
|
||||
@@ -741,6 +741,14 @@ impl Executor {
|
||||
}
|
||||
self.current = current;
|
||||
if has_link_material && for_form {
|
||||
let first = outputs.iter().find(|node| node.is_pdf_link());
|
||||
if let Some(node) = first
|
||||
&& matches!(node.operation, OutputOperation::PdfLink(
|
||||
crate::pdf_device::links::LinkNode::Annotation { .. }
|
||||
))
|
||||
{
|
||||
return Err(self.error("annotations cannot be inside an XForm", node.span));
|
||||
}
|
||||
return self.fail("PDF page links and destinations require a page shipout");
|
||||
}
|
||||
for usage in font_uses {
|
||||
|
||||
@@ -41,6 +41,7 @@ pub(super) struct PdfLinkShipout {
|
||||
page_height: i64,
|
||||
graphics_index: usize,
|
||||
output: PageLinks,
|
||||
direct_annotations: Vec<LinkAnnotation>,
|
||||
origin: [i64; 2],
|
||||
margin: i64,
|
||||
numbered: bool,
|
||||
@@ -79,6 +80,15 @@ impl NavigationGeometry {
|
||||
}
|
||||
}
|
||||
|
||||
fn rule_rectangle(row: BoxLayout, [x, y]: [i64; 2], [width, height, depth]: [Option<i32>; 3]) -> [i64; 4] {
|
||||
[
|
||||
x,
|
||||
height.map_or(row.baseline - row.height, |h| y - i64::from(h)),
|
||||
width.map_or(row.left + row.width, |w| x + i64::from(w)),
|
||||
depth.map_or(row.baseline + row.depth, |d| y + i64::from(d)),
|
||||
]
|
||||
}
|
||||
|
||||
impl ActiveLink {
|
||||
fn start_segment(
|
||||
&mut self,
|
||||
@@ -89,19 +99,7 @@ impl ActiveLink {
|
||||
page_height: i64,
|
||||
margin: i64,
|
||||
) {
|
||||
let [x, y] = point;
|
||||
let rect = [
|
||||
x,
|
||||
self.layout
|
||||
.height
|
||||
.map_or(row.baseline - row.height, |h| y - i64::from(h)),
|
||||
self.layout
|
||||
.width
|
||||
.map_or(row.left + row.width, |w| x + i64::from(w)),
|
||||
self.layout
|
||||
.depth
|
||||
.map_or(row.baseline + row.depth, |d| y + i64::from(d)),
|
||||
];
|
||||
let rect = rule_rectangle(row, point, [self.layout.width, self.layout.height, self.layout.depth]);
|
||||
self.segment = Some(output.annotations.len());
|
||||
output.annotations.push(LinkAnnotation {
|
||||
object_number: None,
|
||||
@@ -147,7 +145,11 @@ impl Executor {
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), Error> {
|
||||
if self.native_page.is_none() || self.read_parameter(Parameter::Pdfoutput).width() <= 0 {
|
||||
return self.fail("PDF link operation not allowed in DVI mode");
|
||||
return self.fail(if command == Primitive::Pdfannot {
|
||||
"\\pdfannot not allowed in DVI mode"
|
||||
} else {
|
||||
"PDF link operation not allowed in DVI mode"
|
||||
});
|
||||
}
|
||||
if matches!(command, Primitive::Pdfstartlink | Primitive::Pdfendlink)
|
||||
&& self.paragraph.is_none()
|
||||
@@ -160,13 +162,35 @@ impl Executor {
|
||||
});
|
||||
}
|
||||
let operation = match command {
|
||||
Primitive::Pdfannot => {
|
||||
self.reads.insert(Dependency::PdfObjects);
|
||||
let limit = self.limits.tokens;
|
||||
if self.keyword("reserveobjnum", files)? {
|
||||
self.state.pdf_objects.get_or_insert_with(Default::default)
|
||||
.reserve_annotation_number(limit)
|
||||
.map_err(|error| self.error(error, span))?;
|
||||
return self.optional_space(files);
|
||||
}
|
||||
let requested = if self.keyword("useobjnum", files)? {
|
||||
Some(self.integer(files)? as u32)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let object_number = self.state.pdf_objects.get_or_insert_with(Default::default)
|
||||
.begin_annotation(requested, limit)
|
||||
.map_err(|error| self.error(error, span))?;
|
||||
let dimensions = self.scan_pdf_rule(files)?;
|
||||
let entries = self.scan_pdf_tokens(files)?;
|
||||
self.state.pdf_objects.as_mut().unwrap().record_last_annotation(object_number);
|
||||
LinkNode::Annotation { object_number, dimensions, entries }
|
||||
}
|
||||
Primitive::Pdfstartlink => {
|
||||
self.reads.insert(Dependency::PdfObjects);
|
||||
let object_number = self
|
||||
.state
|
||||
.pdf_objects
|
||||
.get_or_insert_with(Default::default)
|
||||
.reserve_link(self.limits.tokens)
|
||||
.allocate_annotation(self.limits.tokens)
|
||||
.map_err(|error| self.error(error, self.current))?;
|
||||
let [width, height, depth] = self.scan_pdf_rule(files)?;
|
||||
let attributes = if self.keyword("attr", files)? {
|
||||
@@ -281,10 +305,10 @@ impl Executor {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn register_pdf_link(&mut self, number: Option<u32>) -> Result<u32, Error> {
|
||||
pub(super) fn register_pdf_annotation(&mut self, number: Option<u32>) -> Result<u32, Error> {
|
||||
self.reads.insert(Dependency::PdfObjects);
|
||||
self.state.pdf_objects.get_or_insert_with(Default::default)
|
||||
.ship_link(number, self.limits.tokens)
|
||||
.ship_annotation(number, self.limits.tokens)
|
||||
.map_err(|message| self.error(message, self.current))
|
||||
}
|
||||
|
||||
@@ -350,6 +374,7 @@ impl Executor {
|
||||
page_height: device.page_height,
|
||||
graphics_index: 0,
|
||||
output: PageLinks::default(),
|
||||
direct_annotations: Vec::new(),
|
||||
origin: [device.horizontal_origin, device.vertical_origin + b.height],
|
||||
margin: i64::from(self.read_parameter(Parameter::Pdflinkmargin).width()),
|
||||
numbered,
|
||||
@@ -403,7 +428,7 @@ impl PdfLinkShipout {
|
||||
for link in self.active.iter_mut().filter(|link| {
|
||||
link.level == row.level && link.layout.width.is_none()
|
||||
}) {
|
||||
let number = if self.numbered { Some(executor.register_pdf_link(None)?) } else { None };
|
||||
let number = if self.numbered { Some(executor.register_pdf_annotation(None)?) } else { None };
|
||||
link.start_segment(&mut self.output, absolute_row(row), [x, y], &mut self.geometry,
|
||||
self.page_height, self.margin);
|
||||
self.output.annotations.last_mut().unwrap().object_number = number;
|
||||
@@ -417,6 +442,16 @@ impl PdfLinkShipout {
|
||||
let container = absolute_row(container);
|
||||
executor.current = node.span;
|
||||
match link {
|
||||
LinkNode::Annotation { object_number, dimensions, entries } => {
|
||||
let number = executor.register_pdf_annotation(Some(*object_number))?;
|
||||
let rect = rule_rectangle(container, [x, y], *dimensions);
|
||||
self.direct_annotations.push(LinkAnnotation {
|
||||
object_number: Some(number),
|
||||
rect: self.geometry.rectangle(rect, self.page_height, 0),
|
||||
attributes: String::new(),
|
||||
action: AnnotationAction::Direct(executor.pdf_token_bytes(entries)?),
|
||||
});
|
||||
}
|
||||
LinkNode::Start(layout) => {
|
||||
if self.active.len() == 10 {
|
||||
return executor.fail("PDF link stack capacity exceeded");
|
||||
@@ -425,7 +460,7 @@ impl PdfLinkShipout {
|
||||
return executor.fail("PDF link start arrived outside a horizontal list");
|
||||
}
|
||||
let number = if self.numbered {
|
||||
Some(executor.register_pdf_link(layout.object_number)?)
|
||||
Some(executor.register_pdf_annotation(layout.object_number)?)
|
||||
} else {
|
||||
layout.object_number
|
||||
};
|
||||
@@ -479,12 +514,10 @@ impl PdfLinkShipout {
|
||||
point
|
||||
}
|
||||
});
|
||||
let view = view.map_rectangle(|[width, height, depth]| {
|
||||
let view = view.map_rectangle(|dimensions| {
|
||||
let margin = i64::from(executor.read_parameter(Parameter::Pdfdestmargin).width());
|
||||
let top = height.map_or(container.baseline - container.height, |h| y - i64::from(h));
|
||||
let right = width.map_or(container.left + container.width, |w| x + i64::from(w));
|
||||
let bottom = depth.map_or(container.baseline + container.depth, |d| y + i64::from(d));
|
||||
self.geometry.rectangle([x, top, right, bottom], self.page_height, margin)
|
||||
let rect = rule_rectangle(container, [x, y], dimensions);
|
||||
self.geometry.rectangle(rect, self.page_height, margin)
|
||||
});
|
||||
self.output.destinations.push(PageDestination {
|
||||
object_number, destination, point, view,
|
||||
@@ -516,6 +549,10 @@ impl PdfLinkShipout {
|
||||
if self.numbered {
|
||||
executor.pdf_navigation_geometry = self.geometry;
|
||||
}
|
||||
if !self.direct_annotations.is_empty() {
|
||||
self.direct_annotations.append(&mut self.output.annotations);
|
||||
self.output.annotations = self.direct_annotations;
|
||||
}
|
||||
Ok(self.output)
|
||||
}
|
||||
}
|
||||
@@ -220,6 +220,7 @@ impl Executor {
|
||||
| Primitive::Pdfrandomseed
|
||||
| Primitive::Pdflastobj
|
||||
| Primitive::Pdflastlink
|
||||
| Primitive::Pdflastannot
|
||||
| Primitive::Pdflastxform
|
||||
| Primitive::Pdflastximage
|
||||
| Primitive::Pdflastximagepages
|
||||
|
||||
Reference in new issue
Block a user