Keep native PDF navigation transforms across page output
This commit is contained in:
1 parent
0e71c15b01
commit
cb8ed90df2
6 files changed
+155
-46
No files matched your search
@@ -490,7 +490,10 @@ impl Executor {
|
||||
}
|
||||
let output_count = outputs.len();
|
||||
let has_link_material = !self.pdf_link_stack.is_empty() || outputs.iter().any(|node| node.is_pdf_link());
|
||||
let mut pdf_link_shipout = (has_link_material && !for_form && device.pdf.is_some())
|
||||
let mut pdf_link_shipout = (!for_form && device.pdf.is_some()
|
||||
&& (has_link_material || outputs.iter().any(|node| {
|
||||
matches!(node.operation, OutputOperation::PdfSetMatrix(_))
|
||||
})))
|
||||
.then(|| self.begin_pdf_link_shipout(b, device));
|
||||
let mut output_positions = if let Some(effects) = &leader_effects {
|
||||
effects.output_positions.clone().into_iter()
|
||||
|
||||
@@ -13,8 +13,7 @@ use crate::execution::boxes::{BoxPaint, BoxLayout};
|
||||
struct Segment {
|
||||
index: usize,
|
||||
rect: [i64; 4],
|
||||
transform: Affine,
|
||||
transformed: bool,
|
||||
transform: Option<Affine>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -36,8 +35,10 @@ pub(super) struct PdfLinkShipout {
|
||||
events: std::iter::Peekable<std::vec::IntoIter<((usize, usize), i64, i64, LinkPaint)>>,
|
||||
output_count: usize,
|
||||
active: Vec<ActiveLink>,
|
||||
transform: (Affine, bool),
|
||||
saved: Vec<(Affine, bool)>,
|
||||
// pdfTeX retains the navigation matrix in PDF coordinates across shipouts.
|
||||
transform: Option<Affine>,
|
||||
saved: Vec<Option<Affine>>,
|
||||
page_height: i64,
|
||||
graphics_index: usize,
|
||||
output: PageLinks,
|
||||
origin: [i64; 2],
|
||||
@@ -45,26 +46,23 @@ pub(super) struct PdfLinkShipout {
|
||||
numbered: bool,
|
||||
}
|
||||
|
||||
fn map_point(matrix: Affine, x: i64, y: i64) -> [i64; 2] {
|
||||
matrix
|
||||
.point(x as f64, y as f64)
|
||||
.map(|value| value.round() as i64)
|
||||
fn map_point(matrix: Affine, page_height: i64, [x, y]: [i64; 2]) -> [i64; 2] {
|
||||
let [x, y] = matrix.point(x as f64, (page_height - y) as f64);
|
||||
// The PDF device rounds transformed positions to scaled TeX integers.
|
||||
[i64::from(x.round() as i32), page_height - i64::from(y.round() as i32)]
|
||||
}
|
||||
|
||||
fn link_rect(rect: [i64; 4], matrix: Affine, margin: i64) -> [i64; 4] {
|
||||
let [left, top, right, bottom] = rect;
|
||||
let corners = [
|
||||
map_point(matrix, left, top),
|
||||
map_point(matrix, right, top),
|
||||
map_point(matrix, left, bottom),
|
||||
map_point(matrix, right, bottom),
|
||||
];
|
||||
[
|
||||
corners.iter().map(|p| p[0]).min().unwrap() - margin,
|
||||
corners.iter().map(|p| p[1]).min().unwrap() - margin,
|
||||
corners.iter().map(|p| p[0]).max().unwrap() + margin,
|
||||
corners.iter().map(|p| p[1]).max().unwrap() + margin,
|
||||
]
|
||||
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();
|
||||
}
|
||||
[left - margin, top - margin, right + margin, bottom + margin]
|
||||
}
|
||||
|
||||
impl ActiveLink {
|
||||
@@ -73,7 +71,8 @@ impl ActiveLink {
|
||||
output: &mut PageLinks,
|
||||
row: BoxLayout,
|
||||
point: [i64; 2],
|
||||
transform: (Affine, bool),
|
||||
transform: Option<Affine>,
|
||||
page_height: i64,
|
||||
margin: i64,
|
||||
) {
|
||||
let [x, y] = point;
|
||||
@@ -92,28 +91,27 @@ impl ActiveLink {
|
||||
self.segment = Some(Segment {
|
||||
index: output.annotations.len(),
|
||||
rect,
|
||||
transform: transform.0,
|
||||
transformed: transform.1,
|
||||
transform,
|
||||
});
|
||||
output.annotations.push(LinkAnnotation {
|
||||
object_number: None,
|
||||
rect: link_rect(rect, transform.0, margin),
|
||||
rect: link_rect(rect, transform, page_height, margin),
|
||||
attributes: self.attributes.clone(),
|
||||
action: self.action.clone(),
|
||||
});
|
||||
}
|
||||
|
||||
fn end_segment(self, output: &mut PageLinks, x: i64, margin: i64) {
|
||||
fn end_segment(self, output: &mut PageLinks, x: i64, page_height: i64, margin: i64) {
|
||||
if let Some(segment) = self.segment
|
||||
&& self.layout.width.is_none()
|
||||
{
|
||||
let painted = &mut output.annotations[segment.index].rect;
|
||||
if segment.transformed {
|
||||
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, margin);
|
||||
*painted = link_rect(rect, segment.transform, page_height, margin);
|
||||
} else {
|
||||
painted[2] = x + margin;
|
||||
}
|
||||
@@ -339,17 +337,19 @@ impl Executor {
|
||||
BoxPaint::PdfGraphics => events.push(((output_count, paint_count), x, y, LinkPaint::Graphics)),
|
||||
_ => {}
|
||||
});
|
||||
let numbered = self.state.input_profile() == InputProfile::TeX8Bit;
|
||||
PdfLinkShipout {
|
||||
events: events.into_iter().peekable(),
|
||||
output_count,
|
||||
active: std::mem::take(&mut self.pdf_link_stack),
|
||||
transform: (Affine::IDENTITY, false),
|
||||
transform: if numbered { self.pdf_navigation_matrix } else { None },
|
||||
saved: Vec::new(),
|
||||
page_height: device.page_height,
|
||||
graphics_index: 0,
|
||||
output: PageLinks::default(),
|
||||
origin: [device.horizontal_origin, device.vertical_origin + b.height],
|
||||
margin: i64::from(self.read_parameter(Parameter::Pdflinkmargin).width()),
|
||||
numbered: self.state.input_profile() == InputProfile::TeX8Bit,
|
||||
numbered,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -382,12 +382,14 @@ impl PdfLinkShipout {
|
||||
if matches!(operation, PdfGraphics::SaveMatrix(_)) {
|
||||
self.saved.push(self.transform);
|
||||
}
|
||||
self.transform = (self.transform.0.concatenate(matrix.about(x as f64, y as f64, true)), true);
|
||||
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));
|
||||
}
|
||||
Some(PdfGraphics::Restore { shift: [dx, dy] }) => {
|
||||
Some(PdfGraphics::Restore { .. }) => {
|
||||
if let Some(previous) = self.saved.pop() {
|
||||
self.transform = (previous.0.concatenate(Affine::translation(*dx as f64, *dy as f64)),
|
||||
previous.1 || *dx != 0 || *dy != 0);
|
||||
// The graphics restore shift compensates the drawing origin.
|
||||
// Navigation still uses TeX's logical position.
|
||||
self.transform = previous;
|
||||
}
|
||||
}
|
||||
Some(PdfGraphics::Literal { .. } | PdfGraphics::TextBoundary) | None => {}
|
||||
@@ -398,7 +400,8 @@ 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, self.margin);
|
||||
link.start_segment(&mut self.output, absolute_row(row), [x, y], self.transform,
|
||||
self.page_height, self.margin);
|
||||
self.output.annotations.last_mut().unwrap().object_number = number;
|
||||
}
|
||||
}
|
||||
@@ -430,7 +433,8 @@ 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, self.margin);
|
||||
start.start_segment(&mut self.output, container, [x, y], self.transform,
|
||||
self.page_height, self.margin);
|
||||
self.output.annotations.last_mut().unwrap().object_number = number;
|
||||
self.active.push(start);
|
||||
}
|
||||
@@ -443,7 +447,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.margin);
|
||||
link.end_segment(&mut self.output, x, self.page_height, self.margin);
|
||||
}
|
||||
LinkNode::Running(enabled) => executor.pdf_running_links = *enabled,
|
||||
LinkNode::Destination { destination, view, structure } => {
|
||||
@@ -457,20 +461,27 @@ impl PdfLinkShipout {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let transform = self.transform;
|
||||
let point = self.transform.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 margin = i64::from(executor.read_parameter(Parameter::Pdfdestmargin).width());
|
||||
point.map(|coordinate| coordinate - margin)
|
||||
} else {
|
||||
point
|
||||
}
|
||||
});
|
||||
let view = view.map_rectangle(|[width, height, depth]| {
|
||||
let margin = i64::from(executor.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]
|
||||
}
|
||||
link_rect([x, top, right, bottom], self.transform, self.page_height, margin)
|
||||
});
|
||||
self.output.destinations.push(PageDestination {
|
||||
object_number, destination, point: map_point(transform.0, x, y), view,
|
||||
object_number, destination, point, view,
|
||||
structure: *structure,
|
||||
});
|
||||
}
|
||||
@@ -496,6 +507,9 @@ impl PdfLinkShipout {
|
||||
link.segment = None;
|
||||
}
|
||||
executor.pdf_link_stack = self.active;
|
||||
if self.numbered {
|
||||
executor.pdf_navigation_matrix = self.transform;
|
||||
}
|
||||
Ok(self.output)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user