blob: db1b5b0809050f488a06e53396e9e94b3e1fd8ff [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"
dsinclairc6c425a2016-09-29 12:01:30 -070013#include "core/fpdfapi/fpdf_parser/cpdf_array.h"
14#include "core/fpdfapi/fpdf_parser/cpdf_document.h"
15#include "core/fpdfapi/fpdf_parser/fpdf_parser_decode.h"
dsinclair41872fa2016-10-04 11:29:35 -070016#include "core/fpdfapi/page/cpdf_page.h"
dsinclair1727aee2016-09-29 13:12:56 -070017#include "core/fpdfdoc/cpdf_interform.h"
18#include "core/fpdfdoc/cpdf_nametree.h"
dsinclair114e46a2016-09-29 17:18:21 -070019#include "fpdfsdk/cpdfsdk_annotiterator.h"
20#include "fpdfsdk/cpdfsdk_document.h"
21#include "fpdfsdk/cpdfsdk_environment.h"
22#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"
dsinclair64376be2016-03-31 20:03:24 -070033#include "fpdfsdk/javascript/cjs_context.h"
34#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"
Chris Palmer9108ad22014-06-26 16:01:46 -070037
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038BEGIN_JS_STATIC_CONST(CJS_PrintParamsObj)
39END_JS_STATIC_CONST()
40
41BEGIN_JS_STATIC_PROP(CJS_PrintParamsObj)
42END_JS_STATIC_PROP()
43
44BEGIN_JS_STATIC_METHOD(CJS_PrintParamsObj)
45END_JS_STATIC_METHOD()
46
47IMPLEMENT_JS_CLASS(CJS_PrintParamsObj, PrintParamsObj)
48
49PrintParamsObj::PrintParamsObj(CJS_Object* pJSObject)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 : CJS_EmbedObj(pJSObject) {
51 bUI = TRUE;
52 nStart = 0;
53 nEnd = 0;
54 bSilent = FALSE;
55 bShrinkToFit = FALSE;
56 bPrintAsImage = FALSE;
57 bReverse = FALSE;
58 bAnnotations = TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059}
60
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061#define MINWIDTH 5.0f
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062#define MINHEIGHT 5.0f
63
64BEGIN_JS_STATIC_CONST(CJS_Document)
65END_JS_STATIC_CONST()
66
67BEGIN_JS_STATIC_PROP(CJS_Document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068JS_STATIC_PROP_ENTRY(ADBE)
69JS_STATIC_PROP_ENTRY(author)
70JS_STATIC_PROP_ENTRY(baseURL)
71JS_STATIC_PROP_ENTRY(bookmarkRoot)
72JS_STATIC_PROP_ENTRY(calculate)
73JS_STATIC_PROP_ENTRY(Collab)
74JS_STATIC_PROP_ENTRY(creationDate)
75JS_STATIC_PROP_ENTRY(creator)
76JS_STATIC_PROP_ENTRY(delay)
77JS_STATIC_PROP_ENTRY(dirty)
78JS_STATIC_PROP_ENTRY(documentFileName)
79JS_STATIC_PROP_ENTRY(external)
80JS_STATIC_PROP_ENTRY(filesize)
81JS_STATIC_PROP_ENTRY(icons)
82JS_STATIC_PROP_ENTRY(info)
83JS_STATIC_PROP_ENTRY(keywords)
84JS_STATIC_PROP_ENTRY(layout)
85JS_STATIC_PROP_ENTRY(media)
86JS_STATIC_PROP_ENTRY(modDate)
87JS_STATIC_PROP_ENTRY(mouseX)
88JS_STATIC_PROP_ENTRY(mouseY)
89JS_STATIC_PROP_ENTRY(numFields)
90JS_STATIC_PROP_ENTRY(numPages)
91JS_STATIC_PROP_ENTRY(pageNum)
92JS_STATIC_PROP_ENTRY(pageWindowRect)
93JS_STATIC_PROP_ENTRY(path)
94JS_STATIC_PROP_ENTRY(producer)
95JS_STATIC_PROP_ENTRY(subject)
96JS_STATIC_PROP_ENTRY(title)
tonikitoo33c4cdb2016-08-08 10:52:51 -070097JS_STATIC_PROP_ENTRY(URL)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098JS_STATIC_PROP_ENTRY(zoom)
99JS_STATIC_PROP_ENTRY(zoomType)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100END_JS_STATIC_PROP()
101
102BEGIN_JS_STATIC_METHOD(CJS_Document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103JS_STATIC_METHOD_ENTRY(addAnnot)
104JS_STATIC_METHOD_ENTRY(addField)
105JS_STATIC_METHOD_ENTRY(addLink)
106JS_STATIC_METHOD_ENTRY(addIcon)
107JS_STATIC_METHOD_ENTRY(calculateNow)
108JS_STATIC_METHOD_ENTRY(closeDoc)
109JS_STATIC_METHOD_ENTRY(createDataObject)
110JS_STATIC_METHOD_ENTRY(deletePages)
111JS_STATIC_METHOD_ENTRY(exportAsText)
112JS_STATIC_METHOD_ENTRY(exportAsFDF)
113JS_STATIC_METHOD_ENTRY(exportAsXFDF)
114JS_STATIC_METHOD_ENTRY(extractPages)
115JS_STATIC_METHOD_ENTRY(getAnnot)
116JS_STATIC_METHOD_ENTRY(getAnnots)
117JS_STATIC_METHOD_ENTRY(getAnnot3D)
118JS_STATIC_METHOD_ENTRY(getAnnots3D)
119JS_STATIC_METHOD_ENTRY(getField)
120JS_STATIC_METHOD_ENTRY(getIcon)
121JS_STATIC_METHOD_ENTRY(getLinks)
122JS_STATIC_METHOD_ENTRY(getNthFieldName)
123JS_STATIC_METHOD_ENTRY(getOCGs)
124JS_STATIC_METHOD_ENTRY(getPageBox)
125JS_STATIC_METHOD_ENTRY(getPageNthWord)
126JS_STATIC_METHOD_ENTRY(getPageNthWordQuads)
127JS_STATIC_METHOD_ENTRY(getPageNumWords)
128JS_STATIC_METHOD_ENTRY(getPrintParams)
129JS_STATIC_METHOD_ENTRY(getURL)
tonikitoo1c836752016-08-08 16:14:05 -0700130JS_STATIC_METHOD_ENTRY(gotoNamedDest)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131JS_STATIC_METHOD_ENTRY(importAnFDF)
132JS_STATIC_METHOD_ENTRY(importAnXFDF)
133JS_STATIC_METHOD_ENTRY(importTextData)
134JS_STATIC_METHOD_ENTRY(insertPages)
135JS_STATIC_METHOD_ENTRY(mailForm)
136JS_STATIC_METHOD_ENTRY(print)
137JS_STATIC_METHOD_ENTRY(removeField)
138JS_STATIC_METHOD_ENTRY(replacePages)
139JS_STATIC_METHOD_ENTRY(resetForm)
140JS_STATIC_METHOD_ENTRY(removeIcon)
141JS_STATIC_METHOD_ENTRY(saveAs)
142JS_STATIC_METHOD_ENTRY(submitForm)
tonikitoobb5fa042016-08-19 11:18:29 -0700143JS_STATIC_METHOD_ENTRY(syncAnnotScan)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144JS_STATIC_METHOD_ENTRY(mailDoc)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700145END_JS_STATIC_METHOD()
146
147IMPLEMENT_JS_CLASS(CJS_Document, Document)
148
Tom Sepez33420902015-10-13 15:00:10 -0700149void CJS_Document::InitInstance(IJS_Runtime* pIRuntime) {
150 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime);
151 Document* pDoc = static_cast<Document*>(GetEmbedObject());
152 pDoc->AttachDoc(pRuntime->GetReaderDocument());
Tom Sepez33420902015-10-13 15:00:10 -0700153}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155Document::Document(CJS_Object* pJSObject)
156 : CJS_EmbedObj(pJSObject),
thestig1cd352e2016-06-07 17:53:06 -0700157 m_pDocument(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 m_cwBaseURL(L""),
159 m_bDelay(FALSE) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161Document::~Document() {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162}
163
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164// the total number of fileds in document.
Tom Sepezba038bc2015-10-08 12:03:00 -0700165FX_BOOL Document::numFields(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 CJS_PropValue& vp,
167 CFX_WideString& sError) {
168 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700169 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700170 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 }
tsepez55be02e2016-09-12 11:21:42 -0700172 if (!m_pDocument) {
173 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
174 return FALSE;
175 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
177 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700178 vp << static_cast<int>(pPDFForm->CountFields(CFX_WideString()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180}
181
Tom Sepezba038bc2015-10-08 12:03:00 -0700182FX_BOOL Document::dirty(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 CJS_PropValue& vp,
184 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700185 if (!m_pDocument) {
186 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
187 return FALSE;
188 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -0700190 vp << !!m_pDocument->GetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 } else {
192 bool bChanged = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 vp >> bChanged;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 if (bChanged)
196 m_pDocument->SetChangeMark();
197 else
198 m_pDocument->ClearChangeMark();
199 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201}
202
Tom Sepezba038bc2015-10-08 12:03:00 -0700203FX_BOOL Document::ADBE(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 CJS_PropValue& vp,
205 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700206 if (vp.IsGetting())
207 vp.GetJSValue()->SetNull(CJS_Runtime::FromContext(cc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208
209 return TRUE;
210}
211
Tom Sepezba038bc2015-10-08 12:03:00 -0700212FX_BOOL Document::pageNum(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 CJS_PropValue& vp,
214 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700215 if (!m_pDocument) {
216 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
217 return FALSE;
218 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 if (vp.IsGetting()) {
220 if (CPDFSDK_PageView* pPageView = m_pDocument->GetCurrentView()) {
221 vp << pPageView->GetPageIndex();
222 }
223 } else {
224 int iPageCount = m_pDocument->GetPageCount();
225 int iPageNum = 0;
226 vp >> iPageNum;
227
dsinclair79db6092016-09-14 07:27:21 -0700228 CPDFSDK_Environment* pEnv = m_pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 if (iPageNum >= 0 && iPageNum < iPageCount) {
230 pEnv->JS_docgotoPage(iPageNum);
231 } else if (iPageNum >= iPageCount) {
232 pEnv->JS_docgotoPage(iPageCount - 1);
233 } else if (iPageNum < 0) {
234 pEnv->JS_docgotoPage(0);
235 }
236 }
237
238 return TRUE;
239}
240
Tom Sepezba038bc2015-10-08 12:03:00 -0700241FX_BOOL Document::addAnnot(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800242 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 CJS_Value& vRet,
244 CFX_WideString& sError) {
245 // Not supported.
246 return TRUE;
247}
248
Tom Sepezba038bc2015-10-08 12:03:00 -0700249FX_BOOL Document::addField(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800250 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 CJS_Value& vRet,
252 CFX_WideString& sError) {
253 // Not supported.
254 return TRUE;
255}
256
Tom Sepezba038bc2015-10-08 12:03:00 -0700257FX_BOOL Document::exportAsText(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800258 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 CJS_Value& vRet,
260 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -0800261 // Unsafe, not supported.
262 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263}
264
Tom Sepezba038bc2015-10-08 12:03:00 -0700265FX_BOOL Document::exportAsFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800266 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 CJS_Value& vRet,
268 CFX_WideString& sError) {
269 // Unsafe, not supported.
270 return TRUE;
271}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272
Tom Sepezba038bc2015-10-08 12:03:00 -0700273FX_BOOL Document::exportAsXFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800274 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275 CJS_Value& vRet,
276 CFX_WideString& sError) {
277 // Unsafe, not supported.
278 return TRUE;
279}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700280
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281// Maps a field object in PDF document to a JavaScript variable
282// comment:
283// note: the paremter cName, this is clue how to treat if the cName is not a
284// valiable filed name in this document
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285
Tom Sepezba038bc2015-10-08 12:03:00 -0700286FX_BOOL Document::getField(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800287 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 CJS_Value& vRet,
289 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700291 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 return FALSE;
293 }
tsepez55be02e2016-09-12 11:21:42 -0700294 if (!m_pDocument) {
295 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
296 return FALSE;
297 }
tsepezcd5dc852016-09-08 11:23:24 -0700298 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700299 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -0700300 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
302 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
303 if (pPDFForm->CountFields(wideName) <= 0) {
tsepezf3dc8c62016-08-10 06:29:29 -0700304 vRet.SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 return TRUE;
306 }
307
tsepezf3dc8c62016-08-10 06:29:29 -0700308 v8::Local<v8::Object> pFieldObj =
tsepezb4694242016-08-15 16:44:55 -0700309 pRuntime->NewFxDynamicObj(CJS_Field::g_nObjDefnID);
tsepezf3dc8c62016-08-10 06:29:29 -0700310 CJS_Field* pJSField =
tsepezb4694242016-08-15 16:44:55 -0700311 static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
tsepezf3dc8c62016-08-10 06:29:29 -0700312 Field* pField = static_cast<Field*>(pJSField->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 pField->AttachField(this, wideName);
314
tsepezf3dc8c62016-08-10 06:29:29 -0700315 vRet = CJS_Value(pRuntime, pJSField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316 return TRUE;
317}
318
319// Gets the name of the nth field in the document
Tom Sepezba038bc2015-10-08 12:03:00 -0700320FX_BOOL Document::getNthFieldName(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800321 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 CJS_Value& vRet,
323 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700324 if (params.size() != 1) {
325 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
326 return FALSE;
327 }
tsepez55be02e2016-09-12 11:21:42 -0700328 if (!m_pDocument) {
329 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
330 return FALSE;
331 }
tsepezf3dc8c62016-08-10 06:29:29 -0700332 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezb4694242016-08-15 16:44:55 -0700333 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -0700334 int nIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 if (nIndex < 0) {
tsepezcd5dc852016-09-08 11:23:24 -0700336 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 return FALSE;
338 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700339 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
340 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700341 CPDF_FormField* pField = pPDFForm->GetField(nIndex, CFX_WideString());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 if (!pField)
343 return FALSE;
344
tsepezf3dc8c62016-08-10 06:29:29 -0700345 vRet = CJS_Value(pRuntime, pField->GetFullName().c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700346 return TRUE;
347}
348
Tom Sepezba038bc2015-10-08 12:03:00 -0700349FX_BOOL Document::importAnFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800350 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 CJS_Value& vRet,
352 CFX_WideString& sError) {
353 // Unsafe, not supported.
354 return TRUE;
355}
356
Tom Sepezba038bc2015-10-08 12:03:00 -0700357FX_BOOL Document::importAnXFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800358 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 CJS_Value& vRet,
360 CFX_WideString& sError) {
361 // Unsafe, not supported.
362 return TRUE;
363}
364
Tom Sepezba038bc2015-10-08 12:03:00 -0700365FX_BOOL Document::importTextData(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800366 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 CJS_Value& vRet,
368 CFX_WideString& sError) {
369 // Unsafe, not supported.
370 return TRUE;
371}
372
373// exports the form data and mails the resulting fdf file as an attachment to
374// all recipients.
375// comment: need reader supports
376// note:
377// int CPDFSDK_Document::mailForm(FX_BOOL bUI,String cto,string ccc,string
378// cbcc,string cSubject,string cms);
379
Tom Sepezba038bc2015-10-08 12:03:00 -0700380FX_BOOL Document::mailForm(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800381 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382 CJS_Value& vRet,
383 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700384 if (!m_pDocument) {
385 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
386 return FALSE;
387 }
388 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
389 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
390 return FALSE;
391 }
392
tsepezf3dc8c62016-08-10 06:29:29 -0700393 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezb4694242016-08-15 16:44:55 -0700394 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -0700395
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 int iLength = params.size();
tsepezb4694242016-08-15 16:44:55 -0700397 FX_BOOL bUI = iLength > 0 ? params[0].ToBool(pRuntime) : TRUE;
398 CFX_WideString cTo = iLength > 1 ? params[1].ToCFXWideString(pRuntime) : L"";
399 CFX_WideString cCc = iLength > 2 ? params[2].ToCFXWideString(pRuntime) : L"";
400 CFX_WideString cBcc = iLength > 3 ? params[3].ToCFXWideString(pRuntime) : L"";
tsepezf3dc8c62016-08-10 06:29:29 -0700401 CFX_WideString cSubject =
tsepezb4694242016-08-15 16:44:55 -0700402 iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
403 CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404
tsepez23ae4a52016-06-08 20:44:54 -0700405 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 CFX_ByteTextBuf textBuf;
407 if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
408 return FALSE;
409
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410 pRuntime->BeginBlock();
dsinclair8e0638b2016-09-22 11:06:02 -0700411 CPDFSDK_Environment* pEnv = pContext->GetReaderEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412 pEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI,
413 cTo.c_str(), cSubject.c_str(), cCc.c_str(), cBcc.c_str(),
414 cMsg.c_str());
415 pRuntime->EndBlock();
416 return TRUE;
417}
418
Tom Sepezba038bc2015-10-08 12:03:00 -0700419FX_BOOL Document::print(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800420 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421 CJS_Value& vRet,
422 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700423 if (!m_pDocument) {
424 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
425 return FALSE;
426 }
427
tsepezf3dc8c62016-08-10 06:29:29 -0700428 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
429 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -0700430
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 FX_BOOL bUI = TRUE;
432 int nStart = 0;
433 int nEnd = 0;
434 FX_BOOL bSilent = FALSE;
435 FX_BOOL bShrinkToFit = FALSE;
436 FX_BOOL bPrintAsImage = FALSE;
437 FX_BOOL bReverse = FALSE;
438 FX_BOOL bAnnotations = FALSE;
439
440 int nlength = params.size();
441 if (nlength == 9) {
tsepez40faa792016-07-15 17:58:02 -0700442 if (params[8].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700443 v8::Local<v8::Object> pObj = params[8].ToV8Object(pRuntime);
444 if (CFXJS_Engine::GetObjDefnID(pObj) ==
445 CJS_PrintParamsObj::g_nObjDefnID) {
446 if (CJS_Object* pJSObj = params[8].ToCJSObject(pRuntime)) {
tsepez40faa792016-07-15 17:58:02 -0700447 if (PrintParamsObj* pprintparamsObj =
448 static_cast<PrintParamsObj*>(pJSObj->GetEmbedObject())) {
449 bUI = pprintparamsObj->bUI;
450 nStart = pprintparamsObj->nStart;
451 nEnd = pprintparamsObj->nEnd;
452 bSilent = pprintparamsObj->bSilent;
453 bShrinkToFit = pprintparamsObj->bShrinkToFit;
454 bPrintAsImage = pprintparamsObj->bPrintAsImage;
455 bReverse = pprintparamsObj->bReverse;
456 bAnnotations = pprintparamsObj->bAnnotations;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700458 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459 }
460 }
461 } else {
462 if (nlength >= 1)
tsepezb4694242016-08-15 16:44:55 -0700463 bUI = params[0].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700464 if (nlength >= 2)
tsepezb4694242016-08-15 16:44:55 -0700465 nStart = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466 if (nlength >= 3)
tsepezb4694242016-08-15 16:44:55 -0700467 nEnd = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700468 if (nlength >= 4)
tsepezb4694242016-08-15 16:44:55 -0700469 bSilent = params[3].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470 if (nlength >= 5)
tsepezb4694242016-08-15 16:44:55 -0700471 bShrinkToFit = params[4].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700472 if (nlength >= 6)
tsepezb4694242016-08-15 16:44:55 -0700473 bPrintAsImage = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474 if (nlength >= 7)
tsepezb4694242016-08-15 16:44:55 -0700475 bReverse = params[6].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700476 if (nlength >= 8)
tsepezb4694242016-08-15 16:44:55 -0700477 bAnnotations = params[7].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478 }
479
dsinclair79db6092016-09-14 07:27:21 -0700480 if (CPDFSDK_Environment* pEnv = m_pDocument->GetEnv()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481 pEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit, bPrintAsImage,
482 bReverse, bAnnotations);
483 return TRUE;
484 }
485 return FALSE;
486}
487
488// removes the specified field from the document.
489// comment:
dsinclair1897bdc2016-09-06 14:18:57 -0700490// note: if the filed name is not rational, adobe is dumb for it.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700491
Tom Sepezba038bc2015-10-08 12:03:00 -0700492FX_BOOL Document::removeField(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800493 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494 CJS_Value& vRet,
495 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700496 if (params.size() != 1) {
497 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
498 return FALSE;
499 }
500 if (!m_pDocument) {
501 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
502 return FALSE;
503 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700504 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
tsepezcd5dc852016-09-08 11:23:24 -0700505 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM))) {
506 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700507 return FALSE;
508 }
tsepezcd5dc852016-09-08 11:23:24 -0700509 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700510 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -0700511 CFX_WideString sFieldName = params[0].ToCFXWideString(pRuntime);
tsepez23ae4a52016-06-08 20:44:54 -0700512 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Lei Zhangd88a3642015-11-10 09:38:57 -0800513 std::vector<CPDFSDK_Widget*> widgets;
514 pInterForm->GetWidgets(sFieldName, &widgets);
Lei Zhangd88a3642015-11-10 09:38:57 -0800515 if (widgets.empty())
516 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700517
Lei Zhangd88a3642015-11-10 09:38:57 -0800518 for (CPDFSDK_Widget* pWidget : widgets) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800519 CFX_FloatRect rcAnnot = pWidget->GetRect();
Lei Zhangd88a3642015-11-10 09:38:57 -0800520 --rcAnnot.left;
521 --rcAnnot.bottom;
522 ++rcAnnot.right;
523 ++rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700524
tsepezdf964df2016-04-21 12:09:41 -0700525 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800526 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Lei Zhangd88a3642015-11-10 09:38:57 -0800527 ASSERT(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700528
dsinclair1897bdc2016-09-06 14:18:57 -0700529 // If there is currently no pageview associated with the page being used
530 // do not create one. We may be in the process of tearing down the document
531 // and creating a new pageview at this point will cause bad things.
532 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, false);
533 if (pPageView) {
534 pPageView->DeleteAnnot(pWidget);
535 pPageView->UpdateRects(aRefresh);
536 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700537 }
Lei Zhangd88a3642015-11-10 09:38:57 -0800538 m_pDocument->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700539
540 return TRUE;
541}
542
543// reset filed values within a document.
544// comment:
545// note: if the fields names r not rational, aodbe is dumb for it.
546
Tom Sepezba038bc2015-10-08 12:03:00 -0700547FX_BOOL Document::resetForm(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800548 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700549 CJS_Value& vRet,
550 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700551 if (!m_pDocument) {
552 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
553 return FALSE;
554 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700555 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
556 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM) ||
tsepez55be02e2016-09-12 11:21:42 -0700557 m_pDocument->GetPermissions(FPDFPERM_FILL_FORM))) {
558 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700559 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -0700560 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561
tsepez23ae4a52016-06-08 20:44:54 -0700562 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
tsepeze5aff742016-08-08 09:49:42 -0700564 CJS_Array aName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565
Lei Zhangdd734de2015-11-11 10:16:52 -0800566 if (params.empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700567 pPDFForm->ResetForm(TRUE);
568 m_pDocument->SetChangeMark();
Lei Zhangd88a3642015-11-10 09:38:57 -0800569 return TRUE;
570 }
571
tsepez55be02e2016-09-12 11:21:42 -0700572 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700573 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -0700574
Lei Zhangd88a3642015-11-10 09:38:57 -0800575 switch (params[0].GetType()) {
576 default:
tsepezb4694242016-08-15 16:44:55 -0700577 aName.Attach(params[0].ToV8Array(pRuntime));
Lei Zhangd88a3642015-11-10 09:38:57 -0800578 break;
579 case CJS_Value::VT_string:
tsepezb4694242016-08-15 16:44:55 -0700580 aName.SetElement(pRuntime, 0, params[0]);
Lei Zhangd88a3642015-11-10 09:38:57 -0800581 break;
582 }
583
584 std::vector<CPDF_FormField*> aFields;
tsepezb4694242016-08-15 16:44:55 -0700585 for (int i = 0, isz = aName.GetLength(pRuntime); i < isz; ++i) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800586 CJS_Value valElement(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700587 aName.GetElement(pRuntime, i, valElement);
588 CFX_WideString swVal = valElement.ToCFXWideString(pRuntime);
Lei Zhangd88a3642015-11-10 09:38:57 -0800589 for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j)
590 aFields.push_back(pPDFForm->GetField(j, swVal));
591 }
592
593 if (!aFields.empty()) {
594 pPDFForm->ResetForm(aFields, TRUE, TRUE);
595 m_pDocument->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700596 }
597
598 return TRUE;
599}
600
Tom Sepezba038bc2015-10-08 12:03:00 -0700601FX_BOOL Document::saveAs(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800602 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700603 CJS_Value& vRet,
604 CFX_WideString& sError) {
605 // Unsafe, not supported.
606 return TRUE;
607}
608
tonikitoobb5fa042016-08-19 11:18:29 -0700609FX_BOOL Document::syncAnnotScan(IJS_Context* cc,
610 const std::vector<CJS_Value>& params,
611 CJS_Value& vRet,
612 CFX_WideString& sError) {
613 return TRUE;
614}
615
Tom Sepezba038bc2015-10-08 12:03:00 -0700616FX_BOOL Document::submitForm(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800617 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700618 CJS_Value& vRet,
619 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700620 int nSize = params.size();
621 if (nSize < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700622 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700623 return FALSE;
624 }
tsepez55be02e2016-09-12 11:21:42 -0700625 if (!m_pDocument) {
626 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
627 return FALSE;
628 }
tsepezcd5dc852016-09-08 11:23:24 -0700629 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
630 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepeze5aff742016-08-08 09:49:42 -0700631 CJS_Array aFields;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700632 CFX_WideString strURL;
633 FX_BOOL bFDF = TRUE;
634 FX_BOOL bEmpty = FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700635 CJS_Value v = params[0];
Tom Sepez39bfe122015-09-17 15:25:23 -0700636 if (v.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700637 strURL = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700638 if (nSize > 1)
tsepezb4694242016-08-15 16:44:55 -0700639 bFDF = params[1].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640 if (nSize > 2)
tsepezb4694242016-08-15 16:44:55 -0700641 bEmpty = params[2].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700642 if (nSize > 3)
tsepezb4694242016-08-15 16:44:55 -0700643 aFields.Attach(params[3].ToV8Array(pRuntime));
Tom Sepez39bfe122015-09-17 15:25:23 -0700644 } else if (v.GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700645 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
646 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"cURL");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700647 if (!pValue.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700648 strURL = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700649
tsepezb4694242016-08-15 16:44:55 -0700650 pValue = pRuntime->GetObjectProperty(pObj, L"bFDF");
651 bFDF = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700652
tsepezb4694242016-08-15 16:44:55 -0700653 pValue = pRuntime->GetObjectProperty(pObj, L"bEmpty");
654 bEmpty = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700655
tsepezb4694242016-08-15 16:44:55 -0700656 pValue = pRuntime->GetObjectProperty(pObj, L"aFields");
657 aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700658 }
659
tsepez23ae4a52016-06-08 20:44:54 -0700660 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700661 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
tsepezb4694242016-08-15 16:44:55 -0700662 if (aFields.GetLength(pRuntime) == 0 && bEmpty) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800663 if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700664 pRuntime->BeginBlock();
665 pInterForm->SubmitForm(strURL, FALSE);
666 pRuntime->EndBlock();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700667 }
668 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700669 }
670
Lei Zhangd88a3642015-11-10 09:38:57 -0800671 std::vector<CPDF_FormField*> fieldObjects;
tsepezb4694242016-08-15 16:44:55 -0700672 for (int i = 0, sz = aFields.GetLength(pRuntime); i < sz; ++i) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700673 CJS_Value valName(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700674 aFields.GetElement(pRuntime, i, valName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675
tsepezb4694242016-08-15 16:44:55 -0700676 CFX_WideString sName = valName.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700677 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
678 for (int j = 0, jsz = pPDFForm->CountFields(sName); j < jsz; ++j) {
679 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
680 if (!bEmpty && pField->GetValue().IsEmpty())
681 continue;
682
Lei Zhangd88a3642015-11-10 09:38:57 -0800683 fieldObjects.push_back(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700684 }
685 }
686
Lei Zhangd88a3642015-11-10 09:38:57 -0800687 if (pPDFInterForm->CheckRequiredFields(&fieldObjects, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700688 pRuntime->BeginBlock();
Wei Li97da9762016-03-11 17:00:48 -0800689 pInterForm->SubmitFields(strURL, fieldObjects, true, !bFDF);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700690 pRuntime->EndBlock();
691 }
692 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700693}
694
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700695void Document::AttachDoc(CPDFSDK_Document* pDoc) {
tsepez55be02e2016-09-12 11:21:42 -0700696 m_pDocument.Reset(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700697}
698
Tom Sepezba038bc2015-10-08 12:03:00 -0700699FX_BOOL Document::bookmarkRoot(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700700 CJS_PropValue& vp,
701 CFX_WideString& sError) {
702 return TRUE;
703}
704
Tom Sepezba038bc2015-10-08 12:03:00 -0700705FX_BOOL Document::mailDoc(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800706 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700707 CJS_Value& vRet,
708 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700709 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
710
711 // TODO(tsepez): Check maximum number of allowed params.
712
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700713 FX_BOOL bUI = TRUE;
714 CFX_WideString cTo = L"";
715 CFX_WideString cCc = L"";
716 CFX_WideString cBcc = L"";
717 CFX_WideString cSubject = L"";
718 CFX_WideString cMsg = L"";
719
tsepezf3dc8c62016-08-10 06:29:29 -0700720 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700721
tsepezf3dc8c62016-08-10 06:29:29 -0700722 if (params.size() >= 1)
tsepezb4694242016-08-15 16:44:55 -0700723 bUI = params[0].ToBool(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700724 if (params.size() >= 2)
tsepezb4694242016-08-15 16:44:55 -0700725 cTo = params[1].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700726 if (params.size() >= 3)
tsepezb4694242016-08-15 16:44:55 -0700727 cCc = params[2].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700728 if (params.size() >= 4)
tsepezb4694242016-08-15 16:44:55 -0700729 cBcc = params[3].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700730 if (params.size() >= 5)
tsepezb4694242016-08-15 16:44:55 -0700731 cSubject = params[4].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700732 if (params.size() >= 6)
tsepezb4694242016-08-15 16:44:55 -0700733 cMsg = params[5].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700734
Tom Sepez39bfe122015-09-17 15:25:23 -0700735 if (params.size() >= 1 && params[0].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700736 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700737
tsepezb4694242016-08-15 16:44:55 -0700738 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"bUI");
739 bUI = CJS_Value(pRuntime, pValue).ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700740
tsepezb4694242016-08-15 16:44:55 -0700741 pValue = pRuntime->GetObjectProperty(pObj, L"cTo");
742 cTo = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700743
tsepezb4694242016-08-15 16:44:55 -0700744 pValue = pRuntime->GetObjectProperty(pObj, L"cCc");
745 cCc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700746
tsepezb4694242016-08-15 16:44:55 -0700747 pValue = pRuntime->GetObjectProperty(pObj, L"cBcc");
748 cBcc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700749
tsepezb4694242016-08-15 16:44:55 -0700750 pValue = pRuntime->GetObjectProperty(pObj, L"cSubject");
751 cSubject = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700752
tsepezb4694242016-08-15 16:44:55 -0700753 pValue = pRuntime->GetObjectProperty(pObj, L"cMsg");
754 cMsg = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700755 }
756
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700757 pRuntime->BeginBlock();
dsinclair8e0638b2016-09-22 11:06:02 -0700758 CPDFSDK_Environment* pEnv = pRuntime->GetReaderEnv();
thestig1cd352e2016-06-07 17:53:06 -0700759 pEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
760 cCc.c_str(), cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700761 pRuntime->EndBlock();
762
763 return TRUE;
764}
765
Tom Sepezba038bc2015-10-08 12:03:00 -0700766FX_BOOL Document::author(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700767 CJS_PropValue& vp,
768 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700769 return getPropertyInternal(cc, vp, "Author", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700770}
771
Tom Sepezba038bc2015-10-08 12:03:00 -0700772FX_BOOL Document::info(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700773 CJS_PropValue& vp,
774 CFX_WideString& sError) {
tonikitooec268762016-08-10 12:29:28 -0700775 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700776 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tonikitooec268762016-08-10 12:29:28 -0700777 return FALSE;
778 }
tsepez55be02e2016-09-12 11:21:42 -0700779 if (!m_pDocument) {
780 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
781 return FALSE;
782 }
Tom Sepez50d12ad2015-11-24 09:50:51 -0800783 CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700784 if (!pDictionary)
785 return FALSE;
Tom Sepezc6ab1722015-02-05 15:27:25 -0800786
dsinclair38fd8442016-09-15 10:15:32 -0700787 CFX_WideString cwAuthor = pDictionary->GetUnicodeTextFor("Author");
788 CFX_WideString cwTitle = pDictionary->GetUnicodeTextFor("Title");
789 CFX_WideString cwSubject = pDictionary->GetUnicodeTextFor("Subject");
790 CFX_WideString cwKeywords = pDictionary->GetUnicodeTextFor("Keywords");
791 CFX_WideString cwCreator = pDictionary->GetUnicodeTextFor("Creator");
792 CFX_WideString cwProducer = pDictionary->GetUnicodeTextFor("Producer");
793 CFX_WideString cwCreationDate =
794 pDictionary->GetUnicodeTextFor("CreationDate");
795 CFX_WideString cwModDate = pDictionary->GetUnicodeTextFor("ModDate");
796 CFX_WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped");
Tom Sepezc6ab1722015-02-05 15:27:25 -0800797
tonikitooec268762016-08-10 12:29:28 -0700798 CJS_Context* pContext = (CJS_Context*)cc;
799 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
Tom Sepezc6ab1722015-02-05 15:27:25 -0800800
tsepezb4694242016-08-15 16:44:55 -0700801 v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1);
802 pRuntime->PutObjectString(pObj, L"Author", cwAuthor);
803 pRuntime->PutObjectString(pObj, L"Title", cwTitle);
804 pRuntime->PutObjectString(pObj, L"Subject", cwSubject);
805 pRuntime->PutObjectString(pObj, L"Keywords", cwKeywords);
806 pRuntime->PutObjectString(pObj, L"Creator", cwCreator);
807 pRuntime->PutObjectString(pObj, L"Producer", cwProducer);
808 pRuntime->PutObjectString(pObj, L"CreationDate", cwCreationDate);
809 pRuntime->PutObjectString(pObj, L"ModDate", cwModDate);
810 pRuntime->PutObjectString(pObj, L"Trapped", cwTrapped);
tonikitooec268762016-08-10 12:29:28 -0700811
812 // It's to be compatible to non-standard info dictionary.
813 for (const auto& it : *pDictionary) {
814 const CFX_ByteString& bsKey = it.first;
815 CPDF_Object* pValueObj = it.second;
816 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
817 if (pValueObj->IsString() || pValueObj->IsName()) {
tsepezb4694242016-08-15 16:44:55 -0700818 pRuntime->PutObjectString(pObj, wsKey, pValueObj->GetUnicodeText());
tonikitooec268762016-08-10 12:29:28 -0700819 } else if (pValueObj->IsNumber()) {
tsepezb4694242016-08-15 16:44:55 -0700820 pRuntime->PutObjectNumber(pObj, wsKey, (float)pValueObj->GetNumber());
tonikitooec268762016-08-10 12:29:28 -0700821 } else if (pValueObj->IsBoolean()) {
tsepezb4694242016-08-15 16:44:55 -0700822 pRuntime->PutObjectBoolean(pObj, wsKey, !!pValueObj->GetInteger());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700823 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700824 }
tonikitooec268762016-08-10 12:29:28 -0700825 vp << pObj;
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700826 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700827}
828
tonikitooea3ff9e2016-08-02 11:52:28 -0700829FX_BOOL Document::getPropertyInternal(IJS_Context* cc,
830 CJS_PropValue& vp,
831 const CFX_ByteString& propName,
832 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700833 if (!m_pDocument) {
834 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
835 return FALSE;
836 }
Tom Sepez50d12ad2015-11-24 09:50:51 -0800837 CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700838 if (!pDictionary)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700839 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700840
841 if (vp.IsGetting()) {
dsinclair38fd8442016-09-15 10:15:32 -0700842 vp << pDictionary->GetUnicodeTextFor(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700843 } else {
tsepez55be02e2016-09-12 11:21:42 -0700844 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) {
845 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700846 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -0700847 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700848 CFX_WideString csProperty;
849 vp >> csProperty;
dsinclair38fd8442016-09-15 10:15:32 -0700850 pDictionary->SetStringFor(propName, PDF_EncodeText(csProperty));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700851 m_pDocument->SetChangeMark();
852 }
853 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700854}
855
tonikitooea3ff9e2016-08-02 11:52:28 -0700856FX_BOOL Document::creationDate(IJS_Context* cc,
857 CJS_PropValue& vp,
858 CFX_WideString& sError) {
859 return getPropertyInternal(cc, vp, "CreationDate", sError);
860}
861
Tom Sepezba038bc2015-10-08 12:03:00 -0700862FX_BOOL Document::creator(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700863 CJS_PropValue& vp,
864 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700865 return getPropertyInternal(cc, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700866}
867
Tom Sepezba038bc2015-10-08 12:03:00 -0700868FX_BOOL Document::delay(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700869 CJS_PropValue& vp,
870 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700871 if (!m_pDocument) {
872 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
873 return FALSE;
874 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700875 if (vp.IsGetting()) {
876 vp << m_bDelay;
877 } else {
tsepez55be02e2016-09-12 11:21:42 -0700878 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) {
879 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700880 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -0700881 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700882 vp >> m_bDelay;
883 if (m_bDelay) {
Tom Sepez56c10192016-03-15 12:34:17 -0700884 m_DelayData.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885 } else {
Tom Sepez56c10192016-03-15 12:34:17 -0700886 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
887 DelayDataToProcess.swap(m_DelayData);
888 for (const auto& pData : DelayDataToProcess)
tsepez55be02e2016-09-12 11:21:42 -0700889 Field::DoDelay(m_pDocument.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700890 }
891 }
892 return TRUE;
893}
894
Tom Sepezba038bc2015-10-08 12:03:00 -0700895FX_BOOL Document::keywords(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700896 CJS_PropValue& vp,
897 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700898 return getPropertyInternal(cc, vp, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899}
900
Tom Sepezba038bc2015-10-08 12:03:00 -0700901FX_BOOL Document::modDate(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700902 CJS_PropValue& vp,
903 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700904 return getPropertyInternal(cc, vp, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700905}
906
Tom Sepezba038bc2015-10-08 12:03:00 -0700907FX_BOOL Document::producer(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700908 CJS_PropValue& vp,
909 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700910 return getPropertyInternal(cc, vp, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700911}
912
Tom Sepezba038bc2015-10-08 12:03:00 -0700913FX_BOOL Document::subject(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700914 CJS_PropValue& vp,
915 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700916 return getPropertyInternal(cc, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917}
918
Tom Sepezba038bc2015-10-08 12:03:00 -0700919FX_BOOL Document::title(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700920 CJS_PropValue& vp,
921 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700922 if (!m_pDocument || !m_pDocument->GetUnderlyingDocument()) {
923 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700924 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -0700925 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700926 return getPropertyInternal(cc, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700927}
928
Tom Sepezba038bc2015-10-08 12:03:00 -0700929FX_BOOL Document::numPages(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700930 CJS_PropValue& vp,
931 CFX_WideString& sError) {
932 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700933 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700934 return FALSE;
935 }
tsepez55be02e2016-09-12 11:21:42 -0700936 if (!m_pDocument) {
937 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
938 return FALSE;
939 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700940 vp << m_pDocument->GetPageCount();
941 return TRUE;
942}
943
Tom Sepezba038bc2015-10-08 12:03:00 -0700944FX_BOOL Document::external(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945 CJS_PropValue& vp,
946 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700947 // In Chrome case, should always return true.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700948 if (vp.IsGetting()) {
949 vp << true;
950 }
951 return TRUE;
952}
953
Tom Sepezba038bc2015-10-08 12:03:00 -0700954FX_BOOL Document::filesize(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700955 CJS_PropValue& vp,
956 CFX_WideString& sError) {
957 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700958 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959 return FALSE;
960 }
961 vp << 0;
962 return TRUE;
963}
964
Tom Sepezba038bc2015-10-08 12:03:00 -0700965FX_BOOL Document::mouseX(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700966 CJS_PropValue& vp,
967 CFX_WideString& sError) {
968 return TRUE;
969}
970
Tom Sepezba038bc2015-10-08 12:03:00 -0700971FX_BOOL Document::mouseY(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700972 CJS_PropValue& vp,
973 CFX_WideString& sError) {
974 return TRUE;
975}
976
tonikitoo33c4cdb2016-08-08 10:52:51 -0700977FX_BOOL Document::URL(IJS_Context* cc,
978 CJS_PropValue& vp,
979 CFX_WideString& sError) {
980 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700981 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tonikitoo33c4cdb2016-08-08 10:52:51 -0700982 return FALSE;
983 }
tsepez55be02e2016-09-12 11:21:42 -0700984 if (!m_pDocument) {
985 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
986 return FALSE;
987 }
tonikitoo33c4cdb2016-08-08 10:52:51 -0700988 vp << m_pDocument->GetPath();
989 return TRUE;
990}
991
Tom Sepezba038bc2015-10-08 12:03:00 -0700992FX_BOOL Document::baseURL(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700993 CJS_PropValue& vp,
994 CFX_WideString& sError) {
995 if (vp.IsGetting()) {
996 vp << m_cwBaseURL;
997 } else {
998 vp >> m_cwBaseURL;
999 }
1000 return TRUE;
1001}
1002
Tom Sepezba038bc2015-10-08 12:03:00 -07001003FX_BOOL Document::calculate(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004 CJS_PropValue& vp,
1005 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001006 if (!m_pDocument) {
1007 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1008 return FALSE;
1009 }
tsepez23ae4a52016-06-08 20:44:54 -07001010 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -07001012 vp << !!pInterForm->IsCalculateEnabled();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001013 } else {
1014 bool bCalculate;
1015 vp >> bCalculate;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001016 pInterForm->EnableCalculate(bCalculate);
1017 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018 return TRUE;
1019}
1020
Tom Sepezba038bc2015-10-08 12:03:00 -07001021FX_BOOL Document::documentFileName(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022 CJS_PropValue& vp,
1023 CFX_WideString& sError) {
1024 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001025 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001026 return FALSE;
1027 }
tsepez55be02e2016-09-12 11:21:42 -07001028 if (!m_pDocument) {
1029 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1030 return FALSE;
1031 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001032 CFX_WideString wsFilePath = m_pDocument->GetPath();
1033 int32_t i = wsFilePath.GetLength() - 1;
1034 for (; i >= 0; i--) {
1035 if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
1036 break;
1037 }
1038 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
1039 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
1040 } else {
1041 vp << L"";
1042 }
1043 return TRUE;
1044}
1045
Tom Sepezba038bc2015-10-08 12:03:00 -07001046FX_BOOL Document::path(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001047 CJS_PropValue& vp,
1048 CFX_WideString& sError) {
1049 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001050 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051 return FALSE;
1052 }
tsepez55be02e2016-09-12 11:21:42 -07001053 if (!m_pDocument) {
1054 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1055 return FALSE;
1056 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001057 vp << app::SysPathToPDFPath(m_pDocument->GetPath());
1058 return TRUE;
1059}
1060
Tom Sepezba038bc2015-10-08 12:03:00 -07001061FX_BOOL Document::pageWindowRect(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001062 CJS_PropValue& vp,
1063 CFX_WideString& sError) {
1064 return TRUE;
1065}
1066
Tom Sepezba038bc2015-10-08 12:03:00 -07001067FX_BOOL Document::layout(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001068 CJS_PropValue& vp,
1069 CFX_WideString& sError) {
1070 return TRUE;
1071}
1072
Tom Sepezba038bc2015-10-08 12:03:00 -07001073FX_BOOL Document::addLink(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001074 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001075 CJS_Value& vRet,
1076 CFX_WideString& sError) {
1077 return TRUE;
1078}
1079
Tom Sepezba038bc2015-10-08 12:03:00 -07001080FX_BOOL Document::closeDoc(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001081 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082 CJS_Value& vRet,
1083 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084 return TRUE;
1085}
1086
Tom Sepezba038bc2015-10-08 12:03:00 -07001087FX_BOOL Document::getPageBox(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001088 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001089 CJS_Value& vRet,
1090 CFX_WideString& sError) {
1091 return TRUE;
1092}
1093
Tom Sepezba038bc2015-10-08 12:03:00 -07001094FX_BOOL Document::getAnnot(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001095 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001096 CJS_Value& vRet,
1097 CFX_WideString& sError) {
tonikitoo618cb1f2016-08-18 20:10:17 -07001098 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001099 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tonikitoo618cb1f2016-08-18 20:10:17 -07001100 return FALSE;
1101 }
tsepez55be02e2016-09-12 11:21:42 -07001102 if (!m_pDocument) {
1103 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1104 return FALSE;
1105 }
tsepezcd5dc852016-09-08 11:23:24 -07001106 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tonikitoo618cb1f2016-08-18 20:10:17 -07001107 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1108 int nPageNo = params[0].ToInt(pRuntime);
1109 CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
tonikitoo618cb1f2016-08-18 20:10:17 -07001110 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(nPageNo);
1111 if (!pPageView)
1112 return FALSE;
1113
1114 CPDFSDK_AnnotIterator annotIterator(pPageView, false);
1115 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
1116 while (CPDFSDK_Annot* pSDKAnnotCur = annotIterator.Next()) {
1117 CPDFSDK_BAAnnot* pBAAnnot = static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur);
1118 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1119 pSDKBAAnnot = pBAAnnot;
1120 break;
1121 }
1122 }
1123
1124 if (!pSDKBAAnnot)
1125 return FALSE;
1126
1127 v8::Local<v8::Object> pObj =
1128 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1129 if (pObj.IsEmpty())
1130 return FALSE;
1131
1132 CJS_Annot* pJS_Annot =
1133 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1134 if (!pJS_Annot)
1135 return FALSE;
1136
1137 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1138 if (!pAnnot)
1139 return FALSE;
1140
1141 pAnnot->SetSDKAnnot(pSDKBAAnnot);
1142
1143 vRet = CJS_Value(pRuntime, pJS_Annot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001144 return TRUE;
1145}
1146
Tom Sepezba038bc2015-10-08 12:03:00 -07001147FX_BOOL Document::getAnnots(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001148 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001149 CJS_Value& vRet,
1150 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001151 if (!m_pDocument) {
1152 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1153 return FALSE;
1154 }
tonikitoo0ee35902016-08-26 14:41:29 -07001155 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepez55be02e2016-09-12 11:21:42 -07001156 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tonikitoo0ee35902016-08-26 14:41:29 -07001157
1158 // TODO(tonikitoo): Add support supported parameters as per
1159 // the PDF spec.
1160
tonikitoo0ee35902016-08-26 14:41:29 -07001161 int nPageNo = m_pDocument->GetPageCount();
1162 CJS_Array annots;
1163
1164 for (int i = 0; i < nPageNo; ++i) {
1165 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(i);
1166 if (!pPageView)
1167 return FALSE;
1168
1169 CPDFSDK_AnnotIterator annotIterator(pPageView, false);
1170 while (CPDFSDK_Annot* pSDKAnnotCur = annotIterator.Next()) {
1171 CPDFSDK_BAAnnot* pSDKBAAnnot =
1172 static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur);
1173 if (!pSDKBAAnnot)
1174 return FALSE;
1175
1176 v8::Local<v8::Object> pObj =
1177 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1178 if (pObj.IsEmpty())
1179 return FALSE;
1180
1181 CJS_Annot* pJS_Annot =
1182 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1183 if (!pJS_Annot)
1184 return FALSE;
1185
1186 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1187 if (!pAnnot)
1188 return FALSE;
1189
1190 pAnnot->SetSDKAnnot(pSDKBAAnnot);
1191 annots.SetElement(pRuntime, i, CJS_Value(pRuntime, pJS_Annot));
1192 }
1193 }
1194
1195 vRet = CJS_Value(pRuntime, annots);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001196 return TRUE;
1197}
1198
Tom Sepezba038bc2015-10-08 12:03:00 -07001199FX_BOOL Document::getAnnot3D(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001200 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001201 CJS_Value& vRet,
1202 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001203 vRet.SetNull(CJS_Runtime::FromContext(cc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204 return TRUE;
1205}
1206
Tom Sepezba038bc2015-10-08 12:03:00 -07001207FX_BOOL Document::getAnnots3D(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001208 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001209 CJS_Value& vRet,
1210 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001211 return TRUE;
1212}
1213
Tom Sepezba038bc2015-10-08 12:03:00 -07001214FX_BOOL Document::getOCGs(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001215 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001216 CJS_Value& vRet,
1217 CFX_WideString& sError) {
1218 return TRUE;
1219}
1220
Tom Sepezba038bc2015-10-08 12:03:00 -07001221FX_BOOL Document::getLinks(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001222 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001223 CJS_Value& vRet,
1224 CFX_WideString& sError) {
1225 return TRUE;
1226}
1227
1228bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1229 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1230 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1231}
1232
Tom Sepezba038bc2015-10-08 12:03:00 -07001233FX_BOOL Document::addIcon(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001234 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001235 CJS_Value& vRet,
1236 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001237 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001238 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001239 return FALSE;
1240 }
tsepezf3dc8c62016-08-10 06:29:29 -07001241
tsepezcd5dc852016-09-08 11:23:24 -07001242 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -07001243 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -07001244 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Tom Sepezaecd9d02015-08-31 15:04:12 -07001245
Tom Sepez39bfe122015-09-17 15:25:23 -07001246 if (params[1].GetType() != CJS_Value::VT_object) {
tsepezcd5dc852016-09-08 11:23:24 -07001247 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
Tom Sepezaecd9d02015-08-31 15:04:12 -07001248 return FALSE;
1249 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001250
tsepezb4694242016-08-15 16:44:55 -07001251 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pRuntime);
1252 if (pRuntime->GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
tsepezcd5dc852016-09-08 11:23:24 -07001253 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001254 return FALSE;
1255 }
1256
tsepezb4694242016-08-15 16:44:55 -07001257 CJS_EmbedObj* pEmbedObj = params[1].ToCJSObject(pRuntime)->GetEmbedObject();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258 if (!pEmbedObj) {
tsepezcd5dc852016-09-08 11:23:24 -07001259 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001260 return FALSE;
1261 }
1262
Tom Sepez6ba32d92016-01-05 16:26:32 -08001263 m_IconList.push_back(std::unique_ptr<IconElement>(
1264 new IconElement(swIconName, (Icon*)pEmbedObj)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001265 return TRUE;
1266}
1267
Tom Sepezba038bc2015-10-08 12:03:00 -07001268FX_BOOL Document::icons(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001269 CJS_PropValue& vp,
1270 CFX_WideString& sError) {
1271 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001272 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001273 return FALSE;
1274 }
1275
tsepezf3dc8c62016-08-10 06:29:29 -07001276 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Tom Sepez6ba32d92016-01-05 16:26:32 -08001277 if (m_IconList.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001278 vp.GetJSValue()->SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001279 return TRUE;
1280 }
1281
tsepeze5aff742016-08-08 09:49:42 -07001282 CJS_Array Icons;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001283
Tom Sepez6ba32d92016-01-05 16:26:32 -08001284 int i = 0;
1285 for (const auto& pIconElement : m_IconList) {
tsepezb4694242016-08-15 16:44:55 -07001286 v8::Local<v8::Object> pObj =
1287 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001288 if (pObj.IsEmpty())
1289 return FALSE;
1290
tsepezb4694242016-08-15 16:44:55 -07001291 CJS_Icon* pJS_Icon =
1292 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001293 if (!pJS_Icon)
1294 return FALSE;
1295
1296 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1297 if (!pIcon)
1298 return FALSE;
1299
1300 pIcon->SetStream(pIconElement->IconStream->GetStream());
1301 pIcon->SetIconName(pIconElement->IconName);
tsepezb4694242016-08-15 16:44:55 -07001302 Icons.SetElement(pRuntime, i++, CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001303 }
1304
1305 vp << Icons;
1306 return TRUE;
1307}
1308
Tom Sepezba038bc2015-10-08 12:03:00 -07001309FX_BOOL Document::getIcon(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001310 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001311 CJS_Value& vRet,
1312 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001313 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001314 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001315 return FALSE;
1316 }
1317
Tom Sepez6ba32d92016-01-05 16:26:32 -08001318 if (m_IconList.empty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001319 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001320
tsepezcd5dc852016-09-08 11:23:24 -07001321 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001322 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001323
Tom Sepez6ba32d92016-01-05 16:26:32 -08001324 for (const auto& pIconElement : m_IconList) {
1325 if (pIconElement->IconName == swIconName) {
1326 Icon* pRetIcon = pIconElement->IconStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001327
tsepezb4694242016-08-15 16:44:55 -07001328 v8::Local<v8::Object> pObj =
1329 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001330 if (pObj.IsEmpty())
1331 return FALSE;
1332
tsepezb4694242016-08-15 16:44:55 -07001333 CJS_Icon* pJS_Icon =
1334 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001335 if (!pJS_Icon)
1336 return FALSE;
1337
1338 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1339 if (!pIcon)
1340 return FALSE;
1341
1342 pIcon->SetIconName(swIconName);
1343 pIcon->SetStream(pRetIcon->GetStream());
tsepezf3dc8c62016-08-10 06:29:29 -07001344 vRet = CJS_Value(pRuntime, pJS_Icon);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001345 return TRUE;
1346 }
1347 }
1348
1349 return FALSE;
1350}
1351
Tom Sepezba038bc2015-10-08 12:03:00 -07001352FX_BOOL Document::removeIcon(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001353 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001354 CJS_Value& vRet,
1355 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001356 // Unsafe, no supported.
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001357 return TRUE;
1358}
1359
Tom Sepezba038bc2015-10-08 12:03:00 -07001360FX_BOOL Document::createDataObject(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001361 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001362 CJS_Value& vRet,
1363 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001364 // Unsafe, not implemented.
1365 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001366}
1367
Tom Sepezba038bc2015-10-08 12:03:00 -07001368FX_BOOL Document::media(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001369 CJS_PropValue& vp,
1370 CFX_WideString& sError) {
1371 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001372}
1373
Tom Sepezba038bc2015-10-08 12:03:00 -07001374FX_BOOL Document::calculateNow(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001375 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001376 CJS_Value& vRet,
1377 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001378 if (!m_pDocument) {
1379 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1380 return FALSE;
1381 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001382 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001383 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM) ||
tsepez55be02e2016-09-12 11:21:42 -07001384 m_pDocument->GetPermissions(FPDFPERM_FILL_FORM))) {
1385 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001386 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -07001387 }
1388 m_pDocument->GetInterForm()->OnCalculate();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001389 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001390}
1391
Tom Sepezba038bc2015-10-08 12:03:00 -07001392FX_BOOL Document::Collab(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001393 CJS_PropValue& vp,
1394 CFX_WideString& sError) {
1395 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001396}
1397
Tom Sepezba038bc2015-10-08 12:03:00 -07001398FX_BOOL Document::getPageNthWord(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001399 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001400 CJS_Value& vRet,
1401 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001402 if (!m_pDocument) {
1403 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1404 return FALSE;
1405 }
tsepezcd5dc852016-09-08 11:23:24 -07001406 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
1407 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001408 return FALSE;
tsepezcd5dc852016-09-08 11:23:24 -07001409 }
1410 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepez55be02e2016-09-12 11:21:42 -07001411
1412 // TODO(tsepez): check maximum allowable params.
1413
tsepezb4694242016-08-15 16:44:55 -07001414 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
1415 int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
1416 bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001417
Tom Sepez50d12ad2015-11-24 09:50:51 -08001418 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001419 if (!pDocument)
1420 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001421
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001422 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001423 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001424 return FALSE;
1425 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001426
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001427 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1428 if (!pPageDict)
1429 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001430
thestig5cc24652016-04-26 11:46:02 -07001431 CPDF_Page page(pDocument, pPageDict, true);
1432 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001433
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001434 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001435 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001436 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001437 if (pPageObj->IsText()) {
1438 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001439 int nObjWords = CountWords(pTextObj);
1440 if (nWords + nObjWords >= nWordNo) {
1441 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1442 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001443 }
Tom Sepez2398d892016-02-17 16:46:26 -08001444 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001445 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001446 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001447
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001448 if (bStrip) {
1449 swRet.TrimLeft();
1450 swRet.TrimRight();
1451 }
1452
tsepezf3dc8c62016-08-10 06:29:29 -07001453 vRet = CJS_Value(pRuntime, swRet.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001454 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001455}
1456
Tom Sepezba038bc2015-10-08 12:03:00 -07001457FX_BOOL Document::getPageNthWordQuads(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001458 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001459 CJS_Value& vRet,
1460 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001461 if (!m_pDocument) {
1462 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001463 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -07001464 }
1465 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
1466 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1467 return FALSE;
1468 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001469 return FALSE;
1470}
1471
Tom Sepezba038bc2015-10-08 12:03:00 -07001472FX_BOOL Document::getPageNumWords(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001473 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001474 CJS_Value& vRet,
1475 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001476 if (!m_pDocument) {
1477 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1478 return FALSE;
1479 }
tsepezcd5dc852016-09-08 11:23:24 -07001480 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
1481 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001482 return FALSE;
tsepezcd5dc852016-09-08 11:23:24 -07001483 }
1484 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001485 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
Tom Sepez50d12ad2015-11-24 09:50:51 -08001486 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001487 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001488 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001489 return FALSE;
1490 }
1491
1492 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1493 if (!pPageDict)
1494 return FALSE;
1495
thestig5cc24652016-04-26 11:46:02 -07001496 CPDF_Page page(pDocument, pPageDict, true);
1497 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001498
1499 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001500 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001501 if (pPageObj->IsText())
1502 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001503 }
1504
tsepezf3dc8c62016-08-10 06:29:29 -07001505 vRet = CJS_Value(pRuntime, nWords);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001506 return TRUE;
1507}
1508
Tom Sepezba038bc2015-10-08 12:03:00 -07001509FX_BOOL Document::getPrintParams(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001510 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001511 CJS_Value& vRet,
1512 CFX_WideString& sError) {
1513 CJS_Context* pContext = (CJS_Context*)cc;
1514 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -07001515 v8::Local<v8::Object> pRetObj =
1516 pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001517
1518 // Not implemented yet.
1519
tsepezf3dc8c62016-08-10 06:29:29 -07001520 vRet = CJS_Value(pRuntime, pRetObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001521 return TRUE;
1522}
1523
1524#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1525
1526int Document::CountWords(CPDF_TextObject* pTextObj) {
1527 if (!pTextObj)
1528 return 0;
1529
1530 int nWords = 0;
1531
1532 CPDF_Font* pFont = pTextObj->GetFont();
1533 if (!pFont)
1534 return 0;
1535
1536 FX_BOOL bIsLatin = FALSE;
1537
1538 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001539 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001540 FX_FLOAT kerning;
1541
1542 pTextObj->GetCharInfo(i, charcode, kerning);
1543 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1544
Tom Sepez62a70f92016-03-21 15:00:20 -07001545 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001546 if (swUnicode.GetLength() > 0)
1547 unicode = swUnicode[0];
1548
1549 if (ISLATINWORD(unicode) && bIsLatin)
1550 continue;
1551
1552 bIsLatin = ISLATINWORD(unicode);
1553 if (unicode != 0x20)
1554 nWords++;
1555 }
1556
1557 return nWords;
1558}
1559
1560CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1561 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001562 CFX_WideString swRet;
1563
1564 CPDF_Font* pFont = pTextObj->GetFont();
1565 if (!pFont)
1566 return L"";
1567
1568 int nWords = 0;
1569 FX_BOOL bIsLatin = FALSE;
1570
1571 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001572 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001573 FX_FLOAT kerning;
1574
1575 pTextObj->GetCharInfo(i, charcode, kerning);
1576 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1577
Tom Sepez62a70f92016-03-21 15:00:20 -07001578 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001579 if (swUnicode.GetLength() > 0)
1580 unicode = swUnicode[0];
1581
1582 if (ISLATINWORD(unicode) && bIsLatin) {
1583 } else {
1584 bIsLatin = ISLATINWORD(unicode);
1585 if (unicode != 0x20)
1586 nWords++;
1587 }
1588
1589 if (nWords - 1 == nWordIndex)
1590 swRet += unicode;
1591 }
1592
1593 return swRet;
1594}
1595
Tom Sepezba038bc2015-10-08 12:03:00 -07001596FX_BOOL Document::zoom(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001597 CJS_PropValue& vp,
1598 CFX_WideString& sError) {
1599 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001600}
1601
1602/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001603(none, NoVary)
1604(fitP, FitPage)
1605(fitW, FitWidth)
1606(fitH, FitHeight)
1607(fitV, FitVisibleWidth)
1608(pref, Preferred)
1609(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001610*/
1611
Tom Sepezba038bc2015-10-08 12:03:00 -07001612FX_BOOL Document::zoomType(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001613 CJS_PropValue& vp,
1614 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001615 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001616}
1617
Tom Sepezba038bc2015-10-08 12:03:00 -07001618FX_BOOL Document::deletePages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001619 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001620 CJS_Value& vRet,
1621 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001622 // Unsafe, no supported.
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001623 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001624}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001625
Tom Sepezba038bc2015-10-08 12:03:00 -07001626FX_BOOL Document::extractPages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001627 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001628 CJS_Value& vRet,
1629 CFX_WideString& sError) {
1630 // Unsafe, not supported.
1631 return TRUE;
1632}
1633
Tom Sepezba038bc2015-10-08 12:03:00 -07001634FX_BOOL Document::insertPages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001635 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001636 CJS_Value& vRet,
1637 CFX_WideString& sError) {
1638 // Unsafe, not supported.
1639 return TRUE;
1640}
1641
Tom Sepezba038bc2015-10-08 12:03:00 -07001642FX_BOOL Document::replacePages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001643 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001644 CJS_Value& vRet,
1645 CFX_WideString& sError) {
1646 // Unsafe, not supported.
1647 return TRUE;
1648}
1649
Tom Sepezba038bc2015-10-08 12:03:00 -07001650FX_BOOL Document::getURL(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001651 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001652 CJS_Value& vRet,
1653 CFX_WideString& sError) {
1654 // Unsafe, not supported.
1655 return TRUE;
1656}
1657
tonikitoo1c836752016-08-08 16:14:05 -07001658FX_BOOL Document::gotoNamedDest(IJS_Context* cc,
1659 const std::vector<CJS_Value>& params,
1660 CJS_Value& vRet,
1661 CFX_WideString& sError) {
tonikitoo1c836752016-08-08 16:14:05 -07001662 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001663 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tonikitoo1c836752016-08-08 16:14:05 -07001664 return FALSE;
1665 }
tsepez55be02e2016-09-12 11:21:42 -07001666 if (!m_pDocument) {
1667 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1668 return FALSE;
1669 }
tsepezcd5dc852016-09-08 11:23:24 -07001670 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001671 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -07001672 CFX_ByteString utf8Name = wideName.UTF8Encode();
tonikitoo1c836752016-08-08 16:14:05 -07001673 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
1674 if (!pDocument)
1675 return FALSE;
1676
tonikitoo1c836752016-08-08 16:14:05 -07001677 CPDF_NameTree nameTree(pDocument, "Dests");
1678 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, utf8Name);
1679 if (!destArray)
1680 return FALSE;
1681
1682 CPDF_Dest dest(destArray);
1683 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
1684
1685 std::unique_ptr<float[]> scrollPositionArray;
1686 int scrollPositionArraySize = 0;
1687
1688 if (arrayObject) {
1689 scrollPositionArray.reset(new float[arrayObject->GetCount()]);
1690 int j = 0;
1691 for (size_t i = 2; i < arrayObject->GetCount(); i++)
1692 scrollPositionArray[j++] = arrayObject->GetFloatAt(i);
1693 scrollPositionArraySize = j;
1694 }
1695
tsepezb4694242016-08-15 16:44:55 -07001696 pRuntime->BeginBlock();
dsinclair79db6092016-09-14 07:27:21 -07001697 CPDFSDK_Environment* pApp = m_pDocument->GetEnv();
dsinclair1f248902016-09-14 10:38:17 -07001698 pApp->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
1699 scrollPositionArray.get(), scrollPositionArraySize);
tsepezb4694242016-08-15 16:44:55 -07001700 pRuntime->EndBlock();
tonikitoo1c836752016-08-08 16:14:05 -07001701
1702 return TRUE;
1703}
1704
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001705void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001706 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001707}
1708
1709void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1710 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001711 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1712 auto iter = m_DelayData.begin();
1713 while (iter != m_DelayData.end()) {
1714 auto old = iter++;
1715 if ((*old)->sFieldName == sFieldName &&
1716 (*old)->nControlIndex == nControlIndex) {
1717 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1718 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001719 }
1720 }
1721
Tom Sepez56c10192016-03-15 12:34:17 -07001722 for (const auto& pData : DelayDataForFieldAndControlIndex)
tsepez55be02e2016-09-12 11:21:42 -07001723 Field::DoDelay(m_pDocument.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001724}
1725
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001726CJS_Document* Document::GetCJSDoc() const {
1727 return static_cast<CJS_Document*>(m_pJSObject);
JUN FANG33f6f0d2015-04-06 12:39:51 -07001728}