Skip to main content

pdfkit/
outline.rs

1use std::ptr;
2
3use crate::action::{PdfAction, PdfActionLike};
4use crate::action_goto::PdfActionGoTo;
5use crate::action_url::PdfActionUrl;
6use crate::destination::PdfDestination;
7use crate::error::Result;
8use crate::ffi;
9use crate::handle::ObjectHandle;
10use crate::util::{option_c_string, take_string};
11
12/// Wraps `PDFOutline`.
13#[derive(Debug, Clone)]
14pub struct PdfOutline {
15    handle: ObjectHandle,
16}
17
18impl PdfOutline {
19    pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
20        Self { handle }
21    }
22
23    /// Wraps `PDFOutline()`.
24    pub fn new() -> Result<Self> {
25        let mut out_outline = ptr::null_mut();
26        let mut out_error = ptr::null_mut();
27        let status = unsafe { ffi::pdf_outline_new(&mut out_outline, &mut out_error) };
28        crate::util::status_result(status, out_error)?;
29        Ok(Self::from_handle(crate::util::required_handle(
30            out_outline,
31            "PDFOutline",
32        )?))
33    }
34
35    /// Wraps the corresponding `PDFOutline` API.
36    #[must_use]
37    pub fn label(&self) -> Option<String> {
38        take_string(unsafe { ffi::pdf_outline_label_string(self.handle.as_ptr()) })
39    }
40
41    /// Wraps the corresponding `PDFOutline` API.
42    pub fn set_label(&self, value: Option<&str>) -> Result<()> {
43        let value = option_c_string(value)?;
44        let mut out_error = ptr::null_mut();
45        let status = unsafe {
46            ffi::pdf_outline_set_label(
47                self.handle.as_ptr(),
48                value.as_ref().map_or(ptr::null(), |value| value.as_ptr()),
49                &mut out_error,
50            )
51        };
52        crate::util::status_result(status, out_error)
53    }
54
55    /// Wraps the corresponding `PDFOutline` API.
56    #[must_use]
57    pub fn child_count(&self) -> usize {
58        unsafe { ffi::pdf_outline_child_count(self.handle.as_ptr()) as usize }
59    }
60
61    /// Wraps the corresponding `PDFOutline` API.
62    #[must_use]
63    pub fn child(&self, index: usize) -> Option<Self> {
64        let ptr = unsafe { ffi::pdf_outline_child_at(self.handle.as_ptr(), index as u64) };
65        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(Self::from_handle)
66    }
67
68    /// Wraps the corresponding `PDFOutline` API.
69    #[must_use]
70    pub fn children(&self) -> Vec<Self> {
71        (0..self.child_count())
72            .filter_map(|index| self.child(index))
73            .collect()
74    }
75
76    /// Wraps the corresponding `PDFOutline` API.
77    pub fn insert_child(&self, child: &Self, index: usize) -> Result<()> {
78        let mut out_error = ptr::null_mut();
79        let status = unsafe {
80            ffi::pdf_outline_insert_child(
81                self.handle.as_ptr(),
82                child.handle.as_ptr(),
83                index as u64,
84                &mut out_error,
85            )
86        };
87        crate::util::status_result(status, out_error)
88    }
89
90    /// Wraps the corresponding `PDFOutline` API.
91    pub fn remove_from_parent(&self) {
92        unsafe { ffi::pdf_outline_remove_from_parent(self.handle.as_ptr()) };
93    }
94
95    /// Wraps the corresponding `PDFOutline` API.
96    #[must_use]
97    pub fn index(&self) -> usize {
98        unsafe { ffi::pdf_outline_index(self.handle.as_ptr()) as usize }
99    }
100
101    /// Wraps the corresponding `PDFOutline` API.
102    #[must_use]
103    pub fn is_open(&self) -> bool {
104        unsafe { ffi::pdf_outline_is_open(self.handle.as_ptr()) != 0 }
105    }
106
107    /// Wraps the corresponding `PDFOutline` API.
108    pub fn set_open(&self, value: bool) {
109        unsafe { ffi::pdf_outline_set_open(self.handle.as_ptr(), i32::from(value)) };
110    }
111
112    /// Wraps the corresponding `PDFOutline` API.
113    #[must_use]
114    pub fn destination(&self) -> Option<PdfDestination> {
115        let ptr = unsafe { ffi::pdf_outline_destination(self.handle.as_ptr()) };
116        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(PdfDestination::from_handle)
117    }
118
119    /// Wraps the corresponding `PDFOutline` API.
120    pub fn set_destination(&self, destination: Option<&PdfDestination>) -> Result<()> {
121        let mut out_error = ptr::null_mut();
122        let status = unsafe {
123            ffi::pdf_outline_set_destination(
124                self.handle.as_ptr(),
125                destination.map_or(ptr::null_mut(), PdfDestination::as_handle_ptr),
126                &mut out_error,
127            )
128        };
129        crate::util::status_result(status, out_error)
130    }
131
132    /// Wraps the corresponding `PDFOutline` API.
133    #[must_use]
134    pub fn action(&self) -> Option<PdfAction> {
135        let ptr = unsafe { ffi::pdf_outline_action(self.handle.as_ptr()) };
136        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(PdfAction::from_handle)
137    }
138
139    /// Wraps the corresponding `PDFOutline` API.
140    pub fn set_action<A: PdfActionLike>(&self, action: Option<&A>) -> Result<()> {
141        let mut out_error = ptr::null_mut();
142        let status = unsafe {
143            ffi::pdf_outline_set_action(
144                self.handle.as_ptr(),
145                action.map_or(ptr::null_mut(), PdfActionLike::as_action_handle_ptr),
146                &mut out_error,
147            )
148        };
149        crate::util::status_result(status, out_error)
150    }
151
152    /// Wraps the corresponding `PDFOutline` API.
153    pub fn clear_action(&self) -> Result<()> {
154        self.set_action::<PdfAction>(None)
155    }
156
157    /// Wraps the corresponding `PDFOutline` API.
158    #[must_use]
159    pub fn action_url(&self) -> Option<PdfActionUrl> {
160        let ptr = unsafe { ffi::pdf_outline_action_url(self.handle.as_ptr()) };
161        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(PdfActionUrl::from_handle)
162    }
163
164    /// Wraps the corresponding `PDFOutline` API.
165    pub fn set_action_url(&self, action: Option<&PdfActionUrl>) -> Result<()> {
166        self.set_action(action)
167    }
168
169    /// Wraps the corresponding `PDFOutline` API.
170    #[must_use]
171    pub fn action_goto(&self) -> Option<PdfActionGoTo> {
172        let ptr = unsafe { ffi::pdf_outline_action_goto(self.handle.as_ptr()) };
173        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(PdfActionGoTo::from_handle)
174    }
175
176    /// Wraps the corresponding `PDFOutline` API.
177    pub fn set_action_goto(&self, action: Option<&PdfActionGoTo>) -> Result<()> {
178        self.set_action(action)
179    }
180
181    /// Wraps the corresponding `PDFOutline` API.
182    #[must_use]
183    pub fn parent(&self) -> Option<Self> {
184        let ptr = unsafe { ffi::pdf_outline_parent(self.handle.as_ptr()) };
185        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(Self::from_handle)
186    }
187
188    pub(crate) fn as_handle_ptr(&self) -> *mut core::ffi::c_void {
189        self.handle.as_ptr()
190    }
191}