Skip to main content

pdfkit/
destination.rs

1use std::cmp::Ordering;
2use std::ptr;
3
4use crate::error::Result;
5use crate::ffi;
6use crate::handle::ObjectHandle;
7use crate::page::PdfPage;
8use crate::types::{PdfDestinationInfo, PdfPoint};
9use crate::util::parse_json;
10
11#[derive(Debug, Clone)]
12pub struct PdfDestination {
13    handle: ObjectHandle,
14}
15
16impl PdfDestination {
17    pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
18        Self { handle }
19    }
20
21    pub fn new(page: &PdfPage, point: PdfPoint) -> Result<Self> {
22        let mut out_destination = ptr::null_mut();
23        let mut out_error = ptr::null_mut();
24        let status = unsafe {
25            ffi::pdf_destination_new(
26                page.as_handle_ptr(),
27                point.x,
28                point.y,
29                &mut out_destination,
30                &mut out_error,
31            )
32        };
33        crate::util::status_result(status, out_error)?;
34        Ok(Self::from_handle(crate::util::required_handle(
35            out_destination,
36            "PDFDestination",
37        )?))
38    }
39
40    pub fn info(&self) -> Result<PdfDestinationInfo> {
41        parse_json(
42            unsafe { ffi::pdf_destination_info_json(self.handle.as_ptr()) },
43            "PDFDestination",
44        )
45    }
46
47    #[must_use]
48    pub fn page(&self) -> Option<PdfPage> {
49        let ptr = unsafe { ffi::pdf_destination_page(self.handle.as_ptr()) };
50        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(PdfPage::from_handle)
51    }
52
53    pub fn set_zoom(&self, zoom: f64) {
54        unsafe { ffi::pdf_destination_set_zoom(self.handle.as_ptr(), zoom) };
55    }
56
57    #[must_use]
58    pub fn compare(&self, other: &Self) -> Ordering {
59        match unsafe { ffi::pdf_destination_compare(self.handle.as_ptr(), other.handle.as_ptr()) } {
60            value if value < 0 => Ordering::Less,
61            value if value > 0 => Ordering::Greater,
62            _ => Ordering::Equal,
63        }
64    }
65
66    pub(crate) fn as_handle_ptr(&self) -> *mut core::ffi::c_void {
67        self.handle.as_ptr()
68    }
69}