use lopdf::{Document, Object, Stream, dictionary}; use std::{fs, path::Path, process::Command}; use texrra_engine::native::{CompileRequest, Entry, NativeCompiler}; use texrra_tex_core::{execution::JobTime, resources::WorldConfig}; fn rotated_document(path: &Path) { let mut pdf = Document::with_version("1.5"); let parents = pdf.new_object_id(); let resources = pdf.add_object(dictionary! {}); let content = [ b"1 0 0 rg 15 22 m 135 22 l 20 70 l h f\n".as_slice(), b"0 0.7 0 rg 80 42 35 27 re f\n".as_slice(), ] .map(|bytes| pdf.add_object(Stream::new(dictionary! {}, bytes.to_vec()))); let page = pdf.add_object(dictionary! { "Type" => "Page", "Parent" => parents, "Rotate" => 90, "TrimBox" => vec![15.into(),22.into(),140.into(),78.into()], "Contents" => content.into_iter().map(Object::Reference).collect::>(), }); pdf.objects.insert( parents, Object::Dictionary(dictionary! { "Type" => "Pages", "Kids" => vec![Object::Reference(page)], "Count" => 1, "Resources" => resources, "MediaBox" => vec![(-20).into(),5.into(),180.into(),105.into()], "CropBox" => vec![10.into(),20.into(),160.into(),95.into()], }), ); let catalog = pdf.add_object(dictionary! {"Type" => "Catalog", "Pages" => parents}); pdf.trailer.set("Root", catalog); pdf.save(path).unwrap(); } #[test] fn rotated_pdf_page_uses_selected_box_real_content_and_source_map() { let root = std::env::temp_dir().join(format!("texrra-pdf-image-{}", std::process::id())); fs::create_dir(&root).unwrap(); let source = root.join("vector.pdf"); rotated_document(&source); let body = r"\catcode123=1 \catcode125=2 \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]} \shipout\box0 \end "; let mut artifacts = Vec::new(); for native in [false, true] { let work = root.join(if native { "native" } else { "reference" }); fs::create_dir(&work).unwrap(); fs::write(work.join("probe.tex"), body).unwrap(); fs::copy(&source, work.join("vector.pdf")).unwrap(); let mut command = if native { let mut command = Command::new(env!("CARGO_BIN_EXE_texrra")); command.args([ "native", "--job", "--initex", "--etex", "--resource-manifest", "resources.json", "--layout-manifest", "layout.json", "probe.tex", "probe.pdf", ]); command } else { let mut command = Command::new("pdftex"); command.args([ "-ini", "-etex", "-interaction=nonstopmode", "-halt-on-error", "probe.tex", ]); command }; let output = command .current_dir(&work) .env("TEXINPUTS", ":") .env("TEXRRA_STATE_DIR", work.join("state")) .output() .unwrap(); assert!( output.status.success(), "{}{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr) ); let log = fs::read_to_string(work.join("probe.log")).unwrap(); let dims = log .split_once("[w=") .unwrap() .1 .split_once("[pages=1]") .unwrap() .0 .to_owned(); let render = Command::new("pdftoppm") .args(["-r", "144", "-gray", "-singlefile", "probe.pdf", "page"]) .current_dir(&work) .output() .unwrap(); assert!( render.status.success(), "{}", String::from_utf8_lossy(&render.stderr) ); artifacts.push((dims, 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(); let image = &layout["pages"][0]["pdf_images"][0]; assert_eq!(image["page"], 1); assert_eq!( layout["pages"][0]["paint_order"][0], serde_json::json!(["pdf_image", 0]) ); let map = &layout["source_map"]["pages"][0]["entries"][0]; assert_eq!(map["kind"]["PdfImage"]["page"], 1); assert_eq!(map["bounds"]["width"], image["width"]); } } assert_eq!(artifacts[0].0, artifacts[1].0); assert!( artifacts[0].1 == artifacts[1].1, "rotated image pixels differ" ); fs::remove_dir_all(root).unwrap(); } #[test] fn embedded_pdf_preview_uses_current_form_and_retains_the_other_page() { let root = std::env::temp_dir().join(format!("texrra-pdf-svg-{}", std::process::id())); fs::create_dir(&root).unwrap(); let vector = root.join("vector.pdf"); rotated_document(&vector); fs::write( root.join("probe.tex"), r"\catcode123=1 \catcode125=2 \pdfoutput=1 \pdfpagewidth=160pt \pdfpageheight=200pt \pdfhorigin=12pt \pdfvorigin=20pt \pdfximage page 1 trimbox {vector.pdf} \shipout\hbox{\pdfsave\pdfsetmatrix{0.75 0 0 0.75} \hbox to0pt{\pdfrefximage\pdflastximage\hss}\pdfrestore} \shipout\vbox{\hrule width12pt height20pt depth0pt}\end ", ) .unwrap(); let mut request = CompileRequest::new( WorldConfig::new(&root), "probe.tex", "probe.pdf", JobTime::new(2026, 11, 18, 800) .unwrap() .with_pdf_clock(0, 0) .unwrap(), ); request.entry = Entry::Initex; request.etex = true; let compiler = NativeCompiler::default(); let first = compiler.compile(request.clone()).unwrap(); let full = first.preview_since(None).unwrap(); assert_eq!(full.page_count, 2); let art = full.pages[0].svg.to_ascii_lowercase(); assert!(art.contains("