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/// Wraps `PDFDestination`.
12#[derive(Debug, Clone)]
13pub struct PdfDestination {
14    handle: ObjectHandle,
15}
16
17impl PdfDestination {
18    /// Wraps `PDFDestination.unspecifiedValue`.
19    pub const UNSPECIFIED_VALUE: f64 = 3.402_823_466_385_288_6e38;
20
21    pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
22        Self { handle }
23    }
24
25    /// Wraps `PDFDestination(page:at:)`.
26    pub fn new(page: &PdfPage, point: PdfPoint) -> Result<Self> {
27        let mut out_destination = ptr::null_mut();
28        let mut out_error = ptr::null_mut();
29        let status = unsafe {
30            ffi::pdf_destination_new(
31                page.as_handle_ptr(),
32                point.x,
33                point.y,
34                &mut out_destination,
35                &mut out_error,
36            )
37        };
38        crate::util::status_result(status, out_error)?;
39        Ok(Self::from_handle(crate::util::required_handle(
40            out_destination,
41            "PDFDestination",
42        )?))
43    }
44
45    /// Wraps the corresponding `PDFDestination` API.
46    pub fn info(&self) -> Result<PdfDestinationInfo> {
47        parse_json(
48            unsafe { ffi::pdf_destination_info_json(self.handle.as_ptr()) },
49            "PDFDestination",
50        )
51    }
52
53    /// Wraps the corresponding `PDFDestination` API.
54    #[must_use]
55    pub fn page(&self) -> Option<PdfPage> {
56        let ptr = unsafe { ffi::pdf_destination_page(self.handle.as_ptr()) };
57        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(PdfPage::from_handle)
58    }
59
60    /// Wraps the corresponding `PDFDestination` API.
61    pub fn set_zoom(&self, zoom: f64) {
62        unsafe { ffi::pdf_destination_set_zoom(self.handle.as_ptr(), zoom) };
63    }
64
65    /// Wraps the corresponding `PDFDestination` API.
66    #[must_use]
67    pub fn compare(&self, other: &Self) -> Ordering {
68        match unsafe { ffi::pdf_destination_compare(self.handle.as_ptr(), other.handle.as_ptr()) } {
69            value if value < 0 => Ordering::Less,
70            value if value > 0 => Ordering::Greater,
71            _ => Ordering::Equal,
72        }
73    }
74
75    pub(crate) fn as_handle_ptr(&self) -> *mut core::ffi::c_void {
76        self.handle.as_ptr()
77    }
78}