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)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user