blob: ba4b2ae622b5299882afe3cbf3af934b6537996f [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.
Tom Sepezc6ab1722015-02-05 15:27:25 -08004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#include "fpdfsdk/javascript/Document.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07008
Lei Zhang313c4252017-02-27 16:37:36 -08009#include <algorithm>
thestig7c292e02016-09-28 14:14:26 -070010#include <utility>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050011#include <vector>
12
dsinclairbc5e6d22016-10-04 11:08:49 -070013#include "core/fpdfapi/font/cpdf_font.h"
dsinclair41872fa2016-10-04 11:29:35 -070014#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070015#include "core/fpdfapi/parser/cpdf_array.h"
16#include "core/fpdfapi/parser/cpdf_document.h"
tsepez0e606b52016-11-18 16:22:41 -080017#include "core/fpdfapi/parser/cpdf_string.h"
dsinclair488b7ad2016-10-04 11:55:50 -070018#include "core/fpdfapi/parser/fpdf_parser_decode.h"
dsinclair1727aee2016-09-29 13:12:56 -070019#include "core/fpdfdoc/cpdf_interform.h"
20#include "core/fpdfdoc/cpdf_nametree.h"
tsepezd805eec2017-01-11 14:03:54 -080021#include "fpdfsdk/cpdfsdk_annotiteration.h"
dsinclair735606d2016-10-05 15:47:02 -070022#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070023#include "fpdfsdk/cpdfsdk_interform.h"
24#include "fpdfsdk/cpdfsdk_pageview.h"
25#include "fpdfsdk/cpdfsdk_widget.h"
tonikitoo618cb1f2016-08-18 20:10:17 -070026#include "fpdfsdk/javascript/Annot.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040027#include "fpdfsdk/javascript/Field.h"
28#include "fpdfsdk/javascript/Icon.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040029#include "fpdfsdk/javascript/JS_Define.h"
30#include "fpdfsdk/javascript/JS_EventHandler.h"
31#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040032#include "fpdfsdk/javascript/JS_Value.h"
33#include "fpdfsdk/javascript/app.h"
Tom Sepezd6ae2af2017-02-16 11:49:55 -080034#include "fpdfsdk/javascript/cjs_event_context.h"
dsinclair64376be2016-03-31 20:03:24 -070035#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040036#include "fpdfsdk/javascript/resource.h"
Lei Zhang8241df72015-11-06 14:38:48 -080037#include "third_party/base/numerics/safe_math.h"
thestigdadea5f2016-10-19 13:29:48 -070038#include "third_party/base/ptr_util.h"
Chris Palmer9108ad22014-06-26 16:01:46 -070039
Tom Sepez04557b82017-02-16 09:43:10 -080040JSConstSpec CJS_PrintParamsObj::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041
Tom Sepez04557b82017-02-16 09:43:10 -080042JSPropertySpec CJS_PrintParamsObj::PropertySpecs[] = {{0, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043
Tom Sepez04557b82017-02-16 09:43:10 -080044JSMethodSpec CJS_PrintParamsObj::MethodSpecs[] = {{0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045
46IMPLEMENT_JS_CLASS(CJS_PrintParamsObj, PrintParamsObj)
47
48PrintParamsObj::PrintParamsObj(CJS_Object* pJSObject)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 : CJS_EmbedObj(pJSObject) {
tsepez4cf55152016-11-02 14:37:54 -070050 bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 nStart = 0;
52 nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -070053 bSilent = false;
54 bShrinkToFit = false;
55 bPrintAsImage = false;
56 bReverse = false;
57 bAnnotations = true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070058}
59
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060#define MINWIDTH 5.0f
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061#define MINHEIGHT 5.0f
62
Tom Sepez04557b82017-02-16 09:43:10 -080063JSConstSpec CJS_Document::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064
Tom Sepez04557b82017-02-16 09:43:10 -080065JSPropertySpec CJS_Document::PropertySpecs[] = {
Tom Sepez4d5b8c52017-02-21 15:17:07 -080066 {"ADBE", get_ADBE_static, set_ADBE_static},
67 {"author", get_author_static, set_author_static},
68 {"baseURL", get_baseURL_static, set_baseURL_static},
69 {"bookmarkRoot", get_bookmarkRoot_static, set_bookmarkRoot_static},
70 {"calculate", get_calculate_static, set_calculate_static},
71 {"Collab", get_Collab_static, set_Collab_static},
72 {"creationDate", get_creationDate_static, set_creationDate_static},
73 {"creator", get_creator_static, set_creator_static},
74 {"delay", get_delay_static, set_delay_static},
75 {"dirty", get_dirty_static, set_dirty_static},
76 {"documentFileName", get_documentFileName_static,
Tom Sepez04557b82017-02-16 09:43:10 -080077 set_documentFileName_static},
Tom Sepez4d5b8c52017-02-21 15:17:07 -080078 {"external", get_external_static, set_external_static},
79 {"filesize", get_filesize_static, set_filesize_static},
80 {"icons", get_icons_static, set_icons_static},
81 {"info", get_info_static, set_info_static},
82 {"keywords", get_keywords_static, set_keywords_static},
83 {"layout", get_layout_static, set_layout_static},
84 {"media", get_media_static, set_media_static},
85 {"modDate", get_modDate_static, set_modDate_static},
86 {"mouseX", get_mouseX_static, set_mouseX_static},
87 {"mouseY", get_mouseY_static, set_mouseY_static},
88 {"numFields", get_numFields_static, set_numFields_static},
89 {"numPages", get_numPages_static, set_numPages_static},
90 {"pageNum", get_pageNum_static, set_pageNum_static},
91 {"pageWindowRect", get_pageWindowRect_static, set_pageWindowRect_static},
92 {"path", get_path_static, set_path_static},
93 {"producer", get_producer_static, set_producer_static},
94 {"subject", get_subject_static, set_subject_static},
95 {"title", get_title_static, set_title_static},
96 {"URL", get_URL_static, set_URL_static},
97 {"zoom", get_zoom_static, set_zoom_static},
98 {"zoomType", get_zoomType_static, set_zoomType_static},
Tom Sepez04557b82017-02-16 09:43:10 -080099 {0, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100
Tom Sepez04557b82017-02-16 09:43:10 -0800101JSMethodSpec CJS_Document::MethodSpecs[] = {
Tom Sepez9b99b632017-02-21 15:05:57 -0800102 {"addAnnot", addAnnot_static},
103 {"addField", addField_static},
104 {"addLink", addLink_static},
105 {"addIcon", addIcon_static},
106 {"calculateNow", calculateNow_static},
107 {"closeDoc", closeDoc_static},
108 {"createDataObject", createDataObject_static},
109 {"deletePages", deletePages_static},
110 {"exportAsText", exportAsText_static},
111 {"exportAsFDF", exportAsFDF_static},
112 {"exportAsXFDF", exportAsXFDF_static},
113 {"extractPages", extractPages_static},
114 {"getAnnot", getAnnot_static},
115 {"getAnnots", getAnnots_static},
116 {"getAnnot3D", getAnnot3D_static},
117 {"getAnnots3D", getAnnots3D_static},
118 {"getField", getField_static},
119 {"getIcon", getIcon_static},
120 {"getLinks", getLinks_static},
121 {"getNthFieldName", getNthFieldName_static},
122 {"getOCGs", getOCGs_static},
123 {"getPageBox", getPageBox_static},
124 {"getPageNthWord", getPageNthWord_static},
125 {"getPageNthWordQuads", getPageNthWordQuads_static},
126 {"getPageNumWords", getPageNumWords_static},
127 {"getPrintParams", getPrintParams_static},
128 {"getURL", getURL_static},
129 {"gotoNamedDest", gotoNamedDest_static},
130 {"importAnFDF", importAnFDF_static},
131 {"importAnXFDF", importAnXFDF_static},
132 {"importTextData", importTextData_static},
133 {"insertPages", insertPages_static},
134 {"mailForm", mailForm_static},
135 {"print", print_static},
136 {"removeField", removeField_static},
137 {"replacePages", replacePages_static},
138 {"resetForm", resetForm_static},
139 {"removeIcon", removeIcon_static},
140 {"saveAs", saveAs_static},
141 {"submitForm", submitForm_static},
142 {"syncAnnotScan", syncAnnotScan_static},
143 {"mailDoc", mailDoc_static},
Tom Sepez04557b82017-02-16 09:43:10 -0800144 {0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700145
146IMPLEMENT_JS_CLASS(CJS_Document, Document)
147
Tom Sepez33420902015-10-13 15:00:10 -0700148void CJS_Document::InitInstance(IJS_Runtime* pIRuntime) {
149 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime);
150 Document* pDoc = static_cast<Document*>(GetEmbedObject());
dsinclair82e17672016-10-11 12:38:01 -0700151 pDoc->SetFormFillEnv(pRuntime->GetFormFillEnv());
Tom Sepez33420902015-10-13 15:00:10 -0700152}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154Document::Document(CJS_Object* pJSObject)
155 : CJS_EmbedObj(pJSObject),
dsinclair82e17672016-10-11 12:38:01 -0700156 m_pFormFillEnv(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 m_cwBaseURL(L""),
tsepez4cf55152016-11-02 14:37:54 -0700158 m_bDelay(false) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160Document::~Document() {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161}
162
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163// the total number of fileds in document.
Tom Sepezb1670b52017-02-16 17:01:00 -0800164bool Document::numFields(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700165 CJS_PropValue& vp,
166 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700168 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700169 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170 }
dsinclair82e17672016-10-11 12:38:01 -0700171 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700172 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700173 return false;
tsepez55be02e2016-09-12 11:21:42 -0700174 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700175 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700177 vp << static_cast<int>(pPDFForm->CountFields(CFX_WideString()));
tsepez4cf55152016-11-02 14:37:54 -0700178 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700179}
180
Tom Sepezb1670b52017-02-16 17:01:00 -0800181bool Document::dirty(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700182 CJS_PropValue& vp,
183 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700184 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700185 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700186 return false;
tsepez55be02e2016-09-12 11:21:42 -0700187 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 if (vp.IsGetting()) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700189 vp << !!m_pFormFillEnv->GetChangeMark();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800190 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800192 bool bChanged = false;
193 vp >> bChanged;
194 if (bChanged)
195 m_pFormFillEnv->SetChangeMark();
196 else
197 m_pFormFillEnv->ClearChangeMark();
198
tsepez4cf55152016-11-02 14:37:54 -0700199 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200}
201
Tom Sepezb1670b52017-02-16 17:01:00 -0800202bool Document::ADBE(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700203 CJS_PropValue& vp,
204 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700205 if (vp.IsGetting())
Tom Sepezb1670b52017-02-16 17:01:00 -0800206 vp.GetJSValue()->SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207
tsepez4cf55152016-11-02 14:37:54 -0700208 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209}
210
Tom Sepezb1670b52017-02-16 17:01:00 -0800211bool Document::pageNum(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700212 CJS_PropValue& vp,
213 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700214 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700215 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700216 return false;
tsepez55be02e2016-09-12 11:21:42 -0700217 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 if (vp.IsGetting()) {
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800219 if (CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetCurrentView())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 vp << pPageView->GetPageIndex();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800221 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800223 int iPageCount = m_pFormFillEnv->GetPageCount();
224 int iPageNum = 0;
225 vp >> iPageNum;
226 if (iPageNum >= 0 && iPageNum < iPageCount)
227 m_pFormFillEnv->JS_docgotoPage(iPageNum);
228 else if (iPageNum >= iPageCount)
229 m_pFormFillEnv->JS_docgotoPage(iPageCount - 1);
230 else if (iPageNum < 0)
231 m_pFormFillEnv->JS_docgotoPage(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232
tsepez4cf55152016-11-02 14:37:54 -0700233 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234}
235
Tom Sepezb1670b52017-02-16 17:01:00 -0800236bool Document::addAnnot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700237 const std::vector<CJS_Value>& params,
238 CJS_Value& vRet,
239 CFX_WideString& sError) {
240 // Not supported.
241 return true;
242}
243
Tom Sepezb1670b52017-02-16 17:01:00 -0800244bool Document::addField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700245 const std::vector<CJS_Value>& params,
246 CJS_Value& vRet,
247 CFX_WideString& sError) {
248 // Not supported.
249 return true;
250}
251
Tom Sepezb1670b52017-02-16 17:01:00 -0800252bool Document::exportAsText(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700253 const std::vector<CJS_Value>& params,
254 CJS_Value& vRet,
255 CFX_WideString& sError) {
256 // Unsafe, not supported.
257 return true;
258}
259
Tom Sepezb1670b52017-02-16 17:01:00 -0800260bool Document::exportAsFDF(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800261 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 CJS_Value& vRet,
263 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -0800264 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700265 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700266}
267
Tom Sepezb1670b52017-02-16 17:01:00 -0800268bool Document::exportAsXFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700269 const std::vector<CJS_Value>& params,
270 CJS_Value& vRet,
271 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700273 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275
Tom Sepezb1670b52017-02-16 17:01:00 -0800276bool Document::getField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700277 const std::vector<CJS_Value>& params,
278 CJS_Value& vRet,
279 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700281 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700282 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 }
dsinclair82e17672016-10-11 12:38:01 -0700284 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700285 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700286 return false;
tsepez55be02e2016-09-12 11:21:42 -0700287 }
tsepezb4694242016-08-15 16:44:55 -0700288 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700289 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
291 if (pPDFForm->CountFields(wideName) <= 0) {
tsepezf3dc8c62016-08-10 06:29:29 -0700292 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700293 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 }
295
tsepezf3dc8c62016-08-10 06:29:29 -0700296 v8::Local<v8::Object> pFieldObj =
tsepezb4694242016-08-15 16:44:55 -0700297 pRuntime->NewFxDynamicObj(CJS_Field::g_nObjDefnID);
Tom Sepezc5a14722017-02-24 15:31:12 -0800298 if (pFieldObj.IsEmpty())
299 return false;
300
tsepezf3dc8c62016-08-10 06:29:29 -0700301 CJS_Field* pJSField =
tsepezb4694242016-08-15 16:44:55 -0700302 static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
tsepezf3dc8c62016-08-10 06:29:29 -0700303 Field* pField = static_cast<Field*>(pJSField->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 pField->AttachField(this, wideName);
tsepezf3dc8c62016-08-10 06:29:29 -0700305 vRet = CJS_Value(pRuntime, pJSField);
tsepez4cf55152016-11-02 14:37:54 -0700306 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307}
308
309// Gets the name of the nth field in the document
Tom Sepezb1670b52017-02-16 17:01:00 -0800310bool Document::getNthFieldName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700311 const std::vector<CJS_Value>& params,
312 CJS_Value& vRet,
313 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700314 if (params.size() != 1) {
315 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700316 return false;
tsepezcd5dc852016-09-08 11:23:24 -0700317 }
dsinclair82e17672016-10-11 12:38:01 -0700318 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700319 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700320 return false;
tsepez55be02e2016-09-12 11:21:42 -0700321 }
tsepezb4694242016-08-15 16:44:55 -0700322 int nIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 if (nIndex < 0) {
tsepezcd5dc852016-09-08 11:23:24 -0700324 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700325 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700327 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700329 CPDF_FormField* pField = pPDFForm->GetField(nIndex, CFX_WideString());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 if (!pField)
tsepez4cf55152016-11-02 14:37:54 -0700331 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332
tsepezf3dc8c62016-08-10 06:29:29 -0700333 vRet = CJS_Value(pRuntime, pField->GetFullName().c_str());
tsepez4cf55152016-11-02 14:37:54 -0700334 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335}
336
Tom Sepezb1670b52017-02-16 17:01:00 -0800337bool Document::importAnFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700338 const std::vector<CJS_Value>& params,
339 CJS_Value& vRet,
340 CFX_WideString& sError) {
341 // Unsafe, not supported.
342 return true;
343}
344
Tom Sepezb1670b52017-02-16 17:01:00 -0800345bool Document::importAnXFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700346 const std::vector<CJS_Value>& params,
347 CJS_Value& vRet,
348 CFX_WideString& sError) {
349 // Unsafe, not supported.
350 return true;
351}
352
Tom Sepezb1670b52017-02-16 17:01:00 -0800353bool Document::importTextData(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800354 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 CJS_Value& vRet,
356 CFX_WideString& sError) {
357 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700358 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359}
360
361// exports the form data and mails the resulting fdf file as an attachment to
362// all recipients.
363// comment: need reader supports
Tom Sepezb1670b52017-02-16 17:01:00 -0800364bool Document::mailForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700365 const std::vector<CJS_Value>& params,
366 CJS_Value& vRet,
367 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700368 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700369 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700370 return false;
tsepez55be02e2016-09-12 11:21:42 -0700371 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700372 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -0700373 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700374 return false;
tsepez55be02e2016-09-12 11:21:42 -0700375 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 int iLength = params.size();
tsepez4cf55152016-11-02 14:37:54 -0700377 bool bUI = iLength > 0 ? params[0].ToBool(pRuntime) : true;
tsepezb4694242016-08-15 16:44:55 -0700378 CFX_WideString cTo = iLength > 1 ? params[1].ToCFXWideString(pRuntime) : L"";
379 CFX_WideString cCc = iLength > 2 ? params[2].ToCFXWideString(pRuntime) : L"";
380 CFX_WideString cBcc = iLength > 3 ? params[3].ToCFXWideString(pRuntime) : L"";
tsepezf3dc8c62016-08-10 06:29:29 -0700381 CFX_WideString cSubject =
tsepezb4694242016-08-15 16:44:55 -0700382 iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
383 CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
dsinclair7cbe68e2016-10-12 11:56:23 -0700384 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 CFX_ByteTextBuf textBuf;
386 if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
tsepez4cf55152016-11-02 14:37:54 -0700387 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 pRuntime->BeginBlock();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800390 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
dsinclair8779fa82016-10-12 12:05:44 -0700391 pFormFillEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI,
392 cTo.c_str(), cSubject.c_str(), cCc.c_str(),
393 cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700395 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396}
397
Tom Sepezb1670b52017-02-16 17:01:00 -0800398bool Document::print(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700399 const std::vector<CJS_Value>& params,
400 CJS_Value& vRet,
401 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700402 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700403 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700404 return false;
tsepez55be02e2016-09-12 11:21:42 -0700405 }
tsepez4cf55152016-11-02 14:37:54 -0700406 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700407 int nStart = 0;
408 int nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -0700409 bool bSilent = false;
410 bool bShrinkToFit = false;
411 bool bPrintAsImage = false;
412 bool bReverse = false;
413 bool bAnnotations = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414 int nlength = params.size();
415 if (nlength == 9) {
tsepez40faa792016-07-15 17:58:02 -0700416 if (params[8].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700417 v8::Local<v8::Object> pObj = params[8].ToV8Object(pRuntime);
418 if (CFXJS_Engine::GetObjDefnID(pObj) ==
419 CJS_PrintParamsObj::g_nObjDefnID) {
420 if (CJS_Object* pJSObj = params[8].ToCJSObject(pRuntime)) {
tsepez40faa792016-07-15 17:58:02 -0700421 if (PrintParamsObj* pprintparamsObj =
422 static_cast<PrintParamsObj*>(pJSObj->GetEmbedObject())) {
423 bUI = pprintparamsObj->bUI;
424 nStart = pprintparamsObj->nStart;
425 nEnd = pprintparamsObj->nEnd;
426 bSilent = pprintparamsObj->bSilent;
427 bShrinkToFit = pprintparamsObj->bShrinkToFit;
428 bPrintAsImage = pprintparamsObj->bPrintAsImage;
429 bReverse = pprintparamsObj->bReverse;
430 bAnnotations = pprintparamsObj->bAnnotations;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700432 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 }
434 }
435 } else {
436 if (nlength >= 1)
tsepezb4694242016-08-15 16:44:55 -0700437 bUI = params[0].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700438 if (nlength >= 2)
tsepezb4694242016-08-15 16:44:55 -0700439 nStart = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440 if (nlength >= 3)
tsepezb4694242016-08-15 16:44:55 -0700441 nEnd = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 if (nlength >= 4)
tsepezb4694242016-08-15 16:44:55 -0700443 bSilent = params[3].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444 if (nlength >= 5)
tsepezb4694242016-08-15 16:44:55 -0700445 bShrinkToFit = params[4].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 if (nlength >= 6)
tsepezb4694242016-08-15 16:44:55 -0700447 bPrintAsImage = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 if (nlength >= 7)
tsepezb4694242016-08-15 16:44:55 -0700449 bReverse = params[6].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450 if (nlength >= 8)
tsepezb4694242016-08-15 16:44:55 -0700451 bAnnotations = params[7].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 }
453
dsinclair82e17672016-10-11 12:38:01 -0700454 if (m_pFormFillEnv) {
455 m_pFormFillEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit,
456 bPrintAsImage, bReverse, bAnnotations);
tsepez4cf55152016-11-02 14:37:54 -0700457 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700458 }
tsepez4cf55152016-11-02 14:37:54 -0700459 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460}
461
462// removes the specified field from the document.
463// comment:
dsinclair1897bdc2016-09-06 14:18:57 -0700464// note: if the filed name is not rational, adobe is dumb for it.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465
Tom Sepezb1670b52017-02-16 17:01:00 -0800466bool Document::removeField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700467 const std::vector<CJS_Value>& params,
468 CJS_Value& vRet,
469 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700470 if (params.size() != 1) {
471 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700472 return false;
tsepez55be02e2016-09-12 11:21:42 -0700473 }
dsinclair82e17672016-10-11 12:38:01 -0700474 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700475 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700476 return false;
tsepez55be02e2016-09-12 11:21:42 -0700477 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700478 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
479 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM))) {
tsepezcd5dc852016-09-08 11:23:24 -0700480 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700481 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482 }
tsepezb4694242016-08-15 16:44:55 -0700483 CFX_WideString sFieldName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700484 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
tsepez8fa82792017-01-11 09:32:33 -0800485 std::vector<CPDFSDK_Annot::ObservedPtr> widgets;
Lei Zhangd88a3642015-11-10 09:38:57 -0800486 pInterForm->GetWidgets(sFieldName, &widgets);
Lei Zhangd88a3642015-11-10 09:38:57 -0800487 if (widgets.empty())
tsepez4cf55152016-11-02 14:37:54 -0700488 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700489
tsepez8fa82792017-01-11 09:32:33 -0800490 for (const auto& pAnnot : widgets) {
491 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot.Get());
492 if (!pWidget)
493 continue;
494
Tom Sepez281a9ea2016-02-26 14:24:28 -0800495 CFX_FloatRect rcAnnot = pWidget->GetRect();
Lei Zhangd88a3642015-11-10 09:38:57 -0800496 --rcAnnot.left;
497 --rcAnnot.bottom;
498 ++rcAnnot.right;
499 ++rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700500
tsepezdf964df2016-04-21 12:09:41 -0700501 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800502 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Lei Zhangd88a3642015-11-10 09:38:57 -0800503 ASSERT(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700504
dsinclair1897bdc2016-09-06 14:18:57 -0700505 // If there is currently no pageview associated with the page being used
506 // do not create one. We may be in the process of tearing down the document
507 // and creating a new pageview at this point will cause bad things.
dsinclair7cbe68e2016-10-12 11:56:23 -0700508 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, false);
dsinclair1897bdc2016-09-06 14:18:57 -0700509 if (pPageView) {
dsinclair8afe15a2016-10-05 12:00:34 -0700510#if PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700511 pPageView->DeleteAnnot(pWidget);
dsinclair8afe15a2016-10-05 12:00:34 -0700512#endif // PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700513 pPageView->UpdateRects(aRefresh);
514 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700515 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700516 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700517
tsepez4cf55152016-11-02 14:37:54 -0700518 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700519}
520
521// reset filed values within a document.
522// comment:
523// note: if the fields names r not rational, aodbe is dumb for it.
524
Tom Sepezb1670b52017-02-16 17:01:00 -0800525bool Document::resetForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700526 const std::vector<CJS_Value>& params,
527 CJS_Value& vRet,
528 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700529 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700530 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700531 return false;
tsepez55be02e2016-09-12 11:21:42 -0700532 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700533 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
534 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
535 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -0700536 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700537 return false;
tsepez55be02e2016-09-12 11:21:42 -0700538 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700539
dsinclair7cbe68e2016-10-12 11:56:23 -0700540 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
tsepeze5aff742016-08-08 09:49:42 -0700542 CJS_Array aName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543
Lei Zhangdd734de2015-11-11 10:16:52 -0800544 if (params.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700545 pPDFForm->ResetForm(true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700546 m_pFormFillEnv->SetChangeMark();
tsepez4cf55152016-11-02 14:37:54 -0700547 return true;
Lei Zhangd88a3642015-11-10 09:38:57 -0800548 }
549
550 switch (params[0].GetType()) {
551 default:
tsepezb4694242016-08-15 16:44:55 -0700552 aName.Attach(params[0].ToV8Array(pRuntime));
Lei Zhangd88a3642015-11-10 09:38:57 -0800553 break;
554 case CJS_Value::VT_string:
tsepezb4694242016-08-15 16:44:55 -0700555 aName.SetElement(pRuntime, 0, params[0]);
Lei Zhangd88a3642015-11-10 09:38:57 -0800556 break;
557 }
558
559 std::vector<CPDF_FormField*> aFields;
tsepezb4694242016-08-15 16:44:55 -0700560 for (int i = 0, isz = aName.GetLength(pRuntime); i < isz; ++i) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800561 CJS_Value valElement(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700562 aName.GetElement(pRuntime, i, valElement);
563 CFX_WideString swVal = valElement.ToCFXWideString(pRuntime);
Lei Zhangd88a3642015-11-10 09:38:57 -0800564 for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j)
565 aFields.push_back(pPDFForm->GetField(j, swVal));
566 }
567
568 if (!aFields.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700569 pPDFForm->ResetForm(aFields, true, true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700570 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700571 }
572
tsepez4cf55152016-11-02 14:37:54 -0700573 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700574}
575
Tom Sepezb1670b52017-02-16 17:01:00 -0800576bool Document::saveAs(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700577 const std::vector<CJS_Value>& params,
578 CJS_Value& vRet,
579 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700580 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700581 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582}
583
Tom Sepezb1670b52017-02-16 17:01:00 -0800584bool Document::syncAnnotScan(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800585 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586 CJS_Value& vRet,
587 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700588 return true;
589}
590
Tom Sepezb1670b52017-02-16 17:01:00 -0800591bool Document::submitForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700592 const std::vector<CJS_Value>& params,
593 CJS_Value& vRet,
594 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700595 int nSize = params.size();
596 if (nSize < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700597 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700598 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599 }
dsinclair82e17672016-10-11 12:38:01 -0700600 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700601 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700602 return false;
tsepez55be02e2016-09-12 11:21:42 -0700603 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800604
tsepeze5aff742016-08-08 09:49:42 -0700605 CJS_Array aFields;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700606 CFX_WideString strURL;
tsepez4cf55152016-11-02 14:37:54 -0700607 bool bFDF = true;
608 bool bEmpty = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609 CJS_Value v = params[0];
Tom Sepez39bfe122015-09-17 15:25:23 -0700610 if (v.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700611 strURL = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700612 if (nSize > 1)
tsepezb4694242016-08-15 16:44:55 -0700613 bFDF = params[1].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 if (nSize > 2)
tsepezb4694242016-08-15 16:44:55 -0700615 bEmpty = params[2].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700616 if (nSize > 3)
tsepezb4694242016-08-15 16:44:55 -0700617 aFields.Attach(params[3].ToV8Array(pRuntime));
Tom Sepez39bfe122015-09-17 15:25:23 -0700618 } else if (v.GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700619 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
620 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"cURL");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700621 if (!pValue.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700622 strURL = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700623
tsepezb4694242016-08-15 16:44:55 -0700624 pValue = pRuntime->GetObjectProperty(pObj, L"bFDF");
625 bFDF = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700626
tsepezb4694242016-08-15 16:44:55 -0700627 pValue = pRuntime->GetObjectProperty(pObj, L"bEmpty");
628 bEmpty = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700629
tsepezb4694242016-08-15 16:44:55 -0700630 pValue = pRuntime->GetObjectProperty(pObj, L"aFields");
631 aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700632 }
633
dsinclair7cbe68e2016-10-12 11:56:23 -0700634 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700635 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
tsepezb4694242016-08-15 16:44:55 -0700636 if (aFields.GetLength(pRuntime) == 0 && bEmpty) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800637 if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700638 pRuntime->BeginBlock();
tsepez4cf55152016-11-02 14:37:54 -0700639 pInterForm->SubmitForm(strURL, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640 pRuntime->EndBlock();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700641 }
tsepez4cf55152016-11-02 14:37:54 -0700642 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643 }
644
Lei Zhangd88a3642015-11-10 09:38:57 -0800645 std::vector<CPDF_FormField*> fieldObjects;
tsepezb4694242016-08-15 16:44:55 -0700646 for (int i = 0, sz = aFields.GetLength(pRuntime); i < sz; ++i) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700647 CJS_Value valName(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700648 aFields.GetElement(pRuntime, i, valName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700649
tsepezb4694242016-08-15 16:44:55 -0700650 CFX_WideString sName = valName.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700651 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
652 for (int j = 0, jsz = pPDFForm->CountFields(sName); j < jsz; ++j) {
653 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
654 if (!bEmpty && pField->GetValue().IsEmpty())
655 continue;
656
Lei Zhangd88a3642015-11-10 09:38:57 -0800657 fieldObjects.push_back(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700658 }
659 }
660
Lei Zhangd88a3642015-11-10 09:38:57 -0800661 if (pPDFInterForm->CheckRequiredFields(&fieldObjects, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700662 pRuntime->BeginBlock();
Wei Li97da9762016-03-11 17:00:48 -0800663 pInterForm->SubmitFields(strURL, fieldObjects, true, !bFDF);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700664 pRuntime->EndBlock();
665 }
tsepez4cf55152016-11-02 14:37:54 -0700666 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700667}
668
dsinclair82e17672016-10-11 12:38:01 -0700669void Document::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
670 m_pFormFillEnv.Reset(pFormFillEnv);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700671}
672
Tom Sepezb1670b52017-02-16 17:01:00 -0800673bool Document::bookmarkRoot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700674 CJS_PropValue& vp,
675 CFX_WideString& sError) {
676 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700677}
678
Tom Sepezb1670b52017-02-16 17:01:00 -0800679bool Document::mailDoc(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700680 const std::vector<CJS_Value>& params,
681 CJS_Value& vRet,
682 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700683 // TODO(tsepez): Check maximum number of allowed params.
tsepez4cf55152016-11-02 14:37:54 -0700684 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700685 CFX_WideString cTo = L"";
686 CFX_WideString cCc = L"";
687 CFX_WideString cBcc = L"";
688 CFX_WideString cSubject = L"";
689 CFX_WideString cMsg = L"";
690
tsepezf3dc8c62016-08-10 06:29:29 -0700691 if (params.size() >= 1)
tsepezb4694242016-08-15 16:44:55 -0700692 bUI = params[0].ToBool(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700693 if (params.size() >= 2)
tsepezb4694242016-08-15 16:44:55 -0700694 cTo = params[1].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700695 if (params.size() >= 3)
tsepezb4694242016-08-15 16:44:55 -0700696 cCc = params[2].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700697 if (params.size() >= 4)
tsepezb4694242016-08-15 16:44:55 -0700698 cBcc = params[3].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700699 if (params.size() >= 5)
tsepezb4694242016-08-15 16:44:55 -0700700 cSubject = params[4].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700701 if (params.size() >= 6)
tsepezb4694242016-08-15 16:44:55 -0700702 cMsg = params[5].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700703
Tom Sepez39bfe122015-09-17 15:25:23 -0700704 if (params.size() >= 1 && params[0].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700705 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700706
tsepezb4694242016-08-15 16:44:55 -0700707 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"bUI");
tsepez71ba5882016-10-28 09:35:49 -0700708 bUI = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700709
tsepezb4694242016-08-15 16:44:55 -0700710 pValue = pRuntime->GetObjectProperty(pObj, L"cTo");
711 cTo = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700712
tsepezb4694242016-08-15 16:44:55 -0700713 pValue = pRuntime->GetObjectProperty(pObj, L"cCc");
714 cCc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700715
tsepezb4694242016-08-15 16:44:55 -0700716 pValue = pRuntime->GetObjectProperty(pObj, L"cBcc");
717 cBcc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718
tsepezb4694242016-08-15 16:44:55 -0700719 pValue = pRuntime->GetObjectProperty(pObj, L"cSubject");
720 cSubject = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700721
tsepezb4694242016-08-15 16:44:55 -0700722 pValue = pRuntime->GetObjectProperty(pObj, L"cMsg");
723 cMsg = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700724 }
725
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700726 pRuntime->BeginBlock();
dsinclair8779fa82016-10-12 12:05:44 -0700727 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
728 pFormFillEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
729 cCc.c_str(), cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700730 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700731 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700732}
733
Tom Sepezb1670b52017-02-16 17:01:00 -0800734bool Document::author(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700735 CJS_PropValue& vp,
736 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800737 return getPropertyInternal(pRuntime, vp, "Author", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700738}
739
Tom Sepezb1670b52017-02-16 17:01:00 -0800740bool Document::info(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700741 CJS_PropValue& vp,
742 CFX_WideString& sError) {
tonikitooec268762016-08-10 12:29:28 -0700743 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700744 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700745 return false;
tonikitooec268762016-08-10 12:29:28 -0700746 }
dsinclair82e17672016-10-11 12:38:01 -0700747 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700748 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700749 return false;
tsepez55be02e2016-09-12 11:21:42 -0700750 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700751 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700752 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700753 return false;
Tom Sepezc6ab1722015-02-05 15:27:25 -0800754
dsinclair38fd8442016-09-15 10:15:32 -0700755 CFX_WideString cwAuthor = pDictionary->GetUnicodeTextFor("Author");
756 CFX_WideString cwTitle = pDictionary->GetUnicodeTextFor("Title");
757 CFX_WideString cwSubject = pDictionary->GetUnicodeTextFor("Subject");
758 CFX_WideString cwKeywords = pDictionary->GetUnicodeTextFor("Keywords");
759 CFX_WideString cwCreator = pDictionary->GetUnicodeTextFor("Creator");
760 CFX_WideString cwProducer = pDictionary->GetUnicodeTextFor("Producer");
761 CFX_WideString cwCreationDate =
762 pDictionary->GetUnicodeTextFor("CreationDate");
763 CFX_WideString cwModDate = pDictionary->GetUnicodeTextFor("ModDate");
764 CFX_WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped");
Tom Sepezc6ab1722015-02-05 15:27:25 -0800765
tsepezb4694242016-08-15 16:44:55 -0700766 v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1);
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800767 pRuntime->PutObjectProperty(pObj, L"Author",
768 pRuntime->NewString(cwAuthor.AsStringC()));
769 pRuntime->PutObjectProperty(pObj, L"Title",
770 pRuntime->NewString(cwTitle.AsStringC()));
771 pRuntime->PutObjectProperty(pObj, L"Subject",
772 pRuntime->NewString(cwSubject.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800773 pRuntime->PutObjectProperty(pObj, L"Keywords",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800774 pRuntime->NewString(cwKeywords.AsStringC()));
775 pRuntime->PutObjectProperty(pObj, L"Creator",
776 pRuntime->NewString(cwCreator.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800777 pRuntime->PutObjectProperty(pObj, L"Producer",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800778 pRuntime->NewString(cwProducer.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800779 pRuntime->PutObjectProperty(pObj, L"CreationDate",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800780 pRuntime->NewString(cwCreationDate.AsStringC()));
781 pRuntime->PutObjectProperty(pObj, L"ModDate",
782 pRuntime->NewString(cwModDate.AsStringC()));
783 pRuntime->PutObjectProperty(pObj, L"Trapped",
784 pRuntime->NewString(cwTrapped.AsStringC()));
tonikitooec268762016-08-10 12:29:28 -0700785
786 // It's to be compatible to non-standard info dictionary.
787 for (const auto& it : *pDictionary) {
788 const CFX_ByteString& bsKey = it.first;
tsepez0e606b52016-11-18 16:22:41 -0800789 CPDF_Object* pValueObj = it.second.get();
tonikitooec268762016-08-10 12:29:28 -0700790 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
791 if (pValueObj->IsString() || pValueObj->IsName()) {
tsepeze6cf0132017-01-18 14:38:18 -0800792 pRuntime->PutObjectProperty(
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800793 pObj, wsKey,
794 pRuntime->NewString(pValueObj->GetUnicodeText().AsStringC()));
tonikitooec268762016-08-10 12:29:28 -0700795 } else if (pValueObj->IsNumber()) {
tsepeze6cf0132017-01-18 14:38:18 -0800796 pRuntime->PutObjectProperty(pObj, wsKey,
797 pRuntime->NewNumber(pValueObj->GetNumber()));
tonikitooec268762016-08-10 12:29:28 -0700798 } else if (pValueObj->IsBoolean()) {
tsepeze6cf0132017-01-18 14:38:18 -0800799 pRuntime->PutObjectProperty(
800 pObj, wsKey, pRuntime->NewBoolean(!!pValueObj->GetInteger()));
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700801 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700802 }
tonikitooec268762016-08-10 12:29:28 -0700803 vp << pObj;
tsepez4cf55152016-11-02 14:37:54 -0700804 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700805}
806
Tom Sepezb1670b52017-02-16 17:01:00 -0800807bool Document::getPropertyInternal(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700808 CJS_PropValue& vp,
809 const CFX_ByteString& propName,
810 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700811 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700812 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700813 return false;
tsepez55be02e2016-09-12 11:21:42 -0700814 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700815 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700817 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700818
819 if (vp.IsGetting()) {
dsinclair38fd8442016-09-15 10:15:32 -0700820 vp << pDictionary->GetUnicodeTextFor(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700821 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700822 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
tsepez55be02e2016-09-12 11:21:42 -0700823 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700824 return false;
tsepez55be02e2016-09-12 11:21:42 -0700825 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700826 CFX_WideString csProperty;
827 vp >> csProperty;
tsepez0e606b52016-11-18 16:22:41 -0800828 pDictionary->SetNewFor<CPDF_String>(propName, PDF_EncodeText(csProperty),
829 false);
dsinclair7cbe68e2016-10-12 11:56:23 -0700830 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700831 }
tsepez4cf55152016-11-02 14:37:54 -0700832 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700833}
834
Tom Sepezb1670b52017-02-16 17:01:00 -0800835bool Document::creationDate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700836 CJS_PropValue& vp,
837 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800838 return getPropertyInternal(pRuntime, vp, "CreationDate", sError);
tonikitooea3ff9e2016-08-02 11:52:28 -0700839}
840
Tom Sepezb1670b52017-02-16 17:01:00 -0800841bool Document::creator(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700842 CJS_PropValue& vp,
843 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800844 return getPropertyInternal(pRuntime, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700845}
846
Tom Sepezb1670b52017-02-16 17:01:00 -0800847bool Document::delay(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700848 CJS_PropValue& vp,
849 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700850 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700851 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700852 return false;
tsepez55be02e2016-09-12 11:21:42 -0700853 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700854 if (vp.IsGetting()) {
855 vp << m_bDelay;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800856 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700857 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800858 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
859 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
860 return false;
861 }
862 vp >> m_bDelay;
863 if (m_bDelay) {
864 m_DelayData.clear();
865 return true;
866 }
867 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
868 DelayDataToProcess.swap(m_DelayData);
869 for (const auto& pData : DelayDataToProcess)
870 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
871
tsepez4cf55152016-11-02 14:37:54 -0700872 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700873}
874
Tom Sepezb1670b52017-02-16 17:01:00 -0800875bool Document::keywords(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700876 CJS_PropValue& vp,
877 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800878 return getPropertyInternal(pRuntime, vp, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700879}
880
Tom Sepezb1670b52017-02-16 17:01:00 -0800881bool Document::modDate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700882 CJS_PropValue& vp,
883 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800884 return getPropertyInternal(pRuntime, vp, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885}
886
Tom Sepezb1670b52017-02-16 17:01:00 -0800887bool Document::producer(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700888 CJS_PropValue& vp,
889 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800890 return getPropertyInternal(pRuntime, vp, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700891}
892
Tom Sepezb1670b52017-02-16 17:01:00 -0800893bool Document::subject(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700894 CJS_PropValue& vp,
895 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800896 return getPropertyInternal(pRuntime, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897}
898
Tom Sepezb1670b52017-02-16 17:01:00 -0800899bool Document::title(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700900 CJS_PropValue& vp,
901 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700902 if (!m_pFormFillEnv || !m_pFormFillEnv->GetUnderlyingDocument()) {
tsepez55be02e2016-09-12 11:21:42 -0700903 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700904 return false;
tsepez55be02e2016-09-12 11:21:42 -0700905 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800906 return getPropertyInternal(pRuntime, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907}
908
Tom Sepezb1670b52017-02-16 17:01:00 -0800909bool Document::numPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700910 CJS_PropValue& vp,
911 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700912 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700913 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700914 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700915 }
dsinclair82e17672016-10-11 12:38:01 -0700916 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700917 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700918 return false;
tsepez55be02e2016-09-12 11:21:42 -0700919 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700920 vp << m_pFormFillEnv->GetPageCount();
tsepez4cf55152016-11-02 14:37:54 -0700921 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700922}
923
Tom Sepezb1670b52017-02-16 17:01:00 -0800924bool Document::external(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700925 CJS_PropValue& vp,
926 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700927 // In Chrome case, should always return true.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700928 if (vp.IsGetting()) {
929 vp << true;
930 }
tsepez4cf55152016-11-02 14:37:54 -0700931 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700932}
933
Tom Sepezb1670b52017-02-16 17:01:00 -0800934bool Document::filesize(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700935 CJS_PropValue& vp,
936 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700938 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700939 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700940 }
941 vp << 0;
tsepez4cf55152016-11-02 14:37:54 -0700942 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700943}
944
Tom Sepezb1670b52017-02-16 17:01:00 -0800945bool Document::mouseX(CJS_Runtime* pRuntime,
tonikitoo33c4cdb2016-08-08 10:52:51 -0700946 CJS_PropValue& vp,
947 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700948 return true;
949}
950
Tom Sepezb1670b52017-02-16 17:01:00 -0800951bool Document::mouseY(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700952 CJS_PropValue& vp,
953 CFX_WideString& sError) {
954 return true;
955}
956
Tom Sepezb1670b52017-02-16 17:01:00 -0800957bool Document::URL(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800958 CJS_PropValue& vp,
959 CFX_WideString& sError) {
tonikitoo33c4cdb2016-08-08 10:52:51 -0700960 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700961 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700962 return false;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700963 }
dsinclair82e17672016-10-11 12:38:01 -0700964 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700965 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700966 return false;
tsepez55be02e2016-09-12 11:21:42 -0700967 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700968 vp << m_pFormFillEnv->JS_docGetFilePath();
tsepez4cf55152016-11-02 14:37:54 -0700969 return true;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700970}
971
Tom Sepezb1670b52017-02-16 17:01:00 -0800972bool Document::baseURL(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700973 CJS_PropValue& vp,
974 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700975 if (vp.IsGetting()) {
976 vp << m_cwBaseURL;
977 } else {
978 vp >> m_cwBaseURL;
979 }
tsepez4cf55152016-11-02 14:37:54 -0700980 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981}
982
Tom Sepezb1670b52017-02-16 17:01:00 -0800983bool Document::calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700984 CJS_PropValue& vp,
985 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700986 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700987 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700988 return false;
tsepez55be02e2016-09-12 11:21:42 -0700989 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700990 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700991 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -0700992 vp << !!pInterForm->IsCalculateEnabled();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800993 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700994 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800995 bool bCalculate;
996 vp >> bCalculate;
997 pInterForm->EnableCalculate(bCalculate);
tsepez4cf55152016-11-02 14:37:54 -0700998 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999}
1000
Tom Sepezb1670b52017-02-16 17:01:00 -08001001bool Document::documentFileName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001002 CJS_PropValue& vp,
1003 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001005 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001006 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001007 }
dsinclair82e17672016-10-11 12:38:01 -07001008 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001009 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001010 return false;
tsepez55be02e2016-09-12 11:21:42 -07001011 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001012 CFX_WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001013 int32_t i = wsFilePath.GetLength() - 1;
1014 for (; i >= 0; i--) {
1015 if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
1016 break;
1017 }
1018 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
1019 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
1020 } else {
1021 vp << L"";
1022 }
tsepez4cf55152016-11-02 14:37:54 -07001023 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001024}
1025
Tom Sepezb1670b52017-02-16 17:01:00 -08001026bool Document::path(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001027 CJS_PropValue& vp,
1028 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001029 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001030 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001031 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001032 }
dsinclair82e17672016-10-11 12:38:01 -07001033 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001034 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001035 return false;
tsepez55be02e2016-09-12 11:21:42 -07001036 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001037 vp << app::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath());
tsepez4cf55152016-11-02 14:37:54 -07001038 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001039}
1040
Tom Sepezb1670b52017-02-16 17:01:00 -08001041bool Document::pageWindowRect(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001042 CJS_PropValue& vp,
1043 CFX_WideString& sError) {
1044 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001045}
1046
Tom Sepezb1670b52017-02-16 17:01:00 -08001047bool Document::layout(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001048 CJS_PropValue& vp,
1049 CFX_WideString& sError) {
1050 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051}
1052
Tom Sepezb1670b52017-02-16 17:01:00 -08001053bool Document::addLink(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001054 const std::vector<CJS_Value>& params,
1055 CJS_Value& vRet,
1056 CFX_WideString& sError) {
1057 return true;
1058}
1059
Tom Sepezb1670b52017-02-16 17:01:00 -08001060bool Document::closeDoc(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001061 const std::vector<CJS_Value>& params,
1062 CJS_Value& vRet,
1063 CFX_WideString& sError) {
1064 return true;
1065}
1066
Tom Sepezb1670b52017-02-16 17:01:00 -08001067bool Document::getPageBox(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001068 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069 CJS_Value& vRet,
1070 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001071 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001072}
1073
Tom Sepezb1670b52017-02-16 17:01:00 -08001074bool Document::getAnnot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001075 const std::vector<CJS_Value>& params,
1076 CJS_Value& vRet,
1077 CFX_WideString& sError) {
tonikitoo618cb1f2016-08-18 20:10:17 -07001078 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001079 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001080 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001081 }
dsinclair82e17672016-10-11 12:38:01 -07001082 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001083 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001084 return false;
tsepez55be02e2016-09-12 11:21:42 -07001085 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001086 int nPageNo = params[0].ToInt(pRuntime);
1087 CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -07001088 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(nPageNo);
tonikitoo618cb1f2016-08-18 20:10:17 -07001089 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001090 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001091
tsepezd805eec2017-01-11 14:03:54 -08001092 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
tonikitoo618cb1f2016-08-18 20:10:17 -07001093 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
tsepezd805eec2017-01-11 14:03:54 -08001094 for (const auto& pSDKAnnotCur : annotIteration) {
1095 CPDFSDK_BAAnnot* pBAAnnot =
1096 static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get());
tonikitoo618cb1f2016-08-18 20:10:17 -07001097 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1098 pSDKBAAnnot = pBAAnnot;
1099 break;
1100 }
1101 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001102 if (!pSDKBAAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001103 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001104
1105 v8::Local<v8::Object> pObj =
1106 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1107 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001108 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001109
1110 CJS_Annot* pJS_Annot =
1111 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
tonikitoo618cb1f2016-08-18 20:10:17 -07001112 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
tonikitoo618cb1f2016-08-18 20:10:17 -07001113 pAnnot->SetSDKAnnot(pSDKBAAnnot);
tonikitoo618cb1f2016-08-18 20:10:17 -07001114 vRet = CJS_Value(pRuntime, pJS_Annot);
tsepez4cf55152016-11-02 14:37:54 -07001115 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001116}
1117
Tom Sepezb1670b52017-02-16 17:01:00 -08001118bool Document::getAnnots(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001119 const std::vector<CJS_Value>& params,
1120 CJS_Value& vRet,
1121 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001122 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001123 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001124 return false;
tsepez55be02e2016-09-12 11:21:42 -07001125 }
tonikitoo0ee35902016-08-26 14:41:29 -07001126 // TODO(tonikitoo): Add support supported parameters as per
1127 // the PDF spec.
1128
dsinclair7cbe68e2016-10-12 11:56:23 -07001129 int nPageNo = m_pFormFillEnv->GetPageCount();
tonikitoo0ee35902016-08-26 14:41:29 -07001130 CJS_Array annots;
1131
1132 for (int i = 0; i < nPageNo; ++i) {
dsinclair7cbe68e2016-10-12 11:56:23 -07001133 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(i);
tonikitoo0ee35902016-08-26 14:41:29 -07001134 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001135 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001136
tsepezd805eec2017-01-11 14:03:54 -08001137 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
1138 for (const auto& pSDKAnnotCur : annotIteration) {
1139 if (!pSDKAnnotCur) {
1140 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001141 return false;
tsepezd805eec2017-01-11 14:03:54 -08001142 }
tonikitoo0ee35902016-08-26 14:41:29 -07001143 v8::Local<v8::Object> pObj =
1144 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1145 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001146 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001147
1148 CJS_Annot* pJS_Annot =
1149 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
tonikitoo0ee35902016-08-26 14:41:29 -07001150 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
tsepezd805eec2017-01-11 14:03:54 -08001151 pAnnot->SetSDKAnnot(static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get()));
tonikitoo0ee35902016-08-26 14:41:29 -07001152 annots.SetElement(pRuntime, i, CJS_Value(pRuntime, pJS_Annot));
1153 }
1154 }
tonikitoo0ee35902016-08-26 14:41:29 -07001155 vRet = CJS_Value(pRuntime, annots);
tsepez4cf55152016-11-02 14:37:54 -07001156 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157}
1158
Tom Sepezb1670b52017-02-16 17:01:00 -08001159bool Document::getAnnot3D(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001160 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001161 CJS_Value& vRet,
1162 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001163 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001164 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001165}
1166
Tom Sepezb1670b52017-02-16 17:01:00 -08001167bool Document::getAnnots3D(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001168 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001169 CJS_Value& vRet,
1170 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001171 return true;
1172}
1173
Tom Sepezb1670b52017-02-16 17:01:00 -08001174bool Document::getOCGs(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001175 const std::vector<CJS_Value>& params,
1176 CJS_Value& vRet,
1177 CFX_WideString& sError) {
1178 return true;
1179}
1180
Tom Sepezb1670b52017-02-16 17:01:00 -08001181bool Document::getLinks(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001182 const std::vector<CJS_Value>& params,
1183 CJS_Value& vRet,
1184 CFX_WideString& sError) {
1185 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001186}
1187
1188bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1189 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1190 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1191}
1192
Tom Sepezb1670b52017-02-16 17:01:00 -08001193bool Document::addIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001194 const std::vector<CJS_Value>& params,
1195 CJS_Value& vRet,
1196 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001197 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001198 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001199 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001200 }
tsepezf3dc8c62016-08-10 06:29:29 -07001201
tsepezb4694242016-08-15 16:44:55 -07001202 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Tom Sepez39bfe122015-09-17 15:25:23 -07001203 if (params[1].GetType() != CJS_Value::VT_object) {
tsepezcd5dc852016-09-08 11:23:24 -07001204 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001205 return false;
Tom Sepezaecd9d02015-08-31 15:04:12 -07001206 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001207
tsepezb4694242016-08-15 16:44:55 -07001208 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pRuntime);
Tom Sepezce84f712017-05-30 17:17:33 -07001209 if (CFXJS_Engine::GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
tsepezcd5dc852016-09-08 11:23:24 -07001210 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001211 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001212 }
1213
Lei Zhang313c4252017-02-27 16:37:36 -08001214 if (!params[1].ToCJSObject(pRuntime)->GetEmbedObject()) {
tsepezcd5dc852016-09-08 11:23:24 -07001215 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001216 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001217 }
1218
Lei Zhang313c4252017-02-27 16:37:36 -08001219 m_IconNames.push_back(swIconName);
tsepez4cf55152016-11-02 14:37:54 -07001220 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001221}
1222
Tom Sepezb1670b52017-02-16 17:01:00 -08001223bool Document::icons(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001224 CJS_PropValue& vp,
1225 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001226 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001227 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001228 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229 }
Lei Zhang313c4252017-02-27 16:37:36 -08001230 if (m_IconNames.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001231 vp.GetJSValue()->SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001232 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001233 }
1234
tsepeze5aff742016-08-08 09:49:42 -07001235 CJS_Array Icons;
Tom Sepez6ba32d92016-01-05 16:26:32 -08001236 int i = 0;
Lei Zhang313c4252017-02-27 16:37:36 -08001237 for (const auto& name : m_IconNames) {
tsepezb4694242016-08-15 16:44:55 -07001238 v8::Local<v8::Object> pObj =
1239 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001240 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001241 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001242
tsepezb4694242016-08-15 16:44:55 -07001243 CJS_Icon* pJS_Icon =
1244 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
thestigdadea5f2016-10-19 13:29:48 -07001245 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
Lei Zhang313c4252017-02-27 16:37:36 -08001246 pIcon->SetIconName(name);
tsepezb4694242016-08-15 16:44:55 -07001247 Icons.SetElement(pRuntime, i++, CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001248 }
1249
1250 vp << Icons;
tsepez4cf55152016-11-02 14:37:54 -07001251 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001252}
1253
Tom Sepezb1670b52017-02-16 17:01:00 -08001254bool Document::getIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001255 const std::vector<CJS_Value>& params,
1256 CJS_Value& vRet,
1257 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001259 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001260 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001261 }
1262
Lei Zhang313c4252017-02-27 16:37:36 -08001263 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
1264 auto it = std::find(m_IconNames.begin(), m_IconNames.end(), swIconName);
1265 if (it == m_IconNames.end())
tsepez4cf55152016-11-02 14:37:54 -07001266 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267
Lei Zhang313c4252017-02-27 16:37:36 -08001268 v8::Local<v8::Object> pObj =
1269 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
1270 if (pObj.IsEmpty())
1271 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001272
Lei Zhang313c4252017-02-27 16:37:36 -08001273 CJS_Icon* pJS_Icon = static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
1274 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
1275 pIcon->SetIconName(*it);
1276 vRet = CJS_Value(pRuntime, pJS_Icon);
1277 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001278}
1279
Tom Sepezb1670b52017-02-16 17:01:00 -08001280bool Document::removeIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001281 const std::vector<CJS_Value>& params,
1282 CJS_Value& vRet,
1283 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001284 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001285 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001286}
1287
Tom Sepezb1670b52017-02-16 17:01:00 -08001288bool Document::createDataObject(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001289 const std::vector<CJS_Value>& params,
1290 CJS_Value& vRet,
1291 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001292 // Unsafe, not implemented.
tsepez4cf55152016-11-02 14:37:54 -07001293 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001294}
1295
Tom Sepezb1670b52017-02-16 17:01:00 -08001296bool Document::media(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001297 CJS_PropValue& vp,
1298 CFX_WideString& sError) {
1299 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001300}
1301
Tom Sepezb1670b52017-02-16 17:01:00 -08001302bool Document::calculateNow(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001303 const std::vector<CJS_Value>& params,
1304 CJS_Value& vRet,
1305 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001306 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001307 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001308 return false;
tsepez55be02e2016-09-12 11:21:42 -07001309 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001310 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
1311 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
1312 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -07001313 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001314 return false;
tsepez55be02e2016-09-12 11:21:42 -07001315 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001316 m_pFormFillEnv->GetInterForm()->OnCalculate();
tsepez4cf55152016-11-02 14:37:54 -07001317 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001318}
1319
Tom Sepezb1670b52017-02-16 17:01:00 -08001320bool Document::Collab(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001321 CJS_PropValue& vp,
1322 CFX_WideString& sError) {
1323 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001324}
1325
Tom Sepezb1670b52017-02-16 17:01:00 -08001326bool Document::getPageNthWord(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001327 const std::vector<CJS_Value>& params,
1328 CJS_Value& vRet,
1329 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001330 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001331 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001332 return false;
tsepez55be02e2016-09-12 11:21:42 -07001333 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001334 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001335 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001336 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001337 }
tsepez55be02e2016-09-12 11:21:42 -07001338
1339 // TODO(tsepez): check maximum allowable params.
1340
tsepezb4694242016-08-15 16:44:55 -07001341 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
1342 int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
1343 bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001344
dsinclair7cbe68e2016-10-12 11:56:23 -07001345 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001346 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001347 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001348
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001349 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001350 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001351 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001352 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001353
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001354 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1355 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001356 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001357
thestig5cc24652016-04-26 11:46:02 -07001358 CPDF_Page page(pDocument, pPageDict, true);
1359 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001360
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001361 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001362 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001363 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001364 if (pPageObj->IsText()) {
1365 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001366 int nObjWords = CountWords(pTextObj);
1367 if (nWords + nObjWords >= nWordNo) {
1368 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1369 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001370 }
Tom Sepez2398d892016-02-17 16:46:26 -08001371 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001372 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001373 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001374
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001375 if (bStrip) {
1376 swRet.TrimLeft();
1377 swRet.TrimRight();
1378 }
1379
tsepezf3dc8c62016-08-10 06:29:29 -07001380 vRet = CJS_Value(pRuntime, swRet.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001381 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001382}
1383
Tom Sepezb1670b52017-02-16 17:01:00 -08001384bool Document::getPageNthWordQuads(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001385 const std::vector<CJS_Value>& params,
1386 CJS_Value& vRet,
1387 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001388 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001389 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001390 return false;
tsepez55be02e2016-09-12 11:21:42 -07001391 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001392 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -07001393 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001394 return false;
tsepez55be02e2016-09-12 11:21:42 -07001395 }
tsepez4cf55152016-11-02 14:37:54 -07001396 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001397}
1398
Tom Sepezb1670b52017-02-16 17:01:00 -08001399bool Document::getPageNumWords(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001400 const std::vector<CJS_Value>& params,
1401 CJS_Value& vRet,
1402 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001403 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001404 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001405 return false;
tsepez55be02e2016-09-12 11:21:42 -07001406 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001407 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001408 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001409 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001410 }
tsepezb4694242016-08-15 16:44:55 -07001411 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
dsinclair7cbe68e2016-10-12 11:56:23 -07001412 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001413 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001414 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001415 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001416 }
1417
1418 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1419 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001420 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001421
thestig5cc24652016-04-26 11:46:02 -07001422 CPDF_Page page(pDocument, pPageDict, true);
1423 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001424
1425 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001426 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001427 if (pPageObj->IsText())
1428 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001429 }
1430
tsepezf3dc8c62016-08-10 06:29:29 -07001431 vRet = CJS_Value(pRuntime, nWords);
tsepez4cf55152016-11-02 14:37:54 -07001432 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001433}
1434
Tom Sepezb1670b52017-02-16 17:01:00 -08001435bool Document::getPrintParams(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001436 const std::vector<CJS_Value>& params,
1437 CJS_Value& vRet,
1438 CFX_WideString& sError) {
tsepezb4694242016-08-15 16:44:55 -07001439 v8::Local<v8::Object> pRetObj =
1440 pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::g_nObjDefnID);
Tom Sepezc5a14722017-02-24 15:31:12 -08001441 if (pRetObj.IsEmpty())
1442 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001443
1444 // Not implemented yet.
1445
tsepezf3dc8c62016-08-10 06:29:29 -07001446 vRet = CJS_Value(pRuntime, pRetObj);
tsepez4cf55152016-11-02 14:37:54 -07001447 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001448}
1449
1450#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1451
1452int Document::CountWords(CPDF_TextObject* pTextObj) {
1453 if (!pTextObj)
1454 return 0;
1455
1456 int nWords = 0;
1457
1458 CPDF_Font* pFont = pTextObj->GetFont();
1459 if (!pFont)
1460 return 0;
1461
tsepez4cf55152016-11-02 14:37:54 -07001462 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001463
1464 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001465 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Dan Sinclair05df0752017-03-14 14:43:42 -04001466 float kerning;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001467
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001468 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001469 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1470
Tom Sepez62a70f92016-03-21 15:00:20 -07001471 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001472 if (swUnicode.GetLength() > 0)
1473 unicode = swUnicode[0];
1474
1475 if (ISLATINWORD(unicode) && bIsLatin)
1476 continue;
1477
1478 bIsLatin = ISLATINWORD(unicode);
1479 if (unicode != 0x20)
1480 nWords++;
1481 }
1482
1483 return nWords;
1484}
1485
1486CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1487 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001488 CFX_WideString swRet;
1489
1490 CPDF_Font* pFont = pTextObj->GetFont();
1491 if (!pFont)
1492 return L"";
1493
1494 int nWords = 0;
tsepez4cf55152016-11-02 14:37:54 -07001495 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001496
1497 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001498 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Dan Sinclair05df0752017-03-14 14:43:42 -04001499 float kerning;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001500
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001501 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001502 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1503
Tom Sepez62a70f92016-03-21 15:00:20 -07001504 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001505 if (swUnicode.GetLength() > 0)
1506 unicode = swUnicode[0];
1507
1508 if (ISLATINWORD(unicode) && bIsLatin) {
1509 } else {
1510 bIsLatin = ISLATINWORD(unicode);
1511 if (unicode != 0x20)
1512 nWords++;
1513 }
1514
1515 if (nWords - 1 == nWordIndex)
1516 swRet += unicode;
1517 }
1518
1519 return swRet;
1520}
1521
Tom Sepezb1670b52017-02-16 17:01:00 -08001522bool Document::zoom(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001523 CJS_PropValue& vp,
1524 CFX_WideString& sError) {
1525 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001526}
1527
1528/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001529(none, NoVary)
1530(fitP, FitPage)
1531(fitW, FitWidth)
1532(fitH, FitHeight)
1533(fitV, FitVisibleWidth)
1534(pref, Preferred)
1535(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001536*/
1537
Tom Sepezb1670b52017-02-16 17:01:00 -08001538bool Document::zoomType(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001539 CJS_PropValue& vp,
1540 CFX_WideString& sError) {
1541 return true;
1542}
1543
Tom Sepezb1670b52017-02-16 17:01:00 -08001544bool Document::deletePages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001545 const std::vector<CJS_Value>& params,
1546 CJS_Value& vRet,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001547 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001548 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001549 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001550}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001551
Tom Sepezb1670b52017-02-16 17:01:00 -08001552bool Document::extractPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001553 const std::vector<CJS_Value>& params,
1554 CJS_Value& vRet,
1555 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001556 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001557 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001558}
1559
Tom Sepezb1670b52017-02-16 17:01:00 -08001560bool Document::insertPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001561 const std::vector<CJS_Value>& params,
1562 CJS_Value& vRet,
1563 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001564 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001565 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001566}
1567
Tom Sepezb1670b52017-02-16 17:01:00 -08001568bool Document::replacePages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001569 const std::vector<CJS_Value>& params,
1570 CJS_Value& vRet,
1571 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001572 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001573 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001574}
1575
Tom Sepezb1670b52017-02-16 17:01:00 -08001576bool Document::getURL(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001577 const std::vector<CJS_Value>& params,
1578 CJS_Value& vRet,
1579 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001580 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001581 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001582}
1583
Tom Sepezb1670b52017-02-16 17:01:00 -08001584bool Document::gotoNamedDest(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001585 const std::vector<CJS_Value>& params,
1586 CJS_Value& vRet,
1587 CFX_WideString& sError) {
tonikitoo1c836752016-08-08 16:14:05 -07001588 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001589 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001590 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001591 }
dsinclair82e17672016-10-11 12:38:01 -07001592 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001593 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001594 return false;
tsepez55be02e2016-09-12 11:21:42 -07001595 }
tsepezb4694242016-08-15 16:44:55 -07001596 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -07001597 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
tonikitoo1c836752016-08-08 16:14:05 -07001598 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001599 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001600
tonikitoo1c836752016-08-08 16:14:05 -07001601 CPDF_NameTree nameTree(pDocument, "Dests");
Jane Liu67ccef72017-07-19 13:10:50 -04001602 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, wideName);
tonikitoo1c836752016-08-08 16:14:05 -07001603 if (!destArray)
tsepez4cf55152016-11-02 14:37:54 -07001604 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001605
1606 CPDF_Dest dest(destArray);
1607 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
Tom Sepezd7188f72017-05-02 15:10:58 -07001608 std::vector<float> scrollPositionArray;
tonikitoo1c836752016-08-08 16:14:05 -07001609 if (arrayObject) {
tonikitoo1c836752016-08-08 16:14:05 -07001610 for (size_t i = 2; i < arrayObject->GetCount(); i++)
Tom Sepezd7188f72017-05-02 15:10:58 -07001611 scrollPositionArray.push_back(arrayObject->GetFloatAt(i));
tonikitoo1c836752016-08-08 16:14:05 -07001612 }
tsepezb4694242016-08-15 16:44:55 -07001613 pRuntime->BeginBlock();
dsinclair82e17672016-10-11 12:38:01 -07001614 m_pFormFillEnv->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
Tom Sepezd7188f72017-05-02 15:10:58 -07001615 scrollPositionArray.data(),
1616 scrollPositionArray.size());
tsepezb4694242016-08-15 16:44:55 -07001617 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -07001618 return true;
tonikitoo1c836752016-08-08 16:14:05 -07001619}
1620
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001621void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001622 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001623}
1624
1625void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1626 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001627 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1628 auto iter = m_DelayData.begin();
1629 while (iter != m_DelayData.end()) {
1630 auto old = iter++;
1631 if ((*old)->sFieldName == sFieldName &&
1632 (*old)->nControlIndex == nControlIndex) {
1633 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1634 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001635 }
1636 }
1637
dsinclair82e17672016-10-11 12:38:01 -07001638 for (const auto& pData : DelayDataForFieldAndControlIndex)
1639 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001640}
1641
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001642CJS_Document* Document::GetCJSDoc() const {
Tom Sepez797ca5c2017-05-25 12:03:18 -07001643 return static_cast<CJS_Document*>(m_pJSObject.Get());
JUN FANG33f6f0d2015-04-06 12:39:51 -07001644}