firecrawl_pdfium/
forms.rs1use std::ffi::c_int;
5
6use crate::error::{Error, Result};
7use crate::library::Pdfium;
8use crate::sys;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13#[non_exhaustive]
14pub enum FormType {
15 None,
17 AcroForm,
19 XfaFull,
22 XfaForeground,
24 Unknown(i32),
26}
27
28impl FormType {
29 pub(crate) fn from_raw(raw: c_int) -> FormType {
30 match raw {
31 sys::FORMTYPE_NONE => FormType::None,
32 sys::FORMTYPE_ACRO_FORM => FormType::AcroForm,
33 sys::FORMTYPE_XFA_FULL => FormType::XfaFull,
34 sys::FORMTYPE_XFA_FOREGROUND => FormType::XfaForeground,
35 other => FormType::Unknown(other),
36 }
37 }
38
39 pub fn is_renderable(&self) -> bool {
42 matches!(self, FormType::AcroForm)
43 }
44}
45
46pub(crate) struct FormEnv {
53 info: Box<sys::FPDF_FORMFILLINFO>,
55 handle: sys::FPDF_FORMHANDLE,
56}
57
58unsafe impl Send for FormEnv {}
63unsafe impl Sync for FormEnv {}
64
65impl FormEnv {
66 pub(crate) fn new(pdfium: Pdfium, document: sys::FPDF_DOCUMENT) -> Result<FormEnv> {
72 let mut info = Box::new(new_formfillinfo());
73 let handle = pdfium.ffi(|b|
74 unsafe { b.FPDFDOC_InitFormFillEnvironment(document, info.as_mut()) });
79 if handle.is_null() {
80 return Err(Error::FormInitFailed);
81 }
82 Ok(FormEnv { info, handle })
83 }
84
85 pub(crate) fn handle(&self) -> sys::FPDF_FORMHANDLE {
86 self.handle
87 }
88
89 pub(crate) fn destroy(&mut self, bindings: &sys::Bindings) {
93 if !self.handle.is_null() {
94 unsafe { bindings.FPDFDOC_ExitFormFillEnvironment(self.handle) };
99 self.handle = std::ptr::null_mut();
100 }
101 let _ = &self.info; }
103}
104
105fn new_formfillinfo() -> sys::FPDF_FORMFILLINFO {
118 sys::FPDF_FORMFILLINFO {
119 version: 1,
120 Release: None,
121 FFI_Invalidate: Some(ffi_invalidate),
122 FFI_OutputSelectedRect: None,
123 FFI_SetCursor: Some(ffi_set_cursor),
124 FFI_SetTimer: Some(ffi_set_timer),
125 FFI_KillTimer: Some(ffi_kill_timer),
126 FFI_GetLocalTime: Some(ffi_get_local_time),
127 FFI_OnChange: None,
128 FFI_GetPage: Some(ffi_get_page),
129 FFI_GetCurrentPage: Some(ffi_get_current_page),
130 FFI_GetRotation: Some(ffi_get_rotation),
131 FFI_ExecuteNamedAction: Some(ffi_execute_named_action),
132 FFI_SetTextFieldFocus: None,
133 FFI_DoURIAction: None,
134 FFI_DoGoToAction: None,
135 m_pJsPlatform: std::ptr::null_mut(),
136 xfa_disabled: 0,
137 FFI_DisplayCaret: None,
138 FFI_GetCurrentPageIndex: None,
139 FFI_SetCurrentPage: None,
140 FFI_GotoURL: None,
141 FFI_GetPageViewRect: None,
142 FFI_PageEvent: None,
143 FFI_PopupMenu: None,
144 FFI_OpenFile: None,
145 FFI_EmailTo: None,
146 FFI_UploadTo: None,
147 FFI_GetPlatform: None,
148 FFI_GetLanguage: None,
149 FFI_DownloadFromURL: None,
150 FFI_PostRequestURL: None,
151 FFI_PutRequestURL: None,
152 FFI_OnFocusChange: None,
153 FFI_DoURIActionWithKeyboardModifier: None,
154 }
155}
156
157unsafe extern "C" fn ffi_invalidate(
158 _this: *mut sys::FPDF_FORMFILLINFO,
159 _page: sys::FPDF_PAGE,
160 _left: f64,
161 _top: f64,
162 _right: f64,
163 _bottom: f64,
164) {
165 }
167
168unsafe extern "C" fn ffi_set_cursor(_this: *mut sys::FPDF_FORMFILLINFO, _cursor: c_int) {}
169
170unsafe extern "C" fn ffi_set_timer(
171 _this: *mut sys::FPDF_FORMFILLINFO,
172 _elapse: c_int,
173 _timer_func: sys::TimerCallback,
174) -> c_int {
175 0
178}
179
180unsafe extern "C" fn ffi_kill_timer(_this: *mut sys::FPDF_FORMFILLINFO, _timer_id: c_int) {}
181
182unsafe extern "C" fn ffi_get_local_time(
183 _this: *mut sys::FPDF_FORMFILLINFO,
184) -> sys::FPDF_SYSTEMTIME {
185 sys::FPDF_SYSTEMTIME::default()
186}
187
188unsafe extern "C" fn ffi_get_page(
189 _this: *mut sys::FPDF_FORMFILLINFO,
190 _document: sys::FPDF_DOCUMENT,
191 _page_index: c_int,
192) -> sys::FPDF_PAGE {
193 std::ptr::null_mut()
195}
196
197unsafe extern "C" fn ffi_get_current_page(
198 _this: *mut sys::FPDF_FORMFILLINFO,
199 _document: sys::FPDF_DOCUMENT,
200) -> sys::FPDF_PAGE {
201 std::ptr::null_mut()
202}
203
204unsafe extern "C" fn ffi_get_rotation(
205 _this: *mut sys::FPDF_FORMFILLINFO,
206 _page: sys::FPDF_PAGE,
207) -> c_int {
208 0
209}
210
211unsafe extern "C" fn ffi_execute_named_action(
212 _this: *mut sys::FPDF_FORMFILLINFO,
213 _named_action: sys::FPDF_BYTESTRING,
214) {
215}