Resolve FitR destinations using shipped box geometry

This commit is contained in:
gloamxun committed 2026-11-21 05:18:07 +08:00
1 parent 22154b53a6
commit 003d5d8726
8 files changed
+123 -40

No files matched your search

+27 -10
View File
@@ -151,8 +151,8 @@ pub(super) enum BoxPaint<'a> {
PdfColor,
PdfGraphics,
PdfTextSpace(crate::pdf_device::text_spaces::SpaceOperation),
RowStart(RowLayout),
PdfLink(&'a OutputNode, Option<RowLayout>),
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<RowLayout>,
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 {
@@ -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<i32>; 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,
});
}
@@ -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"));
@@ -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