Resolve named PDF image destinations through the page tree
This commit is contained in:
1 parent
4c532f9396
commit
58012d9397
5 files changed
+196
-17
No files matched your search
@@ -1,5 +1,6 @@
|
||||
//! Native image XObjects and the resource objects reachable from their content.
|
||||
mod bmp;
|
||||
mod destinations;
|
||||
mod jpeg;
|
||||
mod icc;
|
||||
mod imported_fonts;
|
||||
@@ -16,6 +17,11 @@ use std::{
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
pub(crate) enum PageSelection {
|
||||
Number(i32),
|
||||
Named(Vec<u8>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub(crate) enum PageBox {
|
||||
Media,
|
||||
@@ -554,14 +560,17 @@ impl PdfImage {
|
||||
pub(crate) fn read(
|
||||
resource: LoadedResource,
|
||||
doc: Document,
|
||||
page: i32,
|
||||
selection: PageSelection,
|
||||
kind: PageBox,
|
||||
remap_type1: bool,
|
||||
) -> Result<Self, String> {
|
||||
Self::read_page(resource, doc, kind.keys(), false, remap_type1, |pages| {
|
||||
u32::try_from(page)
|
||||
.ok()
|
||||
.filter(|&page| page > 0 && page as usize <= pages)
|
||||
Self::read_page(resource, doc, kind.keys(), false, remap_type1, |doc, pages| {
|
||||
match selection {
|
||||
PageSelection::Number(page) => {
|
||||
u32::try_from(page).map_err(|_| "required page does not exist".into())
|
||||
}
|
||||
PageSelection::Named(name) => destinations::page(doc, pages, &name),
|
||||
}
|
||||
})
|
||||
.map(|(image, _)| image)
|
||||
}
|
||||
@@ -580,14 +589,16 @@ impl PdfImage {
|
||||
b"ArtBox",
|
||||
];
|
||||
let doc = Self::open_pdf(&resource)?;
|
||||
Self::read_page(resource, doc, &keys, true, false, |pages| {
|
||||
let pages = i64::try_from(pages).ok()?;
|
||||
Self::read_page(resource, doc, &keys, true, false, |_, pages| {
|
||||
let pages = i64::try_from(pages.len()).map_err(|_| "required page does not exist")?;
|
||||
let page = match page {
|
||||
0 => 1,
|
||||
page if page < 0 => pages + i64::from(page) + 1,
|
||||
page => i64::from(page),
|
||||
};
|
||||
(pages > 0).then(|| page.clamp(1, pages) as u32)
|
||||
(pages > 0)
|
||||
.then(|| page.clamp(1, pages) as u32)
|
||||
.ok_or_else(|| "required page does not exist".into())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -614,10 +625,10 @@ impl PdfImage {
|
||||
box_keys: &[&[u8]],
|
||||
keep_page_origin: bool,
|
||||
remap_type1: bool,
|
||||
select_page: impl FnOnce(usize) -> Option<u32>,
|
||||
select_page: impl FnOnce(&Document, &BTreeMap<u32, ObjectId>) -> Result<u32, String>,
|
||||
) -> Result<(Self, [f64; 2]), String> {
|
||||
let pages = doc.get_pages();
|
||||
let page = select_page(pages.len()).ok_or("required page does not exist")?;
|
||||
let page = select_page(&doc, &pages)?;
|
||||
let page_id = pages
|
||||
.get(&page)
|
||||
.copied()
|
||||
|
||||
Reference in new issue
Block a user