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

+39 -8
View File
@@ -177,12 +177,14 @@ fn rotated_pdf_page_uses_selected_box_real_content_and_source_map() {
fs::create_dir(&root).unwrap();
let source = root.join("vector.pdf");
rotated_document(&source);
let body = r"\catcode123=1 \catcode125=2
let body = r"\catcode123=1 \catcode125=2 \catcode35=6
\pdfoutput=1 \pdfminorversion=5 \pdfobjcompresslevel=0
\pdfpagewidth=200pt \pdfpageheight=220pt \pdfhorigin=8pt \pdfvorigin=12pt
\pdfximage page 1 trimbox {vector.pdf}
\setbox0=\hbox{\pdfrefximage\pdflastximage}
\message{[w=\number\wd0][h=\number\ht0][pages=\the\pdflastximagepages]}
\def\bound#1{\pdfximagebbox\pdflastximage#1}
\immediate\write16{BBOX=\bound1,\bound2,\bound3,\bound4}
\shipout\box0
\end
";
@@ -249,7 +251,12 @@ fn rotated_pdf_page_uses_selected_box_real_content_and_source_map() {
"{}",
String::from_utf8_lossy(&render.stderr)
);
artifacts.push((dims, fs::read(work.join("page.pgm")).unwrap()));
let bounds = log
.lines()
.find(|line| line.starts_with("BBOX="))
.unwrap()
.to_owned();
artifacts.push((dims, bounds, fs::read(work.join("page.pgm")).unwrap()));
if native {
let layout: serde_json::Value =
serde_json::from_slice(&fs::read(work.join("layout.json")).unwrap()).unwrap();
@@ -265,8 +272,9 @@ fn rotated_pdf_page_uses_selected_box_real_content_and_source_map() {
}
}
assert_eq!(artifacts[0].0, artifacts[1].0);
assert_eq!(artifacts[0].1, artifacts[1].1);
assert!(
artifacts[0].1 == artifacts[1].1,
artifacts[0].2 == artifacts[1].2,
"rotated image pixels differ"
);
fs::remove_dir_all(root).unwrap();
@@ -533,7 +541,10 @@ fn png_project_edit_updates_only_the_page_using_the_image() {
#[test]
fn format_image_resource_edits_invalidate_checkpoints_and_update_the_picture_page() {
use texrra_engine::native::dump_format;
use texrra_tex_core::{execution::Cancellation, render::DisplayItem};
use texrra_tex_core::{
execution::Cancellation,
render::{DisplayItem, DisplayList},
};
let root = std::env::temp_dir().join(format!("texrra-format-image-{}", std::process::id()));
fs::create_dir(&root).unwrap();
@@ -548,6 +559,7 @@ fn format_image_resource_edits_invalidate_checkpoints_and_update_the_picture_pag
r"\catcode123=1 \catcode125=2
\pdfoutput=1 \pdfminorversion=5
\pdfximage width40pt trimbox {picture.pdf}
\edef\figureobj{\the\pdflastximage}
\setbox0=\hbox{\pdfrefximage\pdflastximage}
\dump
",
@@ -566,10 +578,17 @@ fn format_image_resource_edits_invalidate_checkpoints_and_update_the_picture_pag
";
let padding = format!("{}\n", "\\relax ".repeat(32)).repeat(48);
let entry = format!(
"{prefix}{padding}\\shipout\\copy0\n\\input tail.tex\n\\shipout\\hbox{{\\vrule width\\tailwidth pt height4pt}}\n\\end\n"
"{prefix}{padding}\\shipout\\vbox{{\\copy0\\hrule width\\pdfximagebbox\\numexpr\\figureobj\\relax 1 height1pt}}\n\\input tail.tex\n\\shipout\\hbox{{\\vrule width\\tailwidth pt height4pt}}\n\\end\n"
);
let original = fs::read(&figure).unwrap();
let mut edited = Document::load_mem(&original).unwrap();
let page = edited.get_pages()[&1];
edited
.get_object_mut(page)
.unwrap()
.as_dict_mut()
.unwrap()
.set("TrimBox", vec![19.into(), 22.into(), 144.into(), 78.into()]);
let mut changed = false;
for object in edited.objects.values_mut() {
let Ok(stream) = object.as_stream_mut() else {
@@ -581,8 +600,8 @@ fn format_image_resource_edits_invalidate_checkpoints_and_update_the_picture_pag
}
}
assert!(changed);
let mut recolored = Vec::new();
edited.save_to(&mut recolored).unwrap();
let mut edited_pdf = Vec::new();
edited.save_to(&mut edited_pdf).unwrap();
let files = [
(seed.clone(), fs::read(&seed).unwrap().into()),
@@ -619,7 +638,7 @@ fn format_image_resource_edits_invalidate_checkpoints_and_update_the_picture_pag
.compile(request.clone().with_project(snapshot.clone()))
.unwrap();
assert!(second.entry_checkpoint_work.reused_steps > 0);
let edits = [(figure.clone(), Some(recolored.into()))].into();
let edits = [(figure.clone(), Some(edited_pdf.into()))].into();
let snapshot = snapshot.with_file_changes(3, edits).unwrap();
let revised = request.with_project(snapshot);
let third = compiler.compile(revised.clone()).unwrap();
@@ -635,6 +654,18 @@ fn format_image_resource_edits_invalidate_checkpoints_and_update_the_picture_pag
third.entry_checkpoint_work
);
let queried_width = |page: &DisplayList| {
page.items
.iter()
.find_map(|item| match item {
DisplayItem::Rule { width, .. } => Some(*width),
_ => None,
})
.unwrap()
};
assert_eq!(queried_width(&second.pages[1]), 986_726);
assert_eq!(queried_width(&third.pages[1]), 1_249_853);
let image_page = &third.pages[1];
let image = image_page
.items