use std::borrow::Cow;
use std::cell::OnceCell;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;
use laminapdf_cos::{DirectPath, ObjRef, Object, PathSegment, Result, Warning};
use laminapdf_syntax::text::decode_text_string;
use crate::document::{Document, object_number};
use crate::geometry::UserSpaceRect;
use crate::id::OutlineItemId;
use crate::page::{Pages, number, read_rect};
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum View {
Xyz {
left: Option<f64>,
top: Option<f64>,
zoom: Option<f64>,
},
Fit,
FitH {
top: Option<f64>,
},
FitV {
left: Option<f64>,
},
FitR(UserSpaceRect),
FitB,
FitBH {
top: Option<f64>,
},
FitBV {
left: Option<f64>,
},
Unknown,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Destination {
pub page: Option<crate::id::PageId>,
pub page_index: Option<usize>,
pub view: View,
pub name: Option<Vec<u8>>,
pub raw: Object,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ActionKind {
GoTo,
GoToR,
GoToE,
Launch,
Thread,
Uri,
Named,
JavaScript,
SubmitForm,
ResetForm,
ImportData,
Other(Vec<u8>),
}
impl ActionKind {
#[must_use]
pub fn as_name(&self) -> Cow<'_, str> {
Cow::Borrowed(match self {
Self::GoTo => "GoTo",
Self::GoToR => "GoToR",
Self::GoToE => "GoToE",
Self::Launch => "Launch",
Self::Thread => "Thread",
Self::Uri => "URI",
Self::Named => "Named",
Self::JavaScript => "JavaScript",
Self::SubmitForm => "SubmitForm",
Self::ResetForm => "ResetForm",
Self::ImportData => "ImportData",
Self::Other(name) => return String::from_utf8_lossy(name),
})
}
pub(crate) fn from_name(name: &[u8]) -> Self {
match name {
b"GoTo" => Self::GoTo,
b"GoToR" => Self::GoToR,
b"GoToE" => Self::GoToE,
b"Launch" => Self::Launch,
b"Thread" => Self::Thread,
b"URI" => Self::Uri,
b"Named" => Self::Named,
b"JavaScript" => Self::JavaScript,
b"SubmitForm" => Self::SubmitForm,
b"ResetForm" => Self::ResetForm,
b"ImportData" => Self::ImportData,
other => Self::Other(other.to_vec()),
}
}
}
struct Item {
id: OutlineItemId,
node: Arc<Object>,
depth: usize,
children: Vec<usize>,
}
pub struct Outline<'d> {
doc: &'d Document,
items: Vec<Item>,
roots: Vec<usize>,
by_id: BTreeMap<OutlineItemId, usize>,
pages: PageSource<'d>,
names: OnceCell<BTreeMap<Vec<u8>, Object>>,
}
enum PageSource<'d> {
Borrowed(&'d Pages<'d>),
Lazy(OnceCell<Option<Pages<'d>>>),
}
impl std::fmt::Debug for Outline<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Outline")
.field("items", &self.items.len())
.finish_non_exhaustive()
}
}
struct Pending {
obj: Object,
parent: Option<usize>,
depth: usize,
path: DirectPath,
}
impl<'d> Outline<'d> {
pub(crate) fn new(doc: &'d Document) -> Result<Self> {
Self::build(doc, PageSource::Lazy(OnceCell::new()))
}
pub(crate) fn with_pages(doc: &'d Document, pages: &'d Pages<'d>) -> Result<Self> {
Self::build(doc, PageSource::Borrowed(pages))
}
pub(crate) const fn without_items(doc: &'d Document, pages: &'d Pages<'d>) -> Self {
Self {
doc,
items: Vec::new(),
roots: Vec::new(),
by_id: BTreeMap::new(),
pages: PageSource::Borrowed(pages),
names: OnceCell::new(),
}
}
pub(crate) const fn doc(&self) -> &'d Document {
self.doc
}
fn build(doc: &'d Document, pages: PageSource<'d>) -> Result<Self> {
let mut outline = Self {
doc,
items: Vec::new(),
roots: Vec::new(),
by_id: BTreeMap::new(),
pages,
names: OnceCell::new(),
};
let catalog = doc.catalog()?;
let Some(root_obj) = catalog.get("Outlines") else {
return Ok(outline);
};
let catalog_ref = doc.trailer().get("Root").and_then(Object::as_ref);
let root = doc.read(root_obj)?;
let Some(root) = root.as_deref().and_then(Object::as_dict) else {
if !matches!(root.as_deref(), Some(Object::Null)) {
doc.warn(Warning::InvalidEntry {
object: catalog_ref.map(|r| r.num),
key: "Outlines",
})?;
}
return Ok(outline);
};
let Some(first) = root.get("First") else {
return Ok(outline);
};
let root_path = match (root_obj.as_ref(), catalog_ref) {
(Some(r), _) => DirectPath::new(Some(r), Vec::new()),
(None, Some(catalog)) => DirectPath::new(Some(catalog), vec![PathSegment::Key("Outlines")]),
(None, None) => DirectPath::new(None, vec![PathSegment::Key("Root"), PathSegment::Key("Outlines")]),
};
outline.walk(first.clone(), &root_path, root_obj.as_ref())?;
outline.by_id = outline
.items
.iter()
.enumerate()
.map(|(i, item)| (item.id.clone(), i))
.collect();
Ok(outline)
}
fn walk(&mut self, first: Object, root_path: &DirectPath, root_ref: Option<ObjRef>) -> Result<()> {
let doc = self.doc;
let max_depth = doc.limits().max_tree_depth();
let mut visited: BTreeSet<u32> = root_ref.map(|r| r.num).into_iter().collect();
let mut stack = vec![Pending {
path: child_path(root_path, None, "First"),
obj: first,
parent: None,
depth: 1,
}];
while let Some(Pending {
obj,
parent,
depth,
path,
}) = stack.pop()
{
let skip = || {
doc.warn(Warning::OutlineItemSkipped {
object: object_number(&obj),
})
};
if depth > max_depth {
skip()?;
continue;
}
if let Some(r) = obj.as_ref()
&& !visited.insert(r.num)
{
doc.warn(Warning::OutlineCycle { object: r.num })?;
continue;
}
let Some(node) = doc.read(&obj)? else {
skip()?;
continue;
};
let Some(dict) = node.as_dict() else {
if !matches!(*node, Object::Null) {
skip()?;
}
continue;
};
let id = match obj.as_ref() {
Some(r) => OutlineItemId::object(r),
None => OutlineItemId::path(&path),
};
let index = self.items.len();
if let Some(next) = dict.get("Next") {
stack.push(Pending {
path: child_path(&path, obj.as_ref(), "Next"),
obj: next.clone(),
parent,
depth,
});
}
if let Some(first) = dict.get("First") {
stack.push(Pending {
path: child_path(&path, obj.as_ref(), "First"),
obj: first.clone(),
parent: Some(index),
depth: depth + 1,
});
}
self.items.push(Item {
id,
node: Arc::clone(&node),
depth,
children: Vec::new(),
});
match parent.and_then(|p| self.items.get_mut(p)) {
Some(p) => p.children.push(index),
None => self.roots.push(index),
}
}
Ok(())
}
#[must_use]
pub fn len(&self) -> usize {
self.items.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
pub fn roots(&self) -> impl Iterator<Item = OutlineItem<'_, 'd>> + '_ {
self.roots.iter().map(|&index| OutlineItem { outline: self, index })
}
pub fn iter(&self) -> impl ExactSizeIterator<Item = OutlineItem<'_, 'd>> + '_ {
(0..self.items.len()).map(|index| OutlineItem { outline: self, index })
}
#[must_use]
pub fn by_id(&self, id: &OutlineItemId) -> Option<OutlineItem<'_, 'd>> {
self.by_id.get(id).map(|&index| OutlineItem { outline: self, index })
}
fn pages(&self) -> Result<Option<&Pages<'d>>> {
let cell = match &self.pages {
PageSource::Borrowed(pages) => return Ok(Some(pages)),
PageSource::Lazy(cell) => cell,
};
if let Some(pages) = cell.get() {
return Ok(pages.as_ref());
}
let pages = crate::document::tolerant(self.doc.pages())?;
Ok(cell.get_or_init(|| pages).as_ref())
}
pub fn named_destination(&self, name: &[u8]) -> Result<Option<Destination>> {
if !self.named()?.contains_key(name) {
return Ok(None);
}
self.destination(&Object::Name(laminapdf_syntax::Name::new(name)))
}
fn named(&self) -> Result<&BTreeMap<Vec<u8>, Object>> {
if let Some(names) = self.names.get() {
return Ok(names);
}
let mut names = BTreeMap::new();
if let Some(tree) = self.doc.name_tree("Dests")? {
for (key, value) in tree.entries()? {
names.entry(key).or_insert(value);
}
}
let catalog = self.doc.catalog()?;
if let Some(dests) = catalog.get("Dests") {
let resolved = self.doc.read(dests)?;
if let Some(dests) = resolved.as_deref().and_then(Object::as_dict) {
for (key, value) in dests.iter() {
names.entry(key.as_bytes().to_vec()).or_insert_with(|| value.clone());
}
} else if !matches!(resolved.as_deref(), Some(Object::Null)) {
self.doc.warn(Warning::InvalidEntry {
object: self.doc.trailer().get("Root").and_then(Object::as_ref).map(|r| r.num),
key: "Dests",
})?;
}
}
Ok(self.names.get_or_init(|| names))
}
pub(crate) fn destination(&self, value: &Object) -> Result<Option<Destination>> {
let Some(resolved) = self.doc.read(value)? else {
return Ok(None);
};
let (name, explicit) = match &*resolved {
Object::Name(n) => (Some(n.as_bytes().to_vec()), self.named()?.get(n.as_bytes()).cloned()),
Object::String(s) => (Some(s.bytes.clone()), self.named()?.get(&s.bytes).cloned()),
other => (None, Some(other.clone())),
};
let unresolved = |raw: Object| {
name.clone().map(|name| Destination {
page: None,
page_index: None,
view: View::Unknown,
name: Some(name),
raw,
})
};
let Some(explicit) = explicit else {
return Ok(unresolved(Object::Null));
};
let Some(mut array) = self.doc.read(&explicit)? else {
return Ok(unresolved(explicit));
};
if let Some(d) = array.as_dict().and_then(|d| d.get("D")).cloned() {
match self.doc.read(&d)? {
Some(inner) => array = inner,
None => return Ok(unresolved(explicit)),
}
}
let Some(items) = array.as_array() else {
return Ok(unresolved(explicit));
};
let (page, page_index) = self.target_page(items.first())?;
Ok(Some(Destination {
page,
page_index,
view: self.view(items)?,
name,
raw: (*array).clone(),
}))
}
fn target_page(&self, first: Option<&Object>) -> Result<(Option<crate::id::PageId>, Option<usize>)> {
let Some(pages) = self.pages()? else {
return Ok((None, None));
};
let index = match first {
Some(Object::Ref(r)) => pages.index_of_object(*r),
Some(Object::Integer(i)) => usize::try_from(*i).ok().filter(|i| *i < pages.len()),
_ => None,
};
Ok((index.and_then(|i| pages.get(i)).map(|p| p.id().clone()), index))
}
fn view(&self, items: &[Object]) -> Result<View> {
let doc = self.doc;
let num = |i: usize| -> Result<Option<f64>> {
match items.get(i) {
Some(obj) => number(doc, obj),
None => Ok(None),
}
};
let kind = match items.get(1) {
Some(obj) => doc.read(obj)?,
None => None,
};
let Some(Object::Name(kind)) = kind.as_deref() else {
return Ok(View::Unknown);
};
Ok(match kind.as_bytes() {
b"XYZ" => View::Xyz {
left: num(2)?,
top: num(3)?,
zoom: num(4)?,
},
b"Fit" => View::Fit,
b"FitH" => View::FitH { top: num(2)? },
b"FitV" => View::FitV { left: num(2)? },
b"FitR" => {
let rect = Object::Array(items.get(2..6).unwrap_or_default().to_vec());
match read_rect(doc, &rect)? {
Some(r) => View::FitR(r),
None => View::Unknown,
}
}
b"FitB" => View::FitB,
b"FitBH" => View::FitBH { top: num(2)? },
b"FitBV" => View::FitBV { left: num(2)? },
_ => View::Unknown,
})
}
}
fn child_path(path: &DirectPath, own: Option<ObjRef>, key: &'static str) -> DirectPath {
if let Some(r) = own {
DirectPath::new(Some(r), vec![PathSegment::Key(key)])
} else {
let mut segments = path.segments.clone();
segments.push(PathSegment::Key(key));
DirectPath::new(path.anchor, segments)
}
}
#[derive(Clone, Copy)]
pub struct OutlineItem<'o, 'd> {
outline: &'o Outline<'d>,
index: usize,
}
impl std::fmt::Debug for OutlineItem<'_, '_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OutlineItem")
.field("id", &self.id())
.field("depth", &self.depth())
.finish_non_exhaustive()
}
}
impl<'o, 'd> OutlineItem<'o, 'd> {
fn item(&self) -> Option<&'o Item> {
self.outline.items.get(self.index)
}
fn get(&self, key: &str) -> Option<&'o Object> {
self.item().and_then(|i| i.node.as_dict()).and_then(|d| d.get(key))
}
#[cfg(feature = "edit")]
pub(crate) fn dict(&self) -> Option<&'o laminapdf_cos::Dict> {
self.item().and_then(|i| i.node.as_dict())
}
#[must_use]
pub fn id(&self) -> Option<&'o OutlineItemId> {
self.item().map(|i| &i.id)
}
#[must_use]
pub fn depth(&self) -> usize {
self.item().map_or(0, |i| i.depth)
}
pub fn children(&self) -> impl Iterator<Item = OutlineItem<'o, 'd>> + 'o {
let outline = self.outline;
self.item()
.map(|i| i.children.as_slice())
.unwrap_or_default()
.iter()
.map(move |&index| OutlineItem { outline, index })
}
pub fn title(&self) -> Result<String> {
let Some(title) = self.get("Title") else {
return Ok(String::new());
};
Ok(match self.outline.doc.read(title)?.as_deref() {
Some(Object::String(s)) => decode_text_string(&s.bytes),
_ => String::new(),
})
}
pub fn count(&self) -> Result<Option<i64>> {
let Some(count) = self.get("Count") else {
return Ok(None);
};
Ok(self.outline.doc.read(count)?.and_then(|c| c.as_int()))
}
pub fn is_open(&self) -> Result<bool> {
Ok(self.count()?.is_some_and(|c| c > 0))
}
pub fn action(&self) -> Result<Option<ActionKind>> {
match self.action_dict()?.as_deref().and_then(Object::as_dict) {
Some(action) => self.action_kind(action),
None => Ok(None),
}
}
pub fn target(&self) -> Result<Option<Destination>> {
if let Some(dest) = self.get("Dest")
&& let Some(dest) = self.outline.destination(dest)?
{
return Ok(Some(dest));
}
let Some(action) = self.action_dict()? else {
return Ok(None);
};
let Some(action) = action.as_dict() else {
return Ok(None);
};
if self.action_kind(action)? != Some(ActionKind::GoTo) {
return Ok(None);
}
match action.get("D") {
Some(d) => self.outline.destination(d),
None => Ok(None),
}
}
fn action_dict(&self) -> Result<Option<Arc<Object>>> {
match self.get("A") {
Some(action) => self.outline.doc.read(action),
None => Ok(None),
}
}
fn action_kind(&self, action: &laminapdf_cos::Dict) -> Result<Option<ActionKind>> {
let Some(s) = action.get("S") else {
return Ok(None);
};
Ok(match self.outline.doc.read(s)?.as_deref() {
Some(Object::Name(n)) => Some(ActionKind::from_name(n.as_bytes())),
_ => None,
})
}
}