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