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
@@ -1013,6 +1013,9 @@ impl NativeBox {
|
||||
level,
|
||||
};
|
||||
let mut cursor = y - self.height;
|
||||
if links {
|
||||
emit(edge, cursor, BoxPaint::RowStart(container));
|
||||
}
|
||||
let (mut cumulative, mut previous) = (0, 0);
|
||||
for node in nodes.iter() {
|
||||
budget.inside_leader(1)?;
|
||||
@@ -1112,7 +1115,8 @@ impl NativeBox {
|
||||
&& !matches!(paint, BoxPaint::PdfLink(node, _) if matches!(
|
||||
node.operation,
|
||||
OutputOperation::PdfLink(crate::pdf_device::links::LinkNode::Destination { .. }
|
||||
| crate::pdf_device::links::LinkNode::Annotation { .. })
|
||||
| crate::pdf_device::links::LinkNode::Annotation { .. }
|
||||
| crate::pdf_device::links::LinkNode::Thread { .. })
|
||||
))
|
||||
{
|
||||
emit(x, y, paint);
|
||||
|
||||
@@ -681,6 +681,9 @@ impl Printer<'_> {
|
||||
self.command(match node {
|
||||
LinkNode::Start(_) => "pdfstartlink",
|
||||
LinkNode::Annotation { .. } => "pdfannot",
|
||||
LinkNode::Thread { running: false, .. } => "pdfthread",
|
||||
LinkNode::Thread { running: true, .. } => "pdfstartthread",
|
||||
LinkNode::EndThread => "pdfendthread",
|
||||
LinkNode::End => "pdfendlink",
|
||||
LinkNode::Running(false) => "pdfrunninglinkoff",
|
||||
LinkNode::Running(true) => "pdfrunninglinkon",
|
||||
|
||||
@@ -230,6 +230,7 @@ pub struct Executor {
|
||||
xdv_colors: crate::pdf_device::xdv_color::ColorState,
|
||||
xdv_annotations: crate::pdf_device::xdv_annotation::AnnotationState,
|
||||
pdf_link_stack: Vec<pdf_links::ActiveLink>,
|
||||
pdf_active_thread: Option<pdf_links::ActiveThread>,
|
||||
pdf_navigation_geometry: pdf_links::NavigationGeometry,
|
||||
pdf_running_links: bool,
|
||||
pdf_interword_spaces: bool,
|
||||
@@ -415,6 +416,7 @@ impl Executor {
|
||||
xdv_colors: Default::default(),
|
||||
xdv_annotations: Default::default(),
|
||||
pdf_link_stack: Vec::new(),
|
||||
pdf_active_thread: None,
|
||||
pdf_navigation_geometry: Default::default(),
|
||||
pdf_running_links: true,
|
||||
pdf_interword_spaces: false,
|
||||
@@ -3713,6 +3715,16 @@ impl Executor {
|
||||
previous: Option<&Execution>,
|
||||
) -> Result<Execution, ExecutionFailure> {
|
||||
self.collect_meaning_reads();
|
||||
let result = result.and_then(|()| {
|
||||
if self.shipped_pages > 0 && !self.format_dumped
|
||||
&& let Some(objects) = self.state.pdf_objects.as_deref_mut()
|
||||
{
|
||||
let compress = self.state.pdf_header_options.is_some_and(|header| header.object_compression > 0);
|
||||
objects.finish_threads(compress, self.limits.tokens)
|
||||
.map_err(|message| self.error(message, self.current))?;
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
if let Err(error) = result {
|
||||
use sha2::{Digest, Sha256};
|
||||
let source_versions = self
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -16,9 +16,9 @@ use std::{
|
||||
sync::{Arc, Mutex, OnceLock, Weak},
|
||||
};
|
||||
|
||||
const SCHEMA: u32 = 104;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 70] = [
|
||||
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, SCHEMA,
|
||||
const SCHEMA: u32 = 105;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 71] = [
|
||||
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, SCHEMA,
|
||||
];
|
||||
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
@@ -226,6 +226,9 @@ impl FormatImage {
|
||||
if image.schema < 104 {
|
||||
image.state.install_pdf_annotation_commands();
|
||||
}
|
||||
if image.schema < 105 {
|
||||
image.state.install_pdf_thread_commands();
|
||||
}
|
||||
let pdf_objects = image.state.pdf_objects.as_deref();
|
||||
let mut numbered_images = 0;
|
||||
for (index, saved) in image.pdf_images.iter().enumerate() {
|
||||
|
||||
@@ -47,6 +47,7 @@ pub enum Parameter {
|
||||
Pdfvorigin,
|
||||
Pdflinkmargin,
|
||||
Pdfdestmargin,
|
||||
Pdfthreadmargin,
|
||||
Errorcontextlines,
|
||||
Showboxbreadth,
|
||||
Showboxdepth,
|
||||
@@ -185,6 +186,7 @@ impl Parameter {
|
||||
| Self::Pdfvorigin
|
||||
| Self::Pdflinkmargin
|
||||
| Self::Pdfdestmargin
|
||||
| Self::Pdfthreadmargin
|
||||
| Self::Pdfprotrudechars
|
||||
| Self::Pdfprependkern
|
||||
| Self::Pdfappendkern
|
||||
@@ -252,6 +254,7 @@ impl Parameter {
|
||||
Self::Pdfvorigin,
|
||||
Self::Pdflinkmargin,
|
||||
Self::Pdfdestmargin,
|
||||
Self::Pdfthreadmargin,
|
||||
Self::Errorcontextlines,
|
||||
Self::Showboxbreadth,
|
||||
Self::Showboxdepth,
|
||||
@@ -377,6 +380,7 @@ impl Parameter {
|
||||
| Self::Pdfvorigin
|
||||
| Self::Pdflinkmargin
|
||||
| Self::Pdfdestmargin
|
||||
| Self::Pdfthreadmargin
|
||||
| Self::Delimitershortfall
|
||||
| Self::Predisplaysize
|
||||
| Self::Displaywidth
|
||||
|
||||
@@ -117,6 +117,9 @@ pub enum Primitive {
|
||||
Pdfsave,
|
||||
Pdfstartlink,
|
||||
Pdfannot,
|
||||
Pdfthread,
|
||||
Pdfstartthread,
|
||||
Pdfendthread,
|
||||
Pdfendlink,
|
||||
Pdfrunninglinkoff,
|
||||
Pdfrunninglinkon,
|
||||
@@ -972,6 +975,9 @@ const PDFTEX_ONLY_PRIMITIVES: &[(&str, Primitive)] = {
|
||||
("pdfsave", Pdfsave),
|
||||
("pdfstartlink", Pdfstartlink),
|
||||
("pdfannot", Pdfannot),
|
||||
("pdfthread", Pdfthread),
|
||||
("pdfstartthread", Pdfstartthread),
|
||||
("pdfendthread", Pdfendthread),
|
||||
("pdfendlink", Pdfendlink),
|
||||
("pdfrunninglinkoff", Pdfrunninglinkoff),
|
||||
("pdfrunninglinkon", Pdfrunninglinkon),
|
||||
@@ -1989,6 +1995,18 @@ impl State {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn install_pdf_thread_commands(&mut self) {
|
||||
let enabled = self.input_profile != super::InputProfile::XeTeX;
|
||||
for (name, primitive) in [
|
||||
("pdfthread", Primitive::Pdfthread),
|
||||
("pdfstartthread", Primitive::Pdfstartthread),
|
||||
("pdfendthread", Primitive::Pdfendthread),
|
||||
] {
|
||||
self.update_profile_command(name, Meaning::Primitive(primitive), enabled);
|
||||
}
|
||||
self.update_profile_command("pdfthreadmargin", Meaning::Parameter(Parameter::Pdfthreadmargin), enabled);
|
||||
}
|
||||
|
||||
fn install_pdftex_primitives(&mut self) {
|
||||
let enabled = self.input_profile != super::InputProfile::XeTeX;
|
||||
for &(name, primitive) in PDFTEX_ONLY_PRIMITIVES {
|
||||
|
||||
Reference in new issue
Block a user