blob: a55bf81139459d130093a97135b01da0d07038e1 [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"
Dan Sinclair3ebd1212016-03-09 09:59:23 -050018#include "fpdfsdk/include/fsdk_mgr.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040019#include "fpdfsdk/javascript/Field.h"
20#include "fpdfsdk/javascript/Icon.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040021#include "fpdfsdk/javascript/JS_Define.h"
22#include "fpdfsdk/javascript/JS_EventHandler.h"
23#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040024#include "fpdfsdk/javascript/JS_Value.h"
25#include "fpdfsdk/javascript/app.h"
dsinclair64376be2016-03-31 20:03:24 -070026#include "fpdfsdk/javascript/cjs_context.h"
27#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040028#include "fpdfsdk/javascript/resource.h"
Lei Zhang8241df72015-11-06 14:38:48 -080029#include "third_party/base/numerics/safe_math.h"
Chris Palmer9108ad22014-06-26 16:01:46 -070030
Tom Sepezba038bc2015-10-08 12:03:00 -070031static v8::Isolate* GetIsolate(IJS_Context* cc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 return pRuntime->GetIsolate();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035}
36
37BEGIN_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)
142JS_STATIC_METHOD_ENTRY(mailDoc)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700143END_JS_STATIC_METHOD()
144
145IMPLEMENT_JS_CLASS(CJS_Document, Document)
146
Tom Sepez33420902015-10-13 15:00:10 -0700147void CJS_Document::InitInstance(IJS_Runtime* pIRuntime) {
148 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime);
149 Document* pDoc = static_cast<Document*>(GetEmbedObject());
150 pDoc->AttachDoc(pRuntime->GetReaderDocument());
151 pDoc->SetIsolate(pRuntime->GetIsolate());
152}
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_isolate(nullptr),
157 m_pDocument(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 m_cwBaseURL(L""),
159 m_bDelay(FALSE) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161Document::~Document() {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162}
163
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164// the total number of fileds in document.
Tom Sepezba038bc2015-10-08 12:03:00 -0700165FX_BOOL Document::numFields(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 CJS_PropValue& vp,
167 CFX_WideString& sError) {
168 if (vp.IsSetting()) {
169 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
170 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700171 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 }
173 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) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 if (vp.IsGetting()) {
183 if (m_pDocument->GetChangeMark())
184 vp << true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700185 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 vp << false;
187 } else {
188 bool bChanged = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189
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 }
197
198 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199}
200
Tom Sepezba038bc2015-10-08 12:03:00 -0700201FX_BOOL Document::ADBE(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 CJS_PropValue& vp,
203 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700204 if (vp.IsGetting())
205 vp.GetJSValue()->SetNull(CJS_Runtime::FromContext(cc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206
207 return TRUE;
208}
209
Tom Sepezba038bc2015-10-08 12:03:00 -0700210FX_BOOL Document::pageNum(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 CJS_PropValue& vp,
212 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 if (vp.IsGetting()) {
214 if (CPDFSDK_PageView* pPageView = m_pDocument->GetCurrentView()) {
215 vp << pPageView->GetPageIndex();
216 }
217 } else {
218 int iPageCount = m_pDocument->GetPageCount();
219 int iPageNum = 0;
220 vp >> iPageNum;
221
222 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
223 if (iPageNum >= 0 && iPageNum < iPageCount) {
224 pEnv->JS_docgotoPage(iPageNum);
225 } else if (iPageNum >= iPageCount) {
226 pEnv->JS_docgotoPage(iPageCount - 1);
227 } else if (iPageNum < 0) {
228 pEnv->JS_docgotoPage(0);
229 }
230 }
231
232 return TRUE;
233}
234
Tom Sepezba038bc2015-10-08 12:03:00 -0700235FX_BOOL Document::addAnnot(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800236 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 CJS_Value& vRet,
238 CFX_WideString& sError) {
239 // Not supported.
240 return TRUE;
241}
242
Tom Sepezba038bc2015-10-08 12:03:00 -0700243FX_BOOL Document::addField(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800244 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 CJS_Value& vRet,
246 CFX_WideString& sError) {
247 // Not supported.
248 return TRUE;
249}
250
Tom Sepezba038bc2015-10-08 12:03:00 -0700251FX_BOOL Document::exportAsText(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800252 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 CJS_Value& vRet,
254 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -0800255 // Unsafe, not supported.
256 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700257}
258
Tom Sepezba038bc2015-10-08 12:03:00 -0700259FX_BOOL Document::exportAsFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800260 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 CJS_Value& vRet,
262 CFX_WideString& sError) {
263 // Unsafe, not supported.
264 return TRUE;
265}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700266
Tom Sepezba038bc2015-10-08 12:03:00 -0700267FX_BOOL Document::exportAsXFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800268 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 CJS_Value& vRet,
270 CFX_WideString& sError) {
271 // Unsafe, not supported.
272 return TRUE;
273}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700274
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275// Maps a field object in PDF document to a JavaScript variable
276// comment:
277// note: the paremter cName, this is clue how to treat if the cName is not a
278// valiable filed name in this document
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279
Tom Sepezba038bc2015-10-08 12:03:00 -0700280FX_BOOL Document::getField(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800281 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 CJS_Value& vRet,
283 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700284 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
285
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 if (params.size() < 1) {
287 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
288 return FALSE;
289 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700290
tsepezf3dc8c62016-08-10 06:29:29 -0700291 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
292 v8::Isolate* pIsolate = pRuntime->GetIsolate();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293
tsepezf3dc8c62016-08-10 06:29:29 -0700294 CFX_WideString wideName = params[0].ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
296 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
297 if (pPDFForm->CountFields(wideName) <= 0) {
tsepezf3dc8c62016-08-10 06:29:29 -0700298 vRet.SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 return TRUE;
300 }
301
tsepezf3dc8c62016-08-10 06:29:29 -0700302 v8::Local<v8::Object> pFieldObj =
303 FXJS_NewFxDynamicObj(pIsolate, pRuntime, CJS_Field::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304
tsepezf3dc8c62016-08-10 06:29:29 -0700305 CJS_Field* pJSField =
306 static_cast<CJS_Field*>(FXJS_GetPrivate(pIsolate, pFieldObj));
307 Field* pField = static_cast<Field*>(pJSField->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 pField->AttachField(this, wideName);
309
tsepezf3dc8c62016-08-10 06:29:29 -0700310 vRet = CJS_Value(pRuntime, pJSField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 return TRUE;
312}
313
314// Gets the name of the nth field in the document
Tom Sepezba038bc2015-10-08 12:03:00 -0700315FX_BOOL Document::getNthFieldName(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800316 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 CJS_Value& vRet,
318 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700319 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
320
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 if (params.size() != 1) {
322 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
323 return FALSE;
324 }
325
tsepezf3dc8c62016-08-10 06:29:29 -0700326 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
327 v8::Isolate* pIsolate = pRuntime->GetIsolate();
328
329 int nIndex = params[0].ToInt(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 if (nIndex < 0) {
331 sError = JSGetStringFromID(pContext, IDS_STRING_JSVALUEERROR);
332 return FALSE;
333 }
334
335 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
336 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
337 CPDF_FormField* pField = pPDFForm->GetField(nIndex);
338 if (!pField)
339 return FALSE;
340
tsepezf3dc8c62016-08-10 06:29:29 -0700341 vRet = CJS_Value(pRuntime, pField->GetFullName().c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 return TRUE;
343}
344
Tom Sepezba038bc2015-10-08 12:03:00 -0700345FX_BOOL Document::importAnFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800346 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347 CJS_Value& vRet,
348 CFX_WideString& sError) {
349 // Unsafe, not supported.
350 return TRUE;
351}
352
Tom Sepezba038bc2015-10-08 12:03:00 -0700353FX_BOOL Document::importAnXFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800354 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 CJS_Value& vRet,
356 CFX_WideString& sError) {
357 // Unsafe, not supported.
358 return TRUE;
359}
360
Tom Sepezba038bc2015-10-08 12:03:00 -0700361FX_BOOL Document::importTextData(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800362 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363 CJS_Value& vRet,
364 CFX_WideString& sError) {
365 // Unsafe, not supported.
366 return TRUE;
367}
368
369// exports the form data and mails the resulting fdf file as an attachment to
370// all recipients.
371// comment: need reader supports
372// note:
373// int CPDFSDK_Document::mailForm(FX_BOOL bUI,String cto,string ccc,string
374// cbcc,string cSubject,string cms);
375
Tom Sepezba038bc2015-10-08 12:03:00 -0700376FX_BOOL Document::mailForm(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800377 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 CJS_Value& vRet,
379 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700380 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
381
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS))
383 return FALSE;
384
385 int iLength = params.size();
tsepezf3dc8c62016-08-10 06:29:29 -0700386 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
387 v8::Isolate* pIsolate = pRuntime->GetIsolate();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388
tsepezf3dc8c62016-08-10 06:29:29 -0700389 FX_BOOL bUI = iLength > 0 ? params[0].ToBool(pIsolate) : TRUE;
390 CFX_WideString cTo = iLength > 1 ? params[1].ToCFXWideString(pIsolate) : L"";
391 CFX_WideString cCc = iLength > 2 ? params[2].ToCFXWideString(pIsolate) : L"";
392 CFX_WideString cBcc = iLength > 3 ? params[3].ToCFXWideString(pIsolate) : L"";
393 CFX_WideString cSubject =
394 iLength > 4 ? params[4].ToCFXWideString(pIsolate) : L"";
395 CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pIsolate) : L"";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396
tsepez23ae4a52016-06-08 20:44:54 -0700397 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398 CFX_ByteTextBuf textBuf;
399 if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
400 return FALSE;
401
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402 pRuntime->BeginBlock();
tsepezf3dc8c62016-08-10 06:29:29 -0700403 CPDFDoc_Environment* pEnv = pContext->GetReaderApp();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404 pEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI,
405 cTo.c_str(), cSubject.c_str(), cCc.c_str(), cBcc.c_str(),
406 cMsg.c_str());
407 pRuntime->EndBlock();
408 return TRUE;
409}
410
Tom Sepezba038bc2015-10-08 12:03:00 -0700411FX_BOOL Document::print(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800412 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413 CJS_Value& vRet,
414 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700415 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
416 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
417 v8::Isolate* pIsolate = pRuntime->GetIsolate();
418
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419 FX_BOOL bUI = TRUE;
420 int nStart = 0;
421 int nEnd = 0;
422 FX_BOOL bSilent = FALSE;
423 FX_BOOL bShrinkToFit = FALSE;
424 FX_BOOL bPrintAsImage = FALSE;
425 FX_BOOL bReverse = FALSE;
426 FX_BOOL bAnnotations = FALSE;
427
428 int nlength = params.size();
429 if (nlength == 9) {
tsepez40faa792016-07-15 17:58:02 -0700430 if (params[8].GetType() == CJS_Value::VT_object) {
tsepezf3dc8c62016-08-10 06:29:29 -0700431 v8::Local<v8::Object> pObj = params[8].ToV8Object(pIsolate);
tsepez40faa792016-07-15 17:58:02 -0700432 if (FXJS_GetObjDefnID(pObj) == CJS_PrintParamsObj::g_nObjDefnID) {
tsepezf3dc8c62016-08-10 06:29:29 -0700433 if (CJS_Object* pJSObj = params[8].ToCJSObject(pIsolate)) {
tsepez40faa792016-07-15 17:58:02 -0700434 if (PrintParamsObj* pprintparamsObj =
435 static_cast<PrintParamsObj*>(pJSObj->GetEmbedObject())) {
436 bUI = pprintparamsObj->bUI;
437 nStart = pprintparamsObj->nStart;
438 nEnd = pprintparamsObj->nEnd;
439 bSilent = pprintparamsObj->bSilent;
440 bShrinkToFit = pprintparamsObj->bShrinkToFit;
441 bPrintAsImage = pprintparamsObj->bPrintAsImage;
442 bReverse = pprintparamsObj->bReverse;
443 bAnnotations = pprintparamsObj->bAnnotations;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700445 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 }
447 }
448 } else {
449 if (nlength >= 1)
tsepezf3dc8c62016-08-10 06:29:29 -0700450 bUI = params[0].ToBool(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700451 if (nlength >= 2)
tsepezf3dc8c62016-08-10 06:29:29 -0700452 nStart = params[1].ToInt(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 if (nlength >= 3)
tsepezf3dc8c62016-08-10 06:29:29 -0700454 nEnd = params[2].ToInt(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455 if (nlength >= 4)
tsepezf3dc8c62016-08-10 06:29:29 -0700456 bSilent = params[3].ToBool(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457 if (nlength >= 5)
tsepezf3dc8c62016-08-10 06:29:29 -0700458 bShrinkToFit = params[4].ToBool(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459 if (nlength >= 6)
tsepezf3dc8c62016-08-10 06:29:29 -0700460 bPrintAsImage = params[5].ToBool(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461 if (nlength >= 7)
tsepezf3dc8c62016-08-10 06:29:29 -0700462 bReverse = params[6].ToBool(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463 if (nlength >= 8)
tsepezf3dc8c62016-08-10 06:29:29 -0700464 bAnnotations = params[7].ToBool(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465 }
466
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467 if (CPDFDoc_Environment* pEnv = m_pDocument->GetEnv()) {
468 pEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit, bPrintAsImage,
469 bReverse, bAnnotations);
470 return TRUE;
471 }
472 return FALSE;
473}
474
475// removes the specified field from the document.
476// comment:
477// note: if the filed name is not retional, adobe is dumb for it.
478
Tom Sepezba038bc2015-10-08 12:03:00 -0700479FX_BOOL Document::removeField(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800480 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481 CJS_Value& vRet,
482 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700483 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
484 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM)))
485 return FALSE;
486
tsepezf3dc8c62016-08-10 06:29:29 -0700487 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
488
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700489 if (params.size() != 1) {
490 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
491 return FALSE;
492 }
493
tsepezf3dc8c62016-08-10 06:29:29 -0700494 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
495 v8::Isolate* pIsolate = pRuntime->GetIsolate();
496
497 CFX_WideString sFieldName = params[0].ToCFXWideString(pIsolate);
tsepez23ae4a52016-06-08 20:44:54 -0700498 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Lei Zhangd88a3642015-11-10 09:38:57 -0800499 std::vector<CPDFSDK_Widget*> widgets;
500 pInterForm->GetWidgets(sFieldName, &widgets);
Lei Zhangd88a3642015-11-10 09:38:57 -0800501 if (widgets.empty())
502 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700503
Lei Zhangd88a3642015-11-10 09:38:57 -0800504 for (CPDFSDK_Widget* pWidget : widgets) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800505 CFX_FloatRect rcAnnot = pWidget->GetRect();
Lei Zhangd88a3642015-11-10 09:38:57 -0800506 --rcAnnot.left;
507 --rcAnnot.bottom;
508 ++rcAnnot.right;
509 ++rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510
tsepezdf964df2016-04-21 12:09:41 -0700511 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800512 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Lei Zhangd88a3642015-11-10 09:38:57 -0800513 ASSERT(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700514
dsinclair461eeaf2016-07-27 07:40:05 -0700515 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, true);
Lei Zhangd88a3642015-11-10 09:38:57 -0800516 pPageView->DeleteAnnot(pWidget);
517 pPageView->UpdateRects(aRefresh);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700518 }
Lei Zhangd88a3642015-11-10 09:38:57 -0800519 m_pDocument->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520
521 return TRUE;
522}
523
524// reset filed values within a document.
525// comment:
526// note: if the fields names r not rational, aodbe is dumb for it.
527
Tom Sepezba038bc2015-10-08 12:03:00 -0700528FX_BOOL Document::resetForm(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800529 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700530 CJS_Value& vRet,
531 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700532 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
533
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700534 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
535 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM) ||
536 m_pDocument->GetPermissions(FPDFPERM_FILL_FORM)))
537 return FALSE;
538
tsepez23ae4a52016-06-08 20:44:54 -0700539 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700540 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
tsepeze5aff742016-08-08 09:49:42 -0700541 CJS_Array aName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700542
Lei Zhangdd734de2015-11-11 10:16:52 -0800543 if (params.empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700544 pPDFForm->ResetForm(TRUE);
545 m_pDocument->SetChangeMark();
Lei Zhangd88a3642015-11-10 09:38:57 -0800546 return TRUE;
547 }
548
tsepezf3dc8c62016-08-10 06:29:29 -0700549 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
550 v8::Isolate* pIsolate = pRuntime->GetIsolate();
551
Lei Zhangd88a3642015-11-10 09:38:57 -0800552 switch (params[0].GetType()) {
553 default:
tsepezf3dc8c62016-08-10 06:29:29 -0700554 aName.Attach(params[0].ToV8Array(pIsolate));
Lei Zhangd88a3642015-11-10 09:38:57 -0800555 break;
556 case CJS_Value::VT_string:
tsepeze5aff742016-08-08 09:49:42 -0700557 aName.SetElement(pRuntime->GetIsolate(), 0, params[0]);
Lei Zhangd88a3642015-11-10 09:38:57 -0800558 break;
559 }
560
561 std::vector<CPDF_FormField*> aFields;
562 for (int i = 0, isz = aName.GetLength(); i < isz; ++i) {
563 CJS_Value valElement(pRuntime);
tsepeze5aff742016-08-08 09:49:42 -0700564 aName.GetElement(pRuntime->GetIsolate(), i, valElement);
tsepezf3dc8c62016-08-10 06:29:29 -0700565 CFX_WideString swVal = valElement.ToCFXWideString(pIsolate);
Lei Zhangd88a3642015-11-10 09:38:57 -0800566 for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j)
567 aFields.push_back(pPDFForm->GetField(j, swVal));
568 }
569
570 if (!aFields.empty()) {
571 pPDFForm->ResetForm(aFields, TRUE, TRUE);
572 m_pDocument->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700573 }
574
575 return TRUE;
576}
577
Tom Sepezba038bc2015-10-08 12:03:00 -0700578FX_BOOL Document::saveAs(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800579 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700580 CJS_Value& vRet,
581 CFX_WideString& sError) {
582 // Unsafe, not supported.
583 return TRUE;
584}
585
Tom Sepezba038bc2015-10-08 12:03:00 -0700586FX_BOOL Document::submitForm(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800587 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700588 CJS_Value& vRet,
589 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700590 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
591
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700592 int nSize = params.size();
593 if (nSize < 1) {
594 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
595 return FALSE;
596 }
597
tsepeze5aff742016-08-08 09:49:42 -0700598 CJS_Array aFields;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599 CFX_WideString strURL;
600 FX_BOOL bFDF = TRUE;
601 FX_BOOL bEmpty = FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700602
tsepezf3dc8c62016-08-10 06:29:29 -0700603 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
604 v8::Isolate* pIsolate = pRuntime->GetIsolate();
605
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700606 CJS_Value v = params[0];
Tom Sepez39bfe122015-09-17 15:25:23 -0700607 if (v.GetType() == CJS_Value::VT_string) {
tsepezf3dc8c62016-08-10 06:29:29 -0700608 strURL = params[0].ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609 if (nSize > 1)
tsepezf3dc8c62016-08-10 06:29:29 -0700610 bFDF = params[1].ToBool(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611 if (nSize > 2)
tsepezf3dc8c62016-08-10 06:29:29 -0700612 bEmpty = params[2].ToBool(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700613 if (nSize > 3)
tsepezf3dc8c62016-08-10 06:29:29 -0700614 aFields.Attach(params[3].ToV8Array(pIsolate));
Tom Sepez39bfe122015-09-17 15:25:23 -0700615 } else if (v.GetType() == CJS_Value::VT_object) {
tsepezf3dc8c62016-08-10 06:29:29 -0700616 v8::Local<v8::Object> pObj = params[0].ToV8Object(pIsolate);
617 v8::Local<v8::Value> pValue =
tsepezd0b6ed12016-08-11 19:50:57 -0700618 FXJS_GetObjectProperty(pIsolate, pObj, L"cURL");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619 if (!pValue.IsEmpty())
tsepezf3dc8c62016-08-10 06:29:29 -0700620 strURL = CJS_Value(pRuntime, pValue).ToCFXWideString(pIsolate);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700621
tsepezd0b6ed12016-08-11 19:50:57 -0700622 pValue = FXJS_GetObjectProperty(pIsolate, pObj, L"bFDF");
tsepezf3dc8c62016-08-10 06:29:29 -0700623 bFDF = CJS_Value(pRuntime, pValue).ToBool(pIsolate);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700624
tsepezd0b6ed12016-08-11 19:50:57 -0700625 pValue = FXJS_GetObjectProperty(pIsolate, pObj, L"bEmpty");
tsepezf3dc8c62016-08-10 06:29:29 -0700626 bEmpty = CJS_Value(pRuntime, pValue).ToBool(pIsolate);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700627
tsepezd0b6ed12016-08-11 19:50:57 -0700628 pValue = FXJS_GetObjectProperty(pIsolate, pObj, L"aFields");
tsepezf3dc8c62016-08-10 06:29:29 -0700629 aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pIsolate));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700630 }
631
tsepez23ae4a52016-06-08 20:44:54 -0700632 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700633 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
Tom Sepez007e6c02016-02-26 14:31:56 -0800634 if (aFields.GetLength() == 0 && bEmpty) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800635 if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700636 pRuntime->BeginBlock();
637 pInterForm->SubmitForm(strURL, FALSE);
638 pRuntime->EndBlock();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700639 }
640 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700641 }
642
Lei Zhangd88a3642015-11-10 09:38:57 -0800643 std::vector<CPDF_FormField*> fieldObjects;
644 for (int i = 0, sz = aFields.GetLength(); i < sz; ++i) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700645 CJS_Value valName(pRuntime);
tsepeze5aff742016-08-08 09:49:42 -0700646 aFields.GetElement(pRuntime->GetIsolate(), i, valName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700647
tsepezf3dc8c62016-08-10 06:29:29 -0700648 CFX_WideString sName = valName.ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700649 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
650 for (int j = 0, jsz = pPDFForm->CountFields(sName); j < jsz; ++j) {
651 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
652 if (!bEmpty && pField->GetValue().IsEmpty())
653 continue;
654
Lei Zhangd88a3642015-11-10 09:38:57 -0800655 fieldObjects.push_back(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700656 }
657 }
658
Lei Zhangd88a3642015-11-10 09:38:57 -0800659 if (pPDFInterForm->CheckRequiredFields(&fieldObjects, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700660 pRuntime->BeginBlock();
Wei Li97da9762016-03-11 17:00:48 -0800661 pInterForm->SubmitFields(strURL, fieldObjects, true, !bFDF);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700662 pRuntime->EndBlock();
663 }
664 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700665}
666
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700667void Document::AttachDoc(CPDFSDK_Document* pDoc) {
668 m_pDocument = pDoc;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700669}
670
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700671CPDFSDK_Document* Document::GetReaderDoc() {
672 return m_pDocument;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700673}
674
Tom Sepezba038bc2015-10-08 12:03:00 -0700675FX_BOOL Document::bookmarkRoot(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700676 CJS_PropValue& vp,
677 CFX_WideString& sError) {
678 return TRUE;
679}
680
Tom Sepezba038bc2015-10-08 12:03:00 -0700681FX_BOOL Document::mailDoc(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800682 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700683 CJS_Value& vRet,
684 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700685 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
686
687 // TODO(tsepez): Check maximum number of allowed params.
688
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700689 FX_BOOL bUI = TRUE;
690 CFX_WideString cTo = L"";
691 CFX_WideString cCc = L"";
692 CFX_WideString cBcc = L"";
693 CFX_WideString cSubject = L"";
694 CFX_WideString cMsg = L"";
695
tsepezf3dc8c62016-08-10 06:29:29 -0700696 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
697 v8::Isolate* pIsolate = pRuntime->GetIsolate();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700698
tsepezf3dc8c62016-08-10 06:29:29 -0700699 if (params.size() >= 1)
700 bUI = params[0].ToBool(pIsolate);
701 if (params.size() >= 2)
702 cTo = params[1].ToCFXWideString(pIsolate);
703 if (params.size() >= 3)
704 cCc = params[2].ToCFXWideString(pIsolate);
705 if (params.size() >= 4)
706 cBcc = params[3].ToCFXWideString(pIsolate);
707 if (params.size() >= 5)
708 cSubject = params[4].ToCFXWideString(pIsolate);
709 if (params.size() >= 6)
710 cMsg = params[5].ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711
Tom Sepez39bfe122015-09-17 15:25:23 -0700712 if (params.size() >= 1 && params[0].GetType() == CJS_Value::VT_object) {
tsepezf3dc8c62016-08-10 06:29:29 -0700713 v8::Local<v8::Object> pObj = params[0].ToV8Object(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700714
tsepezd0b6ed12016-08-11 19:50:57 -0700715 v8::Local<v8::Value> pValue =
716 FXJS_GetObjectProperty(pIsolate, pObj, L"bUI");
tsepezf3dc8c62016-08-10 06:29:29 -0700717 bUI = CJS_Value(pRuntime, pValue).ToInt(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718
tsepezd0b6ed12016-08-11 19:50:57 -0700719 pValue = FXJS_GetObjectProperty(pIsolate, pObj, L"cTo");
tsepezf3dc8c62016-08-10 06:29:29 -0700720 cTo = CJS_Value(pRuntime, pValue).ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700721
tsepezd0b6ed12016-08-11 19:50:57 -0700722 pValue = FXJS_GetObjectProperty(pIsolate, pObj, L"cCc");
tsepezf3dc8c62016-08-10 06:29:29 -0700723 cCc = CJS_Value(pRuntime, pValue).ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700724
tsepezd0b6ed12016-08-11 19:50:57 -0700725 pValue = FXJS_GetObjectProperty(pIsolate, pObj, L"cBcc");
tsepezf3dc8c62016-08-10 06:29:29 -0700726 cBcc = CJS_Value(pRuntime, pValue).ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700727
tsepezd0b6ed12016-08-11 19:50:57 -0700728 pValue = FXJS_GetObjectProperty(pIsolate, pObj, L"cSubject");
tsepezf3dc8c62016-08-10 06:29:29 -0700729 cSubject = CJS_Value(pRuntime, pValue).ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700730
tsepezd0b6ed12016-08-11 19:50:57 -0700731 pValue = FXJS_GetObjectProperty(pIsolate, pObj, L"cMsg");
tsepezf3dc8c62016-08-10 06:29:29 -0700732 cMsg = CJS_Value(pRuntime, pValue).ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700733 }
734
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700735 pRuntime->BeginBlock();
736 CPDFDoc_Environment* pEnv = pRuntime->GetReaderApp();
thestig1cd352e2016-06-07 17:53:06 -0700737 pEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
738 cCc.c_str(), cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700739 pRuntime->EndBlock();
740
741 return TRUE;
742}
743
Tom Sepezba038bc2015-10-08 12:03:00 -0700744FX_BOOL Document::author(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700745 CJS_PropValue& vp,
746 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700747 return getPropertyInternal(cc, vp, "Author", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700748}
749
Tom Sepezba038bc2015-10-08 12:03:00 -0700750FX_BOOL Document::info(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700751 CJS_PropValue& vp,
752 CFX_WideString& sError) {
tonikitooec268762016-08-10 12:29:28 -0700753 if (vp.IsSetting()) {
754 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
755 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
756 return FALSE;
757 }
758
Tom Sepez50d12ad2015-11-24 09:50:51 -0800759 CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700760 if (!pDictionary)
761 return FALSE;
Tom Sepezc6ab1722015-02-05 15:27:25 -0800762
Wei Li9b761132016-01-29 15:44:20 -0800763 CFX_WideString cwAuthor = pDictionary->GetUnicodeTextBy("Author");
764 CFX_WideString cwTitle = pDictionary->GetUnicodeTextBy("Title");
765 CFX_WideString cwSubject = pDictionary->GetUnicodeTextBy("Subject");
766 CFX_WideString cwKeywords = pDictionary->GetUnicodeTextBy("Keywords");
767 CFX_WideString cwCreator = pDictionary->GetUnicodeTextBy("Creator");
768 CFX_WideString cwProducer = pDictionary->GetUnicodeTextBy("Producer");
769 CFX_WideString cwCreationDate = pDictionary->GetUnicodeTextBy("CreationDate");
770 CFX_WideString cwModDate = pDictionary->GetUnicodeTextBy("ModDate");
771 CFX_WideString cwTrapped = pDictionary->GetUnicodeTextBy("Trapped");
Tom Sepezc6ab1722015-02-05 15:27:25 -0800772
tonikitooec268762016-08-10 12:29:28 -0700773 CJS_Context* pContext = (CJS_Context*)cc;
774 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
775 v8::Local<v8::Object> pObj =
776 FXJS_NewFxDynamicObj(pRuntime->GetIsolate(), pRuntime, -1);
Tom Sepezc6ab1722015-02-05 15:27:25 -0800777
tonikitooec268762016-08-10 12:29:28 -0700778 v8::Isolate* isolate = GetIsolate(cc);
779 FXJS_PutObjectString(isolate, pObj, L"Author", cwAuthor);
780 FXJS_PutObjectString(isolate, pObj, L"Title", cwTitle);
781 FXJS_PutObjectString(isolate, pObj, L"Subject", cwSubject);
782 FXJS_PutObjectString(isolate, pObj, L"Keywords", cwKeywords);
783 FXJS_PutObjectString(isolate, pObj, L"Creator", cwCreator);
784 FXJS_PutObjectString(isolate, pObj, L"Producer", cwProducer);
785 FXJS_PutObjectString(isolate, pObj, L"CreationDate", cwCreationDate);
786 FXJS_PutObjectString(isolate, pObj, L"ModDate", cwModDate);
787 FXJS_PutObjectString(isolate, pObj, L"Trapped", cwTrapped);
788
789 // It's to be compatible to non-standard info dictionary.
790 for (const auto& it : *pDictionary) {
791 const CFX_ByteString& bsKey = it.first;
792 CPDF_Object* pValueObj = it.second;
793 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
794 if (pValueObj->IsString() || pValueObj->IsName()) {
795 FXJS_PutObjectString(isolate, pObj, wsKey, pValueObj->GetUnicodeText());
796 } else if (pValueObj->IsNumber()) {
797 FXJS_PutObjectNumber(isolate, pObj, wsKey, (float)pValueObj->GetNumber());
798 } else if (pValueObj->IsBoolean()) {
799 FXJS_PutObjectBoolean(isolate, pObj, wsKey, !!pValueObj->GetInteger());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700800 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700801 }
tonikitooec268762016-08-10 12:29:28 -0700802 vp << pObj;
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700803 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700804}
805
tonikitooea3ff9e2016-08-02 11:52:28 -0700806FX_BOOL Document::getPropertyInternal(IJS_Context* cc,
807 CJS_PropValue& vp,
808 const CFX_ByteString& propName,
809 CFX_WideString& sError) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800810 CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700811 if (!pDictionary)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700812 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700813
814 if (vp.IsGetting()) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700815 vp << pDictionary->GetUnicodeTextBy(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816 } else {
817 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY))
818 return FALSE;
819
tonikitooea3ff9e2016-08-02 11:52:28 -0700820 CFX_WideString csProperty;
821 vp >> csProperty;
822 pDictionary->SetAtString(propName, PDF_EncodeText(csProperty));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700823 m_pDocument->SetChangeMark();
824 }
825 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700826}
827
tonikitooea3ff9e2016-08-02 11:52:28 -0700828FX_BOOL Document::creationDate(IJS_Context* cc,
829 CJS_PropValue& vp,
830 CFX_WideString& sError) {
831 return getPropertyInternal(cc, vp, "CreationDate", sError);
832}
833
Tom Sepezba038bc2015-10-08 12:03:00 -0700834FX_BOOL Document::creator(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700835 CJS_PropValue& vp,
836 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700837 return getPropertyInternal(cc, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700838}
839
Tom Sepezba038bc2015-10-08 12:03:00 -0700840FX_BOOL Document::delay(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700841 CJS_PropValue& vp,
842 CFX_WideString& sError) {
843 if (vp.IsGetting()) {
844 vp << m_bDelay;
845 } else {
846 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY))
847 return FALSE;
848
849 vp >> m_bDelay;
850 if (m_bDelay) {
Tom Sepez56c10192016-03-15 12:34:17 -0700851 m_DelayData.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700852 } else {
Tom Sepez56c10192016-03-15 12:34:17 -0700853 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
854 DelayDataToProcess.swap(m_DelayData);
855 for (const auto& pData : DelayDataToProcess)
856 Field::DoDelay(m_pDocument, pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700857 }
858 }
859 return TRUE;
860}
861
Tom Sepezba038bc2015-10-08 12:03:00 -0700862FX_BOOL Document::keywords(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, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866}
867
Tom Sepezba038bc2015-10-08 12:03:00 -0700868FX_BOOL Document::modDate(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700869 CJS_PropValue& vp,
870 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700871 return getPropertyInternal(cc, vp, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700872}
873
Tom Sepezba038bc2015-10-08 12:03:00 -0700874FX_BOOL Document::producer(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700875 CJS_PropValue& vp,
876 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700877 return getPropertyInternal(cc, vp, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878}
879
Tom Sepezba038bc2015-10-08 12:03:00 -0700880FX_BOOL Document::subject(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881 CJS_PropValue& vp,
882 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700883 return getPropertyInternal(cc, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884}
885
Tom Sepezba038bc2015-10-08 12:03:00 -0700886FX_BOOL Document::title(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887 CJS_PropValue& vp,
888 CFX_WideString& sError) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800889 if (!m_pDocument || !m_pDocument->GetUnderlyingDocument())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700890 return FALSE;
891
tonikitooea3ff9e2016-08-02 11:52:28 -0700892 return getPropertyInternal(cc, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893}
894
Tom Sepezba038bc2015-10-08 12:03:00 -0700895FX_BOOL Document::numPages(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700896 CJS_PropValue& vp,
897 CFX_WideString& sError) {
898 if (vp.IsSetting()) {
899 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
900 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
901 return FALSE;
902 }
903 vp << m_pDocument->GetPageCount();
904 return TRUE;
905}
906
Tom Sepezba038bc2015-10-08 12:03:00 -0700907FX_BOOL Document::external(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700908 CJS_PropValue& vp,
909 CFX_WideString& sError) {
910 // In Chrome case,should always return true.
911 if (vp.IsGetting()) {
912 vp << true;
913 }
914 return TRUE;
915}
916
Tom Sepezba038bc2015-10-08 12:03:00 -0700917FX_BOOL Document::filesize(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700918 CJS_PropValue& vp,
919 CFX_WideString& sError) {
920 if (vp.IsSetting()) {
921 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
922 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
923 return FALSE;
924 }
925 vp << 0;
926 return TRUE;
927}
928
Tom Sepezba038bc2015-10-08 12:03:00 -0700929FX_BOOL Document::mouseX(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700930 CJS_PropValue& vp,
931 CFX_WideString& sError) {
932 return TRUE;
933}
934
Tom Sepezba038bc2015-10-08 12:03:00 -0700935FX_BOOL Document::mouseY(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700936 CJS_PropValue& vp,
937 CFX_WideString& sError) {
938 return TRUE;
939}
940
tonikitoo33c4cdb2016-08-08 10:52:51 -0700941FX_BOOL Document::URL(IJS_Context* cc,
942 CJS_PropValue& vp,
943 CFX_WideString& sError) {
944 if (vp.IsSetting()) {
945 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
946 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
947 return FALSE;
948 }
949 vp << m_pDocument->GetPath();
950 return TRUE;
951}
952
Tom Sepezba038bc2015-10-08 12:03:00 -0700953FX_BOOL Document::baseURL(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700954 CJS_PropValue& vp,
955 CFX_WideString& sError) {
956 if (vp.IsGetting()) {
957 vp << m_cwBaseURL;
958 } else {
959 vp >> m_cwBaseURL;
960 }
961 return TRUE;
962}
963
Tom Sepezba038bc2015-10-08 12:03:00 -0700964FX_BOOL Document::calculate(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700965 CJS_PropValue& vp,
966 CFX_WideString& sError) {
tsepez23ae4a52016-06-08 20:44:54 -0700967 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700968 if (vp.IsGetting()) {
969 if (pInterForm->IsCalculateEnabled())
970 vp << true;
971 else
972 vp << false;
973 } else {
974 bool bCalculate;
975 vp >> bCalculate;
976
977 pInterForm->EnableCalculate(bCalculate);
978 }
979
980 return TRUE;
981}
982
Tom Sepezba038bc2015-10-08 12:03:00 -0700983FX_BOOL Document::documentFileName(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700984 CJS_PropValue& vp,
985 CFX_WideString& sError) {
986 if (vp.IsSetting()) {
987 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
988 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
989 return FALSE;
990 }
991 CFX_WideString wsFilePath = m_pDocument->GetPath();
992 int32_t i = wsFilePath.GetLength() - 1;
993 for (; i >= 0; i--) {
994 if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
995 break;
996 }
997 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
998 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
999 } else {
1000 vp << L"";
1001 }
1002 return TRUE;
1003}
1004
Tom Sepezba038bc2015-10-08 12:03:00 -07001005FX_BOOL Document::path(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001006 CJS_PropValue& vp,
1007 CFX_WideString& sError) {
1008 if (vp.IsSetting()) {
1009 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1010 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
1011 return FALSE;
1012 }
1013 vp << app::SysPathToPDFPath(m_pDocument->GetPath());
1014 return TRUE;
1015}
1016
Tom Sepezba038bc2015-10-08 12:03:00 -07001017FX_BOOL Document::pageWindowRect(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018 CJS_PropValue& vp,
1019 CFX_WideString& sError) {
1020 return TRUE;
1021}
1022
Tom Sepezba038bc2015-10-08 12:03:00 -07001023FX_BOOL Document::layout(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001024 CJS_PropValue& vp,
1025 CFX_WideString& sError) {
1026 return TRUE;
1027}
1028
Tom Sepezba038bc2015-10-08 12:03:00 -07001029FX_BOOL Document::addLink(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001030 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001031 CJS_Value& vRet,
1032 CFX_WideString& sError) {
1033 return TRUE;
1034}
1035
Tom Sepezba038bc2015-10-08 12:03:00 -07001036FX_BOOL Document::closeDoc(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001037 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001038 CJS_Value& vRet,
1039 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040 return TRUE;
1041}
1042
Tom Sepezba038bc2015-10-08 12:03:00 -07001043FX_BOOL Document::getPageBox(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001044 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001045 CJS_Value& vRet,
1046 CFX_WideString& sError) {
1047 return TRUE;
1048}
1049
Tom Sepezba038bc2015-10-08 12:03:00 -07001050FX_BOOL Document::getAnnot(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001051 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001052 CJS_Value& vRet,
1053 CFX_WideString& sError) {
1054 return TRUE;
1055}
1056
Tom Sepezba038bc2015-10-08 12:03:00 -07001057FX_BOOL Document::getAnnots(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001058 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059 CJS_Value& vRet,
1060 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001061 vRet.SetNull(CJS_Runtime::FromContext(cc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001062 return TRUE;
1063}
1064
Tom Sepezba038bc2015-10-08 12:03:00 -07001065FX_BOOL Document::getAnnot3D(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001066 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001067 CJS_Value& vRet,
1068 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001069 vRet.SetNull(CJS_Runtime::FromContext(cc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001070 return TRUE;
1071}
1072
Tom Sepezba038bc2015-10-08 12:03:00 -07001073FX_BOOL Document::getAnnots3D(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) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001077 return TRUE;
1078}
1079
Tom Sepezba038bc2015-10-08 12:03:00 -07001080FX_BOOL Document::getOCGs(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) {
1084 return TRUE;
1085}
1086
Tom Sepezba038bc2015-10-08 12:03:00 -07001087FX_BOOL Document::getLinks(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
1094bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1095 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1096 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1097}
1098
Tom Sepezba038bc2015-10-08 12:03:00 -07001099FX_BOOL Document::addIcon(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001100 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001101 CJS_Value& vRet,
1102 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001103 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1104
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001105 if (params.size() != 2) {
1106 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1107 return FALSE;
1108 }
tsepezf3dc8c62016-08-10 06:29:29 -07001109
1110 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1111 v8::Isolate* pIsolate = pRuntime->GetIsolate();
1112
1113 CFX_WideString swIconName = params[0].ToCFXWideString(pIsolate);
Tom Sepezaecd9d02015-08-31 15:04:12 -07001114
Tom Sepez39bfe122015-09-17 15:25:23 -07001115 if (params[1].GetType() != CJS_Value::VT_object) {
Tom Sepezaecd9d02015-08-31 15:04:12 -07001116 sError = JSGetStringFromID(pContext, IDS_STRING_JSTYPEERROR);
1117 return FALSE;
1118 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001119
tsepezf3dc8c62016-08-10 06:29:29 -07001120 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pIsolate);
Tom Sepezcd56a7d2015-10-06 11:45:28 -07001121 if (FXJS_GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001122 sError = JSGetStringFromID(pContext, IDS_STRING_JSTYPEERROR);
1123 return FALSE;
1124 }
1125
tsepezf3dc8c62016-08-10 06:29:29 -07001126 CJS_EmbedObj* pEmbedObj = params[1].ToCJSObject(pIsolate)->GetEmbedObject();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001127 if (!pEmbedObj) {
1128 sError = JSGetStringFromID(pContext, IDS_STRING_JSTYPEERROR);
1129 return FALSE;
1130 }
1131
Tom Sepez6ba32d92016-01-05 16:26:32 -08001132 m_IconList.push_back(std::unique_ptr<IconElement>(
1133 new IconElement(swIconName, (Icon*)pEmbedObj)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001134 return TRUE;
1135}
1136
Tom Sepezba038bc2015-10-08 12:03:00 -07001137FX_BOOL Document::icons(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001138 CJS_PropValue& vp,
1139 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001140 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001141 if (vp.IsSetting()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001142 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
1143 return FALSE;
1144 }
1145
tsepezf3dc8c62016-08-10 06:29:29 -07001146 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Tom Sepez6ba32d92016-01-05 16:26:32 -08001147 if (m_IconList.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001148 vp.GetJSValue()->SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001149 return TRUE;
1150 }
1151
tsepeze5aff742016-08-08 09:49:42 -07001152 CJS_Array Icons;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001153
Tom Sepez6ba32d92016-01-05 16:26:32 -08001154 int i = 0;
1155 for (const auto& pIconElement : m_IconList) {
Tom Sepez39bfe122015-09-17 15:25:23 -07001156 v8::Local<v8::Object> pObj = FXJS_NewFxDynamicObj(
Tom Sepez33420902015-10-13 15:00:10 -07001157 pRuntime->GetIsolate(), pRuntime, CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 if (pObj.IsEmpty())
1159 return FALSE;
1160
Tom Sepezd5a0e952015-09-17 15:40:06 -07001161 CJS_Icon* pJS_Icon = (CJS_Icon*)FXJS_GetPrivate(m_isolate, pObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001162 if (!pJS_Icon)
1163 return FALSE;
1164
1165 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1166 if (!pIcon)
1167 return FALSE;
1168
1169 pIcon->SetStream(pIconElement->IconStream->GetStream());
1170 pIcon->SetIconName(pIconElement->IconName);
tsepeze5aff742016-08-08 09:49:42 -07001171 Icons.SetElement(pRuntime->GetIsolate(), i++,
1172 CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173 }
1174
1175 vp << Icons;
1176 return TRUE;
1177}
1178
Tom Sepezba038bc2015-10-08 12:03:00 -07001179FX_BOOL Document::getIcon(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001180 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001181 CJS_Value& vRet,
1182 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001183 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001184 if (params.size() != 1) {
1185 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1186 return FALSE;
1187 }
1188
Tom Sepez6ba32d92016-01-05 16:26:32 -08001189 if (m_IconList.empty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001190 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001191
1192 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -07001193 v8::Isolate* pIsolate = pRuntime->GetIsolate();
1194
1195 CFX_WideString swIconName = params[0].ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001196
Tom Sepez6ba32d92016-01-05 16:26:32 -08001197 for (const auto& pIconElement : m_IconList) {
1198 if (pIconElement->IconName == swIconName) {
1199 Icon* pRetIcon = pIconElement->IconStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001200
Tom Sepez39bfe122015-09-17 15:25:23 -07001201 v8::Local<v8::Object> pObj = FXJS_NewFxDynamicObj(
Tom Sepez33420902015-10-13 15:00:10 -07001202 pRuntime->GetIsolate(), pRuntime, CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001203 if (pObj.IsEmpty())
1204 return FALSE;
1205
Tom Sepezd5a0e952015-09-17 15:40:06 -07001206 CJS_Icon* pJS_Icon = (CJS_Icon*)FXJS_GetPrivate(m_isolate, pObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001207 if (!pJS_Icon)
1208 return FALSE;
1209
1210 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1211 if (!pIcon)
1212 return FALSE;
1213
1214 pIcon->SetIconName(swIconName);
1215 pIcon->SetStream(pRetIcon->GetStream());
tsepezf3dc8c62016-08-10 06:29:29 -07001216 vRet = CJS_Value(pRuntime, pJS_Icon);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001217 return TRUE;
1218 }
1219 }
1220
1221 return FALSE;
1222}
1223
Tom Sepezba038bc2015-10-08 12:03:00 -07001224FX_BOOL Document::removeIcon(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001225 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001226 CJS_Value& vRet,
1227 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001228 // Unsafe, no supported.
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229 return TRUE;
1230}
1231
Tom Sepezba038bc2015-10-08 12:03:00 -07001232FX_BOOL Document::createDataObject(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001233 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001234 CJS_Value& vRet,
1235 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001236 // Unsafe, not implemented.
1237 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001238}
1239
Tom Sepezba038bc2015-10-08 12:03:00 -07001240FX_BOOL Document::media(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001241 CJS_PropValue& vp,
1242 CFX_WideString& sError) {
1243 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001244}
1245
Tom Sepezba038bc2015-10-08 12:03:00 -07001246FX_BOOL Document::calculateNow(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001247 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001248 CJS_Value& vRet,
1249 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001250 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001251 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM) ||
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001252 m_pDocument->GetPermissions(FPDFPERM_FILL_FORM)))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001253 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001254
tsepez23ae4a52016-06-08 20:44:54 -07001255 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001256 pInterForm->OnCalculate();
1257 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001258}
1259
Tom Sepezba038bc2015-10-08 12:03:00 -07001260FX_BOOL Document::Collab(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001261 CJS_PropValue& vp,
1262 CFX_WideString& sError) {
1263 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001264}
1265
Tom Sepezba038bc2015-10-08 12:03:00 -07001266FX_BOOL Document::getPageNthWord(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001267 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001268 CJS_Value& vRet,
1269 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001270 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1271
1272 // TODO(tsepez): check maximum allowable params.
1273
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001274 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS))
1275 return FALSE;
Tom Sepeze5350ef2015-04-23 18:14:26 -07001276
tsepezf3dc8c62016-08-10 06:29:29 -07001277 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1278 v8::Isolate* pIsolate = pRuntime->GetIsolate();
1279
1280 int nPageNo = params.size() > 0 ? params[0].ToInt(pIsolate) : 0;
1281 int nWordNo = params.size() > 1 ? params[1].ToInt(pIsolate) : 0;
1282 bool bStrip = params.size() > 2 ? params[2].ToBool(pIsolate) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001283
Tom Sepez50d12ad2015-11-24 09:50:51 -08001284 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001285 if (!pDocument)
1286 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001287
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001288 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
1289 sError = JSGetStringFromID(pContext, IDS_STRING_JSVALUEERROR);
1290 return FALSE;
1291 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001292
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001293 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1294 if (!pPageDict)
1295 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001296
thestig5cc24652016-04-26 11:46:02 -07001297 CPDF_Page page(pDocument, pPageDict, true);
1298 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001299
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001300 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001301 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001302 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001303 if (pPageObj->IsText()) {
1304 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001305 int nObjWords = CountWords(pTextObj);
1306 if (nWords + nObjWords >= nWordNo) {
1307 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1308 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001309 }
Tom Sepez2398d892016-02-17 16:46:26 -08001310 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001311 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001312 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001313
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001314 if (bStrip) {
1315 swRet.TrimLeft();
1316 swRet.TrimRight();
1317 }
1318
tsepezf3dc8c62016-08-10 06:29:29 -07001319 vRet = CJS_Value(pRuntime, swRet.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001320 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001321}
1322
Tom Sepezba038bc2015-10-08 12:03:00 -07001323FX_BOOL Document::getPageNthWordQuads(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001324 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001325 CJS_Value& vRet,
1326 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001327 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS))
1328 return FALSE;
1329
1330 return FALSE;
1331}
1332
Tom Sepezba038bc2015-10-08 12:03:00 -07001333FX_BOOL Document::getPageNumWords(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001334 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001335 CJS_Value& vRet,
1336 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001337 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1338
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001339 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS))
1340 return FALSE;
1341
tsepezf3dc8c62016-08-10 06:29:29 -07001342 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1343 v8::Isolate* pIsolate = pRuntime->GetIsolate();
1344
1345 int nPageNo = params.size() > 0 ? params[0].ToInt(pIsolate) : 0;
Tom Sepez50d12ad2015-11-24 09:50:51 -08001346 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001347 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
1348 sError = JSGetStringFromID(pContext, IDS_STRING_JSVALUEERROR);
1349 return FALSE;
1350 }
1351
1352 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1353 if (!pPageDict)
1354 return FALSE;
1355
thestig5cc24652016-04-26 11:46:02 -07001356 CPDF_Page page(pDocument, pPageDict, true);
1357 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001358
1359 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001360 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001361 if (pPageObj->IsText())
1362 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001363 }
1364
tsepezf3dc8c62016-08-10 06:29:29 -07001365 vRet = CJS_Value(pRuntime, nWords);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001366 return TRUE;
1367}
1368
Tom Sepezba038bc2015-10-08 12:03:00 -07001369FX_BOOL Document::getPrintParams(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001370 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001371 CJS_Value& vRet,
1372 CFX_WideString& sError) {
1373 CJS_Context* pContext = (CJS_Context*)cc;
1374 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
Tom Sepez39bfe122015-09-17 15:25:23 -07001375 v8::Local<v8::Object> pRetObj = FXJS_NewFxDynamicObj(
Tom Sepez33420902015-10-13 15:00:10 -07001376 pRuntime->GetIsolate(), pRuntime, CJS_PrintParamsObj::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001377
1378 // Not implemented yet.
1379
tsepezf3dc8c62016-08-10 06:29:29 -07001380 vRet = CJS_Value(pRuntime, pRetObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001381 return TRUE;
1382}
1383
1384#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1385
1386int Document::CountWords(CPDF_TextObject* pTextObj) {
1387 if (!pTextObj)
1388 return 0;
1389
1390 int nWords = 0;
1391
1392 CPDF_Font* pFont = pTextObj->GetFont();
1393 if (!pFont)
1394 return 0;
1395
1396 FX_BOOL bIsLatin = FALSE;
1397
1398 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001399 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001400 FX_FLOAT kerning;
1401
1402 pTextObj->GetCharInfo(i, charcode, kerning);
1403 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1404
Tom Sepez62a70f92016-03-21 15:00:20 -07001405 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001406 if (swUnicode.GetLength() > 0)
1407 unicode = swUnicode[0];
1408
1409 if (ISLATINWORD(unicode) && bIsLatin)
1410 continue;
1411
1412 bIsLatin = ISLATINWORD(unicode);
1413 if (unicode != 0x20)
1414 nWords++;
1415 }
1416
1417 return nWords;
1418}
1419
1420CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1421 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001422 CFX_WideString swRet;
1423
1424 CPDF_Font* pFont = pTextObj->GetFont();
1425 if (!pFont)
1426 return L"";
1427
1428 int nWords = 0;
1429 FX_BOOL bIsLatin = FALSE;
1430
1431 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001432 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001433 FX_FLOAT kerning;
1434
1435 pTextObj->GetCharInfo(i, charcode, kerning);
1436 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1437
Tom Sepez62a70f92016-03-21 15:00:20 -07001438 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001439 if (swUnicode.GetLength() > 0)
1440 unicode = swUnicode[0];
1441
1442 if (ISLATINWORD(unicode) && bIsLatin) {
1443 } else {
1444 bIsLatin = ISLATINWORD(unicode);
1445 if (unicode != 0x20)
1446 nWords++;
1447 }
1448
1449 if (nWords - 1 == nWordIndex)
1450 swRet += unicode;
1451 }
1452
1453 return swRet;
1454}
1455
Tom Sepezba038bc2015-10-08 12:03:00 -07001456FX_BOOL Document::zoom(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001457 CJS_PropValue& vp,
1458 CFX_WideString& sError) {
1459 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001460}
1461
1462/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001463(none, NoVary)
1464(fitP, FitPage)
1465(fitW, FitWidth)
1466(fitH, FitHeight)
1467(fitV, FitVisibleWidth)
1468(pref, Preferred)
1469(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001470*/
1471
Tom Sepezba038bc2015-10-08 12:03:00 -07001472FX_BOOL Document::zoomType(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001473 CJS_PropValue& vp,
1474 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001475 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001476}
1477
Tom Sepezba038bc2015-10-08 12:03:00 -07001478FX_BOOL Document::deletePages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001479 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001480 CJS_Value& vRet,
1481 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001482 // Unsafe, no supported.
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001483 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001484}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001485
Tom Sepezba038bc2015-10-08 12:03:00 -07001486FX_BOOL Document::extractPages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001487 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001488 CJS_Value& vRet,
1489 CFX_WideString& sError) {
1490 // Unsafe, not supported.
1491 return TRUE;
1492}
1493
Tom Sepezba038bc2015-10-08 12:03:00 -07001494FX_BOOL Document::insertPages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001495 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001496 CJS_Value& vRet,
1497 CFX_WideString& sError) {
1498 // Unsafe, not supported.
1499 return TRUE;
1500}
1501
Tom Sepezba038bc2015-10-08 12:03:00 -07001502FX_BOOL Document::replacePages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001503 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001504 CJS_Value& vRet,
1505 CFX_WideString& sError) {
1506 // Unsafe, not supported.
1507 return TRUE;
1508}
1509
Tom Sepezba038bc2015-10-08 12:03:00 -07001510FX_BOOL Document::getURL(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 // Unsafe, not supported.
1515 return TRUE;
1516}
1517
tonikitoo1c836752016-08-08 16:14:05 -07001518FX_BOOL Document::gotoNamedDest(IJS_Context* cc,
1519 const std::vector<CJS_Value>& params,
1520 CJS_Value& vRet,
1521 CFX_WideString& sError) {
1522 CJS_Context* context = (CJS_Context*)cc;
tsepezf3dc8c62016-08-10 06:29:29 -07001523
tonikitoo1c836752016-08-08 16:14:05 -07001524 if (params.size() != 1) {
1525 sError = JSGetStringFromID(context, IDS_STRING_JSPARAMERROR);
1526 return FALSE;
1527 }
1528
tsepezf3dc8c62016-08-10 06:29:29 -07001529 CJS_Runtime* runtime = context->GetJSRuntime();
1530 CFX_WideString wideName = params[0].ToCFXWideString(runtime->GetIsolate());
1531 CFX_ByteString utf8Name = wideName.UTF8Encode();
1532
tonikitoo1c836752016-08-08 16:14:05 -07001533 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
1534 if (!pDocument)
1535 return FALSE;
1536
tonikitoo1c836752016-08-08 16:14:05 -07001537 CPDF_NameTree nameTree(pDocument, "Dests");
1538 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, utf8Name);
1539 if (!destArray)
1540 return FALSE;
1541
1542 CPDF_Dest dest(destArray);
1543 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
1544
1545 std::unique_ptr<float[]> scrollPositionArray;
1546 int scrollPositionArraySize = 0;
1547
1548 if (arrayObject) {
1549 scrollPositionArray.reset(new float[arrayObject->GetCount()]);
1550 int j = 0;
1551 for (size_t i = 2; i < arrayObject->GetCount(); i++)
1552 scrollPositionArray[j++] = arrayObject->GetFloatAt(i);
1553 scrollPositionArraySize = j;
1554 }
1555
tonikitoo1c836752016-08-08 16:14:05 -07001556 runtime->BeginBlock();
1557 CPDFDoc_Environment* pApp = m_pDocument->GetEnv();
1558 pApp->FFI_DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
1559 scrollPositionArray.get(), scrollPositionArraySize);
1560 runtime->EndBlock();
1561
1562 return TRUE;
1563}
1564
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001565void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001566 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001567}
1568
1569void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1570 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001571 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1572 auto iter = m_DelayData.begin();
1573 while (iter != m_DelayData.end()) {
1574 auto old = iter++;
1575 if ((*old)->sFieldName == sFieldName &&
1576 (*old)->nControlIndex == nControlIndex) {
1577 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1578 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001579 }
1580 }
1581
Tom Sepez56c10192016-03-15 12:34:17 -07001582 for (const auto& pData : DelayDataForFieldAndControlIndex)
1583 Field::DoDelay(m_pDocument, pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001584}
1585
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001586CJS_Document* Document::GetCJSDoc() const {
1587 return static_cast<CJS_Document*>(m_pJSObject);
JUN FANG33f6f0d2015-04-06 12:39:51 -07001588}