Apply pdfTeX version policy at PDF image declaration

This commit is contained in:
gloamxun committed 2026-11-26 17:19:56 +08:00
1 parent 1fe0ac9dd5
commit 4c532f9396
9 files changed
+204 -50

No files matched your search

@@ -1,5 +1,5 @@
use super::*;
use crate::pdf_device::{PdfDriver, PdfFileOptions, PdfPageData};
use crate::pdf_device::{PdfDriver, PdfFileOptions, PdfHeaderOptions, PdfPageData};
impl Executor {
/// TeX extension commands share the scanner and produce their normal
/// effects in math as well as in vertical and horizontal lists.
@@ -219,10 +219,7 @@ impl Executor {
copy
}
pub(super) fn pdf_page_data(
&mut self,
files: &mut impl SourceProvider,
) -> Result<Option<PdfPageData>, Error> {
pub(super) fn prepare_pdf_output_mode(&mut self) -> Result<i32, Error> {
self.reads.insert(Dependency::PdfFileState);
let mode = self.pdf_output_mode();
if self
@@ -233,39 +230,58 @@ impl Executor {
return self.fail("native pdfoutput cannot change after output has started");
}
self.state.output_mode_set = Some(mode);
if mode <= 0 {
return Ok(None);
}
Ok(mode)
}
/// The first image declaration or PDF writer fixes the header. The
/// coordinate and PK settings are observed separately at device setup.
pub(super) fn prepare_pdf_header(
&mut self,
files: &mut impl SourceProvider,
) -> Result<PdfHeaderOptions, Error> {
self.reads.insert(Dependency::PdfFileState);
if self.state.input_profile() == InputProfile::TeX8Bit {
self.pdf_copy_imported_fonts();
}
let major = self.read_parameter(Parameter::Pdfmajorversion).width();
let minor = self.read_parameter(Parameter::Pdfminorversion).width();
if major < 1 || !(0..=9).contains(&minor) {
return self.fail("invalid native PDF version");
}
if let Some(fixed) = &self.state.pdf_file_options {
if let Some(fixed) = self.state.pdf_header_options {
if fixed.major != major || i32::from(fixed.minor) != minor {
return self.fail("native PDF version cannot change after output has started");
}
} else {
let mut object_compression = self
.read_parameter(Parameter::Pdfobjcompresslevel)
.width()
.clamp(0, 3) as u8;
if major == 1 && minor < 5 && object_compression > 0 {
object_compression = 0;
let text =
"native PDF warning: object streams require PDF 1.5 or greater; disabled"
.to_string();
files
.observe_diagnostic(WriteDestination::TerminalAndLog, &format!("{text}\n"))
.map_err(|e| self.error(e, self.current))?;
self.events.push(Event::Message {
text,
span: self.current,
});
}
return Ok(fixed);
}
if major < 1 || !(0..=9).contains(&minor) {
return self.fail("invalid native PDF version");
}
let mut object_compression = self
.read_parameter(Parameter::Pdfobjcompresslevel)
.width()
.clamp(0, 3) as u8;
if major == 1 && minor < 5 && object_compression > 0 {
object_compression = 0;
let text =
"native PDF warning: object streams require PDF 1.5 or greater; disabled"
.to_string();
files
.observe_diagnostic(WriteDestination::TerminalAndLog, &format!("{text}\n"))
.map_err(|e| self.error(e, self.current))?;
self.events.push(Event::Message { text, span: self.current });
}
let options = PdfHeaderOptions { major, minor: minor as u8, object_compression };
self.state.pdf_header_options = Some(options);
Ok(options)
}
pub(super) fn pdf_page_data(
&mut self,
files: &mut impl SourceProvider,
) -> Result<Option<PdfPageData>, Error> {
if self.prepare_pdf_output_mode()? <= 0 {
return Ok(None);
}
let header = self.prepare_pdf_header(files)?;
if self.state.pdf_file_options.is_none() {
let decimal_digits = self
.read_parameter(Parameter::Pdfdecimaldigits)
.width()
@@ -282,9 +298,9 @@ impl Executor {
self.state.initialize_pdf_pk_resolution(pk_resolution);
}
self.state.pdf_file_options = Some(PdfFileOptions {
major,
minor: minor as u8,
object_compression,
major: header.major,
minor: header.minor,
object_compression: header.object_compression,
decimal_digits,
pk_resolution: pk_resolution.clamp(72, 8000) as u32,
});