blob: 133e1b3fd90c5a7b676ded460851243e75668083 [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
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039BEGIN_JS_STATIC_CONST(CJS_PrintParamsObj)
40END_JS_STATIC_CONST()
41
42BEGIN_JS_STATIC_PROP(CJS_PrintParamsObj)
43END_JS_STATIC_PROP()
44
45BEGIN_JS_STATIC_METHOD(CJS_PrintParamsObj)
46END_JS_STATIC_METHOD()
47
48IMPLEMENT_JS_CLASS(CJS_PrintParamsObj, PrintParamsObj)
49
50PrintParamsObj::PrintParamsObj(CJS_Object* pJSObject)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 : CJS_EmbedObj(pJSObject) {
tsepez4cf55152016-11-02 14:37:54 -070052 bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 nStart = 0;
54 nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -070055 bSilent = false;
56 bShrinkToFit = false;
57 bPrintAsImage = false;
58 bReverse = false;
59 bAnnotations = true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060}
61
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062#define MINWIDTH 5.0f
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063#define MINHEIGHT 5.0f
64
65BEGIN_JS_STATIC_CONST(CJS_Document)
66END_JS_STATIC_CONST()
67
68BEGIN_JS_STATIC_PROP(CJS_Document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069JS_STATIC_PROP_ENTRY(ADBE)
70JS_STATIC_PROP_ENTRY(author)
71JS_STATIC_PROP_ENTRY(baseURL)
72JS_STATIC_PROP_ENTRY(bookmarkRoot)
73JS_STATIC_PROP_ENTRY(calculate)
74JS_STATIC_PROP_ENTRY(Collab)
75JS_STATIC_PROP_ENTRY(creationDate)
76JS_STATIC_PROP_ENTRY(creator)
77JS_STATIC_PROP_ENTRY(delay)
78JS_STATIC_PROP_ENTRY(dirty)
79JS_STATIC_PROP_ENTRY(documentFileName)
80JS_STATIC_PROP_ENTRY(external)
81JS_STATIC_PROP_ENTRY(filesize)
82JS_STATIC_PROP_ENTRY(icons)
83JS_STATIC_PROP_ENTRY(info)
84JS_STATIC_PROP_ENTRY(keywords)
85JS_STATIC_PROP_ENTRY(layout)
86JS_STATIC_PROP_ENTRY(media)
87JS_STATIC_PROP_ENTRY(modDate)
88JS_STATIC_PROP_ENTRY(mouseX)
89JS_STATIC_PROP_ENTRY(mouseY)
90JS_STATIC_PROP_ENTRY(numFields)
91JS_STATIC_PROP_ENTRY(numPages)
92JS_STATIC_PROP_ENTRY(pageNum)
93JS_STATIC_PROP_ENTRY(pageWindowRect)
94JS_STATIC_PROP_ENTRY(path)
95JS_STATIC_PROP_ENTRY(producer)
96JS_STATIC_PROP_ENTRY(subject)
97JS_STATIC_PROP_ENTRY(title)
tonikitoo33c4cdb2016-08-08 10:52:51 -070098JS_STATIC_PROP_ENTRY(URL)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099JS_STATIC_PROP_ENTRY(zoom)
100JS_STATIC_PROP_ENTRY(zoomType)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101END_JS_STATIC_PROP()
102
103BEGIN_JS_STATIC_METHOD(CJS_Document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104JS_STATIC_METHOD_ENTRY(addAnnot)
105JS_STATIC_METHOD_ENTRY(addField)
106JS_STATIC_METHOD_ENTRY(addLink)
107JS_STATIC_METHOD_ENTRY(addIcon)
108JS_STATIC_METHOD_ENTRY(calculateNow)
109JS_STATIC_METHOD_ENTRY(closeDoc)
110JS_STATIC_METHOD_ENTRY(createDataObject)
111JS_STATIC_METHOD_ENTRY(deletePages)
112JS_STATIC_METHOD_ENTRY(exportAsText)
113JS_STATIC_METHOD_ENTRY(exportAsFDF)
114JS_STATIC_METHOD_ENTRY(exportAsXFDF)
115JS_STATIC_METHOD_ENTRY(extractPages)
116JS_STATIC_METHOD_ENTRY(getAnnot)
117JS_STATIC_METHOD_ENTRY(getAnnots)
118JS_STATIC_METHOD_ENTRY(getAnnot3D)
119JS_STATIC_METHOD_ENTRY(getAnnots3D)
120JS_STATIC_METHOD_ENTRY(getField)
121JS_STATIC_METHOD_ENTRY(getIcon)
122JS_STATIC_METHOD_ENTRY(getLinks)
123JS_STATIC_METHOD_ENTRY(getNthFieldName)
124JS_STATIC_METHOD_ENTRY(getOCGs)
125JS_STATIC_METHOD_ENTRY(getPageBox)
126JS_STATIC_METHOD_ENTRY(getPageNthWord)
127JS_STATIC_METHOD_ENTRY(getPageNthWordQuads)
128JS_STATIC_METHOD_ENTRY(getPageNumWords)
129JS_STATIC_METHOD_ENTRY(getPrintParams)
130JS_STATIC_METHOD_ENTRY(getURL)
tonikitoo1c836752016-08-08 16:14:05 -0700131JS_STATIC_METHOD_ENTRY(gotoNamedDest)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132JS_STATIC_METHOD_ENTRY(importAnFDF)
133JS_STATIC_METHOD_ENTRY(importAnXFDF)
134JS_STATIC_METHOD_ENTRY(importTextData)
135JS_STATIC_METHOD_ENTRY(insertPages)
136JS_STATIC_METHOD_ENTRY(mailForm)
137JS_STATIC_METHOD_ENTRY(print)
138JS_STATIC_METHOD_ENTRY(removeField)
139JS_STATIC_METHOD_ENTRY(replacePages)
140JS_STATIC_METHOD_ENTRY(resetForm)
141JS_STATIC_METHOD_ENTRY(removeIcon)
142JS_STATIC_METHOD_ENTRY(saveAs)
143JS_STATIC_METHOD_ENTRY(submitForm)
tonikitoobb5fa042016-08-19 11:18:29 -0700144JS_STATIC_METHOD_ENTRY(syncAnnotScan)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145JS_STATIC_METHOD_ENTRY(mailDoc)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146END_JS_STATIC_METHOD()
147
148IMPLEMENT_JS_CLASS(CJS_Document, Document)
149
Tom Sepez33420902015-10-13 15:00:10 -0700150void CJS_Document::InitInstance(IJS_Runtime* pIRuntime) {
151 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime);
152 Document* pDoc = static_cast<Document*>(GetEmbedObject());
dsinclair82e17672016-10-11 12:38:01 -0700153 pDoc->SetFormFillEnv(pRuntime->GetFormFillEnv());
Tom Sepez33420902015-10-13 15:00:10 -0700154}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700155
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156Document::Document(CJS_Object* pJSObject)
157 : CJS_EmbedObj(pJSObject),
dsinclair82e17672016-10-11 12:38:01 -0700158 m_pFormFillEnv(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 m_cwBaseURL(L""),
tsepez4cf55152016-11-02 14:37:54 -0700160 m_bDelay(false) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162Document::~Document() {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700163}
164
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165// the total number of fileds in document.
Tom Sepezb1670b52017-02-16 17:01:00 -0800166bool Document::numFields(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700167 CJS_PropValue& vp,
168 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700170 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700171 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 }
dsinclair82e17672016-10-11 12:38:01 -0700173 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700174 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700175 return false;
tsepez55be02e2016-09-12 11:21:42 -0700176 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700177 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700179 vp << static_cast<int>(pPDFForm->CountFields(CFX_WideString()));
tsepez4cf55152016-11-02 14:37:54 -0700180 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700181}
182
Tom Sepezb1670b52017-02-16 17:01:00 -0800183bool Document::dirty(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700184 CJS_PropValue& vp,
185 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700186 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700187 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700188 return false;
tsepez55be02e2016-09-12 11:21:42 -0700189 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 if (vp.IsGetting()) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700191 vp << !!m_pFormFillEnv->GetChangeMark();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800192 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800194 bool bChanged = false;
195 vp >> bChanged;
196 if (bChanged)
197 m_pFormFillEnv->SetChangeMark();
198 else
199 m_pFormFillEnv->ClearChangeMark();
200
tsepez4cf55152016-11-02 14:37:54 -0700201 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700202}
203
Tom Sepezb1670b52017-02-16 17:01:00 -0800204bool Document::ADBE(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700205 CJS_PropValue& vp,
206 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700207 if (vp.IsGetting())
Tom Sepezb1670b52017-02-16 17:01:00 -0800208 vp.GetJSValue()->SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209
tsepez4cf55152016-11-02 14:37:54 -0700210 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211}
212
Tom Sepezb1670b52017-02-16 17:01:00 -0800213bool Document::pageNum(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700214 CJS_PropValue& vp,
215 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700216 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700217 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700218 return false;
tsepez55be02e2016-09-12 11:21:42 -0700219 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 if (vp.IsGetting()) {
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800221 if (CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetCurrentView())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 vp << pPageView->GetPageIndex();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800223 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800225 int iPageCount = m_pFormFillEnv->GetPageCount();
226 int iPageNum = 0;
227 vp >> iPageNum;
228 if (iPageNum >= 0 && iPageNum < iPageCount)
229 m_pFormFillEnv->JS_docgotoPage(iPageNum);
230 else if (iPageNum >= iPageCount)
231 m_pFormFillEnv->JS_docgotoPage(iPageCount - 1);
232 else if (iPageNum < 0)
233 m_pFormFillEnv->JS_docgotoPage(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234
tsepez4cf55152016-11-02 14:37:54 -0700235 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236}
237
Tom Sepezb1670b52017-02-16 17:01:00 -0800238bool Document::addAnnot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700239 const std::vector<CJS_Value>& params,
240 CJS_Value& vRet,
241 CFX_WideString& sError) {
242 // Not supported.
243 return true;
244}
245
Tom Sepezb1670b52017-02-16 17:01:00 -0800246bool Document::addField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700247 const std::vector<CJS_Value>& params,
248 CJS_Value& vRet,
249 CFX_WideString& sError) {
250 // Not supported.
251 return true;
252}
253
Tom Sepezb1670b52017-02-16 17:01:00 -0800254bool Document::exportAsText(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700255 const std::vector<CJS_Value>& params,
256 CJS_Value& vRet,
257 CFX_WideString& sError) {
258 // Unsafe, not supported.
259 return true;
260}
261
Tom Sepezb1670b52017-02-16 17:01:00 -0800262bool Document::exportAsFDF(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800263 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 CJS_Value& vRet,
265 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -0800266 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700267 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700268}
269
Tom Sepezb1670b52017-02-16 17:01:00 -0800270bool Document::exportAsXFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700271 const std::vector<CJS_Value>& params,
272 CJS_Value& vRet,
273 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700275 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700277
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278// Maps a field object in PDF document to a JavaScript variable
279// comment:
280// note: the paremter cName, this is clue how to treat if the cName is not a
281// valiable filed name in this document
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282
Tom Sepezb1670b52017-02-16 17:01:00 -0800283bool Document::getField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700284 const std::vector<CJS_Value>& params,
285 CJS_Value& vRet,
286 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700288 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700289 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 }
dsinclair82e17672016-10-11 12:38:01 -0700291 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700292 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700293 return false;
tsepez55be02e2016-09-12 11:21:42 -0700294 }
tsepezb4694242016-08-15 16:44:55 -0700295 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700296 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
298 if (pPDFForm->CountFields(wideName) <= 0) {
tsepezf3dc8c62016-08-10 06:29:29 -0700299 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700300 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 }
302
tsepezf3dc8c62016-08-10 06:29:29 -0700303 v8::Local<v8::Object> pFieldObj =
tsepezb4694242016-08-15 16:44:55 -0700304 pRuntime->NewFxDynamicObj(CJS_Field::g_nObjDefnID);
tsepezf3dc8c62016-08-10 06:29:29 -0700305 CJS_Field* pJSField =
tsepezb4694242016-08-15 16:44:55 -0700306 static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
tsepezf3dc8c62016-08-10 06:29:29 -0700307 Field* pField = static_cast<Field*>(pJSField->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 pField->AttachField(this, wideName);
309
tsepezf3dc8c62016-08-10 06:29:29 -0700310 vRet = CJS_Value(pRuntime, pJSField);
tsepez4cf55152016-11-02 14:37:54 -0700311 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312}
313
314// Gets the name of the nth field in the document
Tom Sepezb1670b52017-02-16 17:01:00 -0800315bool Document::getNthFieldName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700316 const std::vector<CJS_Value>& params,
317 CJS_Value& vRet,
318 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700319 if (params.size() != 1) {
320 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700321 return false;
tsepezcd5dc852016-09-08 11:23:24 -0700322 }
dsinclair82e17672016-10-11 12:38:01 -0700323 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700324 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700325 return false;
tsepez55be02e2016-09-12 11:21:42 -0700326 }
tsepezb4694242016-08-15 16:44:55 -0700327 int nIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 if (nIndex < 0) {
tsepezcd5dc852016-09-08 11:23:24 -0700329 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700330 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700332 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700334 CPDF_FormField* pField = pPDFForm->GetField(nIndex, CFX_WideString());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 if (!pField)
tsepez4cf55152016-11-02 14:37:54 -0700336 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337
tsepezf3dc8c62016-08-10 06:29:29 -0700338 vRet = CJS_Value(pRuntime, pField->GetFullName().c_str());
tsepez4cf55152016-11-02 14:37:54 -0700339 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340}
341
Tom Sepezb1670b52017-02-16 17:01:00 -0800342bool Document::importAnFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700343 const std::vector<CJS_Value>& params,
344 CJS_Value& vRet,
345 CFX_WideString& sError) {
346 // Unsafe, not supported.
347 return true;
348}
349
Tom Sepezb1670b52017-02-16 17:01:00 -0800350bool Document::importAnXFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700351 const std::vector<CJS_Value>& params,
352 CJS_Value& vRet,
353 CFX_WideString& sError) {
354 // Unsafe, not supported.
355 return true;
356}
357
Tom Sepezb1670b52017-02-16 17:01:00 -0800358bool Document::importTextData(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800359 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 CJS_Value& vRet,
361 CFX_WideString& sError) {
362 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700363 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364}
365
366// exports the form data and mails the resulting fdf file as an attachment to
367// all recipients.
368// comment: need reader supports
Tom Sepezb1670b52017-02-16 17:01:00 -0800369bool Document::mailForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700370 const std::vector<CJS_Value>& params,
371 CJS_Value& vRet,
372 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700373 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700374 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700375 return false;
tsepez55be02e2016-09-12 11:21:42 -0700376 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700377 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -0700378 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700379 return false;
tsepez55be02e2016-09-12 11:21:42 -0700380 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 int iLength = params.size();
tsepez4cf55152016-11-02 14:37:54 -0700382 bool bUI = iLength > 0 ? params[0].ToBool(pRuntime) : true;
tsepezb4694242016-08-15 16:44:55 -0700383 CFX_WideString cTo = iLength > 1 ? params[1].ToCFXWideString(pRuntime) : L"";
384 CFX_WideString cCc = iLength > 2 ? params[2].ToCFXWideString(pRuntime) : L"";
385 CFX_WideString cBcc = iLength > 3 ? params[3].ToCFXWideString(pRuntime) : L"";
tsepezf3dc8c62016-08-10 06:29:29 -0700386 CFX_WideString cSubject =
tsepezb4694242016-08-15 16:44:55 -0700387 iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
388 CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
dsinclair7cbe68e2016-10-12 11:56:23 -0700389 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 CFX_ByteTextBuf textBuf;
391 if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
tsepez4cf55152016-11-02 14:37:54 -0700392 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 pRuntime->BeginBlock();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800395 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
dsinclair8779fa82016-10-12 12:05:44 -0700396 pFormFillEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI,
397 cTo.c_str(), cSubject.c_str(), cCc.c_str(),
398 cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700400 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401}
402
Tom Sepezb1670b52017-02-16 17:01:00 -0800403bool Document::print(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700404 const std::vector<CJS_Value>& params,
405 CJS_Value& vRet,
406 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700407 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700408 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700409 return false;
tsepez55be02e2016-09-12 11:21:42 -0700410 }
tsepez4cf55152016-11-02 14:37:54 -0700411 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412 int nStart = 0;
413 int nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -0700414 bool bSilent = false;
415 bool bShrinkToFit = false;
416 bool bPrintAsImage = false;
417 bool bReverse = false;
418 bool bAnnotations = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419 int nlength = params.size();
420 if (nlength == 9) {
tsepez40faa792016-07-15 17:58:02 -0700421 if (params[8].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700422 v8::Local<v8::Object> pObj = params[8].ToV8Object(pRuntime);
423 if (CFXJS_Engine::GetObjDefnID(pObj) ==
424 CJS_PrintParamsObj::g_nObjDefnID) {
425 if (CJS_Object* pJSObj = params[8].ToCJSObject(pRuntime)) {
tsepez40faa792016-07-15 17:58:02 -0700426 if (PrintParamsObj* pprintparamsObj =
427 static_cast<PrintParamsObj*>(pJSObj->GetEmbedObject())) {
428 bUI = pprintparamsObj->bUI;
429 nStart = pprintparamsObj->nStart;
430 nEnd = pprintparamsObj->nEnd;
431 bSilent = pprintparamsObj->bSilent;
432 bShrinkToFit = pprintparamsObj->bShrinkToFit;
433 bPrintAsImage = pprintparamsObj->bPrintAsImage;
434 bReverse = pprintparamsObj->bReverse;
435 bAnnotations = pprintparamsObj->bAnnotations;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700437 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700438 }
439 }
440 } else {
441 if (nlength >= 1)
tsepezb4694242016-08-15 16:44:55 -0700442 bUI = params[0].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443 if (nlength >= 2)
tsepezb4694242016-08-15 16:44:55 -0700444 nStart = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700445 if (nlength >= 3)
tsepezb4694242016-08-15 16:44:55 -0700446 nEnd = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447 if (nlength >= 4)
tsepezb4694242016-08-15 16:44:55 -0700448 bSilent = params[3].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449 if (nlength >= 5)
tsepezb4694242016-08-15 16:44:55 -0700450 bShrinkToFit = params[4].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700451 if (nlength >= 6)
tsepezb4694242016-08-15 16:44:55 -0700452 bPrintAsImage = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 if (nlength >= 7)
tsepezb4694242016-08-15 16:44:55 -0700454 bReverse = params[6].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455 if (nlength >= 8)
tsepezb4694242016-08-15 16:44:55 -0700456 bAnnotations = params[7].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457 }
458
dsinclair82e17672016-10-11 12:38:01 -0700459 if (m_pFormFillEnv) {
460 m_pFormFillEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit,
461 bPrintAsImage, bReverse, bAnnotations);
tsepez4cf55152016-11-02 14:37:54 -0700462 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463 }
tsepez4cf55152016-11-02 14:37:54 -0700464 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465}
466
467// removes the specified field from the document.
468// comment:
dsinclair1897bdc2016-09-06 14:18:57 -0700469// note: if the filed name is not rational, adobe is dumb for it.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470
Tom Sepezb1670b52017-02-16 17:01:00 -0800471bool Document::removeField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700472 const std::vector<CJS_Value>& params,
473 CJS_Value& vRet,
474 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700475 if (params.size() != 1) {
476 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700477 return false;
tsepez55be02e2016-09-12 11:21:42 -0700478 }
dsinclair82e17672016-10-11 12:38:01 -0700479 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700480 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700481 return false;
tsepez55be02e2016-09-12 11:21:42 -0700482 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700483 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
484 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM))) {
tsepezcd5dc852016-09-08 11:23:24 -0700485 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700486 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700487 }
tsepezb4694242016-08-15 16:44:55 -0700488 CFX_WideString sFieldName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700489 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
tsepez8fa82792017-01-11 09:32:33 -0800490 std::vector<CPDFSDK_Annot::ObservedPtr> widgets;
Lei Zhangd88a3642015-11-10 09:38:57 -0800491 pInterForm->GetWidgets(sFieldName, &widgets);
Lei Zhangd88a3642015-11-10 09:38:57 -0800492 if (widgets.empty())
tsepez4cf55152016-11-02 14:37:54 -0700493 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494
tsepez8fa82792017-01-11 09:32:33 -0800495 for (const auto& pAnnot : widgets) {
496 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot.Get());
497 if (!pWidget)
498 continue;
499
Tom Sepez281a9ea2016-02-26 14:24:28 -0800500 CFX_FloatRect rcAnnot = pWidget->GetRect();
Lei Zhangd88a3642015-11-10 09:38:57 -0800501 --rcAnnot.left;
502 --rcAnnot.bottom;
503 ++rcAnnot.right;
504 ++rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505
tsepezdf964df2016-04-21 12:09:41 -0700506 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800507 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Lei Zhangd88a3642015-11-10 09:38:57 -0800508 ASSERT(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509
dsinclair1897bdc2016-09-06 14:18:57 -0700510 // If there is currently no pageview associated with the page being used
511 // do not create one. We may be in the process of tearing down the document
512 // and creating a new pageview at this point will cause bad things.
dsinclair7cbe68e2016-10-12 11:56:23 -0700513 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, false);
dsinclair1897bdc2016-09-06 14:18:57 -0700514 if (pPageView) {
dsinclair8afe15a2016-10-05 12:00:34 -0700515#if PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700516 pPageView->DeleteAnnot(pWidget);
dsinclair8afe15a2016-10-05 12:00:34 -0700517#endif // PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700518 pPageView->UpdateRects(aRefresh);
519 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700521 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700522
tsepez4cf55152016-11-02 14:37:54 -0700523 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700524}
525
526// reset filed values within a document.
527// comment:
528// note: if the fields names r not rational, aodbe is dumb for it.
529
Tom Sepezb1670b52017-02-16 17:01:00 -0800530bool Document::resetForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700531 const std::vector<CJS_Value>& params,
532 CJS_Value& vRet,
533 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700534 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700535 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700536 return false;
tsepez55be02e2016-09-12 11:21:42 -0700537 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700538 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
539 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
540 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -0700541 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700542 return false;
tsepez55be02e2016-09-12 11:21:42 -0700543 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700544
dsinclair7cbe68e2016-10-12 11:56:23 -0700545 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
tsepeze5aff742016-08-08 09:49:42 -0700547 CJS_Array aName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700548
Lei Zhangdd734de2015-11-11 10:16:52 -0800549 if (params.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700550 pPDFForm->ResetForm(true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700551 m_pFormFillEnv->SetChangeMark();
tsepez4cf55152016-11-02 14:37:54 -0700552 return true;
Lei Zhangd88a3642015-11-10 09:38:57 -0800553 }
554
555 switch (params[0].GetType()) {
556 default:
tsepezb4694242016-08-15 16:44:55 -0700557 aName.Attach(params[0].ToV8Array(pRuntime));
Lei Zhangd88a3642015-11-10 09:38:57 -0800558 break;
559 case CJS_Value::VT_string:
tsepezb4694242016-08-15 16:44:55 -0700560 aName.SetElement(pRuntime, 0, params[0]);
Lei Zhangd88a3642015-11-10 09:38:57 -0800561 break;
562 }
563
564 std::vector<CPDF_FormField*> aFields;
tsepezb4694242016-08-15 16:44:55 -0700565 for (int i = 0, isz = aName.GetLength(pRuntime); i < isz; ++i) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800566 CJS_Value valElement(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700567 aName.GetElement(pRuntime, i, valElement);
568 CFX_WideString swVal = valElement.ToCFXWideString(pRuntime);
Lei Zhangd88a3642015-11-10 09:38:57 -0800569 for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j)
570 aFields.push_back(pPDFForm->GetField(j, swVal));
571 }
572
573 if (!aFields.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700574 pPDFForm->ResetForm(aFields, true, true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700575 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576 }
577
tsepez4cf55152016-11-02 14:37:54 -0700578 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700579}
580
Tom Sepezb1670b52017-02-16 17:01:00 -0800581bool Document::saveAs(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700582 const std::vector<CJS_Value>& params,
583 CJS_Value& vRet,
584 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700585 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700586 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700587}
588
Tom Sepezb1670b52017-02-16 17:01:00 -0800589bool Document::syncAnnotScan(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800590 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700591 CJS_Value& vRet,
592 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700593 return true;
594}
595
Tom Sepezb1670b52017-02-16 17:01:00 -0800596bool Document::submitForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700597 const std::vector<CJS_Value>& params,
598 CJS_Value& vRet,
599 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700600 int nSize = params.size();
601 if (nSize < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700602 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700603 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700604 }
dsinclair82e17672016-10-11 12:38:01 -0700605 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700606 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700607 return false;
tsepez55be02e2016-09-12 11:21:42 -0700608 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800609
tsepeze5aff742016-08-08 09:49:42 -0700610 CJS_Array aFields;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611 CFX_WideString strURL;
tsepez4cf55152016-11-02 14:37:54 -0700612 bool bFDF = true;
613 bool bEmpty = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 CJS_Value v = params[0];
Tom Sepez39bfe122015-09-17 15:25:23 -0700615 if (v.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700616 strURL = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617 if (nSize > 1)
tsepezb4694242016-08-15 16:44:55 -0700618 bFDF = params[1].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619 if (nSize > 2)
tsepezb4694242016-08-15 16:44:55 -0700620 bEmpty = params[2].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700621 if (nSize > 3)
tsepezb4694242016-08-15 16:44:55 -0700622 aFields.Attach(params[3].ToV8Array(pRuntime));
Tom Sepez39bfe122015-09-17 15:25:23 -0700623 } else if (v.GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700624 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
625 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"cURL");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700626 if (!pValue.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700627 strURL = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700628
tsepezb4694242016-08-15 16:44:55 -0700629 pValue = pRuntime->GetObjectProperty(pObj, L"bFDF");
630 bFDF = 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"bEmpty");
633 bEmpty = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700634
tsepezb4694242016-08-15 16:44:55 -0700635 pValue = pRuntime->GetObjectProperty(pObj, L"aFields");
636 aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700637 }
638
dsinclair7cbe68e2016-10-12 11:56:23 -0700639 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
tsepezb4694242016-08-15 16:44:55 -0700641 if (aFields.GetLength(pRuntime) == 0 && bEmpty) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800642 if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643 pRuntime->BeginBlock();
tsepez4cf55152016-11-02 14:37:54 -0700644 pInterForm->SubmitForm(strURL, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700645 pRuntime->EndBlock();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700646 }
tsepez4cf55152016-11-02 14:37:54 -0700647 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700648 }
649
Lei Zhangd88a3642015-11-10 09:38:57 -0800650 std::vector<CPDF_FormField*> fieldObjects;
tsepezb4694242016-08-15 16:44:55 -0700651 for (int i = 0, sz = aFields.GetLength(pRuntime); i < sz; ++i) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700652 CJS_Value valName(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700653 aFields.GetElement(pRuntime, i, valName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700654
tsepezb4694242016-08-15 16:44:55 -0700655 CFX_WideString sName = valName.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700656 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
657 for (int j = 0, jsz = pPDFForm->CountFields(sName); j < jsz; ++j) {
658 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
659 if (!bEmpty && pField->GetValue().IsEmpty())
660 continue;
661
Lei Zhangd88a3642015-11-10 09:38:57 -0800662 fieldObjects.push_back(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700663 }
664 }
665
Lei Zhangd88a3642015-11-10 09:38:57 -0800666 if (pPDFInterForm->CheckRequiredFields(&fieldObjects, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700667 pRuntime->BeginBlock();
Wei Li97da9762016-03-11 17:00:48 -0800668 pInterForm->SubmitFields(strURL, fieldObjects, true, !bFDF);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700669 pRuntime->EndBlock();
670 }
tsepez4cf55152016-11-02 14:37:54 -0700671 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700672}
673
dsinclair82e17672016-10-11 12:38:01 -0700674void Document::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
675 m_pFormFillEnv.Reset(pFormFillEnv);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700676}
677
Tom Sepezb1670b52017-02-16 17:01:00 -0800678bool Document::bookmarkRoot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700679 CJS_PropValue& vp,
680 CFX_WideString& sError) {
681 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700682}
683
Tom Sepezb1670b52017-02-16 17:01:00 -0800684bool Document::mailDoc(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700685 const std::vector<CJS_Value>& params,
686 CJS_Value& vRet,
687 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700688 // TODO(tsepez): Check maximum number of allowed params.
tsepez4cf55152016-11-02 14:37:54 -0700689 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700690 CFX_WideString cTo = L"";
691 CFX_WideString cCc = L"";
692 CFX_WideString cBcc = L"";
693 CFX_WideString cSubject = L"";
694 CFX_WideString cMsg = L"";
695
tsepezf3dc8c62016-08-10 06:29:29 -0700696 if (params.size() >= 1)
tsepezb4694242016-08-15 16:44:55 -0700697 bUI = params[0].ToBool(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700698 if (params.size() >= 2)
tsepezb4694242016-08-15 16:44:55 -0700699 cTo = params[1].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700700 if (params.size() >= 3)
tsepezb4694242016-08-15 16:44:55 -0700701 cCc = params[2].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700702 if (params.size() >= 4)
tsepezb4694242016-08-15 16:44:55 -0700703 cBcc = params[3].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700704 if (params.size() >= 5)
tsepezb4694242016-08-15 16:44:55 -0700705 cSubject = params[4].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700706 if (params.size() >= 6)
tsepezb4694242016-08-15 16:44:55 -0700707 cMsg = params[5].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708
Tom Sepez39bfe122015-09-17 15:25:23 -0700709 if (params.size() >= 1 && params[0].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700710 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711
tsepezb4694242016-08-15 16:44:55 -0700712 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"bUI");
tsepez71ba5882016-10-28 09:35:49 -0700713 bUI = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700714
tsepezb4694242016-08-15 16:44:55 -0700715 pValue = pRuntime->GetObjectProperty(pObj, L"cTo");
716 cTo = 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"cCc");
719 cCc = 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"cBcc");
722 cBcc = 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"cSubject");
725 cSubject = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700726
tsepezb4694242016-08-15 16:44:55 -0700727 pValue = pRuntime->GetObjectProperty(pObj, L"cMsg");
728 cMsg = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700729 }
730
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700731 pRuntime->BeginBlock();
dsinclair8779fa82016-10-12 12:05:44 -0700732 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
733 pFormFillEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
734 cCc.c_str(), cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700735 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700736 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700737}
738
Tom Sepezb1670b52017-02-16 17:01:00 -0800739bool Document::author(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700740 CJS_PropValue& vp,
741 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800742 return getPropertyInternal(pRuntime, vp, "Author", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700743}
744
Tom Sepezb1670b52017-02-16 17:01:00 -0800745bool Document::info(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700746 CJS_PropValue& vp,
747 CFX_WideString& sError) {
tonikitooec268762016-08-10 12:29:28 -0700748 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700749 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700750 return false;
tonikitooec268762016-08-10 12:29:28 -0700751 }
dsinclair82e17672016-10-11 12:38:01 -0700752 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700753 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700754 return false;
tsepez55be02e2016-09-12 11:21:42 -0700755 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700756 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700757 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700758 return false;
Tom Sepezc6ab1722015-02-05 15:27:25 -0800759
dsinclair38fd8442016-09-15 10:15:32 -0700760 CFX_WideString cwAuthor = pDictionary->GetUnicodeTextFor("Author");
761 CFX_WideString cwTitle = pDictionary->GetUnicodeTextFor("Title");
762 CFX_WideString cwSubject = pDictionary->GetUnicodeTextFor("Subject");
763 CFX_WideString cwKeywords = pDictionary->GetUnicodeTextFor("Keywords");
764 CFX_WideString cwCreator = pDictionary->GetUnicodeTextFor("Creator");
765 CFX_WideString cwProducer = pDictionary->GetUnicodeTextFor("Producer");
766 CFX_WideString cwCreationDate =
767 pDictionary->GetUnicodeTextFor("CreationDate");
768 CFX_WideString cwModDate = pDictionary->GetUnicodeTextFor("ModDate");
769 CFX_WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped");
Tom Sepezc6ab1722015-02-05 15:27:25 -0800770
tsepezb4694242016-08-15 16:44:55 -0700771 v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1);
tsepeze6cf0132017-01-18 14:38:18 -0800772 pRuntime->PutObjectProperty(pObj, L"Author", pRuntime->NewString(cwAuthor));
773 pRuntime->PutObjectProperty(pObj, L"Title", pRuntime->NewString(cwTitle));
774 pRuntime->PutObjectProperty(pObj, L"Subject", pRuntime->NewString(cwSubject));
775 pRuntime->PutObjectProperty(pObj, L"Keywords",
776 pRuntime->NewString(cwKeywords));
777 pRuntime->PutObjectProperty(pObj, L"Creator", pRuntime->NewString(cwCreator));
778 pRuntime->PutObjectProperty(pObj, L"Producer",
779 pRuntime->NewString(cwProducer));
780 pRuntime->PutObjectProperty(pObj, L"CreationDate",
781 pRuntime->NewString(cwCreationDate));
782 pRuntime->PutObjectProperty(pObj, L"ModDate", pRuntime->NewString(cwModDate));
783 pRuntime->PutObjectProperty(pObj, L"Trapped", pRuntime->NewString(cwTrapped));
tonikitooec268762016-08-10 12:29:28 -0700784
785 // It's to be compatible to non-standard info dictionary.
786 for (const auto& it : *pDictionary) {
787 const CFX_ByteString& bsKey = it.first;
tsepez0e606b52016-11-18 16:22:41 -0800788 CPDF_Object* pValueObj = it.second.get();
tonikitooec268762016-08-10 12:29:28 -0700789 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
790 if (pValueObj->IsString() || pValueObj->IsName()) {
tsepeze6cf0132017-01-18 14:38:18 -0800791 pRuntime->PutObjectProperty(
792 pObj, wsKey, pRuntime->NewString(pValueObj->GetUnicodeText()));
tonikitooec268762016-08-10 12:29:28 -0700793 } else if (pValueObj->IsNumber()) {
tsepeze6cf0132017-01-18 14:38:18 -0800794 pRuntime->PutObjectProperty(pObj, wsKey,
795 pRuntime->NewNumber(pValueObj->GetNumber()));
tonikitooec268762016-08-10 12:29:28 -0700796 } else if (pValueObj->IsBoolean()) {
tsepeze6cf0132017-01-18 14:38:18 -0800797 pRuntime->PutObjectProperty(
798 pObj, wsKey, pRuntime->NewBoolean(!!pValueObj->GetInteger()));
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700799 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700800 }
tonikitooec268762016-08-10 12:29:28 -0700801 vp << pObj;
tsepez4cf55152016-11-02 14:37:54 -0700802 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700803}
804
Tom Sepezb1670b52017-02-16 17:01:00 -0800805bool Document::getPropertyInternal(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700806 CJS_PropValue& vp,
807 const CFX_ByteString& propName,
808 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700809 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700810 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700811 return false;
tsepez55be02e2016-09-12 11:21:42 -0700812 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700813 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700814 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700815 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816
817 if (vp.IsGetting()) {
dsinclair38fd8442016-09-15 10:15:32 -0700818 vp << pDictionary->GetUnicodeTextFor(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700819 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700820 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
tsepez55be02e2016-09-12 11:21:42 -0700821 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700822 return false;
tsepez55be02e2016-09-12 11:21:42 -0700823 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700824 CFX_WideString csProperty;
825 vp >> csProperty;
tsepez0e606b52016-11-18 16:22:41 -0800826 pDictionary->SetNewFor<CPDF_String>(propName, PDF_EncodeText(csProperty),
827 false);
dsinclair7cbe68e2016-10-12 11:56:23 -0700828 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829 }
tsepez4cf55152016-11-02 14:37:54 -0700830 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700831}
832
Tom Sepezb1670b52017-02-16 17:01:00 -0800833bool Document::creationDate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700834 CJS_PropValue& vp,
835 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800836 return getPropertyInternal(pRuntime, vp, "CreationDate", sError);
tonikitooea3ff9e2016-08-02 11:52:28 -0700837}
838
Tom Sepezb1670b52017-02-16 17:01:00 -0800839bool Document::creator(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700840 CJS_PropValue& vp,
841 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800842 return getPropertyInternal(pRuntime, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700843}
844
Tom Sepezb1670b52017-02-16 17:01:00 -0800845bool Document::delay(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700846 CJS_PropValue& vp,
847 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700848 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700849 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700850 return false;
tsepez55be02e2016-09-12 11:21:42 -0700851 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700852 if (vp.IsGetting()) {
853 vp << m_bDelay;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800854 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700855 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800856 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
857 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
858 return false;
859 }
860 vp >> m_bDelay;
861 if (m_bDelay) {
862 m_DelayData.clear();
863 return true;
864 }
865 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
866 DelayDataToProcess.swap(m_DelayData);
867 for (const auto& pData : DelayDataToProcess)
868 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
869
tsepez4cf55152016-11-02 14:37:54 -0700870 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700871}
872
Tom Sepezb1670b52017-02-16 17:01:00 -0800873bool Document::keywords(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700874 CJS_PropValue& vp,
875 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800876 return getPropertyInternal(pRuntime, vp, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877}
878
Tom Sepezb1670b52017-02-16 17:01:00 -0800879bool Document::modDate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700880 CJS_PropValue& vp,
881 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800882 return getPropertyInternal(pRuntime, vp, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700883}
884
Tom Sepezb1670b52017-02-16 17:01:00 -0800885bool Document::producer(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700886 CJS_PropValue& vp,
887 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800888 return getPropertyInternal(pRuntime, vp, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889}
890
Tom Sepezb1670b52017-02-16 17:01:00 -0800891bool Document::subject(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700892 CJS_PropValue& vp,
893 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800894 return getPropertyInternal(pRuntime, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700895}
896
Tom Sepezb1670b52017-02-16 17:01:00 -0800897bool Document::title(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700898 CJS_PropValue& vp,
899 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700900 if (!m_pFormFillEnv || !m_pFormFillEnv->GetUnderlyingDocument()) {
tsepez55be02e2016-09-12 11:21:42 -0700901 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700902 return false;
tsepez55be02e2016-09-12 11:21:42 -0700903 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800904 return getPropertyInternal(pRuntime, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700905}
906
Tom Sepezb1670b52017-02-16 17:01:00 -0800907bool Document::numPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700908 CJS_PropValue& vp,
909 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700910 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700911 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700912 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700913 }
dsinclair82e17672016-10-11 12:38:01 -0700914 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700915 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700916 return false;
tsepez55be02e2016-09-12 11:21:42 -0700917 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700918 vp << m_pFormFillEnv->GetPageCount();
tsepez4cf55152016-11-02 14:37:54 -0700919 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700920}
921
Tom Sepezb1670b52017-02-16 17:01:00 -0800922bool Document::external(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700923 CJS_PropValue& vp,
924 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700925 // In Chrome case, should always return true.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926 if (vp.IsGetting()) {
927 vp << true;
928 }
tsepez4cf55152016-11-02 14:37:54 -0700929 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700930}
931
Tom Sepezb1670b52017-02-16 17:01:00 -0800932bool Document::filesize(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700933 CJS_PropValue& vp,
934 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700935 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700936 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700937 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700938 }
939 vp << 0;
tsepez4cf55152016-11-02 14:37:54 -0700940 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941}
942
Tom Sepezb1670b52017-02-16 17:01:00 -0800943bool Document::mouseX(CJS_Runtime* pRuntime,
tonikitoo33c4cdb2016-08-08 10:52:51 -0700944 CJS_PropValue& vp,
945 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700946 return true;
947}
948
Tom Sepezb1670b52017-02-16 17:01:00 -0800949bool Document::mouseY(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700950 CJS_PropValue& vp,
951 CFX_WideString& sError) {
952 return true;
953}
954
Tom Sepezb1670b52017-02-16 17:01:00 -0800955bool Document::URL(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800956 CJS_PropValue& vp,
957 CFX_WideString& sError) {
tonikitoo33c4cdb2016-08-08 10:52:51 -0700958 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700959 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700960 return false;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700961 }
dsinclair82e17672016-10-11 12:38:01 -0700962 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700963 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700964 return false;
tsepez55be02e2016-09-12 11:21:42 -0700965 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700966 vp << m_pFormFillEnv->JS_docGetFilePath();
tsepez4cf55152016-11-02 14:37:54 -0700967 return true;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700968}
969
Tom Sepezb1670b52017-02-16 17:01:00 -0800970bool Document::baseURL(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700971 CJS_PropValue& vp,
972 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700973 if (vp.IsGetting()) {
974 vp << m_cwBaseURL;
975 } else {
976 vp >> m_cwBaseURL;
977 }
tsepez4cf55152016-11-02 14:37:54 -0700978 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700979}
980
Tom Sepezb1670b52017-02-16 17:01:00 -0800981bool Document::calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700982 CJS_PropValue& vp,
983 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700984 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700985 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700986 return false;
tsepez55be02e2016-09-12 11:21:42 -0700987 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700988 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700989 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -0700990 vp << !!pInterForm->IsCalculateEnabled();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800991 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700992 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800993 bool bCalculate;
994 vp >> bCalculate;
995 pInterForm->EnableCalculate(bCalculate);
tsepez4cf55152016-11-02 14:37:54 -0700996 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700997}
998
Tom Sepezb1670b52017-02-16 17:01:00 -0800999bool Document::documentFileName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001000 CJS_PropValue& vp,
1001 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001002 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001003 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001004 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 }
dsinclair82e17672016-10-11 12:38:01 -07001006 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001007 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001008 return false;
tsepez55be02e2016-09-12 11:21:42 -07001009 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001010 CFX_WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011 int32_t i = wsFilePath.GetLength() - 1;
1012 for (; i >= 0; i--) {
1013 if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
1014 break;
1015 }
1016 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
1017 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
1018 } else {
1019 vp << L"";
1020 }
tsepez4cf55152016-11-02 14:37:54 -07001021 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022}
1023
Tom Sepezb1670b52017-02-16 17:01:00 -08001024bool Document::path(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001025 CJS_PropValue& vp,
1026 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001027 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001028 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001029 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001030 }
dsinclair82e17672016-10-11 12:38:01 -07001031 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001032 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001033 return false;
tsepez55be02e2016-09-12 11:21:42 -07001034 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001035 vp << app::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath());
tsepez4cf55152016-11-02 14:37:54 -07001036 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001037}
1038
Tom Sepezb1670b52017-02-16 17:01:00 -08001039bool Document::pageWindowRect(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001040 CJS_PropValue& vp,
1041 CFX_WideString& sError) {
1042 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001043}
1044
Tom Sepezb1670b52017-02-16 17:01:00 -08001045bool Document::layout(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001046 CJS_PropValue& vp,
1047 CFX_WideString& sError) {
1048 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049}
1050
Tom Sepezb1670b52017-02-16 17:01:00 -08001051bool Document::addLink(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001052 const std::vector<CJS_Value>& params,
1053 CJS_Value& vRet,
1054 CFX_WideString& sError) {
1055 return true;
1056}
1057
Tom Sepezb1670b52017-02-16 17:01:00 -08001058bool Document::closeDoc(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001059 const std::vector<CJS_Value>& params,
1060 CJS_Value& vRet,
1061 CFX_WideString& sError) {
1062 return true;
1063}
1064
Tom Sepezb1670b52017-02-16 17:01:00 -08001065bool Document::getPageBox(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001066 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001067 CJS_Value& vRet,
1068 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001069 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001070}
1071
Tom Sepezb1670b52017-02-16 17:01:00 -08001072bool Document::getAnnot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001073 const std::vector<CJS_Value>& params,
1074 CJS_Value& vRet,
1075 CFX_WideString& sError) {
tonikitoo618cb1f2016-08-18 20:10:17 -07001076 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001077 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001078 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001079 }
dsinclair82e17672016-10-11 12:38:01 -07001080 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001081 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001082 return false;
tsepez55be02e2016-09-12 11:21:42 -07001083 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001084 int nPageNo = params[0].ToInt(pRuntime);
1085 CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -07001086 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(nPageNo);
tonikitoo618cb1f2016-08-18 20:10:17 -07001087 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001088 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001089
tsepezd805eec2017-01-11 14:03:54 -08001090 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
tonikitoo618cb1f2016-08-18 20:10:17 -07001091 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
tsepezd805eec2017-01-11 14:03:54 -08001092 for (const auto& pSDKAnnotCur : annotIteration) {
1093 CPDFSDK_BAAnnot* pBAAnnot =
1094 static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get());
tonikitoo618cb1f2016-08-18 20:10:17 -07001095 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1096 pSDKBAAnnot = pBAAnnot;
1097 break;
1098 }
1099 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001100 if (!pSDKBAAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001101 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001102
1103 v8::Local<v8::Object> pObj =
1104 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1105 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001106 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001107
1108 CJS_Annot* pJS_Annot =
1109 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1110 if (!pJS_Annot)
tsepez4cf55152016-11-02 14:37:54 -07001111 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001112
1113 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1114 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001115 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001116
1117 pAnnot->SetSDKAnnot(pSDKBAAnnot);
tonikitoo618cb1f2016-08-18 20:10:17 -07001118 vRet = CJS_Value(pRuntime, pJS_Annot);
tsepez4cf55152016-11-02 14:37:54 -07001119 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001120}
1121
Tom Sepezb1670b52017-02-16 17:01:00 -08001122bool Document::getAnnots(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001123 const std::vector<CJS_Value>& params,
1124 CJS_Value& vRet,
1125 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001126 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001127 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001128 return false;
tsepez55be02e2016-09-12 11:21:42 -07001129 }
tonikitoo0ee35902016-08-26 14:41:29 -07001130 // TODO(tonikitoo): Add support supported parameters as per
1131 // the PDF spec.
1132
dsinclair7cbe68e2016-10-12 11:56:23 -07001133 int nPageNo = m_pFormFillEnv->GetPageCount();
tonikitoo0ee35902016-08-26 14:41:29 -07001134 CJS_Array annots;
1135
1136 for (int i = 0; i < nPageNo; ++i) {
dsinclair7cbe68e2016-10-12 11:56:23 -07001137 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(i);
tonikitoo0ee35902016-08-26 14:41:29 -07001138 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001139 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001140
tsepezd805eec2017-01-11 14:03:54 -08001141 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
1142 for (const auto& pSDKAnnotCur : annotIteration) {
1143 if (!pSDKAnnotCur) {
1144 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001145 return false;
tsepezd805eec2017-01-11 14:03:54 -08001146 }
tonikitoo0ee35902016-08-26 14:41:29 -07001147 v8::Local<v8::Object> pObj =
1148 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1149 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001150 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001151
1152 CJS_Annot* pJS_Annot =
1153 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1154 if (!pJS_Annot)
tsepez4cf55152016-11-02 14:37:54 -07001155 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001156
1157 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1158 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001159 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001160
tsepezd805eec2017-01-11 14:03:54 -08001161 pAnnot->SetSDKAnnot(static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get()));
tonikitoo0ee35902016-08-26 14:41:29 -07001162 annots.SetElement(pRuntime, i, CJS_Value(pRuntime, pJS_Annot));
1163 }
1164 }
tonikitoo0ee35902016-08-26 14:41:29 -07001165 vRet = CJS_Value(pRuntime, annots);
tsepez4cf55152016-11-02 14:37:54 -07001166 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167}
1168
Tom Sepezb1670b52017-02-16 17:01:00 -08001169bool Document::getAnnot3D(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001170 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171 CJS_Value& vRet,
1172 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001173 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001174 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175}
1176
Tom Sepezb1670b52017-02-16 17:01:00 -08001177bool Document::getAnnots3D(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001178 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001179 CJS_Value& vRet,
1180 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001181 return true;
1182}
1183
Tom Sepezb1670b52017-02-16 17:01:00 -08001184bool Document::getOCGs(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001185 const std::vector<CJS_Value>& params,
1186 CJS_Value& vRet,
1187 CFX_WideString& sError) {
1188 return true;
1189}
1190
Tom Sepezb1670b52017-02-16 17:01:00 -08001191bool Document::getLinks(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001192 const std::vector<CJS_Value>& params,
1193 CJS_Value& vRet,
1194 CFX_WideString& sError) {
1195 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001196}
1197
1198bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1199 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1200 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1201}
1202
Tom Sepezb1670b52017-02-16 17:01:00 -08001203bool Document::addIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001204 const std::vector<CJS_Value>& params,
1205 CJS_Value& vRet,
1206 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001207 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001208 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001209 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001210 }
tsepezf3dc8c62016-08-10 06:29:29 -07001211
tsepezb4694242016-08-15 16:44:55 -07001212 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Tom Sepez39bfe122015-09-17 15:25:23 -07001213 if (params[1].GetType() != CJS_Value::VT_object) {
tsepezcd5dc852016-09-08 11:23:24 -07001214 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001215 return false;
Tom Sepezaecd9d02015-08-31 15:04:12 -07001216 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001217
tsepezb4694242016-08-15 16:44:55 -07001218 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pRuntime);
1219 if (pRuntime->GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
tsepezcd5dc852016-09-08 11:23:24 -07001220 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001221 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001222 }
1223
tsepezb4694242016-08-15 16:44:55 -07001224 CJS_EmbedObj* pEmbedObj = params[1].ToCJSObject(pRuntime)->GetEmbedObject();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001225 if (!pEmbedObj) {
tsepezcd5dc852016-09-08 11:23:24 -07001226 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001227 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001228 }
1229
thestigdadea5f2016-10-19 13:29:48 -07001230 m_Icons.push_back(pdfium::MakeUnique<IconElement>(
1231 swIconName, static_cast<Icon*>(pEmbedObj)));
tsepez4cf55152016-11-02 14:37:54 -07001232 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001233}
1234
Tom Sepezb1670b52017-02-16 17:01:00 -08001235bool Document::icons(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001236 CJS_PropValue& vp,
1237 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001238 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001239 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001240 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001241 }
thestigdadea5f2016-10-19 13:29:48 -07001242 if (m_Icons.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001243 vp.GetJSValue()->SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001244 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001245 }
1246
tsepeze5aff742016-08-08 09:49:42 -07001247 CJS_Array Icons;
Tom Sepez6ba32d92016-01-05 16:26:32 -08001248 int i = 0;
thestigdadea5f2016-10-19 13:29:48 -07001249 for (const auto& pIconElement : m_Icons) {
tsepezb4694242016-08-15 16:44:55 -07001250 v8::Local<v8::Object> pObj =
1251 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001252 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001253 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001254
tsepezb4694242016-08-15 16:44:55 -07001255 CJS_Icon* pJS_Icon =
1256 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001257 if (!pJS_Icon)
tsepez4cf55152016-11-02 14:37:54 -07001258 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001259
thestigdadea5f2016-10-19 13:29:48 -07001260 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001261 if (!pIcon)
tsepez4cf55152016-11-02 14:37:54 -07001262 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001263
dsinclair09bad1c2016-10-18 19:26:30 -07001264 pIcon->SetStream(pIconElement->IconStream->GetStream());
1265 pIcon->SetIconName(pIconElement->IconName);
tsepezb4694242016-08-15 16:44:55 -07001266 Icons.SetElement(pRuntime, i++, CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267 }
1268
1269 vp << Icons;
tsepez4cf55152016-11-02 14:37:54 -07001270 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001271}
1272
Tom Sepezb1670b52017-02-16 17:01:00 -08001273bool Document::getIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001274 const std::vector<CJS_Value>& params,
1275 CJS_Value& vRet,
1276 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001278 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001279 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001280 }
1281
thestigdadea5f2016-10-19 13:29:48 -07001282 if (m_Icons.empty())
tsepez4cf55152016-11-02 14:37:54 -07001283 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001284
tsepezb4694242016-08-15 16:44:55 -07001285 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
thestigdadea5f2016-10-19 13:29:48 -07001286 for (const auto& pIconElement : m_Icons) {
1287 if (pIconElement->IconName != swIconName)
1288 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001289
thestigdadea5f2016-10-19 13:29:48 -07001290 v8::Local<v8::Object> pObj =
1291 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
1292 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001293 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001294
thestigdadea5f2016-10-19 13:29:48 -07001295 CJS_Icon* pJS_Icon =
1296 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
1297 if (!pJS_Icon)
tsepez4cf55152016-11-02 14:37:54 -07001298 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001299
thestigdadea5f2016-10-19 13:29:48 -07001300 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1301 if (!pIcon)
tsepez4cf55152016-11-02 14:37:54 -07001302 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001303
thestigdadea5f2016-10-19 13:29:48 -07001304 pIcon->SetIconName(swIconName);
1305 pIcon->SetStream(pIconElement->IconStream->GetStream());
1306
1307 vRet = CJS_Value(pRuntime, pJS_Icon);
tsepez4cf55152016-11-02 14:37:54 -07001308 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001309 }
1310
tsepez4cf55152016-11-02 14:37:54 -07001311 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001312}
1313
Tom Sepezb1670b52017-02-16 17:01:00 -08001314bool Document::removeIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001315 const std::vector<CJS_Value>& params,
1316 CJS_Value& vRet,
1317 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001318 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001319 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001320}
1321
Tom Sepezb1670b52017-02-16 17:01:00 -08001322bool Document::createDataObject(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001323 const std::vector<CJS_Value>& params,
1324 CJS_Value& vRet,
1325 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001326 // Unsafe, not implemented.
tsepez4cf55152016-11-02 14:37:54 -07001327 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001328}
1329
Tom Sepezb1670b52017-02-16 17:01:00 -08001330bool Document::media(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001331 CJS_PropValue& vp,
1332 CFX_WideString& sError) {
1333 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001334}
1335
Tom Sepezb1670b52017-02-16 17:01:00 -08001336bool Document::calculateNow(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001337 const std::vector<CJS_Value>& params,
1338 CJS_Value& vRet,
1339 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001340 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001341 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001342 return false;
tsepez55be02e2016-09-12 11:21:42 -07001343 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001344 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
1345 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
1346 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -07001347 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001348 return false;
tsepez55be02e2016-09-12 11:21:42 -07001349 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001350 m_pFormFillEnv->GetInterForm()->OnCalculate();
tsepez4cf55152016-11-02 14:37:54 -07001351 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001352}
1353
Tom Sepezb1670b52017-02-16 17:01:00 -08001354bool Document::Collab(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001355 CJS_PropValue& vp,
1356 CFX_WideString& sError) {
1357 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001358}
1359
Tom Sepezb1670b52017-02-16 17:01:00 -08001360bool Document::getPageNthWord(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001361 const std::vector<CJS_Value>& params,
1362 CJS_Value& vRet,
1363 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001364 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001365 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001366 return false;
tsepez55be02e2016-09-12 11:21:42 -07001367 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001368 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001369 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001370 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001371 }
tsepez55be02e2016-09-12 11:21:42 -07001372
1373 // TODO(tsepez): check maximum allowable params.
1374
tsepezb4694242016-08-15 16:44:55 -07001375 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
1376 int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
1377 bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001378
dsinclair7cbe68e2016-10-12 11:56:23 -07001379 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001380 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001381 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001382
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001383 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001384 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001385 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001386 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001387
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001388 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1389 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001390 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001391
thestig5cc24652016-04-26 11:46:02 -07001392 CPDF_Page page(pDocument, pPageDict, true);
1393 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001394
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001395 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001396 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001397 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001398 if (pPageObj->IsText()) {
1399 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001400 int nObjWords = CountWords(pTextObj);
1401 if (nWords + nObjWords >= nWordNo) {
1402 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1403 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001404 }
Tom Sepez2398d892016-02-17 16:46:26 -08001405 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001406 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001407 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001408
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001409 if (bStrip) {
1410 swRet.TrimLeft();
1411 swRet.TrimRight();
1412 }
1413
tsepezf3dc8c62016-08-10 06:29:29 -07001414 vRet = CJS_Value(pRuntime, swRet.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001415 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001416}
1417
Tom Sepezb1670b52017-02-16 17:01:00 -08001418bool Document::getPageNthWordQuads(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001419 const std::vector<CJS_Value>& params,
1420 CJS_Value& vRet,
1421 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001422 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001423 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001424 return false;
tsepez55be02e2016-09-12 11:21:42 -07001425 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001426 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -07001427 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001428 return false;
tsepez55be02e2016-09-12 11:21:42 -07001429 }
tsepez4cf55152016-11-02 14:37:54 -07001430 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001431}
1432
Tom Sepezb1670b52017-02-16 17:01:00 -08001433bool Document::getPageNumWords(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001434 const std::vector<CJS_Value>& params,
1435 CJS_Value& vRet,
1436 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001437 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001438 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001439 return false;
tsepez55be02e2016-09-12 11:21:42 -07001440 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001441 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001442 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001443 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001444 }
tsepezb4694242016-08-15 16:44:55 -07001445 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
dsinclair7cbe68e2016-10-12 11:56:23 -07001446 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001447 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001448 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001449 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001450 }
1451
1452 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1453 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001454 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001455
thestig5cc24652016-04-26 11:46:02 -07001456 CPDF_Page page(pDocument, pPageDict, true);
1457 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001458
1459 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001460 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001461 if (pPageObj->IsText())
1462 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001463 }
1464
tsepezf3dc8c62016-08-10 06:29:29 -07001465 vRet = CJS_Value(pRuntime, nWords);
tsepez4cf55152016-11-02 14:37:54 -07001466 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001467}
1468
Tom Sepezb1670b52017-02-16 17:01:00 -08001469bool Document::getPrintParams(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001470 const std::vector<CJS_Value>& params,
1471 CJS_Value& vRet,
1472 CFX_WideString& sError) {
tsepezb4694242016-08-15 16:44:55 -07001473 v8::Local<v8::Object> pRetObj =
1474 pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001475
1476 // Not implemented yet.
1477
tsepezf3dc8c62016-08-10 06:29:29 -07001478 vRet = CJS_Value(pRuntime, pRetObj);
tsepez4cf55152016-11-02 14:37:54 -07001479 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001480}
1481
1482#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1483
1484int Document::CountWords(CPDF_TextObject* pTextObj) {
1485 if (!pTextObj)
1486 return 0;
1487
1488 int nWords = 0;
1489
1490 CPDF_Font* pFont = pTextObj->GetFont();
1491 if (!pFont)
1492 return 0;
1493
tsepez4cf55152016-11-02 14:37:54 -07001494 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001495
1496 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001497 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001498 FX_FLOAT kerning;
1499
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001500 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001501 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1502
Tom Sepez62a70f92016-03-21 15:00:20 -07001503 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001504 if (swUnicode.GetLength() > 0)
1505 unicode = swUnicode[0];
1506
1507 if (ISLATINWORD(unicode) && bIsLatin)
1508 continue;
1509
1510 bIsLatin = ISLATINWORD(unicode);
1511 if (unicode != 0x20)
1512 nWords++;
1513 }
1514
1515 return nWords;
1516}
1517
1518CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1519 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001520 CFX_WideString swRet;
1521
1522 CPDF_Font* pFont = pTextObj->GetFont();
1523 if (!pFont)
1524 return L"";
1525
1526 int nWords = 0;
tsepez4cf55152016-11-02 14:37:54 -07001527 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001528
1529 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001530 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001531 FX_FLOAT kerning;
1532
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001533 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001534 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1535
Tom Sepez62a70f92016-03-21 15:00:20 -07001536 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001537 if (swUnicode.GetLength() > 0)
1538 unicode = swUnicode[0];
1539
1540 if (ISLATINWORD(unicode) && bIsLatin) {
1541 } else {
1542 bIsLatin = ISLATINWORD(unicode);
1543 if (unicode != 0x20)
1544 nWords++;
1545 }
1546
1547 if (nWords - 1 == nWordIndex)
1548 swRet += unicode;
1549 }
1550
1551 return swRet;
1552}
1553
Tom Sepezb1670b52017-02-16 17:01:00 -08001554bool Document::zoom(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001555 CJS_PropValue& vp,
1556 CFX_WideString& sError) {
1557 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001558}
1559
1560/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001561(none, NoVary)
1562(fitP, FitPage)
1563(fitW, FitWidth)
1564(fitH, FitHeight)
1565(fitV, FitVisibleWidth)
1566(pref, Preferred)
1567(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001568*/
1569
Tom Sepezb1670b52017-02-16 17:01:00 -08001570bool Document::zoomType(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001571 CJS_PropValue& vp,
1572 CFX_WideString& sError) {
1573 return true;
1574}
1575
Tom Sepezb1670b52017-02-16 17:01:00 -08001576bool Document::deletePages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001577 const std::vector<CJS_Value>& params,
1578 CJS_Value& vRet,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001579 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001580 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001581 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001582}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001583
Tom Sepezb1670b52017-02-16 17:01:00 -08001584bool Document::extractPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001585 const std::vector<CJS_Value>& params,
1586 CJS_Value& vRet,
1587 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001588 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001589 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001590}
1591
Tom Sepezb1670b52017-02-16 17:01:00 -08001592bool Document::insertPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001593 const std::vector<CJS_Value>& params,
1594 CJS_Value& vRet,
1595 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001596 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001597 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001598}
1599
Tom Sepezb1670b52017-02-16 17:01:00 -08001600bool Document::replacePages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001601 const std::vector<CJS_Value>& params,
1602 CJS_Value& vRet,
1603 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001604 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001605 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001606}
1607
Tom Sepezb1670b52017-02-16 17:01:00 -08001608bool Document::getURL(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001609 const std::vector<CJS_Value>& params,
1610 CJS_Value& vRet,
1611 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001612 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001613 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001614}
1615
Tom Sepezb1670b52017-02-16 17:01:00 -08001616bool Document::gotoNamedDest(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001617 const std::vector<CJS_Value>& params,
1618 CJS_Value& vRet,
1619 CFX_WideString& sError) {
tonikitoo1c836752016-08-08 16:14:05 -07001620 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001621 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001622 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001623 }
dsinclair82e17672016-10-11 12:38:01 -07001624 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001625 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001626 return false;
tsepez55be02e2016-09-12 11:21:42 -07001627 }
tsepezb4694242016-08-15 16:44:55 -07001628 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -07001629 CFX_ByteString utf8Name = wideName.UTF8Encode();
dsinclair7cbe68e2016-10-12 11:56:23 -07001630 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
tonikitoo1c836752016-08-08 16:14:05 -07001631 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001632 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001633
tonikitoo1c836752016-08-08 16:14:05 -07001634 CPDF_NameTree nameTree(pDocument, "Dests");
1635 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, utf8Name);
1636 if (!destArray)
tsepez4cf55152016-11-02 14:37:54 -07001637 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001638
1639 CPDF_Dest dest(destArray);
1640 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
1641
1642 std::unique_ptr<float[]> scrollPositionArray;
1643 int scrollPositionArraySize = 0;
1644
1645 if (arrayObject) {
1646 scrollPositionArray.reset(new float[arrayObject->GetCount()]);
1647 int j = 0;
1648 for (size_t i = 2; i < arrayObject->GetCount(); i++)
1649 scrollPositionArray[j++] = arrayObject->GetFloatAt(i);
1650 scrollPositionArraySize = j;
1651 }
1652
tsepezb4694242016-08-15 16:44:55 -07001653 pRuntime->BeginBlock();
dsinclair82e17672016-10-11 12:38:01 -07001654 m_pFormFillEnv->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
1655 scrollPositionArray.get(),
1656 scrollPositionArraySize);
tsepezb4694242016-08-15 16:44:55 -07001657 pRuntime->EndBlock();
tonikitoo1c836752016-08-08 16:14:05 -07001658
tsepez4cf55152016-11-02 14:37:54 -07001659 return true;
tonikitoo1c836752016-08-08 16:14:05 -07001660}
1661
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001662void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001663 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001664}
1665
1666void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1667 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001668 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1669 auto iter = m_DelayData.begin();
1670 while (iter != m_DelayData.end()) {
1671 auto old = iter++;
1672 if ((*old)->sFieldName == sFieldName &&
1673 (*old)->nControlIndex == nControlIndex) {
1674 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1675 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001676 }
1677 }
1678
dsinclair82e17672016-10-11 12:38:01 -07001679 for (const auto& pData : DelayDataForFieldAndControlIndex)
1680 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001681}
1682
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001683CJS_Document* Document::GetCJSDoc() const {
1684 return static_cast<CJS_Document*>(m_pJSObject);
JUN FANG33f6f0d2015-04-06 12:39:51 -07001685}