blob: 3fdb906108cb78418209ae437d9fd5a9b9632058 [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
thestig7c292e02016-09-28 14:14:26 -07009#include <utility>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050010#include <vector>
11
dsinclairbc5e6d22016-10-04 11:08:49 -070012#include "core/fpdfapi/font/cpdf_font.h"
dsinclair41872fa2016-10-04 11:29:35 -070013#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070014#include "core/fpdfapi/parser/cpdf_array.h"
15#include "core/fpdfapi/parser/cpdf_document.h"
tsepez0e606b52016-11-18 16:22:41 -080016#include "core/fpdfapi/parser/cpdf_string.h"
dsinclair488b7ad2016-10-04 11:55:50 -070017#include "core/fpdfapi/parser/fpdf_parser_decode.h"
dsinclair1727aee2016-09-29 13:12:56 -070018#include "core/fpdfdoc/cpdf_interform.h"
19#include "core/fpdfdoc/cpdf_nametree.h"
tsepezd805eec2017-01-11 14:03:54 -080020#include "fpdfsdk/cpdfsdk_annotiteration.h"
dsinclair735606d2016-10-05 15:47:02 -070021#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070022#include "fpdfsdk/cpdfsdk_interform.h"
23#include "fpdfsdk/cpdfsdk_pageview.h"
24#include "fpdfsdk/cpdfsdk_widget.h"
tonikitoo618cb1f2016-08-18 20:10:17 -070025#include "fpdfsdk/javascript/Annot.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040026#include "fpdfsdk/javascript/Field.h"
27#include "fpdfsdk/javascript/Icon.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040028#include "fpdfsdk/javascript/JS_Define.h"
29#include "fpdfsdk/javascript/JS_EventHandler.h"
30#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040031#include "fpdfsdk/javascript/JS_Value.h"
32#include "fpdfsdk/javascript/app.h"
Tom Sepezd6ae2af2017-02-16 11:49:55 -080033#include "fpdfsdk/javascript/cjs_event_context.h"
dsinclair64376be2016-03-31 20:03:24 -070034#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040035#include "fpdfsdk/javascript/resource.h"
Lei Zhang8241df72015-11-06 14:38:48 -080036#include "third_party/base/numerics/safe_math.h"
thestigdadea5f2016-10-19 13:29:48 -070037#include "third_party/base/ptr_util.h"
Chris Palmer9108ad22014-06-26 16:01:46 -070038
Tom Sepez04557b82017-02-16 09:43:10 -080039JSConstSpec CJS_PrintParamsObj::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040
Tom Sepez04557b82017-02-16 09:43:10 -080041JSPropertySpec CJS_PrintParamsObj::PropertySpecs[] = {{0, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042
Tom Sepez04557b82017-02-16 09:43:10 -080043JSMethodSpec CJS_PrintParamsObj::MethodSpecs[] = {{0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
45IMPLEMENT_JS_CLASS(CJS_PrintParamsObj, PrintParamsObj)
46
47PrintParamsObj::PrintParamsObj(CJS_Object* pJSObject)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 : CJS_EmbedObj(pJSObject) {
tsepez4cf55152016-11-02 14:37:54 -070049 bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 nStart = 0;
51 nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -070052 bSilent = false;
53 bShrinkToFit = false;
54 bPrintAsImage = false;
55 bReverse = false;
56 bAnnotations = true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057}
58
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059#define MINWIDTH 5.0f
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060#define MINHEIGHT 5.0f
61
Tom Sepez04557b82017-02-16 09:43:10 -080062JSConstSpec CJS_Document::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063
Tom Sepez04557b82017-02-16 09:43:10 -080064JSPropertySpec CJS_Document::PropertySpecs[] = {
Tom Sepez4d5b8c52017-02-21 15:17:07 -080065 {"ADBE", get_ADBE_static, set_ADBE_static},
66 {"author", get_author_static, set_author_static},
67 {"baseURL", get_baseURL_static, set_baseURL_static},
68 {"bookmarkRoot", get_bookmarkRoot_static, set_bookmarkRoot_static},
69 {"calculate", get_calculate_static, set_calculate_static},
70 {"Collab", get_Collab_static, set_Collab_static},
71 {"creationDate", get_creationDate_static, set_creationDate_static},
72 {"creator", get_creator_static, set_creator_static},
73 {"delay", get_delay_static, set_delay_static},
74 {"dirty", get_dirty_static, set_dirty_static},
75 {"documentFileName", get_documentFileName_static,
Tom Sepez04557b82017-02-16 09:43:10 -080076 set_documentFileName_static},
Tom Sepez4d5b8c52017-02-21 15:17:07 -080077 {"external", get_external_static, set_external_static},
78 {"filesize", get_filesize_static, set_filesize_static},
79 {"icons", get_icons_static, set_icons_static},
80 {"info", get_info_static, set_info_static},
81 {"keywords", get_keywords_static, set_keywords_static},
82 {"layout", get_layout_static, set_layout_static},
83 {"media", get_media_static, set_media_static},
84 {"modDate", get_modDate_static, set_modDate_static},
85 {"mouseX", get_mouseX_static, set_mouseX_static},
86 {"mouseY", get_mouseY_static, set_mouseY_static},
87 {"numFields", get_numFields_static, set_numFields_static},
88 {"numPages", get_numPages_static, set_numPages_static},
89 {"pageNum", get_pageNum_static, set_pageNum_static},
90 {"pageWindowRect", get_pageWindowRect_static, set_pageWindowRect_static},
91 {"path", get_path_static, set_path_static},
92 {"producer", get_producer_static, set_producer_static},
93 {"subject", get_subject_static, set_subject_static},
94 {"title", get_title_static, set_title_static},
95 {"URL", get_URL_static, set_URL_static},
96 {"zoom", get_zoom_static, set_zoom_static},
97 {"zoomType", get_zoomType_static, set_zoomType_static},
Tom Sepez04557b82017-02-16 09:43:10 -080098 {0, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099
Tom Sepez04557b82017-02-16 09:43:10 -0800100JSMethodSpec CJS_Document::MethodSpecs[] = {
Tom Sepez9b99b632017-02-21 15:05:57 -0800101 {"addAnnot", addAnnot_static},
102 {"addField", addField_static},
103 {"addLink", addLink_static},
104 {"addIcon", addIcon_static},
105 {"calculateNow", calculateNow_static},
106 {"closeDoc", closeDoc_static},
107 {"createDataObject", createDataObject_static},
108 {"deletePages", deletePages_static},
109 {"exportAsText", exportAsText_static},
110 {"exportAsFDF", exportAsFDF_static},
111 {"exportAsXFDF", exportAsXFDF_static},
112 {"extractPages", extractPages_static},
113 {"getAnnot", getAnnot_static},
114 {"getAnnots", getAnnots_static},
115 {"getAnnot3D", getAnnot3D_static},
116 {"getAnnots3D", getAnnots3D_static},
117 {"getField", getField_static},
118 {"getIcon", getIcon_static},
119 {"getLinks", getLinks_static},
120 {"getNthFieldName", getNthFieldName_static},
121 {"getOCGs", getOCGs_static},
122 {"getPageBox", getPageBox_static},
123 {"getPageNthWord", getPageNthWord_static},
124 {"getPageNthWordQuads", getPageNthWordQuads_static},
125 {"getPageNumWords", getPageNumWords_static},
126 {"getPrintParams", getPrintParams_static},
127 {"getURL", getURL_static},
128 {"gotoNamedDest", gotoNamedDest_static},
129 {"importAnFDF", importAnFDF_static},
130 {"importAnXFDF", importAnXFDF_static},
131 {"importTextData", importTextData_static},
132 {"insertPages", insertPages_static},
133 {"mailForm", mailForm_static},
134 {"print", print_static},
135 {"removeField", removeField_static},
136 {"replacePages", replacePages_static},
137 {"resetForm", resetForm_static},
138 {"removeIcon", removeIcon_static},
139 {"saveAs", saveAs_static},
140 {"submitForm", submitForm_static},
141 {"syncAnnotScan", syncAnnotScan_static},
142 {"mailDoc", mailDoc_static},
Tom Sepez04557b82017-02-16 09:43:10 -0800143 {0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144
145IMPLEMENT_JS_CLASS(CJS_Document, Document)
146
Tom Sepez33420902015-10-13 15:00:10 -0700147void CJS_Document::InitInstance(IJS_Runtime* pIRuntime) {
148 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime);
149 Document* pDoc = static_cast<Document*>(GetEmbedObject());
dsinclair82e17672016-10-11 12:38:01 -0700150 pDoc->SetFormFillEnv(pRuntime->GetFormFillEnv());
Tom Sepez33420902015-10-13 15:00:10 -0700151}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153Document::Document(CJS_Object* pJSObject)
154 : CJS_EmbedObj(pJSObject),
dsinclair82e17672016-10-11 12:38:01 -0700155 m_pFormFillEnv(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 m_cwBaseURL(L""),
tsepez4cf55152016-11-02 14:37:54 -0700157 m_bDelay(false) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700158
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159Document::~Document() {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160}
161
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162// the total number of fileds in document.
Tom Sepezb1670b52017-02-16 17:01:00 -0800163bool Document::numFields(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700164 CJS_PropValue& vp,
165 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700167 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700168 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 }
dsinclair82e17672016-10-11 12:38:01 -0700170 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700171 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700172 return false;
tsepez55be02e2016-09-12 11:21:42 -0700173 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700174 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700176 vp << static_cast<int>(pPDFForm->CountFields(CFX_WideString()));
tsepez4cf55152016-11-02 14:37:54 -0700177 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178}
179
Tom Sepezb1670b52017-02-16 17:01:00 -0800180bool Document::dirty(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700181 CJS_PropValue& vp,
182 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700183 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700184 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700185 return false;
tsepez55be02e2016-09-12 11:21:42 -0700186 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 if (vp.IsGetting()) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700188 vp << !!m_pFormFillEnv->GetChangeMark();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800189 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800191 bool bChanged = false;
192 vp >> bChanged;
193 if (bChanged)
194 m_pFormFillEnv->SetChangeMark();
195 else
196 m_pFormFillEnv->ClearChangeMark();
197
tsepez4cf55152016-11-02 14:37:54 -0700198 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199}
200
Tom Sepezb1670b52017-02-16 17:01:00 -0800201bool Document::ADBE(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700202 CJS_PropValue& vp,
203 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700204 if (vp.IsGetting())
Tom Sepezb1670b52017-02-16 17:01:00 -0800205 vp.GetJSValue()->SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206
tsepez4cf55152016-11-02 14:37:54 -0700207 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208}
209
Tom Sepezb1670b52017-02-16 17:01:00 -0800210bool Document::pageNum(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700211 CJS_PropValue& vp,
212 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700213 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700214 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700215 return false;
tsepez55be02e2016-09-12 11:21:42 -0700216 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217 if (vp.IsGetting()) {
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800218 if (CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetCurrentView())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 vp << pPageView->GetPageIndex();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800220 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800222 int iPageCount = m_pFormFillEnv->GetPageCount();
223 int iPageNum = 0;
224 vp >> iPageNum;
225 if (iPageNum >= 0 && iPageNum < iPageCount)
226 m_pFormFillEnv->JS_docgotoPage(iPageNum);
227 else if (iPageNum >= iPageCount)
228 m_pFormFillEnv->JS_docgotoPage(iPageCount - 1);
229 else if (iPageNum < 0)
230 m_pFormFillEnv->JS_docgotoPage(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231
tsepez4cf55152016-11-02 14:37:54 -0700232 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233}
234
Tom Sepezb1670b52017-02-16 17:01:00 -0800235bool Document::addAnnot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700236 const std::vector<CJS_Value>& params,
237 CJS_Value& vRet,
238 CFX_WideString& sError) {
239 // Not supported.
240 return true;
241}
242
Tom Sepezb1670b52017-02-16 17:01:00 -0800243bool Document::addField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700244 const std::vector<CJS_Value>& params,
245 CJS_Value& vRet,
246 CFX_WideString& sError) {
247 // Not supported.
248 return true;
249}
250
Tom Sepezb1670b52017-02-16 17:01:00 -0800251bool Document::exportAsText(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700252 const std::vector<CJS_Value>& params,
253 CJS_Value& vRet,
254 CFX_WideString& sError) {
255 // Unsafe, not supported.
256 return true;
257}
258
Tom Sepezb1670b52017-02-16 17:01:00 -0800259bool Document::exportAsFDF(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800260 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 CJS_Value& vRet,
262 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -0800263 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700264 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265}
266
Tom Sepezb1670b52017-02-16 17:01:00 -0800267bool Document::exportAsXFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700268 const std::vector<CJS_Value>& params,
269 CJS_Value& vRet,
270 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700272 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700274
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275// Maps a field object in PDF document to a JavaScript variable
276// comment:
277// note: the paremter cName, this is clue how to treat if the cName is not a
278// valiable filed name in this document
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279
Tom Sepezb1670b52017-02-16 17:01:00 -0800280bool Document::getField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700281 const std::vector<CJS_Value>& params,
282 CJS_Value& vRet,
283 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700285 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700286 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 }
dsinclair82e17672016-10-11 12:38:01 -0700288 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700289 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700290 return false;
tsepez55be02e2016-09-12 11:21:42 -0700291 }
tsepezb4694242016-08-15 16:44:55 -0700292 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700293 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
295 if (pPDFForm->CountFields(wideName) <= 0) {
tsepezf3dc8c62016-08-10 06:29:29 -0700296 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700297 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 }
299
tsepezf3dc8c62016-08-10 06:29:29 -0700300 v8::Local<v8::Object> pFieldObj =
tsepezb4694242016-08-15 16:44:55 -0700301 pRuntime->NewFxDynamicObj(CJS_Field::g_nObjDefnID);
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);
306
tsepezf3dc8c62016-08-10 06:29:29 -0700307 vRet = CJS_Value(pRuntime, pJSField);
tsepez4cf55152016-11-02 14:37:54 -0700308 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309}
310
311// Gets the name of the nth field in the document
Tom Sepezb1670b52017-02-16 17:01:00 -0800312bool Document::getNthFieldName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700313 const std::vector<CJS_Value>& params,
314 CJS_Value& vRet,
315 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700316 if (params.size() != 1) {
317 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700318 return false;
tsepezcd5dc852016-09-08 11:23:24 -0700319 }
dsinclair82e17672016-10-11 12:38:01 -0700320 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700321 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700322 return false;
tsepez55be02e2016-09-12 11:21:42 -0700323 }
tsepezb4694242016-08-15 16:44:55 -0700324 int nIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 if (nIndex < 0) {
tsepezcd5dc852016-09-08 11:23:24 -0700326 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700327 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700329 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700331 CPDF_FormField* pField = pPDFForm->GetField(nIndex, CFX_WideString());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332 if (!pField)
tsepez4cf55152016-11-02 14:37:54 -0700333 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334
tsepezf3dc8c62016-08-10 06:29:29 -0700335 vRet = CJS_Value(pRuntime, pField->GetFullName().c_str());
tsepez4cf55152016-11-02 14:37:54 -0700336 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337}
338
Tom Sepezb1670b52017-02-16 17:01:00 -0800339bool Document::importAnFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700340 const std::vector<CJS_Value>& params,
341 CJS_Value& vRet,
342 CFX_WideString& sError) {
343 // Unsafe, not supported.
344 return true;
345}
346
Tom Sepezb1670b52017-02-16 17:01:00 -0800347bool Document::importAnXFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700348 const std::vector<CJS_Value>& params,
349 CJS_Value& vRet,
350 CFX_WideString& sError) {
351 // Unsafe, not supported.
352 return true;
353}
354
Tom Sepezb1670b52017-02-16 17:01:00 -0800355bool Document::importTextData(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800356 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357 CJS_Value& vRet,
358 CFX_WideString& sError) {
359 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700360 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361}
362
363// exports the form data and mails the resulting fdf file as an attachment to
364// all recipients.
365// comment: need reader supports
Tom Sepezb1670b52017-02-16 17:01:00 -0800366bool Document::mailForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700367 const std::vector<CJS_Value>& params,
368 CJS_Value& vRet,
369 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700370 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700371 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700372 return false;
tsepez55be02e2016-09-12 11:21:42 -0700373 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700374 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -0700375 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700376 return false;
tsepez55be02e2016-09-12 11:21:42 -0700377 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 int iLength = params.size();
tsepez4cf55152016-11-02 14:37:54 -0700379 bool bUI = iLength > 0 ? params[0].ToBool(pRuntime) : true;
tsepezb4694242016-08-15 16:44:55 -0700380 CFX_WideString cTo = iLength > 1 ? params[1].ToCFXWideString(pRuntime) : L"";
381 CFX_WideString cCc = iLength > 2 ? params[2].ToCFXWideString(pRuntime) : L"";
382 CFX_WideString cBcc = iLength > 3 ? params[3].ToCFXWideString(pRuntime) : L"";
tsepezf3dc8c62016-08-10 06:29:29 -0700383 CFX_WideString cSubject =
tsepezb4694242016-08-15 16:44:55 -0700384 iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
385 CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
dsinclair7cbe68e2016-10-12 11:56:23 -0700386 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387 CFX_ByteTextBuf textBuf;
388 if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
tsepez4cf55152016-11-02 14:37:54 -0700389 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391 pRuntime->BeginBlock();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800392 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
dsinclair8779fa82016-10-12 12:05:44 -0700393 pFormFillEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI,
394 cTo.c_str(), cSubject.c_str(), cCc.c_str(),
395 cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700397 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398}
399
Tom Sepezb1670b52017-02-16 17:01:00 -0800400bool Document::print(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700401 const std::vector<CJS_Value>& params,
402 CJS_Value& vRet,
403 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700404 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700405 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700406 return false;
tsepez55be02e2016-09-12 11:21:42 -0700407 }
tsepez4cf55152016-11-02 14:37:54 -0700408 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 int nStart = 0;
410 int nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -0700411 bool bSilent = false;
412 bool bShrinkToFit = false;
413 bool bPrintAsImage = false;
414 bool bReverse = false;
415 bool bAnnotations = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416 int nlength = params.size();
417 if (nlength == 9) {
tsepez40faa792016-07-15 17:58:02 -0700418 if (params[8].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700419 v8::Local<v8::Object> pObj = params[8].ToV8Object(pRuntime);
420 if (CFXJS_Engine::GetObjDefnID(pObj) ==
421 CJS_PrintParamsObj::g_nObjDefnID) {
422 if (CJS_Object* pJSObj = params[8].ToCJSObject(pRuntime)) {
tsepez40faa792016-07-15 17:58:02 -0700423 if (PrintParamsObj* pprintparamsObj =
424 static_cast<PrintParamsObj*>(pJSObj->GetEmbedObject())) {
425 bUI = pprintparamsObj->bUI;
426 nStart = pprintparamsObj->nStart;
427 nEnd = pprintparamsObj->nEnd;
428 bSilent = pprintparamsObj->bSilent;
429 bShrinkToFit = pprintparamsObj->bShrinkToFit;
430 bPrintAsImage = pprintparamsObj->bPrintAsImage;
431 bReverse = pprintparamsObj->bReverse;
432 bAnnotations = pprintparamsObj->bAnnotations;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700434 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 }
436 }
437 } else {
438 if (nlength >= 1)
tsepezb4694242016-08-15 16:44:55 -0700439 bUI = params[0].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440 if (nlength >= 2)
tsepezb4694242016-08-15 16:44:55 -0700441 nStart = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 if (nlength >= 3)
tsepezb4694242016-08-15 16:44:55 -0700443 nEnd = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444 if (nlength >= 4)
tsepezb4694242016-08-15 16:44:55 -0700445 bSilent = params[3].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 if (nlength >= 5)
tsepezb4694242016-08-15 16:44:55 -0700447 bShrinkToFit = params[4].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 if (nlength >= 6)
tsepezb4694242016-08-15 16:44:55 -0700449 bPrintAsImage = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450 if (nlength >= 7)
tsepezb4694242016-08-15 16:44:55 -0700451 bReverse = params[6].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 if (nlength >= 8)
tsepezb4694242016-08-15 16:44:55 -0700453 bAnnotations = params[7].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 }
455
dsinclair82e17672016-10-11 12:38:01 -0700456 if (m_pFormFillEnv) {
457 m_pFormFillEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit,
458 bPrintAsImage, bReverse, bAnnotations);
tsepez4cf55152016-11-02 14:37:54 -0700459 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 }
tsepez4cf55152016-11-02 14:37:54 -0700461 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462}
463
464// removes the specified field from the document.
465// comment:
dsinclair1897bdc2016-09-06 14:18:57 -0700466// note: if the filed name is not rational, adobe is dumb for it.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467
Tom Sepezb1670b52017-02-16 17:01:00 -0800468bool Document::removeField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700469 const std::vector<CJS_Value>& params,
470 CJS_Value& vRet,
471 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700472 if (params.size() != 1) {
473 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700474 return false;
tsepez55be02e2016-09-12 11:21:42 -0700475 }
dsinclair82e17672016-10-11 12:38:01 -0700476 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700477 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700478 return false;
tsepez55be02e2016-09-12 11:21:42 -0700479 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700480 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
481 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM))) {
tsepezcd5dc852016-09-08 11:23:24 -0700482 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700483 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700484 }
tsepezb4694242016-08-15 16:44:55 -0700485 CFX_WideString sFieldName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700486 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
tsepez8fa82792017-01-11 09:32:33 -0800487 std::vector<CPDFSDK_Annot::ObservedPtr> widgets;
Lei Zhangd88a3642015-11-10 09:38:57 -0800488 pInterForm->GetWidgets(sFieldName, &widgets);
Lei Zhangd88a3642015-11-10 09:38:57 -0800489 if (widgets.empty())
tsepez4cf55152016-11-02 14:37:54 -0700490 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700491
tsepez8fa82792017-01-11 09:32:33 -0800492 for (const auto& pAnnot : widgets) {
493 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot.Get());
494 if (!pWidget)
495 continue;
496
Tom Sepez281a9ea2016-02-26 14:24:28 -0800497 CFX_FloatRect rcAnnot = pWidget->GetRect();
Lei Zhangd88a3642015-11-10 09:38:57 -0800498 --rcAnnot.left;
499 --rcAnnot.bottom;
500 ++rcAnnot.right;
501 ++rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502
tsepezdf964df2016-04-21 12:09:41 -0700503 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800504 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Lei Zhangd88a3642015-11-10 09:38:57 -0800505 ASSERT(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506
dsinclair1897bdc2016-09-06 14:18:57 -0700507 // If there is currently no pageview associated with the page being used
508 // do not create one. We may be in the process of tearing down the document
509 // and creating a new pageview at this point will cause bad things.
dsinclair7cbe68e2016-10-12 11:56:23 -0700510 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, false);
dsinclair1897bdc2016-09-06 14:18:57 -0700511 if (pPageView) {
dsinclair8afe15a2016-10-05 12:00:34 -0700512#if PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700513 pPageView->DeleteAnnot(pWidget);
dsinclair8afe15a2016-10-05 12:00:34 -0700514#endif // PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700515 pPageView->UpdateRects(aRefresh);
516 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700517 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700518 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700519
tsepez4cf55152016-11-02 14:37:54 -0700520 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521}
522
523// reset filed values within a document.
524// comment:
525// note: if the fields names r not rational, aodbe is dumb for it.
526
Tom Sepezb1670b52017-02-16 17:01:00 -0800527bool Document::resetForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700528 const std::vector<CJS_Value>& params,
529 CJS_Value& vRet,
530 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700531 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700532 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700533 return false;
tsepez55be02e2016-09-12 11:21:42 -0700534 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700535 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
536 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
537 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -0700538 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700539 return false;
tsepez55be02e2016-09-12 11:21:42 -0700540 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541
dsinclair7cbe68e2016-10-12 11:56:23 -0700542 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
tsepeze5aff742016-08-08 09:49:42 -0700544 CJS_Array aName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700545
Lei Zhangdd734de2015-11-11 10:16:52 -0800546 if (params.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700547 pPDFForm->ResetForm(true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700548 m_pFormFillEnv->SetChangeMark();
tsepez4cf55152016-11-02 14:37:54 -0700549 return true;
Lei Zhangd88a3642015-11-10 09:38:57 -0800550 }
551
552 switch (params[0].GetType()) {
553 default:
tsepezb4694242016-08-15 16:44:55 -0700554 aName.Attach(params[0].ToV8Array(pRuntime));
Lei Zhangd88a3642015-11-10 09:38:57 -0800555 break;
556 case CJS_Value::VT_string:
tsepezb4694242016-08-15 16:44:55 -0700557 aName.SetElement(pRuntime, 0, params[0]);
Lei Zhangd88a3642015-11-10 09:38:57 -0800558 break;
559 }
560
561 std::vector<CPDF_FormField*> aFields;
tsepezb4694242016-08-15 16:44:55 -0700562 for (int i = 0, isz = aName.GetLength(pRuntime); i < isz; ++i) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800563 CJS_Value valElement(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700564 aName.GetElement(pRuntime, i, valElement);
565 CFX_WideString swVal = valElement.ToCFXWideString(pRuntime);
Lei Zhangd88a3642015-11-10 09:38:57 -0800566 for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j)
567 aFields.push_back(pPDFForm->GetField(j, swVal));
568 }
569
570 if (!aFields.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700571 pPDFForm->ResetForm(aFields, true, true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700572 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700573 }
574
tsepez4cf55152016-11-02 14:37:54 -0700575 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576}
577
Tom Sepezb1670b52017-02-16 17:01:00 -0800578bool Document::saveAs(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700579 const std::vector<CJS_Value>& params,
580 CJS_Value& vRet,
581 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700583 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584}
585
Tom Sepezb1670b52017-02-16 17:01:00 -0800586bool Document::syncAnnotScan(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800587 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700588 CJS_Value& vRet,
589 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700590 return true;
591}
592
Tom Sepezb1670b52017-02-16 17:01:00 -0800593bool Document::submitForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700594 const std::vector<CJS_Value>& params,
595 CJS_Value& vRet,
596 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700597 int nSize = params.size();
598 if (nSize < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700599 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700600 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601 }
dsinclair82e17672016-10-11 12:38:01 -0700602 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700603 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700604 return false;
tsepez55be02e2016-09-12 11:21:42 -0700605 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800606
tsepeze5aff742016-08-08 09:49:42 -0700607 CJS_Array aFields;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700608 CFX_WideString strURL;
tsepez4cf55152016-11-02 14:37:54 -0700609 bool bFDF = true;
610 bool bEmpty = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611 CJS_Value v = params[0];
Tom Sepez39bfe122015-09-17 15:25:23 -0700612 if (v.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700613 strURL = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 if (nSize > 1)
tsepezb4694242016-08-15 16:44:55 -0700615 bFDF = params[1].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700616 if (nSize > 2)
tsepezb4694242016-08-15 16:44:55 -0700617 bEmpty = params[2].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700618 if (nSize > 3)
tsepezb4694242016-08-15 16:44:55 -0700619 aFields.Attach(params[3].ToV8Array(pRuntime));
Tom Sepez39bfe122015-09-17 15:25:23 -0700620 } else if (v.GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700621 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
622 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"cURL");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700623 if (!pValue.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700624 strURL = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700625
tsepezb4694242016-08-15 16:44:55 -0700626 pValue = pRuntime->GetObjectProperty(pObj, L"bFDF");
627 bFDF = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700628
tsepezb4694242016-08-15 16:44:55 -0700629 pValue = pRuntime->GetObjectProperty(pObj, L"bEmpty");
630 bEmpty = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700631
tsepezb4694242016-08-15 16:44:55 -0700632 pValue = pRuntime->GetObjectProperty(pObj, L"aFields");
633 aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634 }
635
dsinclair7cbe68e2016-10-12 11:56:23 -0700636 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700637 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
tsepezb4694242016-08-15 16:44:55 -0700638 if (aFields.GetLength(pRuntime) == 0 && bEmpty) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800639 if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640 pRuntime->BeginBlock();
tsepez4cf55152016-11-02 14:37:54 -0700641 pInterForm->SubmitForm(strURL, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700642 pRuntime->EndBlock();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700643 }
tsepez4cf55152016-11-02 14:37:54 -0700644 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700645 }
646
Lei Zhangd88a3642015-11-10 09:38:57 -0800647 std::vector<CPDF_FormField*> fieldObjects;
tsepezb4694242016-08-15 16:44:55 -0700648 for (int i = 0, sz = aFields.GetLength(pRuntime); i < sz; ++i) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700649 CJS_Value valName(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700650 aFields.GetElement(pRuntime, i, valName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700651
tsepezb4694242016-08-15 16:44:55 -0700652 CFX_WideString sName = valName.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700653 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
654 for (int j = 0, jsz = pPDFForm->CountFields(sName); j < jsz; ++j) {
655 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
656 if (!bEmpty && pField->GetValue().IsEmpty())
657 continue;
658
Lei Zhangd88a3642015-11-10 09:38:57 -0800659 fieldObjects.push_back(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700660 }
661 }
662
Lei Zhangd88a3642015-11-10 09:38:57 -0800663 if (pPDFInterForm->CheckRequiredFields(&fieldObjects, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700664 pRuntime->BeginBlock();
Wei Li97da9762016-03-11 17:00:48 -0800665 pInterForm->SubmitFields(strURL, fieldObjects, true, !bFDF);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700666 pRuntime->EndBlock();
667 }
tsepez4cf55152016-11-02 14:37:54 -0700668 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700669}
670
dsinclair82e17672016-10-11 12:38:01 -0700671void Document::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
672 m_pFormFillEnv.Reset(pFormFillEnv);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700673}
674
Tom Sepezb1670b52017-02-16 17:01:00 -0800675bool Document::bookmarkRoot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700676 CJS_PropValue& vp,
677 CFX_WideString& sError) {
678 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700679}
680
Tom Sepezb1670b52017-02-16 17:01:00 -0800681bool Document::mailDoc(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700682 const std::vector<CJS_Value>& params,
683 CJS_Value& vRet,
684 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700685 // TODO(tsepez): Check maximum number of allowed params.
tsepez4cf55152016-11-02 14:37:54 -0700686 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700687 CFX_WideString cTo = L"";
688 CFX_WideString cCc = L"";
689 CFX_WideString cBcc = L"";
690 CFX_WideString cSubject = L"";
691 CFX_WideString cMsg = L"";
692
tsepezf3dc8c62016-08-10 06:29:29 -0700693 if (params.size() >= 1)
tsepezb4694242016-08-15 16:44:55 -0700694 bUI = params[0].ToBool(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700695 if (params.size() >= 2)
tsepezb4694242016-08-15 16:44:55 -0700696 cTo = params[1].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700697 if (params.size() >= 3)
tsepezb4694242016-08-15 16:44:55 -0700698 cCc = params[2].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700699 if (params.size() >= 4)
tsepezb4694242016-08-15 16:44:55 -0700700 cBcc = params[3].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700701 if (params.size() >= 5)
tsepezb4694242016-08-15 16:44:55 -0700702 cSubject = params[4].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700703 if (params.size() >= 6)
tsepezb4694242016-08-15 16:44:55 -0700704 cMsg = params[5].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700705
Tom Sepez39bfe122015-09-17 15:25:23 -0700706 if (params.size() >= 1 && params[0].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700707 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708
tsepezb4694242016-08-15 16:44:55 -0700709 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"bUI");
tsepez71ba5882016-10-28 09:35:49 -0700710 bUI = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711
tsepezb4694242016-08-15 16:44:55 -0700712 pValue = pRuntime->GetObjectProperty(pObj, L"cTo");
713 cTo = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700714
tsepezb4694242016-08-15 16:44:55 -0700715 pValue = pRuntime->GetObjectProperty(pObj, L"cCc");
716 cCc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700717
tsepezb4694242016-08-15 16:44:55 -0700718 pValue = pRuntime->GetObjectProperty(pObj, L"cBcc");
719 cBcc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700720
tsepezb4694242016-08-15 16:44:55 -0700721 pValue = pRuntime->GetObjectProperty(pObj, L"cSubject");
722 cSubject = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700723
tsepezb4694242016-08-15 16:44:55 -0700724 pValue = pRuntime->GetObjectProperty(pObj, L"cMsg");
725 cMsg = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700726 }
727
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700728 pRuntime->BeginBlock();
dsinclair8779fa82016-10-12 12:05:44 -0700729 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
730 pFormFillEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
731 cCc.c_str(), cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700732 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700733 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700734}
735
Tom Sepezb1670b52017-02-16 17:01:00 -0800736bool Document::author(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700737 CJS_PropValue& vp,
738 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800739 return getPropertyInternal(pRuntime, vp, "Author", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700740}
741
Tom Sepezb1670b52017-02-16 17:01:00 -0800742bool Document::info(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700743 CJS_PropValue& vp,
744 CFX_WideString& sError) {
tonikitooec268762016-08-10 12:29:28 -0700745 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700746 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700747 return false;
tonikitooec268762016-08-10 12:29:28 -0700748 }
dsinclair82e17672016-10-11 12:38:01 -0700749 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700750 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700751 return false;
tsepez55be02e2016-09-12 11:21:42 -0700752 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700753 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700754 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700755 return false;
Tom Sepezc6ab1722015-02-05 15:27:25 -0800756
dsinclair38fd8442016-09-15 10:15:32 -0700757 CFX_WideString cwAuthor = pDictionary->GetUnicodeTextFor("Author");
758 CFX_WideString cwTitle = pDictionary->GetUnicodeTextFor("Title");
759 CFX_WideString cwSubject = pDictionary->GetUnicodeTextFor("Subject");
760 CFX_WideString cwKeywords = pDictionary->GetUnicodeTextFor("Keywords");
761 CFX_WideString cwCreator = pDictionary->GetUnicodeTextFor("Creator");
762 CFX_WideString cwProducer = pDictionary->GetUnicodeTextFor("Producer");
763 CFX_WideString cwCreationDate =
764 pDictionary->GetUnicodeTextFor("CreationDate");
765 CFX_WideString cwModDate = pDictionary->GetUnicodeTextFor("ModDate");
766 CFX_WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped");
Tom Sepezc6ab1722015-02-05 15:27:25 -0800767
tsepezb4694242016-08-15 16:44:55 -0700768 v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1);
tsepeze6cf0132017-01-18 14:38:18 -0800769 pRuntime->PutObjectProperty(pObj, L"Author", pRuntime->NewString(cwAuthor));
770 pRuntime->PutObjectProperty(pObj, L"Title", pRuntime->NewString(cwTitle));
771 pRuntime->PutObjectProperty(pObj, L"Subject", pRuntime->NewString(cwSubject));
772 pRuntime->PutObjectProperty(pObj, L"Keywords",
773 pRuntime->NewString(cwKeywords));
774 pRuntime->PutObjectProperty(pObj, L"Creator", pRuntime->NewString(cwCreator));
775 pRuntime->PutObjectProperty(pObj, L"Producer",
776 pRuntime->NewString(cwProducer));
777 pRuntime->PutObjectProperty(pObj, L"CreationDate",
778 pRuntime->NewString(cwCreationDate));
779 pRuntime->PutObjectProperty(pObj, L"ModDate", pRuntime->NewString(cwModDate));
780 pRuntime->PutObjectProperty(pObj, L"Trapped", pRuntime->NewString(cwTrapped));
tonikitooec268762016-08-10 12:29:28 -0700781
782 // It's to be compatible to non-standard info dictionary.
783 for (const auto& it : *pDictionary) {
784 const CFX_ByteString& bsKey = it.first;
tsepez0e606b52016-11-18 16:22:41 -0800785 CPDF_Object* pValueObj = it.second.get();
tonikitooec268762016-08-10 12:29:28 -0700786 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
787 if (pValueObj->IsString() || pValueObj->IsName()) {
tsepeze6cf0132017-01-18 14:38:18 -0800788 pRuntime->PutObjectProperty(
789 pObj, wsKey, pRuntime->NewString(pValueObj->GetUnicodeText()));
tonikitooec268762016-08-10 12:29:28 -0700790 } else if (pValueObj->IsNumber()) {
tsepeze6cf0132017-01-18 14:38:18 -0800791 pRuntime->PutObjectProperty(pObj, wsKey,
792 pRuntime->NewNumber(pValueObj->GetNumber()));
tonikitooec268762016-08-10 12:29:28 -0700793 } else if (pValueObj->IsBoolean()) {
tsepeze6cf0132017-01-18 14:38:18 -0800794 pRuntime->PutObjectProperty(
795 pObj, wsKey, pRuntime->NewBoolean(!!pValueObj->GetInteger()));
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700796 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700797 }
tonikitooec268762016-08-10 12:29:28 -0700798 vp << pObj;
tsepez4cf55152016-11-02 14:37:54 -0700799 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700800}
801
Tom Sepezb1670b52017-02-16 17:01:00 -0800802bool Document::getPropertyInternal(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700803 CJS_PropValue& vp,
804 const CFX_ByteString& propName,
805 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700806 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700807 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700808 return false;
tsepez55be02e2016-09-12 11:21:42 -0700809 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700810 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700811 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700812 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700813
814 if (vp.IsGetting()) {
dsinclair38fd8442016-09-15 10:15:32 -0700815 vp << pDictionary->GetUnicodeTextFor(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700817 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
tsepez55be02e2016-09-12 11:21:42 -0700818 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700819 return false;
tsepez55be02e2016-09-12 11:21:42 -0700820 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700821 CFX_WideString csProperty;
822 vp >> csProperty;
tsepez0e606b52016-11-18 16:22:41 -0800823 pDictionary->SetNewFor<CPDF_String>(propName, PDF_EncodeText(csProperty),
824 false);
dsinclair7cbe68e2016-10-12 11:56:23 -0700825 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700826 }
tsepez4cf55152016-11-02 14:37:54 -0700827 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700828}
829
Tom Sepezb1670b52017-02-16 17:01:00 -0800830bool Document::creationDate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700831 CJS_PropValue& vp,
832 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800833 return getPropertyInternal(pRuntime, vp, "CreationDate", sError);
tonikitooea3ff9e2016-08-02 11:52:28 -0700834}
835
Tom Sepezb1670b52017-02-16 17:01:00 -0800836bool Document::creator(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700837 CJS_PropValue& vp,
838 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800839 return getPropertyInternal(pRuntime, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700840}
841
Tom Sepezb1670b52017-02-16 17:01:00 -0800842bool Document::delay(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700843 CJS_PropValue& vp,
844 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700845 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700846 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700847 return false;
tsepez55be02e2016-09-12 11:21:42 -0700848 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700849 if (vp.IsGetting()) {
850 vp << m_bDelay;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800851 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700852 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800853 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
854 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
855 return false;
856 }
857 vp >> m_bDelay;
858 if (m_bDelay) {
859 m_DelayData.clear();
860 return true;
861 }
862 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
863 DelayDataToProcess.swap(m_DelayData);
864 for (const auto& pData : DelayDataToProcess)
865 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
866
tsepez4cf55152016-11-02 14:37:54 -0700867 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700868}
869
Tom Sepezb1670b52017-02-16 17:01:00 -0800870bool Document::keywords(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700871 CJS_PropValue& vp,
872 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800873 return getPropertyInternal(pRuntime, vp, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700874}
875
Tom Sepezb1670b52017-02-16 17:01:00 -0800876bool Document::modDate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700877 CJS_PropValue& vp,
878 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800879 return getPropertyInternal(pRuntime, vp, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700880}
881
Tom Sepezb1670b52017-02-16 17:01:00 -0800882bool Document::producer(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700883 CJS_PropValue& vp,
884 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800885 return getPropertyInternal(pRuntime, vp, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700886}
887
Tom Sepezb1670b52017-02-16 17:01:00 -0800888bool Document::subject(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700889 CJS_PropValue& vp,
890 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800891 return getPropertyInternal(pRuntime, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700892}
893
Tom Sepezb1670b52017-02-16 17:01:00 -0800894bool Document::title(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700895 CJS_PropValue& vp,
896 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700897 if (!m_pFormFillEnv || !m_pFormFillEnv->GetUnderlyingDocument()) {
tsepez55be02e2016-09-12 11:21:42 -0700898 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700899 return false;
tsepez55be02e2016-09-12 11:21:42 -0700900 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800901 return getPropertyInternal(pRuntime, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700902}
903
Tom Sepezb1670b52017-02-16 17:01:00 -0800904bool Document::numPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700905 CJS_PropValue& vp,
906 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700908 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700909 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700910 }
dsinclair82e17672016-10-11 12:38:01 -0700911 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700912 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700913 return false;
tsepez55be02e2016-09-12 11:21:42 -0700914 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700915 vp << m_pFormFillEnv->GetPageCount();
tsepez4cf55152016-11-02 14:37:54 -0700916 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917}
918
Tom Sepezb1670b52017-02-16 17:01:00 -0800919bool Document::external(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700920 CJS_PropValue& vp,
921 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700922 // In Chrome case, should always return true.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700923 if (vp.IsGetting()) {
924 vp << true;
925 }
tsepez4cf55152016-11-02 14:37:54 -0700926 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700927}
928
Tom Sepezb1670b52017-02-16 17:01:00 -0800929bool Document::filesize(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700930 CJS_PropValue& vp,
931 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700932 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700933 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700934 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700935 }
936 vp << 0;
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::mouseX(CJS_Runtime* pRuntime,
tonikitoo33c4cdb2016-08-08 10:52:51 -0700941 CJS_PropValue& vp,
942 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700943 return true;
944}
945
Tom Sepezb1670b52017-02-16 17:01:00 -0800946bool Document::mouseY(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700947 CJS_PropValue& vp,
948 CFX_WideString& sError) {
949 return true;
950}
951
Tom Sepezb1670b52017-02-16 17:01:00 -0800952bool Document::URL(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800953 CJS_PropValue& vp,
954 CFX_WideString& sError) {
tonikitoo33c4cdb2016-08-08 10:52:51 -0700955 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700956 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700957 return false;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700958 }
dsinclair82e17672016-10-11 12:38:01 -0700959 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700960 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700961 return false;
tsepez55be02e2016-09-12 11:21:42 -0700962 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700963 vp << m_pFormFillEnv->JS_docGetFilePath();
tsepez4cf55152016-11-02 14:37:54 -0700964 return true;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700965}
966
Tom Sepezb1670b52017-02-16 17:01:00 -0800967bool Document::baseURL(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700968 CJS_PropValue& vp,
969 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700970 if (vp.IsGetting()) {
971 vp << m_cwBaseURL;
972 } else {
973 vp >> m_cwBaseURL;
974 }
tsepez4cf55152016-11-02 14:37:54 -0700975 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700976}
977
Tom Sepezb1670b52017-02-16 17:01:00 -0800978bool Document::calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700979 CJS_PropValue& vp,
980 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700981 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700982 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700983 return false;
tsepez55be02e2016-09-12 11:21:42 -0700984 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700985 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -0700987 vp << !!pInterForm->IsCalculateEnabled();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800988 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700989 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800990 bool bCalculate;
991 vp >> bCalculate;
992 pInterForm->EnableCalculate(bCalculate);
tsepez4cf55152016-11-02 14:37:54 -0700993 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700994}
995
Tom Sepezb1670b52017-02-16 17:01:00 -0800996bool Document::documentFileName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700997 CJS_PropValue& vp,
998 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001000 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001001 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001002 }
dsinclair82e17672016-10-11 12:38:01 -07001003 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001004 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001005 return false;
tsepez55be02e2016-09-12 11:21:42 -07001006 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001007 CFX_WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008 int32_t i = wsFilePath.GetLength() - 1;
1009 for (; i >= 0; i--) {
1010 if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
1011 break;
1012 }
1013 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
1014 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
1015 } else {
1016 vp << L"";
1017 }
tsepez4cf55152016-11-02 14:37:54 -07001018 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019}
1020
Tom Sepezb1670b52017-02-16 17:01:00 -08001021bool Document::path(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001022 CJS_PropValue& vp,
1023 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001024 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001025 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001026 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001027 }
dsinclair82e17672016-10-11 12:38:01 -07001028 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001029 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001030 return false;
tsepez55be02e2016-09-12 11:21:42 -07001031 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001032 vp << app::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath());
tsepez4cf55152016-11-02 14:37:54 -07001033 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001034}
1035
Tom Sepezb1670b52017-02-16 17:01:00 -08001036bool Document::pageWindowRect(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001037 CJS_PropValue& vp,
1038 CFX_WideString& sError) {
1039 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040}
1041
Tom Sepezb1670b52017-02-16 17:01:00 -08001042bool Document::layout(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001043 CJS_PropValue& vp,
1044 CFX_WideString& sError) {
1045 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001046}
1047
Tom Sepezb1670b52017-02-16 17:01:00 -08001048bool Document::addLink(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001049 const std::vector<CJS_Value>& params,
1050 CJS_Value& vRet,
1051 CFX_WideString& sError) {
1052 return true;
1053}
1054
Tom Sepezb1670b52017-02-16 17:01:00 -08001055bool Document::closeDoc(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001056 const std::vector<CJS_Value>& params,
1057 CJS_Value& vRet,
1058 CFX_WideString& sError) {
1059 return true;
1060}
1061
Tom Sepezb1670b52017-02-16 17:01:00 -08001062bool Document::getPageBox(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001063 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001064 CJS_Value& vRet,
1065 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001066 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001067}
1068
Tom Sepezb1670b52017-02-16 17:01:00 -08001069bool Document::getAnnot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001070 const std::vector<CJS_Value>& params,
1071 CJS_Value& vRet,
1072 CFX_WideString& sError) {
tonikitoo618cb1f2016-08-18 20:10:17 -07001073 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001074 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001075 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001076 }
dsinclair82e17672016-10-11 12:38:01 -07001077 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001078 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001079 return false;
tsepez55be02e2016-09-12 11:21:42 -07001080 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001081 int nPageNo = params[0].ToInt(pRuntime);
1082 CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -07001083 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(nPageNo);
tonikitoo618cb1f2016-08-18 20:10:17 -07001084 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001085 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001086
tsepezd805eec2017-01-11 14:03:54 -08001087 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
tonikitoo618cb1f2016-08-18 20:10:17 -07001088 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
tsepezd805eec2017-01-11 14:03:54 -08001089 for (const auto& pSDKAnnotCur : annotIteration) {
1090 CPDFSDK_BAAnnot* pBAAnnot =
1091 static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get());
tonikitoo618cb1f2016-08-18 20:10:17 -07001092 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1093 pSDKBAAnnot = pBAAnnot;
1094 break;
1095 }
1096 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001097 if (!pSDKBAAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001098 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001099
1100 v8::Local<v8::Object> pObj =
1101 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1102 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001103 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001104
1105 CJS_Annot* pJS_Annot =
1106 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1107 if (!pJS_Annot)
tsepez4cf55152016-11-02 14:37:54 -07001108 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001109
1110 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1111 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001112 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001113
1114 pAnnot->SetSDKAnnot(pSDKBAAnnot);
tonikitoo618cb1f2016-08-18 20:10:17 -07001115 vRet = CJS_Value(pRuntime, pJS_Annot);
tsepez4cf55152016-11-02 14:37:54 -07001116 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001117}
1118
Tom Sepezb1670b52017-02-16 17:01:00 -08001119bool Document::getAnnots(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001120 const std::vector<CJS_Value>& params,
1121 CJS_Value& vRet,
1122 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001123 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001124 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001125 return false;
tsepez55be02e2016-09-12 11:21:42 -07001126 }
tonikitoo0ee35902016-08-26 14:41:29 -07001127 // TODO(tonikitoo): Add support supported parameters as per
1128 // the PDF spec.
1129
dsinclair7cbe68e2016-10-12 11:56:23 -07001130 int nPageNo = m_pFormFillEnv->GetPageCount();
tonikitoo0ee35902016-08-26 14:41:29 -07001131 CJS_Array annots;
1132
1133 for (int i = 0; i < nPageNo; ++i) {
dsinclair7cbe68e2016-10-12 11:56:23 -07001134 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(i);
tonikitoo0ee35902016-08-26 14:41:29 -07001135 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001136 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001137
tsepezd805eec2017-01-11 14:03:54 -08001138 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
1139 for (const auto& pSDKAnnotCur : annotIteration) {
1140 if (!pSDKAnnotCur) {
1141 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001142 return false;
tsepezd805eec2017-01-11 14:03:54 -08001143 }
tonikitoo0ee35902016-08-26 14:41:29 -07001144 v8::Local<v8::Object> pObj =
1145 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1146 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001147 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001148
1149 CJS_Annot* pJS_Annot =
1150 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1151 if (!pJS_Annot)
tsepez4cf55152016-11-02 14:37:54 -07001152 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001153
1154 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1155 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001156 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001157
tsepezd805eec2017-01-11 14:03:54 -08001158 pAnnot->SetSDKAnnot(static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get()));
tonikitoo0ee35902016-08-26 14:41:29 -07001159 annots.SetElement(pRuntime, i, CJS_Value(pRuntime, pJS_Annot));
1160 }
1161 }
tonikitoo0ee35902016-08-26 14:41:29 -07001162 vRet = CJS_Value(pRuntime, annots);
tsepez4cf55152016-11-02 14:37:54 -07001163 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001164}
1165
Tom Sepezb1670b52017-02-16 17:01:00 -08001166bool Document::getAnnot3D(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001167 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001168 CJS_Value& vRet,
1169 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001170 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001171 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001172}
1173
Tom Sepezb1670b52017-02-16 17:01:00 -08001174bool Document::getAnnots3D(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001175 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001176 CJS_Value& vRet,
1177 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001178 return true;
1179}
1180
Tom Sepezb1670b52017-02-16 17:01:00 -08001181bool Document::getOCGs(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;
1186}
1187
Tom Sepezb1670b52017-02-16 17:01:00 -08001188bool Document::getLinks(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001189 const std::vector<CJS_Value>& params,
1190 CJS_Value& vRet,
1191 CFX_WideString& sError) {
1192 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193}
1194
1195bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1196 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1197 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1198}
1199
Tom Sepezb1670b52017-02-16 17:01:00 -08001200bool Document::addIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001201 const std::vector<CJS_Value>& params,
1202 CJS_Value& vRet,
1203 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001205 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001206 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001207 }
tsepezf3dc8c62016-08-10 06:29:29 -07001208
tsepezb4694242016-08-15 16:44:55 -07001209 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Tom Sepez39bfe122015-09-17 15:25:23 -07001210 if (params[1].GetType() != CJS_Value::VT_object) {
tsepezcd5dc852016-09-08 11:23:24 -07001211 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001212 return false;
Tom Sepezaecd9d02015-08-31 15:04:12 -07001213 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001214
tsepezb4694242016-08-15 16:44:55 -07001215 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pRuntime);
1216 if (pRuntime->GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
tsepezcd5dc852016-09-08 11:23:24 -07001217 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001218 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001219 }
1220
tsepezb4694242016-08-15 16:44:55 -07001221 CJS_EmbedObj* pEmbedObj = params[1].ToCJSObject(pRuntime)->GetEmbedObject();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001222 if (!pEmbedObj) {
tsepezcd5dc852016-09-08 11:23:24 -07001223 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001224 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001225 }
1226
thestigdadea5f2016-10-19 13:29:48 -07001227 m_Icons.push_back(pdfium::MakeUnique<IconElement>(
1228 swIconName, static_cast<Icon*>(pEmbedObj)));
tsepez4cf55152016-11-02 14:37:54 -07001229 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001230}
1231
Tom Sepezb1670b52017-02-16 17:01:00 -08001232bool Document::icons(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001233 CJS_PropValue& vp,
1234 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001235 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001236 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001237 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001238 }
thestigdadea5f2016-10-19 13:29:48 -07001239 if (m_Icons.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001240 vp.GetJSValue()->SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001241 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001242 }
1243
tsepeze5aff742016-08-08 09:49:42 -07001244 CJS_Array Icons;
Tom Sepez6ba32d92016-01-05 16:26:32 -08001245 int i = 0;
thestigdadea5f2016-10-19 13:29:48 -07001246 for (const auto& pIconElement : m_Icons) {
tsepezb4694242016-08-15 16:44:55 -07001247 v8::Local<v8::Object> pObj =
1248 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001249 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001250 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001251
tsepezb4694242016-08-15 16:44:55 -07001252 CJS_Icon* pJS_Icon =
1253 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001254 if (!pJS_Icon)
tsepez4cf55152016-11-02 14:37:54 -07001255 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001256
thestigdadea5f2016-10-19 13:29:48 -07001257 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258 if (!pIcon)
tsepez4cf55152016-11-02 14:37:54 -07001259 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001260
dsinclair09bad1c2016-10-18 19:26:30 -07001261 pIcon->SetStream(pIconElement->IconStream->GetStream());
1262 pIcon->SetIconName(pIconElement->IconName);
tsepezb4694242016-08-15 16:44:55 -07001263 Icons.SetElement(pRuntime, i++, CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001264 }
1265
1266 vp << Icons;
tsepez4cf55152016-11-02 14:37:54 -07001267 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001268}
1269
Tom Sepezb1670b52017-02-16 17:01:00 -08001270bool Document::getIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001271 const std::vector<CJS_Value>& params,
1272 CJS_Value& vRet,
1273 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001274 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001275 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001276 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277 }
1278
thestigdadea5f2016-10-19 13:29:48 -07001279 if (m_Icons.empty())
tsepez4cf55152016-11-02 14:37:54 -07001280 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001281
tsepezb4694242016-08-15 16:44:55 -07001282 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
thestigdadea5f2016-10-19 13:29:48 -07001283 for (const auto& pIconElement : m_Icons) {
1284 if (pIconElement->IconName != swIconName)
1285 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001286
thestigdadea5f2016-10-19 13:29:48 -07001287 v8::Local<v8::Object> pObj =
1288 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
1289 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001290 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001291
thestigdadea5f2016-10-19 13:29:48 -07001292 CJS_Icon* pJS_Icon =
1293 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
1294 if (!pJS_Icon)
tsepez4cf55152016-11-02 14:37:54 -07001295 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001296
thestigdadea5f2016-10-19 13:29:48 -07001297 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1298 if (!pIcon)
tsepez4cf55152016-11-02 14:37:54 -07001299 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001300
thestigdadea5f2016-10-19 13:29:48 -07001301 pIcon->SetIconName(swIconName);
1302 pIcon->SetStream(pIconElement->IconStream->GetStream());
1303
1304 vRet = CJS_Value(pRuntime, pJS_Icon);
tsepez4cf55152016-11-02 14:37:54 -07001305 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001306 }
1307
tsepez4cf55152016-11-02 14:37:54 -07001308 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001309}
1310
Tom Sepezb1670b52017-02-16 17:01:00 -08001311bool Document::removeIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001312 const std::vector<CJS_Value>& params,
1313 CJS_Value& vRet,
1314 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001315 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001316 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001317}
1318
Tom Sepezb1670b52017-02-16 17:01:00 -08001319bool Document::createDataObject(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001320 const std::vector<CJS_Value>& params,
1321 CJS_Value& vRet,
1322 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001323 // Unsafe, not implemented.
tsepez4cf55152016-11-02 14:37:54 -07001324 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001325}
1326
Tom Sepezb1670b52017-02-16 17:01:00 -08001327bool Document::media(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001328 CJS_PropValue& vp,
1329 CFX_WideString& sError) {
1330 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001331}
1332
Tom Sepezb1670b52017-02-16 17:01:00 -08001333bool Document::calculateNow(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001334 const std::vector<CJS_Value>& params,
1335 CJS_Value& vRet,
1336 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001337 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001338 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001339 return false;
tsepez55be02e2016-09-12 11:21:42 -07001340 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001341 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
1342 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
1343 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -07001344 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001345 return false;
tsepez55be02e2016-09-12 11:21:42 -07001346 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001347 m_pFormFillEnv->GetInterForm()->OnCalculate();
tsepez4cf55152016-11-02 14:37:54 -07001348 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001349}
1350
Tom Sepezb1670b52017-02-16 17:01:00 -08001351bool Document::Collab(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001352 CJS_PropValue& vp,
1353 CFX_WideString& sError) {
1354 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001355}
1356
Tom Sepezb1670b52017-02-16 17:01:00 -08001357bool Document::getPageNthWord(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001358 const std::vector<CJS_Value>& params,
1359 CJS_Value& vRet,
1360 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001361 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001362 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001363 return false;
tsepez55be02e2016-09-12 11:21:42 -07001364 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001365 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001366 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001367 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001368 }
tsepez55be02e2016-09-12 11:21:42 -07001369
1370 // TODO(tsepez): check maximum allowable params.
1371
tsepezb4694242016-08-15 16:44:55 -07001372 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
1373 int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
1374 bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001375
dsinclair7cbe68e2016-10-12 11:56:23 -07001376 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001377 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001378 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001379
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001380 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001381 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001382 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001383 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001384
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001385 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1386 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001387 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001388
thestig5cc24652016-04-26 11:46:02 -07001389 CPDF_Page page(pDocument, pPageDict, true);
1390 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001391
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001392 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001393 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001394 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001395 if (pPageObj->IsText()) {
1396 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001397 int nObjWords = CountWords(pTextObj);
1398 if (nWords + nObjWords >= nWordNo) {
1399 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1400 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001401 }
Tom Sepez2398d892016-02-17 16:46:26 -08001402 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001403 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001404 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001405
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001406 if (bStrip) {
1407 swRet.TrimLeft();
1408 swRet.TrimRight();
1409 }
1410
tsepezf3dc8c62016-08-10 06:29:29 -07001411 vRet = CJS_Value(pRuntime, swRet.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001412 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001413}
1414
Tom Sepezb1670b52017-02-16 17:01:00 -08001415bool Document::getPageNthWordQuads(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001416 const std::vector<CJS_Value>& params,
1417 CJS_Value& vRet,
1418 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001419 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001420 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001421 return false;
tsepez55be02e2016-09-12 11:21:42 -07001422 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001423 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -07001424 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001425 return false;
tsepez55be02e2016-09-12 11:21:42 -07001426 }
tsepez4cf55152016-11-02 14:37:54 -07001427 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001428}
1429
Tom Sepezb1670b52017-02-16 17:01:00 -08001430bool Document::getPageNumWords(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001431 const std::vector<CJS_Value>& params,
1432 CJS_Value& vRet,
1433 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001434 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001435 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001436 return false;
tsepez55be02e2016-09-12 11:21:42 -07001437 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001438 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001439 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001440 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001441 }
tsepezb4694242016-08-15 16:44:55 -07001442 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
dsinclair7cbe68e2016-10-12 11:56:23 -07001443 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001444 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001445 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001446 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001447 }
1448
1449 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1450 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001451 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001452
thestig5cc24652016-04-26 11:46:02 -07001453 CPDF_Page page(pDocument, pPageDict, true);
1454 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001455
1456 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001457 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001458 if (pPageObj->IsText())
1459 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001460 }
1461
tsepezf3dc8c62016-08-10 06:29:29 -07001462 vRet = CJS_Value(pRuntime, nWords);
tsepez4cf55152016-11-02 14:37:54 -07001463 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001464}
1465
Tom Sepezb1670b52017-02-16 17:01:00 -08001466bool Document::getPrintParams(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001467 const std::vector<CJS_Value>& params,
1468 CJS_Value& vRet,
1469 CFX_WideString& sError) {
tsepezb4694242016-08-15 16:44:55 -07001470 v8::Local<v8::Object> pRetObj =
1471 pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001472
1473 // Not implemented yet.
1474
tsepezf3dc8c62016-08-10 06:29:29 -07001475 vRet = CJS_Value(pRuntime, pRetObj);
tsepez4cf55152016-11-02 14:37:54 -07001476 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001477}
1478
1479#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1480
1481int Document::CountWords(CPDF_TextObject* pTextObj) {
1482 if (!pTextObj)
1483 return 0;
1484
1485 int nWords = 0;
1486
1487 CPDF_Font* pFont = pTextObj->GetFont();
1488 if (!pFont)
1489 return 0;
1490
tsepez4cf55152016-11-02 14:37:54 -07001491 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001492
1493 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001494 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001495 FX_FLOAT kerning;
1496
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001497 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001498 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1499
Tom Sepez62a70f92016-03-21 15:00:20 -07001500 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001501 if (swUnicode.GetLength() > 0)
1502 unicode = swUnicode[0];
1503
1504 if (ISLATINWORD(unicode) && bIsLatin)
1505 continue;
1506
1507 bIsLatin = ISLATINWORD(unicode);
1508 if (unicode != 0x20)
1509 nWords++;
1510 }
1511
1512 return nWords;
1513}
1514
1515CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1516 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001517 CFX_WideString swRet;
1518
1519 CPDF_Font* pFont = pTextObj->GetFont();
1520 if (!pFont)
1521 return L"";
1522
1523 int nWords = 0;
tsepez4cf55152016-11-02 14:37:54 -07001524 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001525
1526 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001527 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001528 FX_FLOAT kerning;
1529
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001530 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001531 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1532
Tom Sepez62a70f92016-03-21 15:00:20 -07001533 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001534 if (swUnicode.GetLength() > 0)
1535 unicode = swUnicode[0];
1536
1537 if (ISLATINWORD(unicode) && bIsLatin) {
1538 } else {
1539 bIsLatin = ISLATINWORD(unicode);
1540 if (unicode != 0x20)
1541 nWords++;
1542 }
1543
1544 if (nWords - 1 == nWordIndex)
1545 swRet += unicode;
1546 }
1547
1548 return swRet;
1549}
1550
Tom Sepezb1670b52017-02-16 17:01:00 -08001551bool Document::zoom(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001552 CJS_PropValue& vp,
1553 CFX_WideString& sError) {
1554 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001555}
1556
1557/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001558(none, NoVary)
1559(fitP, FitPage)
1560(fitW, FitWidth)
1561(fitH, FitHeight)
1562(fitV, FitVisibleWidth)
1563(pref, Preferred)
1564(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001565*/
1566
Tom Sepezb1670b52017-02-16 17:01:00 -08001567bool Document::zoomType(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001568 CJS_PropValue& vp,
1569 CFX_WideString& sError) {
1570 return true;
1571}
1572
Tom Sepezb1670b52017-02-16 17:01:00 -08001573bool Document::deletePages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001574 const std::vector<CJS_Value>& params,
1575 CJS_Value& vRet,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001576 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001577 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001578 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001579}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001580
Tom Sepezb1670b52017-02-16 17:01:00 -08001581bool Document::extractPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001582 const std::vector<CJS_Value>& params,
1583 CJS_Value& vRet,
1584 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001585 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001586 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001587}
1588
Tom Sepezb1670b52017-02-16 17:01:00 -08001589bool Document::insertPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001590 const std::vector<CJS_Value>& params,
1591 CJS_Value& vRet,
1592 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001593 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001594 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001595}
1596
Tom Sepezb1670b52017-02-16 17:01:00 -08001597bool Document::replacePages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001598 const std::vector<CJS_Value>& params,
1599 CJS_Value& vRet,
1600 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001601 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001602 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001603}
1604
Tom Sepezb1670b52017-02-16 17:01:00 -08001605bool Document::getURL(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001606 const std::vector<CJS_Value>& params,
1607 CJS_Value& vRet,
1608 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001609 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001610 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001611}
1612
Tom Sepezb1670b52017-02-16 17:01:00 -08001613bool Document::gotoNamedDest(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001614 const std::vector<CJS_Value>& params,
1615 CJS_Value& vRet,
1616 CFX_WideString& sError) {
tonikitoo1c836752016-08-08 16:14:05 -07001617 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001618 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001619 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001620 }
dsinclair82e17672016-10-11 12:38:01 -07001621 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001622 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001623 return false;
tsepez55be02e2016-09-12 11:21:42 -07001624 }
tsepezb4694242016-08-15 16:44:55 -07001625 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -07001626 CFX_ByteString utf8Name = wideName.UTF8Encode();
dsinclair7cbe68e2016-10-12 11:56:23 -07001627 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
tonikitoo1c836752016-08-08 16:14:05 -07001628 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001629 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001630
tonikitoo1c836752016-08-08 16:14:05 -07001631 CPDF_NameTree nameTree(pDocument, "Dests");
1632 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, utf8Name);
1633 if (!destArray)
tsepez4cf55152016-11-02 14:37:54 -07001634 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001635
1636 CPDF_Dest dest(destArray);
1637 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
1638
1639 std::unique_ptr<float[]> scrollPositionArray;
1640 int scrollPositionArraySize = 0;
1641
1642 if (arrayObject) {
1643 scrollPositionArray.reset(new float[arrayObject->GetCount()]);
1644 int j = 0;
1645 for (size_t i = 2; i < arrayObject->GetCount(); i++)
1646 scrollPositionArray[j++] = arrayObject->GetFloatAt(i);
1647 scrollPositionArraySize = j;
1648 }
1649
tsepezb4694242016-08-15 16:44:55 -07001650 pRuntime->BeginBlock();
dsinclair82e17672016-10-11 12:38:01 -07001651 m_pFormFillEnv->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
1652 scrollPositionArray.get(),
1653 scrollPositionArraySize);
tsepezb4694242016-08-15 16:44:55 -07001654 pRuntime->EndBlock();
tonikitoo1c836752016-08-08 16:14:05 -07001655
tsepez4cf55152016-11-02 14:37:54 -07001656 return true;
tonikitoo1c836752016-08-08 16:14:05 -07001657}
1658
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001659void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001660 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001661}
1662
1663void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1664 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001665 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1666 auto iter = m_DelayData.begin();
1667 while (iter != m_DelayData.end()) {
1668 auto old = iter++;
1669 if ((*old)->sFieldName == sFieldName &&
1670 (*old)->nControlIndex == nControlIndex) {
1671 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1672 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001673 }
1674 }
1675
dsinclair82e17672016-10-11 12:38:01 -07001676 for (const auto& pData : DelayDataForFieldAndControlIndex)
1677 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001678}
1679
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001680CJS_Document* Document::GetCJSDoc() const {
1681 return static_cast<CJS_Document*>(m_pJSObject);
JUN FANG33f6f0d2015-04-06 12:39:51 -07001682}