blob: 4b23ce41b0e6512c36f6bc9114d5fe3d99533123 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_formfill.h"
8
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08009#include <memory>
Tom Sepezab277682016-02-17 10:07:21 -080010#include <vector>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080011
dsinclair41872fa2016-10-04 11:29:35 -070012#include "core/fpdfapi/page/cpdf_page.h"
Lei Zhang81535612018-10-09 21:15:17 +000013#include "core/fpdfapi/parser/cpdf_dictionary.h"
dsinclair488b7ad2016-10-04 11:55:50 -070014#include "core/fpdfapi/parser/cpdf_document.h"
dsinclair69d9c682016-10-04 12:18:35 -070015#include "core/fpdfapi/render/cpdf_renderoptions.h"
dsinclair1727aee2016-09-29 13:12:56 -070016#include "core/fpdfdoc/cpdf_formcontrol.h"
17#include "core/fpdfdoc/cpdf_formfield.h"
Lei Zhangc3450652018-10-11 16:54:42 +000018#include "core/fpdfdoc/cpdf_interactiveform.h"
dsinclair1727aee2016-09-29 13:12:56 -070019#include "core/fpdfdoc/cpdf_occontext.h"
Dan Sinclaira5085d42017-05-11 16:26:50 -040020#include "core/fxge/cfx_defaultrenderdevice.h"
Dan Sinclair7d125322018-03-28 18:49:34 +000021#include "fpdfsdk/cpdfsdk_actionhandler.h"
dsinclair735606d2016-10-05 15:47:02 -070022#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
Dan Sinclair00d47a62018-03-28 18:39:04 +000023#include "fpdfsdk/cpdfsdk_helpers.h"
Lei Zhangc3450652018-10-11 16:54:42 +000024#include "fpdfsdk/cpdfsdk_interactiveform.h"
dsinclair114e46a2016-09-29 17:18:21 -070025#include "fpdfsdk/cpdfsdk_pageview.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080026#include "public/fpdfview.h"
tsepez36eb4bd2016-10-03 15:24:27 -070027#include "third_party/base/ptr_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028
Tom Sepez40e9ff32015-11-30 12:39:54 -080029#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070030#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair4d29e782016-10-04 14:02:47 -070031#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
Dan Sinclair80c48782017-03-23 12:11:20 -040032#include "xfa/fxfa/cxfa_ffdocview.h"
33#include "xfa/fxfa/cxfa_ffpageview.h"
34#include "xfa/fxfa/cxfa_ffwidget.h"
Dan Sinclaircdba7472017-03-23 09:17:10 -040035
Ryan Harrisonc3cc2ab2018-06-21 21:09:54 +000036static_assert(static_cast<int>(AlertButton::kDefault) ==
37 JSPLATFORM_ALERT_BUTTON_DEFAULT,
38 "Default alert button types must match");
39static_assert(static_cast<int>(AlertButton::kOK) == JSPLATFORM_ALERT_BUTTON_OK,
40 "OK alert button types must match");
41static_assert(static_cast<int>(AlertButton::kOKCancel) ==
42 JSPLATFORM_ALERT_BUTTON_OKCANCEL,
43 "OKCancel alert button types must match");
44static_assert(static_cast<int>(AlertButton::kYesNo) ==
45 JSPLATFORM_ALERT_BUTTON_YESNO,
46 "YesNo alert button types must match");
47static_assert(static_cast<int>(AlertButton::kYesNoCancel) ==
48 JSPLATFORM_ALERT_BUTTON_YESNOCANCEL,
49 "YesNoCancel alert button types must match");
50
51static_assert(static_cast<int>(AlertIcon::kDefault) ==
52 JSPLATFORM_ALERT_ICON_DEFAULT,
53 "Default alert icon types must match");
54static_assert(static_cast<int>(AlertIcon::kError) ==
55 JSPLATFORM_ALERT_ICON_ERROR,
56 "Error alert icon types must match");
57static_assert(static_cast<int>(AlertIcon::kWarning) ==
58 JSPLATFORM_ALERT_ICON_WARNING,
59 "Warning alert icon types must match");
60static_assert(static_cast<int>(AlertIcon::kQuestion) ==
61 JSPLATFORM_ALERT_ICON_QUESTION,
62 "Question alert icon types must match");
63static_assert(static_cast<int>(AlertIcon::kStatus) ==
64 JSPLATFORM_ALERT_ICON_STATUS,
65 "Status alert icon types must match");
66static_assert(static_cast<int>(AlertIcon::kAsterisk) ==
67 JSPLATFORM_ALERT_ICON_ASTERISK,
68 "Asterisk alert icon types must match");
69
70static_assert(static_cast<int>(AlertReturn::kOK) == JSPLATFORM_ALERT_RETURN_OK,
71 "OK alert return types must match");
72static_assert(static_cast<int>(AlertReturn::kCancel) ==
73 JSPLATFORM_ALERT_RETURN_CANCEL,
74 "Cancel alert return types must match");
75static_assert(static_cast<int>(AlertReturn::kNo) == JSPLATFORM_ALERT_RETURN_NO,
76 "No alert return types must match");
77static_assert(static_cast<int>(AlertReturn::kYes) ==
78 JSPLATFORM_ALERT_RETURN_YES,
79 "Yes alert return types must match");
80
Ryan Harrison854d71c2017-10-18 12:28:14 -040081static_assert(static_cast<int>(FormType::kNone) == FORMTYPE_NONE,
82 "None form types must match");
83static_assert(static_cast<int>(FormType::kAcroForm) == FORMTYPE_ACRO_FORM,
84 "AcroForm form types must match");
85static_assert(static_cast<int>(FormType::kXFAFull) == FORMTYPE_XFA_FULL,
86 "XFA full form types must match");
87static_assert(static_cast<int>(FormType::kXFAForeground) ==
88 FORMTYPE_XFA_FOREGROUND,
89 "XFA foreground form types must match");
Tom Sepez40e9ff32015-11-30 12:39:54 -080090#endif // PDF_ENABLE_XFA
91
Ryan Harrison9baf31f2018-01-12 18:36:30 +000092static_assert(static_cast<int>(FormFieldType::kUnknown) ==
93 FPDF_FORMFIELD_UNKNOWN,
94 "Unknown form field types must match");
95static_assert(static_cast<int>(FormFieldType::kPushButton) ==
96 FPDF_FORMFIELD_PUSHBUTTON,
97 "PushButton form field types must match");
98static_assert(static_cast<int>(FormFieldType::kCheckBox) ==
99 FPDF_FORMFIELD_CHECKBOX,
100 "CheckBox form field types must match");
101static_assert(static_cast<int>(FormFieldType::kRadioButton) ==
102 FPDF_FORMFIELD_RADIOBUTTON,
103 "RadioButton form field types must match");
104static_assert(static_cast<int>(FormFieldType::kComboBox) ==
105 FPDF_FORMFIELD_COMBOBOX,
106 "ComboBox form field types must match");
107static_assert(static_cast<int>(FormFieldType::kListBox) ==
108 FPDF_FORMFIELD_LISTBOX,
109 "ListBox form field types must match");
110static_assert(static_cast<int>(FormFieldType::kTextField) ==
111 FPDF_FORMFIELD_TEXTFIELD,
112 "TextField form field types must match");
113static_assert(static_cast<int>(FormFieldType::kSignature) ==
114 FPDF_FORMFIELD_SIGNATURE,
115 "Signature form field types must match");
116#ifdef PDF_ENABLE_XFA
117static_assert(static_cast<int>(FormFieldType::kXFA) == FPDF_FORMFIELD_XFA,
118 "XFA form field types must match");
Ryan Harrison2056fac2018-01-16 16:06:45 +0000119static_assert(static_cast<int>(FormFieldType::kXFA_CheckBox) ==
120 FPDF_FORMFIELD_XFA_CHECKBOX,
121 "XFA CheckBox form field types must match");
122static_assert(static_cast<int>(FormFieldType::kXFA_ComboBox) ==
123 FPDF_FORMFIELD_XFA_COMBOBOX,
124 "XFA ComboBox form field types must match");
125static_assert(static_cast<int>(FormFieldType::kXFA_ImageField) ==
126 FPDF_FORMFIELD_XFA_IMAGEFIELD,
127 "XFA ImageField form field types must match");
128static_assert(static_cast<int>(FormFieldType::kXFA_ListBox) ==
129 FPDF_FORMFIELD_XFA_LISTBOX,
130 "XFA ListBox form field types must match");
131static_assert(static_cast<int>(FormFieldType::kXFA_PushButton) ==
132 FPDF_FORMFIELD_XFA_PUSHBUTTON,
133 "XFA PushButton form field types must match");
134static_assert(static_cast<int>(FormFieldType::kXFA_Signature) ==
135 FPDF_FORMFIELD_XFA_SIGNATURE,
136 "XFA Signature form field types must match");
137static_assert(static_cast<int>(FormFieldType::kXFA_TextField) ==
138 FPDF_FORMFIELD_XFA_TEXTFIELD,
139 "XFA TextField form field types must match");
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000140#endif // PDF_ENABLE_XFA
141static_assert(kFormFieldTypeCount == FPDF_FORMFIELD_COUNT,
142 "Number of form field types must match");
143
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700144namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700145
Lei Zhang073ecf42018-10-11 16:56:00 +0000146CPDFSDK_InteractiveForm* FormHandleToInteractiveForm(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700147 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000148 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
Lei Zhang073ecf42018-10-11 16:56:00 +0000149 return pFormFillEnv ? pFormFillEnv->GetInteractiveForm() : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700150}
Bo Xufdc00a72014-10-28 23:03:33 -0700151
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152CPDFSDK_PageView* FormHandleToPageView(FPDF_FORMHANDLE hHandle,
Tom Sepezdae73f52018-06-13 17:54:57 +0000153 FPDF_PAGE fpdf_page) {
154 IPDF_Page* pPage = IPDFPageFromFPDFPage(fpdf_page);
Tom Sepez540c4362015-11-24 13:33:57 -0800155 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 return nullptr;
Tom Sepez0b133982015-07-28 11:23:22 -0700157
dsinclairf3fbe832016-10-11 13:08:04 -0700158 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000159 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
dsinclair7cbe68e2016-10-12 11:56:23 -0700160 return pFormFillEnv ? pFormFillEnv->GetPageView(pPage, true) : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700161}
Bo Xufdc00a72014-10-28 23:03:33 -0700162
thestigbefa4502016-05-26 20:15:19 -0700163void FFLCommon(FPDF_FORMHANDLE hHandle,
164 FPDF_BITMAP bitmap,
165 FPDF_RECORDER recorder,
Tom Sepezdae73f52018-06-13 17:54:57 +0000166 FPDF_PAGE fpdf_page,
thestigbefa4502016-05-26 20:15:19 -0700167 int start_x,
168 int start_y,
169 int size_x,
170 int size_y,
171 int rotate,
172 int flags) {
173 if (!hHandle)
174 return;
175
Tom Sepezdae73f52018-06-13 17:54:57 +0000176 IPDF_Page* pPage = IPDFPageFromFPDFPage(fpdf_page);
thestigbefa4502016-05-26 20:15:19 -0700177 if (!pPage)
178 return;
179
Tom Sepezca0273392018-05-25 23:37:50 +0000180 CPDF_Document* pPDFDoc = pPage->GetDocument();
Tom Sepezdae73f52018-06-13 17:54:57 +0000181 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, fpdf_page);
thestigbefa4502016-05-26 20:15:19 -0700182
Lei Zhangc4242b22018-04-12 15:50:49 +0000183 const FX_RECT rect(start_x, start_y, start_x + size_x, start_y + size_y);
184 CFX_Matrix matrix = pPage->GetDisplayMatrix(rect, rotate);
thestigbefa4502016-05-26 20:15:19 -0700185
Dan Sinclaira5085d42017-05-11 16:26:50 -0400186 auto pDevice = pdfium::MakeUnique<CFX_DefaultRenderDevice>();
thestigbefa4502016-05-26 20:15:19 -0700187#ifdef _SKIA_SUPPORT_
188 pDevice->AttachRecorder(static_cast<SkPictureRecorder*>(recorder));
189#endif
Tom Sepezdae73f52018-06-13 17:54:57 +0000190
Tom Sepez525147a2018-05-03 17:19:53 +0000191 RetainPtr<CFX_DIBitmap> holder(CFXDIBitmapFromFPDFBitmap(bitmap));
Tom Sepezf0799fe2017-03-28 09:31:32 -0700192 pDevice->Attach(holder, false, nullptr, false);
Tom Sepez1629f602017-04-21 14:11:26 -0700193 {
194 CFX_RenderDevice::StateRestorer restorer(pDevice.get());
Lei Zhangc4242b22018-04-12 15:50:49 +0000195 pDevice->SetClip_Rect(rect);
thestigbefa4502016-05-26 20:15:19 -0700196
Tom Sepez1629f602017-04-21 14:11:26 -0700197 CPDF_RenderOptions options;
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400198 uint32_t option_flags = options.GetFlags();
Tom Sepez1629f602017-04-21 14:11:26 -0700199 if (flags & FPDF_LCD_TEXT)
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400200 option_flags |= RENDER_CLEARTYPE;
Tom Sepez1629f602017-04-21 14:11:26 -0700201 else
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400202 option_flags &= ~RENDER_CLEARTYPE;
203 options.SetFlags(option_flags);
thestigbefa4502016-05-26 20:15:19 -0700204
Tom Sepez1629f602017-04-21 14:11:26 -0700205 // Grayscale output
Dan Sinclairf55e72e2017-07-13 14:53:28 -0400206 if (flags & FPDF_GRAYSCALE)
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400207 options.SetColorMode(CPDF_RenderOptions::kGray);
Dan Sinclairf55e72e2017-07-13 14:53:28 -0400208
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400209 options.SetDrawAnnots(flags & FPDF_ANNOT);
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400210 options.SetOCContext(
211 pdfium::MakeRetain<CPDF_OCContext>(pPDFDoc, CPDF_OCContext::View));
Tom Sepezca0273392018-05-25 23:37:50 +0000212
Tom Sepez45a55242018-05-25 23:57:13 +0000213 if (pPageView)
Lei Zhang09ee0872018-10-09 19:13:55 +0000214 pPageView->PageView_OnDraw(pDevice.get(), matrix, &options, rect);
Tom Sepez1629f602017-04-21 14:11:26 -0700215 }
Tom Sepezdae73f52018-06-13 17:54:57 +0000216
caryclark8f875502016-12-06 13:49:34 -0800217#ifdef _SKIA_SUPPORT_PATHS_
Cary Clark364d18b2017-07-25 10:39:10 -0400218 pDevice->Flush(true);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700219 holder->UnPreMultiply();
caryclark687fbde2016-11-22 12:44:25 -0800220#endif
thestigbefa4502016-05-26 20:15:19 -0700221}
222
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700223} // namespace
Bo Xufdc00a72014-10-28 23:03:33 -0700224
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400225FPDF_EXPORT int FPDF_CALLCONV
226FPDFPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
227 FPDF_PAGE page,
228 double page_x,
229 double page_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700230 if (!hHandle)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700231 return -1;
Tom Sepezdb0be962015-10-16 14:00:21 -0700232 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 if (pPage) {
Lei Zhang073ecf42018-10-11 16:56:00 +0000234 CPDF_InteractiveForm interactive_form(pPage->GetDocument());
235 CPDF_FormControl* pFormCtrl = interactive_form.GetControlAtPoint(
Dan Sinclair05df0752017-03-14 14:43:42 -0400236 pPage,
237 CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)),
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500238 nullptr);
Lei Zhangbdf72c32015-08-14 19:24:08 -0700239 if (!pFormCtrl)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 CPDF_FormField* pFormField = pFormCtrl->GetField();
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000242 return pFormField ? static_cast<int>(pFormField->GetFieldType()) : -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 }
244
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800245#ifdef PDF_ENABLE_XFA
Tom Sepez101535f2018-06-12 13:36:05 +0000246 CPDFXFA_Page* pXFAPage = ToXFAPage(IPDFPageFromFPDFPage(page));
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800247 if (!pXFAPage)
248 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249
dsinclairdf4bc592016-03-31 20:34:43 -0700250 CXFA_FFPageView* pPageView = pXFAPage->GetXFAPageView();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800251 if (!pPageView)
252 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253
dsinclairdf4bc592016-03-31 20:34:43 -0700254 CXFA_FFDocView* pDocView = pPageView->GetDocView();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800255 if (!pDocView)
256 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257
dsinclairdf4bc592016-03-31 20:34:43 -0700258 CXFA_FFWidgetHandler* pWidgetHandler = pDocView->GetWidgetHandler();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800259 if (!pWidgetHandler)
260 return -1;
261
tsepezcc4d6d82016-05-16 13:21:03 -0700262 std::unique_ptr<IXFA_WidgetIterator> pWidgetIterator(
dsinclair935d8d52016-05-17 10:32:18 -0700263 pPageView->CreateWidgetIterator(XFA_TRAVERSEWAY_Form,
264 XFA_WidgetStatus_Viewable));
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800265 if (!pWidgetIterator)
266 return -1;
267
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000268 CXFA_FFWidget* pXFAAnnot;
269 while ((pXFAAnnot = pWidgetIterator->MoveToNext()) != nullptr) {
Henrique Nakashima1087ebc2018-02-16 19:31:39 +0000270 if (pXFAAnnot->GetFormFieldType() == FormFieldType::kXFA)
271 continue;
272
273 CFX_RectF rcBBox = pXFAAnnot->GetWidgetRect();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800274 CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width,
275 rcBBox.top + rcBBox.height);
Lei Zhang1fed5a22017-06-01 11:52:22 -0700276 rcWidget.Inflate(1.0f, 1.0f);
Dan Sinclair05df0752017-03-14 14:43:42 -0400277 if (rcWidget.Contains(CFX_PointF(static_cast<float>(page_x),
278 static_cast<float>(page_y)))) {
Ryan Harrison2056fac2018-01-16 16:06:45 +0000279 return static_cast<int>(pXFAAnnot->GetFormFieldType());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800282#endif // PDF_ENABLE_XFA
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800283 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700284}
285
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400286FPDF_EXPORT int FPDF_CALLCONV
287FPDFPage_FormFieldZOrderAtPoint(FPDF_FORMHANDLE hHandle,
288 FPDF_PAGE page,
289 double page_x,
290 double page_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700291 if (!hHandle)
Lei Zhangbdf72c32015-08-14 19:24:08 -0700292 return -1;
Tom Sepezdb0be962015-10-16 14:00:21 -0700293 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
294 if (!pPage)
295 return -1;
Lei Zhang073ecf42018-10-11 16:56:00 +0000296 CPDF_InteractiveForm interactive_form(pPage->GetDocument());
Lei Zhangbdf72c32015-08-14 19:24:08 -0700297 int z_order = -1;
Lei Zhang073ecf42018-10-11 16:56:00 +0000298 (void)interactive_form.GetControlAtPoint(
Dan Sinclair05df0752017-03-14 14:43:42 -0400299 pPage, CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)),
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000300 &z_order);
Lei Zhangbdf72c32015-08-14 19:24:08 -0700301 return z_order;
302}
303
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400304FPDF_EXPORT FPDF_FORMHANDLE FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document,
306 FPDF_FORMFILLINFO* formInfo) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800307#ifdef PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800308 const int kRequiredVersion = 2;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800309#else // PDF_ENABLE_XFA
310 const int kRequiredVersion = 1;
311#endif // PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800312 if (!formInfo || formInfo->version != kRequiredVersion)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700314
Tom Sepezfe06d512018-05-01 17:25:25 +0000315 auto* pDocument = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -0800316 if (!pDocument)
317 return nullptr;
318
Tom Sepez40e9ff32015-11-30 12:39:54 -0800319#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -0700320 // If the CPDFXFA_Context has a FormFillEnvironment already then we've done
dsinclair8779fa82016-10-12 12:05:44 -0700321 // this and can just return the old Env. Otherwise, we'll end up setting a new
322 // environment into the XFADocument and, that could get weird.
Tom Sepezfe06d512018-05-01 17:25:25 +0000323 auto* pContext = static_cast<CPDFXFA_Context*>(pDocument->GetExtension());
Tom Sepez2563fc32018-08-01 19:13:06 +0000324 if (pContext && pContext->GetFormFillEnv()) {
325 return FPDFFormHandleFromCPDFSDKFormFillEnvironment(
326 pContext->GetFormFillEnv());
327 }
dsinclaira939bfe2016-09-22 13:18:45 -0700328#endif
329
Tom Sepezda8063f2018-04-30 17:41:49 +0000330 auto pFormFillEnv = pdfium::MakeUnique<CPDFSDK_FormFillEnvironment>(
331 CPDFDocumentFromFPDFDocument(document), formInfo);
dsinclaira939bfe2016-09-22 13:18:45 -0700332
333#ifdef PDF_ENABLE_XFA
Tom Sepez3f3c39d2018-05-01 17:46:34 +0000334 if (pContext)
335 pContext->SetFormFillEnv(pFormFillEnv.get());
Tom Sepez40e9ff32015-11-30 12:39:54 -0800336#endif // PDF_ENABLE_XFA
dsinclaira939bfe2016-09-22 13:18:45 -0700337
Tom Sepez2563fc32018-08-01 19:13:06 +0000338 return FPDFFormHandleFromCPDFSDKFormFillEnvironment(
339 pFormFillEnv.release()); // Caller takes ownership.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700340}
341
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400342FPDF_EXPORT void FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700344 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000345 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
Lei Zhang1fed5a22017-06-01 11:52:22 -0700346 if (!pFormFillEnv)
347 return;
dsinclaira939bfe2016-09-22 13:18:45 -0700348
Tom Sepez40e9ff32015-11-30 12:39:54 -0800349#ifdef PDF_ENABLE_XFA
dsinclair21161052016-10-05 15:46:15 -0700350 // Reset the focused annotations and remove the SDK document from the
351 // XFA document.
dsinclair7cbe68e2016-10-12 11:56:23 -0700352 pFormFillEnv->ClearAllFocusedAnnots();
dsinclairf3fbe832016-10-11 13:08:04 -0700353 // If the document was closed first, it's possible the XFA document
354 // is now a nullptr.
dsinclair521b7502016-11-02 13:02:28 -0700355 if (pFormFillEnv->GetXFAContext())
356 pFormFillEnv->GetXFAContext()->SetFormFillEnv(nullptr);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800357#endif // PDF_ENABLE_XFA
dsinclairf3fbe832016-10-11 13:08:04 -0700358 delete pFormFillEnv;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700359}
360
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400361FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnMouseMove(FPDF_FORMHANDLE hHandle,
362 FPDF_PAGE page,
363 int modifier,
364 double page_x,
365 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
367 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700368 return false;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500369 return pPageView->OnMouseMove(CFX_PointF(page_x, page_y), modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700370}
371
Lei Zhang63b01262017-08-31 08:54:46 -0700372FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnFocus(FPDF_FORMHANDLE hHandle,
373 FPDF_PAGE page,
374 int modifier,
375 double page_x,
376 double page_y) {
377 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
378 if (!pPageView)
379 return false;
380 return pPageView->OnFocus(CFX_PointF(page_x, page_y), modifier);
381}
382
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400383FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnLButtonDown(FPDF_FORMHANDLE hHandle,
384 FPDF_PAGE page,
385 int modifier,
386 double page_x,
387 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
389 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700390 return false;
Ryan Harrison9ce75b82018-06-06 19:45:14 +0000391#ifdef PDF_ENABLE_CLICK_LOGGING
392 fprintf(stderr, "mousedown,left,%d,%d\n", static_cast<int>(round(page_x)),
393 static_cast<int>(round(page_y)));
394#endif // PDF_ENABLE_CLICK_LOGGING
Dan Sinclairf528eee2017-02-14 11:52:07 -0500395 return pPageView->OnLButtonDown(CFX_PointF(page_x, page_y), modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700396}
397
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400398FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnLButtonUp(FPDF_FORMHANDLE hHandle,
399 FPDF_PAGE page,
400 int modifier,
401 double page_x,
402 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
404 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700405 return false;
Ryan Harrison9ce75b82018-06-06 19:45:14 +0000406#ifdef PDF_ENABLE_CLICK_LOGGING
407 fprintf(stderr, "mouseup,left,%d,%d\n", static_cast<int>(round(page_x)),
408 static_cast<int>(round(page_y)));
409#endif // PDF_ENABLE_CLICK_LOGGING
Lei Zhang1fed5a22017-06-01 11:52:22 -0700410 return pPageView->OnLButtonUp(CFX_PointF(page_x, page_y), modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700411}
412
Lei Zhanga37989f2018-10-17 20:32:41 +0000413FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
414FORM_OnLButtonDoubleClick(FPDF_FORMHANDLE hHandle,
415 FPDF_PAGE page,
416 int modifier,
417 double page_x,
418 double page_y) {
419 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
420 if (!pPageView)
421 return false;
422#ifdef PDF_ENABLE_CLICK_LOGGING
423 fprintf(stderr, "mousedown,doubleleft,%d,%d\n",
424 static_cast<int>(round(page_x)), static_cast<int>(round(page_y)));
425#endif // PDF_ENABLE_CLICK_LOGGING
426 return pPageView->OnLButtonDblClk(CFX_PointF(page_x, page_y), modifier);
427}
428
Tom Sepez51da0932015-11-25 16:05:49 -0800429#ifdef PDF_ENABLE_XFA
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400430FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnRButtonDown(FPDF_FORMHANDLE hHandle,
431 FPDF_PAGE page,
432 int modifier,
433 double page_x,
434 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
436 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700437 return false;
Ryan Harrison9ce75b82018-06-06 19:45:14 +0000438#ifdef PDF_ENABLE_CLICK_LOGGING
439 fprintf(stderr, "mousedown,right,%d,%d\n", static_cast<int>(round(page_x)),
440 static_cast<int>(round(page_y)));
441#endif // PDF_ENABLE_CLICK_LOGGING
Dan Sinclairf528eee2017-02-14 11:52:07 -0500442 return pPageView->OnRButtonDown(CFX_PointF(page_x, page_y), modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700443}
444
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400445FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnRButtonUp(FPDF_FORMHANDLE hHandle,
446 FPDF_PAGE page,
447 int modifier,
448 double page_x,
449 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
451 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700452 return false;
Ryan Harrison9ce75b82018-06-06 19:45:14 +0000453#ifdef PDF_ENABLE_CLICK_LOGGING
454 fprintf(stderr, "mouseup,right,%d,%d\n", static_cast<int>(round(page_x)),
455 static_cast<int>(round(page_y)));
456#endif // PDF_ENABLE_CLICK_LOGGING
Lei Zhang1fed5a22017-06-01 11:52:22 -0700457 return pPageView->OnRButtonUp(CFX_PointF(page_x, page_y), modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700458}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800459#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700460
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400461FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnKeyDown(FPDF_FORMHANDLE hHandle,
462 FPDF_PAGE page,
463 int nKeyCode,
464 int modifier) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
466 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700467 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700468 return pPageView->OnKeyDown(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700469}
470
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400471FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnKeyUp(FPDF_FORMHANDLE hHandle,
472 FPDF_PAGE page,
473 int nKeyCode,
474 int modifier) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
476 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700477 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478 return pPageView->OnKeyUp(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700479}
480
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400481FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnChar(FPDF_FORMHANDLE hHandle,
482 FPDF_PAGE page,
483 int nChar,
484 int modifier) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
486 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700487 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488 return pPageView->OnChar(nChar, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700489}
490
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400491FPDF_EXPORT unsigned long FPDF_CALLCONV
Lei Zhanga4c7ac42018-04-17 15:12:58 +0000492FORM_GetFocusedText(FPDF_FORMHANDLE hHandle,
493 FPDF_PAGE page,
494 void* buffer,
495 unsigned long buflen) {
496 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
497 if (!pPageView)
498 return 0;
499
500 return Utf16EncodeMaybeCopyAndReturnLength(pPageView->GetFocusedFormText(),
501 buffer, buflen);
502}
503
504FPDF_EXPORT unsigned long FPDF_CALLCONV
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400505FORM_GetSelectedText(FPDF_FORMHANDLE hHandle,
506 FPDF_PAGE page,
507 void* buffer,
508 unsigned long buflen) {
Diana Gagedce2d722017-06-20 11:17:11 -0700509 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
510 if (!pPageView)
511 return 0;
512
Lei Zhanga4c7ac42018-04-17 15:12:58 +0000513 return Utf16EncodeMaybeCopyAndReturnLength(pPageView->GetSelectedText(),
514 buffer, buflen);
Diana Gagedce2d722017-06-20 11:17:11 -0700515}
516
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400517FPDF_EXPORT void FPDF_CALLCONV FORM_ReplaceSelection(FPDF_FORMHANDLE hHandle,
518 FPDF_PAGE page,
519 FPDF_WIDESTRING wsText) {
Diana Gage1c7f1422017-07-24 11:19:52 -0700520 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
521 if (!pPageView)
522 return;
Diana Gageab390972017-07-28 17:07:39 -0700523
Ryan Harrison875e98c2017-09-27 10:53:11 -0400524 size_t len = WideString::WStringLength(wsText);
Ryan Harrison275e2602017-09-18 14:23:18 -0400525 WideString wide_str_text = WideString::FromUTF16LE(wsText, len);
Diana Gageab390972017-07-28 17:07:39 -0700526
527 pPageView->ReplaceSelection(wide_str_text);
Diana Gage1c7f1422017-07-24 11:19:52 -0700528}
529
Lei Zhangee967722018-04-19 20:55:54 +0000530FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_CanUndo(FPDF_FORMHANDLE hHandle,
531 FPDF_PAGE page) {
532 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
533 if (!pPageView)
534 return false;
535 return pPageView->CanUndo();
536}
537
538FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_CanRedo(FPDF_FORMHANDLE hHandle,
539 FPDF_PAGE page) {
540 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
541 if (!pPageView)
542 return false;
543 return pPageView->CanRedo();
544}
545
546FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_Undo(FPDF_FORMHANDLE hHandle,
547 FPDF_PAGE page) {
548 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
549 if (!pPageView)
550 return false;
551 return pPageView->Undo();
552}
553
554FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_Redo(FPDF_FORMHANDLE hHandle,
555 FPDF_PAGE page) {
556 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
557 if (!pPageView)
558 return false;
559 return pPageView->Redo();
560}
561
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400562FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
563FORM_ForceToKillFocus(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700564 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000565 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
dsinclairf3fbe832016-10-11 13:08:04 -0700566 if (!pFormFillEnv)
tsepez4cf55152016-11-02 14:37:54 -0700567 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700568 return pFormFillEnv->KillFocusAnnot(0);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700569}
570
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400571FPDF_EXPORT void FPDF_CALLCONV FPDF_FFLDraw(FPDF_FORMHANDLE hHandle,
572 FPDF_BITMAP bitmap,
573 FPDF_PAGE page,
574 int start_x,
575 int start_y,
576 int size_x,
577 int size_y,
578 int rotate,
579 int flags) {
Cary Clark399be5b2016-03-14 16:51:29 -0400580 FFLCommon(hHandle, bitmap, nullptr, page, start_x, start_y, size_x, size_y,
581 rotate, flags);
582}
583
584#ifdef _SKIA_SUPPORT_
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400585FPDF_EXPORT void FPDF_CALLCONV FPDF_FFLRecord(FPDF_FORMHANDLE hHandle,
586 FPDF_RECORDER recorder,
587 FPDF_PAGE page,
588 int start_x,
589 int start_y,
590 int size_x,
591 int size_y,
592 int rotate,
593 int flags) {
Cary Clark399be5b2016-03-14 16:51:29 -0400594 FFLCommon(hHandle, nullptr, recorder, page, start_x, start_y, size_x, size_y,
595 rotate, flags);
596}
597#endif
598
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400599FPDF_EXPORT void FPDF_CALLCONV
600FPDF_SetFormFieldHighlightColor(FPDF_FORMHANDLE hHandle,
601 int fieldType,
602 unsigned long color) {
Lei Zhang073ecf42018-10-11 16:56:00 +0000603 CPDFSDK_InteractiveForm* pForm = FormHandleToInteractiveForm(hHandle);
604 if (!pForm)
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000605 return;
606
607 Optional<FormFieldType> cast_input = IntToFormFieldType(fieldType);
Ryan Harrison2056fac2018-01-16 16:06:45 +0000608 if (!cast_input)
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000609 return;
610
Lei Zhang073ecf42018-10-11 16:56:00 +0000611 if (cast_input.value() == FormFieldType::kUnknown)
612 pForm->SetAllHighlightColors(color);
613 else
614 pForm->SetHighlightColor(color, cast_input.value());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700615}
616
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400617FPDF_EXPORT void FPDF_CALLCONV
618FPDF_SetFormFieldHighlightAlpha(FPDF_FORMHANDLE hHandle, unsigned char alpha) {
Lei Zhang073ecf42018-10-11 16:56:00 +0000619 if (CPDFSDK_InteractiveForm* pForm = FormHandleToInteractiveForm(hHandle))
620 pForm->SetHighlightAlpha(alpha);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700621}
622
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400623FPDF_EXPORT void FPDF_CALLCONV
624FPDF_RemoveFormFieldHighlight(FPDF_FORMHANDLE hHandle) {
Lei Zhang073ecf42018-10-11 16:56:00 +0000625 if (CPDFSDK_InteractiveForm* pForm = FormHandleToInteractiveForm(hHandle))
626 pForm->RemoveAllHighLights();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700627}
628
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400629FPDF_EXPORT void FPDF_CALLCONV FORM_OnAfterLoadPage(FPDF_PAGE page,
630 FPDF_FORMHANDLE hHandle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700631 if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page))
tsepez4cf55152016-11-02 14:37:54 -0700632 pPageView->SetValid(true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700633}
634
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400635FPDF_EXPORT void FPDF_CALLCONV FORM_OnBeforeClosePage(FPDF_PAGE page,
636 FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700637 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000638 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
dsinclairf3fbe832016-10-11 13:08:04 -0700639 if (!pFormFillEnv)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700641
Tom Sepez101535f2018-06-12 13:36:05 +0000642 IPDF_Page* pPage = IPDFPageFromFPDFPage(page);
Tom Sepez540c4362015-11-24 13:33:57 -0800643 if (!pPage)
644 return;
645
dsinclair7cbe68e2016-10-12 11:56:23 -0700646 CPDFSDK_PageView* pPageView = pFormFillEnv->GetPageView(pPage, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700647 if (pPageView) {
tsepez4cf55152016-11-02 14:37:54 -0700648 pPageView->SetValid(false);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800649 // RemovePageView() takes care of the delete for us.
dsinclair7cbe68e2016-10-12 11:56:23 -0700650 pFormFillEnv->RemovePageView(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700651 }
652}
653
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400654FPDF_EXPORT void FPDF_CALLCONV
655FORM_DoDocumentJSAction(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700656 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000657 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
Tom Sepezf5ca90c2018-02-01 02:15:44 +0000658 if (pFormFillEnv && pFormFillEnv->IsJSPlatformPresent())
dsinclair7cbe68e2016-10-12 11:56:23 -0700659 pFormFillEnv->ProcJavascriptFun();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700660}
661
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400662FPDF_EXPORT void FPDF_CALLCONV
663FORM_DoDocumentOpenAction(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700664 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000665 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
Tom Sepezf5ca90c2018-02-01 02:15:44 +0000666 if (pFormFillEnv && pFormFillEnv->IsJSPlatformPresent())
dsinclair7cbe68e2016-10-12 11:56:23 -0700667 pFormFillEnv->ProcOpenAction();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700668}
669
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400670FPDF_EXPORT void FPDF_CALLCONV FORM_DoDocumentAAction(FPDF_FORMHANDLE hHandle,
671 int aaType) {
dsinclairf3fbe832016-10-11 13:08:04 -0700672 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000673 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
dsinclairf3fbe832016-10-11 13:08:04 -0700674 if (!pFormFillEnv)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 return;
676
dsinclair7cbe68e2016-10-12 11:56:23 -0700677 CPDF_Document* pDoc = pFormFillEnv->GetPDFDocument();
Lei Zhang01581062017-08-30 14:19:26 -0700678 const CPDF_Dictionary* pDict = pDoc->GetRoot();
Lei Zhang1fed5a22017-06-01 11:52:22 -0700679 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700680 return;
Tom Sepez468e5892015-10-13 15:49:36 -0700681
Henrique Nakashima7b094f82018-05-08 20:32:08 +0000682 CPDF_AAction aa(pDict->GetDictFor("AA"));
Lei Zhang1fed5a22017-06-01 11:52:22 -0700683 auto type = static_cast<CPDF_AAction::AActionType>(aaType);
684 if (aa.ActionExist(type)) {
685 CPDF_Action action = aa.GetAction(type);
Lei Zhang7db136a2018-10-10 21:34:17 +0000686 pFormFillEnv->GetActionHandler()->DoAction_Document(action, type,
687 pFormFillEnv);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700688 }
689}
690
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400691FPDF_EXPORT void FPDF_CALLCONV FORM_DoPageAAction(FPDF_PAGE page,
692 FPDF_FORMHANDLE hHandle,
693 int aaType) {
dsinclairf3fbe832016-10-11 13:08:04 -0700694 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000695 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
dsinclairf3fbe832016-10-11 13:08:04 -0700696 if (!pFormFillEnv)
tonikitoo401d4f22016-08-10 11:37:45 -0700697 return;
698
Tom Sepez101535f2018-06-12 13:36:05 +0000699 IPDF_Page* pPage = IPDFPageFromFPDFPage(page);
Tom Sepez540c4362015-11-24 13:33:57 -0800700 CPDF_Page* pPDFPage = CPDFPageFromFPDFPage(page);
Henrique Nakashima7b094f82018-05-08 20:32:08 +0000701 if (!pPDFPage)
702 return;
703
704 if (!pFormFillEnv->GetPageView(pPage, false))
tonikitoo401d4f22016-08-10 11:37:45 -0700705 return;
706
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700707 CPDFSDK_ActionHandler* pActionHandler = pFormFillEnv->GetActionHandler();
Henrique Nakashima888af472018-06-07 19:43:42 +0000708 CPDF_Dictionary* pPageDict = pPDFPage->GetDict();
Henrique Nakashima7b094f82018-05-08 20:32:08 +0000709 CPDF_AAction aa(pPageDict->GetDictFor("AA"));
Lei Zhang1fed5a22017-06-01 11:52:22 -0700710 CPDF_AAction::AActionType type = aaType == FPDFPAGE_AACTION_OPEN
711 ? CPDF_AAction::OpenPage
712 : CPDF_AAction::ClosePage;
Henrique Nakashima7b094f82018-05-08 20:32:08 +0000713 if (aa.ActionExist(type)) {
714 CPDF_Action action = aa.GetAction(type);
715 pActionHandler->DoAction_Page(action, type, pFormFillEnv);
716 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700717}