pdfium_render/pdf/document/page/
field.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//! Defines the [PdfFormField] enum, exposing functionality related to a single interactive
//! form field in a [PdfForm].

pub mod button;
pub mod checkbox;
pub mod combo;
pub mod list;
pub mod option;
pub mod options;
pub(crate) mod private; // Keep private so that the PdfFormFieldPrivate trait is not exposed.
pub mod radio;
pub mod signature;
pub mod text;
pub mod unknown;

use crate::bindgen::{
    FPDF_ANNOTATION, FPDF_FORMFIELD_CHECKBOX, FPDF_FORMFIELD_COMBOBOX, FPDF_FORMFIELD_LISTBOX,
    FPDF_FORMFIELD_PUSHBUTTON, FPDF_FORMFIELD_RADIOBUTTON, FPDF_FORMFIELD_SIGNATURE,
    FPDF_FORMFIELD_TEXTFIELD, FPDF_FORMFIELD_UNKNOWN, FPDF_FORMHANDLE,
};
use crate::bindings::PdfiumLibraryBindings;
use crate::error::PdfiumError;
use crate::pdf::appearance_mode::PdfAppearanceMode;
use crate::pdf::document::page::field::button::PdfFormPushButtonField;
use crate::pdf::document::page::field::checkbox::PdfFormCheckboxField;
use crate::pdf::document::page::field::combo::PdfFormComboBoxField;
use crate::pdf::document::page::field::list::PdfFormListBoxField;
use crate::pdf::document::page::field::private::internal::PdfFormFieldPrivate;
use crate::pdf::document::page::field::radio::PdfFormRadioButtonField;
use crate::pdf::document::page::field::signature::PdfFormSignatureField;
use crate::pdf::document::page::field::text::PdfFormTextField;
use crate::pdf::document::page::field::unknown::PdfFormUnknownField;
use std::os::raw::c_int;

#[cfg(doc)]
use crate::pdf::document::form::PdfForm;

/// The widget display type of a single interactive form field in a [PdfForm].
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
pub enum PdfFormFieldType {
    // The FPDF_FORMFIELD_COUNT constant simply specifies the number of form field
    // widget types supported by Pdfium; we do not need to expose it.
    Unknown = FPDF_FORMFIELD_UNKNOWN as isize,
    PushButton = FPDF_FORMFIELD_PUSHBUTTON as isize,
    Checkbox = FPDF_FORMFIELD_CHECKBOX as isize,
    RadioButton = FPDF_FORMFIELD_RADIOBUTTON as isize,
    ComboBox = FPDF_FORMFIELD_COMBOBOX as isize,
    ListBox = FPDF_FORMFIELD_LISTBOX as isize,
    Text = FPDF_FORMFIELD_TEXTFIELD as isize,
    Signature = FPDF_FORMFIELD_SIGNATURE as isize,
}

impl PdfFormFieldType {
    #[inline]
    #[allow(dead_code)]
    pub(crate) fn from_pdfium(value: c_int) -> Result<PdfFormFieldType, PdfiumError> {
        match value as u32 {
            FPDF_FORMFIELD_UNKNOWN => Ok(PdfFormFieldType::Unknown),
            FPDF_FORMFIELD_PUSHBUTTON => Ok(PdfFormFieldType::PushButton),
            FPDF_FORMFIELD_CHECKBOX => Ok(PdfFormFieldType::Checkbox),
            FPDF_FORMFIELD_RADIOBUTTON => Ok(PdfFormFieldType::RadioButton),
            FPDF_FORMFIELD_COMBOBOX => Ok(PdfFormFieldType::ComboBox),
            FPDF_FORMFIELD_LISTBOX => Ok(PdfFormFieldType::ListBox),
            FPDF_FORMFIELD_TEXTFIELD => Ok(PdfFormFieldType::Text),
            FPDF_FORMFIELD_SIGNATURE => Ok(PdfFormFieldType::Signature),
            _ => Err(PdfiumError::UnknownFormFieldType),
        }
    }

    #[inline]
    #[allow(dead_code)]
    // The as_pdfium() function is not currently used, but we expect it to be in future
    pub(crate) fn as_pdfium(&self) -> u32 {
        match self {
            PdfFormFieldType::Unknown => FPDF_FORMFIELD_UNKNOWN,
            PdfFormFieldType::PushButton => FPDF_FORMFIELD_PUSHBUTTON,
            PdfFormFieldType::Checkbox => FPDF_FORMFIELD_CHECKBOX,
            PdfFormFieldType::RadioButton => FPDF_FORMFIELD_RADIOBUTTON,
            PdfFormFieldType::ComboBox => FPDF_FORMFIELD_COMBOBOX,
            PdfFormFieldType::ListBox => FPDF_FORMFIELD_LISTBOX,
            PdfFormFieldType::Text => FPDF_FORMFIELD_TEXTFIELD,
            PdfFormFieldType::Signature => FPDF_FORMFIELD_SIGNATURE,
        }
    }
}

/// A single interactive form field in a [PdfForm].
pub enum PdfFormField<'a> {
    PushButton(PdfFormPushButtonField<'a>),
    Checkbox(PdfFormCheckboxField<'a>),
    RadioButton(PdfFormRadioButtonField<'a>),
    ComboBox(PdfFormComboBoxField<'a>),
    ListBox(PdfFormListBoxField<'a>),
    Signature(PdfFormSignatureField<'a>),
    Text(PdfFormTextField<'a>),
    Unknown(PdfFormUnknownField<'a>),
}

impl<'a> PdfFormField<'a> {
    pub(crate) fn from_pdfium(
        form_handle: FPDF_FORMHANDLE,
        annotation_handle: FPDF_ANNOTATION,
        bindings: &'a dyn PdfiumLibraryBindings,
    ) -> Option<Self> {
        let result = bindings.FPDFAnnot_GetFormFieldType(form_handle, annotation_handle);

        if result == -1 {
            return None;
        }

        let form_field_type =
            PdfFormFieldType::from_pdfium(result).unwrap_or(PdfFormFieldType::Unknown);

        Some(match form_field_type {
            PdfFormFieldType::PushButton => PdfFormField::PushButton(
                PdfFormPushButtonField::from_pdfium(form_handle, annotation_handle, bindings),
            ),
            PdfFormFieldType::Checkbox => PdfFormField::Checkbox(
                PdfFormCheckboxField::from_pdfium(form_handle, annotation_handle, bindings),
            ),
            PdfFormFieldType::RadioButton => PdfFormField::RadioButton(
                PdfFormRadioButtonField::from_pdfium(form_handle, annotation_handle, bindings),
            ),
            PdfFormFieldType::ComboBox => PdfFormField::ComboBox(
                PdfFormComboBoxField::from_pdfium(form_handle, annotation_handle, bindings),
            ),
            PdfFormFieldType::ListBox => PdfFormField::ListBox(PdfFormListBoxField::from_pdfium(
                form_handle,
                annotation_handle,
                bindings,
            )),
            PdfFormFieldType::Text => PdfFormField::Text(PdfFormTextField::from_pdfium(
                form_handle,
                annotation_handle,
                bindings,
            )),
            PdfFormFieldType::Signature => PdfFormField::Signature(
                PdfFormSignatureField::from_pdfium(form_handle, annotation_handle, bindings),
            ),
            _ => PdfFormField::Unknown(PdfFormUnknownField::from_pdfium(
                form_handle,
                annotation_handle,
                bindings,
            )),
        })
    }

    #[inline]
    pub(crate) fn unwrap_as_trait(&self) -> &dyn PdfFormFieldPrivate<'a> {
        match self {
            PdfFormField::PushButton(field) => field,
            PdfFormField::Checkbox(field) => field,
            PdfFormField::RadioButton(field) => field,
            PdfFormField::ComboBox(field) => field,
            PdfFormField::ListBox(field) => field,
            PdfFormField::Signature(field) => field,
            PdfFormField::Text(field) => field,
            PdfFormField::Unknown(field) => field,
        }
    }

    /// The type of this [PdfFormField].
    #[inline]
    pub fn field_type(&self) -> PdfFormFieldType {
        match self {
            PdfFormField::PushButton(_) => PdfFormFieldType::PushButton,
            PdfFormField::Checkbox(_) => PdfFormFieldType::Checkbox,
            PdfFormField::RadioButton(_) => PdfFormFieldType::RadioButton,
            PdfFormField::ComboBox(_) => PdfFormFieldType::ComboBox,
            PdfFormField::ListBox(_) => PdfFormFieldType::ListBox,
            PdfFormField::Signature(_) => PdfFormFieldType::Signature,
            PdfFormField::Text(_) => PdfFormFieldType::Text,
            PdfFormField::Unknown(_) => PdfFormFieldType::Unknown,
        }
    }

    /// Returns a reference to the underlying [PdfFormPushButtonField] for this [PdfFormField],
    /// if this form field has a field type of [PdfFormField::PushButton].
    #[inline]
    pub fn as_push_button_field(&self) -> Option<&PdfFormPushButtonField> {
        match self {
            PdfFormField::PushButton(field) => Some(field),
            _ => None,
        }
    }

    /// Returns a reference to the underlying [PdfFormCheckboxField] for this [PdfFormField],
    /// if this form field has a field type of [PdfFormField::Checkbox].
    #[inline]
    pub fn as_checkbox_field(&self) -> Option<&PdfFormCheckboxField> {
        match self {
            PdfFormField::Checkbox(field) => Some(field),
            _ => None,
        }
    }

    /// Returns a mutable reference to the underlying [PdfFormCheckboxField]
    /// for this [PdfFormField], if this form field has a field type of [PdfFormField::Checkbox].
    #[inline]
    pub fn as_checkbox_field_mut(&mut self) -> Option<&mut PdfFormCheckboxField<'a>> {
        match self {
            PdfFormField::Checkbox(field) => Some(field),
            _ => None,
        }
    }

    /// Returns a reference to the underlying [PdfFormRadioButtonField] for this [PdfFormField],
    /// if this form field has a field type of [PdfFormField::RadioButton].
    #[inline]
    pub fn as_radio_button_field(&self) -> Option<&PdfFormRadioButtonField> {
        match self {
            PdfFormField::RadioButton(field) => Some(field),
            _ => None,
        }
    }

    /// Returns a mutable reference to the underlying [PdfFormRadioButtonField]
    /// for this [PdfFormField], if this form field has a field type of [PdfFormField::RadioButton].
    #[inline]
    pub fn as_radio_button_field_mut(&mut self) -> Option<&mut PdfFormRadioButtonField<'a>> {
        match self {
            PdfFormField::RadioButton(field) => Some(field),
            _ => None,
        }
    }

    /// Returns a reference to the underlying [PdfFormComboBoxField] for this [PdfFormField],
    /// if this form field has a field type of [PdfFormField::ComboBox].
    #[inline]
    pub fn as_combo_box_field(&self) -> Option<&PdfFormComboBoxField> {
        match self {
            PdfFormField::ComboBox(field) => Some(field),
            _ => None,
        }
    }

    /// Returns a reference to the underlying [PdfFormListBoxField] for this [PdfFormField],
    /// if this form field has a field type of [PdfFormField::ListBox].
    #[inline]
    pub fn as_list_box_field(&self) -> Option<&PdfFormListBoxField> {
        match self {
            PdfFormField::ListBox(field) => Some(field),
            _ => None,
        }
    }

    /// Returns a reference to the underlying [PdfFormSignatureField] for this [PdfFormField],
    /// if this form field has a field type of [PdfFormField::Signature].
    #[inline]
    pub fn as_signature_field(&self) -> Option<&PdfFormSignatureField> {
        match self {
            PdfFormField::Signature(field) => Some(field),
            _ => None,
        }
    }

    /// Returns a reference to the underlying [PdfFormTextField] for this [PdfFormField],
    /// if this form field has a field type of [PdfFormField::Text].
    #[inline]
    pub fn as_text_field(&self) -> Option<&PdfFormTextField> {
        match self {
            PdfFormField::Text(field) => Some(field),
            _ => None,
        }
    }

    /// Returns a mutable reference to the underlying [PdfFormTextField] for this
    /// [PdfFormField], if this form field has a field type of [PdfFormField::Text].
    pub fn as_text_field_mut(&mut self) -> Option<&mut PdfFormTextField<'a>> {
        match self {
            PdfFormField::Text(field) => Some(field),
            _ => None,
        }
    }

    /// Returns a reference to the underlying [PdfFormUnknownField] for this [PdfFormField],
    /// if this form field has a field type of [PdfFormField::Unknown].
    #[inline]
    pub fn as_unknown_field(&self) -> Option<&PdfFormUnknownField> {
        match self {
            PdfFormField::Unknown(field) => Some(field),
            _ => None,
        }
    }
}

/// Functionality common to all [PdfFormField] objects, regardless of their [PdfFormFieldType].
pub trait PdfFormFieldCommon {
    /// Returns the name of this [PdfFormField], if any.
    fn name(&self) -> Option<String>;

    /// Returns the name of the currently set appearance stream for this [PdfFormField], if any.
    fn appearance_stream(&self) -> Option<String>;

    /// Returns the value currently set for the given appearance mode for this [PdfFormField],
    /// if any.
    fn appearance_mode_value(&self, appearance_mode: PdfAppearanceMode) -> Option<String>;
}

// Blanket implementation for all PdfFormFieldCommon types.

impl<'a, T> PdfFormFieldCommon for T
where
    T: PdfFormFieldPrivate<'a>,
{
    #[inline]
    fn name(&self) -> Option<String> {
        self.name_impl()
    }

    #[inline]
    fn appearance_stream(&self) -> Option<String> {
        self.appearance_stream_impl()
    }

    #[inline]
    fn appearance_mode_value(&self, appearance_mode: PdfAppearanceMode) -> Option<String> {
        self.appearance_mode_value_impl(appearance_mode)
    }
}

impl<'a> PdfFormFieldPrivate<'a> for PdfFormField<'a> {
    #[inline]
    fn form_handle(&self) -> &FPDF_FORMHANDLE {
        self.unwrap_as_trait().form_handle()
    }

    #[inline]
    fn annotation_handle(&self) -> &FPDF_ANNOTATION {
        self.unwrap_as_trait().annotation_handle()
    }

    #[inline]
    fn bindings(&self) -> &dyn PdfiumLibraryBindings {
        self.unwrap_as_trait().bindings()
    }
}

impl<'a> From<PdfFormPushButtonField<'a>> for PdfFormField<'a> {
    #[inline]
    fn from(field: PdfFormPushButtonField<'a>) -> Self {
        Self::PushButton(field)
    }
}

impl<'a> From<PdfFormCheckboxField<'a>> for PdfFormField<'a> {
    #[inline]
    fn from(field: PdfFormCheckboxField<'a>) -> Self {
        Self::Checkbox(field)
    }
}

impl<'a> From<PdfFormRadioButtonField<'a>> for PdfFormField<'a> {
    #[inline]
    fn from(field: PdfFormRadioButtonField<'a>) -> Self {
        Self::RadioButton(field)
    }
}

impl<'a> From<PdfFormComboBoxField<'a>> for PdfFormField<'a> {
    #[inline]
    fn from(field: PdfFormComboBoxField<'a>) -> Self {
        Self::ComboBox(field)
    }
}

impl<'a> From<PdfFormListBoxField<'a>> for PdfFormField<'a> {
    #[inline]
    fn from(field: PdfFormListBoxField<'a>) -> Self {
        Self::ListBox(field)
    }
}

impl<'a> From<PdfFormTextField<'a>> for PdfFormField<'a> {
    #[inline]
    fn from(field: PdfFormTextField<'a>) -> Self {
        Self::Text(field)
    }
}

impl<'a> From<PdfFormSignatureField<'a>> for PdfFormField<'a> {
    #[inline]
    fn from(field: PdfFormSignatureField<'a>) -> Self {
        Self::Signature(field)
    }
}

impl<'a> From<PdfFormUnknownField<'a>> for PdfFormField<'a> {
    #[inline]
    fn from(field: PdfFormUnknownField<'a>) -> Self {
        Self::Unknown(field)
    }
}