Match pdfTeX link rectangles in repeated leaders

This commit is contained in:
gloamxun committed 2026-12-01 23:30:43 +08:00
1 parent aed7010cba
commit 5b9eca1d60
6 files changed
+178 -10

No files matched your search

@@ -1,4 +1,5 @@
use super::*;
use std::collections::BTreeMap;
use crate::pdf_device::{
Affine, PdfGraphics,
catalog::{CatalogAction, CatalogTarget, StructureTarget},
@@ -43,7 +44,7 @@ pub(super) struct ActiveThread {
enum LinkPaint {
Graphics,
RowStart(BoxLayout),
Node(BoxLayout),
Node(BoxLayout, Option<usize>),
}
pub(super) struct PdfLinkShipout {
@@ -57,6 +58,7 @@ pub(super) struct PdfLinkShipout {
graphics_index: usize,
output: PageLinks,
direct_annotations: Vec<LinkAnnotation>,
leader_links: BTreeMap<usize, Vec<usize>>,
origin: [i64; 2],
margin: i64,
numbered: bool,
@@ -400,7 +402,9 @@ impl Executor {
BoxPaint::Box(_) => paint_count += 1,
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::PdfLink(_, row, leader_link) => events.push((
(output_count, paint_count), x, y, LinkPaint::Node(row, leader_link),
)),
BoxPaint::PdfGraphics => events.push(((output_count, paint_count), x, y, LinkPaint::Graphics)),
_ => {}
});
@@ -416,6 +420,7 @@ impl Executor {
graphics_index: 0,
output: PageLinks::default(),
direct_annotations: Vec::new(),
leader_links: BTreeMap::new(),
origin: [device.horizontal_origin, device.vertical_origin + b.height],
margin: i64::from(self.read_parameter(Parameter::Pdflinkmargin).width()),
numbered,
@@ -503,7 +508,7 @@ impl PdfLinkShipout {
self.output.annotations.last_mut().unwrap().object_number = number;
}
}
LinkPaint::Node(container) => {
LinkPaint::Node(container, leader_link) => {
let node = outputs[output_index - 1];
let OutputOperation::PdfLink(link) = &node.operation else {
return executor.fail("PDF navigation and output traversal diverged");
@@ -570,6 +575,22 @@ impl PdfLinkShipout {
};
let mut start = ActiveLink { layout: layout.clone(), attributes,
action, level: container.level, segment: None };
if self.numbered && let Some(slot) = leader_link {
let annotations = self.leader_links.entry(slot).or_default();
if let Some(&previous) = annotations.last() {
// pdfTeX stores the rectangle in the same words as
// the declared width, height and depth.
let [left, top, right, _] = self.output.annotations[previous].rect;
let [width, height, depth] = [left, top, right].map(|value| {
let value = value as i32;
(value != -(1 << 30)).then_some(value)
});
start.layout.width = width;
start.layout.height = height;
start.layout.depth = depth;
}
annotations.push(self.output.annotations.len());
}
start.start_segment(&mut self.output, container, [x, y], &mut self.geometry,
self.page_height, self.margin);
self.output.annotations.last_mut().unwrap().object_number = number;
@@ -648,6 +669,14 @@ impl PdfLinkShipout {
if self.numbered {
executor.pdf_navigation_geometry = self.geometry;
}
// Every scheduled object for this node reads its final rectangle.
for annotations in self.leader_links.values() {
let (&last, earlier) = annotations.split_last().unwrap();
let rect = self.output.annotations[last].rect;
for &index in earlier {
self.output.annotations[index].rect = rect;
}
}
if !self.direct_annotations.is_empty() {
self.direct_annotations.append(&mut self.output.annotations);
self.output.annotations = self.direct_annotations;