use laminapdf_cos::{CosMut, Dict, ObjRef, Object, Result};
use crate::document::Document;
use crate::edit::{put, set_catalog_entry};
use crate::id::OutlineItemId;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SiblingPosition {
First,
Last,
Before(OutlineItemId),
After(OutlineItemId),
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum OutlineTreeError {
#[error("the outline has no item with this ID")]
UnknownItem(OutlineItemId),
#[error("the position names an item that is not a child of the given parent")]
NotASibling(OutlineItemId),
#[error("an outline item can't be moved inside itself")]
IntoItself(OutlineItemId),
}
impl OutlineTreeError {
#[must_use]
pub const fn item(&self) -> &OutlineItemId {
match self {
Self::UnknownItem(id) | Self::NotASibling(id) | Self::IntoItself(id) => id,
}
}
}
#[derive(Debug, Clone)]
enum Slot {
Indirect(ObjRef),
Direct(Dict),
}
#[derive(Debug, Clone)]
struct Node {
id: OutlineItemId,
slot: Slot,
parent: Option<usize>,
children: Vec<usize>,
open: bool,
gone: bool,
}
#[derive(Debug, Clone)]
pub struct OutlineTree {
root: Option<Slot>,
nodes: Vec<Node>,
roots: Vec<usize>,
repaired: bool,
}
impl OutlineTree {
pub fn read(doc: &Document) -> Result<Self> {
let catalog = doc.catalog()?;
let root = match catalog.get("Outlines") {
Some(Object::Ref(r)) => doc
.read(&Object::Ref(*r))?
.filter(|o| o.as_dict().is_some())
.map(|_| Slot::Indirect(*r)),
Some(Object::Dict(d)) => Some(Slot::Direct(d.clone())),
_ => None,
};
let outline = doc.outline()?;
let mut tree = Self {
root,
nodes: Vec::with_capacity(outline.len()),
roots: Vec::new(),
repaired: false,
};
let mut stack: Vec<(Option<usize>, crate::OutlineItem<'_, '_>)> = outline.roots().map(|i| (None, i)).collect();
stack.reverse();
while let Some((parent, item)) = stack.pop() {
let Some(id) = item.id().cloned() else { continue };
let slot = match id.as_lamina_id().obj_ref() {
Some(r) => Slot::Indirect(r),
None => Slot::Direct(item.dict().cloned().unwrap_or_default()),
};
let index = tree.nodes.len();
tree.nodes.push(Node {
id,
slot,
parent,
children: Vec::new(),
open: item.is_open()?,
gone: false,
});
match parent.and_then(|p| tree.nodes.get_mut(p)) {
Some(p) => p.children.push(index),
None => tree.roots.push(index),
}
let mut children: Vec<_> = item.children().map(|c| (Some(index), c)).collect();
children.reverse();
stack.extend(children);
}
tree.repaired = tree.nodes.iter().any(|n| matches!(n.slot, Slot::Direct(_)))
|| matches!(tree.root, Some(Slot::Direct(_)))
|| tree.links_differ(doc)?;
Ok(tree)
}
#[must_use]
pub const fn repaired(&self) -> bool {
self.repaired
}
#[must_use]
pub fn contains(&self, id: &OutlineItemId) -> bool {
self.find(id).is_some()
}
#[must_use]
pub fn parent(&self, id: &OutlineItemId) -> Option<Option<&OutlineItemId>> {
let node = self.nodes.get(self.find(id)?)?;
Some(node.parent.and_then(|p| self.nodes.get(p)).map(|p| &p.id))
}
#[must_use]
pub fn subtree(&self, id: &OutlineItemId) -> Vec<OutlineItemId> {
let Some(index) = self.find(id) else { return Vec::new() };
self.descendants(index)
.into_iter()
.filter_map(|i| self.nodes.get(i).map(|n| n.id.clone()))
.collect()
}
#[must_use]
pub fn items(&self) -> Vec<OutlineItemId> {
let mut out = Vec::new();
for &root in &self.roots {
for i in self.descendants(root) {
if let Some(n) = self.nodes.get(i) {
out.push(n.id.clone());
}
}
}
out
}
pub fn insert(
&mut self,
item: ObjRef,
parent: Option<&OutlineItemId>,
at: &SiblingPosition,
) -> core::result::Result<OutlineItemId, OutlineTreeError> {
let parent = parent.map(|p| self.require(p)).transpose()?;
let slot = self.slot_index(parent, at)?;
let id = OutlineItemId::object(item);
let index = self.nodes.len();
self.nodes.push(Node {
id: id.clone(),
slot: Slot::Indirect(item),
parent,
children: Vec::new(),
open: false,
gone: false,
});
self.siblings_mut(parent).insert(slot, index);
Ok(id)
}
pub fn move_item(
&mut self,
item: &OutlineItemId,
parent: Option<&OutlineItemId>,
at: &SiblingPosition,
) -> core::result::Result<(), OutlineTreeError> {
let index = self.require(item)?;
let parent = parent.map(|p| self.require(p)).transpose()?;
if parent.is_some_and(|p| self.descendants(index).contains(&p))
|| matches!(at, SiblingPosition::Before(s) | SiblingPosition::After(s) if s == item)
{
return Err(OutlineTreeError::IntoItself(item.clone()));
}
self.slot_index(parent, at)?;
let old = self.nodes.get(index).and_then(|n| n.parent);
self.siblings_mut(old).retain(|&i| i != index);
let slot = self.slot_index(parent, at)?;
self.siblings_mut(parent).insert(slot, index);
if let Some(node) = self.nodes.get_mut(index) {
node.parent = parent;
}
Ok(())
}
pub fn remove(
&mut self,
item: &OutlineItemId,
promote: bool,
) -> core::result::Result<Vec<OutlineItemId>, OutlineTreeError> {
let index = self.require(item)?;
let (parent, children) = match self.nodes.get(index) {
Some(n) => (n.parent, n.children.clone()),
None => return Err(OutlineTreeError::UnknownItem(item.clone())),
};
let gone = if promote { vec![index] } else { self.descendants(index) };
let siblings = self.siblings_mut(parent);
let at = siblings.iter().position(|&i| i == index).unwrap_or(siblings.len());
siblings.retain(|&i| i != index);
if promote {
for (k, &child) in children.iter().enumerate() {
siblings.insert(at + k, child);
}
for &child in &children {
if let Some(c) = self.nodes.get_mut(child) {
c.parent = parent;
}
}
if let Some(n) = self.nodes.get_mut(index) {
n.children.clear();
}
}
let mut removed = Vec::with_capacity(gone.len());
for i in gone {
if let Some(n) = self.nodes.get_mut(i) {
n.gone = true;
removed.push(n.id.clone());
}
}
Ok(removed)
}
pub fn set_open(&mut self, item: &OutlineItemId, open: bool) -> core::result::Result<(), OutlineTreeError> {
let index = self.require(item)?;
if let Some(n) = self.nodes.get_mut(index) {
n.open = open;
}
Ok(())
}
pub fn write(&mut self, cos: &mut CosMut<'_>) -> Result<()> {
for node in &mut self.nodes {
if let Slot::Direct(dict) = &node.slot {
let r = cos.create(Object::Dict(dict.clone()))?;
node.slot = Slot::Indirect(r);
node.id = OutlineItemId::object(r);
}
}
let root = match self.root.take() {
Some(Slot::Indirect(r)) => r,
Some(Slot::Direct(dict)) => {
let r = cos.create(Object::Dict(dict))?;
set_catalog_entry(cos, "Outlines", Some(Object::Ref(r)))?;
r
}
None if self.roots.is_empty() => return Ok(()),
None => {
let dict: Dict = [("Type", Object::name("Outlines"))].into_iter().collect();
let r = cos.create(Object::Dict(dict))?;
set_catalog_entry(cos, "Outlines", Some(Object::Ref(r)))?;
r
}
};
self.root = Some(Slot::Indirect(root));
for (r, links) in self.wanted(root) {
let Some(current) = cos.get(r)? else { continue };
let Some(dict) = current.as_dict() else { continue };
let mut next = dict.clone();
links.apply(&mut next);
if next != *dict {
cos.set(r, Object::Dict(next))?;
}
}
self.repaired = false;
Ok(())
}
fn links_differ(&self, doc: &Document) -> Result<bool> {
let Some(Slot::Indirect(root)) = self.root else {
return Ok(!self.roots.is_empty());
};
for (r, links) in self.wanted(root) {
let Some(current) = doc.read(&Object::Ref(r))? else {
return Ok(true);
};
let Some(dict) = current.as_dict() else { return Ok(true) };
let mut want = dict.clone();
links.apply_links(&mut want);
if want != *dict {
return Ok(true);
}
}
Ok(false)
}
fn wanted(&self, root: ObjRef) -> Vec<(ObjRef, Links)> {
let r = |i: usize| match self.nodes.get(i).map(|n| &n.slot) {
Some(Slot::Indirect(r)) => Some(*r),
_ => None,
};
let ends = |children: &[usize]| {
(
children.first().and_then(|&c| r(c)),
children.last().and_then(|&c| r(c)),
)
};
let (first, last) = ends(&self.roots);
let visible: i64 = self.roots.iter().map(|&c| 1 + self.visible(c)).sum();
let mut out = vec![(
root,
Links {
parent: None,
prev: None,
next: None,
first,
last,
count: (visible > 0).then_some(visible),
is_root: true,
has_children: !self.roots.is_empty(),
},
)];
let mut walk: Vec<(Option<usize>, &[usize])> = vec![(None, &self.roots)];
while let Some((parent, children)) = walk.pop() {
for (k, &c) in children.iter().enumerate() {
let (Some(own), Some(node)) = (r(c), self.nodes.get(c)) else {
continue;
};
let (first, last) = ends(&node.children);
let shown: i64 = node.children.iter().map(|&g| 1 + self.visible(g)).sum();
out.push((
own,
Links {
parent: Some(parent.and_then(r).unwrap_or(root)),
prev: k.checked_sub(1).and_then(|p| children.get(p)).and_then(|&p| r(p)),
next: children.get(k + 1).and_then(|&n| r(n)),
first,
last,
count: (!node.children.is_empty()).then_some(if node.open { shown } else { -shown }),
is_root: false,
has_children: !node.children.is_empty(),
},
));
walk.push((Some(c), &node.children));
}
}
out
}
fn visible(&self, index: usize) -> i64 {
let mut total = 0;
let mut stack = vec![index];
while let Some(i) = stack.pop() {
let Some(n) = self.nodes.get(i) else { continue };
if n.open {
total += i64::try_from(n.children.len()).unwrap_or(i64::MAX);
stack.extend(&n.children);
}
}
total
}
fn descendants(&self, index: usize) -> Vec<usize> {
let mut out = Vec::new();
let mut stack = vec![index];
while let Some(i) = stack.pop() {
out.push(i);
if let Some(n) = self.nodes.get(i) {
stack.extend(n.children.iter().rev());
}
}
out
}
fn find(&self, id: &OutlineItemId) -> Option<usize> {
self.nodes.iter().position(|n| !n.gone && n.id == *id)
}
fn require(&self, id: &OutlineItemId) -> core::result::Result<usize, OutlineTreeError> {
self.find(id).ok_or_else(|| OutlineTreeError::UnknownItem(id.clone()))
}
fn siblings_mut(&mut self, parent: Option<usize>) -> &mut Vec<usize> {
match parent {
Some(p) if p < self.nodes.len() => match self.nodes.get_mut(p) {
Some(n) => &mut n.children,
None => &mut self.roots,
},
_ => &mut self.roots,
}
}
fn slot_index(
&mut self,
parent: Option<usize>,
at: &SiblingPosition,
) -> core::result::Result<usize, OutlineTreeError> {
let sibling = match at {
SiblingPosition::Before(s) | SiblingPosition::After(s) => Some((s.clone(), self.require(s)?)),
_ => None,
};
let siblings = self.siblings_mut(parent);
Ok(match (at, sibling) {
(SiblingPosition::First, _) => 0,
(SiblingPosition::Before(_) | SiblingPosition::After(_), Some((id, s))) => {
let k = siblings
.iter()
.position(|&i| i == s)
.ok_or(OutlineTreeError::NotASibling(id))?;
if matches!(at, SiblingPosition::After(_)) {
k + 1
} else {
k
}
}
_ => siblings.len(),
})
}
}
#[derive(Debug, Clone, Copy)]
struct Links {
parent: Option<ObjRef>,
prev: Option<ObjRef>,
next: Option<ObjRef>,
first: Option<ObjRef>,
last: Option<ObjRef>,
count: Option<i64>,
is_root: bool,
has_children: bool,
}
impl Links {
fn apply_links(self, dict: &mut Dict) {
if !self.is_root {
put(dict, "Parent", self.parent.map(Object::Ref));
put(dict, "Prev", self.prev.map(Object::Ref));
put(dict, "Next", self.next.map(Object::Ref));
}
put(dict, "First", self.first.map(Object::Ref));
put(dict, "Last", self.last.map(Object::Ref));
}
fn apply(self, dict: &mut Dict) {
self.apply_links(dict);
match self.count {
Some(count) => dict.insert("Count", Object::Integer(count)),
None if !self.has_children && dict.get("Count").and_then(Object::as_int) == Some(0) => {}
None => {
dict.remove("Count");
}
}
}
}