From 003d5d8726692fa2fb6dd13558461d2b790df1a4 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Sat, 21 Nov 2026 05:18:07 +0800 Subject: [PATCH] Resolve FitR destinations using shipped box geometry --- crates/texrra-tex-core/src/execution/boxes.rs | 37 ++++++++--- .../src/execution/engine/pdf_links.rs | 65 ++++++++++++------- .../src/execution/format_image.rs | 6 +- .../src/execution/parameters.rs | 3 + crates/texrra-tex-core/src/pdf.rs | 4 ++ .../texrra-tex-core/src/pdf_device/links.rs | 23 ++++++- docs/P2_M1_PROGRESS.md | 20 ++++++ tests/native_pdf_links.rs | 5 +- 8 files changed, 123 insertions(+), 40 deletions(-) diff --git a/crates/texrra-tex-core/src/execution/boxes.rs b/crates/texrra-tex-core/src/execution/boxes.rs index 8c72098..fefa93d 100644 --- a/crates/texrra-tex-core/src/execution/boxes.rs +++ b/crates/texrra-tex-core/src/execution/boxes.rs @@ -151,8 +151,8 @@ pub(super) enum BoxPaint<'a> { PdfColor, PdfGraphics, PdfTextSpace(crate::pdf_device::text_spaces::SpaceOperation), - RowStart(RowLayout), - PdfLink(&'a OutputNode, Option), + RowStart(BoxLayout), + PdfLink(&'a OutputNode, BoxLayout), Output(&'a OutputNode), } @@ -197,7 +197,8 @@ pub(super) struct LeaderEffects { } #[derive(Clone, Copy)] -pub(super) struct RowLayout { +pub(super) struct BoxLayout { + pub horizontal: bool, pub left: i64, pub baseline: i64, pub width: i64, @@ -802,14 +803,30 @@ impl NativeBox { match &self.content { BoxContent::Horizontal(nodes) => { budget.inside_leader(nodes.len())?; - let row = RowLayout { left: x, baseline: y, width: self.width, - height: self.height, depth: self.depth, level }; + let row = BoxLayout { + horizontal: true, + left: x, + baseline: y, + width: self.width, + height: self.height, + depth: self.depth, + level, + }; if links { emit(x, y, BoxPaint::RowStart(row)); } walk_row(&self.packed_horizontal().unwrap(), row, links, budget, emit)?; } BoxContent::Vertical(nodes) => { + let container = BoxLayout { + horizontal: false, + left: x, + baseline: y, + width: self.width, + height: self.height, + depth: self.depth, + level, + }; let mut cursor = y - self.height; let (mut cumulative, mut previous) = (0, 0); for node in nodes { @@ -872,7 +889,7 @@ impl NativeBox { cursor += b.depth; } NativeVerticalNode::Output(node) => { - walk_output(node, x, cursor, None, links, emit); + walk_output(node, x, cursor, container, links, emit); if let Some(image) = node.image_box() { cursor += image.height; emit(x, cursor, BoxPaint::Box(&image)); @@ -922,7 +939,7 @@ pub(super) fn bp(n: i64) -> i64 { } fn walk_row( items: &[PackedHorizontal<'_>], - row: RowLayout, + row: BoxLayout, links: bool, budget: &mut WalkBudget<'_>, emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>), @@ -963,7 +980,7 @@ fn walk_row( x += width; } PackedHorizontal::Output(node) => { - walk_output(node, x, y, Some(row), links, emit); + walk_output(node, x, y, row, links, emit); if let Some(image) = node.image_box() { emit(x, y, BoxPaint::Box(&image)); x += image.width; @@ -979,7 +996,7 @@ fn walk_output( node: &OutputNode, x: i64, y: i64, - row: Option, + container: BoxLayout, links: bool, emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>), ) { @@ -991,7 +1008,7 @@ 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, row)); + emit(x, y, BoxPaint::PdfLink(node, container)); } } 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 6b83ad6..849a5bc 100644 --- a/crates/texrra-tex-core/src/execution/engine/pdf_links.rs +++ b/crates/texrra-tex-core/src/execution/engine/pdf_links.rs @@ -6,7 +6,7 @@ use crate::pdf_device::{ PageDestination, PageLinks, }, }; -use crate::execution::boxes::{BoxPaint, RowLayout}; +use crate::execution::boxes::{BoxPaint, BoxLayout}; #[derive(Clone, Copy)] struct Segment { @@ -51,7 +51,7 @@ impl ActiveLink { fn start_segment( &mut self, output: &mut PageLinks, - row: RowLayout, + row: BoxLayout, point: [i64; 2], transform: (Affine, bool), margin: i64, @@ -102,6 +102,23 @@ impl ActiveLink { } impl Executor { + fn scan_pdf_rule(&mut self, files: &mut impl SourceProvider) -> Result<[Option; 3], Error> { + let mut dimensions = [None; 3]; + loop { + let index = if self.keyword("width", files)? { + 0 + } else if self.keyword("height", files)? { + 1 + } else if self.keyword("depth", files)? { + 2 + } else { + break; + }; + dimensions[index] = Some(self.dimension(false, false, files)?.amount); + } + Ok(dimensions) + } + pub(super) fn scan_pdf_link( &mut self, command: Primitive, @@ -130,19 +147,7 @@ impl Executor { .get_or_insert_with(Default::default) .reserve_link(self.limits.tokens) .map_err(|error| self.error(error, self.current))?; - let (mut width, mut height, mut depth) = (None, None, None); - loop { - let target = if self.keyword("width", files)? { - &mut width - } else if self.keyword("height", files)? { - &mut height - } else if self.keyword("depth", files)? { - &mut depth - } else { - break; - }; - *target = Some(self.dimension(false, false, files)?.amount); - } + let [width, height, depth] = self.scan_pdf_rule(files)?; let attributes = if self.keyword("attr", files)? { self.scan_pdf_tokens(files)? } else { @@ -191,6 +196,8 @@ impl Executor { None }; DestinationView::Xyz(zoom) + } else if self.keyword("fitr", files)? { + DestinationView::FitR(self.scan_pdf_rule(files)?) } else { let mut view = None; for (name, candidate) in [ @@ -237,7 +244,7 @@ impl Executor { b.walk_pdf_links(&mut |x, y, item| { if failure.is_some() { return; } let (x, y) = (x + origin_x, y + origin_y); - let absolute_row = |mut row: RowLayout| { + let absolute_row = |mut row: BoxLayout| { row.left += origin_x; row.baseline += origin_y; row @@ -264,15 +271,16 @@ impl Executor { link.start_segment(&mut output, absolute_row(row), [x, y], transform, margin); } } - BoxPaint::PdfLink(node, row) => { + BoxPaint::PdfLink(node, container) => { + let container = absolute_row(container); self.current = node.span; let OutputOperation::PdfLink(link) = &node.operation else { unreachable!() }; match link { LinkNode::Start(layout) => { - let Some(row) = row.map(absolute_row) else { + if !container.horizontal { failure = Some(self.error("PDF link start arrived outside a horizontal list", node.span)); return; - }; + } let attributes = self.token_list_text(&layout.attributes); let action = match &layout.action { LinkAction::User(tokens) => AnnotationAction::User(self.token_list_text(tokens)), @@ -280,8 +288,8 @@ impl Executor { LinkAction::Catalog(action) => AnnotationAction::Catalog(action.clone()), }; let mut start = ActiveLink { layout: layout.clone(), attributes, - action, level: row.level, segment: None }; - start.start_segment(&mut output, row, [x, y], transform, margin); + action, level: container.level, segment: None }; + start.start_segment(&mut output, container, [x, y], transform, margin); // The scan-time object number belongs only to the // initial annotation, never to a continuation row. output.annotations.last_mut().unwrap().object_number = layout.object_number; @@ -292,7 +300,7 @@ impl Executor { failure = Some(self.error("\\pdfendlink used without \\pdfstartlink", node.span)); return; }; - if row.is_none_or(|r| r.level != link.level) { + if !container.horizontal || container.level != link.level { let result = files.observe_diagnostic(WriteDestination::TerminalAndLog, "pdfTeX warning: pdftex: \\pdfendlink ended up in different nesting level than \\pdfstartlink\n"); if let Err(error) = result { @@ -308,8 +316,19 @@ impl Executor { DestinationId::Name(tokens) => DestinationId::Name(self.token_list_text(tokens)), DestinationId::Number(number) => DestinationId::Number(*number), }; + let view = view.map_rectangle(|[width, height, depth]| { + let margin = i64::from(self.read_parameter(Parameter::Pdfdestmargin).width()); + let top = height.map_or(container.baseline - container.height, |h| y - i64::from(h)); + let right = width.map_or(container.left + container.width, |w| x + i64::from(w)); + let bottom = depth.map_or(container.baseline + container.depth, |d| y + i64::from(d)); + if transform.1 { + link_rect([x, top, right, bottom], transform.0, margin) + } else { + [x - margin, top - margin, right + margin, bottom + margin] + } + }); output.destinations.push(PageDestination { - destination, point: map_point(transform.0, x, y), view: *view, + destination, point: map_point(transform.0, x, y), view, structure: *structure, }); } diff --git a/crates/texrra-tex-core/src/execution/format_image.rs b/crates/texrra-tex-core/src/execution/format_image.rs index 5f5769c..8c4f8bd 100644 --- a/crates/texrra-tex-core/src/execution/format_image.rs +++ b/crates/texrra-tex-core/src/execution/format_image.rs @@ -8,9 +8,9 @@ use std::{ sync::{Arc, Mutex, OnceLock, Weak}, }; -const SCHEMA: u32 = 46; -const COMPATIBLE_SCHEMAS: [u32; 15] = [ - 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, SCHEMA, +const SCHEMA: u32 = 47; +const COMPATIBLE_SCHEMAS: [u32; 16] = [ + 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, SCHEMA, ]; const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION")); diff --git a/crates/texrra-tex-core/src/execution/parameters.rs b/crates/texrra-tex-core/src/execution/parameters.rs index 8c75959..3b578c8 100644 --- a/crates/texrra-tex-core/src/execution/parameters.rs +++ b/crates/texrra-tex-core/src/execution/parameters.rs @@ -35,6 +35,7 @@ pub enum Parameter { Pdfhorigin, Pdfvorigin, Pdflinkmargin, + Pdfdestmargin, Errorcontextlines, Showboxbreadth, Showboxdepth, @@ -152,6 +153,7 @@ impl Parameter { Self::Pdfhorigin, Self::Pdfvorigin, Self::Pdflinkmargin, + Self::Pdfdestmargin, Self::Errorcontextlines, Self::Showboxbreadth, Self::Showboxdepth, @@ -245,6 +247,7 @@ impl Parameter { | Self::Pdfhorigin | Self::Pdfvorigin | Self::Pdflinkmargin + | Self::Pdfdestmargin | Self::Delimitershortfall | Self::Predisplaysize | Self::Displaywidth diff --git a/crates/texrra-tex-core/src/pdf.rs b/crates/texrra-tex-core/src/pdf.rs index 2dec1aa..3bacf4b 100755 --- a/crates/texrra-tex-core/src/pdf.rs +++ b/crates/texrra-tex-core/src/pdf.rs @@ -2512,6 +2512,10 @@ fn write_pdf( DestinationView::FitV => format!("/FitV {x}"), DestinationView::FitBH => format!("/FitBH {y}"), DestinationView::FitBV => format!("/FitBV {x}"), + DestinationView::FitR([left, top, right, bottom]) => { + let coordinates = [left, height - bottom, right, height - top].map(physical); + format!("/FitR {}", coordinates.join(" ")) + } }; if let Some(number) = destination.structure { use std::collections::btree_map::Entry; diff --git a/crates/texrra-tex-core/src/pdf_device/links.rs b/crates/texrra-tex-core/src/pdf_device/links.rs index 5883b81..8af1fc5 100644 --- a/crates/texrra-tex-core/src/pdf_device/links.rs +++ b/crates/texrra-tex-core/src/pdf_device/links.rs @@ -21,7 +21,7 @@ pub struct LinkStart { } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] -pub enum DestinationView { +pub enum DestinationView; 3]> { Xyz(Option), Fit, FitH, @@ -29,6 +29,25 @@ pub enum DestinationView { FitB, FitBH, FitBV, + FitR(Rectangle), +} + +impl DestinationView { + pub(crate) fn map_rectangle( + self, + map: impl FnOnce(Rectangle) -> Output, + ) -> DestinationView { + match self { + Self::Xyz(zoom) => DestinationView::Xyz(zoom), + Self::Fit => DestinationView::Fit, + Self::FitH => DestinationView::FitH, + Self::FitV => DestinationView::FitV, + Self::FitB => DestinationView::FitB, + Self::FitBH => DestinationView::FitBH, + Self::FitBV => DestinationView::FitBV, + Self::FitR(rectangle) => DestinationView::FitR(map(rectangle)), + } + } } #[derive( @@ -73,7 +92,7 @@ pub struct LinkAnnotation { pub struct PageDestination { pub destination: DestinationId, pub point: [i64; 2], - pub view: DestinationView, + pub view: DestinationView<[i64; 4]>, #[serde(default, skip_serializing_if = "Option::is_none")] pub structure: Option, } diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index 9a8d872..755800d 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -1412,6 +1412,26 @@ DVI 的坐标文件在 PDF 驱动报告 DVI 导出限制前完成,原生 DVI - `legacy-link/receipt.json` 为 `eefdee02507881e3901498626f23abf7772e9dad9f1b1e52dcb8a8f5248e7b9e`。 - `related-tests.log` 为 `1c088e4dc5ae755a7f247e5b62be1867f12888b224d455834ddd465b775b62fb`。 +### PDF 矩形目的地 + +普通、数字和结构目的地共用 `FitR` 导出。规则尺寸缺省时读取实际 +横盒或纵盒的包围尺寸。带图形矩阵的矩形按四角变换计算边界,按 +发页时 `\pdfdestmargin` 的局部数值扩边。未变换的有符号尺寸保留 +参考端使用的角点顺序。新尺寸参数和拥有型视图进入 schema47。 +[pdfTeX 的矩形目的地几何](https://github.com/TeX-Live/texlive-source/blob/trunk/texk/web2c/pdftexdir/pdftex.web) + +与 pdfTeX 的五个矩形目的地坐标逐项一致,其中四个实际引用结构元素。 +用例包含明确尺寸、横盒和纵盒缺省尺寸、剪切矩阵、负宽度以及按发页实际 +分组改变边距。两项 PDF 链接测试通过。只增大第二页目的地边距后续编 +复用1156步,实际 PDF 矩形及两页 SVG 和来源映射与独立全量一致。 +两页可见绘制均相同,续编没有重发绘制页面。 + +回执在 `target/p3-pdf-fit-rectangle-native`。 + +- `receipt.json` 为 `aca26459b723fdc8c6001e933b39619b6a886f161760a9f9377c4c1fad292057`。 +- `checkpoint/receipt.json` 为 `9f43c57a440c25faf39f8faa2a1298e45f1e057f481ae818b81a38c60a224e27`。 +- `related-tests.log` 为 `d9bb129201a3f82b4ce125bc31d69a5a9d0b30d69868022ccac05790bf852e45`。 + ### PDF 字面中的矩阵与来源映射 来源映射与 PDF 写入共用设备原点的取整。实际字面中的 `q`、`Q` 和 diff --git a/tests/native_pdf_links.rs b/tests/native_pdf_links.rs index 644d1eb..66d862e 100644 --- a/tests/native_pdf_links.rs +++ b/tests/native_pdf_links.rs @@ -141,7 +141,7 @@ fn local_structure_action_references_the_shipped_structure_and_later_page() { \shipout\hbox{\pdfstartlink goto struct name{anchor} name{anchor} \vrule width10pt height6pt \pdfendlink} \shipout\hbox{\pdfdest name{anchor} xyz - \pdfdest struct \count11 name{anchor} fit \vrule width10pt height6pt} + \pdfdest struct \count11 name{anchor} fitr width4pt height3pt depth2pt \vrule width10pt height6pt} \end"; let doc = compile(&root, source, true); let catalog = doc.catalog().unwrap(); @@ -177,6 +177,7 @@ fn local_structure_action_references_the_shipped_structure_and_later_page() { .as_reference() .unwrap() ); - assert_eq!(destination[1].as_name().unwrap(), b"Fit"); + assert_eq!(destination.len(), 6); + assert_eq!(destination[1].as_name().unwrap(), b"FitR"); fs::remove_dir_all(root).unwrap(); }