blob: ea225f095de537fa7cd3342d8c1f65c6e9b9329a [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
Dan Sinclair3ebd1212016-03-09 09:59:23 -05009#include <vector>
10
dan sinclair61b2fc72016-03-23 19:21:44 -040011#include "core/fpdfapi/fpdf_font/include/cpdf_font.h"
Dan Sinclair455a4192016-03-16 09:48:56 -040012#include "core/fpdfapi/fpdf_page/include/cpdf_page.h"
tonikitoo1c836752016-08-08 16:14:05 -070013#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040014#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
dsinclaircac704d2016-07-28 12:59:09 -070015#include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h"
16#include "core/fpdfdoc/include/cpdf_interform.h"
tonikitoo1c836752016-08-08 16:14:05 -070017#include "core/fpdfdoc/include/cpdf_nametree.h"
tonikitoo618cb1f2016-08-18 20:10:17 -070018#include "fpdfsdk/include/cpdfsdk_annotiterator.h"
dsinclairf34518b2016-09-13 12:03:48 -070019#include "fpdfsdk/include/cpdfsdk_document.h"
dsinclair79db6092016-09-14 07:27:21 -070020#include "fpdfsdk/include/cpdfsdk_environment.h"
jaepark611adb82016-08-17 11:34:36 -070021#include "fpdfsdk/include/cpdfsdk_interform.h"
dsinclairf34518b2016-09-13 12:03:48 -070022#include "fpdfsdk/include/cpdfsdk_pageview.h"
jaepark611adb82016-08-17 11:34:36 -070023#include "fpdfsdk/include/cpdfsdk_widget.h"
tonikitoo618cb1f2016-08-18 20:10:17 -070024#include "fpdfsdk/javascript/Annot.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040025#include "fpdfsdk/javascript/Field.h"
26#include "fpdfsdk/javascript/Icon.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040027#include "fpdfsdk/javascript/JS_Define.h"
28#include "fpdfsdk/javascript/JS_EventHandler.h"
29#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040030#include "fpdfsdk/javascript/JS_Value.h"
31#include "fpdfsdk/javascript/app.h"
dsinclair64376be2016-03-31 20:03:24 -070032#include "fpdfsdk/javascript/cjs_context.h"
33#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040034#include "fpdfsdk/javascript/resource.h"
Lei Zhang8241df72015-11-06 14:38:48 -080035#include "third_party/base/numerics/safe_math.h"
Chris Palmer9108ad22014-06-26 16:01:46 -070036
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070037BEGIN_JS_STATIC_CONST(CJS_PrintParamsObj)
38END_JS_STATIC_CONST()
39
40BEGIN_JS_STATIC_PROP(CJS_PrintParamsObj)
41END_JS_STATIC_PROP()
42
43BEGIN_JS_STATIC_METHOD(CJS_PrintParamsObj)
44END_JS_STATIC_METHOD()
45
46IMPLEMENT_JS_CLASS(CJS_PrintParamsObj, PrintParamsObj)
47
48PrintParamsObj::PrintParamsObj(CJS_Object* pJSObject)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 : CJS_EmbedObj(pJSObject) {
50 bUI = TRUE;
51 nStart = 0;
52 nEnd = 0;
53 bSilent = FALSE;
54 bShrinkToFit = FALSE;
55 bPrintAsImage = FALSE;
56 bReverse = FALSE;
57 bAnnotations = TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070058}
59
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060#define MINWIDTH 5.0f
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061#define MINHEIGHT 5.0f
62
63BEGIN_JS_STATIC_CONST(CJS_Document)
64END_JS_STATIC_CONST()
65
66BEGIN_JS_STATIC_PROP(CJS_Document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067JS_STATIC_PROP_ENTRY(ADBE)
68JS_STATIC_PROP_ENTRY(author)
69JS_STATIC_PROP_ENTRY(baseURL)
70JS_STATIC_PROP_ENTRY(bookmarkRoot)
71JS_STATIC_PROP_ENTRY(calculate)
72JS_STATIC_PROP_ENTRY(Collab)
73JS_STATIC_PROP_ENTRY(creationDate)
74JS_STATIC_PROP_ENTRY(creator)
75JS_STATIC_PROP_ENTRY(delay)
76JS_STATIC_PROP_ENTRY(dirty)
77JS_STATIC_PROP_ENTRY(documentFileName)
78JS_STATIC_PROP_ENTRY(external)
79JS_STATIC_PROP_ENTRY(filesize)
80JS_STATIC_PROP_ENTRY(icons)
81JS_STATIC_PROP_ENTRY(info)
82JS_STATIC_PROP_ENTRY(keywords)
83JS_STATIC_PROP_ENTRY(layout)
84JS_STATIC_PROP_ENTRY(media)
85JS_STATIC_PROP_ENTRY(modDate)
86JS_STATIC_PROP_ENTRY(mouseX)
87JS_STATIC_PROP_ENTRY(mouseY)
88JS_STATIC_PROP_ENTRY(numFields)
89JS_STATIC_PROP_ENTRY(numPages)
90JS_STATIC_PROP_ENTRY(pageNum)
91JS_STATIC_PROP_ENTRY(pageWindowRect)
92JS_STATIC_PROP_ENTRY(path)
93JS_STATIC_PROP_ENTRY(producer)
94JS_STATIC_PROP_ENTRY(subject)
95JS_STATIC_PROP_ENTRY(title)
tonikitoo33c4cdb2016-08-08 10:52:51 -070096JS_STATIC_PROP_ENTRY(URL)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097JS_STATIC_PROP_ENTRY(zoom)
98JS_STATIC_PROP_ENTRY(zoomType)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099END_JS_STATIC_PROP()
100
101BEGIN_JS_STATIC_METHOD(CJS_Document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102JS_STATIC_METHOD_ENTRY(addAnnot)
103JS_STATIC_METHOD_ENTRY(addField)
104JS_STATIC_METHOD_ENTRY(addLink)
105JS_STATIC_METHOD_ENTRY(addIcon)
106JS_STATIC_METHOD_ENTRY(calculateNow)
107JS_STATIC_METHOD_ENTRY(closeDoc)
108JS_STATIC_METHOD_ENTRY(createDataObject)
109JS_STATIC_METHOD_ENTRY(deletePages)
110JS_STATIC_METHOD_ENTRY(exportAsText)
111JS_STATIC_METHOD_ENTRY(exportAsFDF)
112JS_STATIC_METHOD_ENTRY(exportAsXFDF)
113JS_STATIC_METHOD_ENTRY(extractPages)
114JS_STATIC_METHOD_ENTRY(getAnnot)
115JS_STATIC_METHOD_ENTRY(getAnnots)
116JS_STATIC_METHOD_ENTRY(getAnnot3D)
117JS_STATIC_METHOD_ENTRY(getAnnots3D)
118JS_STATIC_METHOD_ENTRY(getField)
119JS_STATIC_METHOD_ENTRY(getIcon)
120JS_STATIC_METHOD_ENTRY(getLinks)
121JS_STATIC_METHOD_ENTRY(getNthFieldName)
122JS_STATIC_METHOD_ENTRY(getOCGs)
123JS_STATIC_METHOD_ENTRY(getPageBox)
124JS_STATIC_METHOD_ENTRY(getPageNthWord)
125JS_STATIC_METHOD_ENTRY(getPageNthWordQuads)
126JS_STATIC_METHOD_ENTRY(getPageNumWords)
127JS_STATIC_METHOD_ENTRY(getPrintParams)
128JS_STATIC_METHOD_ENTRY(getURL)
tonikitoo1c836752016-08-08 16:14:05 -0700129JS_STATIC_METHOD_ENTRY(gotoNamedDest)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130JS_STATIC_METHOD_ENTRY(importAnFDF)
131JS_STATIC_METHOD_ENTRY(importAnXFDF)
132JS_STATIC_METHOD_ENTRY(importTextData)
133JS_STATIC_METHOD_ENTRY(insertPages)
134JS_STATIC_METHOD_ENTRY(mailForm)
135JS_STATIC_METHOD_ENTRY(print)
136JS_STATIC_METHOD_ENTRY(removeField)
137JS_STATIC_METHOD_ENTRY(replacePages)
138JS_STATIC_METHOD_ENTRY(resetForm)
139JS_STATIC_METHOD_ENTRY(removeIcon)
140JS_STATIC_METHOD_ENTRY(saveAs)
141JS_STATIC_METHOD_ENTRY(submitForm)
tonikitoobb5fa042016-08-19 11:18:29 -0700142JS_STATIC_METHOD_ENTRY(syncAnnotScan)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143JS_STATIC_METHOD_ENTRY(mailDoc)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144END_JS_STATIC_METHOD()
145
146IMPLEMENT_JS_CLASS(CJS_Document, Document)
147
Tom Sepez33420902015-10-13 15:00:10 -0700148void CJS_Document::InitInstance(IJS_Runtime* pIRuntime) {
149 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime);
150 Document* pDoc = static_cast<Document*>(GetEmbedObject());
151 pDoc->AttachDoc(pRuntime->GetReaderDocument());
Tom Sepez33420902015-10-13 15:00:10 -0700152}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154Document::Document(CJS_Object* pJSObject)
155 : CJS_EmbedObj(pJSObject),
thestig1cd352e2016-06-07 17:53:06 -0700156 m_pDocument(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 m_cwBaseURL(L""),
158 m_bDelay(FALSE) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160Document::~Document() {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161}
162
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163// the total number of fileds in document.
Tom Sepezba038bc2015-10-08 12:03:00 -0700164FX_BOOL Document::numFields(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 CJS_PropValue& vp,
166 CFX_WideString& sError) {
167 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700168 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700169 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170 }
tsepez55be02e2016-09-12 11:21:42 -0700171 if (!m_pDocument) {
172 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
173 return FALSE;
174 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
176 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
177 vp << (int)pPDFForm->CountFields();
178 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700179}
180
Tom Sepezba038bc2015-10-08 12:03:00 -0700181FX_BOOL Document::dirty(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 CJS_PropValue& vp,
183 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700184 if (!m_pDocument) {
185 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
186 return FALSE;
187 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -0700189 vp << !!m_pDocument->GetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 } else {
191 bool bChanged = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 vp >> bChanged;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700193
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194 if (bChanged)
195 m_pDocument->SetChangeMark();
196 else
197 m_pDocument->ClearChangeMark();
198 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200}
201
Tom Sepezba038bc2015-10-08 12:03:00 -0700202FX_BOOL Document::ADBE(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 CJS_PropValue& vp,
204 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700205 if (vp.IsGetting())
206 vp.GetJSValue()->SetNull(CJS_Runtime::FromContext(cc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207
208 return TRUE;
209}
210
Tom Sepezba038bc2015-10-08 12:03:00 -0700211FX_BOOL Document::pageNum(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 CJS_PropValue& vp,
213 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700214 if (!m_pDocument) {
215 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
216 return FALSE;
217 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 if (vp.IsGetting()) {
219 if (CPDFSDK_PageView* pPageView = m_pDocument->GetCurrentView()) {
220 vp << pPageView->GetPageIndex();
221 }
222 } else {
223 int iPageCount = m_pDocument->GetPageCount();
224 int iPageNum = 0;
225 vp >> iPageNum;
226
dsinclair79db6092016-09-14 07:27:21 -0700227 CPDFSDK_Environment* pEnv = m_pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 if (iPageNum >= 0 && iPageNum < iPageCount) {
229 pEnv->JS_docgotoPage(iPageNum);
230 } else if (iPageNum >= iPageCount) {
231 pEnv->JS_docgotoPage(iPageCount - 1);
232 } else if (iPageNum < 0) {
233 pEnv->JS_docgotoPage(0);
234 }
235 }
236
237 return TRUE;
238}
239
Tom Sepezba038bc2015-10-08 12:03:00 -0700240FX_BOOL Document::addAnnot(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800241 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 CJS_Value& vRet,
243 CFX_WideString& sError) {
244 // Not supported.
245 return TRUE;
246}
247
Tom Sepezba038bc2015-10-08 12:03:00 -0700248FX_BOOL Document::addField(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800249 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 CJS_Value& vRet,
251 CFX_WideString& sError) {
252 // Not supported.
253 return TRUE;
254}
255
Tom Sepezba038bc2015-10-08 12:03:00 -0700256FX_BOOL Document::exportAsText(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800257 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 CJS_Value& vRet,
259 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -0800260 // Unsafe, not supported.
261 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262}
263
Tom Sepezba038bc2015-10-08 12:03:00 -0700264FX_BOOL Document::exportAsFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800265 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 CJS_Value& vRet,
267 CFX_WideString& sError) {
268 // Unsafe, not supported.
269 return TRUE;
270}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271
Tom Sepezba038bc2015-10-08 12:03:00 -0700272FX_BOOL Document::exportAsXFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800273 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 CJS_Value& vRet,
275 CFX_WideString& sError) {
276 // Unsafe, not supported.
277 return TRUE;
278}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280// Maps a field object in PDF document to a JavaScript variable
281// comment:
282// note: the paremter cName, this is clue how to treat if the cName is not a
283// valiable filed name in this document
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700284
Tom Sepezba038bc2015-10-08 12:03:00 -0700285FX_BOOL Document::getField(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800286 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 CJS_Value& vRet,
288 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700290 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 return FALSE;
292 }
tsepez55be02e2016-09-12 11:21:42 -0700293 if (!m_pDocument) {
294 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
295 return FALSE;
296 }
tsepezcd5dc852016-09-08 11:23:24 -0700297 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700298 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -0700299 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
301 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
302 if (pPDFForm->CountFields(wideName) <= 0) {
tsepezf3dc8c62016-08-10 06:29:29 -0700303 vRet.SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 return TRUE;
305 }
306
tsepezf3dc8c62016-08-10 06:29:29 -0700307 v8::Local<v8::Object> pFieldObj =
tsepezb4694242016-08-15 16:44:55 -0700308 pRuntime->NewFxDynamicObj(CJS_Field::g_nObjDefnID);
tsepezf3dc8c62016-08-10 06:29:29 -0700309 CJS_Field* pJSField =
tsepezb4694242016-08-15 16:44:55 -0700310 static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
tsepezf3dc8c62016-08-10 06:29:29 -0700311 Field* pField = static_cast<Field*>(pJSField->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312 pField->AttachField(this, wideName);
313
tsepezf3dc8c62016-08-10 06:29:29 -0700314 vRet = CJS_Value(pRuntime, pJSField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 return TRUE;
316}
317
318// Gets the name of the nth field in the document
Tom Sepezba038bc2015-10-08 12:03:00 -0700319FX_BOOL Document::getNthFieldName(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800320 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 CJS_Value& vRet,
322 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700323 if (params.size() != 1) {
324 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
325 return FALSE;
326 }
tsepez55be02e2016-09-12 11:21:42 -0700327 if (!m_pDocument) {
328 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
329 return FALSE;
330 }
tsepezf3dc8c62016-08-10 06:29:29 -0700331 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezb4694242016-08-15 16:44:55 -0700332 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -0700333 int nIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 if (nIndex < 0) {
tsepezcd5dc852016-09-08 11:23:24 -0700335 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 return FALSE;
337 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
339 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
340 CPDF_FormField* pField = pPDFForm->GetField(nIndex);
341 if (!pField)
342 return FALSE;
343
tsepezf3dc8c62016-08-10 06:29:29 -0700344 vRet = CJS_Value(pRuntime, pField->GetFullName().c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345 return TRUE;
346}
347
Tom Sepezba038bc2015-10-08 12:03:00 -0700348FX_BOOL Document::importAnFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800349 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700350 CJS_Value& vRet,
351 CFX_WideString& sError) {
352 // Unsafe, not supported.
353 return TRUE;
354}
355
Tom Sepezba038bc2015-10-08 12:03:00 -0700356FX_BOOL Document::importAnXFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800357 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700358 CJS_Value& vRet,
359 CFX_WideString& sError) {
360 // Unsafe, not supported.
361 return TRUE;
362}
363
Tom Sepezba038bc2015-10-08 12:03:00 -0700364FX_BOOL Document::importTextData(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800365 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 CJS_Value& vRet,
367 CFX_WideString& sError) {
368 // Unsafe, not supported.
369 return TRUE;
370}
371
372// exports the form data and mails the resulting fdf file as an attachment to
373// all recipients.
374// comment: need reader supports
375// note:
376// int CPDFSDK_Document::mailForm(FX_BOOL bUI,String cto,string ccc,string
377// cbcc,string cSubject,string cms);
378
Tom Sepezba038bc2015-10-08 12:03:00 -0700379FX_BOOL Document::mailForm(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800380 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 CJS_Value& vRet,
382 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700383 if (!m_pDocument) {
384 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
385 return FALSE;
386 }
387 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
388 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
389 return FALSE;
390 }
391
tsepezf3dc8c62016-08-10 06:29:29 -0700392 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezb4694242016-08-15 16:44:55 -0700393 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -0700394
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 int iLength = params.size();
tsepezb4694242016-08-15 16:44:55 -0700396 FX_BOOL bUI = iLength > 0 ? params[0].ToBool(pRuntime) : TRUE;
397 CFX_WideString cTo = iLength > 1 ? params[1].ToCFXWideString(pRuntime) : L"";
398 CFX_WideString cCc = iLength > 2 ? params[2].ToCFXWideString(pRuntime) : L"";
399 CFX_WideString cBcc = iLength > 3 ? params[3].ToCFXWideString(pRuntime) : L"";
tsepezf3dc8c62016-08-10 06:29:29 -0700400 CFX_WideString cSubject =
tsepezb4694242016-08-15 16:44:55 -0700401 iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
402 CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403
tsepez23ae4a52016-06-08 20:44:54 -0700404 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 CFX_ByteTextBuf textBuf;
406 if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
407 return FALSE;
408
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 pRuntime->BeginBlock();
dsinclair8e0638b2016-09-22 11:06:02 -0700410 CPDFSDK_Environment* pEnv = pContext->GetReaderEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 pEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI,
412 cTo.c_str(), cSubject.c_str(), cCc.c_str(), cBcc.c_str(),
413 cMsg.c_str());
414 pRuntime->EndBlock();
415 return TRUE;
416}
417
Tom Sepezba038bc2015-10-08 12:03:00 -0700418FX_BOOL Document::print(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800419 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700420 CJS_Value& vRet,
421 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700422 if (!m_pDocument) {
423 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
424 return FALSE;
425 }
426
tsepezf3dc8c62016-08-10 06:29:29 -0700427 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
428 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -0700429
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430 FX_BOOL bUI = TRUE;
431 int nStart = 0;
432 int nEnd = 0;
433 FX_BOOL bSilent = FALSE;
434 FX_BOOL bShrinkToFit = FALSE;
435 FX_BOOL bPrintAsImage = FALSE;
436 FX_BOOL bReverse = FALSE;
437 FX_BOOL bAnnotations = FALSE;
438
439 int nlength = params.size();
440 if (nlength == 9) {
tsepez40faa792016-07-15 17:58:02 -0700441 if (params[8].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700442 v8::Local<v8::Object> pObj = params[8].ToV8Object(pRuntime);
443 if (CFXJS_Engine::GetObjDefnID(pObj) ==
444 CJS_PrintParamsObj::g_nObjDefnID) {
445 if (CJS_Object* pJSObj = params[8].ToCJSObject(pRuntime)) {
tsepez40faa792016-07-15 17:58:02 -0700446 if (PrintParamsObj* pprintparamsObj =
447 static_cast<PrintParamsObj*>(pJSObj->GetEmbedObject())) {
448 bUI = pprintparamsObj->bUI;
449 nStart = pprintparamsObj->nStart;
450 nEnd = pprintparamsObj->nEnd;
451 bSilent = pprintparamsObj->bSilent;
452 bShrinkToFit = pprintparamsObj->bShrinkToFit;
453 bPrintAsImage = pprintparamsObj->bPrintAsImage;
454 bReverse = pprintparamsObj->bReverse;
455 bAnnotations = pprintparamsObj->bAnnotations;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700457 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700458 }
459 }
460 } else {
461 if (nlength >= 1)
tsepezb4694242016-08-15 16:44:55 -0700462 bUI = params[0].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463 if (nlength >= 2)
tsepezb4694242016-08-15 16:44:55 -0700464 nStart = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465 if (nlength >= 3)
tsepezb4694242016-08-15 16:44:55 -0700466 nEnd = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467 if (nlength >= 4)
tsepezb4694242016-08-15 16:44:55 -0700468 bSilent = params[3].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469 if (nlength >= 5)
tsepezb4694242016-08-15 16:44:55 -0700470 bShrinkToFit = params[4].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471 if (nlength >= 6)
tsepezb4694242016-08-15 16:44:55 -0700472 bPrintAsImage = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 if (nlength >= 7)
tsepezb4694242016-08-15 16:44:55 -0700474 bReverse = params[6].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475 if (nlength >= 8)
tsepezb4694242016-08-15 16:44:55 -0700476 bAnnotations = params[7].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700477 }
478
dsinclair79db6092016-09-14 07:27:21 -0700479 if (CPDFSDK_Environment* pEnv = m_pDocument->GetEnv()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700480 pEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit, bPrintAsImage,
481 bReverse, bAnnotations);
482 return TRUE;
483 }
484 return FALSE;
485}
486
487// removes the specified field from the document.
488// comment:
dsinclair1897bdc2016-09-06 14:18:57 -0700489// note: if the filed name is not rational, adobe is dumb for it.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490
Tom Sepezba038bc2015-10-08 12:03:00 -0700491FX_BOOL Document::removeField(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800492 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700493 CJS_Value& vRet,
494 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700495 if (params.size() != 1) {
496 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
497 return FALSE;
498 }
499 if (!m_pDocument) {
500 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
501 return FALSE;
502 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700503 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
tsepezcd5dc852016-09-08 11:23:24 -0700504 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM))) {
505 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506 return FALSE;
507 }
tsepezcd5dc852016-09-08 11:23:24 -0700508 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700509 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -0700510 CFX_WideString sFieldName = params[0].ToCFXWideString(pRuntime);
tsepez23ae4a52016-06-08 20:44:54 -0700511 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Lei Zhangd88a3642015-11-10 09:38:57 -0800512 std::vector<CPDFSDK_Widget*> widgets;
513 pInterForm->GetWidgets(sFieldName, &widgets);
Lei Zhangd88a3642015-11-10 09:38:57 -0800514 if (widgets.empty())
515 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700516
Lei Zhangd88a3642015-11-10 09:38:57 -0800517 for (CPDFSDK_Widget* pWidget : widgets) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800518 CFX_FloatRect rcAnnot = pWidget->GetRect();
Lei Zhangd88a3642015-11-10 09:38:57 -0800519 --rcAnnot.left;
520 --rcAnnot.bottom;
521 ++rcAnnot.right;
522 ++rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523
tsepezdf964df2016-04-21 12:09:41 -0700524 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800525 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Lei Zhangd88a3642015-11-10 09:38:57 -0800526 ASSERT(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527
dsinclair1897bdc2016-09-06 14:18:57 -0700528 // If there is currently no pageview associated with the page being used
529 // do not create one. We may be in the process of tearing down the document
530 // and creating a new pageview at this point will cause bad things.
531 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, false);
532 if (pPageView) {
533 pPageView->DeleteAnnot(pWidget);
534 pPageView->UpdateRects(aRefresh);
535 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700536 }
Lei Zhangd88a3642015-11-10 09:38:57 -0800537 m_pDocument->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700538
539 return TRUE;
540}
541
542// reset filed values within a document.
543// comment:
544// note: if the fields names r not rational, aodbe is dumb for it.
545
Tom Sepezba038bc2015-10-08 12:03:00 -0700546FX_BOOL Document::resetForm(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800547 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700548 CJS_Value& vRet,
549 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700550 if (!m_pDocument) {
551 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
552 return FALSE;
553 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700554 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
555 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM) ||
tsepez55be02e2016-09-12 11:21:42 -0700556 m_pDocument->GetPermissions(FPDFPERM_FILL_FORM))) {
557 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700558 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -0700559 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560
tsepez23ae4a52016-06-08 20:44:54 -0700561 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
tsepeze5aff742016-08-08 09:49:42 -0700563 CJS_Array aName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700564
Lei Zhangdd734de2015-11-11 10:16:52 -0800565 if (params.empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700566 pPDFForm->ResetForm(TRUE);
567 m_pDocument->SetChangeMark();
Lei Zhangd88a3642015-11-10 09:38:57 -0800568 return TRUE;
569 }
570
tsepez55be02e2016-09-12 11:21:42 -0700571 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700572 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -0700573
Lei Zhangd88a3642015-11-10 09:38:57 -0800574 switch (params[0].GetType()) {
575 default:
tsepezb4694242016-08-15 16:44:55 -0700576 aName.Attach(params[0].ToV8Array(pRuntime));
Lei Zhangd88a3642015-11-10 09:38:57 -0800577 break;
578 case CJS_Value::VT_string:
tsepezb4694242016-08-15 16:44:55 -0700579 aName.SetElement(pRuntime, 0, params[0]);
Lei Zhangd88a3642015-11-10 09:38:57 -0800580 break;
581 }
582
583 std::vector<CPDF_FormField*> aFields;
tsepezb4694242016-08-15 16:44:55 -0700584 for (int i = 0, isz = aName.GetLength(pRuntime); i < isz; ++i) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800585 CJS_Value valElement(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700586 aName.GetElement(pRuntime, i, valElement);
587 CFX_WideString swVal = valElement.ToCFXWideString(pRuntime);
Lei Zhangd88a3642015-11-10 09:38:57 -0800588 for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j)
589 aFields.push_back(pPDFForm->GetField(j, swVal));
590 }
591
592 if (!aFields.empty()) {
593 pPDFForm->ResetForm(aFields, TRUE, TRUE);
594 m_pDocument->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700595 }
596
597 return TRUE;
598}
599
Tom Sepezba038bc2015-10-08 12:03:00 -0700600FX_BOOL Document::saveAs(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800601 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700602 CJS_Value& vRet,
603 CFX_WideString& sError) {
604 // Unsafe, not supported.
605 return TRUE;
606}
607
tonikitoobb5fa042016-08-19 11:18:29 -0700608FX_BOOL Document::syncAnnotScan(IJS_Context* cc,
609 const std::vector<CJS_Value>& params,
610 CJS_Value& vRet,
611 CFX_WideString& sError) {
612 return TRUE;
613}
614
Tom Sepezba038bc2015-10-08 12:03:00 -0700615FX_BOOL Document::submitForm(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800616 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617 CJS_Value& vRet,
618 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700619
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) {
tsepezf3dc8c62016-08-10 06:29:29 -07001237
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001238 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001239 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001240 return FALSE;
1241 }
tsepezf3dc8c62016-08-10 06:29:29 -07001242
tsepezcd5dc852016-09-08 11:23:24 -07001243 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -07001244 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -07001245 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Tom Sepezaecd9d02015-08-31 15:04:12 -07001246
Tom Sepez39bfe122015-09-17 15:25:23 -07001247 if (params[1].GetType() != CJS_Value::VT_object) {
tsepezcd5dc852016-09-08 11:23:24 -07001248 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
Tom Sepezaecd9d02015-08-31 15:04:12 -07001249 return FALSE;
1250 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001251
tsepezb4694242016-08-15 16:44:55 -07001252 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pRuntime);
1253 if (pRuntime->GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
tsepezcd5dc852016-09-08 11:23:24 -07001254 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001255 return FALSE;
1256 }
1257
tsepezb4694242016-08-15 16:44:55 -07001258 CJS_EmbedObj* pEmbedObj = params[1].ToCJSObject(pRuntime)->GetEmbedObject();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001259 if (!pEmbedObj) {
tsepezcd5dc852016-09-08 11:23:24 -07001260 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001261 return FALSE;
1262 }
1263
Tom Sepez6ba32d92016-01-05 16:26:32 -08001264 m_IconList.push_back(std::unique_ptr<IconElement>(
1265 new IconElement(swIconName, (Icon*)pEmbedObj)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001266 return TRUE;
1267}
1268
Tom Sepezba038bc2015-10-08 12:03:00 -07001269FX_BOOL Document::icons(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001270 CJS_PropValue& vp,
1271 CFX_WideString& sError) {
1272 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001273 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001274 return FALSE;
1275 }
1276
tsepezf3dc8c62016-08-10 06:29:29 -07001277 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Tom Sepez6ba32d92016-01-05 16:26:32 -08001278 if (m_IconList.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001279 vp.GetJSValue()->SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001280 return TRUE;
1281 }
1282
tsepeze5aff742016-08-08 09:49:42 -07001283 CJS_Array Icons;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001284
Tom Sepez6ba32d92016-01-05 16:26:32 -08001285 int i = 0;
1286 for (const auto& pIconElement : m_IconList) {
tsepezb4694242016-08-15 16:44:55 -07001287 v8::Local<v8::Object> pObj =
1288 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001289 if (pObj.IsEmpty())
1290 return FALSE;
1291
tsepezb4694242016-08-15 16:44:55 -07001292 CJS_Icon* pJS_Icon =
1293 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001294 if (!pJS_Icon)
1295 return FALSE;
1296
1297 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1298 if (!pIcon)
1299 return FALSE;
1300
1301 pIcon->SetStream(pIconElement->IconStream->GetStream());
1302 pIcon->SetIconName(pIconElement->IconName);
tsepezb4694242016-08-15 16:44:55 -07001303 Icons.SetElement(pRuntime, i++, CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001304 }
1305
1306 vp << Icons;
1307 return TRUE;
1308}
1309
Tom Sepezba038bc2015-10-08 12:03:00 -07001310FX_BOOL Document::getIcon(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001311 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001312 CJS_Value& vRet,
1313 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001314 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001315 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001316 return FALSE;
1317 }
1318
Tom Sepez6ba32d92016-01-05 16:26:32 -08001319 if (m_IconList.empty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001320 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001321
tsepezcd5dc852016-09-08 11:23:24 -07001322 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001323 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001324
Tom Sepez6ba32d92016-01-05 16:26:32 -08001325 for (const auto& pIconElement : m_IconList) {
1326 if (pIconElement->IconName == swIconName) {
1327 Icon* pRetIcon = pIconElement->IconStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001328
tsepezb4694242016-08-15 16:44:55 -07001329 v8::Local<v8::Object> pObj =
1330 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001331 if (pObj.IsEmpty())
1332 return FALSE;
1333
tsepezb4694242016-08-15 16:44:55 -07001334 CJS_Icon* pJS_Icon =
1335 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001336 if (!pJS_Icon)
1337 return FALSE;
1338
1339 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1340 if (!pIcon)
1341 return FALSE;
1342
1343 pIcon->SetIconName(swIconName);
1344 pIcon->SetStream(pRetIcon->GetStream());
tsepezf3dc8c62016-08-10 06:29:29 -07001345 vRet = CJS_Value(pRuntime, pJS_Icon);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001346 return TRUE;
1347 }
1348 }
1349
1350 return FALSE;
1351}
1352
Tom Sepezba038bc2015-10-08 12:03:00 -07001353FX_BOOL Document::removeIcon(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001354 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001355 CJS_Value& vRet,
1356 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001357 // Unsafe, no supported.
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001358 return TRUE;
1359}
1360
Tom Sepezba038bc2015-10-08 12:03:00 -07001361FX_BOOL Document::createDataObject(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001362 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001363 CJS_Value& vRet,
1364 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001365 // Unsafe, not implemented.
1366 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001367}
1368
Tom Sepezba038bc2015-10-08 12:03:00 -07001369FX_BOOL Document::media(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001370 CJS_PropValue& vp,
1371 CFX_WideString& sError) {
1372 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001373}
1374
Tom Sepezba038bc2015-10-08 12:03:00 -07001375FX_BOOL Document::calculateNow(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001376 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001377 CJS_Value& vRet,
1378 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001379 if (!m_pDocument) {
1380 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1381 return FALSE;
1382 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001383 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001384 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM) ||
tsepez55be02e2016-09-12 11:21:42 -07001385 m_pDocument->GetPermissions(FPDFPERM_FILL_FORM))) {
1386 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001387 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -07001388 }
1389 m_pDocument->GetInterForm()->OnCalculate();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001390 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001391}
1392
Tom Sepezba038bc2015-10-08 12:03:00 -07001393FX_BOOL Document::Collab(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001394 CJS_PropValue& vp,
1395 CFX_WideString& sError) {
1396 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001397}
1398
Tom Sepezba038bc2015-10-08 12:03:00 -07001399FX_BOOL Document::getPageNthWord(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001400 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001401 CJS_Value& vRet,
1402 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001403 if (!m_pDocument) {
1404 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1405 return FALSE;
1406 }
tsepezcd5dc852016-09-08 11:23:24 -07001407 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
1408 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001409 return FALSE;
tsepezcd5dc852016-09-08 11:23:24 -07001410 }
1411 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepez55be02e2016-09-12 11:21:42 -07001412
1413 // TODO(tsepez): check maximum allowable params.
1414
tsepezb4694242016-08-15 16:44:55 -07001415 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
1416 int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
1417 bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001418
Tom Sepez50d12ad2015-11-24 09:50:51 -08001419 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001420 if (!pDocument)
1421 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001422
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001423 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001424 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001425 return FALSE;
1426 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001427
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001428 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1429 if (!pPageDict)
1430 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001431
thestig5cc24652016-04-26 11:46:02 -07001432 CPDF_Page page(pDocument, pPageDict, true);
1433 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001434
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001435 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001436 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001437 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001438 if (pPageObj->IsText()) {
1439 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001440 int nObjWords = CountWords(pTextObj);
1441 if (nWords + nObjWords >= nWordNo) {
1442 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1443 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001444 }
Tom Sepez2398d892016-02-17 16:46:26 -08001445 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001446 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001447 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001448
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001449 if (bStrip) {
1450 swRet.TrimLeft();
1451 swRet.TrimRight();
1452 }
1453
tsepezf3dc8c62016-08-10 06:29:29 -07001454 vRet = CJS_Value(pRuntime, swRet.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001455 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001456}
1457
Tom Sepezba038bc2015-10-08 12:03:00 -07001458FX_BOOL Document::getPageNthWordQuads(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001459 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001460 CJS_Value& vRet,
1461 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001462 if (!m_pDocument) {
1463 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001464 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -07001465 }
1466 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
1467 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1468 return FALSE;
1469 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001470 return FALSE;
1471}
1472
Tom Sepezba038bc2015-10-08 12:03:00 -07001473FX_BOOL Document::getPageNumWords(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001474 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001475 CJS_Value& vRet,
1476 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001477 if (!m_pDocument) {
1478 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1479 return FALSE;
1480 }
tsepezcd5dc852016-09-08 11:23:24 -07001481 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
1482 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001483 return FALSE;
tsepezcd5dc852016-09-08 11:23:24 -07001484 }
1485 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001486 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
Tom Sepez50d12ad2015-11-24 09:50:51 -08001487 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001488 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001489 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001490 return FALSE;
1491 }
1492
1493 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1494 if (!pPageDict)
1495 return FALSE;
1496
thestig5cc24652016-04-26 11:46:02 -07001497 CPDF_Page page(pDocument, pPageDict, true);
1498 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001499
1500 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001501 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001502 if (pPageObj->IsText())
1503 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001504 }
1505
tsepezf3dc8c62016-08-10 06:29:29 -07001506 vRet = CJS_Value(pRuntime, nWords);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001507 return TRUE;
1508}
1509
Tom Sepezba038bc2015-10-08 12:03:00 -07001510FX_BOOL Document::getPrintParams(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001511 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001512 CJS_Value& vRet,
1513 CFX_WideString& sError) {
1514 CJS_Context* pContext = (CJS_Context*)cc;
1515 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -07001516 v8::Local<v8::Object> pRetObj =
1517 pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001518
1519 // Not implemented yet.
1520
tsepezf3dc8c62016-08-10 06:29:29 -07001521 vRet = CJS_Value(pRuntime, pRetObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001522 return TRUE;
1523}
1524
1525#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1526
1527int Document::CountWords(CPDF_TextObject* pTextObj) {
1528 if (!pTextObj)
1529 return 0;
1530
1531 int nWords = 0;
1532
1533 CPDF_Font* pFont = pTextObj->GetFont();
1534 if (!pFont)
1535 return 0;
1536
1537 FX_BOOL bIsLatin = FALSE;
1538
1539 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001540 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001541 FX_FLOAT kerning;
1542
1543 pTextObj->GetCharInfo(i, charcode, kerning);
1544 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1545
Tom Sepez62a70f92016-03-21 15:00:20 -07001546 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001547 if (swUnicode.GetLength() > 0)
1548 unicode = swUnicode[0];
1549
1550 if (ISLATINWORD(unicode) && bIsLatin)
1551 continue;
1552
1553 bIsLatin = ISLATINWORD(unicode);
1554 if (unicode != 0x20)
1555 nWords++;
1556 }
1557
1558 return nWords;
1559}
1560
1561CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1562 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001563 CFX_WideString swRet;
1564
1565 CPDF_Font* pFont = pTextObj->GetFont();
1566 if (!pFont)
1567 return L"";
1568
1569 int nWords = 0;
1570 FX_BOOL bIsLatin = FALSE;
1571
1572 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001573 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001574 FX_FLOAT kerning;
1575
1576 pTextObj->GetCharInfo(i, charcode, kerning);
1577 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1578
Tom Sepez62a70f92016-03-21 15:00:20 -07001579 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001580 if (swUnicode.GetLength() > 0)
1581 unicode = swUnicode[0];
1582
1583 if (ISLATINWORD(unicode) && bIsLatin) {
1584 } else {
1585 bIsLatin = ISLATINWORD(unicode);
1586 if (unicode != 0x20)
1587 nWords++;
1588 }
1589
1590 if (nWords - 1 == nWordIndex)
1591 swRet += unicode;
1592 }
1593
1594 return swRet;
1595}
1596
Tom Sepezba038bc2015-10-08 12:03:00 -07001597FX_BOOL Document::zoom(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001598 CJS_PropValue& vp,
1599 CFX_WideString& sError) {
1600 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001601}
1602
1603/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001604(none, NoVary)
1605(fitP, FitPage)
1606(fitW, FitWidth)
1607(fitH, FitHeight)
1608(fitV, FitVisibleWidth)
1609(pref, Preferred)
1610(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001611*/
1612
Tom Sepezba038bc2015-10-08 12:03:00 -07001613FX_BOOL Document::zoomType(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001614 CJS_PropValue& vp,
1615 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001616 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001617}
1618
Tom Sepezba038bc2015-10-08 12:03:00 -07001619FX_BOOL Document::deletePages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001620 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001621 CJS_Value& vRet,
1622 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001623 // Unsafe, no supported.
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001624 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001625}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001626
Tom Sepezba038bc2015-10-08 12:03:00 -07001627FX_BOOL Document::extractPages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001628 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001629 CJS_Value& vRet,
1630 CFX_WideString& sError) {
1631 // Unsafe, not supported.
1632 return TRUE;
1633}
1634
Tom Sepezba038bc2015-10-08 12:03:00 -07001635FX_BOOL Document::insertPages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001636 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001637 CJS_Value& vRet,
1638 CFX_WideString& sError) {
1639 // Unsafe, not supported.
1640 return TRUE;
1641}
1642
Tom Sepezba038bc2015-10-08 12:03:00 -07001643FX_BOOL Document::replacePages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001644 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001645 CJS_Value& vRet,
1646 CFX_WideString& sError) {
1647 // Unsafe, not supported.
1648 return TRUE;
1649}
1650
Tom Sepezba038bc2015-10-08 12:03:00 -07001651FX_BOOL Document::getURL(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001652 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001653 CJS_Value& vRet,
1654 CFX_WideString& sError) {
1655 // Unsafe, not supported.
1656 return TRUE;
1657}
1658
tonikitoo1c836752016-08-08 16:14:05 -07001659FX_BOOL Document::gotoNamedDest(IJS_Context* cc,
1660 const std::vector<CJS_Value>& params,
1661 CJS_Value& vRet,
1662 CFX_WideString& sError) {
tonikitoo1c836752016-08-08 16:14:05 -07001663 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001664 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tonikitoo1c836752016-08-08 16:14:05 -07001665 return FALSE;
1666 }
tsepez55be02e2016-09-12 11:21:42 -07001667 if (!m_pDocument) {
1668 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1669 return FALSE;
1670 }
tsepezcd5dc852016-09-08 11:23:24 -07001671 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001672 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -07001673 CFX_ByteString utf8Name = wideName.UTF8Encode();
tonikitoo1c836752016-08-08 16:14:05 -07001674 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
1675 if (!pDocument)
1676 return FALSE;
1677
tonikitoo1c836752016-08-08 16:14:05 -07001678 CPDF_NameTree nameTree(pDocument, "Dests");
1679 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, utf8Name);
1680 if (!destArray)
1681 return FALSE;
1682
1683 CPDF_Dest dest(destArray);
1684 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
1685
1686 std::unique_ptr<float[]> scrollPositionArray;
1687 int scrollPositionArraySize = 0;
1688
1689 if (arrayObject) {
1690 scrollPositionArray.reset(new float[arrayObject->GetCount()]);
1691 int j = 0;
1692 for (size_t i = 2; i < arrayObject->GetCount(); i++)
1693 scrollPositionArray[j++] = arrayObject->GetFloatAt(i);
1694 scrollPositionArraySize = j;
1695 }
1696
tsepezb4694242016-08-15 16:44:55 -07001697 pRuntime->BeginBlock();
dsinclair79db6092016-09-14 07:27:21 -07001698 CPDFSDK_Environment* pApp = m_pDocument->GetEnv();
dsinclair1f248902016-09-14 10:38:17 -07001699 pApp->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
1700 scrollPositionArray.get(), scrollPositionArraySize);
tsepezb4694242016-08-15 16:44:55 -07001701 pRuntime->EndBlock();
tonikitoo1c836752016-08-08 16:14:05 -07001702
1703 return TRUE;
1704}
1705
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001706void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001707 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001708}
1709
1710void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1711 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001712 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1713 auto iter = m_DelayData.begin();
1714 while (iter != m_DelayData.end()) {
1715 auto old = iter++;
1716 if ((*old)->sFieldName == sFieldName &&
1717 (*old)->nControlIndex == nControlIndex) {
1718 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1719 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001720 }
1721 }
1722
Tom Sepez56c10192016-03-15 12:34:17 -07001723 for (const auto& pData : DelayDataForFieldAndControlIndex)
tsepez55be02e2016-09-12 11:21:42 -07001724 Field::DoDelay(m_pDocument.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001725}
1726
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001727CJS_Document* Document::GetCJSDoc() const {
1728 return static_cast<CJS_Document*>(m_pJSObject);
JUN FANG33f6f0d2015-04-06 12:39:51 -07001729}