Expand native PDF image bounding-box queries

This commit is contained in:
gloamxun committed 2026-11-28 16:20:17 +08:00
1 parent 4fb0f2c89b
commit 2fa11fb971
8 files changed
+141 -31

No files matched your search

@@ -1032,6 +1032,7 @@ impl Executor {
)?;
}
}
Pdfximagebbox => self.expand_pdf_image_bbox(token, files)?,
Pdfstrcmp => {
let left = self.scan_expanded_group(files)?;
let left = self.token_list_text(&left);
@@ -660,6 +660,40 @@ impl Executor {
}));
}
fn registered_pdf_image(&mut self, number: i32) -> Option<&RegisteredImage> {
self.reads.insert(Dependency::PdfImages);
let slot = if self.state.input_profile() == InputProfile::TeX8Bit {
self.reads.insert(Dependency::PdfObjects);
u32::try_from(number).ok()
.and_then(|number| self.state.pdf_objects.as_deref()?.image_slot(number))
} else {
None
};
if let Some(slot) = slot {
return self.pdf_images.get(slot - 1);
}
// Existing format images keep the positional numbers embedded in their macros.
let index = usize::try_from(number).ok()?.checked_sub(1)?;
self.pdf_images.get(index).filter(|registered| registered.image.pdf_object_number.is_none())
}
pub(super) fn expand_pdf_image_bbox(
&mut self, token: &Token, files: &mut impl SourceProvider,
) -> Result<(), Error> {
let number = self.integer(files)?;
let Some(image) = self.registered_pdf_image(number) else {
return self.fail(format!("cannot find referenced image object {number}"));
};
let bbox = image.image.image.pdftex_bbox.unwrap_or([0; 4]);
let coordinate = self.integer(files)?;
let index = match coordinate {
1..=4 => coordinate as usize - 1,
_ => return self.fail(format!("invalid PDF image bounding box coordinate {coordinate}")),
};
let value = super::super::registers::print_scaled_wide(bbox[index]);
self.print_tokens(&format!("{value}pt"), token)
}
pub(super) fn reference_pdf_image(
&mut self,
token: &Token,
@@ -667,23 +701,7 @@ impl Executor {
) -> Result<(), Error> {
self.check_pdf_image()?;
let id = self.integer(files)?;
self.reads.insert(Dependency::PdfImages);
let slot = if self.state.input_profile() == InputProfile::TeX8Bit {
self.reads.insert(Dependency::PdfObjects);
u32::try_from(id).ok()
.and_then(|number| self.state.pdf_objects.as_deref()?.image_slot(number))
} else {
None
};
let image = slot.or_else(|| {
// Existing format images keep the positional numbers embedded in their macros.
let id = usize::try_from(id).ok()?;
self.pdf_images.get(id.checked_sub(1)?)
.filter(|registered| registered.image.pdf_object_number.is_none())
.map(|_| id)
})
.and_then(|id| self.pdf_images.get(id - 1))
.map(|registered| registered.image.clone())
let image = self.registered_pdf_image(id).map(|registered| registered.image.clone())
.ok_or_else(|| self.error(format!("cannot find referenced object {id}"), token.span))?;
self.defer_image(image, token)
}
@@ -16,9 +16,9 @@ use std::{
sync::{Arc, Mutex, OnceLock, Weak},
};
const SCHEMA: u32 = 105;
const COMPATIBLE_SCHEMAS: [u32; 71] = [
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, 103, 104, SCHEMA,
const SCHEMA: u32 = 106;
const COMPATIBLE_SCHEMAS: [u32; 72] = [
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, 103, 104, 105, SCHEMA,
];
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
@@ -229,6 +229,9 @@ impl FormatImage {
if image.schema < 105 {
image.state.install_pdf_thread_commands();
}
if image.schema < 106 {
image.state.install_pdf_image_query_command();
}
let pdf_objects = image.state.pdf_objects.as_deref();
let mut numbered_images = 0;
for (index, saved) in image.pdf_images.iter().enumerate() {
@@ -102,6 +102,7 @@ pub enum Primitive {
Pdfximage,
Pdfrefximage,
Pdflastximage,
Pdfximagebbox,
Pdflastximagepages,
Pdflastximagecolordepth,
Jobname,
@@ -531,6 +532,7 @@ impl Primitive {
| XeTeXfeaturename
| XeTeXselectorname
| Pdfstrcmp
| Pdfximagebbox
| Pdfmatch
| Pdflastmatch
| Pdfuniformdeviate
@@ -949,6 +951,7 @@ const PDFTEX_ONLY_PRIMITIVES: &[(&str, Primitive)] = {
("pdfximage", Pdfximage),
("pdfrefximage", Pdfrefximage),
("pdflastximage", Pdflastximage),
("pdfximagebbox", Pdfximagebbox),
("pdflastximagepages", Pdflastximagepages),
("pdflastximagecolordepth", Pdflastximagecolordepth),
("pdfcreationdate", Pdfcreationdate),
@@ -2007,6 +2010,11 @@ impl State {
self.update_profile_command("pdfthreadmargin", Meaning::Parameter(Parameter::Pdfthreadmargin), enabled);
}
pub(super) fn install_pdf_image_query_command(&mut self) {
let enabled = self.input_profile != super::InputProfile::XeTeX;
self.update_profile_command("pdfximagebbox", Meaning::Primitive(Primitive::Pdfximagebbox), enabled);
}
fn install_pdftex_primitives(&mut self) {
let enabled = self.input_profile != super::InputProfile::XeTeX;
for &(name, primitive) in PDFTEX_ONLY_PRIMITIVES {
+15 -3
View File
@@ -52,6 +52,8 @@ pub struct PdfImage {
extent: [u64; 2],
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) restored_pdf_origin: Option<[i64; 2]>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) pdftex_bbox: Option<[i64; 4]>,
objects: Vec<Vec<Part>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) color_space: Option<i32>,
@@ -423,6 +425,7 @@ impl PdfImage {
natural: self.natural,
extent: self.extent,
restored_pdf_origin: self.restored_pdf_origin,
pdftex_bbox: self.pdftex_bbox,
objects: Vec::new(),
color_space: self.color_space,
imported_fonts: Vec::new(),
@@ -565,6 +568,7 @@ impl PdfImage {
natural,
extent: extent.map(f64::to_bits),
restored_pdf_origin: None,
pdftex_bbox: None,
objects,
color_space,
imported_fonts: Vec::new(),
@@ -617,6 +621,7 @@ impl PdfImage {
natural: natural(bp),
extent: bp.map(f64::to_bits),
restored_pdf_origin: None,
pdftex_bbox: None,
objects: vec![vec![part]],
color_space: None,
imported_fonts: Vec::new(),
@@ -825,12 +830,18 @@ impl PdfImage {
}
Ok(value as i64)
};
let source_sp = |bp: f64| {
(f64::from(bp as f32) * 6_578_176.0 / 100.0).round()
};
let pdftex_bbox = pdftex.then(|| {
// pdfTeX rounds the origin and unrotated extents separately.
let [x, y, w, h] = [left, bottom, right - left, top - bottom]
.map(|value| source_sp(value) as i64);
[x, y, x.saturating_add(w), y.saturating_add(h)]
});
// pdfTeX reopens the PDF with unrotated dimensions for the outer
// placement. The actual page rotation still belongs to the Form.
let (natural, restored_pdf_origin) = if restored {
let source_sp = |bp: f64| {
(f64::from(bp as f32) * 6_578_176.0 / 100.0).round()
};
let [width, height] = [source_sp(right - left), source_sp(top - bottom)];
if ![width, height].into_iter().all(|n| (1.0..=1073741823.0).contains(&n)) {
return Err("invalid image dimensions".into());
@@ -910,6 +921,7 @@ impl PdfImage {
natural,
extent: [width.to_bits(), height.to_bits()],
restored_pdf_origin,
pdftex_bbox,
objects: graph.bodies,
color_space: None,
imported_fonts,
@@ -152,6 +152,7 @@ pub(super) fn read(
device_cid_cff_fonts: Vec::new(),
pdftex_source: None,
restored_pdf_origin: None,
pdftex_bbox: None,
},
warning,
))