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

+23 -6
View File
@@ -172,7 +172,7 @@ pub(super) enum BoxPaint<'a> {
RowStart(BoxLayout),
ScopeStart(usize),
ScopeEnd(usize),
PdfLink(&'a OutputNode, BoxLayout),
PdfLink(&'a OutputNode, BoxLayout, Option<usize>),
Output(&'a OutputNode),
}
@@ -180,6 +180,7 @@ struct WalkBudget<'a> {
remaining: usize,
cancellation: Option<&'a Cancellation>,
leader_depth: usize,
next_leader_link: usize,
xdv_scopes: bool,
}
@@ -189,6 +190,7 @@ impl WalkBudget<'_> {
remaining: usize::MAX,
cancellation: None,
leader_depth: 0,
next_leader_link: 0,
xdv_scopes: false,
}
}
@@ -296,6 +298,7 @@ impl NativeBox {
remaining: limit,
cancellation,
leader_depth: 0,
next_leader_link: 0,
xdv_scopes: false,
};
self.walk_at(0, 0, 0, false, false, &mut budget, &mut |x, y, item| {
@@ -890,7 +893,7 @@ impl NativeBox {
return;
}
BoxPaint::RowStart(_) | BoxPaint::ScopeStart(_) | BoxPaint::ScopeEnd(_)
| BoxPaint::PdfLink(_, _) => return,
| BoxPaint::PdfLink(..) => return,
};
if let Some(child) = &b.math_geometry {
for p in &child.items {
@@ -1078,12 +1081,14 @@ impl NativeBox {
+ self.setting.adjustment(g, &mut cumulative, &mut previous);
match &leader.pattern {
LeaderPattern::Box(b) => {
let first_link = budget.next_leader_link;
for pos in leader.positions(
y - self.height,
cursor,
length,
b.height + b.depth,
) {
budget.next_leader_link = first_link;
b.walk_leader(
if rtl { edge - b.shift - b.width } else { x + b.shift },
pos + b.height,
@@ -1118,7 +1123,7 @@ impl NativeBox {
cursor += b.depth;
}
NativeVerticalNode::Output(node) => {
walk_output(node, edge, cursor, container, links, emit);
walk_output(node, edge, cursor, container, links, budget, emit);
if let Some(image) = node.image_box() {
cursor += image.height;
emit(if rtl { edge - image.width } else { x }, cursor, BoxPaint::Box(&image));
@@ -1151,7 +1156,7 @@ impl NativeBox {
let result = self.walk_at(x, y, level, rtl, links, budget, &mut |x, y, paint| {
if !matches!(paint, BoxPaint::Output(o) if matches!(o.operation,
OutputOperation::Open { .. } | OutputOperation::Write { .. } | OutputOperation::Close { .. }))
&& !matches!(paint, BoxPaint::PdfLink(node, _) if matches!(
&& !matches!(paint, BoxPaint::PdfLink(node, ..) if matches!(
node.operation,
OutputOperation::PdfLink(crate::pdf_device::links::LinkNode::Destination { .. }
| crate::pdf_device::links::LinkNode::Annotation { .. }
@@ -1207,7 +1212,9 @@ fn walk_row(
PackedHorizontal::Leaders(leader, width) => {
match &leader.pattern {
LeaderPattern::Box(b) => {
let first_link = budget.next_leader_link;
for pos in leader.positions(row.left, x, *width, b.width) {
budget.next_leader_link = first_link;
b.walk_leader(pos, y + b.shift, row.level + 1, *rtl, links, budget, emit)?;
}
}
@@ -1224,7 +1231,7 @@ fn walk_row(
x += width;
}
PackedHorizontal::Output(node) => {
walk_output(node, x, y, row, links, emit);
walk_output(node, x, y, row, links, budget, emit);
if let Some(image) = node.image_box() {
emit(x, y, BoxPaint::Box(&image));
x += image.width;
@@ -1242,6 +1249,7 @@ fn walk_output(
y: i64,
container: BoxLayout,
links: bool,
budget: &mut WalkBudget<'_>,
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>),
) {
emit(x, y, BoxPaint::Output(node));
@@ -1252,7 +1260,16 @@ fn walk_output(
} else if let OutputOperation::PdfTextSpace(operation) = node.operation {
emit(x, y, BoxPaint::PdfTextSpace(operation));
} else if links && node.is_pdf_link() {
emit(x, y, BoxPaint::PdfLink(node, container));
// Repeated tiles revisit the same node slots. Separate copies of a
// box get separate slots even when Rust shares their backing list.
let leader_link = (budget.leader_depth > 0 && matches!(
node.operation, OutputOperation::PdfLink(crate::pdf_device::links::LinkNode::Start(_))
)).then(|| {
let slot = budget.next_leader_link;
budget.next_leader_link += 1;
slot
});
emit(x, y, BoxPaint::PdfLink(node, container, leader_link));
}
}
pub(super) fn box_paragraph(mut b: NativeBox) -> Paragraph {
@@ -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;