Build native PDF article threads and bead rings
This commit is contained in:
1 parent
de4bd1fa76
commit
4fb0f2c89b
15 files changed
+528
-28
No files matched your search
@@ -860,3 +860,105 @@ fn direct_annotations_keep_reserved_references_and_copy_geometry() {
|
||||
};
|
||||
assert_eq!(inspect(&native), inspect(&reference));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn article_thread_links_real_beads_between_boxes_and_pages() {
|
||||
let body = r"\pdfpagewidth=200pt \pdfpageheight=200pt \pdfthreadmargin=1pt
|
||||
\def\space{ }
|
||||
\immediate\pdfobj{(Creator)}\edef\author{\the\pdflastobj}
|
||||
\pdfcatalog{} openaction thread num7
|
||||
\shipout\vbox{
|
||||
\vbox{\pdfstartthread width20pt attr{/I << /Title (start) >>} num7
|
||||
\hbox{\vrule width20pt height5pt depth2pt}\kern3pt}
|
||||
\kern6pt
|
||||
\vbox{\hbox{\vrule width21pt height7pt}\kern4pt\pdfendthread\kern11pt}}
|
||||
\pdfpageheight=230pt
|
||||
\shipout\hbox{\pdfsave\pdfsetmatrix{0 1 -1 0}\kern9pt
|
||||
\pdfthread width13pt height6pt depth2pt
|
||||
attr{/I << /Title (final) /Creator \author\space 0 R >>} num7
|
||||
\kern-9pt\pdfrestore\vrule width15pt height5pt}";
|
||||
|
||||
let inspect = |pdf: &Document| {
|
||||
let catalog = pdf.catalog().unwrap();
|
||||
let threads = get(pdf, catalog, b"Threads").as_array().unwrap();
|
||||
let [thread] = threads.as_slice() else {
|
||||
panic!("expected one article thread");
|
||||
};
|
||||
let thread_id = thread.as_reference().unwrap();
|
||||
let thread = pdf.get_dictionary(thread_id).unwrap();
|
||||
let info = get(pdf, thread, b"I").as_dict().unwrap();
|
||||
assert_eq!(get(pdf, info, b"Title").as_str().unwrap(), b"final");
|
||||
assert_eq!(get(pdf, info, b"Creator").as_str().unwrap(), b"Creator");
|
||||
let open = get(pdf, catalog, b"OpenAction").as_dict().unwrap();
|
||||
assert_eq!(get(pdf, open, b"S").as_name().unwrap(), b"Thread");
|
||||
assert_eq!(open.get(b"D").unwrap().as_reference().unwrap(), thread_id);
|
||||
|
||||
let pages: Vec<_> = pdf.get_pages().into_values().collect();
|
||||
let beads: Vec<_> = pages
|
||||
.iter()
|
||||
.map(|page| {
|
||||
get(pdf, pdf.get_dictionary(*page).unwrap(), b"B")
|
||||
.as_array()
|
||||
.unwrap()
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(
|
||||
beads.iter().map(|beads| beads.len()).collect::<Vec<_>>(),
|
||||
[2, 1]
|
||||
);
|
||||
let ring: Vec<_> = beads
|
||||
.iter()
|
||||
.flat_map(|beads| beads.iter())
|
||||
.map(|bead| bead.as_reference().unwrap())
|
||||
.collect();
|
||||
assert_eq!(ring[0], thread.get(b"F").unwrap().as_reference().unwrap());
|
||||
ring.iter()
|
||||
.enumerate()
|
||||
.map(|(index, id)| {
|
||||
let bead = pdf.get_dictionary(*id).unwrap();
|
||||
assert_eq!(
|
||||
bead.get(b"V").unwrap().as_reference().unwrap(),
|
||||
ring[(index + 2) % 3]
|
||||
);
|
||||
assert_eq!(
|
||||
bead.get(b"N").unwrap().as_reference().unwrap(),
|
||||
ring[(index + 1) % 3]
|
||||
);
|
||||
assert_eq!(
|
||||
bead.get(b"P").unwrap().as_reference().unwrap(),
|
||||
pages[usize::from(index == 2)]
|
||||
);
|
||||
if index == 0 {
|
||||
assert_eq!(bead.get(b"T").unwrap().as_reference().unwrap(), thread_id);
|
||||
} else {
|
||||
assert!(bead.get(b"T").is_err());
|
||||
}
|
||||
get(pdf, bead, b"R")
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|coordinate| match coordinate {
|
||||
Object::Integer(value) => *value as f64,
|
||||
Object::Real(value) => f64::from(*value),
|
||||
other => panic!("expected a bead coordinate, found {other:?}"),
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let (reference, native, diagnostics) = pair(body);
|
||||
assert!(diagnostics.is_empty(), "{diagnostics:?}");
|
||||
let expected = inspect(&reference);
|
||||
assert_eq!(inspect(&native), expected);
|
||||
|
||||
// lopdf 0.26 cannot resolve the catalog in pdfTeX's compressed output.
|
||||
let compressed = [r"\pdfminorversion=5 \pdfobjcompresslevel=3 ", body].concat();
|
||||
let (_, native, diagnostics) = pair(&compressed);
|
||||
assert!(diagnostics.is_empty(), "{diagnostics:?}");
|
||||
assert!(native.objects.values().any(|object| {
|
||||
object
|
||||
.as_stream()
|
||||
.is_ok_and(|stream| stream.dict.type_is(b"ObjStm"))
|
||||
}));
|
||||
assert_eq!(inspect(&native), expected);
|
||||
}
|
||||
Reference in new issue
Block a user