184 lines
7.0 KiB
Rust
184 lines
7.0 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();
|
|
}
|
|
|
|
#[test]
|
|
fn local_structure_action_references_the_shipped_structure_and_later_page() {
|
|
let root = std::env::temp_dir().join(format!("texrra-pdf-structure-{}", std::process::id()));
|
|
let source = r"\catcode123=1 \catcode125=2
|
|
\def\space{ }
|
|
\pdfoutput=1 \pdfmajorversion=2 \pdfminorversion=0
|
|
\pdfcompresslevel=0 \pdfobjcompresslevel=0
|
|
\pdfobj reserveobjnum \count10=\pdflastobj
|
|
\pdfobj reserveobjnum \count11=\pdflastobj
|
|
\edef\ROOT{\number\count10} \edef\CHILD{\number\count11}
|
|
\immediate\pdfobj useobjnum\count11{<< /Type /StructElem /S /Sect /P \ROOT\space 0 R >>}
|
|
\immediate\pdfobj useobjnum\count10{<< /Type /StructTreeRoot /K [\CHILD\space 0 R] >>}
|
|
\pdfcatalog{/StructTreeRoot \ROOT\space 0 R} openaction goto struct name{anchor} page 2 {/Fit}
|
|
\shipout\hbox{\pdfstartlink goto struct name{anchor} name{anchor}
|
|
\vrule width10pt height6pt \pdfendlink}
|
|
\shipout\hbox{\pdfdest name{anchor} xyz
|
|
\pdfdest struct \count11 name{anchor} fitr width4pt height3pt depth2pt \vrule width10pt height6pt}
|
|
\end";
|
|
let doc = compile(&root, source, true);
|
|
let catalog = doc.catalog().unwrap();
|
|
let pages = doc.get_pages();
|
|
let page = doc.get_object(pages[&1]).unwrap().as_dict().unwrap();
|
|
let annotations = resolve(&doc, page.get(b"Annots").unwrap())
|
|
.as_array()
|
|
.unwrap();
|
|
let link = resolve(&doc, &annotations[0]).as_dict().unwrap();
|
|
let action = resolve(&doc, link.get(b"A").unwrap()).as_dict().unwrap();
|
|
let opener = resolve(&doc, catalog.get(b"OpenAction").unwrap())
|
|
.as_dict()
|
|
.unwrap();
|
|
let page_destination = opener.get(b"D").unwrap().as_array().unwrap();
|
|
assert_eq!(page_destination[0].as_reference().unwrap(), pages[&2]);
|
|
|
|
let structure_destination = opener.get(b"SD").unwrap();
|
|
assert_eq!(
|
|
action.get(b"SD").unwrap().as_reference().unwrap(),
|
|
structure_destination.as_reference().unwrap()
|
|
);
|
|
let destination = resolve(&doc, structure_destination).as_array().unwrap();
|
|
let structure = resolve(&doc, &destination[0]).as_dict().unwrap();
|
|
assert_eq!(
|
|
structure.get(b"Type").unwrap().as_name().unwrap(),
|
|
b"StructElem"
|
|
);
|
|
assert_eq!(
|
|
structure.get(b"P").unwrap().as_reference().unwrap(),
|
|
catalog
|
|
.get(b"StructTreeRoot")
|
|
.unwrap()
|
|
.as_reference()
|
|
.unwrap()
|
|
);
|
|
assert_eq!(destination.len(), 6);
|
|
assert_eq!(destination[1].as_name().unwrap(), b"FitR");
|
|
fs::remove_dir_all(root).unwrap();
|
|
}
|