Discard duplicate destinations during native PDF execution

This commit is contained in:
gloamxun committed 2026-11-28 14:45:52 +08:00
1 parent ba548ef05d
commit 0e71c15b01
9 files changed
+152 -7

No files matched your search

@@ -217,6 +217,7 @@ impl Executor {
};
DestinationView::Xyz(zoom)
} else if self.keyword("fitr", files)? {
self.optional_space(files)?;
DestinationView::FitR(self.scan_pdf_rule(files)?)
} else {
let mut view = None;
@@ -235,6 +236,18 @@ impl Executor {
}
view.ok_or_else(|| self.error("expected PDF destination view", span))?
};
if !matches!(view, DestinationView::FitR(_)) {
self.optional_space(files)?;
}
if self.state.input_profile() == InputProfile::TeX8Bit {
self.reads.insert(Dependency::PdfObjects);
let id = self.pdf_destination_id(&destination);
if self.state.pdf_objects.as_ref().is_some_and(|objects| {
objects.destination_is_defined(id.clone(), structure.is_some())
}) {
return self.warn_pdf_duplicate_destination(&id, span, files);
}
}
LinkNode::Destination { destination, view, structure }
}
_ => unreachable!(),
@@ -245,6 +258,28 @@ impl Executor {
})
}
fn warn_pdf_duplicate_destination(
&mut self,
id: &DestinationId<String>,
span: Span,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
if self.read_parameter(Parameter::Pdfsuppresswarningdupdest).width() > 0 {
return Ok(());
}
let id = crate::pdf::destination_identifier(id);
let text = format!(
"pdfTeX warning (ext4): destination with the same identifier ({id}) has been already used, duplicate ignored"
);
let context = self.diagnostic_context();
let output = format!("{text}\n{}", context.text);
self.reserve_diagnostic(output.len(), &context)?;
files.observe_diagnostic(WriteDestination::TerminalAndLog, &output)
.map_err(|error| self.error(error, span))?;
self.events.push(Event::Message { text, span });
Ok(())
}
pub(super) fn register_pdf_link(&mut self, number: Option<u32>) -> Result<u32, Error> {
self.reads.insert(Dependency::PdfObjects);
self.state.pdf_objects.get_or_insert_with(Default::default)
@@ -414,7 +449,11 @@ impl PdfLinkShipout {
LinkNode::Destination { destination, view, structure } => {
let destination = executor.pdf_destination_id(destination);
let object_number = if self.numbered {
executor.register_pdf_destination(destination.clone(), structure.is_some())?
let Some(number) = executor.register_pdf_destination(destination.clone(), structure.is_some())? else {
executor.warn_pdf_duplicate_destination(&destination, node.span, files)?;
continue;
};
Some(number)
} else {
None
};
@@ -16,9 +16,9 @@ use std::{
sync::{Arc, Mutex, OnceLock, Weak},
};
const SCHEMA: u32 = 102;
const COMPATIBLE_SCHEMAS: [u32; 68] = [
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, SCHEMA,
const SCHEMA: u32 = 103;
const COMPATIBLE_SCHEMAS: [u32; 69] = [
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, SCHEMA,
];
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
@@ -220,6 +220,9 @@ impl FormatImage {
if image.schema < 102 {
image.state.install_primitive_commands();
}
if image.schema < 103 {
image.state.restore_legacy_pdf_parameters(&[Parameter::Pdfsuppresswarningdupdest]);
}
let pdf_objects = image.state.pdf_objects.as_deref();
let mut numbered_images = 0;
for (index, saved) in image.pdf_images.iter().enumerate() {
@@ -16,6 +16,7 @@ pub enum Parameter {
Pdfinclusioncopyfonts,
Pdfgentounicode,
Pdfsuppresswarningdupmap,
Pdfsuppresswarningdupdest,
Pdfinfoomitdate,
Pdfomitcharset,
Pdfomitinfodict,
@@ -170,6 +171,7 @@ impl Parameter {
| Self::Pdfinclusioncopyfonts
| Self::Pdfgentounicode
| Self::Pdfsuppresswarningdupmap
| Self::Pdfsuppresswarningdupdest
| Self::Pdfinfoomitdate
| Self::Pdfomitcharset
| Self::Pdfomitinfodict
@@ -219,6 +221,7 @@ impl Parameter {
Self::Pdfinclusioncopyfonts,
Self::Pdfgentounicode,
Self::Pdfsuppresswarningdupmap,
Self::Pdfsuppresswarningdupdest,
Self::Pdfinfoomitdate,
Self::Pdfomitcharset,
Self::Pdfomitinfodict,
+1 -1
View File
@@ -2054,7 +2054,7 @@ fn write_destination(objects: &mut Vec<Vec<u8>>, slot: Option<usize>, body: Stri
}
}
fn destination_identifier(id: &DestinationId<String>) -> String {
pub(crate) fn destination_identifier(id: &DestinationId<String>) -> String {
match id {
DestinationId::Name(name) => format!("name{{{name}}}"),
DestinationId::Number(number) => format!("num{number}"),
@@ -404,6 +404,12 @@ impl PdfObjects {
Ok(number)
}
pub(crate) fn destination_is_defined(&self, id: DestinationId<String>, structure: bool) -> bool {
self.destination_numbers
.get(&(id, structure))
.is_some_and(|(_, defined)| *defined)
}
pub(crate) fn destination_number(
&mut self, id: DestinationId<String>, structure: bool, define: bool, limit: usize,
) -> Result<Option<u32>, String> {
@@ -662,3 +662,38 @@ fn outline_and_open_action_share_a_missing_numeric_destination() {
}
assert_eq!(messages.iter().filter(|text| text.contains("num23")).count(), 1);
}
#[test]
fn duplicate_destinations_are_discarded_after_the_first_definition() {
use texrra_tex_core::execution::Event;
let source = format!(
r"{SETUP}
\setbox0=\hbox{{\kern5pt\pdfdest name{{same}} xyz\kern3pt\pdfdest name{{same}} fit}}
\setbox1=\hbox{{\kern3pt\pdfdest num7 xyz\kern4pt\pdfdest num7 fith}}
\shipout\vbox{{\box0\box1}}
\setbox0=\hbox{{\kern1pt\pdfdest name{{same}} fit
\ifnum\lastnodetype=12 \else\errmessage{{duplicate named node retained}}\fi}}
{{\pdfsuppresswarningdupdest=1
\setbox1=\hbox{{\kern1pt\pdfdest num7 fit
\ifnum\lastnodetype=12 \else\errmessage{{duplicate numbered node retained}}\fi}}}}
\shipout\hbox{{\pdfdest name{{new}} fit\pdfdest name{{new}} xyz}}
\end"
);
let execution = run_native(&source).unwrap();
let warnings = execution.events.iter().filter_map(|event| match event {
Event::Message { text, .. } if text.contains("duplicate ignored") => Some(text.as_str()),
_ => None,
}).collect::<Vec<_>>();
assert_eq!(
warnings,
["name{same}", "num7", "name{same}", "name{new}"].map(|id| format!(
"pdfTeX warning (ext4): destination with the same identifier ({id}) has been already used, duplicate ignored"
))
);
let pages = execution.shipout_display_lists().unwrap();
assert_eq!(pages[0].pdf_links.destinations.len(), 2);
assert_eq!(pages[1].pdf_links.destinations.len(), 1);
assert!(export_native(&execution).diagnostics.is_empty());
}