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
use crate::bindgen::{FPDF_ANNOTATION, FPDF_FORMHANDLE, FPDF_WCHAR};
use crate::bindings::PdfiumLibraryBindings;
use crate::error::PdfiumError;
use crate::form_field_option::PdfFormFieldOption;
use crate::utils::mem::create_byte_buffer;
use crate::utils::utf16le::get_string_from_pdfium_utf16le_bytes;
use std::ops::{Range, RangeInclusive};
use std::os::raw::c_int;
pub type PdfFormFieldOptionIndex = usize;
pub struct PdfFormFieldOptions<'a> {
form_handle: FPDF_FORMHANDLE,
annotation_handle: FPDF_ANNOTATION,
bindings: &'a dyn PdfiumLibraryBindings,
}
impl<'a> PdfFormFieldOptions<'a> {
#[inline]
pub(crate) fn from_pdfium(
form_handle: FPDF_FORMHANDLE,
annotation_handle: FPDF_ANNOTATION,
bindings: &'a dyn PdfiumLibraryBindings,
) -> Self {
PdfFormFieldOptions {
form_handle,
annotation_handle,
bindings,
}
}
#[inline]
pub fn bindings(&self) -> &'a dyn PdfiumLibraryBindings {
self.bindings
}
pub fn len(&self) -> PdfFormFieldOptionIndex {
let result = self
.bindings
.FPDFAnnot_GetOptionCount(self.form_handle, self.annotation_handle);
if result == -1 {
0
} else {
result as PdfFormFieldOptionIndex
}
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn as_range(&self) -> Range<PdfFormFieldOptionIndex> {
0..self.len()
}
#[inline]
pub fn as_range_inclusive(&self) -> RangeInclusive<PdfFormFieldOptionIndex> {
if self.is_empty() {
0..=0
} else {
0..=(self.len() - 1)
}
}
pub fn get(&self, index: PdfFormFieldOptionIndex) -> Result<PdfFormFieldOption, PdfiumError> {
if index >= self.len() {
return Err(PdfiumError::FormFieldOptionIndexOutOfBounds);
}
let buffer_length = self.bindings().FPDFAnnot_GetOptionLabel(
self.form_handle,
self.annotation_handle,
index as c_int,
std::ptr::null_mut(),
0,
);
let option_label = if buffer_length == 0 {
None
} else {
let mut buffer = create_byte_buffer(buffer_length as usize);
let result = self.bindings().FPDFAnnot_GetOptionLabel(
self.form_handle,
self.annotation_handle,
index as c_int,
buffer.as_mut_ptr() as *mut FPDF_WCHAR,
buffer_length,
);
debug_assert_eq!(result, buffer_length);
get_string_from_pdfium_utf16le_bytes(buffer)
};
let option_is_set = self
.bindings
.is_true(self.bindings.FPDFAnnot_IsOptionSelected(
self.form_handle,
self.annotation_handle,
index as c_int,
));
Ok(PdfFormFieldOption::new(index, option_is_set, option_label))
}
#[inline]
pub fn iter(&self) -> PdfFormFieldOptionsIterator {
PdfFormFieldOptionsIterator::new(self)
}
}
pub struct PdfFormFieldOptionsIterator<'a> {
options: &'a PdfFormFieldOptions<'a>,
next_index: PdfFormFieldOptionIndex,
}
impl<'a> PdfFormFieldOptionsIterator<'a> {
#[inline]
pub(crate) fn new(options: &'a PdfFormFieldOptions<'a>) -> Self {
PdfFormFieldOptionsIterator {
options,
next_index: 0,
}
}
}
impl<'a> Iterator for PdfFormFieldOptionsIterator<'a> {
type Item = PdfFormFieldOption;
fn next(&mut self) -> Option<Self::Item> {
let next = self.options.get(self.next_index);
self.next_index += 1;
next.ok()
}
}