blob: d8934a44f651eb51d01777bb1390626a71833b9b [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>
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -040010#include <sstream>
thestig7c292e02016-09-28 14:14:26 -070011#include <utility>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050012#include <vector>
13
dsinclairbc5e6d22016-10-04 11:08:49 -070014#include "core/fpdfapi/font/cpdf_font.h"
dsinclair41872fa2016-10-04 11:29:35 -070015#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070016#include "core/fpdfapi/parser/cpdf_array.h"
17#include "core/fpdfapi/parser/cpdf_document.h"
tsepez0e606b52016-11-18 16:22:41 -080018#include "core/fpdfapi/parser/cpdf_string.h"
dsinclair488b7ad2016-10-04 11:55:50 -070019#include "core/fpdfapi/parser/fpdf_parser_decode.h"
dsinclair1727aee2016-09-29 13:12:56 -070020#include "core/fpdfdoc/cpdf_interform.h"
21#include "core/fpdfdoc/cpdf_nametree.h"
tsepezd805eec2017-01-11 14:03:54 -080022#include "fpdfsdk/cpdfsdk_annotiteration.h"
dsinclair735606d2016-10-05 15:47:02 -070023#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070024#include "fpdfsdk/cpdfsdk_interform.h"
25#include "fpdfsdk/cpdfsdk_pageview.h"
26#include "fpdfsdk/cpdfsdk_widget.h"
tonikitoo618cb1f2016-08-18 20:10:17 -070027#include "fpdfsdk/javascript/Annot.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040028#include "fpdfsdk/javascript/Field.h"
29#include "fpdfsdk/javascript/Icon.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040030#include "fpdfsdk/javascript/JS_Define.h"
31#include "fpdfsdk/javascript/JS_EventHandler.h"
32#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040033#include "fpdfsdk/javascript/JS_Value.h"
34#include "fpdfsdk/javascript/app.h"
Tom Sepezd6ae2af2017-02-16 11:49:55 -080035#include "fpdfsdk/javascript/cjs_event_context.h"
dsinclair64376be2016-03-31 20:03:24 -070036#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040037#include "fpdfsdk/javascript/resource.h"
Lei Zhang8241df72015-11-06 14:38:48 -080038#include "third_party/base/numerics/safe_math.h"
thestigdadea5f2016-10-19 13:29:48 -070039#include "third_party/base/ptr_util.h"
Chris Palmer9108ad22014-06-26 16:01:46 -070040
Tom Sepez04557b82017-02-16 09:43:10 -080041JSConstSpec CJS_PrintParamsObj::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042
Tom Sepez04557b82017-02-16 09:43:10 -080043JSPropertySpec CJS_PrintParamsObj::PropertySpecs[] = {{0, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Tom Sepez04557b82017-02-16 09:43:10 -080045JSMethodSpec CJS_PrintParamsObj::MethodSpecs[] = {{0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046
47IMPLEMENT_JS_CLASS(CJS_PrintParamsObj, PrintParamsObj)
48
49PrintParamsObj::PrintParamsObj(CJS_Object* pJSObject)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 : CJS_EmbedObj(pJSObject) {
tsepez4cf55152016-11-02 14:37:54 -070051 bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 nStart = 0;
53 nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -070054 bSilent = false;
55 bShrinkToFit = false;
56 bPrintAsImage = false;
57 bReverse = false;
58 bAnnotations = true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059}
60
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061#define MINWIDTH 5.0f
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062#define MINHEIGHT 5.0f
63
Tom Sepez04557b82017-02-16 09:43:10 -080064JSConstSpec CJS_Document::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065
Tom Sepez04557b82017-02-16 09:43:10 -080066JSPropertySpec CJS_Document::PropertySpecs[] = {
Tom Sepez4d5b8c52017-02-21 15:17:07 -080067 {"ADBE", get_ADBE_static, set_ADBE_static},
68 {"author", get_author_static, set_author_static},
69 {"baseURL", get_baseURL_static, set_baseURL_static},
70 {"bookmarkRoot", get_bookmarkRoot_static, set_bookmarkRoot_static},
71 {"calculate", get_calculate_static, set_calculate_static},
72 {"Collab", get_Collab_static, set_Collab_static},
73 {"creationDate", get_creationDate_static, set_creationDate_static},
74 {"creator", get_creator_static, set_creator_static},
75 {"delay", get_delay_static, set_delay_static},
76 {"dirty", get_dirty_static, set_dirty_static},
77 {"documentFileName", get_documentFileName_static,
Tom Sepez04557b82017-02-16 09:43:10 -080078 set_documentFileName_static},
Tom Sepez4d5b8c52017-02-21 15:17:07 -080079 {"external", get_external_static, set_external_static},
80 {"filesize", get_filesize_static, set_filesize_static},
81 {"icons", get_icons_static, set_icons_static},
82 {"info", get_info_static, set_info_static},
83 {"keywords", get_keywords_static, set_keywords_static},
84 {"layout", get_layout_static, set_layout_static},
85 {"media", get_media_static, set_media_static},
86 {"modDate", get_modDate_static, set_modDate_static},
87 {"mouseX", get_mouseX_static, set_mouseX_static},
88 {"mouseY", get_mouseY_static, set_mouseY_static},
89 {"numFields", get_numFields_static, set_numFields_static},
90 {"numPages", get_numPages_static, set_numPages_static},
91 {"pageNum", get_pageNum_static, set_pageNum_static},
92 {"pageWindowRect", get_pageWindowRect_static, set_pageWindowRect_static},
93 {"path", get_path_static, set_path_static},
94 {"producer", get_producer_static, set_producer_static},
95 {"subject", get_subject_static, set_subject_static},
96 {"title", get_title_static, set_title_static},
97 {"URL", get_URL_static, set_URL_static},
98 {"zoom", get_zoom_static, set_zoom_static},
99 {"zoomType", get_zoomType_static, set_zoomType_static},
Tom Sepez04557b82017-02-16 09:43:10 -0800100 {0, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101
Tom Sepez04557b82017-02-16 09:43:10 -0800102JSMethodSpec CJS_Document::MethodSpecs[] = {
Tom Sepez9b99b632017-02-21 15:05:57 -0800103 {"addAnnot", addAnnot_static},
104 {"addField", addField_static},
105 {"addLink", addLink_static},
106 {"addIcon", addIcon_static},
107 {"calculateNow", calculateNow_static},
108 {"closeDoc", closeDoc_static},
109 {"createDataObject", createDataObject_static},
110 {"deletePages", deletePages_static},
111 {"exportAsText", exportAsText_static},
112 {"exportAsFDF", exportAsFDF_static},
113 {"exportAsXFDF", exportAsXFDF_static},
114 {"extractPages", extractPages_static},
115 {"getAnnot", getAnnot_static},
116 {"getAnnots", getAnnots_static},
117 {"getAnnot3D", getAnnot3D_static},
118 {"getAnnots3D", getAnnots3D_static},
119 {"getField", getField_static},
120 {"getIcon", getIcon_static},
121 {"getLinks", getLinks_static},
122 {"getNthFieldName", getNthFieldName_static},
123 {"getOCGs", getOCGs_static},
124 {"getPageBox", getPageBox_static},
125 {"getPageNthWord", getPageNthWord_static},
126 {"getPageNthWordQuads", getPageNthWordQuads_static},
127 {"getPageNumWords", getPageNumWords_static},
128 {"getPrintParams", getPrintParams_static},
129 {"getURL", getURL_static},
130 {"gotoNamedDest", gotoNamedDest_static},
131 {"importAnFDF", importAnFDF_static},
132 {"importAnXFDF", importAnXFDF_static},
133 {"importTextData", importTextData_static},
134 {"insertPages", insertPages_static},
135 {"mailForm", mailForm_static},
136 {"print", print_static},
137 {"removeField", removeField_static},
138 {"replacePages", replacePages_static},
139 {"resetForm", resetForm_static},
140 {"removeIcon", removeIcon_static},
141 {"saveAs", saveAs_static},
142 {"submitForm", submitForm_static},
143 {"syncAnnotScan", syncAnnotScan_static},
144 {"mailDoc", mailDoc_static},
Tom Sepez04557b82017-02-16 09:43:10 -0800145 {0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146
147IMPLEMENT_JS_CLASS(CJS_Document, Document)
148
Tom Sepez33420902015-10-13 15:00:10 -0700149void CJS_Document::InitInstance(IJS_Runtime* pIRuntime) {
150 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime);
151 Document* pDoc = static_cast<Document*>(GetEmbedObject());
dsinclair82e17672016-10-11 12:38:01 -0700152 pDoc->SetFormFillEnv(pRuntime->GetFormFillEnv());
Tom Sepez33420902015-10-13 15:00:10 -0700153}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155Document::Document(CJS_Object* pJSObject)
156 : CJS_EmbedObj(pJSObject),
dsinclair82e17672016-10-11 12:38:01 -0700157 m_pFormFillEnv(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 m_cwBaseURL(L""),
tsepez4cf55152016-11-02 14:37:54 -0700159 m_bDelay(false) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161Document::~Document() {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162}
163
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164// the total number of fileds in document.
Tom Sepezb1670b52017-02-16 17:01:00 -0800165bool Document::numFields(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700166 CJS_PropValue& vp,
167 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700169 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700170 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 }
dsinclair82e17672016-10-11 12:38:01 -0700172 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700173 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700174 return false;
tsepez55be02e2016-09-12 11:21:42 -0700175 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700176 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700178 vp << static_cast<int>(pPDFForm->CountFields(CFX_WideString()));
tsepez4cf55152016-11-02 14:37:54 -0700179 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180}
181
Tom Sepezb1670b52017-02-16 17:01:00 -0800182bool Document::dirty(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700183 CJS_PropValue& vp,
184 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700185 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700186 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700187 return false;
tsepez55be02e2016-09-12 11:21:42 -0700188 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189 if (vp.IsGetting()) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700190 vp << !!m_pFormFillEnv->GetChangeMark();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800191 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800193 bool bChanged = false;
194 vp >> bChanged;
195 if (bChanged)
196 m_pFormFillEnv->SetChangeMark();
197 else
198 m_pFormFillEnv->ClearChangeMark();
199
tsepez4cf55152016-11-02 14:37:54 -0700200 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201}
202
Tom Sepezb1670b52017-02-16 17:01:00 -0800203bool Document::ADBE(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700204 CJS_PropValue& vp,
205 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700206 if (vp.IsGetting())
Tom Sepezb1670b52017-02-16 17:01:00 -0800207 vp.GetJSValue()->SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208
tsepez4cf55152016-11-02 14:37:54 -0700209 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210}
211
Tom Sepezb1670b52017-02-16 17:01:00 -0800212bool Document::pageNum(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700213 CJS_PropValue& vp,
214 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700215 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700216 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700217 return false;
tsepez55be02e2016-09-12 11:21:42 -0700218 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 if (vp.IsGetting()) {
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800220 if (CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetCurrentView())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 vp << pPageView->GetPageIndex();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800222 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800224 int iPageCount = m_pFormFillEnv->GetPageCount();
225 int iPageNum = 0;
226 vp >> iPageNum;
227 if (iPageNum >= 0 && iPageNum < iPageCount)
228 m_pFormFillEnv->JS_docgotoPage(iPageNum);
229 else if (iPageNum >= iPageCount)
230 m_pFormFillEnv->JS_docgotoPage(iPageCount - 1);
231 else if (iPageNum < 0)
232 m_pFormFillEnv->JS_docgotoPage(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233
tsepez4cf55152016-11-02 14:37:54 -0700234 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235}
236
Tom Sepezb1670b52017-02-16 17:01:00 -0800237bool Document::addAnnot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700238 const std::vector<CJS_Value>& params,
239 CJS_Value& vRet,
240 CFX_WideString& sError) {
241 // Not supported.
242 return true;
243}
244
Tom Sepezb1670b52017-02-16 17:01:00 -0800245bool Document::addField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700246 const std::vector<CJS_Value>& params,
247 CJS_Value& vRet,
248 CFX_WideString& sError) {
249 // Not supported.
250 return true;
251}
252
Tom Sepezb1670b52017-02-16 17:01:00 -0800253bool Document::exportAsText(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700254 const std::vector<CJS_Value>& params,
255 CJS_Value& vRet,
256 CFX_WideString& sError) {
257 // Unsafe, not supported.
258 return true;
259}
260
Tom Sepezb1670b52017-02-16 17:01:00 -0800261bool Document::exportAsFDF(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800262 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 CJS_Value& vRet,
264 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -0800265 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700266 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700267}
268
Tom Sepezb1670b52017-02-16 17:01:00 -0800269bool Document::exportAsXFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700270 const std::vector<CJS_Value>& params,
271 CJS_Value& vRet,
272 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700274 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700276
Tom Sepezb1670b52017-02-16 17:01:00 -0800277bool Document::getField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700278 const std::vector<CJS_Value>& params,
279 CJS_Value& vRet,
280 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700282 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700283 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 }
dsinclair82e17672016-10-11 12:38:01 -0700285 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700286 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700287 return false;
tsepez55be02e2016-09-12 11:21:42 -0700288 }
tsepezb4694242016-08-15 16:44:55 -0700289 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700290 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
292 if (pPDFForm->CountFields(wideName) <= 0) {
tsepezf3dc8c62016-08-10 06:29:29 -0700293 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700294 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 }
296
tsepezf3dc8c62016-08-10 06:29:29 -0700297 v8::Local<v8::Object> pFieldObj =
tsepezb4694242016-08-15 16:44:55 -0700298 pRuntime->NewFxDynamicObj(CJS_Field::g_nObjDefnID);
Tom Sepezc5a14722017-02-24 15:31:12 -0800299 if (pFieldObj.IsEmpty())
300 return false;
301
tsepezf3dc8c62016-08-10 06:29:29 -0700302 CJS_Field* pJSField =
tsepezb4694242016-08-15 16:44:55 -0700303 static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
tsepezf3dc8c62016-08-10 06:29:29 -0700304 Field* pField = static_cast<Field*>(pJSField->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 pField->AttachField(this, wideName);
tsepezf3dc8c62016-08-10 06:29:29 -0700306 vRet = CJS_Value(pRuntime, pJSField);
tsepez4cf55152016-11-02 14:37:54 -0700307 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308}
309
310// Gets the name of the nth field in the document
Tom Sepezb1670b52017-02-16 17:01:00 -0800311bool Document::getNthFieldName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700312 const std::vector<CJS_Value>& params,
313 CJS_Value& vRet,
314 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700315 if (params.size() != 1) {
316 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700317 return false;
tsepezcd5dc852016-09-08 11:23:24 -0700318 }
dsinclair82e17672016-10-11 12:38:01 -0700319 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700320 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700321 return false;
tsepez55be02e2016-09-12 11:21:42 -0700322 }
tsepezb4694242016-08-15 16:44:55 -0700323 int nIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324 if (nIndex < 0) {
tsepezcd5dc852016-09-08 11:23:24 -0700325 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700326 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700328 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700330 CPDF_FormField* pField = pPDFForm->GetField(nIndex, CFX_WideString());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331 if (!pField)
tsepez4cf55152016-11-02 14:37:54 -0700332 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333
tsepezf3dc8c62016-08-10 06:29:29 -0700334 vRet = CJS_Value(pRuntime, pField->GetFullName().c_str());
tsepez4cf55152016-11-02 14:37:54 -0700335 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336}
337
Tom Sepezb1670b52017-02-16 17:01:00 -0800338bool Document::importAnFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700339 const std::vector<CJS_Value>& params,
340 CJS_Value& vRet,
341 CFX_WideString& sError) {
342 // Unsafe, not supported.
343 return true;
344}
345
Tom Sepezb1670b52017-02-16 17:01:00 -0800346bool Document::importAnXFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700347 const std::vector<CJS_Value>& params,
348 CJS_Value& vRet,
349 CFX_WideString& sError) {
350 // Unsafe, not supported.
351 return true;
352}
353
Tom Sepezb1670b52017-02-16 17:01:00 -0800354bool Document::importTextData(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800355 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356 CJS_Value& vRet,
357 CFX_WideString& sError) {
358 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700359 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360}
361
362// exports the form data and mails the resulting fdf file as an attachment to
363// all recipients.
364// comment: need reader supports
Tom Sepezb1670b52017-02-16 17:01:00 -0800365bool Document::mailForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700366 const std::vector<CJS_Value>& params,
367 CJS_Value& vRet,
368 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700369 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700370 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700371 return false;
tsepez55be02e2016-09-12 11:21:42 -0700372 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700373 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -0700374 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700375 return false;
tsepez55be02e2016-09-12 11:21:42 -0700376 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377 int iLength = params.size();
tsepez4cf55152016-11-02 14:37:54 -0700378 bool bUI = iLength > 0 ? params[0].ToBool(pRuntime) : true;
tsepezb4694242016-08-15 16:44:55 -0700379 CFX_WideString cTo = iLength > 1 ? params[1].ToCFXWideString(pRuntime) : L"";
380 CFX_WideString cCc = iLength > 2 ? params[2].ToCFXWideString(pRuntime) : L"";
381 CFX_WideString cBcc = iLength > 3 ? params[3].ToCFXWideString(pRuntime) : L"";
tsepezf3dc8c62016-08-10 06:29:29 -0700382 CFX_WideString cSubject =
tsepezb4694242016-08-15 16:44:55 -0700383 iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
384 CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
dsinclair7cbe68e2016-10-12 11:56:23 -0700385 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400386 CFX_ByteString sTextBuf = pInterForm->ExportFormToFDFTextBuf();
387 if (sTextBuf.GetLength() == 0)
tsepez4cf55152016-11-02 14:37:54 -0700388 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400390 FX_STRSIZE nBufSize = sTextBuf.GetLength();
391 char* pMutableBuf = FX_Alloc(char, nBufSize);
392 memcpy(pMutableBuf, sTextBuf.c_str(), nBufSize);
393
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 pRuntime->BeginBlock();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800395 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400396 pFormFillEnv->JS_docmailForm(pMutableBuf, nBufSize, bUI, cTo.c_str(),
397 cSubject.c_str(), cCc.c_str(), cBcc.c_str(),
398 cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 pRuntime->EndBlock();
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400400 FX_Free(pMutableBuf);
tsepez4cf55152016-11-02 14:37:54 -0700401 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402}
403
Tom Sepezb1670b52017-02-16 17:01:00 -0800404bool Document::print(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700405 const std::vector<CJS_Value>& params,
406 CJS_Value& vRet,
407 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700408 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700409 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700410 return false;
tsepez55be02e2016-09-12 11:21:42 -0700411 }
tsepez4cf55152016-11-02 14:37:54 -0700412 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413 int nStart = 0;
414 int nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -0700415 bool bSilent = false;
416 bool bShrinkToFit = false;
417 bool bPrintAsImage = false;
418 bool bReverse = false;
419 bool bAnnotations = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700420 int nlength = params.size();
421 if (nlength == 9) {
tsepez40faa792016-07-15 17:58:02 -0700422 if (params[8].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700423 v8::Local<v8::Object> pObj = params[8].ToV8Object(pRuntime);
424 if (CFXJS_Engine::GetObjDefnID(pObj) ==
425 CJS_PrintParamsObj::g_nObjDefnID) {
426 if (CJS_Object* pJSObj = params[8].ToCJSObject(pRuntime)) {
tsepez40faa792016-07-15 17:58:02 -0700427 if (PrintParamsObj* pprintparamsObj =
428 static_cast<PrintParamsObj*>(pJSObj->GetEmbedObject())) {
429 bUI = pprintparamsObj->bUI;
430 nStart = pprintparamsObj->nStart;
431 nEnd = pprintparamsObj->nEnd;
432 bSilent = pprintparamsObj->bSilent;
433 bShrinkToFit = pprintparamsObj->bShrinkToFit;
434 bPrintAsImage = pprintparamsObj->bPrintAsImage;
435 bReverse = pprintparamsObj->bReverse;
436 bAnnotations = pprintparamsObj->bAnnotations;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700438 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 }
440 }
441 } else {
442 if (nlength >= 1)
tsepezb4694242016-08-15 16:44:55 -0700443 bUI = params[0].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444 if (nlength >= 2)
tsepezb4694242016-08-15 16:44:55 -0700445 nStart = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 if (nlength >= 3)
tsepezb4694242016-08-15 16:44:55 -0700447 nEnd = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 if (nlength >= 4)
tsepezb4694242016-08-15 16:44:55 -0700449 bSilent = params[3].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450 if (nlength >= 5)
tsepezb4694242016-08-15 16:44:55 -0700451 bShrinkToFit = params[4].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 if (nlength >= 6)
tsepezb4694242016-08-15 16:44:55 -0700453 bPrintAsImage = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 if (nlength >= 7)
tsepezb4694242016-08-15 16:44:55 -0700455 bReverse = params[6].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 if (nlength >= 8)
tsepezb4694242016-08-15 16:44:55 -0700457 bAnnotations = params[7].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700458 }
459
dsinclair82e17672016-10-11 12:38:01 -0700460 if (m_pFormFillEnv) {
461 m_pFormFillEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit,
462 bPrintAsImage, bReverse, bAnnotations);
tsepez4cf55152016-11-02 14:37:54 -0700463 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700464 }
tsepez4cf55152016-11-02 14:37:54 -0700465 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466}
467
468// removes the specified field from the document.
469// comment:
dsinclair1897bdc2016-09-06 14:18:57 -0700470// note: if the filed name is not rational, adobe is dumb for it.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471
Tom Sepezb1670b52017-02-16 17:01:00 -0800472bool Document::removeField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700473 const std::vector<CJS_Value>& params,
474 CJS_Value& vRet,
475 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700476 if (params.size() != 1) {
477 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700478 return false;
tsepez55be02e2016-09-12 11:21:42 -0700479 }
dsinclair82e17672016-10-11 12:38:01 -0700480 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700481 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700482 return false;
tsepez55be02e2016-09-12 11:21:42 -0700483 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700484 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
485 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM))) {
tsepezcd5dc852016-09-08 11:23:24 -0700486 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700487 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488 }
tsepezb4694242016-08-15 16:44:55 -0700489 CFX_WideString sFieldName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700490 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
tsepez8fa82792017-01-11 09:32:33 -0800491 std::vector<CPDFSDK_Annot::ObservedPtr> widgets;
Lei Zhangd88a3642015-11-10 09:38:57 -0800492 pInterForm->GetWidgets(sFieldName, &widgets);
Lei Zhangd88a3642015-11-10 09:38:57 -0800493 if (widgets.empty())
tsepez4cf55152016-11-02 14:37:54 -0700494 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495
tsepez8fa82792017-01-11 09:32:33 -0800496 for (const auto& pAnnot : widgets) {
497 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot.Get());
498 if (!pWidget)
499 continue;
500
Tom Sepez281a9ea2016-02-26 14:24:28 -0800501 CFX_FloatRect rcAnnot = pWidget->GetRect();
Lei Zhangd88a3642015-11-10 09:38:57 -0800502 --rcAnnot.left;
503 --rcAnnot.bottom;
504 ++rcAnnot.right;
505 ++rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506
tsepezdf964df2016-04-21 12:09:41 -0700507 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800508 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Lei Zhangd88a3642015-11-10 09:38:57 -0800509 ASSERT(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510
dsinclair1897bdc2016-09-06 14:18:57 -0700511 // If there is currently no pageview associated with the page being used
512 // do not create one. We may be in the process of tearing down the document
513 // and creating a new pageview at this point will cause bad things.
dsinclair7cbe68e2016-10-12 11:56:23 -0700514 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, false);
dsinclair1897bdc2016-09-06 14:18:57 -0700515 if (pPageView) {
dsinclair8afe15a2016-10-05 12:00:34 -0700516#if PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700517 pPageView->DeleteAnnot(pWidget);
dsinclair8afe15a2016-10-05 12:00:34 -0700518#endif // PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700519 pPageView->UpdateRects(aRefresh);
520 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700522 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523
tsepez4cf55152016-11-02 14:37:54 -0700524 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525}
526
527// reset filed values within a document.
528// comment:
529// note: if the fields names r not rational, aodbe is dumb for it.
530
Tom Sepezb1670b52017-02-16 17:01:00 -0800531bool Document::resetForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700532 const std::vector<CJS_Value>& params,
533 CJS_Value& vRet,
534 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700535 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700536 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700537 return false;
tsepez55be02e2016-09-12 11:21:42 -0700538 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700539 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
540 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
541 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -0700542 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700543 return false;
tsepez55be02e2016-09-12 11:21:42 -0700544 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700545
dsinclair7cbe68e2016-10-12 11:56:23 -0700546 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700547 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
tsepeze5aff742016-08-08 09:49:42 -0700548 CJS_Array aName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700549
Lei Zhangdd734de2015-11-11 10:16:52 -0800550 if (params.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700551 pPDFForm->ResetForm(true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700552 m_pFormFillEnv->SetChangeMark();
tsepez4cf55152016-11-02 14:37:54 -0700553 return true;
Lei Zhangd88a3642015-11-10 09:38:57 -0800554 }
555
556 switch (params[0].GetType()) {
557 default:
tsepezb4694242016-08-15 16:44:55 -0700558 aName.Attach(params[0].ToV8Array(pRuntime));
Lei Zhangd88a3642015-11-10 09:38:57 -0800559 break;
560 case CJS_Value::VT_string:
tsepezb4694242016-08-15 16:44:55 -0700561 aName.SetElement(pRuntime, 0, params[0]);
Lei Zhangd88a3642015-11-10 09:38:57 -0800562 break;
563 }
564
565 std::vector<CPDF_FormField*> aFields;
tsepezb4694242016-08-15 16:44:55 -0700566 for (int i = 0, isz = aName.GetLength(pRuntime); i < isz; ++i) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800567 CJS_Value valElement(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700568 aName.GetElement(pRuntime, i, valElement);
569 CFX_WideString swVal = valElement.ToCFXWideString(pRuntime);
Lei Zhangd88a3642015-11-10 09:38:57 -0800570 for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j)
571 aFields.push_back(pPDFForm->GetField(j, swVal));
572 }
573
574 if (!aFields.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700575 pPDFForm->ResetForm(aFields, true, true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700576 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700577 }
578
tsepez4cf55152016-11-02 14:37:54 -0700579 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700580}
581
Tom Sepezb1670b52017-02-16 17:01:00 -0800582bool Document::saveAs(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700583 const std::vector<CJS_Value>& params,
584 CJS_Value& vRet,
585 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700587 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700588}
589
Tom Sepezb1670b52017-02-16 17:01:00 -0800590bool Document::syncAnnotScan(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800591 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700592 CJS_Value& vRet,
593 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700594 return true;
595}
596
Tom Sepezb1670b52017-02-16 17:01:00 -0800597bool Document::submitForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700598 const std::vector<CJS_Value>& params,
599 CJS_Value& vRet,
600 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601 int nSize = params.size();
602 if (nSize < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700603 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700604 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700605 }
dsinclair82e17672016-10-11 12:38:01 -0700606 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700607 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700608 return false;
tsepez55be02e2016-09-12 11:21:42 -0700609 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800610
tsepeze5aff742016-08-08 09:49:42 -0700611 CJS_Array aFields;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700612 CFX_WideString strURL;
tsepez4cf55152016-11-02 14:37:54 -0700613 bool bFDF = true;
614 bool bEmpty = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700615 CJS_Value v = params[0];
Tom Sepez39bfe122015-09-17 15:25:23 -0700616 if (v.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700617 strURL = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700618 if (nSize > 1)
tsepezb4694242016-08-15 16:44:55 -0700619 bFDF = params[1].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700620 if (nSize > 2)
tsepezb4694242016-08-15 16:44:55 -0700621 bEmpty = params[2].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700622 if (nSize > 3)
tsepezb4694242016-08-15 16:44:55 -0700623 aFields.Attach(params[3].ToV8Array(pRuntime));
Tom Sepez39bfe122015-09-17 15:25:23 -0700624 } else if (v.GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700625 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
626 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"cURL");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700627 if (!pValue.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700628 strURL = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700629
tsepezb4694242016-08-15 16:44:55 -0700630 pValue = pRuntime->GetObjectProperty(pObj, L"bFDF");
631 bFDF = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700632
tsepezb4694242016-08-15 16:44:55 -0700633 pValue = pRuntime->GetObjectProperty(pObj, L"bEmpty");
634 bEmpty = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700635
tsepezb4694242016-08-15 16:44:55 -0700636 pValue = pRuntime->GetObjectProperty(pObj, L"aFields");
637 aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700638 }
639
dsinclair7cbe68e2016-10-12 11:56:23 -0700640 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700641 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
tsepezb4694242016-08-15 16:44:55 -0700642 if (aFields.GetLength(pRuntime) == 0 && bEmpty) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800643 if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700644 pRuntime->BeginBlock();
tsepez4cf55152016-11-02 14:37:54 -0700645 pInterForm->SubmitForm(strURL, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700646 pRuntime->EndBlock();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700647 }
tsepez4cf55152016-11-02 14:37:54 -0700648 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700649 }
650
Lei Zhangd88a3642015-11-10 09:38:57 -0800651 std::vector<CPDF_FormField*> fieldObjects;
tsepezb4694242016-08-15 16:44:55 -0700652 for (int i = 0, sz = aFields.GetLength(pRuntime); i < sz; ++i) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700653 CJS_Value valName(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700654 aFields.GetElement(pRuntime, i, valName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700655
tsepezb4694242016-08-15 16:44:55 -0700656 CFX_WideString sName = valName.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700657 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
658 for (int j = 0, jsz = pPDFForm->CountFields(sName); j < jsz; ++j) {
659 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
660 if (!bEmpty && pField->GetValue().IsEmpty())
661 continue;
662
Lei Zhangd88a3642015-11-10 09:38:57 -0800663 fieldObjects.push_back(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700664 }
665 }
666
Lei Zhangd88a3642015-11-10 09:38:57 -0800667 if (pPDFInterForm->CheckRequiredFields(&fieldObjects, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700668 pRuntime->BeginBlock();
Wei Li97da9762016-03-11 17:00:48 -0800669 pInterForm->SubmitFields(strURL, fieldObjects, true, !bFDF);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700670 pRuntime->EndBlock();
671 }
tsepez4cf55152016-11-02 14:37:54 -0700672 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700673}
674
dsinclair82e17672016-10-11 12:38:01 -0700675void Document::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
676 m_pFormFillEnv.Reset(pFormFillEnv);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700677}
678
Tom Sepezb1670b52017-02-16 17:01:00 -0800679bool Document::bookmarkRoot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700680 CJS_PropValue& vp,
681 CFX_WideString& sError) {
682 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700683}
684
Tom Sepezb1670b52017-02-16 17:01:00 -0800685bool Document::mailDoc(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700686 const std::vector<CJS_Value>& params,
687 CJS_Value& vRet,
688 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700689 // TODO(tsepez): Check maximum number of allowed params.
tsepez4cf55152016-11-02 14:37:54 -0700690 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700691 CFX_WideString cTo = L"";
692 CFX_WideString cCc = L"";
693 CFX_WideString cBcc = L"";
694 CFX_WideString cSubject = L"";
695 CFX_WideString cMsg = L"";
696
tsepezf3dc8c62016-08-10 06:29:29 -0700697 if (params.size() >= 1)
tsepezb4694242016-08-15 16:44:55 -0700698 bUI = params[0].ToBool(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700699 if (params.size() >= 2)
tsepezb4694242016-08-15 16:44:55 -0700700 cTo = params[1].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700701 if (params.size() >= 3)
tsepezb4694242016-08-15 16:44:55 -0700702 cCc = params[2].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700703 if (params.size() >= 4)
tsepezb4694242016-08-15 16:44:55 -0700704 cBcc = params[3].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700705 if (params.size() >= 5)
tsepezb4694242016-08-15 16:44:55 -0700706 cSubject = params[4].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700707 if (params.size() >= 6)
tsepezb4694242016-08-15 16:44:55 -0700708 cMsg = params[5].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700709
Tom Sepez39bfe122015-09-17 15:25:23 -0700710 if (params.size() >= 1 && params[0].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700711 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700712
tsepezb4694242016-08-15 16:44:55 -0700713 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"bUI");
tsepez71ba5882016-10-28 09:35:49 -0700714 bUI = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700715
tsepezb4694242016-08-15 16:44:55 -0700716 pValue = pRuntime->GetObjectProperty(pObj, L"cTo");
717 cTo = 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"cCc");
720 cCc = 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"cBcc");
723 cBcc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700724
tsepezb4694242016-08-15 16:44:55 -0700725 pValue = pRuntime->GetObjectProperty(pObj, L"cSubject");
726 cSubject = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700727
tsepezb4694242016-08-15 16:44:55 -0700728 pValue = pRuntime->GetObjectProperty(pObj, L"cMsg");
729 cMsg = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700730 }
731
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700732 pRuntime->BeginBlock();
dsinclair8779fa82016-10-12 12:05:44 -0700733 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
734 pFormFillEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
735 cCc.c_str(), cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700736 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700737 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700738}
739
Tom Sepezb1670b52017-02-16 17:01:00 -0800740bool Document::author(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700741 CJS_PropValue& vp,
742 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800743 return getPropertyInternal(pRuntime, vp, "Author", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700744}
745
Tom Sepezb1670b52017-02-16 17:01:00 -0800746bool Document::info(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700747 CJS_PropValue& vp,
748 CFX_WideString& sError) {
tonikitooec268762016-08-10 12:29:28 -0700749 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700750 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700751 return false;
tonikitooec268762016-08-10 12:29:28 -0700752 }
dsinclair82e17672016-10-11 12:38:01 -0700753 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700754 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700755 return false;
tsepez55be02e2016-09-12 11:21:42 -0700756 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700757 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700758 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700759 return false;
Tom Sepezc6ab1722015-02-05 15:27:25 -0800760
dsinclair38fd8442016-09-15 10:15:32 -0700761 CFX_WideString cwAuthor = pDictionary->GetUnicodeTextFor("Author");
762 CFX_WideString cwTitle = pDictionary->GetUnicodeTextFor("Title");
763 CFX_WideString cwSubject = pDictionary->GetUnicodeTextFor("Subject");
764 CFX_WideString cwKeywords = pDictionary->GetUnicodeTextFor("Keywords");
765 CFX_WideString cwCreator = pDictionary->GetUnicodeTextFor("Creator");
766 CFX_WideString cwProducer = pDictionary->GetUnicodeTextFor("Producer");
767 CFX_WideString cwCreationDate =
768 pDictionary->GetUnicodeTextFor("CreationDate");
769 CFX_WideString cwModDate = pDictionary->GetUnicodeTextFor("ModDate");
770 CFX_WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped");
Tom Sepezc6ab1722015-02-05 15:27:25 -0800771
tsepezb4694242016-08-15 16:44:55 -0700772 v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1);
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800773 pRuntime->PutObjectProperty(pObj, L"Author",
774 pRuntime->NewString(cwAuthor.AsStringC()));
775 pRuntime->PutObjectProperty(pObj, L"Title",
776 pRuntime->NewString(cwTitle.AsStringC()));
777 pRuntime->PutObjectProperty(pObj, L"Subject",
778 pRuntime->NewString(cwSubject.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800779 pRuntime->PutObjectProperty(pObj, L"Keywords",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800780 pRuntime->NewString(cwKeywords.AsStringC()));
781 pRuntime->PutObjectProperty(pObj, L"Creator",
782 pRuntime->NewString(cwCreator.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800783 pRuntime->PutObjectProperty(pObj, L"Producer",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800784 pRuntime->NewString(cwProducer.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800785 pRuntime->PutObjectProperty(pObj, L"CreationDate",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800786 pRuntime->NewString(cwCreationDate.AsStringC()));
787 pRuntime->PutObjectProperty(pObj, L"ModDate",
788 pRuntime->NewString(cwModDate.AsStringC()));
789 pRuntime->PutObjectProperty(pObj, L"Trapped",
790 pRuntime->NewString(cwTrapped.AsStringC()));
tonikitooec268762016-08-10 12:29:28 -0700791
792 // It's to be compatible to non-standard info dictionary.
793 for (const auto& it : *pDictionary) {
794 const CFX_ByteString& bsKey = it.first;
tsepez0e606b52016-11-18 16:22:41 -0800795 CPDF_Object* pValueObj = it.second.get();
tonikitooec268762016-08-10 12:29:28 -0700796 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
797 if (pValueObj->IsString() || pValueObj->IsName()) {
tsepeze6cf0132017-01-18 14:38:18 -0800798 pRuntime->PutObjectProperty(
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800799 pObj, wsKey,
800 pRuntime->NewString(pValueObj->GetUnicodeText().AsStringC()));
tonikitooec268762016-08-10 12:29:28 -0700801 } else if (pValueObj->IsNumber()) {
tsepeze6cf0132017-01-18 14:38:18 -0800802 pRuntime->PutObjectProperty(pObj, wsKey,
803 pRuntime->NewNumber(pValueObj->GetNumber()));
tonikitooec268762016-08-10 12:29:28 -0700804 } else if (pValueObj->IsBoolean()) {
tsepeze6cf0132017-01-18 14:38:18 -0800805 pRuntime->PutObjectProperty(
806 pObj, wsKey, pRuntime->NewBoolean(!!pValueObj->GetInteger()));
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700807 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700808 }
tonikitooec268762016-08-10 12:29:28 -0700809 vp << pObj;
tsepez4cf55152016-11-02 14:37:54 -0700810 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700811}
812
Tom Sepezb1670b52017-02-16 17:01:00 -0800813bool Document::getPropertyInternal(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700814 CJS_PropValue& vp,
815 const CFX_ByteString& propName,
816 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700817 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700818 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700819 return false;
tsepez55be02e2016-09-12 11:21:42 -0700820 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700821 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700823 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700824
825 if (vp.IsGetting()) {
dsinclair38fd8442016-09-15 10:15:32 -0700826 vp << pDictionary->GetUnicodeTextFor(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700827 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700828 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
tsepez55be02e2016-09-12 11:21:42 -0700829 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700830 return false;
tsepez55be02e2016-09-12 11:21:42 -0700831 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700832 CFX_WideString csProperty;
833 vp >> csProperty;
tsepez0e606b52016-11-18 16:22:41 -0800834 pDictionary->SetNewFor<CPDF_String>(propName, PDF_EncodeText(csProperty),
835 false);
dsinclair7cbe68e2016-10-12 11:56:23 -0700836 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700837 }
tsepez4cf55152016-11-02 14:37:54 -0700838 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700839}
840
Tom Sepezb1670b52017-02-16 17:01:00 -0800841bool Document::creationDate(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, "CreationDate", sError);
tonikitooea3ff9e2016-08-02 11:52:28 -0700845}
846
Tom Sepezb1670b52017-02-16 17:01:00 -0800847bool Document::creator(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700848 CJS_PropValue& vp,
849 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800850 return getPropertyInternal(pRuntime, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700851}
852
Tom Sepezb1670b52017-02-16 17:01:00 -0800853bool Document::delay(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700854 CJS_PropValue& vp,
855 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700856 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700857 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700858 return false;
tsepez55be02e2016-09-12 11:21:42 -0700859 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700860 if (vp.IsGetting()) {
861 vp << m_bDelay;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800862 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700863 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800864 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
865 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
866 return false;
867 }
868 vp >> m_bDelay;
869 if (m_bDelay) {
870 m_DelayData.clear();
871 return true;
872 }
873 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
874 DelayDataToProcess.swap(m_DelayData);
875 for (const auto& pData : DelayDataToProcess)
876 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
877
tsepez4cf55152016-11-02 14:37:54 -0700878 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700879}
880
Tom Sepezb1670b52017-02-16 17:01:00 -0800881bool Document::keywords(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, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885}
886
Tom Sepezb1670b52017-02-16 17:01:00 -0800887bool Document::modDate(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, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700891}
892
Tom Sepezb1670b52017-02-16 17:01:00 -0800893bool Document::producer(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, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897}
898
Tom Sepezb1670b52017-02-16 17:01:00 -0800899bool Document::subject(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700900 CJS_PropValue& vp,
901 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800902 return getPropertyInternal(pRuntime, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903}
904
Tom Sepezb1670b52017-02-16 17:01:00 -0800905bool Document::title(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700906 CJS_PropValue& vp,
907 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700908 if (!m_pFormFillEnv || !m_pFormFillEnv->GetUnderlyingDocument()) {
tsepez55be02e2016-09-12 11:21:42 -0700909 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700910 return false;
tsepez55be02e2016-09-12 11:21:42 -0700911 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800912 return getPropertyInternal(pRuntime, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700913}
914
Tom Sepezb1670b52017-02-16 17:01:00 -0800915bool Document::numPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700916 CJS_PropValue& vp,
917 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700918 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700919 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700920 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700921 }
dsinclair82e17672016-10-11 12:38:01 -0700922 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700923 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700924 return false;
tsepez55be02e2016-09-12 11:21:42 -0700925 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700926 vp << m_pFormFillEnv->GetPageCount();
tsepez4cf55152016-11-02 14:37:54 -0700927 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700928}
929
Tom Sepezb1670b52017-02-16 17:01:00 -0800930bool Document::external(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700931 CJS_PropValue& vp,
932 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700933 // In Chrome case, should always return true.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700934 if (vp.IsGetting()) {
935 vp << true;
936 }
tsepez4cf55152016-11-02 14:37:54 -0700937 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700938}
939
Tom Sepezb1670b52017-02-16 17:01:00 -0800940bool Document::filesize(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700941 CJS_PropValue& vp,
942 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700943 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700944 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700945 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700946 }
947 vp << 0;
tsepez4cf55152016-11-02 14:37:54 -0700948 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700949}
950
Tom Sepezb1670b52017-02-16 17:01:00 -0800951bool Document::mouseX(CJS_Runtime* pRuntime,
tonikitoo33c4cdb2016-08-08 10:52:51 -0700952 CJS_PropValue& vp,
953 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700954 return true;
955}
956
Tom Sepezb1670b52017-02-16 17:01:00 -0800957bool Document::mouseY(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700958 CJS_PropValue& vp,
959 CFX_WideString& sError) {
960 return true;
961}
962
Tom Sepezb1670b52017-02-16 17:01:00 -0800963bool Document::URL(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800964 CJS_PropValue& vp,
965 CFX_WideString& sError) {
tonikitoo33c4cdb2016-08-08 10:52:51 -0700966 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700967 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700968 return false;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700969 }
dsinclair82e17672016-10-11 12:38:01 -0700970 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700971 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700972 return false;
tsepez55be02e2016-09-12 11:21:42 -0700973 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700974 vp << m_pFormFillEnv->JS_docGetFilePath();
tsepez4cf55152016-11-02 14:37:54 -0700975 return true;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700976}
977
Tom Sepezb1670b52017-02-16 17:01:00 -0800978bool Document::baseURL(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700979 CJS_PropValue& vp,
980 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981 if (vp.IsGetting()) {
982 vp << m_cwBaseURL;
983 } else {
984 vp >> m_cwBaseURL;
985 }
tsepez4cf55152016-11-02 14:37:54 -0700986 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700987}
988
Tom Sepezb1670b52017-02-16 17:01:00 -0800989bool Document::calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700990 CJS_PropValue& vp,
991 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700992 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700993 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700994 return false;
tsepez55be02e2016-09-12 11:21:42 -0700995 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700996 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700997 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -0700998 vp << !!pInterForm->IsCalculateEnabled();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800999 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001001 bool bCalculate;
1002 vp >> bCalculate;
1003 pInterForm->EnableCalculate(bCalculate);
tsepez4cf55152016-11-02 14:37:54 -07001004 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005}
1006
Tom Sepezb1670b52017-02-16 17:01:00 -08001007bool Document::documentFileName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001008 CJS_PropValue& vp,
1009 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001010 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001011 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001012 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001013 }
dsinclair82e17672016-10-11 12:38:01 -07001014 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001015 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001016 return false;
tsepez55be02e2016-09-12 11:21:42 -07001017 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001018 CFX_WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
Ryan Harrisonaa3a9cd2017-08-29 16:39:44 -04001019 FX_STRSIZE i = wsFilePath.GetLength() - 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001020 for (; i >= 0; i--) {
Ryan Harrison8a1758b2017-08-15 10:37:59 -04001021 if (wsFilePath[i] == L'\\' || wsFilePath[i] == L'/')
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022 break;
1023 }
1024 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
1025 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
1026 } else {
1027 vp << L"";
1028 }
tsepez4cf55152016-11-02 14:37:54 -07001029 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001030}
1031
Tom Sepezb1670b52017-02-16 17:01:00 -08001032bool Document::path(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001033 CJS_PropValue& vp,
1034 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001036 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001037 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001038 }
dsinclair82e17672016-10-11 12:38:01 -07001039 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001040 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001041 return false;
tsepez55be02e2016-09-12 11:21:42 -07001042 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001043 vp << app::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath());
tsepez4cf55152016-11-02 14:37:54 -07001044 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001045}
1046
Tom Sepezb1670b52017-02-16 17:01:00 -08001047bool Document::pageWindowRect(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::layout(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001054 CJS_PropValue& vp,
1055 CFX_WideString& sError) {
1056 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001057}
1058
Tom Sepezb1670b52017-02-16 17:01:00 -08001059bool Document::addLink(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001060 const std::vector<CJS_Value>& params,
1061 CJS_Value& vRet,
1062 CFX_WideString& sError) {
1063 return true;
1064}
1065
Tom Sepezb1670b52017-02-16 17:01:00 -08001066bool Document::closeDoc(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001067 const std::vector<CJS_Value>& params,
1068 CJS_Value& vRet,
1069 CFX_WideString& sError) {
1070 return true;
1071}
1072
Tom Sepezb1670b52017-02-16 17:01:00 -08001073bool Document::getPageBox(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001074 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001075 CJS_Value& vRet,
1076 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001077 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001078}
1079
Tom Sepezb1670b52017-02-16 17:01:00 -08001080bool Document::getAnnot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001081 const std::vector<CJS_Value>& params,
1082 CJS_Value& vRet,
1083 CFX_WideString& sError) {
tonikitoo618cb1f2016-08-18 20:10:17 -07001084 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001085 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001086 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001087 }
dsinclair82e17672016-10-11 12:38:01 -07001088 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001089 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001090 return false;
tsepez55be02e2016-09-12 11:21:42 -07001091 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001092 int nPageNo = params[0].ToInt(pRuntime);
1093 CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -07001094 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(nPageNo);
tonikitoo618cb1f2016-08-18 20:10:17 -07001095 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001096 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001097
tsepezd805eec2017-01-11 14:03:54 -08001098 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
tonikitoo618cb1f2016-08-18 20:10:17 -07001099 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
tsepezd805eec2017-01-11 14:03:54 -08001100 for (const auto& pSDKAnnotCur : annotIteration) {
1101 CPDFSDK_BAAnnot* pBAAnnot =
1102 static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get());
tonikitoo618cb1f2016-08-18 20:10:17 -07001103 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1104 pSDKBAAnnot = pBAAnnot;
1105 break;
1106 }
1107 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001108 if (!pSDKBAAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001109 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001110
1111 v8::Local<v8::Object> pObj =
1112 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1113 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001114 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001115
1116 CJS_Annot* pJS_Annot =
1117 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
tonikitoo618cb1f2016-08-18 20:10:17 -07001118 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
tonikitoo618cb1f2016-08-18 20:10:17 -07001119 pAnnot->SetSDKAnnot(pSDKBAAnnot);
tonikitoo618cb1f2016-08-18 20:10:17 -07001120 vRet = CJS_Value(pRuntime, pJS_Annot);
tsepez4cf55152016-11-02 14:37:54 -07001121 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001122}
1123
Tom Sepezb1670b52017-02-16 17:01:00 -08001124bool Document::getAnnots(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001125 const std::vector<CJS_Value>& params,
1126 CJS_Value& vRet,
1127 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001128 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001129 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001130 return false;
tsepez55be02e2016-09-12 11:21:42 -07001131 }
tonikitoo0ee35902016-08-26 14:41:29 -07001132 // TODO(tonikitoo): Add support supported parameters as per
1133 // the PDF spec.
1134
dsinclair7cbe68e2016-10-12 11:56:23 -07001135 int nPageNo = m_pFormFillEnv->GetPageCount();
tonikitoo0ee35902016-08-26 14:41:29 -07001136 CJS_Array annots;
1137
1138 for (int i = 0; i < nPageNo; ++i) {
dsinclair7cbe68e2016-10-12 11:56:23 -07001139 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(i);
tonikitoo0ee35902016-08-26 14:41:29 -07001140 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001141 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001142
tsepezd805eec2017-01-11 14:03:54 -08001143 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
1144 for (const auto& pSDKAnnotCur : annotIteration) {
1145 if (!pSDKAnnotCur) {
1146 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001147 return false;
tsepezd805eec2017-01-11 14:03:54 -08001148 }
tonikitoo0ee35902016-08-26 14:41:29 -07001149 v8::Local<v8::Object> pObj =
1150 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1151 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001152 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001153
1154 CJS_Annot* pJS_Annot =
1155 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
tonikitoo0ee35902016-08-26 14:41:29 -07001156 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
tsepezd805eec2017-01-11 14:03:54 -08001157 pAnnot->SetSDKAnnot(static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get()));
tonikitoo0ee35902016-08-26 14:41:29 -07001158 annots.SetElement(pRuntime, i, CJS_Value(pRuntime, pJS_Annot));
1159 }
1160 }
tonikitoo0ee35902016-08-26 14:41:29 -07001161 vRet = CJS_Value(pRuntime, annots);
tsepez4cf55152016-11-02 14:37:54 -07001162 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163}
1164
Tom Sepezb1670b52017-02-16 17:01:00 -08001165bool Document::getAnnot3D(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001166 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167 CJS_Value& vRet,
1168 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001169 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001170 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171}
1172
Tom Sepezb1670b52017-02-16 17:01:00 -08001173bool Document::getAnnots3D(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001174 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175 CJS_Value& vRet,
1176 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001177 return true;
1178}
1179
Tom Sepezb1670b52017-02-16 17:01:00 -08001180bool Document::getOCGs(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001181 const std::vector<CJS_Value>& params,
1182 CJS_Value& vRet,
1183 CFX_WideString& sError) {
1184 return true;
1185}
1186
Tom Sepezb1670b52017-02-16 17:01:00 -08001187bool Document::getLinks(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001188 const std::vector<CJS_Value>& params,
1189 CJS_Value& vRet,
1190 CFX_WideString& sError) {
1191 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001192}
1193
1194bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1195 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1196 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1197}
1198
Tom Sepezb1670b52017-02-16 17:01:00 -08001199bool Document::addIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001200 const std::vector<CJS_Value>& params,
1201 CJS_Value& vRet,
1202 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001203 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001204 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001205 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001206 }
tsepezf3dc8c62016-08-10 06:29:29 -07001207
tsepezb4694242016-08-15 16:44:55 -07001208 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Tom Sepez39bfe122015-09-17 15:25:23 -07001209 if (params[1].GetType() != CJS_Value::VT_object) {
tsepezcd5dc852016-09-08 11:23:24 -07001210 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001211 return false;
Tom Sepezaecd9d02015-08-31 15:04:12 -07001212 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001213
tsepezb4694242016-08-15 16:44:55 -07001214 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pRuntime);
Tom Sepezce84f712017-05-30 17:17:33 -07001215 if (CFXJS_Engine::GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
tsepezcd5dc852016-09-08 11:23:24 -07001216 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001217 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001218 }
1219
Lei Zhang313c4252017-02-27 16:37:36 -08001220 if (!params[1].ToCJSObject(pRuntime)->GetEmbedObject()) {
tsepezcd5dc852016-09-08 11:23:24 -07001221 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001222 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001223 }
1224
Lei Zhang313c4252017-02-27 16:37:36 -08001225 m_IconNames.push_back(swIconName);
tsepez4cf55152016-11-02 14:37:54 -07001226 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001227}
1228
Tom Sepezb1670b52017-02-16 17:01:00 -08001229bool Document::icons(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001230 CJS_PropValue& vp,
1231 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001232 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001233 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001234 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001235 }
Lei Zhang313c4252017-02-27 16:37:36 -08001236 if (m_IconNames.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001237 vp.GetJSValue()->SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001238 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001239 }
1240
tsepeze5aff742016-08-08 09:49:42 -07001241 CJS_Array Icons;
Tom Sepez6ba32d92016-01-05 16:26:32 -08001242 int i = 0;
Lei Zhang313c4252017-02-27 16:37:36 -08001243 for (const auto& name : m_IconNames) {
tsepezb4694242016-08-15 16:44:55 -07001244 v8::Local<v8::Object> pObj =
1245 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001246 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001247 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001248
tsepezb4694242016-08-15 16:44:55 -07001249 CJS_Icon* pJS_Icon =
1250 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
thestigdadea5f2016-10-19 13:29:48 -07001251 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
Lei Zhang313c4252017-02-27 16:37:36 -08001252 pIcon->SetIconName(name);
tsepezb4694242016-08-15 16:44:55 -07001253 Icons.SetElement(pRuntime, i++, CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001254 }
1255
1256 vp << Icons;
tsepez4cf55152016-11-02 14:37:54 -07001257 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258}
1259
Tom Sepezb1670b52017-02-16 17:01:00 -08001260bool Document::getIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001261 const std::vector<CJS_Value>& params,
1262 CJS_Value& vRet,
1263 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001264 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001265 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001266 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267 }
1268
Lei Zhang313c4252017-02-27 16:37:36 -08001269 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
1270 auto it = std::find(m_IconNames.begin(), m_IconNames.end(), swIconName);
1271 if (it == m_IconNames.end())
tsepez4cf55152016-11-02 14:37:54 -07001272 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001273
Lei Zhang313c4252017-02-27 16:37:36 -08001274 v8::Local<v8::Object> pObj =
1275 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
1276 if (pObj.IsEmpty())
1277 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001278
Lei Zhang313c4252017-02-27 16:37:36 -08001279 CJS_Icon* pJS_Icon = static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
1280 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
1281 pIcon->SetIconName(*it);
1282 vRet = CJS_Value(pRuntime, pJS_Icon);
1283 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001284}
1285
Tom Sepezb1670b52017-02-16 17:01:00 -08001286bool Document::removeIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001287 const std::vector<CJS_Value>& params,
1288 CJS_Value& vRet,
1289 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001290 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001291 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001292}
1293
Tom Sepezb1670b52017-02-16 17:01:00 -08001294bool Document::createDataObject(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001295 const std::vector<CJS_Value>& params,
1296 CJS_Value& vRet,
1297 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001298 // Unsafe, not implemented.
tsepez4cf55152016-11-02 14:37:54 -07001299 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001300}
1301
Tom Sepezb1670b52017-02-16 17:01:00 -08001302bool Document::media(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001303 CJS_PropValue& vp,
1304 CFX_WideString& sError) {
1305 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001306}
1307
Tom Sepezb1670b52017-02-16 17:01:00 -08001308bool Document::calculateNow(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001309 const std::vector<CJS_Value>& params,
1310 CJS_Value& vRet,
1311 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001312 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001313 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001314 return false;
tsepez55be02e2016-09-12 11:21:42 -07001315 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001316 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
1317 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
1318 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -07001319 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001320 return false;
tsepez55be02e2016-09-12 11:21:42 -07001321 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001322 m_pFormFillEnv->GetInterForm()->OnCalculate();
tsepez4cf55152016-11-02 14:37:54 -07001323 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001324}
1325
Tom Sepezb1670b52017-02-16 17:01:00 -08001326bool Document::Collab(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001327 CJS_PropValue& vp,
1328 CFX_WideString& sError) {
1329 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001330}
1331
Tom Sepezb1670b52017-02-16 17:01:00 -08001332bool Document::getPageNthWord(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001333 const std::vector<CJS_Value>& params,
1334 CJS_Value& vRet,
1335 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001336 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001337 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001338 return false;
tsepez55be02e2016-09-12 11:21:42 -07001339 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001340 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001341 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001342 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001343 }
tsepez55be02e2016-09-12 11:21:42 -07001344
1345 // TODO(tsepez): check maximum allowable params.
1346
tsepezb4694242016-08-15 16:44:55 -07001347 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
1348 int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
1349 bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001350
dsinclair7cbe68e2016-10-12 11:56:23 -07001351 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001352 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001353 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001354
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001355 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001356 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001357 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001358 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001359
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001360 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1361 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001362 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001363
thestig5cc24652016-04-26 11:46:02 -07001364 CPDF_Page page(pDocument, pPageDict, true);
1365 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001366
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001367 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001368 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001369 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001370 if (pPageObj->IsText()) {
1371 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001372 int nObjWords = CountWords(pTextObj);
1373 if (nWords + nObjWords >= nWordNo) {
1374 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1375 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001376 }
Tom Sepez2398d892016-02-17 16:46:26 -08001377 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001378 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001379 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001380
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001381 if (bStrip) {
1382 swRet.TrimLeft();
1383 swRet.TrimRight();
1384 }
1385
tsepezf3dc8c62016-08-10 06:29:29 -07001386 vRet = CJS_Value(pRuntime, swRet.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001387 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001388}
1389
Tom Sepezb1670b52017-02-16 17:01:00 -08001390bool Document::getPageNthWordQuads(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001391 const std::vector<CJS_Value>& params,
1392 CJS_Value& vRet,
1393 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001394 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001395 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001396 return false;
tsepez55be02e2016-09-12 11:21:42 -07001397 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001398 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -07001399 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001400 return false;
tsepez55be02e2016-09-12 11:21:42 -07001401 }
tsepez4cf55152016-11-02 14:37:54 -07001402 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001403}
1404
Tom Sepezb1670b52017-02-16 17:01:00 -08001405bool Document::getPageNumWords(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001406 const std::vector<CJS_Value>& params,
1407 CJS_Value& vRet,
1408 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001409 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001410 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001411 return false;
tsepez55be02e2016-09-12 11:21:42 -07001412 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001413 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001414 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001415 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001416 }
tsepezb4694242016-08-15 16:44:55 -07001417 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
dsinclair7cbe68e2016-10-12 11:56:23 -07001418 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001419 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001420 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001421 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001422 }
1423
1424 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1425 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001426 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001427
thestig5cc24652016-04-26 11:46:02 -07001428 CPDF_Page page(pDocument, pPageDict, true);
1429 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001430
1431 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001432 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001433 if (pPageObj->IsText())
1434 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001435 }
1436
tsepezf3dc8c62016-08-10 06:29:29 -07001437 vRet = CJS_Value(pRuntime, nWords);
tsepez4cf55152016-11-02 14:37:54 -07001438 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001439}
1440
Tom Sepezb1670b52017-02-16 17:01:00 -08001441bool Document::getPrintParams(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001442 const std::vector<CJS_Value>& params,
1443 CJS_Value& vRet,
1444 CFX_WideString& sError) {
tsepezb4694242016-08-15 16:44:55 -07001445 v8::Local<v8::Object> pRetObj =
1446 pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::g_nObjDefnID);
Tom Sepezc5a14722017-02-24 15:31:12 -08001447 if (pRetObj.IsEmpty())
1448 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001449
1450 // Not implemented yet.
1451
tsepezf3dc8c62016-08-10 06:29:29 -07001452 vRet = CJS_Value(pRuntime, pRetObj);
tsepez4cf55152016-11-02 14:37:54 -07001453 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001454}
1455
1456#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1457
1458int Document::CountWords(CPDF_TextObject* pTextObj) {
1459 if (!pTextObj)
1460 return 0;
1461
1462 int nWords = 0;
1463
1464 CPDF_Font* pFont = pTextObj->GetFont();
1465 if (!pFont)
1466 return 0;
1467
tsepez4cf55152016-11-02 14:37:54 -07001468 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001469
1470 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001471 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Dan Sinclair05df0752017-03-14 14:43:42 -04001472 float kerning;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001473
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001474 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001475 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1476
Tom Sepez62a70f92016-03-21 15:00:20 -07001477 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001478 if (swUnicode.GetLength() > 0)
1479 unicode = swUnicode[0];
1480
1481 if (ISLATINWORD(unicode) && bIsLatin)
1482 continue;
1483
1484 bIsLatin = ISLATINWORD(unicode);
1485 if (unicode != 0x20)
1486 nWords++;
1487 }
1488
1489 return nWords;
1490}
1491
1492CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1493 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001494 CFX_WideString swRet;
1495
1496 CPDF_Font* pFont = pTextObj->GetFont();
1497 if (!pFont)
1498 return L"";
1499
1500 int nWords = 0;
tsepez4cf55152016-11-02 14:37:54 -07001501 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001502
1503 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001504 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Dan Sinclair05df0752017-03-14 14:43:42 -04001505 float kerning;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001506
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001507 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001508 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1509
Tom Sepez62a70f92016-03-21 15:00:20 -07001510 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001511 if (swUnicode.GetLength() > 0)
1512 unicode = swUnicode[0];
1513
1514 if (ISLATINWORD(unicode) && bIsLatin) {
1515 } else {
1516 bIsLatin = ISLATINWORD(unicode);
1517 if (unicode != 0x20)
1518 nWords++;
1519 }
1520
1521 if (nWords - 1 == nWordIndex)
1522 swRet += unicode;
1523 }
1524
1525 return swRet;
1526}
1527
Tom Sepezb1670b52017-02-16 17:01:00 -08001528bool Document::zoom(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001529 CJS_PropValue& vp,
1530 CFX_WideString& sError) {
1531 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001532}
1533
1534/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001535(none, NoVary)
1536(fitP, FitPage)
1537(fitW, FitWidth)
1538(fitH, FitHeight)
1539(fitV, FitVisibleWidth)
1540(pref, Preferred)
1541(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001542*/
1543
Tom Sepezb1670b52017-02-16 17:01:00 -08001544bool Document::zoomType(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001545 CJS_PropValue& vp,
1546 CFX_WideString& sError) {
1547 return true;
1548}
1549
Tom Sepezb1670b52017-02-16 17:01:00 -08001550bool Document::deletePages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001551 const std::vector<CJS_Value>& params,
1552 CJS_Value& vRet,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001553 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001554 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001555 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001556}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001557
Tom Sepezb1670b52017-02-16 17:01:00 -08001558bool Document::extractPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001559 const std::vector<CJS_Value>& params,
1560 CJS_Value& vRet,
1561 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001562 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001563 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001564}
1565
Tom Sepezb1670b52017-02-16 17:01:00 -08001566bool Document::insertPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001567 const std::vector<CJS_Value>& params,
1568 CJS_Value& vRet,
1569 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001570 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001571 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001572}
1573
Tom Sepezb1670b52017-02-16 17:01:00 -08001574bool Document::replacePages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001575 const std::vector<CJS_Value>& params,
1576 CJS_Value& vRet,
1577 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001578 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001579 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001580}
1581
Tom Sepezb1670b52017-02-16 17:01:00 -08001582bool Document::getURL(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001583 const std::vector<CJS_Value>& params,
1584 CJS_Value& vRet,
1585 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001586 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001587 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001588}
1589
Tom Sepezb1670b52017-02-16 17:01:00 -08001590bool Document::gotoNamedDest(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001591 const std::vector<CJS_Value>& params,
1592 CJS_Value& vRet,
1593 CFX_WideString& sError) {
tonikitoo1c836752016-08-08 16:14:05 -07001594 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001595 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001596 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001597 }
dsinclair82e17672016-10-11 12:38:01 -07001598 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001599 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001600 return false;
tsepez55be02e2016-09-12 11:21:42 -07001601 }
tsepezb4694242016-08-15 16:44:55 -07001602 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -07001603 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
tonikitoo1c836752016-08-08 16:14:05 -07001604 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001605 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001606
tonikitoo1c836752016-08-08 16:14:05 -07001607 CPDF_NameTree nameTree(pDocument, "Dests");
Jane Liu67ccef72017-07-19 13:10:50 -04001608 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, wideName);
tonikitoo1c836752016-08-08 16:14:05 -07001609 if (!destArray)
tsepez4cf55152016-11-02 14:37:54 -07001610 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001611
1612 CPDF_Dest dest(destArray);
1613 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
Tom Sepezd7188f72017-05-02 15:10:58 -07001614 std::vector<float> scrollPositionArray;
tonikitoo1c836752016-08-08 16:14:05 -07001615 if (arrayObject) {
tonikitoo1c836752016-08-08 16:14:05 -07001616 for (size_t i = 2; i < arrayObject->GetCount(); i++)
Tom Sepezd7188f72017-05-02 15:10:58 -07001617 scrollPositionArray.push_back(arrayObject->GetFloatAt(i));
tonikitoo1c836752016-08-08 16:14:05 -07001618 }
tsepezb4694242016-08-15 16:44:55 -07001619 pRuntime->BeginBlock();
dsinclair82e17672016-10-11 12:38:01 -07001620 m_pFormFillEnv->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
Tom Sepezd7188f72017-05-02 15:10:58 -07001621 scrollPositionArray.data(),
1622 scrollPositionArray.size());
tsepezb4694242016-08-15 16:44:55 -07001623 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -07001624 return true;
tonikitoo1c836752016-08-08 16:14:05 -07001625}
1626
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001627void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001628 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001629}
1630
1631void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1632 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001633 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1634 auto iter = m_DelayData.begin();
1635 while (iter != m_DelayData.end()) {
1636 auto old = iter++;
1637 if ((*old)->sFieldName == sFieldName &&
1638 (*old)->nControlIndex == nControlIndex) {
1639 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1640 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001641 }
1642 }
1643
dsinclair82e17672016-10-11 12:38:01 -07001644 for (const auto& pData : DelayDataForFieldAndControlIndex)
1645 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001646}
1647
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001648CJS_Document* Document::GetCJSDoc() const {
Tom Sepez797ca5c2017-05-25 12:03:18 -07001649 return static_cast<CJS_Document*>(m_pJSObject.Get());
JUN FANG33f6f0d2015-04-06 12:39:51 -07001650}