Files
texrra-engine/tests/native_pdf_links.rs
T

127 lines
4.6 KiB
Rust

use lopdf::{Document, Object};
use std::{fs, path::Path, process::Command};
fn resolve<'a>(document: &'a Document, object: &'a Object) -> &'a Object {
match object {
Object::Reference(id) => document.get_object(*id).unwrap(),
object => object,
}
}
fn describe(document: &Document) -> (Vec<Vec<(String, String, String)>>, usize, String) {
let ids: Vec<_> = document.get_pages().into_values().collect();
let annotations = ids
.iter()
.map(|id| {
let page = document.get_object(*id).unwrap().as_dict().unwrap();
page.get(b"Annots").map_or_else(
|_| Vec::new(),
|annots| {
resolve(document, annots)
.as_array()
.unwrap()
.iter()
.map(|item| {
let item = resolve(document, item).as_dict().unwrap();
assert_eq!(item.get(b"Type").unwrap().as_name().unwrap(), b"Annot");
assert_eq!(item.get(b"Subtype").unwrap().as_name().unwrap(), b"Link");
let action = resolve(document, item.get(b"A").unwrap())
.as_dict()
.unwrap();
(
format!("{:?}", item.get(b"Rect").unwrap()),
format!("{:?}", item.get(b"Border").unwrap()),
format!("{:?}", action.get(b"D").unwrap()),
)
})
.collect()
},
)
})
.collect();
let catalog = document.catalog().unwrap();
let names = resolve(document, catalog.get(b"Names").unwrap())
.as_dict()
.unwrap();
let destinations = resolve(document, names.get(b"Dests").unwrap())
.as_dict()
.unwrap();
let pairs = destinations.get(b"Names").unwrap().as_array().unwrap();
assert_eq!(pairs.len(), 2);
assert_eq!(pairs[0].as_str().unwrap(), b"target");
let view = resolve(document, &pairs[1]);
let view = match view {
Object::Dictionary(dictionary) => resolve(document, dictionary.get(b"D").unwrap()),
view => view,
}
.as_array()
.unwrap();
let page = ids
.iter()
.position(|id| *id == view[0].as_reference().unwrap())
.unwrap();
(annotations, page, format!("{:?}", &view[1..]))
}
fn compile(root: &Path, source: &str, native: bool) -> Document {
let work = root.join(if native { "native" } else { "reference" });
fs::create_dir_all(&work).unwrap();
fs::write(work.join("probe.tex"), source).unwrap();
let mut command = if native {
let mut command = Command::new(env!("CARGO_BIN_EXE_texrra"));
command.args([
"native",
"--job",
"--initex",
"--etex",
"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.stderr)
);
Document::load(work.join("probe.pdf")).unwrap()
}
#[test]
fn real_pdf_link_fragments_and_named_destination_match_pdftex() {
let root = std::env::temp_dir().join(format!("texrra-pdf-links-{}", std::process::id()));
let source = r"\catcode123=1 \catcode125=2
\pdfoutput=1 \pdfcompresslevel=0 \pdfobjcompresslevel=0
\pdfpagewidth=140pt \pdfpageheight=100pt \pdfhorigin=20pt \pdfvorigin=30pt
\pdflinkmargin=1pt
\shipout\vbox{\hsize=40pt \baselineskip=16pt \lineskiplimit=0pt
\parfillskip=0pt plus1fil \noindent
\pdfstartlink attr{/Border [0 0 0]} goto name{target}
\vrule width10pt height5pt depth2pt
\hfil\penalty-10000\relax
\vrule width10pt height5pt depth2pt\pdfendlink\par}
\shipout\hbox{\pdfdest name{target} xyz \vrule width10pt height5pt depth2pt}
\end";
let expected = describe(&compile(&root, source, false));
assert_eq!(expected.0.iter().map(Vec::len).collect::<Vec<_>>(), [2, 0]);
assert_eq!(expected.1, 1);
assert_eq!(describe(&compile(&root, source, true)), expected);
fs::remove_dir_all(root).unwrap();
}