diff --git a/crates/texrra-tex-core/src/execution/boxes.rs b/crates/texrra-tex-core/src/execution/boxes.rs index a12bc10..7c3be35 100644 --- a/crates/texrra-tex-core/src/execution/boxes.rs +++ b/crates/texrra-tex-core/src/execution/boxes.rs @@ -172,7 +172,7 @@ pub(super) enum BoxPaint<'a> { RowStart(BoxLayout), ScopeStart(usize), ScopeEnd(usize), - PdfLink(&'a OutputNode, BoxLayout), + PdfLink(&'a OutputNode, BoxLayout, Option), 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 { diff --git a/crates/texrra-tex-core/src/execution/engine/pdf_links.rs b/crates/texrra-tex-core/src/execution/engine/pdf_links.rs index cc17e5e..2a2d836 100644 --- a/crates/texrra-tex-core/src/execution/engine/pdf_links.rs +++ b/crates/texrra-tex-core/src/execution/engine/pdf_links.rs @@ -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), } pub(super) struct PdfLinkShipout { @@ -57,6 +58,7 @@ pub(super) struct PdfLinkShipout { graphics_index: usize, output: PageLinks, direct_annotations: Vec, + leader_links: BTreeMap>, 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; diff --git a/crates/texrra-tex-core/tests/execution_leaders.rs b/crates/texrra-tex-core/tests/execution_leaders.rs index b28745f..4ba138c 100644 --- a/crates/texrra-tex-core/tests/execution_leaders.rs +++ b/crates/texrra-tex-core/tests/execution_leaders.rs @@ -1,4 +1,4 @@ -//! Reference material copied from the independent pdfTeX DVI run. +//! Leader placement and repeated shipout effects against pdfTeX fixtures. use serde_json::Value; use std::{fs, io::Write, path::PathBuf}; use texrra_tex_core::{execution::*, math_engine_tex::MathItem}; @@ -176,3 +176,67 @@ fn repeated_device_effects_follow_the_paint_order_and_obey_placement_budget() { ); } } + +#[test] +fn repeated_pdf_link_nodes_keep_their_final_rectangle() { + let execution = Executor::new( + State::initex() + .with_etex() + .with_input_profile(InputProfile::TeX8Bit), + Limits::default(), + ) + .with_native_pages() + .run( + SourceFile { + name: "links.tex".into(), + data: include_bytes!("fixtures/leaders/links.tex").to_vec(), + }, + &mut NoFiles, + ) + .unwrap(); + let pdf = texrra_tex_core::pdf::shipout_lists_to_native_pdf( + &execution.shipout_display_lists().unwrap(), + execution.state.pdf_final_options(), + ) + .unwrap(); + let document = lopdf::Document::load_mem(&pdf).unwrap(); + + // PDF /Rect arrays from pdfTeX 1.40.29, in thousandths of a PDF point. + let expected: Vec> = serde_json::from_str(include_str!( + "fixtures/leaders/reference-link-rectangles.json" + )) + .unwrap(); + let pages = document.get_pages(); + assert_eq!(pages.len(), expected.len()); + let mut objects = std::collections::BTreeSet::new(); + for ((page, id), expected) in pages.into_iter().zip(expected) { + let annotations = document + .get_dictionary(id) + .unwrap() + .get(b"Annots") + .unwrap() + .as_array() + .unwrap(); + let actual = annotations + .iter() + .map(|object| { + let id = object.as_reference().unwrap(); + assert!(objects.insert(id), "duplicate annotation object on page {page}"); + let rect = document + .get_dictionary(id) + .unwrap() + .get(b"Rect") + .unwrap() + .as_array() + .unwrap(); + assert_eq!(rect.len(), 4); + std::array::from_fn(|i| match rect[i] { + lopdf::Object::Integer(value) => value * 1000, + lopdf::Object::Real(value) => (value * 1000.0).round() as i64, + ref value => panic!("unexpected rectangle coordinate {value:?}"), + }) + }) + .collect::>(); + assert_eq!(actual, expected, "page {page}"); + } +} diff --git a/crates/texrra-tex-core/tests/fixtures/leaders/links.tex b/crates/texrra-tex-core/tests/fixtures/leaders/links.tex new file mode 100644 index 0000000..3b20b6f --- /dev/null +++ b/crates/texrra-tex-core/tests/fixtures/leaders/links.tex @@ -0,0 +1,11 @@ +\pdfoutput=1\pdfcompresslevel=0\pdfobjcompresslevel=0\pdfpkresolution=600 +\catcode123=1\catcode125=2\pdflinkmargin=0pt +\def\tile{\hbox to2pt{\pdfstartlink width1pt height2pt depth0pt user{/Subtype /Link /A << /S /URI /URI (https://example.invalid/) >>}\vrule width1pt height2pt\pdfendlink\hss}} +\setbox5=\tile +\shipout\hbox{\cleaders\copy5\hskip6pt} +\shipout\hbox{\copy5\kern2pt\copy5} +\shipout\hbox{\cleaders\hbox to18pt{\copy5\kern1pt\leaders\copy5\hskip1pt\copy5% + \kern1pt\leaders\copy5\hskip4pt\kern1pt\leaders\copy5\hskip4pt\hss}\hskip37pt} +\def\runtile{\hbox to2pt{\pdfstartlink height2pt depth0pt user{/Subtype /Link /A << /S /URI /URI (https://example.invalid/) >>}\vrule width1pt height2pt\pdfendlink\hss}} +\shipout\vbox{\cleaders\vbox{\hbox{\cleaders\runtile\hskip6pt}}\vskip4pt} +\end diff --git a/crates/texrra-tex-core/tests/fixtures/leaders/reference-link-rectangles.json b/crates/texrra-tex-core/tests/fixtures/leaders/reference-link-rectangles.json new file mode 100644 index 0000000..17b8a7b --- /dev/null +++ b/crates/texrra-tex-core/tests/fixtures/leaders/reference-link-rectangles.json @@ -0,0 +1,31 @@ +[ + [ + [75985, -73993, 149978, 73993], + [75985, -73993, 149978, 73993], + [75985, -73993, 149978, 73993] + ], + [ + [72000, 72000, 72996, 73993], + [75985, 72000, 76981, 73993] + ], + [ + [90431, -1494, 162929, 144000], + [94416, -5480, 170899, 144000], + [98401, -9465, 178869, 144000], + [104379, -116832, 206765, 144000], + [104379, -116832, 206765, 144000], + [90431, -1494, 162929, 144000], + [94416, -5480, 170899, 144000], + [98401, -9465, 178869, 144000], + [104379, -116832, 206765, 144000], + [104379, -116832, 206765, 144000] + ], + [ + [75985, -73993, 149978, 144000], + [75985, -73993, 149978, 144000], + [75985, -73993, 149978, 144000], + [75985, -73993, 149978, 144000], + [75985, -73993, 149978, 144000], + [75985, -73993, 149978, 144000] + ] +] diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index 703fd93..acd9db6 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -18,6 +18,22 @@ | W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;原样 12 页 NeurIPS 的中部编辑与恢复已验证前后缀复用、仅更新一页,且与独立全量一致。全部副作用/交叉编辑、旧来源与正式性能门禁仍待闭合 | | W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 | +## 2026-12-01 引导盒重复绘制的 PDF 链接 + +pdfTeX 把链接的声明尺寸和绘制后的矩形存入同一节点。引导盒的 +每次平铺再次访问这个节点,并用上次写入的值计算当前矩形。 +同一节点安排的注释对象最终读取该节点的最后一个矩形。原生在 +发页遍历内保留平铺节点的身份,同时按原有规则为每次出现创建 +注释。普通盒副本和同一平铺图案内的不同链接保留各自的身份。 + +以 pdfTeX 1.40.29 的实跑 PDF 为参考,四页注释数分别为 3、2、10、6, +全部 21 个注释的矩形逐项一致。样例覆盖普通盒副本、平铺内多个链接、 +放不下的图案、不同对齐位置与水平和垂直嵌套,以及宽度自动延伸的 +链接。旧原生在第 1 页给三个注释分别写入不同矩形,复现日志与参考 +PDF 保存在 `target/repeated-leader-links/`。四项引导盒回归通过。 +参考 PDF 的 SHA-256 为 +`a33ca51a7c6c179e91f5edb6699b378f6adaf73c81f8594a0713af10bcf854ce`。 + ## 2026-12-01 拆分后缀会合的内核开销 比较换算后的执行状态时,仅复制需要迁移来源的字段。命令表缓存