blob: fe623e960f1856f3e06a555e9d1a111b9042b874 [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"
jaepark611adb82016-08-17 11:34:36 -070019#include "fpdfsdk/include/cpdfsdk_interform.h"
20#include "fpdfsdk/include/cpdfsdk_widget.h"
Dan Sinclair3ebd1212016-03-09 09:59:23 -050021#include "fpdfsdk/include/fsdk_mgr.h"
tonikitoo618cb1f2016-08-18 20:10:17 -070022#include "fpdfsdk/javascript/Annot.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040023#include "fpdfsdk/javascript/Field.h"
24#include "fpdfsdk/javascript/Icon.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040025#include "fpdfsdk/javascript/JS_Define.h"
26#include "fpdfsdk/javascript/JS_EventHandler.h"
27#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040028#include "fpdfsdk/javascript/JS_Value.h"
29#include "fpdfsdk/javascript/app.h"
dsinclair64376be2016-03-31 20:03:24 -070030#include "fpdfsdk/javascript/cjs_context.h"
31#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040032#include "fpdfsdk/javascript/resource.h"
Lei Zhang8241df72015-11-06 14:38:48 -080033#include "third_party/base/numerics/safe_math.h"
Chris Palmer9108ad22014-06-26 16:01:46 -070034
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035BEGIN_JS_STATIC_CONST(CJS_PrintParamsObj)
36END_JS_STATIC_CONST()
37
38BEGIN_JS_STATIC_PROP(CJS_PrintParamsObj)
39END_JS_STATIC_PROP()
40
41BEGIN_JS_STATIC_METHOD(CJS_PrintParamsObj)
42END_JS_STATIC_METHOD()
43
44IMPLEMENT_JS_CLASS(CJS_PrintParamsObj, PrintParamsObj)
45
46PrintParamsObj::PrintParamsObj(CJS_Object* pJSObject)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 : CJS_EmbedObj(pJSObject) {
48 bUI = TRUE;
49 nStart = 0;
50 nEnd = 0;
51 bSilent = FALSE;
52 bShrinkToFit = FALSE;
53 bPrintAsImage = FALSE;
54 bReverse = FALSE;
55 bAnnotations = TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056}
57
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058#define MINWIDTH 5.0f
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059#define MINHEIGHT 5.0f
60
61BEGIN_JS_STATIC_CONST(CJS_Document)
62END_JS_STATIC_CONST()
63
64BEGIN_JS_STATIC_PROP(CJS_Document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065JS_STATIC_PROP_ENTRY(ADBE)
66JS_STATIC_PROP_ENTRY(author)
67JS_STATIC_PROP_ENTRY(baseURL)
68JS_STATIC_PROP_ENTRY(bookmarkRoot)
69JS_STATIC_PROP_ENTRY(calculate)
70JS_STATIC_PROP_ENTRY(Collab)
71JS_STATIC_PROP_ENTRY(creationDate)
72JS_STATIC_PROP_ENTRY(creator)
73JS_STATIC_PROP_ENTRY(delay)
74JS_STATIC_PROP_ENTRY(dirty)
75JS_STATIC_PROP_ENTRY(documentFileName)
76JS_STATIC_PROP_ENTRY(external)
77JS_STATIC_PROP_ENTRY(filesize)
78JS_STATIC_PROP_ENTRY(icons)
79JS_STATIC_PROP_ENTRY(info)
80JS_STATIC_PROP_ENTRY(keywords)
81JS_STATIC_PROP_ENTRY(layout)
82JS_STATIC_PROP_ENTRY(media)
83JS_STATIC_PROP_ENTRY(modDate)
84JS_STATIC_PROP_ENTRY(mouseX)
85JS_STATIC_PROP_ENTRY(mouseY)
86JS_STATIC_PROP_ENTRY(numFields)
87JS_STATIC_PROP_ENTRY(numPages)
88JS_STATIC_PROP_ENTRY(pageNum)
89JS_STATIC_PROP_ENTRY(pageWindowRect)
90JS_STATIC_PROP_ENTRY(path)
91JS_STATIC_PROP_ENTRY(producer)
92JS_STATIC_PROP_ENTRY(subject)
93JS_STATIC_PROP_ENTRY(title)
tonikitoo33c4cdb2016-08-08 10:52:51 -070094JS_STATIC_PROP_ENTRY(URL)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095JS_STATIC_PROP_ENTRY(zoom)
96JS_STATIC_PROP_ENTRY(zoomType)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097END_JS_STATIC_PROP()
98
99BEGIN_JS_STATIC_METHOD(CJS_Document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100JS_STATIC_METHOD_ENTRY(addAnnot)
101JS_STATIC_METHOD_ENTRY(addField)
102JS_STATIC_METHOD_ENTRY(addLink)
103JS_STATIC_METHOD_ENTRY(addIcon)
104JS_STATIC_METHOD_ENTRY(calculateNow)
105JS_STATIC_METHOD_ENTRY(closeDoc)
106JS_STATIC_METHOD_ENTRY(createDataObject)
107JS_STATIC_METHOD_ENTRY(deletePages)
108JS_STATIC_METHOD_ENTRY(exportAsText)
109JS_STATIC_METHOD_ENTRY(exportAsFDF)
110JS_STATIC_METHOD_ENTRY(exportAsXFDF)
111JS_STATIC_METHOD_ENTRY(extractPages)
112JS_STATIC_METHOD_ENTRY(getAnnot)
113JS_STATIC_METHOD_ENTRY(getAnnots)
114JS_STATIC_METHOD_ENTRY(getAnnot3D)
115JS_STATIC_METHOD_ENTRY(getAnnots3D)
116JS_STATIC_METHOD_ENTRY(getField)
117JS_STATIC_METHOD_ENTRY(getIcon)
118JS_STATIC_METHOD_ENTRY(getLinks)
119JS_STATIC_METHOD_ENTRY(getNthFieldName)
120JS_STATIC_METHOD_ENTRY(getOCGs)
121JS_STATIC_METHOD_ENTRY(getPageBox)
122JS_STATIC_METHOD_ENTRY(getPageNthWord)
123JS_STATIC_METHOD_ENTRY(getPageNthWordQuads)
124JS_STATIC_METHOD_ENTRY(getPageNumWords)
125JS_STATIC_METHOD_ENTRY(getPrintParams)
126JS_STATIC_METHOD_ENTRY(getURL)
tonikitoo1c836752016-08-08 16:14:05 -0700127JS_STATIC_METHOD_ENTRY(gotoNamedDest)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128JS_STATIC_METHOD_ENTRY(importAnFDF)
129JS_STATIC_METHOD_ENTRY(importAnXFDF)
130JS_STATIC_METHOD_ENTRY(importTextData)
131JS_STATIC_METHOD_ENTRY(insertPages)
132JS_STATIC_METHOD_ENTRY(mailForm)
133JS_STATIC_METHOD_ENTRY(print)
134JS_STATIC_METHOD_ENTRY(removeField)
135JS_STATIC_METHOD_ENTRY(replacePages)
136JS_STATIC_METHOD_ENTRY(resetForm)
137JS_STATIC_METHOD_ENTRY(removeIcon)
138JS_STATIC_METHOD_ENTRY(saveAs)
139JS_STATIC_METHOD_ENTRY(submitForm)
tonikitoobb5fa042016-08-19 11:18:29 -0700140JS_STATIC_METHOD_ENTRY(syncAnnotScan)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141JS_STATIC_METHOD_ENTRY(mailDoc)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142END_JS_STATIC_METHOD()
143
144IMPLEMENT_JS_CLASS(CJS_Document, Document)
145
Tom Sepez33420902015-10-13 15:00:10 -0700146void CJS_Document::InitInstance(IJS_Runtime* pIRuntime) {
147 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime);
148 Document* pDoc = static_cast<Document*>(GetEmbedObject());
149 pDoc->AttachDoc(pRuntime->GetReaderDocument());
Tom Sepez33420902015-10-13 15:00:10 -0700150}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700151
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152Document::Document(CJS_Object* pJSObject)
153 : CJS_EmbedObj(pJSObject),
thestig1cd352e2016-06-07 17:53:06 -0700154 m_pDocument(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 m_cwBaseURL(L""),
156 m_bDelay(FALSE) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700157
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158Document::~Document() {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159}
160
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161// the total number of fileds in document.
Tom Sepezba038bc2015-10-08 12:03:00 -0700162FX_BOOL Document::numFields(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 CJS_PropValue& vp,
164 CFX_WideString& sError) {
165 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700166 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700167 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 }
tsepez55be02e2016-09-12 11:21:42 -0700169 if (!m_pDocument) {
170 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
171 return FALSE;
172 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
174 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
175 vp << (int)pPDFForm->CountFields();
176 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700177}
178
Tom Sepezba038bc2015-10-08 12:03:00 -0700179FX_BOOL Document::dirty(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 CJS_PropValue& vp,
181 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700182 if (!m_pDocument) {
183 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
184 return FALSE;
185 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -0700187 vp << !!m_pDocument->GetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 } else {
189 bool bChanged = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 vp >> bChanged;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 if (bChanged)
193 m_pDocument->SetChangeMark();
194 else
195 m_pDocument->ClearChangeMark();
196 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198}
199
Tom Sepezba038bc2015-10-08 12:03:00 -0700200FX_BOOL Document::ADBE(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 CJS_PropValue& vp,
202 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700203 if (vp.IsGetting())
204 vp.GetJSValue()->SetNull(CJS_Runtime::FromContext(cc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205
206 return TRUE;
207}
208
Tom Sepezba038bc2015-10-08 12:03:00 -0700209FX_BOOL Document::pageNum(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 CJS_PropValue& vp,
211 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700212 if (!m_pDocument) {
213 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
214 return FALSE;
215 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 if (vp.IsGetting()) {
217 if (CPDFSDK_PageView* pPageView = m_pDocument->GetCurrentView()) {
218 vp << pPageView->GetPageIndex();
219 }
220 } else {
221 int iPageCount = m_pDocument->GetPageCount();
222 int iPageNum = 0;
223 vp >> iPageNum;
224
225 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
226 if (iPageNum >= 0 && iPageNum < iPageCount) {
227 pEnv->JS_docgotoPage(iPageNum);
228 } else if (iPageNum >= iPageCount) {
229 pEnv->JS_docgotoPage(iPageCount - 1);
230 } else if (iPageNum < 0) {
231 pEnv->JS_docgotoPage(0);
232 }
233 }
234
235 return TRUE;
236}
237
Tom Sepezba038bc2015-10-08 12:03:00 -0700238FX_BOOL Document::addAnnot(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800239 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 CJS_Value& vRet,
241 CFX_WideString& sError) {
242 // Not supported.
243 return TRUE;
244}
245
Tom Sepezba038bc2015-10-08 12:03:00 -0700246FX_BOOL Document::addField(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800247 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 CJS_Value& vRet,
249 CFX_WideString& sError) {
250 // Not supported.
251 return TRUE;
252}
253
Tom Sepezba038bc2015-10-08 12:03:00 -0700254FX_BOOL Document::exportAsText(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800255 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 CJS_Value& vRet,
257 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -0800258 // Unsafe, not supported.
259 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260}
261
Tom Sepezba038bc2015-10-08 12:03:00 -0700262FX_BOOL Document::exportAsFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800263 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 CJS_Value& vRet,
265 CFX_WideString& sError) {
266 // Unsafe, not supported.
267 return TRUE;
268}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700269
Tom Sepezba038bc2015-10-08 12:03:00 -0700270FX_BOOL Document::exportAsXFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800271 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 CJS_Value& vRet,
273 CFX_WideString& sError) {
274 // Unsafe, not supported.
275 return TRUE;
276}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700277
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278// Maps a field object in PDF document to a JavaScript variable
279// comment:
280// note: the paremter cName, this is clue how to treat if the cName is not a
281// valiable filed name in this document
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282
Tom Sepezba038bc2015-10-08 12:03:00 -0700283FX_BOOL Document::getField(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800284 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 CJS_Value& vRet,
286 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700288 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 return FALSE;
290 }
tsepez55be02e2016-09-12 11:21:42 -0700291 if (!m_pDocument) {
292 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
293 return FALSE;
294 }
tsepezcd5dc852016-09-08 11:23:24 -0700295 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700296 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -0700297 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
299 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
300 if (pPDFForm->CountFields(wideName) <= 0) {
tsepezf3dc8c62016-08-10 06:29:29 -0700301 vRet.SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302 return TRUE;
303 }
304
tsepezf3dc8c62016-08-10 06:29:29 -0700305 v8::Local<v8::Object> pFieldObj =
tsepezb4694242016-08-15 16:44:55 -0700306 pRuntime->NewFxDynamicObj(CJS_Field::g_nObjDefnID);
tsepezf3dc8c62016-08-10 06:29:29 -0700307 CJS_Field* pJSField =
tsepezb4694242016-08-15 16:44:55 -0700308 static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
tsepezf3dc8c62016-08-10 06:29:29 -0700309 Field* pField = static_cast<Field*>(pJSField->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 pField->AttachField(this, wideName);
311
tsepezf3dc8c62016-08-10 06:29:29 -0700312 vRet = CJS_Value(pRuntime, pJSField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 return TRUE;
314}
315
316// Gets the name of the nth field in the document
Tom Sepezba038bc2015-10-08 12:03:00 -0700317FX_BOOL Document::getNthFieldName(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800318 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 CJS_Value& vRet,
320 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700321 if (params.size() != 1) {
322 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
323 return FALSE;
324 }
tsepez55be02e2016-09-12 11:21:42 -0700325 if (!m_pDocument) {
326 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
327 return FALSE;
328 }
tsepezf3dc8c62016-08-10 06:29:29 -0700329 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezb4694242016-08-15 16:44:55 -0700330 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -0700331 int nIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332 if (nIndex < 0) {
tsepezcd5dc852016-09-08 11:23:24 -0700333 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 return FALSE;
335 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
337 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
338 CPDF_FormField* pField = pPDFForm->GetField(nIndex);
339 if (!pField)
340 return FALSE;
341
tsepezf3dc8c62016-08-10 06:29:29 -0700342 vRet = CJS_Value(pRuntime, pField->GetFullName().c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343 return TRUE;
344}
345
Tom Sepezba038bc2015-10-08 12:03:00 -0700346FX_BOOL Document::importAnFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800347 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348 CJS_Value& vRet,
349 CFX_WideString& sError) {
350 // Unsafe, not supported.
351 return TRUE;
352}
353
Tom Sepezba038bc2015-10-08 12:03:00 -0700354FX_BOOL Document::importAnXFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800355 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356 CJS_Value& vRet,
357 CFX_WideString& sError) {
358 // Unsafe, not supported.
359 return TRUE;
360}
361
Tom Sepezba038bc2015-10-08 12:03:00 -0700362FX_BOOL Document::importTextData(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800363 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364 CJS_Value& vRet,
365 CFX_WideString& sError) {
366 // Unsafe, not supported.
367 return TRUE;
368}
369
370// exports the form data and mails the resulting fdf file as an attachment to
371// all recipients.
372// comment: need reader supports
373// note:
374// int CPDFSDK_Document::mailForm(FX_BOOL bUI,String cto,string ccc,string
375// cbcc,string cSubject,string cms);
376
Tom Sepezba038bc2015-10-08 12:03:00 -0700377FX_BOOL Document::mailForm(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800378 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 CJS_Value& vRet,
380 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700381 if (!m_pDocument) {
382 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
383 return FALSE;
384 }
385 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
386 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
387 return FALSE;
388 }
389
tsepezf3dc8c62016-08-10 06:29:29 -0700390 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezb4694242016-08-15 16:44:55 -0700391 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -0700392
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393 int iLength = params.size();
tsepezb4694242016-08-15 16:44:55 -0700394 FX_BOOL bUI = iLength > 0 ? params[0].ToBool(pRuntime) : TRUE;
395 CFX_WideString cTo = iLength > 1 ? params[1].ToCFXWideString(pRuntime) : L"";
396 CFX_WideString cCc = iLength > 2 ? params[2].ToCFXWideString(pRuntime) : L"";
397 CFX_WideString cBcc = iLength > 3 ? params[3].ToCFXWideString(pRuntime) : L"";
tsepezf3dc8c62016-08-10 06:29:29 -0700398 CFX_WideString cSubject =
tsepezb4694242016-08-15 16:44:55 -0700399 iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
400 CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401
tsepez23ae4a52016-06-08 20:44:54 -0700402 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 CFX_ByteTextBuf textBuf;
404 if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
405 return FALSE;
406
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700407 pRuntime->BeginBlock();
tsepezf3dc8c62016-08-10 06:29:29 -0700408 CPDFDoc_Environment* pEnv = pContext->GetReaderApp();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 pEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI,
410 cTo.c_str(), cSubject.c_str(), cCc.c_str(), cBcc.c_str(),
411 cMsg.c_str());
412 pRuntime->EndBlock();
413 return TRUE;
414}
415
Tom Sepezba038bc2015-10-08 12:03:00 -0700416FX_BOOL Document::print(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800417 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 CJS_Value& vRet,
419 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700420 if (!m_pDocument) {
421 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
422 return FALSE;
423 }
424
tsepezf3dc8c62016-08-10 06:29:29 -0700425 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
426 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -0700427
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428 FX_BOOL bUI = TRUE;
429 int nStart = 0;
430 int nEnd = 0;
431 FX_BOOL bSilent = FALSE;
432 FX_BOOL bShrinkToFit = FALSE;
433 FX_BOOL bPrintAsImage = FALSE;
434 FX_BOOL bReverse = FALSE;
435 FX_BOOL bAnnotations = FALSE;
436
437 int nlength = params.size();
438 if (nlength == 9) {
tsepez40faa792016-07-15 17:58:02 -0700439 if (params[8].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700440 v8::Local<v8::Object> pObj = params[8].ToV8Object(pRuntime);
441 if (CFXJS_Engine::GetObjDefnID(pObj) ==
442 CJS_PrintParamsObj::g_nObjDefnID) {
443 if (CJS_Object* pJSObj = params[8].ToCJSObject(pRuntime)) {
tsepez40faa792016-07-15 17:58:02 -0700444 if (PrintParamsObj* pprintparamsObj =
445 static_cast<PrintParamsObj*>(pJSObj->GetEmbedObject())) {
446 bUI = pprintparamsObj->bUI;
447 nStart = pprintparamsObj->nStart;
448 nEnd = pprintparamsObj->nEnd;
449 bSilent = pprintparamsObj->bSilent;
450 bShrinkToFit = pprintparamsObj->bShrinkToFit;
451 bPrintAsImage = pprintparamsObj->bPrintAsImage;
452 bReverse = pprintparamsObj->bReverse;
453 bAnnotations = pprintparamsObj->bAnnotations;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700455 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 }
457 }
458 } else {
459 if (nlength >= 1)
tsepezb4694242016-08-15 16:44:55 -0700460 bUI = params[0].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461 if (nlength >= 2)
tsepezb4694242016-08-15 16:44:55 -0700462 nStart = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463 if (nlength >= 3)
tsepezb4694242016-08-15 16:44:55 -0700464 nEnd = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465 if (nlength >= 4)
tsepezb4694242016-08-15 16:44:55 -0700466 bSilent = params[3].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467 if (nlength >= 5)
tsepezb4694242016-08-15 16:44:55 -0700468 bShrinkToFit = params[4].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469 if (nlength >= 6)
tsepezb4694242016-08-15 16:44:55 -0700470 bPrintAsImage = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471 if (nlength >= 7)
tsepezb4694242016-08-15 16:44:55 -0700472 bReverse = params[6].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 if (nlength >= 8)
tsepezb4694242016-08-15 16:44:55 -0700474 bAnnotations = params[7].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475 }
476
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700477 if (CPDFDoc_Environment* pEnv = m_pDocument->GetEnv()) {
478 pEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit, bPrintAsImage,
479 bReverse, bAnnotations);
480 return TRUE;
481 }
482 return FALSE;
483}
484
485// removes the specified field from the document.
486// comment:
dsinclair1897bdc2016-09-06 14:18:57 -0700487// note: if the filed name is not rational, adobe is dumb for it.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488
Tom Sepezba038bc2015-10-08 12:03:00 -0700489FX_BOOL Document::removeField(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800490 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700491 CJS_Value& vRet,
492 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700493 if (params.size() != 1) {
494 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
495 return FALSE;
496 }
497 if (!m_pDocument) {
498 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
499 return FALSE;
500 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700501 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
tsepezcd5dc852016-09-08 11:23:24 -0700502 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM))) {
503 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700504 return FALSE;
505 }
tsepezcd5dc852016-09-08 11:23:24 -0700506 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700507 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -0700508 CFX_WideString sFieldName = params[0].ToCFXWideString(pRuntime);
tsepez23ae4a52016-06-08 20:44:54 -0700509 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Lei Zhangd88a3642015-11-10 09:38:57 -0800510 std::vector<CPDFSDK_Widget*> widgets;
511 pInterForm->GetWidgets(sFieldName, &widgets);
Lei Zhangd88a3642015-11-10 09:38:57 -0800512 if (widgets.empty())
513 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700514
Lei Zhangd88a3642015-11-10 09:38:57 -0800515 for (CPDFSDK_Widget* pWidget : widgets) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800516 CFX_FloatRect rcAnnot = pWidget->GetRect();
Lei Zhangd88a3642015-11-10 09:38:57 -0800517 --rcAnnot.left;
518 --rcAnnot.bottom;
519 ++rcAnnot.right;
520 ++rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521
tsepezdf964df2016-04-21 12:09:41 -0700522 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800523 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Lei Zhangd88a3642015-11-10 09:38:57 -0800524 ASSERT(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525
dsinclair1897bdc2016-09-06 14:18:57 -0700526 // If there is currently no pageview associated with the page being used
527 // do not create one. We may be in the process of tearing down the document
528 // and creating a new pageview at this point will cause bad things.
529 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, false);
530 if (pPageView) {
531 pPageView->DeleteAnnot(pWidget);
532 pPageView->UpdateRects(aRefresh);
533 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700534 }
Lei Zhangd88a3642015-11-10 09:38:57 -0800535 m_pDocument->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700536
537 return TRUE;
538}
539
540// reset filed values within a document.
541// comment:
542// note: if the fields names r not rational, aodbe is dumb for it.
543
Tom Sepezba038bc2015-10-08 12:03:00 -0700544FX_BOOL Document::resetForm(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800545 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546 CJS_Value& vRet,
547 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700548 if (!m_pDocument) {
549 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
550 return FALSE;
551 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700552 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
553 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM) ||
tsepez55be02e2016-09-12 11:21:42 -0700554 m_pDocument->GetPermissions(FPDFPERM_FILL_FORM))) {
555 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700556 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -0700557 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700558
tsepez23ae4a52016-06-08 20:44:54 -0700559 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
tsepeze5aff742016-08-08 09:49:42 -0700561 CJS_Array aName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562
Lei Zhangdd734de2015-11-11 10:16:52 -0800563 if (params.empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700564 pPDFForm->ResetForm(TRUE);
565 m_pDocument->SetChangeMark();
Lei Zhangd88a3642015-11-10 09:38:57 -0800566 return TRUE;
567 }
568
tsepez55be02e2016-09-12 11:21:42 -0700569 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700570 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -0700571
Lei Zhangd88a3642015-11-10 09:38:57 -0800572 switch (params[0].GetType()) {
573 default:
tsepezb4694242016-08-15 16:44:55 -0700574 aName.Attach(params[0].ToV8Array(pRuntime));
Lei Zhangd88a3642015-11-10 09:38:57 -0800575 break;
576 case CJS_Value::VT_string:
tsepezb4694242016-08-15 16:44:55 -0700577 aName.SetElement(pRuntime, 0, params[0]);
Lei Zhangd88a3642015-11-10 09:38:57 -0800578 break;
579 }
580
581 std::vector<CPDF_FormField*> aFields;
tsepezb4694242016-08-15 16:44:55 -0700582 for (int i = 0, isz = aName.GetLength(pRuntime); i < isz; ++i) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800583 CJS_Value valElement(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700584 aName.GetElement(pRuntime, i, valElement);
585 CFX_WideString swVal = valElement.ToCFXWideString(pRuntime);
Lei Zhangd88a3642015-11-10 09:38:57 -0800586 for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j)
587 aFields.push_back(pPDFForm->GetField(j, swVal));
588 }
589
590 if (!aFields.empty()) {
591 pPDFForm->ResetForm(aFields, TRUE, TRUE);
592 m_pDocument->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700593 }
594
595 return TRUE;
596}
597
Tom Sepezba038bc2015-10-08 12:03:00 -0700598FX_BOOL Document::saveAs(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800599 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700600 CJS_Value& vRet,
601 CFX_WideString& sError) {
602 // Unsafe, not supported.
603 return TRUE;
604}
605
tonikitoobb5fa042016-08-19 11:18:29 -0700606FX_BOOL Document::syncAnnotScan(IJS_Context* cc,
607 const std::vector<CJS_Value>& params,
608 CJS_Value& vRet,
609 CFX_WideString& sError) {
610 return TRUE;
611}
612
Tom Sepezba038bc2015-10-08 12:03:00 -0700613FX_BOOL Document::submitForm(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800614 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700615 CJS_Value& vRet,
616 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700617
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700618 int nSize = params.size();
619 if (nSize < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700620 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700621 return FALSE;
622 }
tsepez55be02e2016-09-12 11:21:42 -0700623 if (!m_pDocument) {
624 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
625 return FALSE;
626 }
tsepezcd5dc852016-09-08 11:23:24 -0700627 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
628 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepeze5aff742016-08-08 09:49:42 -0700629 CJS_Array aFields;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700630 CFX_WideString strURL;
631 FX_BOOL bFDF = TRUE;
632 FX_BOOL bEmpty = FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700633 CJS_Value v = params[0];
Tom Sepez39bfe122015-09-17 15:25:23 -0700634 if (v.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700635 strURL = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700636 if (nSize > 1)
tsepezb4694242016-08-15 16:44:55 -0700637 bFDF = params[1].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700638 if (nSize > 2)
tsepezb4694242016-08-15 16:44:55 -0700639 bEmpty = params[2].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640 if (nSize > 3)
tsepezb4694242016-08-15 16:44:55 -0700641 aFields.Attach(params[3].ToV8Array(pRuntime));
Tom Sepez39bfe122015-09-17 15:25:23 -0700642 } else if (v.GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700643 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
644 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"cURL");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700645 if (!pValue.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700646 strURL = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700647
tsepezb4694242016-08-15 16:44:55 -0700648 pValue = pRuntime->GetObjectProperty(pObj, L"bFDF");
649 bFDF = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700650
tsepezb4694242016-08-15 16:44:55 -0700651 pValue = pRuntime->GetObjectProperty(pObj, L"bEmpty");
652 bEmpty = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700653
tsepezb4694242016-08-15 16:44:55 -0700654 pValue = pRuntime->GetObjectProperty(pObj, L"aFields");
655 aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700656 }
657
tsepez23ae4a52016-06-08 20:44:54 -0700658 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700659 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
tsepezb4694242016-08-15 16:44:55 -0700660 if (aFields.GetLength(pRuntime) == 0 && bEmpty) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800661 if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700662 pRuntime->BeginBlock();
663 pInterForm->SubmitForm(strURL, FALSE);
664 pRuntime->EndBlock();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700665 }
666 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700667 }
668
Lei Zhangd88a3642015-11-10 09:38:57 -0800669 std::vector<CPDF_FormField*> fieldObjects;
tsepezb4694242016-08-15 16:44:55 -0700670 for (int i = 0, sz = aFields.GetLength(pRuntime); i < sz; ++i) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700671 CJS_Value valName(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700672 aFields.GetElement(pRuntime, i, valName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700673
tsepezb4694242016-08-15 16:44:55 -0700674 CFX_WideString sName = valName.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
676 for (int j = 0, jsz = pPDFForm->CountFields(sName); j < jsz; ++j) {
677 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
678 if (!bEmpty && pField->GetValue().IsEmpty())
679 continue;
680
Lei Zhangd88a3642015-11-10 09:38:57 -0800681 fieldObjects.push_back(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700682 }
683 }
684
Lei Zhangd88a3642015-11-10 09:38:57 -0800685 if (pPDFInterForm->CheckRequiredFields(&fieldObjects, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700686 pRuntime->BeginBlock();
Wei Li97da9762016-03-11 17:00:48 -0800687 pInterForm->SubmitFields(strURL, fieldObjects, true, !bFDF);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700688 pRuntime->EndBlock();
689 }
690 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700691}
692
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700693void Document::AttachDoc(CPDFSDK_Document* pDoc) {
tsepez55be02e2016-09-12 11:21:42 -0700694 m_pDocument.Reset(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700695}
696
Tom Sepezba038bc2015-10-08 12:03:00 -0700697FX_BOOL Document::bookmarkRoot(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700698 CJS_PropValue& vp,
699 CFX_WideString& sError) {
700 return TRUE;
701}
702
Tom Sepezba038bc2015-10-08 12:03:00 -0700703FX_BOOL Document::mailDoc(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800704 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700705 CJS_Value& vRet,
706 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700707 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
708
709 // TODO(tsepez): Check maximum number of allowed params.
710
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711 FX_BOOL bUI = TRUE;
712 CFX_WideString cTo = L"";
713 CFX_WideString cCc = L"";
714 CFX_WideString cBcc = L"";
715 CFX_WideString cSubject = L"";
716 CFX_WideString cMsg = L"";
717
tsepezf3dc8c62016-08-10 06:29:29 -0700718 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719
tsepezf3dc8c62016-08-10 06:29:29 -0700720 if (params.size() >= 1)
tsepezb4694242016-08-15 16:44:55 -0700721 bUI = params[0].ToBool(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700722 if (params.size() >= 2)
tsepezb4694242016-08-15 16:44:55 -0700723 cTo = params[1].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700724 if (params.size() >= 3)
tsepezb4694242016-08-15 16:44:55 -0700725 cCc = params[2].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700726 if (params.size() >= 4)
tsepezb4694242016-08-15 16:44:55 -0700727 cBcc = params[3].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700728 if (params.size() >= 5)
tsepezb4694242016-08-15 16:44:55 -0700729 cSubject = params[4].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700730 if (params.size() >= 6)
tsepezb4694242016-08-15 16:44:55 -0700731 cMsg = params[5].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700732
Tom Sepez39bfe122015-09-17 15:25:23 -0700733 if (params.size() >= 1 && params[0].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700734 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700735
tsepezb4694242016-08-15 16:44:55 -0700736 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"bUI");
737 bUI = CJS_Value(pRuntime, pValue).ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700738
tsepezb4694242016-08-15 16:44:55 -0700739 pValue = pRuntime->GetObjectProperty(pObj, L"cTo");
740 cTo = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700741
tsepezb4694242016-08-15 16:44:55 -0700742 pValue = pRuntime->GetObjectProperty(pObj, L"cCc");
743 cCc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700744
tsepezb4694242016-08-15 16:44:55 -0700745 pValue = pRuntime->GetObjectProperty(pObj, L"cBcc");
746 cBcc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700747
tsepezb4694242016-08-15 16:44:55 -0700748 pValue = pRuntime->GetObjectProperty(pObj, L"cSubject");
749 cSubject = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700750
tsepezb4694242016-08-15 16:44:55 -0700751 pValue = pRuntime->GetObjectProperty(pObj, L"cMsg");
752 cMsg = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700753 }
754
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700755 pRuntime->BeginBlock();
756 CPDFDoc_Environment* pEnv = pRuntime->GetReaderApp();
thestig1cd352e2016-06-07 17:53:06 -0700757 pEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
758 cCc.c_str(), cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700759 pRuntime->EndBlock();
760
761 return TRUE;
762}
763
Tom Sepezba038bc2015-10-08 12:03:00 -0700764FX_BOOL Document::author(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700765 CJS_PropValue& vp,
766 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700767 return getPropertyInternal(cc, vp, "Author", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700768}
769
Tom Sepezba038bc2015-10-08 12:03:00 -0700770FX_BOOL Document::info(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700771 CJS_PropValue& vp,
772 CFX_WideString& sError) {
tonikitooec268762016-08-10 12:29:28 -0700773 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700774 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tonikitooec268762016-08-10 12:29:28 -0700775 return FALSE;
776 }
tsepez55be02e2016-09-12 11:21:42 -0700777 if (!m_pDocument) {
778 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
779 return FALSE;
780 }
Tom Sepez50d12ad2015-11-24 09:50:51 -0800781 CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700782 if (!pDictionary)
783 return FALSE;
Tom Sepezc6ab1722015-02-05 15:27:25 -0800784
Wei Li9b761132016-01-29 15:44:20 -0800785 CFX_WideString cwAuthor = pDictionary->GetUnicodeTextBy("Author");
786 CFX_WideString cwTitle = pDictionary->GetUnicodeTextBy("Title");
787 CFX_WideString cwSubject = pDictionary->GetUnicodeTextBy("Subject");
788 CFX_WideString cwKeywords = pDictionary->GetUnicodeTextBy("Keywords");
789 CFX_WideString cwCreator = pDictionary->GetUnicodeTextBy("Creator");
790 CFX_WideString cwProducer = pDictionary->GetUnicodeTextBy("Producer");
791 CFX_WideString cwCreationDate = pDictionary->GetUnicodeTextBy("CreationDate");
792 CFX_WideString cwModDate = pDictionary->GetUnicodeTextBy("ModDate");
793 CFX_WideString cwTrapped = pDictionary->GetUnicodeTextBy("Trapped");
Tom Sepezc6ab1722015-02-05 15:27:25 -0800794
tonikitooec268762016-08-10 12:29:28 -0700795 CJS_Context* pContext = (CJS_Context*)cc;
796 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
Tom Sepezc6ab1722015-02-05 15:27:25 -0800797
tsepezb4694242016-08-15 16:44:55 -0700798 v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1);
799 pRuntime->PutObjectString(pObj, L"Author", cwAuthor);
800 pRuntime->PutObjectString(pObj, L"Title", cwTitle);
801 pRuntime->PutObjectString(pObj, L"Subject", cwSubject);
802 pRuntime->PutObjectString(pObj, L"Keywords", cwKeywords);
803 pRuntime->PutObjectString(pObj, L"Creator", cwCreator);
804 pRuntime->PutObjectString(pObj, L"Producer", cwProducer);
805 pRuntime->PutObjectString(pObj, L"CreationDate", cwCreationDate);
806 pRuntime->PutObjectString(pObj, L"ModDate", cwModDate);
807 pRuntime->PutObjectString(pObj, L"Trapped", cwTrapped);
tonikitooec268762016-08-10 12:29:28 -0700808
809 // It's to be compatible to non-standard info dictionary.
810 for (const auto& it : *pDictionary) {
811 const CFX_ByteString& bsKey = it.first;
812 CPDF_Object* pValueObj = it.second;
813 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
814 if (pValueObj->IsString() || pValueObj->IsName()) {
tsepezb4694242016-08-15 16:44:55 -0700815 pRuntime->PutObjectString(pObj, wsKey, pValueObj->GetUnicodeText());
tonikitooec268762016-08-10 12:29:28 -0700816 } else if (pValueObj->IsNumber()) {
tsepezb4694242016-08-15 16:44:55 -0700817 pRuntime->PutObjectNumber(pObj, wsKey, (float)pValueObj->GetNumber());
tonikitooec268762016-08-10 12:29:28 -0700818 } else if (pValueObj->IsBoolean()) {
tsepezb4694242016-08-15 16:44:55 -0700819 pRuntime->PutObjectBoolean(pObj, wsKey, !!pValueObj->GetInteger());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700820 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700821 }
tonikitooec268762016-08-10 12:29:28 -0700822 vp << pObj;
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700823 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700824}
825
tonikitooea3ff9e2016-08-02 11:52:28 -0700826FX_BOOL Document::getPropertyInternal(IJS_Context* cc,
827 CJS_PropValue& vp,
828 const CFX_ByteString& propName,
829 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700830 if (!m_pDocument) {
831 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
832 return FALSE;
833 }
Tom Sepez50d12ad2015-11-24 09:50:51 -0800834 CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700835 if (!pDictionary)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700836 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700837
838 if (vp.IsGetting()) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700839 vp << pDictionary->GetUnicodeTextBy(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700840 } else {
tsepez55be02e2016-09-12 11:21:42 -0700841 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) {
842 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700843 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -0700844 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700845 CFX_WideString csProperty;
846 vp >> csProperty;
847 pDictionary->SetAtString(propName, PDF_EncodeText(csProperty));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700848 m_pDocument->SetChangeMark();
849 }
850 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700851}
852
tonikitooea3ff9e2016-08-02 11:52:28 -0700853FX_BOOL Document::creationDate(IJS_Context* cc,
854 CJS_PropValue& vp,
855 CFX_WideString& sError) {
856 return getPropertyInternal(cc, vp, "CreationDate", sError);
857}
858
Tom Sepezba038bc2015-10-08 12:03:00 -0700859FX_BOOL Document::creator(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700860 CJS_PropValue& vp,
861 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700862 return getPropertyInternal(cc, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700863}
864
Tom Sepezba038bc2015-10-08 12:03:00 -0700865FX_BOOL Document::delay(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866 CJS_PropValue& vp,
867 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700868 if (!m_pDocument) {
869 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
870 return FALSE;
871 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700872 if (vp.IsGetting()) {
873 vp << m_bDelay;
874 } else {
tsepez55be02e2016-09-12 11:21:42 -0700875 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) {
876 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -0700878 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700879 vp >> m_bDelay;
880 if (m_bDelay) {
Tom Sepez56c10192016-03-15 12:34:17 -0700881 m_DelayData.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700882 } else {
Tom Sepez56c10192016-03-15 12:34:17 -0700883 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
884 DelayDataToProcess.swap(m_DelayData);
885 for (const auto& pData : DelayDataToProcess)
tsepez55be02e2016-09-12 11:21:42 -0700886 Field::DoDelay(m_pDocument.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887 }
888 }
889 return TRUE;
890}
891
Tom Sepezba038bc2015-10-08 12:03:00 -0700892FX_BOOL Document::keywords(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893 CJS_PropValue& vp,
894 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700895 return getPropertyInternal(cc, vp, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700896}
897
Tom Sepezba038bc2015-10-08 12:03:00 -0700898FX_BOOL Document::modDate(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899 CJS_PropValue& vp,
900 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700901 return getPropertyInternal(cc, vp, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700902}
903
Tom Sepezba038bc2015-10-08 12:03:00 -0700904FX_BOOL Document::producer(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700905 CJS_PropValue& vp,
906 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700907 return getPropertyInternal(cc, vp, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700908}
909
Tom Sepezba038bc2015-10-08 12:03:00 -0700910FX_BOOL Document::subject(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700911 CJS_PropValue& vp,
912 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700913 return getPropertyInternal(cc, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700914}
915
Tom Sepezba038bc2015-10-08 12:03:00 -0700916FX_BOOL Document::title(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917 CJS_PropValue& vp,
918 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700919 if (!m_pDocument || !m_pDocument->GetUnderlyingDocument()) {
920 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700921 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -0700922 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700923 return getPropertyInternal(cc, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700924}
925
Tom Sepezba038bc2015-10-08 12:03:00 -0700926FX_BOOL Document::numPages(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700927 CJS_PropValue& vp,
928 CFX_WideString& sError) {
929 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700930 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700931 return FALSE;
932 }
tsepez55be02e2016-09-12 11:21:42 -0700933 if (!m_pDocument) {
934 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
935 return FALSE;
936 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937 vp << m_pDocument->GetPageCount();
938 return TRUE;
939}
940
Tom Sepezba038bc2015-10-08 12:03:00 -0700941FX_BOOL Document::external(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700942 CJS_PropValue& vp,
943 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700944 // In Chrome case, should always return true.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945 if (vp.IsGetting()) {
946 vp << true;
947 }
948 return TRUE;
949}
950
Tom Sepezba038bc2015-10-08 12:03:00 -0700951FX_BOOL Document::filesize(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700952 CJS_PropValue& vp,
953 CFX_WideString& sError) {
954 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700955 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700956 return FALSE;
957 }
958 vp << 0;
959 return TRUE;
960}
961
Tom Sepezba038bc2015-10-08 12:03:00 -0700962FX_BOOL Document::mouseX(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700963 CJS_PropValue& vp,
964 CFX_WideString& sError) {
965 return TRUE;
966}
967
Tom Sepezba038bc2015-10-08 12:03:00 -0700968FX_BOOL Document::mouseY(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700969 CJS_PropValue& vp,
970 CFX_WideString& sError) {
971 return TRUE;
972}
973
tonikitoo33c4cdb2016-08-08 10:52:51 -0700974FX_BOOL Document::URL(IJS_Context* cc,
975 CJS_PropValue& vp,
976 CFX_WideString& sError) {
977 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700978 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tonikitoo33c4cdb2016-08-08 10:52:51 -0700979 return FALSE;
980 }
tsepez55be02e2016-09-12 11:21:42 -0700981 if (!m_pDocument) {
982 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
983 return FALSE;
984 }
tonikitoo33c4cdb2016-08-08 10:52:51 -0700985 vp << m_pDocument->GetPath();
986 return TRUE;
987}
988
Tom Sepezba038bc2015-10-08 12:03:00 -0700989FX_BOOL Document::baseURL(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700990 CJS_PropValue& vp,
991 CFX_WideString& sError) {
992 if (vp.IsGetting()) {
993 vp << m_cwBaseURL;
994 } else {
995 vp >> m_cwBaseURL;
996 }
997 return TRUE;
998}
999
Tom Sepezba038bc2015-10-08 12:03:00 -07001000FX_BOOL Document::calculate(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001001 CJS_PropValue& vp,
1002 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001003 if (!m_pDocument) {
1004 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1005 return FALSE;
1006 }
tsepez23ae4a52016-06-08 20:44:54 -07001007 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -07001009 vp << !!pInterForm->IsCalculateEnabled();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001010 } else {
1011 bool bCalculate;
1012 vp >> bCalculate;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001013 pInterForm->EnableCalculate(bCalculate);
1014 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001015 return TRUE;
1016}
1017
Tom Sepezba038bc2015-10-08 12:03:00 -07001018FX_BOOL Document::documentFileName(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019 CJS_PropValue& vp,
1020 CFX_WideString& sError) {
1021 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001022 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001023 return FALSE;
1024 }
tsepez55be02e2016-09-12 11:21:42 -07001025 if (!m_pDocument) {
1026 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1027 return FALSE;
1028 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001029 CFX_WideString wsFilePath = m_pDocument->GetPath();
1030 int32_t i = wsFilePath.GetLength() - 1;
1031 for (; i >= 0; i--) {
1032 if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
1033 break;
1034 }
1035 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
1036 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
1037 } else {
1038 vp << L"";
1039 }
1040 return TRUE;
1041}
1042
Tom Sepezba038bc2015-10-08 12:03:00 -07001043FX_BOOL Document::path(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044 CJS_PropValue& vp,
1045 CFX_WideString& sError) {
1046 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001047 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001048 return FALSE;
1049 }
tsepez55be02e2016-09-12 11:21:42 -07001050 if (!m_pDocument) {
1051 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1052 return FALSE;
1053 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001054 vp << app::SysPathToPDFPath(m_pDocument->GetPath());
1055 return TRUE;
1056}
1057
Tom Sepezba038bc2015-10-08 12:03:00 -07001058FX_BOOL Document::pageWindowRect(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059 CJS_PropValue& vp,
1060 CFX_WideString& sError) {
1061 return TRUE;
1062}
1063
Tom Sepezba038bc2015-10-08 12:03:00 -07001064FX_BOOL Document::layout(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001065 CJS_PropValue& vp,
1066 CFX_WideString& sError) {
1067 return TRUE;
1068}
1069
Tom Sepezba038bc2015-10-08 12:03:00 -07001070FX_BOOL Document::addLink(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001071 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001072 CJS_Value& vRet,
1073 CFX_WideString& sError) {
1074 return TRUE;
1075}
1076
Tom Sepezba038bc2015-10-08 12:03:00 -07001077FX_BOOL Document::closeDoc(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001078 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079 CJS_Value& vRet,
1080 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001081 return TRUE;
1082}
1083
Tom Sepezba038bc2015-10-08 12:03:00 -07001084FX_BOOL Document::getPageBox(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001085 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001086 CJS_Value& vRet,
1087 CFX_WideString& sError) {
1088 return TRUE;
1089}
1090
Tom Sepezba038bc2015-10-08 12:03:00 -07001091FX_BOOL Document::getAnnot(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001092 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093 CJS_Value& vRet,
1094 CFX_WideString& sError) {
tonikitoo618cb1f2016-08-18 20:10:17 -07001095 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001096 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tonikitoo618cb1f2016-08-18 20:10:17 -07001097 return FALSE;
1098 }
tsepez55be02e2016-09-12 11:21:42 -07001099 if (!m_pDocument) {
1100 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1101 return FALSE;
1102 }
tsepezcd5dc852016-09-08 11:23:24 -07001103 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tonikitoo618cb1f2016-08-18 20:10:17 -07001104 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1105 int nPageNo = params[0].ToInt(pRuntime);
1106 CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
tonikitoo618cb1f2016-08-18 20:10:17 -07001107 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(nPageNo);
1108 if (!pPageView)
1109 return FALSE;
1110
1111 CPDFSDK_AnnotIterator annotIterator(pPageView, false);
1112 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
1113 while (CPDFSDK_Annot* pSDKAnnotCur = annotIterator.Next()) {
1114 CPDFSDK_BAAnnot* pBAAnnot = static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur);
1115 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1116 pSDKBAAnnot = pBAAnnot;
1117 break;
1118 }
1119 }
1120
1121 if (!pSDKBAAnnot)
1122 return FALSE;
1123
1124 v8::Local<v8::Object> pObj =
1125 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1126 if (pObj.IsEmpty())
1127 return FALSE;
1128
1129 CJS_Annot* pJS_Annot =
1130 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1131 if (!pJS_Annot)
1132 return FALSE;
1133
1134 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1135 if (!pAnnot)
1136 return FALSE;
1137
1138 pAnnot->SetSDKAnnot(pSDKBAAnnot);
1139
1140 vRet = CJS_Value(pRuntime, pJS_Annot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001141 return TRUE;
1142}
1143
Tom Sepezba038bc2015-10-08 12:03:00 -07001144FX_BOOL Document::getAnnots(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001145 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001146 CJS_Value& vRet,
1147 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001148 if (!m_pDocument) {
1149 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1150 return FALSE;
1151 }
tonikitoo0ee35902016-08-26 14:41:29 -07001152 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepez55be02e2016-09-12 11:21:42 -07001153 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tonikitoo0ee35902016-08-26 14:41:29 -07001154
1155 // TODO(tonikitoo): Add support supported parameters as per
1156 // the PDF spec.
1157
tonikitoo0ee35902016-08-26 14:41:29 -07001158 int nPageNo = m_pDocument->GetPageCount();
1159 CJS_Array annots;
1160
1161 for (int i = 0; i < nPageNo; ++i) {
1162 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(i);
1163 if (!pPageView)
1164 return FALSE;
1165
1166 CPDFSDK_AnnotIterator annotIterator(pPageView, false);
1167 while (CPDFSDK_Annot* pSDKAnnotCur = annotIterator.Next()) {
1168 CPDFSDK_BAAnnot* pSDKBAAnnot =
1169 static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur);
1170 if (!pSDKBAAnnot)
1171 return FALSE;
1172
1173 v8::Local<v8::Object> pObj =
1174 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1175 if (pObj.IsEmpty())
1176 return FALSE;
1177
1178 CJS_Annot* pJS_Annot =
1179 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1180 if (!pJS_Annot)
1181 return FALSE;
1182
1183 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1184 if (!pAnnot)
1185 return FALSE;
1186
1187 pAnnot->SetSDKAnnot(pSDKBAAnnot);
1188 annots.SetElement(pRuntime, i, CJS_Value(pRuntime, pJS_Annot));
1189 }
1190 }
1191
1192 vRet = CJS_Value(pRuntime, annots);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193 return TRUE;
1194}
1195
Tom Sepezba038bc2015-10-08 12:03:00 -07001196FX_BOOL Document::getAnnot3D(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001197 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001198 CJS_Value& vRet,
1199 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001200 vRet.SetNull(CJS_Runtime::FromContext(cc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001201 return TRUE;
1202}
1203
Tom Sepezba038bc2015-10-08 12:03:00 -07001204FX_BOOL Document::getAnnots3D(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001205 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001206 CJS_Value& vRet,
1207 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001208 return TRUE;
1209}
1210
Tom Sepezba038bc2015-10-08 12:03:00 -07001211FX_BOOL Document::getOCGs(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001212 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001213 CJS_Value& vRet,
1214 CFX_WideString& sError) {
1215 return TRUE;
1216}
1217
Tom Sepezba038bc2015-10-08 12:03:00 -07001218FX_BOOL Document::getLinks(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001219 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001220 CJS_Value& vRet,
1221 CFX_WideString& sError) {
1222 return TRUE;
1223}
1224
1225bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1226 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1227 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1228}
1229
Tom Sepezba038bc2015-10-08 12:03:00 -07001230FX_BOOL Document::addIcon(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001231 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001232 CJS_Value& vRet,
1233 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001234
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001235 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001236 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001237 return FALSE;
1238 }
tsepezf3dc8c62016-08-10 06:29:29 -07001239
tsepezcd5dc852016-09-08 11:23:24 -07001240 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -07001241 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -07001242 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Tom Sepezaecd9d02015-08-31 15:04:12 -07001243
Tom Sepez39bfe122015-09-17 15:25:23 -07001244 if (params[1].GetType() != CJS_Value::VT_object) {
tsepezcd5dc852016-09-08 11:23:24 -07001245 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
Tom Sepezaecd9d02015-08-31 15:04:12 -07001246 return FALSE;
1247 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001248
tsepezb4694242016-08-15 16:44:55 -07001249 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pRuntime);
1250 if (pRuntime->GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
tsepezcd5dc852016-09-08 11:23:24 -07001251 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001252 return FALSE;
1253 }
1254
tsepezb4694242016-08-15 16:44:55 -07001255 CJS_EmbedObj* pEmbedObj = params[1].ToCJSObject(pRuntime)->GetEmbedObject();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001256 if (!pEmbedObj) {
tsepezcd5dc852016-09-08 11:23:24 -07001257 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258 return FALSE;
1259 }
1260
Tom Sepez6ba32d92016-01-05 16:26:32 -08001261 m_IconList.push_back(std::unique_ptr<IconElement>(
1262 new IconElement(swIconName, (Icon*)pEmbedObj)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001263 return TRUE;
1264}
1265
Tom Sepezba038bc2015-10-08 12:03:00 -07001266FX_BOOL Document::icons(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267 CJS_PropValue& vp,
1268 CFX_WideString& sError) {
1269 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001270 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001271 return FALSE;
1272 }
1273
tsepezf3dc8c62016-08-10 06:29:29 -07001274 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Tom Sepez6ba32d92016-01-05 16:26:32 -08001275 if (m_IconList.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001276 vp.GetJSValue()->SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277 return TRUE;
1278 }
1279
tsepeze5aff742016-08-08 09:49:42 -07001280 CJS_Array Icons;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001281
Tom Sepez6ba32d92016-01-05 16:26:32 -08001282 int i = 0;
1283 for (const auto& pIconElement : m_IconList) {
tsepezb4694242016-08-15 16:44:55 -07001284 v8::Local<v8::Object> pObj =
1285 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001286 if (pObj.IsEmpty())
1287 return FALSE;
1288
tsepezb4694242016-08-15 16:44:55 -07001289 CJS_Icon* pJS_Icon =
1290 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001291 if (!pJS_Icon)
1292 return FALSE;
1293
1294 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1295 if (!pIcon)
1296 return FALSE;
1297
1298 pIcon->SetStream(pIconElement->IconStream->GetStream());
1299 pIcon->SetIconName(pIconElement->IconName);
tsepezb4694242016-08-15 16:44:55 -07001300 Icons.SetElement(pRuntime, i++, CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001301 }
1302
1303 vp << Icons;
1304 return TRUE;
1305}
1306
Tom Sepezba038bc2015-10-08 12:03:00 -07001307FX_BOOL Document::getIcon(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001308 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001309 CJS_Value& vRet,
1310 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001311 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001312 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001313 return FALSE;
1314 }
1315
Tom Sepez6ba32d92016-01-05 16:26:32 -08001316 if (m_IconList.empty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001317 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001318
tsepezcd5dc852016-09-08 11:23:24 -07001319 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001320 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001321
Tom Sepez6ba32d92016-01-05 16:26:32 -08001322 for (const auto& pIconElement : m_IconList) {
1323 if (pIconElement->IconName == swIconName) {
1324 Icon* pRetIcon = pIconElement->IconStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001325
tsepezb4694242016-08-15 16:44:55 -07001326 v8::Local<v8::Object> pObj =
1327 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001328 if (pObj.IsEmpty())
1329 return FALSE;
1330
tsepezb4694242016-08-15 16:44:55 -07001331 CJS_Icon* pJS_Icon =
1332 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001333 if (!pJS_Icon)
1334 return FALSE;
1335
1336 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1337 if (!pIcon)
1338 return FALSE;
1339
1340 pIcon->SetIconName(swIconName);
1341 pIcon->SetStream(pRetIcon->GetStream());
tsepezf3dc8c62016-08-10 06:29:29 -07001342 vRet = CJS_Value(pRuntime, pJS_Icon);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001343 return TRUE;
1344 }
1345 }
1346
1347 return FALSE;
1348}
1349
Tom Sepezba038bc2015-10-08 12:03:00 -07001350FX_BOOL Document::removeIcon(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001351 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001352 CJS_Value& vRet,
1353 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001354 // Unsafe, no supported.
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001355 return TRUE;
1356}
1357
Tom Sepezba038bc2015-10-08 12:03:00 -07001358FX_BOOL Document::createDataObject(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001359 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001360 CJS_Value& vRet,
1361 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001362 // Unsafe, not implemented.
1363 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001364}
1365
Tom Sepezba038bc2015-10-08 12:03:00 -07001366FX_BOOL Document::media(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001367 CJS_PropValue& vp,
1368 CFX_WideString& sError) {
1369 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001370}
1371
Tom Sepezba038bc2015-10-08 12:03:00 -07001372FX_BOOL Document::calculateNow(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001373 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001374 CJS_Value& vRet,
1375 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001376 if (!m_pDocument) {
1377 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1378 return FALSE;
1379 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001380 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001381 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM) ||
tsepez55be02e2016-09-12 11:21:42 -07001382 m_pDocument->GetPermissions(FPDFPERM_FILL_FORM))) {
1383 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001384 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -07001385 }
1386 m_pDocument->GetInterForm()->OnCalculate();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001387 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001388}
1389
Tom Sepezba038bc2015-10-08 12:03:00 -07001390FX_BOOL Document::Collab(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001391 CJS_PropValue& vp,
1392 CFX_WideString& sError) {
1393 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001394}
1395
Tom Sepezba038bc2015-10-08 12:03:00 -07001396FX_BOOL Document::getPageNthWord(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001397 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001398 CJS_Value& vRet,
1399 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001400 if (!m_pDocument) {
1401 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1402 return FALSE;
1403 }
tsepezcd5dc852016-09-08 11:23:24 -07001404 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
1405 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001406 return FALSE;
tsepezcd5dc852016-09-08 11:23:24 -07001407 }
1408 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepez55be02e2016-09-12 11:21:42 -07001409
1410 // TODO(tsepez): check maximum allowable params.
1411
tsepezb4694242016-08-15 16:44:55 -07001412 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
1413 int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
1414 bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001415
Tom Sepez50d12ad2015-11-24 09:50:51 -08001416 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001417 if (!pDocument)
1418 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001419
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001420 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001421 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001422 return FALSE;
1423 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001424
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001425 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1426 if (!pPageDict)
1427 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001428
thestig5cc24652016-04-26 11:46:02 -07001429 CPDF_Page page(pDocument, pPageDict, true);
1430 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001431
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001432 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001433 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001434 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001435 if (pPageObj->IsText()) {
1436 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001437 int nObjWords = CountWords(pTextObj);
1438 if (nWords + nObjWords >= nWordNo) {
1439 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1440 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001441 }
Tom Sepez2398d892016-02-17 16:46:26 -08001442 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001443 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001444 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001445
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001446 if (bStrip) {
1447 swRet.TrimLeft();
1448 swRet.TrimRight();
1449 }
1450
tsepezf3dc8c62016-08-10 06:29:29 -07001451 vRet = CJS_Value(pRuntime, swRet.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001452 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001453}
1454
Tom Sepezba038bc2015-10-08 12:03:00 -07001455FX_BOOL Document::getPageNthWordQuads(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001456 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001457 CJS_Value& vRet,
1458 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001459 if (!m_pDocument) {
1460 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001461 return FALSE;
tsepez55be02e2016-09-12 11:21:42 -07001462 }
1463 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
1464 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1465 return FALSE;
1466 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001467 return FALSE;
1468}
1469
Tom Sepezba038bc2015-10-08 12:03:00 -07001470FX_BOOL Document::getPageNumWords(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001471 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001472 CJS_Value& vRet,
1473 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -07001474 if (!m_pDocument) {
1475 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1476 return FALSE;
1477 }
tsepezcd5dc852016-09-08 11:23:24 -07001478 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
1479 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001480 return FALSE;
tsepezcd5dc852016-09-08 11:23:24 -07001481 }
1482 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001483 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
Tom Sepez50d12ad2015-11-24 09:50:51 -08001484 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001485 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001486 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001487 return FALSE;
1488 }
1489
1490 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1491 if (!pPageDict)
1492 return FALSE;
1493
thestig5cc24652016-04-26 11:46:02 -07001494 CPDF_Page page(pDocument, pPageDict, true);
1495 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001496
1497 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001498 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001499 if (pPageObj->IsText())
1500 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001501 }
1502
tsepezf3dc8c62016-08-10 06:29:29 -07001503 vRet = CJS_Value(pRuntime, nWords);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001504 return TRUE;
1505}
1506
Tom Sepezba038bc2015-10-08 12:03:00 -07001507FX_BOOL Document::getPrintParams(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001508 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001509 CJS_Value& vRet,
1510 CFX_WideString& sError) {
1511 CJS_Context* pContext = (CJS_Context*)cc;
1512 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -07001513 v8::Local<v8::Object> pRetObj =
1514 pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001515
1516 // Not implemented yet.
1517
tsepezf3dc8c62016-08-10 06:29:29 -07001518 vRet = CJS_Value(pRuntime, pRetObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001519 return TRUE;
1520}
1521
1522#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1523
1524int Document::CountWords(CPDF_TextObject* pTextObj) {
1525 if (!pTextObj)
1526 return 0;
1527
1528 int nWords = 0;
1529
1530 CPDF_Font* pFont = pTextObj->GetFont();
1531 if (!pFont)
1532 return 0;
1533
1534 FX_BOOL bIsLatin = FALSE;
1535
1536 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001537 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001538 FX_FLOAT kerning;
1539
1540 pTextObj->GetCharInfo(i, charcode, kerning);
1541 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1542
Tom Sepez62a70f92016-03-21 15:00:20 -07001543 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001544 if (swUnicode.GetLength() > 0)
1545 unicode = swUnicode[0];
1546
1547 if (ISLATINWORD(unicode) && bIsLatin)
1548 continue;
1549
1550 bIsLatin = ISLATINWORD(unicode);
1551 if (unicode != 0x20)
1552 nWords++;
1553 }
1554
1555 return nWords;
1556}
1557
1558CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1559 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001560 CFX_WideString swRet;
1561
1562 CPDF_Font* pFont = pTextObj->GetFont();
1563 if (!pFont)
1564 return L"";
1565
1566 int nWords = 0;
1567 FX_BOOL bIsLatin = FALSE;
1568
1569 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001570 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001571 FX_FLOAT kerning;
1572
1573 pTextObj->GetCharInfo(i, charcode, kerning);
1574 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1575
Tom Sepez62a70f92016-03-21 15:00:20 -07001576 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001577 if (swUnicode.GetLength() > 0)
1578 unicode = swUnicode[0];
1579
1580 if (ISLATINWORD(unicode) && bIsLatin) {
1581 } else {
1582 bIsLatin = ISLATINWORD(unicode);
1583 if (unicode != 0x20)
1584 nWords++;
1585 }
1586
1587 if (nWords - 1 == nWordIndex)
1588 swRet += unicode;
1589 }
1590
1591 return swRet;
1592}
1593
Tom Sepezba038bc2015-10-08 12:03:00 -07001594FX_BOOL Document::zoom(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001595 CJS_PropValue& vp,
1596 CFX_WideString& sError) {
1597 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001598}
1599
1600/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001601(none, NoVary)
1602(fitP, FitPage)
1603(fitW, FitWidth)
1604(fitH, FitHeight)
1605(fitV, FitVisibleWidth)
1606(pref, Preferred)
1607(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001608*/
1609
Tom Sepezba038bc2015-10-08 12:03:00 -07001610FX_BOOL Document::zoomType(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001611 CJS_PropValue& vp,
1612 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001613 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001614}
1615
Tom Sepezba038bc2015-10-08 12:03:00 -07001616FX_BOOL Document::deletePages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001617 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001618 CJS_Value& vRet,
1619 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001620 // Unsafe, no supported.
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001621 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001622}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001623
Tom Sepezba038bc2015-10-08 12:03:00 -07001624FX_BOOL Document::extractPages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001625 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001626 CJS_Value& vRet,
1627 CFX_WideString& sError) {
1628 // Unsafe, not supported.
1629 return TRUE;
1630}
1631
Tom Sepezba038bc2015-10-08 12:03:00 -07001632FX_BOOL Document::insertPages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001633 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001634 CJS_Value& vRet,
1635 CFX_WideString& sError) {
1636 // Unsafe, not supported.
1637 return TRUE;
1638}
1639
Tom Sepezba038bc2015-10-08 12:03:00 -07001640FX_BOOL Document::replacePages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001641 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001642 CJS_Value& vRet,
1643 CFX_WideString& sError) {
1644 // Unsafe, not supported.
1645 return TRUE;
1646}
1647
Tom Sepezba038bc2015-10-08 12:03:00 -07001648FX_BOOL Document::getURL(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001649 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001650 CJS_Value& vRet,
1651 CFX_WideString& sError) {
1652 // Unsafe, not supported.
1653 return TRUE;
1654}
1655
tonikitoo1c836752016-08-08 16:14:05 -07001656FX_BOOL Document::gotoNamedDest(IJS_Context* cc,
1657 const std::vector<CJS_Value>& params,
1658 CJS_Value& vRet,
1659 CFX_WideString& sError) {
tonikitoo1c836752016-08-08 16:14:05 -07001660 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001661 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tonikitoo1c836752016-08-08 16:14:05 -07001662 return FALSE;
1663 }
tsepez55be02e2016-09-12 11:21:42 -07001664 if (!m_pDocument) {
1665 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
1666 return FALSE;
1667 }
tsepezcd5dc852016-09-08 11:23:24 -07001668 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001669 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -07001670 CFX_ByteString utf8Name = wideName.UTF8Encode();
tonikitoo1c836752016-08-08 16:14:05 -07001671 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
1672 if (!pDocument)
1673 return FALSE;
1674
tonikitoo1c836752016-08-08 16:14:05 -07001675 CPDF_NameTree nameTree(pDocument, "Dests");
1676 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, utf8Name);
1677 if (!destArray)
1678 return FALSE;
1679
1680 CPDF_Dest dest(destArray);
1681 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
1682
1683 std::unique_ptr<float[]> scrollPositionArray;
1684 int scrollPositionArraySize = 0;
1685
1686 if (arrayObject) {
1687 scrollPositionArray.reset(new float[arrayObject->GetCount()]);
1688 int j = 0;
1689 for (size_t i = 2; i < arrayObject->GetCount(); i++)
1690 scrollPositionArray[j++] = arrayObject->GetFloatAt(i);
1691 scrollPositionArraySize = j;
1692 }
1693
tsepezb4694242016-08-15 16:44:55 -07001694 pRuntime->BeginBlock();
tonikitoo1c836752016-08-08 16:14:05 -07001695 CPDFDoc_Environment* pApp = m_pDocument->GetEnv();
1696 pApp->FFI_DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
1697 scrollPositionArray.get(), scrollPositionArraySize);
tsepezb4694242016-08-15 16:44:55 -07001698 pRuntime->EndBlock();
tonikitoo1c836752016-08-08 16:14:05 -07001699
1700 return TRUE;
1701}
1702
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001703void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001704 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001705}
1706
1707void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1708 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001709 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1710 auto iter = m_DelayData.begin();
1711 while (iter != m_DelayData.end()) {
1712 auto old = iter++;
1713 if ((*old)->sFieldName == sFieldName &&
1714 (*old)->nControlIndex == nControlIndex) {
1715 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1716 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001717 }
1718 }
1719
Tom Sepez56c10192016-03-15 12:34:17 -07001720 for (const auto& pData : DelayDataForFieldAndControlIndex)
tsepez55be02e2016-09-12 11:21:42 -07001721 Field::DoDelay(m_pDocument.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001722}
1723
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001724CJS_Document* Document::GetCJSDoc() const {
1725 return static_cast<CJS_Document*>(m_pJSObject);
JUN FANG33f6f0d2015-04-06 12:39:51 -07001726}