Skip to main content

pdfium_render/pdf/document/page/annotation/
variable_text.rs

1use crate::error::{PdfiumError, PdfiumInternalError};
2use crate::pdf::document::form::PdfForm;
3use crate::pdf::document::page::annotation::private::internal::PdfPageAnnotationPrivate;
4use crate::pdf::points::PdfPoints;
5use std::os::raw::c_float;
6
7#[cfg(any(
8    feature = "pdfium_future",
9    feature = "pdfium_7881",
10    feature = "pdfium_7763",
11    feature = "pdfium_7543",
12    feature = "pdfium_7350",
13    feature = "pdfium_7215",
14    feature = "pdfium_7123",
15    feature = "pdfium_6996",
16    feature = "pdfium_6721",
17    feature = "pdfium_6666",
18    feature = "pdfium_6611",
19    feature = "pdfium_6569",
20    feature = "pdfium_6555",
21))]
22use {crate::pdf::color::PdfColor, std::os::raw::c_uint};
23
24#[cfg(doc)]
25use crate::pdf::document::page::annotation::PdfPageAnnotation;
26
27/// The form of justification that should be used when displaying the text assigned
28/// to a [PdfPageAnnotation] that supports variable text.
29#[derive(Copy, Clone, Debug, PartialEq)]
30pub enum PdfPageAnnotationVariableTextJustification {
31    LeftJustified,
32    Centered,
33    RightJustified,
34}
35
36impl PdfPageAnnotationVariableTextJustification {
37    #[inline]
38    pub(crate) fn from_pdfium(value: i32) -> Result<Self, PdfiumError> {
39        match value {
40            0 => Ok(PdfPageAnnotationVariableTextJustification::LeftJustified),
41            1 => Ok(PdfPageAnnotationVariableTextJustification::Centered),
42            2 => Ok(PdfPageAnnotationVariableTextJustification::RightJustified),
43            _ => Err(PdfiumError::UnknownPageAnnotationVariableTextJustificationType),
44        }
45    }
46}
47
48/// Text-handling functions common to all [PdfPageAnnotation] types that
49/// support custom text.
50pub trait PdfPageAnnotationVariableText<'a> {
51    /// Returns the size of the text in this annotation. A value of [PdfPoints::ZERO]
52    /// indicates that the font size is determined automatically from the annotation height.
53    /// See also the [PdfPageAnnotationVariableText::is_font_auto_sized()] function.
54    fn font_size(&self, form: &PdfForm) -> Result<PdfPoints, PdfiumError>;
55
56    /// Returns `true` if the font size for this annotation is determined automatically
57    /// from the annotation height.
58    fn is_font_auto_sized(&self, form: &PdfForm) -> bool;
59
60    #[cfg(any(
61        feature = "pdfium_future",
62        feature = "pdfium_7881",
63        feature = "pdfium_7763",
64        feature = "pdfium_7543",
65        feature = "pdfium_7350",
66        feature = "pdfium_7215",
67        feature = "pdfium_7123",
68        feature = "pdfium_6996",
69        feature = "pdfium_6721",
70        feature = "pdfium_6666",
71        feature = "pdfium_6611",
72        feature = "pdfium_6569",
73        feature = "pdfium_6555",
74    ))]
75    /// Returns the color of the text in this annotation.
76    fn font_color(&self, form: &PdfForm) -> Result<PdfColor, PdfiumError>;
77
78    #[cfg(any(
79        feature = "pdfium_future",
80        feature = "pdfium_7881",
81        feature = "pdfium_7763",
82        feature = "pdfium_7350"
83    ))]
84    /// Sets the color of the text in this annotation.
85    fn set_font_color(&mut self, form: &PdfForm, color: PdfColor) -> Result<(), PdfiumError>;
86
87    /// Returns the form of justification that should be used when displaying the text
88    /// assigned to this annotation.
89    fn justification(&self) -> Result<PdfPageAnnotationVariableTextJustification, PdfiumError>;
90
91    /// Returns the rich text string assigned to this annotation, if any.
92    ///
93    /// Rich text support was added in PDF version 1.5.
94    fn rich_text(&self) -> Option<String>;
95}
96
97impl<'a, T> PdfPageAnnotationVariableText<'a> for T
98where
99    T: PdfPageAnnotationPrivate<'a>,
100{
101    fn font_size(&self, form: &PdfForm) -> Result<PdfPoints, PdfiumError> {
102        let mut value: c_float = 0.0;
103
104        if self.bindings().is_true(unsafe {
105            self.bindings()
106                .FPDFAnnot_GetFontSize(form.handle(), self.handle(), &mut value)
107        }) {
108            Ok(PdfPoints::new(value))
109        } else {
110            Err(PdfiumError::PdfiumLibraryInternalError(
111                PdfiumInternalError::Unknown,
112            ))
113        }
114    }
115
116    #[inline]
117    fn is_font_auto_sized(&self, form: &PdfForm) -> bool {
118        match self.font_size(form) {
119            Ok(size) => size.value == 0.0,
120            _ => false,
121        }
122    }
123
124    #[cfg(any(
125        feature = "pdfium_future",
126        feature = "pdfium_7881",
127        feature = "pdfium_7763",
128        feature = "pdfium_7543",
129        feature = "pdfium_7350",
130        feature = "pdfium_7215",
131        feature = "pdfium_7123",
132        feature = "pdfium_6996",
133        feature = "pdfium_6721",
134        feature = "pdfium_6666",
135        feature = "pdfium_6611",
136        feature = "pdfium_6569",
137        feature = "pdfium_6555",
138    ))]
139    fn font_color(&self, form: &PdfForm) -> Result<PdfColor, PdfiumError> {
140        let mut red: c_uint = 0;
141        let mut green: c_uint = 0;
142        let mut blue: c_uint = 0;
143
144        if self.bindings().is_true(unsafe {
145            self.bindings().FPDFAnnot_GetFontColor(
146                form.handle(),
147                self.handle(),
148                &mut red,
149                &mut green,
150                &mut blue,
151            )
152        }) {
153            Ok(PdfColor::new(red as u8, green as u8, blue as u8, 255))
154        } else {
155            Err(PdfiumError::PdfiumLibraryInternalError(
156                PdfiumInternalError::Unknown,
157            ))
158        }
159    }
160
161    #[cfg(any(
162        feature = "pdfium_future",
163        feature = "pdfium_7881",
164        feature = "pdfium_7763",
165        feature = "pdfium_7350"
166    ))]
167    fn set_font_color(&mut self, form: &PdfForm, color: PdfColor) -> Result<(), PdfiumError> {
168        if self.bindings().is_true(unsafe {
169            self.bindings().FPDFAnnot_SetFontColor(
170                form.handle(),
171                self.handle(),
172                color.red() as c_uint,
173                color.green() as c_uint,
174                color.blue() as c_uint,
175            )
176        }) {
177            Ok(())
178        } else {
179            Err(PdfiumError::PdfiumLibraryInternalError(
180                PdfiumInternalError::Unknown,
181            ))
182        }
183    }
184
185    fn justification(&self) -> Result<PdfPageAnnotationVariableTextJustification, PdfiumError> {
186        let mut value: c_float = 0.0;
187
188        if self.bindings().is_true(unsafe {
189            self.bindings()
190                .FPDFAnnot_GetNumberValue(self.handle(), "Q", &mut value)
191        }) {
192            PdfPageAnnotationVariableTextJustification::from_pdfium(value as i32)
193        } else {
194            Err(PdfiumError::PdfiumLibraryInternalError(
195                PdfiumInternalError::Unknown,
196            ))
197        }
198    }
199
200    #[inline]
201    fn rich_text(&self) -> Option<String> {
202        self.get_string_value("RV")
203    }
204}