blob: f949da1019d221752c550d60a546621882944035 [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
Dan Sinclair455a4192016-03-16 09:48:56 -040012#include "core/fpdfapi/fpdf_page/include/cpdf_page.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040013#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
14#include "core/fpdfapi/fpdf_render/include/cpdf_renderoptions.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080015#include "fpdfsdk/include/fsdk_define.h"
16#include "fpdfsdk/include/fsdk_mgr.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080017#include "public/fpdfview.h"
Tom Sepezab277682016-02-17 10:07:21 -080018#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019
Tom Sepez40e9ff32015-11-30 12:39:54 -080020#ifdef PDF_ENABLE_XFA
dsinclair89bdd082016-04-06 10:47:54 -070021#include "fpdfsdk/fpdfxfa/include/fpdfxfa_app.h"
22#include "fpdfsdk/fpdfxfa/include/fpdfxfa_doc.h"
23#include "fpdfsdk/fpdfxfa/include/fpdfxfa_page.h"
dsinclair7222ea62016-04-06 14:33:07 -070024#include "xfa/fxfa/include/xfa_ffapp.h"
25#include "xfa/fxfa/include/xfa_ffdocview.h"
26#include "xfa/fxfa/include/xfa_ffpageview.h"
27#include "xfa/fxfa/include/xfa_ffwidget.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080028#endif // PDF_ENABLE_XFA
29
Tom Sepezdcbc02f2015-07-17 09:16:17 -070030namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032CPDFSDK_InterForm* FormHandleToInterForm(FPDF_FORMHANDLE hHandle) {
Tom Sepezfe351db2016-01-29 16:26:27 -080033 CPDFSDK_Document* pSDKDoc = CPDFSDK_Document::FromFPDFFormHandle(hHandle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 return pSDKDoc ? pSDKDoc->GetInterForm() : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070035}
Bo Xufdc00a72014-10-28 23:03:33 -070036
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037CPDFSDK_PageView* FormHandleToPageView(FPDF_FORMHANDLE hHandle,
38 FPDF_PAGE page) {
Tom Sepez540c4362015-11-24 13:33:57 -080039 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
40 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 return nullptr;
Tom Sepez0b133982015-07-28 11:23:22 -070042
Tom Sepezfe351db2016-01-29 16:26:27 -080043 CPDFSDK_Document* pSDKDoc = CPDFSDK_Document::FromFPDFFormHandle(hHandle);
Tom Sepez540c4362015-11-24 13:33:57 -080044 return pSDKDoc ? pSDKDoc->GetPageView(pPage, TRUE) : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070045}
Bo Xufdc00a72014-10-28 23:03:33 -070046
Tom Sepezab277682016-02-17 10:07:21 -080047#ifdef PDF_ENABLE_XFA
48std::vector<CFX_ByteString>* FromFPDFStringHandle(FPDF_STRINGHANDLE handle) {
49 return reinterpret_cast<std::vector<CFX_ByteString>*>(handle);
50}
51
52FPDF_STRINGHANDLE ToFPDFStringHandle(std::vector<CFX_ByteString>* strings) {
53 return reinterpret_cast<FPDF_STRINGHANDLE>(strings);
54}
55#endif // PDF_ENABLE_XFA
56
Tom Sepezdcbc02f2015-07-17 09:16:17 -070057} // namespace
Bo Xufdc00a72014-10-28 23:03:33 -070058
Lei Zhangbdf72c32015-08-14 19:24:08 -070059DLLEXPORT int STDCALL FPDFPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
60 FPDF_PAGE page,
61 double page_x,
62 double page_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -070063 if (!hHandle)
Tom Sepezdcbc02f2015-07-17 09:16:17 -070064 return -1;
Tom Sepezdb0be962015-10-16 14:00:21 -070065 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 if (pPage) {
Lei Zhangbdf72c32015-08-14 19:24:08 -070067 CPDF_InterForm interform(pPage->m_pDocument, FALSE);
Lei Zhang7f9fdde2016-02-22 20:47:13 -080068 CPDF_FormControl* pFormCtrl =
69 interform.GetControlAtPoint(pPage, static_cast<FX_FLOAT>(page_x),
70 static_cast<FX_FLOAT>(page_y), nullptr);
Lei Zhangbdf72c32015-08-14 19:24:08 -070071 if (!pFormCtrl)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 CPDF_FormField* pFormField = pFormCtrl->GetField();
Lei Zhang7f9fdde2016-02-22 20:47:13 -080074 return pFormField ? pFormField->GetFieldType() : -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 }
76
Lei Zhang7f9fdde2016-02-22 20:47:13 -080077#ifdef PDF_ENABLE_XFA
78 CPDFXFA_Page* pXFAPage = UnderlyingFromFPDFPage(page);
79 if (!pXFAPage)
80 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081
dsinclairdf4bc592016-03-31 20:34:43 -070082 CXFA_FFPageView* pPageView = pXFAPage->GetXFAPageView();
Lei Zhang7f9fdde2016-02-22 20:47:13 -080083 if (!pPageView)
84 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085
dsinclairdf4bc592016-03-31 20:34:43 -070086 CXFA_FFDocView* pDocView = pPageView->GetDocView();
Lei Zhang7f9fdde2016-02-22 20:47:13 -080087 if (!pDocView)
88 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089
dsinclairdf4bc592016-03-31 20:34:43 -070090 CXFA_FFWidgetHandler* pWidgetHandler = pDocView->GetWidgetHandler();
Lei Zhang7f9fdde2016-02-22 20:47:13 -080091 if (!pWidgetHandler)
92 return -1;
93
tsepezcc4d6d82016-05-16 13:21:03 -070094 std::unique_ptr<IXFA_WidgetIterator> pWidgetIterator(
dsinclair935d8d52016-05-17 10:32:18 -070095 pPageView->CreateWidgetIterator(XFA_TRAVERSEWAY_Form,
96 XFA_WidgetStatus_Viewable));
Lei Zhang7f9fdde2016-02-22 20:47:13 -080097 if (!pWidgetIterator)
98 return -1;
99
dsinclairdf4bc592016-03-31 20:34:43 -0700100 CXFA_FFWidget* pXFAAnnot = pWidgetIterator->MoveToNext();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800101 while (pXFAAnnot) {
102 CFX_RectF rcBBox;
dsinclair221caf62016-04-04 12:08:40 -0700103 pXFAAnnot->GetBBox(rcBBox, 0);
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800104 CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width,
105 rcBBox.top + rcBBox.height);
106 rcWidget.left -= 1.0f;
107 rcWidget.right += 1.0f;
108 rcWidget.bottom -= 1.0f;
109 rcWidget.top += 1.0f;
110
111 if (rcWidget.Contains(static_cast<FX_FLOAT>(page_x),
112 static_cast<FX_FLOAT>(page_y))) {
113 return FPDF_FORMFIELD_XFA;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 }
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800115 pXFAAnnot = pWidgetIterator->MoveToNext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800117#endif // PDF_ENABLE_XFA
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800118 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700119}
120
Lei Zhangbdf72c32015-08-14 19:24:08 -0700121DLLEXPORT int STDCALL FPDPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
122 FPDF_PAGE page,
123 double page_x,
124 double page_y) {
125 return FPDFPage_HasFormFieldAtPoint(hHandle, page, page_x, page_y);
126}
127
128DLLEXPORT int STDCALL FPDFPage_FormFieldZOrderAtPoint(FPDF_FORMHANDLE hHandle,
129 FPDF_PAGE page,
130 double page_x,
131 double page_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700132 if (!hHandle)
Lei Zhangbdf72c32015-08-14 19:24:08 -0700133 return -1;
Tom Sepezdb0be962015-10-16 14:00:21 -0700134 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
135 if (!pPage)
136 return -1;
Lei Zhangbdf72c32015-08-14 19:24:08 -0700137 CPDF_InterForm interform(pPage->m_pDocument, FALSE);
138 int z_order = -1;
139 (void)interform.GetControlAtPoint(pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y,
140 &z_order);
141 return z_order;
142}
143
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144DLLEXPORT FPDF_FORMHANDLE STDCALL
145FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document,
146 FPDF_FORMFILLINFO* formInfo) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800147#ifdef PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800148 const int kRequiredVersion = 2;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800149#else // PDF_ENABLE_XFA
150 const int kRequiredVersion = 1;
151#endif // PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800152 if (!formInfo || formInfo->version != kRequiredVersion)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154
Tom Sepez540c4362015-11-24 13:33:57 -0800155 UnderlyingDocumentType* pDocument = UnderlyingFromFPDFDocument(document);
156 if (!pDocument)
157 return nullptr;
158
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 CPDFDoc_Environment* pEnv = new CPDFDoc_Environment(pDocument, formInfo);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800160#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 pEnv->SetSDKDocument(pDocument->GetSDKDocument(pEnv));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
163 pApp->AddFormFillEnv(pEnv);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800164#else // PDF_ENABLE_XFA
165 pEnv->SetSDKDocument(new CPDFSDK_Document(pDocument, pEnv));
166#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 return pEnv;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168}
169
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170DLLEXPORT void STDCALL
171FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle) {
172 if (!hHandle)
173 return;
Tom Sepez51da0932015-11-25 16:05:49 -0800174 CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800175#ifdef PDF_ENABLE_XFA
176 CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
177 pApp->RemoveFormFillEnv(pEnv);
178#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800179 if (CPDFSDK_Document* pSDKDoc = pEnv->GetSDKDocument()) {
180 pEnv->SetSDKDocument(NULL);
181 delete pSDKDoc;
182 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800183#endif // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800184 delete pEnv;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700185}
186
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187DLLEXPORT FPDF_BOOL STDCALL FORM_OnMouseMove(FPDF_FORMHANDLE hHandle,
188 FPDF_PAGE page,
189 int modifier,
190 double page_x,
191 double page_y) {
192 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
193 if (!pPageView)
194 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700195
Tom Sepez281a9ea2016-02-26 14:24:28 -0800196 CFX_FloatPoint pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 return pPageView->OnMouseMove(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198}
199
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonDown(FPDF_FORMHANDLE hHandle,
201 FPDF_PAGE page,
202 int modifier,
203 double page_x,
204 double page_y) {
205 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
206 if (!pPageView)
207 return FALSE;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700208
Tom Sepez281a9ea2016-02-26 14:24:28 -0800209 CFX_FloatPoint pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 return pPageView->OnLButtonDown(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700211}
212
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonUp(FPDF_FORMHANDLE hHandle,
214 FPDF_PAGE page,
215 int modifier,
216 double page_x,
217 double page_y) {
218 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
219 if (!pPageView)
220 return FALSE;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700221
Tom Sepez281a9ea2016-02-26 14:24:28 -0800222 CFX_FloatPoint pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 return pPageView->OnLButtonUp(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700224}
225
Tom Sepez51da0932015-11-25 16:05:49 -0800226#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonDown(FPDF_FORMHANDLE hHandle,
228 FPDF_PAGE page,
229 int modifier,
230 double page_x,
231 double page_y) {
232 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
233 if (!pPageView)
234 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700235
Tom Sepez281a9ea2016-02-26 14:24:28 -0800236 CFX_FloatPoint pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 return pPageView->OnRButtonDown(pt, modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700238}
239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonUp(FPDF_FORMHANDLE hHandle,
241 FPDF_PAGE page,
242 int modifier,
243 double page_x,
244 double page_y) {
245 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
246 if (!pPageView)
247 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700248
Tom Sepez281a9ea2016-02-26 14:24:28 -0800249 CFX_FloatPoint pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 return pPageView->OnRButtonUp(pt, modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700251}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800252#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700253
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyDown(FPDF_FORMHANDLE hHandle,
255 FPDF_PAGE page,
256 int nKeyCode,
257 int modifier) {
258 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
259 if (!pPageView)
260 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700261
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 return pPageView->OnKeyDown(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263}
264
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyUp(FPDF_FORMHANDLE hHandle,
266 FPDF_PAGE page,
267 int nKeyCode,
268 int modifier) {
269 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
270 if (!pPageView)
271 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 return pPageView->OnKeyUp(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700274}
275
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276DLLEXPORT FPDF_BOOL STDCALL FORM_OnChar(FPDF_FORMHANDLE hHandle,
277 FPDF_PAGE page,
278 int nChar,
279 int modifier) {
280 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
281 if (!pPageView)
282 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 return pPageView->OnChar(nChar, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285}
286
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287DLLEXPORT FPDF_BOOL STDCALL FORM_ForceToKillFocus(FPDF_FORMHANDLE hHandle) {
Tom Sepezfe351db2016-01-29 16:26:27 -0800288 CPDFSDK_Document* pSDKDoc = CPDFSDK_Document::FromFPDFFormHandle(hHandle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 if (!pSDKDoc)
290 return FALSE;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700291
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 return pSDKDoc->KillFocusAnnot(0);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700293}
294
Cary Clark399be5b2016-03-14 16:51:29 -0400295static void FFLCommon(FPDF_FORMHANDLE hHandle,
296 FPDF_BITMAP bitmap,
297 FPDF_RECORDER recorder,
298 FPDF_PAGE page,
299 int start_x,
300 int start_y,
301 int size_x,
302 int size_y,
303 int rotate,
304 int flags) {
Tom Sepez1b246282015-11-25 15:15:31 -0800305 if (!hHandle)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700306 return;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700307
Tom Sepez1b246282015-11-25 15:15:31 -0800308 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
309 if (!pPage)
310 return;
311
Tom Sepez51da0932015-11-25 16:05:49 -0800312#ifndef PDF_ENABLE_XFA
313 CPDF_RenderOptions options;
314 if (flags & FPDF_LCD_TEXT)
315 options.m_Flags |= RENDER_CLEARTYPE;
316 else
317 options.m_Flags &= ~RENDER_CLEARTYPE;
Tom Sepez51da0932015-11-25 16:05:49 -0800318 // Grayscale output
319 if (flags & FPDF_GRAYSCALE) {
320 options.m_ColorMode = RENDER_COLOR_GRAY;
321 options.m_ForeColor = 0;
322 options.m_BackColor = 0xffffff;
323 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800324 options.m_AddFlags = flags >> 8;
325 options.m_pOCContext = new CPDF_OCContext(pPage->m_pDocument);
326#else // PDF_ENABLE_XFA
327 CPDFXFA_Document* pDocument = pPage->GetDocument();
328 if (!pDocument)
329 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 CPDF_Document* pPDFDoc = pDocument->GetPDFDoc();
331 if (!pPDFDoc)
332 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
334 CPDFSDK_Document* pFXDoc = pEnv->GetSDKDocument();
335 if (!pFXDoc)
336 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800337#endif // PDF_ENABLE_XFA
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700338
Tom Sepez60d909e2015-12-10 15:34:55 -0800339 CFX_Matrix matrix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700341
Tom Sepezbec4ea12016-02-29 13:23:13 -0800342 FX_RECT clip(start_x, start_y, start_x + size_x, start_y + size_y);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700343
Lei Zhangaa8bf7e2015-12-24 19:13:32 -0800344 std::unique_ptr<CFX_FxgeDevice> pDevice(new CFX_FxgeDevice);
caryclarkd6e18872016-05-13 10:57:20 -0700345#ifdef _SKIA_SUPPORT_
346 pDevice->AttachRecorder(static_cast<SkPictureRecorder*>(recorder));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700347#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348 pDevice->Attach((CFX_DIBitmap*)bitmap);
349 pDevice->SaveState();
Tom Sepezbec4ea12016-02-29 13:23:13 -0800350 pDevice->SetClip_Rect(clip);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700351
Tom Sepez51da0932015-11-25 16:05:49 -0800352#ifndef PDF_ENABLE_XFA
353 if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, pPage))
354 pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800355#else // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356 CPDF_RenderOptions options;
357 if (flags & FPDF_LCD_TEXT)
358 options.m_Flags |= RENDER_CLEARTYPE;
359 else
360 options.m_Flags &= ~RENDER_CLEARTYPE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700361
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362 // Grayscale output
363 if (flags & FPDF_GRAYSCALE) {
364 options.m_ColorMode = RENDER_COLOR_GRAY;
365 options.m_ForeColor = 0;
366 options.m_BackColor = 0xffffff;
367 }
368 options.m_AddFlags = flags >> 8;
Tom Sepezae51c812015-08-05 12:34:06 -0700369 options.m_pOCContext = new CPDF_OCContext(pPDFDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700370
Tom Sepez1b246282015-11-25 15:15:31 -0800371 if (CPDFSDK_PageView* pPageView = pFXDoc->GetPageView(pPage))
Lei Zhangf0e2e1b2015-11-02 13:27:54 -0800372 pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options, clip);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800373#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700374
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 pDevice->RestoreState();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 delete options.m_pOCContext;
Tom Sepez51da0932015-11-25 16:05:49 -0800377#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 options.m_pOCContext = NULL;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800379#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700380}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800381
Cary Clark399be5b2016-03-14 16:51:29 -0400382DLLEXPORT void STDCALL FPDF_FFLDraw(FPDF_FORMHANDLE hHandle,
383 FPDF_BITMAP bitmap,
384 FPDF_PAGE page,
385 int start_x,
386 int start_y,
387 int size_x,
388 int size_y,
389 int rotate,
390 int flags) {
391 FFLCommon(hHandle, bitmap, nullptr, page, start_x, start_y, size_x, size_y,
392 rotate, flags);
393}
394
395#ifdef _SKIA_SUPPORT_
396DLLEXPORT void STDCALL FPDF_FFLRecord(FPDF_FORMHANDLE hHandle,
397 FPDF_RECORDER recorder,
398 FPDF_PAGE page,
399 int start_x,
400 int start_y,
401 int size_x,
402 int size_y,
403 int rotate,
404 int flags) {
405 FFLCommon(hHandle, nullptr, recorder, page, start_x, start_y, size_x, size_y,
406 rotate, flags);
407}
408#endif
409
Tom Sepez40e9ff32015-11-30 12:39:54 -0800410#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411DLLEXPORT void STDCALL FPDF_Widget_Undo(FPDF_DOCUMENT document,
412 FPDF_WIDGET hWidget) {
dsinclair221caf62016-04-04 12:08:40 -0700413 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700415
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
417 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
418 pDocument->GetDocType() != XFA_DOCTYPE_Static)
419 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700420
dsinclair221caf62016-04-04 12:08:40 -0700421 static_cast<CXFA_FFWidget*>(hWidget)->Undo();
Bo Xufdc00a72014-10-28 23:03:33 -0700422}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423DLLEXPORT void STDCALL FPDF_Widget_Redo(FPDF_DOCUMENT document,
424 FPDF_WIDGET hWidget) {
dsinclair221caf62016-04-04 12:08:40 -0700425 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700427
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
429 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
430 pDocument->GetDocType() != XFA_DOCTYPE_Static)
431 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700432
dsinclair221caf62016-04-04 12:08:40 -0700433 static_cast<CXFA_FFWidget*>(hWidget)->Redo();
Bo Xufdc00a72014-10-28 23:03:33 -0700434}
435
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436DLLEXPORT void STDCALL FPDF_Widget_SelectAll(FPDF_DOCUMENT document,
437 FPDF_WIDGET hWidget) {
dsinclair221caf62016-04-04 12:08:40 -0700438 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700440
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700441 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
442 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
443 pDocument->GetDocType() != XFA_DOCTYPE_Static)
444 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700445
dsinclair221caf62016-04-04 12:08:40 -0700446 static_cast<CXFA_FFWidget*>(hWidget)->SelectAll();
Bo Xufdc00a72014-10-28 23:03:33 -0700447}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448DLLEXPORT void STDCALL FPDF_Widget_Copy(FPDF_DOCUMENT document,
449 FPDF_WIDGET hWidget,
450 FPDF_WIDESTRING wsText,
451 FPDF_DWORD* size) {
dsinclair221caf62016-04-04 12:08:40 -0700452 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700454
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
456 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
457 pDocument->GetDocType() != XFA_DOCTYPE_Static)
458 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700459
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 CFX_WideString wsCpText;
dsinclair221caf62016-04-04 12:08:40 -0700461 static_cast<CXFA_FFWidget*>(hWidget)->Copy(wsCpText);
Bo Xufdc00a72014-10-28 23:03:33 -0700462
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463 CFX_ByteString bsCpText = wsCpText.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -0700464 uint32_t len = bsCpText.GetLength() / sizeof(unsigned short);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465 if (wsText == NULL) {
466 *size = len;
467 return;
468 }
Bo Xufdc00a72014-10-28 23:03:33 -0700469
weili47ca6922016-03-31 15:08:27 -0700470 uint32_t real_size = len < *size ? len : *size;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471 if (real_size > 0) {
472 FXSYS_memcpy((void*)wsText,
473 bsCpText.GetBuffer(real_size * sizeof(unsigned short)),
474 real_size * sizeof(unsigned short));
475 bsCpText.ReleaseBuffer(real_size * sizeof(unsigned short));
476 }
477 *size = real_size;
Bo Xufdc00a72014-10-28 23:03:33 -0700478}
Tom Sepezab277682016-02-17 10:07:21 -0800479
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700480DLLEXPORT void STDCALL FPDF_Widget_Cut(FPDF_DOCUMENT document,
481 FPDF_WIDGET hWidget,
482 FPDF_WIDESTRING wsText,
483 FPDF_DWORD* size) {
484 if (NULL == hWidget || NULL == document)
485 return;
486 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
487 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
488 pDocument->GetDocType() != XFA_DOCTYPE_Static)
489 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700490
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700491 CFX_WideString wsCpText;
dsinclair221caf62016-04-04 12:08:40 -0700492 static_cast<CXFA_FFWidget*>(hWidget)->Cut(wsCpText);
Bo Xufdc00a72014-10-28 23:03:33 -0700493
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494 CFX_ByteString bsCpText = wsCpText.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -0700495 uint32_t len = bsCpText.GetLength() / sizeof(unsigned short);
dsinclair221caf62016-04-04 12:08:40 -0700496 if (!wsText) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700497 *size = len;
498 return;
499 }
Bo Xufdc00a72014-10-28 23:03:33 -0700500
weili47ca6922016-03-31 15:08:27 -0700501 uint32_t real_size = len < *size ? len : *size;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502 if (real_size > 0) {
503 FXSYS_memcpy((void*)wsText,
504 bsCpText.GetBuffer(real_size * sizeof(unsigned short)),
505 real_size * sizeof(unsigned short));
506 bsCpText.ReleaseBuffer(real_size * sizeof(unsigned short));
507 }
508 *size = real_size;
Bo Xufdc00a72014-10-28 23:03:33 -0700509}
Tom Sepezab277682016-02-17 10:07:21 -0800510
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700511DLLEXPORT void STDCALL FPDF_Widget_Paste(FPDF_DOCUMENT document,
512 FPDF_WIDGET hWidget,
513 FPDF_WIDESTRING wsText,
514 FPDF_DWORD size) {
dsinclair221caf62016-04-04 12:08:40 -0700515 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700516 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700517
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700518 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
519 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
520 pDocument->GetDocType() != XFA_DOCTYPE_Static)
521 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700522
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523 CFX_WideString wstr = CFX_WideString::FromUTF16LE(wsText, size);
dsinclair221caf62016-04-04 12:08:40 -0700524 static_cast<CXFA_FFWidget*>(hWidget)->Paste(wstr);
Bo Xufdc00a72014-10-28 23:03:33 -0700525}
Tom Sepezab277682016-02-17 10:07:21 -0800526
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527DLLEXPORT void STDCALL
528FPDF_Widget_ReplaceSpellCheckWord(FPDF_DOCUMENT document,
529 FPDF_WIDGET hWidget,
530 float x,
531 float y,
532 FPDF_BYTESTRING bsText) {
dsinclair221caf62016-04-04 12:08:40 -0700533 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700534 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700535
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700536 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
537 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
538 pDocument->GetDocType() != XFA_DOCTYPE_Static)
539 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700540
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541 CFX_PointF ptPopup;
542 ptPopup.x = x;
543 ptPopup.y = y;
544 CFX_ByteStringC bs(bsText);
dsinclair221caf62016-04-04 12:08:40 -0700545 static_cast<CXFA_FFWidget*>(hWidget)->ReplaceSpellCheckWord(ptPopup, bs);
Bo Xufdc00a72014-10-28 23:03:33 -0700546}
Tom Sepezab277682016-02-17 10:07:21 -0800547
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700548DLLEXPORT void STDCALL
549FPDF_Widget_GetSpellCheckWords(FPDF_DOCUMENT document,
550 FPDF_WIDGET hWidget,
551 float x,
552 float y,
553 FPDF_STRINGHANDLE* stringHandle) {
Tom Sepezab277682016-02-17 10:07:21 -0800554 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700555 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700556
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700557 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
558 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
559 pDocument->GetDocType() != XFA_DOCTYPE_Static)
560 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700561
Tom Sepezab277682016-02-17 10:07:21 -0800562 std::vector<CFX_ByteString>* sSuggestWords = new std::vector<CFX_ByteString>;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563 CFX_PointF ptPopup;
564 ptPopup.x = x;
565 ptPopup.y = y;
dsinclair221caf62016-04-04 12:08:40 -0700566 static_cast<CXFA_FFWidget*>(hWidget)
567 ->GetSuggestWords(ptPopup, *sSuggestWords);
Tom Sepezab277682016-02-17 10:07:21 -0800568 *stringHandle = ToFPDFStringHandle(sSuggestWords);
Bo Xufdc00a72014-10-28 23:03:33 -0700569}
Tom Sepezab277682016-02-17 10:07:21 -0800570
571DLLEXPORT int STDCALL FPDF_StringHandleCounts(FPDF_STRINGHANDLE sHandle) {
572 std::vector<CFX_ByteString>* sSuggestWords = FromFPDFStringHandle(sHandle);
573 return sSuggestWords ? pdfium::CollectionSize<int>(*sSuggestWords) : -1;
Bo Xufdc00a72014-10-28 23:03:33 -0700574}
Tom Sepezab277682016-02-17 10:07:21 -0800575
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576DLLEXPORT FPDF_BOOL STDCALL
Tom Sepezab277682016-02-17 10:07:21 -0800577FPDF_StringHandleGetStringByIndex(FPDF_STRINGHANDLE sHandle,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700578 int index,
579 FPDF_BYTESTRING bsText,
580 FPDF_DWORD* size) {
Tom Sepezab277682016-02-17 10:07:21 -0800581 if (!sHandle || !size)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582 return FALSE;
Tom Sepezab277682016-02-17 10:07:21 -0800583
584 int count = FPDF_StringHandleCounts(sHandle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700585 if (index < 0 || index >= count)
586 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700587
Tom Sepezab277682016-02-17 10:07:21 -0800588 std::vector<CFX_ByteString>* sSuggestWords = FromFPDFStringHandle(sHandle);
weili47ca6922016-03-31 15:08:27 -0700589 uint32_t len = (*sSuggestWords)[index].GetLength();
Tom Sepezab277682016-02-17 10:07:21 -0800590 if (!bsText) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700591 *size = len;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700592 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700593 }
594
weili47ca6922016-03-31 15:08:27 -0700595 uint32_t real_size = len < *size ? len : *size;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700596 if (real_size > 0)
tsepezb4c9f3f2016-04-13 15:41:21 -0700597 FXSYS_memcpy((void*)bsText, (*sSuggestWords)[index].c_str(), real_size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700598 *size = real_size;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700600}
Tom Sepezab277682016-02-17 10:07:21 -0800601
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700602DLLEXPORT void STDCALL
603FPDF_StringHandleRelease(FPDF_STRINGHANDLE stringHandle) {
Tom Sepezab277682016-02-17 10:07:21 -0800604 delete FromFPDFStringHandle(stringHandle);
Bo Xufdc00a72014-10-28 23:03:33 -0700605}
606
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700607DLLEXPORT FPDF_BOOL STDCALL
608FPDF_StringHandleAddString(FPDF_STRINGHANDLE stringHandle,
609 FPDF_BYTESTRING bsText,
610 FPDF_DWORD size) {
Tom Sepezab277682016-02-17 10:07:21 -0800611 if (!stringHandle || !bsText || size == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700612 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700613
Tom Sepezab277682016-02-17 10:07:21 -0800614 FromFPDFStringHandle(stringHandle)->push_back(CFX_ByteString(bsText, size));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700615 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700616}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800617#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700618
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightColor(FPDF_FORMHANDLE hHandle,
620 int fieldType,
621 unsigned long color) {
622 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
623 pInterForm->SetHighlightColor(color, fieldType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700624}
625
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700626DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightAlpha(FPDF_FORMHANDLE hHandle,
627 unsigned char alpha) {
628 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
629 pInterForm->SetHighlightAlpha(alpha);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700630}
631
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700632DLLEXPORT void STDCALL FPDF_RemoveFormFieldHighlight(FPDF_FORMHANDLE hHandle) {
633 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
634 pInterForm->RemoveAllHighLight();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700635}
636
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700637DLLEXPORT void STDCALL FORM_OnAfterLoadPage(FPDF_PAGE page,
638 FPDF_FORMHANDLE hHandle) {
639 if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page))
640 pPageView->SetValid(TRUE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700641}
642
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643DLLEXPORT void STDCALL FORM_OnBeforeClosePage(FPDF_PAGE page,
644 FPDF_FORMHANDLE hHandle) {
Tom Sepez540c4362015-11-24 13:33:57 -0800645 if (!hHandle)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700646 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700647
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700648 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetSDKDocument();
649 if (!pSDKDoc)
650 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700651
Tom Sepez540c4362015-11-24 13:33:57 -0800652 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
653 if (!pPage)
654 return;
655
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700656 CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, FALSE);
657 if (pPageView) {
658 pPageView->SetValid(FALSE);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800659 // RemovePageView() takes care of the delete for us.
660 pSDKDoc->RemovePageView(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700661 }
662}
663
664DLLEXPORT void STDCALL FORM_DoDocumentJSAction(FPDF_FORMHANDLE hHandle) {
Tom Sepezfe351db2016-01-29 16:26:27 -0800665 CPDFSDK_Document* pSDKDoc = CPDFSDK_Document::FromFPDFFormHandle(hHandle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700666 if (pSDKDoc && ((CPDFDoc_Environment*)hHandle)->IsJSInitiated())
667 pSDKDoc->ProcJavascriptFun();
668}
669
670DLLEXPORT void STDCALL FORM_DoDocumentOpenAction(FPDF_FORMHANDLE hHandle) {
Tom Sepezfe351db2016-01-29 16:26:27 -0800671 CPDFSDK_Document* pSDKDoc = CPDFSDK_Document::FromFPDFFormHandle(hHandle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700672 if (pSDKDoc && ((CPDFDoc_Environment*)hHandle)->IsJSInitiated())
673 pSDKDoc->ProcOpenAction();
674}
675
676DLLEXPORT void STDCALL FORM_DoDocumentAAction(FPDF_FORMHANDLE hHandle,
677 int aaType) {
Tom Sepezfe351db2016-01-29 16:26:27 -0800678 CPDFSDK_Document* pSDKDoc = CPDFSDK_Document::FromFPDFFormHandle(hHandle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700679 if (!pSDKDoc)
680 return;
681
Tom Sepez50d12ad2015-11-24 09:50:51 -0800682 CPDF_Document* pDoc = pSDKDoc->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700683 CPDF_Dictionary* pDic = pDoc->GetRoot();
684 if (!pDic)
685 return;
Tom Sepez468e5892015-10-13 15:49:36 -0700686
Wei Li0fc6b252016-03-01 16:29:41 -0800687 CPDF_AAction aa(pDic->GetDictBy("AA"));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700688 if (aa.ActionExist((CPDF_AAction::AActionType)aaType)) {
689 CPDF_Action action = aa.GetAction((CPDF_AAction::AActionType)aaType);
690 CPDFSDK_ActionHandler* pActionHandler =
691 ((CPDFDoc_Environment*)hHandle)->GetActionHander();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700692 pActionHandler->DoAction_Document(action, (CPDF_AAction::AActionType)aaType,
693 pSDKDoc);
694 }
695}
696
697DLLEXPORT void STDCALL FORM_DoPageAAction(FPDF_PAGE page,
698 FPDF_FORMHANDLE hHandle,
699 int aaType) {
Tom Sepez540c4362015-11-24 13:33:57 -0800700 if (!hHandle)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700701 return;
702 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetSDKDocument();
Tom Sepez540c4362015-11-24 13:33:57 -0800703 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
704 CPDF_Page* pPDFPage = CPDFPageFromFPDFPage(page);
Tom Sepezde4791d2015-10-30 12:13:10 -0700705 if (!pPDFPage)
706 return;
707 if (pSDKDoc->GetPageView(pPage, FALSE)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708 CPDFDoc_Environment* pEnv = pSDKDoc->GetEnv();
709 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
Tom Sepezde4791d2015-10-30 12:13:10 -0700710 CPDF_Dictionary* pPageDict = pPDFPage->m_pFormDict;
Wei Li0fc6b252016-03-01 16:29:41 -0800711 CPDF_AAction aa(pPageDict->GetDictBy("AA"));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700712 if (FPDFPAGE_AACTION_OPEN == aaType) {
Tom Sepezde4791d2015-10-30 12:13:10 -0700713 if (aa.ActionExist(CPDF_AAction::OpenPage)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700714 CPDF_Action action = aa.GetAction(CPDF_AAction::OpenPage);
715 pActionHandler->DoAction_Page(action, CPDF_AAction::OpenPage, pSDKDoc);
716 }
717 } else {
Tom Sepezde4791d2015-10-30 12:13:10 -0700718 if (aa.ActionExist(CPDF_AAction::ClosePage)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719 CPDF_Action action = aa.GetAction(CPDF_AAction::ClosePage);
720 pActionHandler->DoAction_Page(action, CPDF_AAction::ClosePage, pSDKDoc);
721 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700722 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700723 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700724}