Register native PDF navigation and raw objects in write order
This commit is contained in:
1 parent
70ebacb056
commit
e95d1264da
9 files changed
+350
-48
No files matched your search
@@ -269,12 +269,32 @@ impl Executor {
|
||||
let before = self.statistics_before_shipout(&shipout.box_node, files)?;
|
||||
self.shipped_pages += 1;
|
||||
let page_resources = shipout.pdf_objects.map(|objects| objects.resources);
|
||||
let links: Vec<_> = shipout.pdf_links.annotations.iter().filter_map(|annotation| {
|
||||
use crate::pdf_device::{catalog::CatalogAction, links::AnnotationAction};
|
||||
let number = annotation.object_number?;
|
||||
let action = match &annotation.action {
|
||||
AnnotationAction::Catalog(action @ CatalogAction::GoTo { file: None, .. }) => Some(action.clone()),
|
||||
_ => None,
|
||||
};
|
||||
Some((number, action))
|
||||
}).collect();
|
||||
let destinations: Vec<_> = shipout.pdf_links.destinations.iter()
|
||||
.filter_map(|destination| destination.object_number).collect();
|
||||
self.events.push(Event::Shipout(Box::new(shipout)));
|
||||
if probed_page && let Some(probe) = self.pdf_space_probe.as_mut() {
|
||||
Arc::make_mut(probe).observed_events = self.events.len();
|
||||
}
|
||||
self.write_shipout_forms(forms, files)?;
|
||||
if let Some(resources) = page_resources {
|
||||
for (number, action) in links {
|
||||
self.pack_pdf_object(number)?;
|
||||
if let Some(action) = action {
|
||||
self.write_pdf_link_action(&action)?;
|
||||
}
|
||||
}
|
||||
for number in destinations {
|
||||
self.pack_pdf_object(number)?;
|
||||
}
|
||||
self.pack_pdf_object(resources)?;
|
||||
}
|
||||
self.statistics_after_shipout(before, files)?;
|
||||
@@ -435,6 +455,7 @@ impl Executor {
|
||||
if let Some(effects) = &leader_effects {
|
||||
outputs = effects.outputs.iter().collect();
|
||||
}
|
||||
let mut navigation_numbers = (Vec::new(), Vec::new());
|
||||
let mut link_depth = self.pdf_link_stack.len();
|
||||
let has_link_material = link_depth != 0 || outputs.iter().any(|node| node.is_pdf_link());
|
||||
let mut output_positions = if let Some(effects) = &leader_effects {
|
||||
@@ -655,11 +676,19 @@ impl Executor {
|
||||
LinkNode::Start(_) if link_depth == 10 => {
|
||||
return self.fail("PDF link stack capacity exceeded");
|
||||
}
|
||||
LinkNode::Start(_) => link_depth += 1,
|
||||
LinkNode::Start(layout) => {
|
||||
link_depth += 1;
|
||||
if !unicode_pages && !for_form {
|
||||
navigation_numbers.0.push(self.register_pdf_link(layout.object_number)?);
|
||||
}
|
||||
}
|
||||
LinkNode::End if link_depth == 0 => {
|
||||
return self.fail("\\pdfendlink used without \\pdfstartlink");
|
||||
}
|
||||
LinkNode::End => link_depth -= 1,
|
||||
LinkNode::Destination { destination, structure, .. } if !unicode_pages && !for_form => {
|
||||
navigation_numbers.1.push(self.register_pdf_destination(destination, structure.is_some())?);
|
||||
}
|
||||
LinkNode::Running(_) | LinkNode::Destination { .. } => {}
|
||||
}
|
||||
} else {
|
||||
@@ -706,7 +735,7 @@ impl Executor {
|
||||
return self.fail("PDF page links and destinations require a page shipout");
|
||||
}
|
||||
let mut links = self.ship_pdf_link_material(
|
||||
b, device, &shipout.pdf_graphics_steps, files,
|
||||
b, device, &shipout.pdf_graphics_steps, navigation_numbers, files,
|
||||
)?;
|
||||
links.xdv_destinations = std::mem::take(&mut shipout.pdf_links.xdv_destinations);
|
||||
shipout.pdf_links = links;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use super::*;
|
||||
use crate::pdf_device::{
|
||||
Affine, PdfGraphics,
|
||||
catalog::{CatalogAction, CatalogTarget, StructureTarget},
|
||||
links::{
|
||||
AnnotationAction, DestinationId, DestinationView, LinkAction, LinkAnnotation, LinkNode, LinkStart,
|
||||
PageDestination, PageLinks,
|
||||
@@ -225,14 +226,63 @@ impl Executor {
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn register_pdf_link(&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)
|
||||
.map_err(|message| self.error(message, self.current))
|
||||
}
|
||||
|
||||
pub(super) fn register_pdf_destination(
|
||||
&mut self, id: &DestinationId<Vec<Token>>, structure: bool,
|
||||
) -> Result<Option<u32>, Error> {
|
||||
self.reads.insert(Dependency::PdfObjects);
|
||||
let id = self.pdf_destination_id(id);
|
||||
self.state.pdf_objects.get_or_insert_with(Default::default)
|
||||
.destination_number(id, structure, true, self.limits.tokens)
|
||||
.map_err(|message| self.error(message, self.current))
|
||||
}
|
||||
|
||||
pub(super) fn write_pdf_link_action(&mut self, action: &CatalogAction) -> Result<(), Error> {
|
||||
let CatalogAction::GoTo { target, file: None, structure, .. } = action else {
|
||||
return Ok(());
|
||||
};
|
||||
self.reads.insert(Dependency::PdfObjects);
|
||||
let limit = self.limits.tokens;
|
||||
let objects = self.state.pdf_objects.as_deref_mut().expect("registered PDF link");
|
||||
let allocation = match target {
|
||||
CatalogTarget::Page { number, .. } => objects.page_reference(*number as u32, limit).map(Some),
|
||||
CatalogTarget::Destination(id) => objects.destination_number(id.clone(), false, false, limit),
|
||||
};
|
||||
allocation.map_err(|message| self.error(message, self.current))?;
|
||||
if let Some(StructureTarget::Local(id)) = structure {
|
||||
self.state.pdf_objects.as_deref_mut().expect("registered PDF link")
|
||||
.destination_number(id.clone(), true, false, limit)
|
||||
.map_err(|message| self.error(message, self.current))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pdf_destination_id(&mut self, id: &DestinationId<Vec<Token>>) -> DestinationId<String> {
|
||||
match id {
|
||||
DestinationId::Name(tokens) => DestinationId::Name(self.token_list_text(tokens)),
|
||||
DestinationId::Number(number) => DestinationId::Number(*number),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn ship_pdf_link_material(
|
||||
&mut self,
|
||||
b: &NativeBox,
|
||||
device: &ShipoutDevice,
|
||||
graphics: &[Option<PdfGraphics>],
|
||||
numbers: (Vec<u32>, Vec<Option<u32>>),
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<PageLinks, Error> {
|
||||
let margin = i64::from(self.read_parameter(Parameter::Pdflinkmargin).width());
|
||||
let numbered = self.state.input_profile() == InputProfile::TeX8Bit;
|
||||
let (links, destinations) = numbers;
|
||||
let mut links = links.into_iter();
|
||||
let mut destinations = destinations.into_iter();
|
||||
let mut active = std::mem::take(&mut self.pdf_link_stack);
|
||||
let mut graphics = graphics.iter();
|
||||
let mut transform = (Affine::IDENTITY, false);
|
||||
@@ -271,7 +321,16 @@ impl Executor {
|
||||
for link in active.iter_mut().filter(|link| {
|
||||
link.level == row.level && link.layout.width.is_none()
|
||||
}) {
|
||||
let number = if numbered {
|
||||
match self.register_pdf_link(None) {
|
||||
Ok(number) => Some(number),
|
||||
Err(error) => { failure = Some(error); return; }
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
link.start_segment(&mut output, absolute_row(row), [x, y], transform, margin);
|
||||
output.annotations.last_mut().unwrap().object_number = number;
|
||||
}
|
||||
}
|
||||
BoxPaint::PdfLink(node, container) => {
|
||||
@@ -293,9 +352,12 @@ impl Executor {
|
||||
let mut start = ActiveLink { layout: layout.clone(), attributes,
|
||||
action, level: container.level, segment: None };
|
||||
start.start_segment(&mut output, container, [x, y], transform, margin);
|
||||
// The scan-time object number belongs only to the
|
||||
// initial annotation, never to a continuation row.
|
||||
output.annotations.last_mut().unwrap().object_number = layout.object_number;
|
||||
let number = if numbered { links.next() } else { layout.object_number };
|
||||
if numbered && number.is_none() {
|
||||
failure = Some(self.error("missing PDF link number at shipout", node.span));
|
||||
return;
|
||||
}
|
||||
output.annotations.last_mut().unwrap().object_number = number;
|
||||
active.push(start);
|
||||
}
|
||||
LinkNode::End => {
|
||||
@@ -315,9 +377,15 @@ impl Executor {
|
||||
}
|
||||
LinkNode::Running(enabled) => self.pdf_running_links = *enabled,
|
||||
LinkNode::Destination { destination, view, structure } => {
|
||||
let destination = match destination {
|
||||
DestinationId::Name(tokens) => DestinationId::Name(self.token_list_text(tokens)),
|
||||
DestinationId::Number(number) => DestinationId::Number(*number),
|
||||
let destination = self.pdf_destination_id(destination);
|
||||
let object_number = if numbered {
|
||||
let Some(number) = destinations.next() else {
|
||||
failure = Some(self.error("missing PDF destination number at shipout", node.span));
|
||||
return;
|
||||
};
|
||||
number
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let view = view.map_rectangle(|[width, height, depth]| {
|
||||
let margin = i64::from(self.read_parameter(Parameter::Pdfdestmargin).width());
|
||||
@@ -331,7 +399,7 @@ impl Executor {
|
||||
}
|
||||
});
|
||||
output.destinations.push(PageDestination {
|
||||
destination, point: map_point(transform.0, x, y), view,
|
||||
object_number, destination, point: map_point(transform.0, x, y), view,
|
||||
structure: *structure,
|
||||
});
|
||||
}
|
||||
@@ -343,6 +411,9 @@ impl Executor {
|
||||
if let Some(error) = failure {
|
||||
return Err(error);
|
||||
}
|
||||
if links.next().is_some() || destinations.next().is_some() {
|
||||
return self.fail("unused PDF navigation numbers at shipout");
|
||||
}
|
||||
for link in &mut active {
|
||||
link.segment = None;
|
||||
}
|
||||
|
||||
@@ -60,6 +60,9 @@ impl Executor {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let number = self.state.pdf_objects.get_or_insert_with(Default::default)
|
||||
.begin_definition(requested, self.limits.tokens)
|
||||
.map_err(|error| self.error(error, self.current))?;
|
||||
let declaration = self.current;
|
||||
let attributes = if self.keyword("stream", files)? {
|
||||
Some(if self.keyword("attr", files)? {
|
||||
@@ -85,11 +88,8 @@ impl Executor {
|
||||
None => ObjectBody::Direct(bytes),
|
||||
}
|
||||
};
|
||||
let number = self
|
||||
.state
|
||||
.pdf_objects
|
||||
.get_or_insert_with(Default::default)
|
||||
.define(requested, value, self.limits.tokens)
|
||||
self.state.pdf_objects.as_deref_mut().expect("registered PDF object")
|
||||
.define(number, value, self.limits.tokens)
|
||||
.map_err(|error| self.error(error, self.current))?;
|
||||
if immediate {
|
||||
self.pdf_page_data(files)?;
|
||||
|
||||
Reference in new issue
Block a user