Recalculate native running links from the current PDF geometry
This commit is contained in:
1 parent
cb8ed90df2
commit
975dbc8060
4 files changed
+139
-52
No files matched your search
@@ -230,7 +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_navigation_matrix: Option<crate::pdf_device::Affine>,
|
||||
pdf_navigation_geometry: pdf_links::NavigationGeometry,
|
||||
pdf_running_links: bool,
|
||||
pdf_interword_spaces: bool,
|
||||
pdf_space_font: Option<crate::pdf_device::text_spaces::SpaceFontRequest>,
|
||||
@@ -415,7 +415,7 @@ impl Executor {
|
||||
xdv_colors: Default::default(),
|
||||
xdv_annotations: Default::default(),
|
||||
pdf_link_stack: Vec::new(),
|
||||
pdf_navigation_matrix: None,
|
||||
pdf_navigation_geometry: Default::default(),
|
||||
pdf_running_links: true,
|
||||
pdf_interword_spaces: false,
|
||||
pdf_space_font: None,
|
||||
|
||||
@@ -9,11 +9,12 @@ use crate::pdf_device::{
|
||||
};
|
||||
use crate::execution::boxes::{BoxPaint, BoxLayout};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Segment {
|
||||
index: usize,
|
||||
rect: [i64; 4],
|
||||
transform: Option<Affine>,
|
||||
#[derive(Clone, Copy, Default)]
|
||||
pub(super) struct NavigationGeometry {
|
||||
// Both values use PDF coordinates and survive page boundaries.
|
||||
matrix: Option<Affine>,
|
||||
// Last untransformed rectangle as left, bottom, right and top.
|
||||
last_pdf_rect: [i64; 4],
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -22,7 +23,7 @@ pub(super) struct ActiveLink {
|
||||
attributes: String,
|
||||
action: AnnotationAction,
|
||||
level: usize,
|
||||
segment: Option<Segment>,
|
||||
segment: Option<usize>,
|
||||
}
|
||||
|
||||
enum LinkPaint {
|
||||
@@ -35,8 +36,7 @@ pub(super) struct PdfLinkShipout {
|
||||
events: std::iter::Peekable<std::vec::IntoIter<((usize, usize), i64, i64, LinkPaint)>>,
|
||||
output_count: usize,
|
||||
active: Vec<ActiveLink>,
|
||||
// pdfTeX retains the navigation matrix in PDF coordinates across shipouts.
|
||||
transform: Option<Affine>,
|
||||
geometry: NavigationGeometry,
|
||||
saved: Vec<Option<Affine>>,
|
||||
page_height: i64,
|
||||
graphics_index: usize,
|
||||
@@ -52,17 +52,31 @@ fn map_point(matrix: Affine, page_height: i64, [x, y]: [i64; 2]) -> [i64; 2] {
|
||||
[i64::from(x.round() as i32), page_height - i64::from(y.round() as i32)]
|
||||
}
|
||||
|
||||
fn link_rect(rect: [i64; 4], matrix: Option<Affine>, page_height: i64, margin: i64) -> [i64; 4] {
|
||||
let [mut left, mut top, mut right, mut bottom] = rect;
|
||||
if let Some(matrix) = matrix {
|
||||
let corners = [[left, top], [right, top], [left, bottom], [right, bottom]]
|
||||
.map(|point| map_point(matrix, page_height, point));
|
||||
left = corners.iter().map(|p| p[0]).min().unwrap();
|
||||
top = corners.iter().map(|p| p[1]).min().unwrap();
|
||||
right = corners.iter().map(|p| p[0]).max().unwrap();
|
||||
bottom = corners.iter().map(|p| p[1]).max().unwrap();
|
||||
impl NavigationGeometry {
|
||||
fn rectangle(&mut self, rect: [i64; 4], page_height: i64, margin: i64) -> [i64; 4] {
|
||||
let [mut left, mut top, mut right, mut bottom] = rect;
|
||||
if let Some(matrix) = self.matrix {
|
||||
self.last_pdf_rect = [left, page_height - bottom, right, page_height - top];
|
||||
let corners = [[left, top], [right, top], [left, bottom], [right, bottom]]
|
||||
.map(|point| map_point(matrix, page_height, point));
|
||||
left = corners.iter().map(|p| p[0]).min().unwrap();
|
||||
top = corners.iter().map(|p| p[1]).min().unwrap();
|
||||
right = corners.iter().map(|p| p[0]).max().unwrap();
|
||||
bottom = corners.iter().map(|p| p[1]).max().unwrap();
|
||||
}
|
||||
[left - margin, top - margin, right + margin, bottom + margin]
|
||||
}
|
||||
|
||||
fn end_running_link(&mut self, rect: &mut [i64; 4], x: i64, page_height: i64, margin: i64) {
|
||||
let right = x + margin;
|
||||
if self.matrix.is_some() {
|
||||
// pdfTeX recalculates the device's latest rectangle with the current matrix.
|
||||
let [left, bottom, _, top] = self.last_pdf_rect;
|
||||
*rect = self.rectangle([left, page_height - top, right, page_height - bottom], page_height, margin);
|
||||
} else {
|
||||
rect[2] = right;
|
||||
}
|
||||
}
|
||||
[left - margin, top - margin, right + margin, bottom + margin]
|
||||
}
|
||||
|
||||
impl ActiveLink {
|
||||
@@ -71,7 +85,7 @@ impl ActiveLink {
|
||||
output: &mut PageLinks,
|
||||
row: BoxLayout,
|
||||
point: [i64; 2],
|
||||
transform: Option<Affine>,
|
||||
geometry: &mut NavigationGeometry,
|
||||
page_height: i64,
|
||||
margin: i64,
|
||||
) {
|
||||
@@ -88,33 +102,22 @@ impl ActiveLink {
|
||||
.depth
|
||||
.map_or(row.baseline + row.depth, |d| y + i64::from(d)),
|
||||
];
|
||||
self.segment = Some(Segment {
|
||||
index: output.annotations.len(),
|
||||
rect,
|
||||
transform,
|
||||
});
|
||||
self.segment = Some(output.annotations.len());
|
||||
output.annotations.push(LinkAnnotation {
|
||||
object_number: None,
|
||||
rect: link_rect(rect, transform, page_height, margin),
|
||||
rect: geometry.rectangle(rect, page_height, margin),
|
||||
attributes: self.attributes.clone(),
|
||||
action: self.action.clone(),
|
||||
});
|
||||
}
|
||||
|
||||
fn end_segment(self, output: &mut PageLinks, x: i64, page_height: i64, margin: i64) {
|
||||
if let Some(segment) = self.segment
|
||||
fn end_segment(
|
||||
self, output: &mut PageLinks, x: i64, geometry: &mut NavigationGeometry, page_height: i64, margin: i64,
|
||||
) {
|
||||
if let Some(index) = self.segment
|
||||
&& self.layout.width.is_none()
|
||||
{
|
||||
let painted = &mut output.annotations[segment.index].rect;
|
||||
if segment.transform.is_some() {
|
||||
let mut rect = segment.rect;
|
||||
// The device extends the running right edge before recalculating
|
||||
// the transformed rectangle, then applies the annotation margins.
|
||||
rect[2] = x + margin;
|
||||
*painted = link_rect(rect, segment.transform, page_height, margin);
|
||||
} else {
|
||||
painted[2] = x + margin;
|
||||
}
|
||||
geometry.end_running_link(&mut output.annotations[index].rect, x, page_height, margin);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -342,7 +345,7 @@ impl Executor {
|
||||
events: events.into_iter().peekable(),
|
||||
output_count,
|
||||
active: std::mem::take(&mut self.pdf_link_stack),
|
||||
transform: if numbered { self.pdf_navigation_matrix } else { None },
|
||||
geometry: if numbered { self.pdf_navigation_geometry } else { NavigationGeometry::default() },
|
||||
saved: Vec::new(),
|
||||
page_height: device.page_height,
|
||||
graphics_index: 0,
|
||||
@@ -376,20 +379,21 @@ impl PdfLinkShipout {
|
||||
executor.error("missing PDF transform at link shipout", executor.current)
|
||||
})?;
|
||||
self.graphics_index += 1;
|
||||
let matrix = &mut self.geometry.matrix;
|
||||
match entry {
|
||||
Some(PdfGraphics::Save) => self.saved.push(self.transform),
|
||||
Some(operation @ (PdfGraphics::Matrix(matrix) | PdfGraphics::SaveMatrix(matrix))) => {
|
||||
Some(PdfGraphics::Save) => self.saved.push(*matrix),
|
||||
Some(operation @ (PdfGraphics::Matrix(incoming) | PdfGraphics::SaveMatrix(incoming))) => {
|
||||
if matches!(operation, PdfGraphics::SaveMatrix(_)) {
|
||||
self.saved.push(self.transform);
|
||||
self.saved.push(*matrix);
|
||||
}
|
||||
let incoming = matrix.about(x as f64, (self.page_height - y) as f64, false);
|
||||
self.transform = Some(self.transform.unwrap_or(Affine::IDENTITY).concatenate(incoming));
|
||||
let incoming = incoming.about(x as f64, (self.page_height - y) as f64, false);
|
||||
*matrix = Some(matrix.unwrap_or(Affine::IDENTITY).concatenate(incoming));
|
||||
}
|
||||
Some(PdfGraphics::Restore { .. }) => {
|
||||
if let Some(previous) = self.saved.pop() {
|
||||
// The graphics restore shift compensates the drawing origin.
|
||||
// Navigation still uses TeX's logical position.
|
||||
self.transform = previous;
|
||||
*matrix = previous;
|
||||
}
|
||||
}
|
||||
Some(PdfGraphics::Literal { .. } | PdfGraphics::TextBoundary) | None => {}
|
||||
@@ -400,7 +404,7 @@ impl PdfLinkShipout {
|
||||
link.level == row.level && link.layout.width.is_none()
|
||||
}) {
|
||||
let number = if self.numbered { Some(executor.register_pdf_link(None)?) } else { None };
|
||||
link.start_segment(&mut self.output, absolute_row(row), [x, y], self.transform,
|
||||
link.start_segment(&mut self.output, absolute_row(row), [x, y], &mut self.geometry,
|
||||
self.page_height, self.margin);
|
||||
self.output.annotations.last_mut().unwrap().object_number = number;
|
||||
}
|
||||
@@ -433,7 +437,7 @@ impl PdfLinkShipout {
|
||||
};
|
||||
let mut start = ActiveLink { layout: layout.clone(), attributes,
|
||||
action, level: container.level, segment: None };
|
||||
start.start_segment(&mut self.output, container, [x, y], self.transform,
|
||||
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;
|
||||
self.active.push(start);
|
||||
@@ -447,7 +451,7 @@ impl PdfLinkShipout {
|
||||
"pdfTeX warning: pdftex: \\pdfendlink ended up in different nesting level than \\pdfstartlink\n")
|
||||
.map_err(|error| executor.error(error, node.span))?;
|
||||
}
|
||||
link.end_segment(&mut self.output, x, self.page_height, self.margin);
|
||||
link.end_segment(&mut self.output, x, &mut self.geometry, self.page_height, self.margin);
|
||||
}
|
||||
LinkNode::Running(enabled) => executor.pdf_running_links = *enabled,
|
||||
LinkNode::Destination { destination, view, structure } => {
|
||||
@@ -461,12 +465,14 @@ impl PdfLinkShipout {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let point = self.transform.map_or([x, y], |matrix| {
|
||||
let point = self.geometry.matrix.map_or([x, y], |matrix| {
|
||||
let point = map_point(matrix, self.page_height, [x, y]);
|
||||
if matches!(view, DestinationView::Xyz(_) | DestinationView::FitH
|
||||
| DestinationView::FitBH | DestinationView::FitV | DestinationView::FitBV)
|
||||
{
|
||||
// Point views have no explicit width, height or depth.
|
||||
let pdf_y = self.page_height - y;
|
||||
self.geometry.last_pdf_rect = [x, pdf_y, x, pdf_y];
|
||||
let margin = i64::from(executor.read_parameter(Parameter::Pdfdestmargin).width());
|
||||
point.map(|coordinate| coordinate - margin)
|
||||
} else {
|
||||
@@ -478,7 +484,7 @@ impl PdfLinkShipout {
|
||||
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));
|
||||
link_rect([x, top, right, bottom], self.transform, self.page_height, margin)
|
||||
self.geometry.rectangle([x, top, right, bottom], self.page_height, margin)
|
||||
});
|
||||
self.output.destinations.push(PageDestination {
|
||||
object_number, destination, point, view,
|
||||
@@ -508,7 +514,7 @@ impl PdfLinkShipout {
|
||||
}
|
||||
executor.pdf_link_stack = self.active;
|
||||
if self.numbered {
|
||||
executor.pdf_navigation_matrix = self.transform;
|
||||
executor.pdf_navigation_geometry = self.geometry;
|
||||
}
|
||||
Ok(self.output)
|
||||
}
|
||||
|
||||
@@ -750,3 +750,50 @@ fn pdf_navigation_uses_the_logical_matrix_across_shipouts() {
|
||||
assert_eq!(expected.1.len(), 2);
|
||||
assert_eq!(navigation(&native), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn running_links_use_the_current_matrix_and_last_device_rectangle() {
|
||||
let (reference, native, diagnostics) = pair(
|
||||
r"\pdfdecimaldigits=4 \pdfpagewidth=200pt \pdfpageheight=200pt
|
||||
\pdflinkmargin=1.25pt \pdfdestmargin=2pt
|
||||
\shipout\hbox{\pdfdest name{jump} fit
|
||||
\pdfsave\pdfsetmatrix{0 1 -1 0}
|
||||
\pdfstartlink height3pt depth2pt goto name{jump}\vrule width8pt height3pt\kern-8pt
|
||||
\pdfrestore\kern6pt\pdfendlink\vrule width10pt height6pt}
|
||||
\shipout\hbox{\pdfsave\pdfsetmatrix{2 0 0 1}
|
||||
\pdfstartlink height6pt depth1pt goto name{jump}
|
||||
\kern3pt\pdfdest name{inside} fitr width4pt height9pt depth3pt
|
||||
\kern7pt\pdfendlink
|
||||
\pdfrestore\vrule width10pt height6pt}
|
||||
\shipout\hbox{\pdfsave\pdfsetmatrix{1 0 .5 1}
|
||||
\pdfstartlink height3pt depth1pt goto name{jump}
|
||||
\kern2pt\pdfstartlink width5pt height8pt depth2pt goto name{inside}
|
||||
\kern4pt\pdfendlink\kern3pt\pdfendlink
|
||||
\pdfrestore\vrule width10pt height6pt}
|
||||
\shipout\hbox{\pdfsave\pdfsetmatrix{0 1 -1 0}
|
||||
\kern5pt\pdfdest name{history} fitr width6pt height12pt depth4pt\kern-5pt
|
||||
\pdfrestore\vrule width10pt height6pt}
|
||||
\pdfpageheight=230pt
|
||||
\shipout\hbox{\pdfstartlink height3pt depth2pt goto name{jump}
|
||||
\pdfsave\pdfsetmatrix{1 0 .5 1}
|
||||
\kern9pt\pdfendlink\kern-9pt
|
||||
\pdfrestore\vrule width10pt height6pt}",
|
||||
);
|
||||
assert!(diagnostics.is_empty(), "{diagnostics:?}");
|
||||
|
||||
let page_rectangles = |doc: &Document| {
|
||||
doc.get_pages().values().map(|id| {
|
||||
let page = doc.get_dictionary(*id).unwrap();
|
||||
if page.get(b"Annots").is_err() {
|
||||
return Vec::new();
|
||||
}
|
||||
get(doc, page, b"Annots").as_array().unwrap().iter().map(|annotation| {
|
||||
let dict = doc.get_dictionary(annotation.as_reference().unwrap()).unwrap();
|
||||
comparable(get(doc, dict, b"Rect"))
|
||||
}).collect::<Vec<_>>()
|
||||
}).collect::<Vec<_>>()
|
||||
};
|
||||
let expected = page_rectangles(&reference);
|
||||
assert_eq!(expected.iter().map(Vec::len).collect::<Vec<_>>(), [1, 1, 2, 0, 1]);
|
||||
assert_eq!(page_rectangles(&native), expected);
|
||||
}
|
||||
@@ -18,6 +18,40 @@
|
||||
| W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;完整副作用/恢复契约、旧来源和性能仍缺 |
|
||||
| W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 |
|
||||
|
||||
## 2026-11-28 自动宽度 PDF 链接的收尾矩形
|
||||
|
||||
自动宽度链接在结束节点读取当前变换。活动矩阵下,结束处的横坐标
|
||||
加链接边距后用作逻辑右边,和最近一次矩形的其余边界一起变换,
|
||||
再应用注释边距。没有活动矩阵时只更新已经生成的注释右边。
|
||||
最近一次矩形独立保存在作业状态中。它使用 PDF 轴向,在出页和
|
||||
执行检查点之间保留。链接、续行、FitR 和活动矩阵下的点目标
|
||||
更新同一状态。
|
||||
[pdfTeX 的自动宽度链接结尾](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftex.web#L36533-L36559)
|
||||
· [最近矩形的变换](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/utils.c#L1478-L1538)
|
||||
|
||||
实际 pdfTeX 对照覆盖结束前恢复矩阵、链接内的 FitR、嵌套链接、
|
||||
不同页高间保留矩形和首次变换的初态。六页原语对照的六个注释
|
||||
矩形与参考一致。实际 article、graphicx 和 hyperref 的一页对照中,
|
||||
两个链接矩形、URL、49 个字形和 aux 与参考一致。两端使用
|
||||
相同的类、包和驱动字节。全部七页含注释的 144 DPI 灰度光栅
|
||||
逐像素一致。发布构建及 25 项定向回归通过。
|
||||
|
||||
三页编辑会话从第一页保留活动变换用过的逻辑矩形,第二页的链接
|
||||
结束时读取它。两轮往返编辑各复用 1,244 步,只更新第二页。
|
||||
自动宽度注释、PDF、页面、SVG、来源映射、日志和辅助文件与独立
|
||||
全量一致。恢复时实际矩形的高度与第一页的来源边界一致。
|
||||
|
||||
原样 Springer Nature 的既有 table.3 坐标差沿用前次证据。单独用
|
||||
原样 hyperref 的 TextField 检查了较新 schema100 格式,参考和原生
|
||||
均生成一页 AcroForm,首轮灰度光栅一致。更早 schema79 在加载该
|
||||
项目时缺少 \pdfxform。另一个直接注释用例中,参考的 \pdflastannot
|
||||
为 1 并写出 /Subtype /Text 和实际矩形,当前原生在 \pdfannot 处
|
||||
停止。该用例与日志在 target/p3-pdf-running-link-geometry/next-annotation,
|
||||
可从 PDF 的编号、出页几何和导出路径继续实现。
|
||||
|
||||
回执在 target/p3-pdf-running-link-geometry,receipt.json 的 SHA-256
|
||||
为 7907588f867c24a08b47ca3106b8a4a3ea8bf0bebbfc034c5b0628f9df790a81。
|
||||
|
||||
## 2026-11-28 PDF 导航变换与跨页保留
|
||||
|
||||
pdfTeX 模式在 PDF 坐标系计算目标和链接的逻辑变换。矩阵在同一
|
||||
|
||||
Reference in new issue
Block a user