blob: 38600ed766be34ad213fd73ee7d45c95d38b8710 [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 Sepezd6ae2af2017-02-16 11:49:55 -0800166bool Document::numFields(IJS_EventContext* cc,
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 Sepezd6ae2af2017-02-16 11:49:55 -0800183bool Document::dirty(IJS_EventContext* cc,
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 Sepezd6ae2af2017-02-16 11:49:55 -0800204bool Document::ADBE(IJS_EventContext* cc,
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 Sepezd6ae2af2017-02-16 11:49:55 -0800208 vp.GetJSValue()->SetNull(CJS_Runtime::FromEventContext(cc));
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 Sepezd6ae2af2017-02-16 11:49:55 -0800213bool Document::pageNum(IJS_EventContext* cc,
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 Sepezd6ae2af2017-02-16 11:49:55 -0800238bool Document::addAnnot(IJS_EventContext* cc,
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 Sepezd6ae2af2017-02-16 11:49:55 -0800246bool Document::addField(IJS_EventContext* cc,
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 Sepezd6ae2af2017-02-16 11:49:55 -0800254bool Document::exportAsText(IJS_EventContext* cc,
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 Sepezd6ae2af2017-02-16 11:49:55 -0800262bool Document::exportAsFDF(IJS_EventContext* cc,
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 Sepezd6ae2af2017-02-16 11:49:55 -0800270bool Document::exportAsXFDF(IJS_EventContext* cc,
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 Sepezd6ae2af2017-02-16 11:49:55 -0800283bool Document::getField(IJS_EventContext* cc,
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 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800295 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tsepezb4694242016-08-15 16:44:55 -0700296 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700297 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
299 if (pPDFForm->CountFields(wideName) <= 0) {
tsepezf3dc8c62016-08-10 06:29:29 -0700300 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700301 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302 }
303
tsepezf3dc8c62016-08-10 06:29:29 -0700304 v8::Local<v8::Object> pFieldObj =
tsepezb4694242016-08-15 16:44:55 -0700305 pRuntime->NewFxDynamicObj(CJS_Field::g_nObjDefnID);
tsepezf3dc8c62016-08-10 06:29:29 -0700306 CJS_Field* pJSField =
tsepezb4694242016-08-15 16:44:55 -0700307 static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
tsepezf3dc8c62016-08-10 06:29:29 -0700308 Field* pField = static_cast<Field*>(pJSField->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309 pField->AttachField(this, wideName);
310
tsepezf3dc8c62016-08-10 06:29:29 -0700311 vRet = CJS_Value(pRuntime, pJSField);
tsepez4cf55152016-11-02 14:37:54 -0700312 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313}
314
315// Gets the name of the nth field in the document
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800316bool Document::getNthFieldName(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700317 const std::vector<CJS_Value>& params,
318 CJS_Value& vRet,
319 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700320 if (params.size() != 1) {
321 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700322 return false;
tsepezcd5dc852016-09-08 11:23:24 -0700323 }
dsinclair82e17672016-10-11 12:38:01 -0700324 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700325 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700326 return false;
tsepez55be02e2016-09-12 11:21:42 -0700327 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800328 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tsepezb4694242016-08-15 16:44:55 -0700329 int nIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 if (nIndex < 0) {
tsepezcd5dc852016-09-08 11:23:24 -0700331 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700332 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700334 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700336 CPDF_FormField* pField = pPDFForm->GetField(nIndex, CFX_WideString());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 if (!pField)
tsepez4cf55152016-11-02 14:37:54 -0700338 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700339
tsepezf3dc8c62016-08-10 06:29:29 -0700340 vRet = CJS_Value(pRuntime, pField->GetFullName().c_str());
tsepez4cf55152016-11-02 14:37:54 -0700341 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342}
343
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800344bool Document::importAnFDF(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700345 const std::vector<CJS_Value>& params,
346 CJS_Value& vRet,
347 CFX_WideString& sError) {
348 // Unsafe, not supported.
349 return true;
350}
351
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800352bool Document::importAnXFDF(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700353 const std::vector<CJS_Value>& params,
354 CJS_Value& vRet,
355 CFX_WideString& sError) {
356 // Unsafe, not supported.
357 return true;
358}
359
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800360bool Document::importTextData(IJS_EventContext* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800361 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362 CJS_Value& vRet,
363 CFX_WideString& sError) {
364 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700365 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366}
367
368// exports the form data and mails the resulting fdf file as an attachment to
369// all recipients.
370// comment: need reader supports
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800371bool Document::mailForm(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700372 const std::vector<CJS_Value>& params,
373 CJS_Value& vRet,
374 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700375 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700376 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700377 return false;
tsepez55be02e2016-09-12 11:21:42 -0700378 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700379 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -0700380 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700381 return false;
tsepez55be02e2016-09-12 11:21:42 -0700382 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800383 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384 int iLength = params.size();
tsepez4cf55152016-11-02 14:37:54 -0700385 bool bUI = iLength > 0 ? params[0].ToBool(pRuntime) : true;
tsepezb4694242016-08-15 16:44:55 -0700386 CFX_WideString cTo = iLength > 1 ? params[1].ToCFXWideString(pRuntime) : L"";
387 CFX_WideString cCc = iLength > 2 ? params[2].ToCFXWideString(pRuntime) : L"";
388 CFX_WideString cBcc = iLength > 3 ? params[3].ToCFXWideString(pRuntime) : L"";
tsepezf3dc8c62016-08-10 06:29:29 -0700389 CFX_WideString cSubject =
tsepezb4694242016-08-15 16:44:55 -0700390 iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
391 CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
dsinclair7cbe68e2016-10-12 11:56:23 -0700392 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393 CFX_ByteTextBuf textBuf;
394 if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
tsepez4cf55152016-11-02 14:37:54 -0700395 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 pRuntime->BeginBlock();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800398 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
dsinclair8779fa82016-10-12 12:05:44 -0700399 pFormFillEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI,
400 cTo.c_str(), cSubject.c_str(), cCc.c_str(),
401 cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700403 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404}
405
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800406bool Document::print(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700407 const std::vector<CJS_Value>& params,
408 CJS_Value& vRet,
409 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700410 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700411 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700412 return false;
tsepez55be02e2016-09-12 11:21:42 -0700413 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800414 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tsepez4cf55152016-11-02 14:37:54 -0700415 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416 int nStart = 0;
417 int nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -0700418 bool bSilent = false;
419 bool bShrinkToFit = false;
420 bool bPrintAsImage = false;
421 bool bReverse = false;
422 bool bAnnotations = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423 int nlength = params.size();
424 if (nlength == 9) {
tsepez40faa792016-07-15 17:58:02 -0700425 if (params[8].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700426 v8::Local<v8::Object> pObj = params[8].ToV8Object(pRuntime);
427 if (CFXJS_Engine::GetObjDefnID(pObj) ==
428 CJS_PrintParamsObj::g_nObjDefnID) {
429 if (CJS_Object* pJSObj = params[8].ToCJSObject(pRuntime)) {
tsepez40faa792016-07-15 17:58:02 -0700430 if (PrintParamsObj* pprintparamsObj =
431 static_cast<PrintParamsObj*>(pJSObj->GetEmbedObject())) {
432 bUI = pprintparamsObj->bUI;
433 nStart = pprintparamsObj->nStart;
434 nEnd = pprintparamsObj->nEnd;
435 bSilent = pprintparamsObj->bSilent;
436 bShrinkToFit = pprintparamsObj->bShrinkToFit;
437 bPrintAsImage = pprintparamsObj->bPrintAsImage;
438 bReverse = pprintparamsObj->bReverse;
439 bAnnotations = pprintparamsObj->bAnnotations;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700441 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 }
443 }
444 } else {
445 if (nlength >= 1)
tsepezb4694242016-08-15 16:44:55 -0700446 bUI = params[0].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447 if (nlength >= 2)
tsepezb4694242016-08-15 16:44:55 -0700448 nStart = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449 if (nlength >= 3)
tsepezb4694242016-08-15 16:44:55 -0700450 nEnd = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700451 if (nlength >= 4)
tsepezb4694242016-08-15 16:44:55 -0700452 bSilent = params[3].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 if (nlength >= 5)
tsepezb4694242016-08-15 16:44:55 -0700454 bShrinkToFit = params[4].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455 if (nlength >= 6)
tsepezb4694242016-08-15 16:44:55 -0700456 bPrintAsImage = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457 if (nlength >= 7)
tsepezb4694242016-08-15 16:44:55 -0700458 bReverse = params[6].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459 if (nlength >= 8)
tsepezb4694242016-08-15 16:44:55 -0700460 bAnnotations = params[7].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461 }
462
dsinclair82e17672016-10-11 12:38:01 -0700463 if (m_pFormFillEnv) {
464 m_pFormFillEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit,
465 bPrintAsImage, bReverse, bAnnotations);
tsepez4cf55152016-11-02 14:37:54 -0700466 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467 }
tsepez4cf55152016-11-02 14:37:54 -0700468 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469}
470
471// removes the specified field from the document.
472// comment:
dsinclair1897bdc2016-09-06 14:18:57 -0700473// note: if the filed name is not rational, adobe is dumb for it.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800475bool Document::removeField(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700476 const std::vector<CJS_Value>& params,
477 CJS_Value& vRet,
478 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700479 if (params.size() != 1) {
480 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700481 return false;
tsepez55be02e2016-09-12 11:21:42 -0700482 }
dsinclair82e17672016-10-11 12:38:01 -0700483 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700484 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700485 return false;
tsepez55be02e2016-09-12 11:21:42 -0700486 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700487 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
488 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM))) {
tsepezcd5dc852016-09-08 11:23:24 -0700489 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700490 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700491 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800492 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tsepezb4694242016-08-15 16:44:55 -0700493 CFX_WideString sFieldName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700494 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
tsepez8fa82792017-01-11 09:32:33 -0800495 std::vector<CPDFSDK_Annot::ObservedPtr> widgets;
Lei Zhangd88a3642015-11-10 09:38:57 -0800496 pInterForm->GetWidgets(sFieldName, &widgets);
Lei Zhangd88a3642015-11-10 09:38:57 -0800497 if (widgets.empty())
tsepez4cf55152016-11-02 14:37:54 -0700498 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700499
tsepez8fa82792017-01-11 09:32:33 -0800500 for (const auto& pAnnot : widgets) {
501 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot.Get());
502 if (!pWidget)
503 continue;
504
Tom Sepez281a9ea2016-02-26 14:24:28 -0800505 CFX_FloatRect rcAnnot = pWidget->GetRect();
Lei Zhangd88a3642015-11-10 09:38:57 -0800506 --rcAnnot.left;
507 --rcAnnot.bottom;
508 ++rcAnnot.right;
509 ++rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510
tsepezdf964df2016-04-21 12:09:41 -0700511 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800512 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Lei Zhangd88a3642015-11-10 09:38:57 -0800513 ASSERT(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700514
dsinclair1897bdc2016-09-06 14:18:57 -0700515 // If there is currently no pageview associated with the page being used
516 // do not create one. We may be in the process of tearing down the document
517 // and creating a new pageview at this point will cause bad things.
dsinclair7cbe68e2016-10-12 11:56:23 -0700518 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, false);
dsinclair1897bdc2016-09-06 14:18:57 -0700519 if (pPageView) {
dsinclair8afe15a2016-10-05 12:00:34 -0700520#if PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700521 pPageView->DeleteAnnot(pWidget);
dsinclair8afe15a2016-10-05 12:00:34 -0700522#endif // PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700523 pPageView->UpdateRects(aRefresh);
524 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700526 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527
tsepez4cf55152016-11-02 14:37:54 -0700528 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700529}
530
531// reset filed values within a document.
532// comment:
533// note: if the fields names r not rational, aodbe is dumb for it.
534
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800535bool Document::resetForm(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700536 const std::vector<CJS_Value>& params,
537 CJS_Value& vRet,
538 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700539 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700540 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700541 return false;
tsepez55be02e2016-09-12 11:21:42 -0700542 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700543 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
544 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
545 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -0700546 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700547 return false;
tsepez55be02e2016-09-12 11:21:42 -0700548 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700549
dsinclair7cbe68e2016-10-12 11:56:23 -0700550 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700551 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
tsepeze5aff742016-08-08 09:49:42 -0700552 CJS_Array aName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700553
Lei Zhangdd734de2015-11-11 10:16:52 -0800554 if (params.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700555 pPDFForm->ResetForm(true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700556 m_pFormFillEnv->SetChangeMark();
tsepez4cf55152016-11-02 14:37:54 -0700557 return true;
Lei Zhangd88a3642015-11-10 09:38:57 -0800558 }
559
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800560 CJS_EventContext* pContext = static_cast<CJS_EventContext*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700561 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -0700562
Lei Zhangd88a3642015-11-10 09:38:57 -0800563 switch (params[0].GetType()) {
564 default:
tsepezb4694242016-08-15 16:44:55 -0700565 aName.Attach(params[0].ToV8Array(pRuntime));
Lei Zhangd88a3642015-11-10 09:38:57 -0800566 break;
567 case CJS_Value::VT_string:
tsepezb4694242016-08-15 16:44:55 -0700568 aName.SetElement(pRuntime, 0, params[0]);
Lei Zhangd88a3642015-11-10 09:38:57 -0800569 break;
570 }
571
572 std::vector<CPDF_FormField*> aFields;
tsepezb4694242016-08-15 16:44:55 -0700573 for (int i = 0, isz = aName.GetLength(pRuntime); i < isz; ++i) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800574 CJS_Value valElement(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700575 aName.GetElement(pRuntime, i, valElement);
576 CFX_WideString swVal = valElement.ToCFXWideString(pRuntime);
Lei Zhangd88a3642015-11-10 09:38:57 -0800577 for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j)
578 aFields.push_back(pPDFForm->GetField(j, swVal));
579 }
580
581 if (!aFields.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700582 pPDFForm->ResetForm(aFields, true, true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700583 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584 }
585
tsepez4cf55152016-11-02 14:37:54 -0700586 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700587}
588
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800589bool Document::saveAs(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700590 const std::vector<CJS_Value>& params,
591 CJS_Value& vRet,
592 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700593 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700594 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700595}
596
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800597bool Document::syncAnnotScan(IJS_EventContext* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800598 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599 CJS_Value& vRet,
600 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700601 return true;
602}
603
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800604bool Document::submitForm(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700605 const std::vector<CJS_Value>& params,
606 CJS_Value& vRet,
607 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700608 int nSize = params.size();
609 if (nSize < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700610 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700611 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700612 }
dsinclair82e17672016-10-11 12:38:01 -0700613 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700614 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700615 return false;
tsepez55be02e2016-09-12 11:21:42 -0700616 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800617 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tsepeze5aff742016-08-08 09:49:42 -0700618 CJS_Array aFields;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619 CFX_WideString strURL;
tsepez4cf55152016-11-02 14:37:54 -0700620 bool bFDF = true;
621 bool bEmpty = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700622 CJS_Value v = params[0];
Tom Sepez39bfe122015-09-17 15:25:23 -0700623 if (v.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700624 strURL = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700625 if (nSize > 1)
tsepezb4694242016-08-15 16:44:55 -0700626 bFDF = params[1].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700627 if (nSize > 2)
tsepezb4694242016-08-15 16:44:55 -0700628 bEmpty = params[2].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700629 if (nSize > 3)
tsepezb4694242016-08-15 16:44:55 -0700630 aFields.Attach(params[3].ToV8Array(pRuntime));
Tom Sepez39bfe122015-09-17 15:25:23 -0700631 } else if (v.GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700632 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
633 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"cURL");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634 if (!pValue.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700635 strURL = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700636
tsepezb4694242016-08-15 16:44:55 -0700637 pValue = pRuntime->GetObjectProperty(pObj, L"bFDF");
638 bFDF = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700639
tsepezb4694242016-08-15 16:44:55 -0700640 pValue = pRuntime->GetObjectProperty(pObj, L"bEmpty");
641 bEmpty = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700642
tsepezb4694242016-08-15 16:44:55 -0700643 pValue = pRuntime->GetObjectProperty(pObj, L"aFields");
644 aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700645 }
646
dsinclair7cbe68e2016-10-12 11:56:23 -0700647 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700648 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
tsepezb4694242016-08-15 16:44:55 -0700649 if (aFields.GetLength(pRuntime) == 0 && bEmpty) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800650 if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700651 pRuntime->BeginBlock();
tsepez4cf55152016-11-02 14:37:54 -0700652 pInterForm->SubmitForm(strURL, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700653 pRuntime->EndBlock();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700654 }
tsepez4cf55152016-11-02 14:37:54 -0700655 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700656 }
657
Lei Zhangd88a3642015-11-10 09:38:57 -0800658 std::vector<CPDF_FormField*> fieldObjects;
tsepezb4694242016-08-15 16:44:55 -0700659 for (int i = 0, sz = aFields.GetLength(pRuntime); i < sz; ++i) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700660 CJS_Value valName(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700661 aFields.GetElement(pRuntime, i, valName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700662
tsepezb4694242016-08-15 16:44:55 -0700663 CFX_WideString sName = valName.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700664 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
665 for (int j = 0, jsz = pPDFForm->CountFields(sName); j < jsz; ++j) {
666 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
667 if (!bEmpty && pField->GetValue().IsEmpty())
668 continue;
669
Lei Zhangd88a3642015-11-10 09:38:57 -0800670 fieldObjects.push_back(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700671 }
672 }
673
Lei Zhangd88a3642015-11-10 09:38:57 -0800674 if (pPDFInterForm->CheckRequiredFields(&fieldObjects, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 pRuntime->BeginBlock();
Wei Li97da9762016-03-11 17:00:48 -0800676 pInterForm->SubmitFields(strURL, fieldObjects, true, !bFDF);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700677 pRuntime->EndBlock();
678 }
tsepez4cf55152016-11-02 14:37:54 -0700679 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700680}
681
dsinclair82e17672016-10-11 12:38:01 -0700682void Document::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
683 m_pFormFillEnv.Reset(pFormFillEnv);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700684}
685
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800686bool Document::bookmarkRoot(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700687 CJS_PropValue& vp,
688 CFX_WideString& sError) {
689 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700690}
691
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800692bool Document::mailDoc(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700693 const std::vector<CJS_Value>& params,
694 CJS_Value& vRet,
695 CFX_WideString& sError) {
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800696 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700697
698 // TODO(tsepez): Check maximum number of allowed params.
tsepez4cf55152016-11-02 14:37:54 -0700699 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700700 CFX_WideString cTo = L"";
701 CFX_WideString cCc = L"";
702 CFX_WideString cBcc = L"";
703 CFX_WideString cSubject = L"";
704 CFX_WideString cMsg = L"";
705
tsepezf3dc8c62016-08-10 06:29:29 -0700706 if (params.size() >= 1)
tsepezb4694242016-08-15 16:44:55 -0700707 bUI = params[0].ToBool(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700708 if (params.size() >= 2)
tsepezb4694242016-08-15 16:44:55 -0700709 cTo = params[1].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700710 if (params.size() >= 3)
tsepezb4694242016-08-15 16:44:55 -0700711 cCc = params[2].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700712 if (params.size() >= 4)
tsepezb4694242016-08-15 16:44:55 -0700713 cBcc = params[3].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700714 if (params.size() >= 5)
tsepezb4694242016-08-15 16:44:55 -0700715 cSubject = params[4].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700716 if (params.size() >= 6)
tsepezb4694242016-08-15 16:44:55 -0700717 cMsg = params[5].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718
Tom Sepez39bfe122015-09-17 15:25:23 -0700719 if (params.size() >= 1 && params[0].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700720 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700721
tsepezb4694242016-08-15 16:44:55 -0700722 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"bUI");
tsepez71ba5882016-10-28 09:35:49 -0700723 bUI = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700724
tsepezb4694242016-08-15 16:44:55 -0700725 pValue = pRuntime->GetObjectProperty(pObj, L"cTo");
726 cTo = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700727
tsepezb4694242016-08-15 16:44:55 -0700728 pValue = pRuntime->GetObjectProperty(pObj, L"cCc");
729 cCc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700730
tsepezb4694242016-08-15 16:44:55 -0700731 pValue = pRuntime->GetObjectProperty(pObj, L"cBcc");
732 cBcc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700733
tsepezb4694242016-08-15 16:44:55 -0700734 pValue = pRuntime->GetObjectProperty(pObj, L"cSubject");
735 cSubject = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700736
tsepezb4694242016-08-15 16:44:55 -0700737 pValue = pRuntime->GetObjectProperty(pObj, L"cMsg");
738 cMsg = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700739 }
740
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700741 pRuntime->BeginBlock();
dsinclair8779fa82016-10-12 12:05:44 -0700742 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
743 pFormFillEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
744 cCc.c_str(), cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700745 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700746 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700747}
748
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800749bool Document::author(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700750 CJS_PropValue& vp,
751 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700752 return getPropertyInternal(cc, vp, "Author", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700753}
754
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800755bool Document::info(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700756 CJS_PropValue& vp,
757 CFX_WideString& sError) {
tonikitooec268762016-08-10 12:29:28 -0700758 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700759 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700760 return false;
tonikitooec268762016-08-10 12:29:28 -0700761 }
dsinclair82e17672016-10-11 12:38:01 -0700762 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700763 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700764 return false;
tsepez55be02e2016-09-12 11:21:42 -0700765 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700766 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700767 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700768 return false;
Tom Sepezc6ab1722015-02-05 15:27:25 -0800769
dsinclair38fd8442016-09-15 10:15:32 -0700770 CFX_WideString cwAuthor = pDictionary->GetUnicodeTextFor("Author");
771 CFX_WideString cwTitle = pDictionary->GetUnicodeTextFor("Title");
772 CFX_WideString cwSubject = pDictionary->GetUnicodeTextFor("Subject");
773 CFX_WideString cwKeywords = pDictionary->GetUnicodeTextFor("Keywords");
774 CFX_WideString cwCreator = pDictionary->GetUnicodeTextFor("Creator");
775 CFX_WideString cwProducer = pDictionary->GetUnicodeTextFor("Producer");
776 CFX_WideString cwCreationDate =
777 pDictionary->GetUnicodeTextFor("CreationDate");
778 CFX_WideString cwModDate = pDictionary->GetUnicodeTextFor("ModDate");
779 CFX_WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped");
Tom Sepezc6ab1722015-02-05 15:27:25 -0800780
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800781 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tsepezb4694242016-08-15 16:44:55 -0700782 v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1);
tsepeze6cf0132017-01-18 14:38:18 -0800783 pRuntime->PutObjectProperty(pObj, L"Author", pRuntime->NewString(cwAuthor));
784 pRuntime->PutObjectProperty(pObj, L"Title", pRuntime->NewString(cwTitle));
785 pRuntime->PutObjectProperty(pObj, L"Subject", pRuntime->NewString(cwSubject));
786 pRuntime->PutObjectProperty(pObj, L"Keywords",
787 pRuntime->NewString(cwKeywords));
788 pRuntime->PutObjectProperty(pObj, L"Creator", pRuntime->NewString(cwCreator));
789 pRuntime->PutObjectProperty(pObj, L"Producer",
790 pRuntime->NewString(cwProducer));
791 pRuntime->PutObjectProperty(pObj, L"CreationDate",
792 pRuntime->NewString(cwCreationDate));
793 pRuntime->PutObjectProperty(pObj, L"ModDate", pRuntime->NewString(cwModDate));
794 pRuntime->PutObjectProperty(pObj, L"Trapped", pRuntime->NewString(cwTrapped));
tonikitooec268762016-08-10 12:29:28 -0700795
796 // It's to be compatible to non-standard info dictionary.
797 for (const auto& it : *pDictionary) {
798 const CFX_ByteString& bsKey = it.first;
tsepez0e606b52016-11-18 16:22:41 -0800799 CPDF_Object* pValueObj = it.second.get();
tonikitooec268762016-08-10 12:29:28 -0700800 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
801 if (pValueObj->IsString() || pValueObj->IsName()) {
tsepeze6cf0132017-01-18 14:38:18 -0800802 pRuntime->PutObjectProperty(
803 pObj, wsKey, pRuntime->NewString(pValueObj->GetUnicodeText()));
tonikitooec268762016-08-10 12:29:28 -0700804 } else if (pValueObj->IsNumber()) {
tsepeze6cf0132017-01-18 14:38:18 -0800805 pRuntime->PutObjectProperty(pObj, wsKey,
806 pRuntime->NewNumber(pValueObj->GetNumber()));
tonikitooec268762016-08-10 12:29:28 -0700807 } else if (pValueObj->IsBoolean()) {
tsepeze6cf0132017-01-18 14:38:18 -0800808 pRuntime->PutObjectProperty(
809 pObj, wsKey, pRuntime->NewBoolean(!!pValueObj->GetInteger()));
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700810 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700811 }
tonikitooec268762016-08-10 12:29:28 -0700812 vp << pObj;
tsepez4cf55152016-11-02 14:37:54 -0700813 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700814}
815
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800816bool Document::getPropertyInternal(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700817 CJS_PropValue& vp,
818 const CFX_ByteString& propName,
819 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700820 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700821 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700822 return false;
tsepez55be02e2016-09-12 11:21:42 -0700823 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700824 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700825 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700826 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700827
828 if (vp.IsGetting()) {
dsinclair38fd8442016-09-15 10:15:32 -0700829 vp << pDictionary->GetUnicodeTextFor(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700830 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700831 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
tsepez55be02e2016-09-12 11:21:42 -0700832 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700833 return false;
tsepez55be02e2016-09-12 11:21:42 -0700834 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700835 CFX_WideString csProperty;
836 vp >> csProperty;
tsepez0e606b52016-11-18 16:22:41 -0800837 pDictionary->SetNewFor<CPDF_String>(propName, PDF_EncodeText(csProperty),
838 false);
dsinclair7cbe68e2016-10-12 11:56:23 -0700839 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700840 }
tsepez4cf55152016-11-02 14:37:54 -0700841 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700842}
843
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800844bool Document::creationDate(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700845 CJS_PropValue& vp,
846 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700847 return getPropertyInternal(cc, vp, "CreationDate", sError);
848}
849
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800850bool Document::creator(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700851 CJS_PropValue& vp,
852 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700853 return getPropertyInternal(cc, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700854}
855
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800856bool Document::delay(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700857 CJS_PropValue& vp,
858 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700859 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700860 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700861 return false;
tsepez55be02e2016-09-12 11:21:42 -0700862 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700863 if (vp.IsGetting()) {
864 vp << m_bDelay;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800865 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800867 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
868 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
869 return false;
870 }
871 vp >> m_bDelay;
872 if (m_bDelay) {
873 m_DelayData.clear();
874 return true;
875 }
876 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
877 DelayDataToProcess.swap(m_DelayData);
878 for (const auto& pData : DelayDataToProcess)
879 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
880
tsepez4cf55152016-11-02 14:37:54 -0700881 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700882}
883
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800884bool Document::keywords(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700885 CJS_PropValue& vp,
886 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700887 return getPropertyInternal(cc, vp, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700888}
889
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800890bool Document::modDate(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700891 CJS_PropValue& vp,
892 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700893 return getPropertyInternal(cc, vp, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894}
895
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800896bool Document::producer(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700897 CJS_PropValue& vp,
898 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700899 return getPropertyInternal(cc, vp, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700900}
901
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800902bool Document::subject(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700903 CJS_PropValue& vp,
904 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700905 return getPropertyInternal(cc, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700906}
907
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800908bool Document::title(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700909 CJS_PropValue& vp,
910 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700911 if (!m_pFormFillEnv || !m_pFormFillEnv->GetUnderlyingDocument()) {
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 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700915 return getPropertyInternal(cc, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700916}
917
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800918bool Document::numPages(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700919 CJS_PropValue& vp,
920 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700921 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700922 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700923 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700924 }
dsinclair82e17672016-10-11 12:38:01 -0700925 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700926 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700927 return false;
tsepez55be02e2016-09-12 11:21:42 -0700928 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700929 vp << m_pFormFillEnv->GetPageCount();
tsepez4cf55152016-11-02 14:37:54 -0700930 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700931}
932
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800933bool Document::external(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700934 CJS_PropValue& vp,
935 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700936 // In Chrome case, should always return true.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937 if (vp.IsGetting()) {
938 vp << true;
939 }
tsepez4cf55152016-11-02 14:37:54 -0700940 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941}
942
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800943bool Document::filesize(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700944 CJS_PropValue& vp,
945 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700946 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700947 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700948 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700949 }
950 vp << 0;
tsepez4cf55152016-11-02 14:37:54 -0700951 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700952}
953
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800954bool Document::mouseX(IJS_EventContext* cc,
tonikitoo33c4cdb2016-08-08 10:52:51 -0700955 CJS_PropValue& vp,
956 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700957 return true;
958}
959
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800960bool Document::mouseY(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700961 CJS_PropValue& vp,
962 CFX_WideString& sError) {
963 return true;
964}
965
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800966bool Document::URL(IJS_EventContext* cc,
967 CJS_PropValue& vp,
968 CFX_WideString& sError) {
tonikitoo33c4cdb2016-08-08 10:52:51 -0700969 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700970 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700971 return false;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700972 }
dsinclair82e17672016-10-11 12:38:01 -0700973 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700974 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700975 return false;
tsepez55be02e2016-09-12 11:21:42 -0700976 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700977 vp << m_pFormFillEnv->JS_docGetFilePath();
tsepez4cf55152016-11-02 14:37:54 -0700978 return true;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700979}
980
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800981bool Document::baseURL(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700982 CJS_PropValue& vp,
983 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700984 if (vp.IsGetting()) {
985 vp << m_cwBaseURL;
986 } else {
987 vp >> m_cwBaseURL;
988 }
tsepez4cf55152016-11-02 14:37:54 -0700989 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700990}
991
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800992bool Document::calculate(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -0700993 CJS_PropValue& vp,
994 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700995 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700996 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700997 return false;
tsepez55be02e2016-09-12 11:21:42 -0700998 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700999 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -07001001 vp << !!pInterForm->IsCalculateEnabled();
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001002 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001003 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001004 bool bCalculate;
1005 vp >> bCalculate;
1006 pInterForm->EnableCalculate(bCalculate);
tsepez4cf55152016-11-02 14:37:54 -07001007 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008}
1009
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001010bool Document::documentFileName(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001011 CJS_PropValue& vp,
1012 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001013 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001014 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001015 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001016 }
dsinclair82e17672016-10-11 12:38:01 -07001017 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001018 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001019 return false;
tsepez55be02e2016-09-12 11:21:42 -07001020 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001021 CFX_WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022 int32_t i = wsFilePath.GetLength() - 1;
1023 for (; i >= 0; i--) {
1024 if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
1025 break;
1026 }
1027 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
1028 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
1029 } else {
1030 vp << L"";
1031 }
tsepez4cf55152016-11-02 14:37:54 -07001032 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001033}
1034
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001035bool Document::path(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001036 CJS_PropValue& vp,
1037 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001038 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001039 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001040 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001041 }
dsinclair82e17672016-10-11 12:38:01 -07001042 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001043 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001044 return false;
tsepez55be02e2016-09-12 11:21:42 -07001045 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001046 vp << app::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath());
tsepez4cf55152016-11-02 14:37:54 -07001047 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001048}
1049
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001050bool Document::pageWindowRect(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001051 CJS_PropValue& vp,
1052 CFX_WideString& sError) {
1053 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001054}
1055
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001056bool Document::layout(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001057 CJS_PropValue& vp,
1058 CFX_WideString& sError) {
1059 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001060}
1061
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001062bool Document::addLink(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001063 const std::vector<CJS_Value>& params,
1064 CJS_Value& vRet,
1065 CFX_WideString& sError) {
1066 return true;
1067}
1068
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001069bool Document::closeDoc(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001070 const std::vector<CJS_Value>& params,
1071 CJS_Value& vRet,
1072 CFX_WideString& sError) {
1073 return true;
1074}
1075
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001076bool Document::getPageBox(IJS_EventContext* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001077 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001078 CJS_Value& vRet,
1079 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001080 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001081}
1082
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001083bool Document::getAnnot(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001084 const std::vector<CJS_Value>& params,
1085 CJS_Value& vRet,
1086 CFX_WideString& sError) {
tonikitoo618cb1f2016-08-18 20:10:17 -07001087 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001088 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001089 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001090 }
dsinclair82e17672016-10-11 12:38:01 -07001091 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001092 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001093 return false;
tsepez55be02e2016-09-12 11:21:42 -07001094 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001095 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tonikitoo618cb1f2016-08-18 20:10:17 -07001096 int nPageNo = params[0].ToInt(pRuntime);
1097 CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -07001098 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(nPageNo);
tonikitoo618cb1f2016-08-18 20:10:17 -07001099 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001100 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001101
tsepezd805eec2017-01-11 14:03:54 -08001102 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
tonikitoo618cb1f2016-08-18 20:10:17 -07001103 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
tsepezd805eec2017-01-11 14:03:54 -08001104 for (const auto& pSDKAnnotCur : annotIteration) {
1105 CPDFSDK_BAAnnot* pBAAnnot =
1106 static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get());
tonikitoo618cb1f2016-08-18 20:10:17 -07001107 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1108 pSDKBAAnnot = pBAAnnot;
1109 break;
1110 }
1111 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001112 if (!pSDKBAAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001113 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001114
1115 v8::Local<v8::Object> pObj =
1116 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1117 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001118 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001119
1120 CJS_Annot* pJS_Annot =
1121 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1122 if (!pJS_Annot)
tsepez4cf55152016-11-02 14:37:54 -07001123 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001124
1125 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1126 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001127 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001128
1129 pAnnot->SetSDKAnnot(pSDKBAAnnot);
tonikitoo618cb1f2016-08-18 20:10:17 -07001130 vRet = CJS_Value(pRuntime, pJS_Annot);
tsepez4cf55152016-11-02 14:37:54 -07001131 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001132}
1133
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001134bool Document::getAnnots(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001135 const std::vector<CJS_Value>& params,
1136 CJS_Value& vRet,
1137 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001138 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001139 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001140 return false;
tsepez55be02e2016-09-12 11:21:42 -07001141 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001142 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tonikitoo0ee35902016-08-26 14:41:29 -07001143
1144 // TODO(tonikitoo): Add support supported parameters as per
1145 // the PDF spec.
1146
dsinclair7cbe68e2016-10-12 11:56:23 -07001147 int nPageNo = m_pFormFillEnv->GetPageCount();
tonikitoo0ee35902016-08-26 14:41:29 -07001148 CJS_Array annots;
1149
1150 for (int i = 0; i < nPageNo; ++i) {
dsinclair7cbe68e2016-10-12 11:56:23 -07001151 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(i);
tonikitoo0ee35902016-08-26 14:41:29 -07001152 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001153 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001154
tsepezd805eec2017-01-11 14:03:54 -08001155 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
1156 for (const auto& pSDKAnnotCur : annotIteration) {
1157 if (!pSDKAnnotCur) {
1158 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001159 return false;
tsepezd805eec2017-01-11 14:03:54 -08001160 }
tonikitoo0ee35902016-08-26 14:41:29 -07001161 v8::Local<v8::Object> pObj =
1162 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1163 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001164 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001165
1166 CJS_Annot* pJS_Annot =
1167 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1168 if (!pJS_Annot)
tsepez4cf55152016-11-02 14:37:54 -07001169 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001170
1171 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1172 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001173 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001174
tsepezd805eec2017-01-11 14:03:54 -08001175 pAnnot->SetSDKAnnot(static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get()));
tonikitoo0ee35902016-08-26 14:41:29 -07001176 annots.SetElement(pRuntime, i, CJS_Value(pRuntime, pJS_Annot));
1177 }
1178 }
tonikitoo0ee35902016-08-26 14:41:29 -07001179 vRet = CJS_Value(pRuntime, annots);
tsepez4cf55152016-11-02 14:37:54 -07001180 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001181}
1182
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001183bool Document::getAnnot3D(IJS_EventContext* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001184 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001185 CJS_Value& vRet,
1186 CFX_WideString& sError) {
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001187 vRet.SetNull(CJS_Runtime::FromEventContext(cc));
tsepez4cf55152016-11-02 14:37:54 -07001188 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001189}
1190
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001191bool Document::getAnnots3D(IJS_EventContext* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001192 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193 CJS_Value& vRet,
1194 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001195 return true;
1196}
1197
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001198bool Document::getOCGs(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001199 const std::vector<CJS_Value>& params,
1200 CJS_Value& vRet,
1201 CFX_WideString& sError) {
1202 return true;
1203}
1204
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001205bool Document::getLinks(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001206 const std::vector<CJS_Value>& params,
1207 CJS_Value& vRet,
1208 CFX_WideString& sError) {
1209 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001210}
1211
1212bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1213 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1214 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1215}
1216
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001217bool Document::addIcon(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001218 const std::vector<CJS_Value>& params,
1219 CJS_Value& vRet,
1220 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001221 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001222 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001223 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001224 }
tsepezf3dc8c62016-08-10 06:29:29 -07001225
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001226 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001227 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Tom Sepez39bfe122015-09-17 15:25:23 -07001228 if (params[1].GetType() != CJS_Value::VT_object) {
tsepezcd5dc852016-09-08 11:23:24 -07001229 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001230 return false;
Tom Sepezaecd9d02015-08-31 15:04:12 -07001231 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001232
tsepezb4694242016-08-15 16:44:55 -07001233 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pRuntime);
1234 if (pRuntime->GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
tsepezcd5dc852016-09-08 11:23:24 -07001235 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001236 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001237 }
1238
tsepezb4694242016-08-15 16:44:55 -07001239 CJS_EmbedObj* pEmbedObj = params[1].ToCJSObject(pRuntime)->GetEmbedObject();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001240 if (!pEmbedObj) {
tsepezcd5dc852016-09-08 11:23:24 -07001241 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001242 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001243 }
1244
thestigdadea5f2016-10-19 13:29:48 -07001245 m_Icons.push_back(pdfium::MakeUnique<IconElement>(
1246 swIconName, static_cast<Icon*>(pEmbedObj)));
tsepez4cf55152016-11-02 14:37:54 -07001247 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001248}
1249
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001250bool Document::icons(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001251 CJS_PropValue& vp,
1252 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001253 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001254 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001255 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001256 }
1257
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001258 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
thestigdadea5f2016-10-19 13:29:48 -07001259 if (m_Icons.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001260 vp.GetJSValue()->SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001261 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001262 }
1263
tsepeze5aff742016-08-08 09:49:42 -07001264 CJS_Array Icons;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001265
Tom Sepez6ba32d92016-01-05 16:26:32 -08001266 int i = 0;
thestigdadea5f2016-10-19 13:29:48 -07001267 for (const auto& pIconElement : m_Icons) {
tsepezb4694242016-08-15 16:44:55 -07001268 v8::Local<v8::Object> pObj =
1269 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001270 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001271 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001272
tsepezb4694242016-08-15 16:44:55 -07001273 CJS_Icon* pJS_Icon =
1274 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001275 if (!pJS_Icon)
tsepez4cf55152016-11-02 14:37:54 -07001276 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277
thestigdadea5f2016-10-19 13:29:48 -07001278 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001279 if (!pIcon)
tsepez4cf55152016-11-02 14:37:54 -07001280 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001281
dsinclair09bad1c2016-10-18 19:26:30 -07001282 pIcon->SetStream(pIconElement->IconStream->GetStream());
1283 pIcon->SetIconName(pIconElement->IconName);
tsepezb4694242016-08-15 16:44:55 -07001284 Icons.SetElement(pRuntime, i++, CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001285 }
1286
1287 vp << Icons;
tsepez4cf55152016-11-02 14:37:54 -07001288 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001289}
1290
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001291bool Document::getIcon(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001292 const std::vector<CJS_Value>& params,
1293 CJS_Value& vRet,
1294 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001295 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001296 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001297 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001298 }
1299
thestigdadea5f2016-10-19 13:29:48 -07001300 if (m_Icons.empty())
tsepez4cf55152016-11-02 14:37:54 -07001301 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001302
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001303 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001304 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001305
thestigdadea5f2016-10-19 13:29:48 -07001306 for (const auto& pIconElement : m_Icons) {
1307 if (pIconElement->IconName != swIconName)
1308 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001309
thestigdadea5f2016-10-19 13:29:48 -07001310 v8::Local<v8::Object> pObj =
1311 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
1312 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001313 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001314
thestigdadea5f2016-10-19 13:29:48 -07001315 CJS_Icon* pJS_Icon =
1316 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
1317 if (!pJS_Icon)
tsepez4cf55152016-11-02 14:37:54 -07001318 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001319
thestigdadea5f2016-10-19 13:29:48 -07001320 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1321 if (!pIcon)
tsepez4cf55152016-11-02 14:37:54 -07001322 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001323
thestigdadea5f2016-10-19 13:29:48 -07001324 pIcon->SetIconName(swIconName);
1325 pIcon->SetStream(pIconElement->IconStream->GetStream());
1326
1327 vRet = CJS_Value(pRuntime, pJS_Icon);
tsepez4cf55152016-11-02 14:37:54 -07001328 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001329 }
1330
tsepez4cf55152016-11-02 14:37:54 -07001331 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001332}
1333
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001334bool Document::removeIcon(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001335 const std::vector<CJS_Value>& params,
1336 CJS_Value& vRet,
1337 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001338 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001339 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001340}
1341
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001342bool Document::createDataObject(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001343 const std::vector<CJS_Value>& params,
1344 CJS_Value& vRet,
1345 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001346 // Unsafe, not implemented.
tsepez4cf55152016-11-02 14:37:54 -07001347 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001348}
1349
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001350bool Document::media(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001351 CJS_PropValue& vp,
1352 CFX_WideString& sError) {
1353 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001354}
1355
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001356bool Document::calculateNow(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001357 const std::vector<CJS_Value>& params,
1358 CJS_Value& vRet,
1359 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001360 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001361 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001362 return false;
tsepez55be02e2016-09-12 11:21:42 -07001363 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001364 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
1365 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
1366 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -07001367 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001368 return false;
tsepez55be02e2016-09-12 11:21:42 -07001369 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001370 m_pFormFillEnv->GetInterForm()->OnCalculate();
tsepez4cf55152016-11-02 14:37:54 -07001371 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001372}
1373
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001374bool Document::Collab(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001375 CJS_PropValue& vp,
1376 CFX_WideString& sError) {
1377 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001378}
1379
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001380bool Document::getPageNthWord(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001381 const std::vector<CJS_Value>& params,
1382 CJS_Value& vRet,
1383 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001384 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001385 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001386 return false;
tsepez55be02e2016-09-12 11:21:42 -07001387 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001388 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001389 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001390 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001391 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001392 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tsepez55be02e2016-09-12 11:21:42 -07001393
1394 // TODO(tsepez): check maximum allowable params.
1395
tsepezb4694242016-08-15 16:44:55 -07001396 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
1397 int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
1398 bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001399
dsinclair7cbe68e2016-10-12 11:56:23 -07001400 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001401 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001402 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001403
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001404 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001405 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001406 return false;
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 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1410 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001411 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001412
thestig5cc24652016-04-26 11:46:02 -07001413 CPDF_Page page(pDocument, pPageDict, true);
1414 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001415
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001416 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001417 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001418 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001419 if (pPageObj->IsText()) {
1420 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001421 int nObjWords = CountWords(pTextObj);
1422 if (nWords + nObjWords >= nWordNo) {
1423 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1424 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001425 }
Tom Sepez2398d892016-02-17 16:46:26 -08001426 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001427 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001428 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001429
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001430 if (bStrip) {
1431 swRet.TrimLeft();
1432 swRet.TrimRight();
1433 }
1434
tsepezf3dc8c62016-08-10 06:29:29 -07001435 vRet = CJS_Value(pRuntime, swRet.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001436 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001437}
1438
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001439bool Document::getPageNthWordQuads(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001440 const std::vector<CJS_Value>& params,
1441 CJS_Value& vRet,
1442 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001443 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001444 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001445 return false;
tsepez55be02e2016-09-12 11:21:42 -07001446 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001447 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -07001448 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001449 return false;
tsepez55be02e2016-09-12 11:21:42 -07001450 }
tsepez4cf55152016-11-02 14:37:54 -07001451 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001452}
1453
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001454bool Document::getPageNumWords(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001455 const std::vector<CJS_Value>& params,
1456 CJS_Value& vRet,
1457 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001458 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001459 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001460 return false;
tsepez55be02e2016-09-12 11:21:42 -07001461 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001462 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001463 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001464 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001465 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001466 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001467 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
dsinclair7cbe68e2016-10-12 11:56:23 -07001468 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001469 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001470 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001471 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001472 }
1473
1474 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1475 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001476 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001477
thestig5cc24652016-04-26 11:46:02 -07001478 CPDF_Page page(pDocument, pPageDict, true);
1479 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001480
1481 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001482 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001483 if (pPageObj->IsText())
1484 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001485 }
1486
tsepezf3dc8c62016-08-10 06:29:29 -07001487 vRet = CJS_Value(pRuntime, nWords);
tsepez4cf55152016-11-02 14:37:54 -07001488 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001489}
1490
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001491bool Document::getPrintParams(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001492 const std::vector<CJS_Value>& params,
1493 CJS_Value& vRet,
1494 CFX_WideString& sError) {
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001495 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001496 v8::Local<v8::Object> pRetObj =
1497 pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001498
1499 // Not implemented yet.
1500
tsepezf3dc8c62016-08-10 06:29:29 -07001501 vRet = CJS_Value(pRuntime, pRetObj);
tsepez4cf55152016-11-02 14:37:54 -07001502 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001503}
1504
1505#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1506
1507int Document::CountWords(CPDF_TextObject* pTextObj) {
1508 if (!pTextObj)
1509 return 0;
1510
1511 int nWords = 0;
1512
1513 CPDF_Font* pFont = pTextObj->GetFont();
1514 if (!pFont)
1515 return 0;
1516
tsepez4cf55152016-11-02 14:37:54 -07001517 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001518
1519 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001520 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001521 FX_FLOAT kerning;
1522
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001523 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001524 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1525
Tom Sepez62a70f92016-03-21 15:00:20 -07001526 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001527 if (swUnicode.GetLength() > 0)
1528 unicode = swUnicode[0];
1529
1530 if (ISLATINWORD(unicode) && bIsLatin)
1531 continue;
1532
1533 bIsLatin = ISLATINWORD(unicode);
1534 if (unicode != 0x20)
1535 nWords++;
1536 }
1537
1538 return nWords;
1539}
1540
1541CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1542 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001543 CFX_WideString swRet;
1544
1545 CPDF_Font* pFont = pTextObj->GetFont();
1546 if (!pFont)
1547 return L"";
1548
1549 int nWords = 0;
tsepez4cf55152016-11-02 14:37:54 -07001550 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001551
1552 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001553 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001554 FX_FLOAT kerning;
1555
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001556 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001557 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1558
Tom Sepez62a70f92016-03-21 15:00:20 -07001559 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001560 if (swUnicode.GetLength() > 0)
1561 unicode = swUnicode[0];
1562
1563 if (ISLATINWORD(unicode) && bIsLatin) {
1564 } else {
1565 bIsLatin = ISLATINWORD(unicode);
1566 if (unicode != 0x20)
1567 nWords++;
1568 }
1569
1570 if (nWords - 1 == nWordIndex)
1571 swRet += unicode;
1572 }
1573
1574 return swRet;
1575}
1576
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001577bool Document::zoom(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001578 CJS_PropValue& vp,
1579 CFX_WideString& sError) {
1580 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001581}
1582
1583/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001584(none, NoVary)
1585(fitP, FitPage)
1586(fitW, FitWidth)
1587(fitH, FitHeight)
1588(fitV, FitVisibleWidth)
1589(pref, Preferred)
1590(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001591*/
1592
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001593bool Document::zoomType(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001594 CJS_PropValue& vp,
1595 CFX_WideString& sError) {
1596 return true;
1597}
1598
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001599bool Document::deletePages(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001600 const std::vector<CJS_Value>& params,
1601 CJS_Value& vRet,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001602 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001603 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001604 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001605}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001606
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001607bool Document::extractPages(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001608 const std::vector<CJS_Value>& params,
1609 CJS_Value& vRet,
1610 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001611 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001612 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001613}
1614
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001615bool Document::insertPages(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001616 const std::vector<CJS_Value>& params,
1617 CJS_Value& vRet,
1618 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001619 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001620 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001621}
1622
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001623bool Document::replacePages(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001624 const std::vector<CJS_Value>& params,
1625 CJS_Value& vRet,
1626 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001627 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001628 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001629}
1630
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001631bool Document::getURL(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001632 const std::vector<CJS_Value>& params,
1633 CJS_Value& vRet,
1634 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001635 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001636 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001637}
1638
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001639bool Document::gotoNamedDest(IJS_EventContext* cc,
tsepez4cf55152016-11-02 14:37:54 -07001640 const std::vector<CJS_Value>& params,
1641 CJS_Value& vRet,
1642 CFX_WideString& sError) {
tonikitoo1c836752016-08-08 16:14:05 -07001643 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001644 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001645 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001646 }
dsinclair82e17672016-10-11 12:38:01 -07001647 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001648 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001649 return false;
tsepez55be02e2016-09-12 11:21:42 -07001650 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001651 CJS_Runtime* pRuntime = CJS_Runtime::FromEventContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001652 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -07001653 CFX_ByteString utf8Name = wideName.UTF8Encode();
dsinclair7cbe68e2016-10-12 11:56:23 -07001654 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
tonikitoo1c836752016-08-08 16:14:05 -07001655 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001656 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001657
tonikitoo1c836752016-08-08 16:14:05 -07001658 CPDF_NameTree nameTree(pDocument, "Dests");
1659 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, utf8Name);
1660 if (!destArray)
tsepez4cf55152016-11-02 14:37:54 -07001661 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001662
1663 CPDF_Dest dest(destArray);
1664 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
1665
1666 std::unique_ptr<float[]> scrollPositionArray;
1667 int scrollPositionArraySize = 0;
1668
1669 if (arrayObject) {
1670 scrollPositionArray.reset(new float[arrayObject->GetCount()]);
1671 int j = 0;
1672 for (size_t i = 2; i < arrayObject->GetCount(); i++)
1673 scrollPositionArray[j++] = arrayObject->GetFloatAt(i);
1674 scrollPositionArraySize = j;
1675 }
1676
tsepezb4694242016-08-15 16:44:55 -07001677 pRuntime->BeginBlock();
dsinclair82e17672016-10-11 12:38:01 -07001678 m_pFormFillEnv->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
1679 scrollPositionArray.get(),
1680 scrollPositionArraySize);
tsepezb4694242016-08-15 16:44:55 -07001681 pRuntime->EndBlock();
tonikitoo1c836752016-08-08 16:14:05 -07001682
tsepez4cf55152016-11-02 14:37:54 -07001683 return true;
tonikitoo1c836752016-08-08 16:14:05 -07001684}
1685
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001686void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001687 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001688}
1689
1690void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1691 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001692 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1693 auto iter = m_DelayData.begin();
1694 while (iter != m_DelayData.end()) {
1695 auto old = iter++;
1696 if ((*old)->sFieldName == sFieldName &&
1697 (*old)->nControlIndex == nControlIndex) {
1698 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1699 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001700 }
1701 }
1702
dsinclair82e17672016-10-11 12:38:01 -07001703 for (const auto& pData : DelayDataForFieldAndControlIndex)
1704 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001705}
1706
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001707CJS_Document* Document::GetCJSDoc() const {
1708 return static_cast<CJS_Document*>(m_pJSObject);
JUN FANG33f6f0d2015-04-06 12:39:51 -07001709}