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