Skip to main content

pdfkit/
appearance_characteristics.rs

1use std::ptr;
2
3use crate::error::Result;
4use crate::ffi;
5use crate::handle::ObjectHandle;
6use crate::types::{PdfAppearanceCharacteristicsInfo, PdfColor, PdfWidgetControlType};
7use crate::util::{option_c_string, parse_json};
8
9/// Wraps `PDFAppearanceCharacteristics`.
10#[derive(Debug, Clone)]
11pub struct PdfAppearanceCharacteristics {
12    handle: ObjectHandle,
13}
14
15impl PdfAppearanceCharacteristics {
16    pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
17        Self { handle }
18    }
19
20    /// Wraps the corresponding `PDFAppearanceCharacteristics` API.
21    pub fn new() -> Result<Self> {
22        let mut out_value = ptr::null_mut();
23        let mut out_error = ptr::null_mut();
24        let status =
25            unsafe { ffi::pdf_appearance_characteristics_new(&mut out_value, &mut out_error) };
26        crate::util::status_result(status, out_error)?;
27        Ok(Self::from_handle(crate::util::required_handle(
28            out_value,
29            "PDFAppearanceCharacteristics",
30        )?))
31    }
32
33    /// Wraps the corresponding `PDFAppearanceCharacteristics` API.
34    pub fn info(&self) -> Result<PdfAppearanceCharacteristicsInfo> {
35        parse_json(
36            unsafe { ffi::pdf_appearance_characteristics_info_json(self.handle.as_ptr()) },
37            "PDFAppearanceCharacteristics",
38        )
39    }
40
41    /// Wraps the corresponding `PDFAppearanceCharacteristics` API.
42    pub fn set_control_type(&self, control_type: PdfWidgetControlType) -> Result<()> {
43        let mut out_error = ptr::null_mut();
44        let status = unsafe {
45            ffi::pdf_appearance_characteristics_set_control_type(
46                self.handle.as_ptr(),
47                control_type as i32,
48                &mut out_error,
49            )
50        };
51        crate::util::status_result(status, out_error)
52    }
53
54    /// Wraps the corresponding `PDFAppearanceCharacteristics` API.
55    pub fn set_rotation(&self, rotation: i32) {
56        unsafe { ffi::pdf_appearance_characteristics_set_rotation(self.handle.as_ptr(), rotation) };
57    }
58
59    /// Wraps the corresponding `PDFAppearanceCharacteristics` API.
60    pub fn set_caption(&self, caption: Option<&str>) -> Result<()> {
61        let caption = option_c_string(caption)?;
62        unsafe {
63            ffi::pdf_appearance_characteristics_set_caption(
64                self.handle.as_ptr(),
65                caption.as_ref().map_or(ptr::null(), |value| value.as_ptr()),
66            );
67        };
68        Ok(())
69    }
70
71    /// Wraps the corresponding `PDFAppearanceCharacteristics` API.
72    pub fn set_rollover_caption(&self, caption: Option<&str>) -> Result<()> {
73        let caption = option_c_string(caption)?;
74        unsafe {
75            ffi::pdf_appearance_characteristics_set_rollover_caption(
76                self.handle.as_ptr(),
77                caption.as_ref().map_or(ptr::null(), |value| value.as_ptr()),
78            );
79        };
80        Ok(())
81    }
82
83    /// Wraps the corresponding `PDFAppearanceCharacteristics` API.
84    pub fn set_down_caption(&self, caption: Option<&str>) -> Result<()> {
85        let caption = option_c_string(caption)?;
86        unsafe {
87            ffi::pdf_appearance_characteristics_set_down_caption(
88                self.handle.as_ptr(),
89                caption.as_ref().map_or(ptr::null(), |value| value.as_ptr()),
90            );
91        };
92        Ok(())
93    }
94
95    /// Wraps the corresponding `PDFAppearanceCharacteristics` API.
96    pub fn set_background_color(&self, color: PdfColor) {
97        unsafe {
98            ffi::pdf_appearance_characteristics_set_background_color(
99                self.handle.as_ptr(),
100                color.red,
101                color.green,
102                color.blue,
103                color.alpha,
104            );
105        };
106    }
107
108    /// Wraps the corresponding `PDFAppearanceCharacteristics` API.
109    pub fn set_border_color(&self, color: PdfColor) {
110        unsafe {
111            ffi::pdf_appearance_characteristics_set_border_color(
112                self.handle.as_ptr(),
113                color.red,
114                color.green,
115                color.blue,
116                color.alpha,
117            );
118        };
119    }
120}