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
pub(crate) mod internal {
// We want to make the PdfFormFieldPrivate trait private while providing a blanket
// implementation of PdfFormFieldCommon for any type T where T: PdfFormFieldPrivate.
// Rust complains, however, that by doing so we are leaking the private trait outside
// the crate.
// Instead of making the PdfFormFieldPrivate trait private, we leave it public but place it
// inside this pub(crate) module in order to prevent it from being visible outside the crate.
use crate::bindgen::{
FPDF_ANNOTATION, FPDF_ANNOT_FLAG_HIDDEN, FPDF_ANNOT_FLAG_INVISIBLE, FPDF_ANNOT_FLAG_LOCKED,
FPDF_ANNOT_FLAG_NONE, FPDF_ANNOT_FLAG_NOROTATE, FPDF_ANNOT_FLAG_NOVIEW,
FPDF_ANNOT_FLAG_NOZOOM, FPDF_ANNOT_FLAG_PRINT, FPDF_ANNOT_FLAG_READONLY,
FPDF_ANNOT_FLAG_TOGGLENOVIEW, FPDF_FORMHANDLE, FPDF_WCHAR,
};
use crate::bindings::PdfiumLibraryBindings;
use crate::error::PdfiumError;
use crate::form_field::PdfFormFieldCommon;
use crate::utils::mem::create_byte_buffer;
use crate::utils::utf16le::get_string_from_pdfium_utf16le_bytes;
use std::os::raw::c_int;
use crate::appearance_mode::PdfAppearanceMode;
use bitflags::bitflags;
bitflags! {
pub struct FpdfAnnotationFlags: u32 {
const None = FPDF_ANNOT_FLAG_NONE;
const Invisible = FPDF_ANNOT_FLAG_INVISIBLE;
const Hidden = FPDF_ANNOT_FLAG_HIDDEN;
const Print = FPDF_ANNOT_FLAG_PRINT;
const NoZoom = FPDF_ANNOT_FLAG_NOZOOM;
const NoRotate = FPDF_ANNOT_FLAG_NOROTATE;
const NoView = FPDF_ANNOT_FLAG_NOVIEW;
const ReadOnly = FPDF_ANNOT_FLAG_READONLY;
const Locked = FPDF_ANNOT_FLAG_LOCKED;
const ToggleNoView = FPDF_ANNOT_FLAG_TOGGLENOVIEW;
}
}
/// Internal crate-specific functionality common to all [PdfFormField] objects.
pub trait PdfFormFieldPrivate<'a>: PdfFormFieldCommon {
/// Returns the internal `FPDF_FORMHANDLE` handle for this [PdfFormField].
fn form_handle(&self) -> &FPDF_FORMHANDLE;
/// Returns the internal `FPDF_ANNOTATION` handle for this [PdfFormField].
fn annotation_handle(&self) -> &FPDF_ANNOTATION;
/// Returns the [PdfiumLibraryBindings] used by this [PdfFormField].
fn bindings(&self) -> &dyn PdfiumLibraryBindings;
/// Internal implementation of [PdfFormFieldCommon::name()].
fn name_impl(&self) -> Option<String> {
// Retrieving the field name from Pdfium is a two-step operation. First, we call
// FPDFAnnot_GetFormFieldName() with a null buffer; this will retrieve the length of
// the field name text in bytes. If the length is zero, then the field name is not set.
// If the length is non-zero, then we reserve a byte buffer of the given
// length and call FPDFAnnot_GetFormFieldName() again with a pointer to the buffer;
// this will write the field name to the buffer in UTF16LE format.
let buffer_length = self.bindings().FPDFAnnot_GetFormFieldName(
*self.form_handle(),
*self.annotation_handle(),
std::ptr::null_mut(),
0,
);
if buffer_length == 0 {
// The field name is not present.
None
} else {
let mut buffer = create_byte_buffer(buffer_length as usize);
let result = self.bindings().FPDFAnnot_GetFormFieldName(
*self.form_handle(),
*self.annotation_handle(),
buffer.as_mut_ptr() as *mut FPDF_WCHAR,
buffer_length,
);
debug_assert_eq!(result, buffer_length);
get_string_from_pdfium_utf16le_bytes(buffer)
}
}
/// Internal implementation of `value()` function shared by value-carrying form field widgets
/// such as text fields. Not exposed directly by [PdfFormFieldCommon].
fn value_impl(&self) -> Option<String> {
// Retrieving the field value from Pdfium is a two-step operation. First, we call
// FPDFAnnot_GetFormFieldValue() with a null buffer; this will retrieve the length of
// the form value text in bytes. If the length is zero, then the form value is not set.
// If the length is non-zero, then we reserve a byte buffer of the given
// length and call FPDFAnnot_GetFormFieldValue() again with a pointer to the buffer;
// this will write the field value to the buffer in UTF16LE format.
let buffer_length = self.bindings().FPDFAnnot_GetFormFieldValue(
*self.form_handle(),
*self.annotation_handle(),
std::ptr::null_mut(),
0,
);
if buffer_length == 0 {
// The field value is not present.
None
} else {
let mut buffer = create_byte_buffer(buffer_length as usize);
let result = self.bindings().FPDFAnnot_GetFormFieldValue(
*self.form_handle(),
*self.annotation_handle(),
buffer.as_mut_ptr() as *mut FPDF_WCHAR,
buffer_length,
);
debug_assert_eq!(result, buffer_length);
get_string_from_pdfium_utf16le_bytes(buffer)
}
}
/// Internal implementation of `value()` function shared by on/off form field widgets
/// such as checkbox and radio button fields. Not exposed directly by [PdfFormFieldCommon].
fn export_value_impl(&self) -> Option<String> {
// Retrieving the export value from Pdfium is a two-step operation. First, we call
// FPDFAnnot_GetFormFieldExportValue() with a null buffer; this will retrieve the length of
// the export value text in bytes. If the length is zero, then the export value is not set.
// If the length is non-zero, then we reserve a byte buffer of the given
// length and call FPDFAnnot_GetFormFieldExportValue() again with a pointer to the buffer;
// this will write the export value to the buffer in UTF16LE format.
let buffer_length = self.bindings().FPDFAnnot_GetFormFieldExportValue(
*self.form_handle(),
*self.annotation_handle(),
std::ptr::null_mut(),
0,
);
if buffer_length == 0 {
// The field value is not present.
None
} else {
let mut buffer = create_byte_buffer(buffer_length as usize);
let result = self.bindings().FPDFAnnot_GetFormFieldExportValue(
*self.form_handle(),
*self.annotation_handle(),
buffer.as_mut_ptr() as *mut FPDF_WCHAR,
buffer_length,
);
debug_assert_eq!(result, buffer_length);
get_string_from_pdfium_utf16le_bytes(buffer)
}
}
/// Internal implementation of `is_checked()` function shared by checkable form field widgets
/// such as radio buttons and checkboxes. Not exposed directly by [PdfFormFieldCommon].
fn is_checked_impl(&self) -> Result<bool, PdfiumError> {
Ok(self.bindings().is_true(
self.bindings()
.FPDFAnnot_IsChecked(*self.form_handle(), *self.annotation_handle()),
))
}
/// Internal implementation of `index_in_group()` function shared by checkable form field
/// widgets such as radio buttons and checkboxes. Not exposed directly by [PdfFormFieldCommon].
fn index_in_group_impl(&self) -> u32 {
let result = self
.bindings()
.FPDFAnnot_GetFormControlIndex(*self.form_handle(), *self.annotation_handle());
if result < 0 {
// Pdfium uses a -1 value to signal an error.
0
} else {
result as u32
}
}
/// Internal implementation of [PdfFormFieldCommon::appearance_mode_value()].
fn appearance_mode_value_impl(&self, appearance_mode: PdfAppearanceMode) -> Option<String> {
// Retrieving the appearance mode value from Pdfium is a two-step operation.
// First, we call FPDFAnnot_GetAP() with a null buffer; this will retrieve the length of
// the appearance mode value text in bytes. If the length is zero, then the
// appearance mode value is not set.
// If the length is non-zero, then we reserve a byte buffer of the given
// length and call FPDFAnnot_GetAP() again with a pointer to the buffer;
// this will write the appearance mode value to the buffer in UTF16LE format.
let buffer_length = self.bindings().FPDFAnnot_GetAP(
*self.annotation_handle(),
appearance_mode.as_pdfium(),
std::ptr::null_mut(),
0,
);
if buffer_length == 0 {
// The appearance mode value is not present.
None
} else {
let mut buffer = create_byte_buffer(buffer_length as usize);
let result = self.bindings().FPDFAnnot_GetAP(
*self.annotation_handle(),
appearance_mode.as_pdfium(),
buffer.as_mut_ptr() as *mut FPDF_WCHAR,
buffer_length,
);
debug_assert_eq!(result, buffer_length);
get_string_from_pdfium_utf16le_bytes(buffer)
}
}
/// Returns the currently set appearance stream for this form field, if any.
fn appearance_stream_impl(&self) -> Option<String> {
// Retrieving the appearance stream value from Pdfium is a two-step operation.
// First, we call FPDFAnnot_GetStringValue() with a null buffer; this will retrieve
// the length of the appearance stream value text in bytes. If the length is zero,
// then the appearance stream value is not set.
// If the length is non-zero, then we reserve a byte buffer of the given
// length and call FPDFAnnot_GetStringValue() again with a pointer to the buffer;
// this will write the appearance stream value to the buffer in UTF16LE format.
let buffer_length = self.bindings().FPDFAnnot_GetStringValue(
*self.annotation_handle(),
"AS",
std::ptr::null_mut(),
0,
);
if buffer_length == 0 {
// The appearance mode value is not present.
None
} else {
let mut buffer = create_byte_buffer(buffer_length as usize);
let result = self.bindings().FPDFAnnot_GetStringValue(
*self.annotation_handle(),
"AS",
buffer.as_mut_ptr() as *mut FPDF_WCHAR,
buffer_length,
);
debug_assert_eq!(result, buffer_length);
get_string_from_pdfium_utf16le_bytes(buffer)
}
}
#[inline]
fn form_field_flags_impl(&self) -> FpdfAnnotationFlags {
FpdfAnnotationFlags::from_bits_truncate(
self.bindings()
.FPDFAnnot_GetFormFieldFlags(*self.form_handle(), *self.annotation_handle())
as u32,
)
}
#[inline]
fn flags_impl(&self) -> FpdfAnnotationFlags {
FpdfAnnotationFlags::from_bits_truncate(
self.bindings()
.FPDFAnnot_GetFlags(*self.annotation_handle()) as u32,
)
}
#[inline]
fn set_flags_impl(&mut self, flags: FpdfAnnotationFlags) -> bool {
self.bindings().is_true(
self.bindings()
.FPDFAnnot_SetFlags(*self.annotation_handle(), flags.bits() as c_int),
)
}
}
}