Build native PDF article threads and bead rings
This commit is contained in:
1 parent
de4bd1fa76
commit
4fb0f2c89b
15 files changed
+528
-28
No files matched your search
@@ -30,7 +30,8 @@ impl Executor {
|
||||
Pdfsave | Pdfsetmatrix | Pdfrestore | Pdfliteral => {
|
||||
self.scan_pdf_graphics(primitive, token.span, files)?
|
||||
}
|
||||
Pdfannot | Pdfstartlink | Pdfendlink | Pdfdest | Pdfrunninglinkoff | Pdfrunninglinkon => {
|
||||
Pdfannot | Pdfstartlink | Pdfendlink | Pdfdest | Pdfrunninglinkoff | Pdfrunninglinkon
|
||||
| Pdfthread | Pdfstartthread | Pdfendthread => {
|
||||
self.scan_pdf_link(primitive, token.span, files)?;
|
||||
}
|
||||
Pdfinterwordspaceon | Pdfinterwordspaceoff | Pdffakespace | Pdfspacefont => {
|
||||
|
||||
@@ -289,13 +289,15 @@ impl Executor {
|
||||
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()),
|
||||
AnnotationAction::Catalog(action @ (CatalogAction::GoTo { file: None, .. }
|
||||
| CatalogAction::Thread { 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();
|
||||
let beads: Vec<_> = shipout.pdf_links.thread_beads.iter().map(|bead| bead.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();
|
||||
@@ -311,6 +313,13 @@ impl Executor {
|
||||
for number in destinations {
|
||||
self.pack_pdf_object(number)?;
|
||||
}
|
||||
for bead in beads {
|
||||
self.reads.insert(Dependency::PdfObjects);
|
||||
let rectangle = self.state.pdf_objects.as_deref_mut().expect("registered thread bead")
|
||||
.new_bead_rectangle(bead, self.limits.tokens)
|
||||
.map_err(|error| self.error(error, self.current))?;
|
||||
self.pack_pdf_object(rectangle)?;
|
||||
}
|
||||
self.pack_pdf_object(resources)?;
|
||||
}
|
||||
self.statistics_after_shipout(before, files)?;
|
||||
@@ -491,10 +500,17 @@ impl Executor {
|
||||
let output_count = outputs.len();
|
||||
let has_link_material = !self.pdf_link_stack.is_empty() || outputs.iter().any(|node| node.is_pdf_link());
|
||||
let mut pdf_link_shipout = (!for_form && device.pdf.is_some()
|
||||
&& (has_link_material || outputs.iter().any(|node| {
|
||||
&& (has_link_material || self.pdf_active_thread.is_some() || outputs.iter().any(|node| {
|
||||
matches!(node.operation, OutputOperation::PdfSetMatrix(_))
|
||||
})))
|
||||
.then(|| self.begin_pdf_link_shipout(b, device));
|
||||
.then(|| {
|
||||
let thread_rows = self.pdf_active_thread.is_some() || outputs.iter().any(|node| {
|
||||
matches!(node.operation, OutputOperation::PdfLink(
|
||||
crate::pdf_device::links::LinkNode::Thread { running: true, .. }
|
||||
))
|
||||
});
|
||||
self.begin_pdf_link_shipout(b, device, thread_rows)
|
||||
});
|
||||
let mut output_positions = if let Some(effects) = &leader_effects {
|
||||
effects.output_positions.clone().into_iter()
|
||||
} else if outputs.iter().any(|node| node.needs_shipout_position()) {
|
||||
@@ -749,6 +765,13 @@ impl Executor {
|
||||
{
|
||||
return Err(self.error("annotations cannot be inside an XForm", node.span));
|
||||
}
|
||||
if let Some(node) = first
|
||||
&& matches!(node.operation, OutputOperation::PdfLink(
|
||||
crate::pdf_device::links::LinkNode::Thread { .. }
|
||||
))
|
||||
{
|
||||
return Err(self.error("threads cannot be inside an XForm", node.span));
|
||||
}
|
||||
return self.fail("PDF page links and destinations require a page shipout");
|
||||
}
|
||||
for usage in font_uses {
|
||||
|
||||
@@ -209,6 +209,14 @@ impl Executor {
|
||||
if enabled {
|
||||
// An action allocates a PDF object at the command boundary.
|
||||
self.pdf_page_data(files)?;
|
||||
if matches!(&action, CatalogAction::Thread { .. }) {
|
||||
self.reads.insert(Dependency::PdfObjects);
|
||||
let number = self.state.pdf_objects.get_or_insert_with(Default::default)
|
||||
.allocate_thread_open_action(self.limits.tokens)
|
||||
.map_err(|message| self.error(message, self.current))?;
|
||||
self.pack_pdf_object(number)?;
|
||||
self.write_pdf_link_action(&action)?;
|
||||
}
|
||||
self.state
|
||||
.pdf_catalog
|
||||
.get_or_insert_with(Default::default)
|
||||
|
||||
@@ -4,7 +4,7 @@ use crate::pdf_device::{
|
||||
catalog::{CatalogAction, CatalogTarget, StructureTarget},
|
||||
links::{
|
||||
AnnotationAction, DestinationId, DestinationView, LinkAction, LinkAnnotation, LinkNode, LinkStart,
|
||||
PageDestination, PageLinks,
|
||||
PageDestination, PageLinks, ThreadBead,
|
||||
},
|
||||
};
|
||||
use crate::execution::boxes::{BoxPaint, BoxLayout};
|
||||
@@ -26,6 +26,13 @@ pub(super) struct ActiveLink {
|
||||
segment: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) struct ActiveThread {
|
||||
destination: DestinationId<String>,
|
||||
dimensions: [Option<i32>; 3],
|
||||
level: usize,
|
||||
}
|
||||
|
||||
enum LinkPaint {
|
||||
Graphics,
|
||||
RowStart(BoxLayout),
|
||||
@@ -36,6 +43,7 @@ pub(super) struct PdfLinkShipout {
|
||||
events: std::iter::Peekable<std::vec::IntoIter<((usize, usize), i64, i64, LinkPaint)>>,
|
||||
output_count: usize,
|
||||
active: Vec<ActiveLink>,
|
||||
thread: Option<ActiveThread>,
|
||||
geometry: NavigationGeometry,
|
||||
saved: Vec<Option<Affine>>,
|
||||
page_height: i64,
|
||||
@@ -184,6 +192,23 @@ impl Executor {
|
||||
self.state.pdf_objects.as_mut().unwrap().record_last_annotation(object_number);
|
||||
LinkNode::Annotation { object_number, dimensions, entries }
|
||||
}
|
||||
Primitive::Pdfthread | Primitive::Pdfstartthread => {
|
||||
let dimensions = self.scan_pdf_rule(files)?;
|
||||
let attributes = self.keyword("attr", files)?.then(|| self.scan_pdf_tokens(files)).transpose()?;
|
||||
let destination = if self.keyword("num", files)? {
|
||||
let number = self.positive_pdf_number("thread identifier", files)?;
|
||||
if number > 268_435_455 {
|
||||
return self.fail("PDF thread number too big");
|
||||
}
|
||||
DestinationId::Number(number)
|
||||
} else if self.keyword("name", files)? {
|
||||
DestinationId::Name(self.scan_pdf_tokens(files)?)
|
||||
} else {
|
||||
return self.fail("expected a PDF thread identifier");
|
||||
};
|
||||
LinkNode::Thread { destination, dimensions, attributes, running: command == Primitive::Pdfstartthread }
|
||||
}
|
||||
Primitive::Pdfendthread => LinkNode::EndThread,
|
||||
Primitive::Pdfstartlink => {
|
||||
self.reads.insert(Dependency::PdfObjects);
|
||||
let object_number = self
|
||||
@@ -322,6 +347,13 @@ impl Executor {
|
||||
}
|
||||
|
||||
pub(super) fn write_pdf_link_action(&mut self, action: &CatalogAction) -> Result<(), Error> {
|
||||
if let CatalogAction::Thread { destination, file: None } = action {
|
||||
self.reads.insert(Dependency::PdfObjects);
|
||||
self.state.pdf_objects.get_or_insert_with(Default::default)
|
||||
.thread_number(destination.clone(), self.limits.tokens)
|
||||
.map_err(|message| self.error(message, self.current))?;
|
||||
return Ok(());
|
||||
}
|
||||
let CatalogAction::GoTo { target, file: None, structure, .. } = action else {
|
||||
return Ok(());
|
||||
};
|
||||
@@ -349,7 +381,7 @@ impl Executor {
|
||||
}
|
||||
|
||||
pub(super) fn begin_pdf_link_shipout(
|
||||
&mut self, b: &NativeBox, device: &ShipoutDevice,
|
||||
&mut self, b: &NativeBox, device: &ShipoutDevice, thread_rows: bool,
|
||||
) -> PdfLinkShipout {
|
||||
let mut output_count = 0;
|
||||
let mut paint_count = 0;
|
||||
@@ -359,7 +391,8 @@ impl Executor {
|
||||
b.walk_pdf_links(&mut |x, y, item| match item {
|
||||
BoxPaint::Output(_) => output_count += 1,
|
||||
BoxPaint::Box(_) => paint_count += 1,
|
||||
BoxPaint::RowStart(row) => events.push(((output_count, paint_count), x, y, LinkPaint::RowStart(row))),
|
||||
BoxPaint::RowStart(row) if row.horizontal || thread_rows =>
|
||||
events.push(((output_count, paint_count), x, y, LinkPaint::RowStart(row))),
|
||||
BoxPaint::PdfLink(_, row) => events.push(((output_count, paint_count), x, y, LinkPaint::Node(row))),
|
||||
BoxPaint::PdfGraphics => events.push(((output_count, paint_count), x, y, LinkPaint::Graphics)),
|
||||
_ => {}
|
||||
@@ -369,6 +402,7 @@ impl Executor {
|
||||
events: events.into_iter().peekable(),
|
||||
output_count,
|
||||
active: std::mem::take(&mut self.pdf_link_stack),
|
||||
thread: self.pdf_active_thread.take(),
|
||||
geometry: if numbered { self.pdf_navigation_geometry } else { NavigationGeometry::default() },
|
||||
saved: Vec::new(),
|
||||
page_height: device.page_height,
|
||||
@@ -383,6 +417,23 @@ impl Executor {
|
||||
}
|
||||
|
||||
impl PdfLinkShipout {
|
||||
fn add_bead(
|
||||
&mut self, executor: &mut Executor, destination: DestinationId<String>, rect: [i64; 4], attributes: Option<Vec<u8>>,
|
||||
) -> Result<(), Error> {
|
||||
executor.reads.insert(Dependency::PdfObjects);
|
||||
let margin = i64::from(executor.read_parameter(Parameter::Pdfthreadmargin).width());
|
||||
let limit = executor.limits.tokens;
|
||||
let objects = executor.state.pdf_objects.get_or_insert_with(Default::default);
|
||||
let allocation = objects.thread_number(destination.clone(), limit)
|
||||
.and_then(|thread| objects.new_bead(thread, limit).map(|number| (thread, number)));
|
||||
let (thread, number) = allocation.map_err(|error| executor.error(error, executor.current))?;
|
||||
self.output.thread_beads.push(ThreadBead {
|
||||
number, thread, destination, attributes,
|
||||
rect: self.geometry.rectangle(rect, self.page_height, margin),
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn advance(
|
||||
&mut self, executor: &mut Executor, through: (usize, usize), outputs: &[&OutputNode],
|
||||
graphics: &[Option<PdfGraphics>],
|
||||
@@ -424,6 +475,15 @@ impl PdfLinkShipout {
|
||||
Some(PdfGraphics::Literal { .. } | PdfGraphics::TextBoundary) | None => {}
|
||||
}
|
||||
}
|
||||
LinkPaint::RowStart(row) if !row.horizontal => {
|
||||
if let Some(thread) = &self.thread
|
||||
&& thread.level == row.level && thread.dimensions[2].is_none()
|
||||
&& !self.output.thread_beads.is_empty()
|
||||
{
|
||||
let rect = rule_rectangle(absolute_row(row), [x, y], thread.dimensions);
|
||||
self.add_bead(executor, thread.destination.clone(), rect, None)?;
|
||||
}
|
||||
}
|
||||
LinkPaint::RowStart(row) if executor.pdf_running_links => {
|
||||
for link in self.active.iter_mut().filter(|link| {
|
||||
link.level == row.level && link.layout.width.is_none()
|
||||
@@ -452,6 +512,35 @@ impl PdfLinkShipout {
|
||||
action: AnnotationAction::Direct(executor.pdf_token_bytes(entries)?),
|
||||
});
|
||||
}
|
||||
LinkNode::Thread { destination, dimensions, attributes, running } => {
|
||||
if *running && container.horizontal {
|
||||
return executor.fail("\\pdfstartthread ended up in hlist");
|
||||
}
|
||||
let destination = executor.pdf_destination_id(destination);
|
||||
let attributes = attributes.as_deref()
|
||||
.map(|tokens| executor.pdf_token_bytes(tokens)).transpose()?;
|
||||
let rect = rule_rectangle(container, [x, y], *dimensions);
|
||||
if *running {
|
||||
self.thread = Some(ActiveThread {
|
||||
destination: destination.clone(), dimensions: *dimensions,
|
||||
level: container.level,
|
||||
});
|
||||
}
|
||||
self.add_bead(executor, destination, rect, attributes)?;
|
||||
}
|
||||
LinkNode::EndThread => {
|
||||
if container.horizontal {
|
||||
return executor.fail("\\pdfendthread ended up in hlist");
|
||||
}
|
||||
let Some(thread) = self.thread.take().filter(|thread| thread.level == container.level) else {
|
||||
return executor.fail("\\pdfendthread ended up in different nesting level than \\pdfstartthread");
|
||||
};
|
||||
if thread.dimensions[2].is_none()
|
||||
&& let Some(bead) = self.output.thread_beads.last_mut()
|
||||
{
|
||||
bead.rect[3] = y + i64::from(executor.read_parameter(Parameter::Pdfthreadmargin).width());
|
||||
}
|
||||
}
|
||||
LinkNode::Start(layout) => {
|
||||
if self.active.len() == 10 {
|
||||
return executor.fail("PDF link stack capacity exceeded");
|
||||
@@ -546,6 +635,7 @@ impl PdfLinkShipout {
|
||||
link.segment = None;
|
||||
}
|
||||
executor.pdf_link_stack = self.active;
|
||||
executor.pdf_active_thread = self.thread;
|
||||
if self.numbered {
|
||||
executor.pdf_navigation_geometry = self.geometry;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user