blob: ea77e1cfad46fb660f450c43b88d73d477a7a6cb [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 =
618 FXJS_GetObjectElement(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
tsepezf3dc8c62016-08-10 06:29:29 -0700622 pValue = FXJS_GetObjectElement(pIsolate, pObj, L"bFDF");
623 bFDF = CJS_Value(pRuntime, pValue).ToBool(pIsolate);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700624
tsepezf3dc8c62016-08-10 06:29:29 -0700625 pValue = FXJS_GetObjectElement(pIsolate, pObj, L"bEmpty");
626 bEmpty = CJS_Value(pRuntime, pValue).ToBool(pIsolate);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700627
tsepezf3dc8c62016-08-10 06:29:29 -0700628 pValue = FXJS_GetObjectElement(pIsolate, pObj, L"aFields");
629 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
tsepezf3dc8c62016-08-10 06:29:29 -0700715 v8::Local<v8::Value> pValue = FXJS_GetObjectElement(pIsolate, pObj, L"bUI");
716 bUI = CJS_Value(pRuntime, pValue).ToInt(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700717
tsepezf3dc8c62016-08-10 06:29:29 -0700718 pValue = FXJS_GetObjectElement(pIsolate, pObj, L"cTo");
719 cTo = CJS_Value(pRuntime, pValue).ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700720
tsepezf3dc8c62016-08-10 06:29:29 -0700721 pValue = FXJS_GetObjectElement(pIsolate, pObj, L"cCc");
722 cCc = CJS_Value(pRuntime, pValue).ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700723
tsepezf3dc8c62016-08-10 06:29:29 -0700724 pValue = FXJS_GetObjectElement(pIsolate, pObj, L"cBcc");
725 cBcc = CJS_Value(pRuntime, pValue).ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700726
tsepezf3dc8c62016-08-10 06:29:29 -0700727 pValue = FXJS_GetObjectElement(pIsolate, pObj, L"cSubject");
728 cSubject = CJS_Value(pRuntime, pValue).ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700729
tsepezf3dc8c62016-08-10 06:29:29 -0700730 pValue = FXJS_GetObjectElement(pIsolate, pObj, L"cMsg");
731 cMsg = CJS_Value(pRuntime, pValue).ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700732 }
733
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700734 pRuntime->BeginBlock();
735 CPDFDoc_Environment* pEnv = pRuntime->GetReaderApp();
thestig1cd352e2016-06-07 17:53:06 -0700736 pEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
737 cCc.c_str(), cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700738 pRuntime->EndBlock();
739
740 return TRUE;
741}
742
Tom Sepezba038bc2015-10-08 12:03:00 -0700743FX_BOOL Document::author(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700744 CJS_PropValue& vp,
745 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700746 return getPropertyInternal(cc, vp, "Author", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700747}
748
Tom Sepezba038bc2015-10-08 12:03:00 -0700749FX_BOOL Document::info(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700750 CJS_PropValue& vp,
751 CFX_WideString& sError) {
tonikitooec268762016-08-10 12:29:28 -0700752 if (vp.IsSetting()) {
753 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
754 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
755 return FALSE;
756 }
757
Tom Sepez50d12ad2015-11-24 09:50:51 -0800758 CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700759 if (!pDictionary)
760 return FALSE;
Tom Sepezc6ab1722015-02-05 15:27:25 -0800761
Wei Li9b761132016-01-29 15:44:20 -0800762 CFX_WideString cwAuthor = pDictionary->GetUnicodeTextBy("Author");
763 CFX_WideString cwTitle = pDictionary->GetUnicodeTextBy("Title");
764 CFX_WideString cwSubject = pDictionary->GetUnicodeTextBy("Subject");
765 CFX_WideString cwKeywords = pDictionary->GetUnicodeTextBy("Keywords");
766 CFX_WideString cwCreator = pDictionary->GetUnicodeTextBy("Creator");
767 CFX_WideString cwProducer = pDictionary->GetUnicodeTextBy("Producer");
768 CFX_WideString cwCreationDate = pDictionary->GetUnicodeTextBy("CreationDate");
769 CFX_WideString cwModDate = pDictionary->GetUnicodeTextBy("ModDate");
770 CFX_WideString cwTrapped = pDictionary->GetUnicodeTextBy("Trapped");
Tom Sepezc6ab1722015-02-05 15:27:25 -0800771
tonikitooec268762016-08-10 12:29:28 -0700772 CJS_Context* pContext = (CJS_Context*)cc;
773 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
774 v8::Local<v8::Object> pObj =
775 FXJS_NewFxDynamicObj(pRuntime->GetIsolate(), pRuntime, -1);
Tom Sepezc6ab1722015-02-05 15:27:25 -0800776
tonikitooec268762016-08-10 12:29:28 -0700777 v8::Isolate* isolate = GetIsolate(cc);
778 FXJS_PutObjectString(isolate, pObj, L"Author", cwAuthor);
779 FXJS_PutObjectString(isolate, pObj, L"Title", cwTitle);
780 FXJS_PutObjectString(isolate, pObj, L"Subject", cwSubject);
781 FXJS_PutObjectString(isolate, pObj, L"Keywords", cwKeywords);
782 FXJS_PutObjectString(isolate, pObj, L"Creator", cwCreator);
783 FXJS_PutObjectString(isolate, pObj, L"Producer", cwProducer);
784 FXJS_PutObjectString(isolate, pObj, L"CreationDate", cwCreationDate);
785 FXJS_PutObjectString(isolate, pObj, L"ModDate", cwModDate);
786 FXJS_PutObjectString(isolate, pObj, L"Trapped", cwTrapped);
787
788 // It's to be compatible to non-standard info dictionary.
789 for (const auto& it : *pDictionary) {
790 const CFX_ByteString& bsKey = it.first;
791 CPDF_Object* pValueObj = it.second;
792 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
793 if (pValueObj->IsString() || pValueObj->IsName()) {
794 FXJS_PutObjectString(isolate, pObj, wsKey, pValueObj->GetUnicodeText());
795 } else if (pValueObj->IsNumber()) {
796 FXJS_PutObjectNumber(isolate, pObj, wsKey, (float)pValueObj->GetNumber());
797 } else if (pValueObj->IsBoolean()) {
798 FXJS_PutObjectBoolean(isolate, pObj, wsKey, !!pValueObj->GetInteger());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700799 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700800 }
tonikitooec268762016-08-10 12:29:28 -0700801 vp << pObj;
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700802 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700803}
804
tonikitooea3ff9e2016-08-02 11:52:28 -0700805FX_BOOL Document::getPropertyInternal(IJS_Context* cc,
806 CJS_PropValue& vp,
807 const CFX_ByteString& propName,
808 CFX_WideString& sError) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800809 CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700810 if (!pDictionary)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700811 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700812
813 if (vp.IsGetting()) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700814 vp << pDictionary->GetUnicodeTextBy(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700815 } else {
816 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY))
817 return FALSE;
818
tonikitooea3ff9e2016-08-02 11:52:28 -0700819 CFX_WideString csProperty;
820 vp >> csProperty;
821 pDictionary->SetAtString(propName, PDF_EncodeText(csProperty));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822 m_pDocument->SetChangeMark();
823 }
824 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700825}
826
tonikitooea3ff9e2016-08-02 11:52:28 -0700827FX_BOOL Document::creationDate(IJS_Context* cc,
828 CJS_PropValue& vp,
829 CFX_WideString& sError) {
830 return getPropertyInternal(cc, vp, "CreationDate", sError);
831}
832
Tom Sepezba038bc2015-10-08 12:03:00 -0700833FX_BOOL Document::creator(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700834 CJS_PropValue& vp,
835 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700836 return getPropertyInternal(cc, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700837}
838
Tom Sepezba038bc2015-10-08 12:03:00 -0700839FX_BOOL Document::delay(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700840 CJS_PropValue& vp,
841 CFX_WideString& sError) {
842 if (vp.IsGetting()) {
843 vp << m_bDelay;
844 } else {
845 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY))
846 return FALSE;
847
848 vp >> m_bDelay;
849 if (m_bDelay) {
Tom Sepez56c10192016-03-15 12:34:17 -0700850 m_DelayData.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700851 } else {
Tom Sepez56c10192016-03-15 12:34:17 -0700852 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
853 DelayDataToProcess.swap(m_DelayData);
854 for (const auto& pData : DelayDataToProcess)
855 Field::DoDelay(m_pDocument, pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856 }
857 }
858 return TRUE;
859}
860
Tom Sepezba038bc2015-10-08 12:03:00 -0700861FX_BOOL Document::keywords(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700862 CJS_PropValue& vp,
863 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700864 return getPropertyInternal(cc, vp, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700865}
866
Tom Sepezba038bc2015-10-08 12:03:00 -0700867FX_BOOL Document::modDate(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700868 CJS_PropValue& vp,
869 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700870 return getPropertyInternal(cc, vp, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700871}
872
Tom Sepezba038bc2015-10-08 12:03:00 -0700873FX_BOOL Document::producer(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700874 CJS_PropValue& vp,
875 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700876 return getPropertyInternal(cc, vp, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877}
878
Tom Sepezba038bc2015-10-08 12:03:00 -0700879FX_BOOL Document::subject(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700880 CJS_PropValue& vp,
881 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700882 return getPropertyInternal(cc, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700883}
884
Tom Sepezba038bc2015-10-08 12:03:00 -0700885FX_BOOL Document::title(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700886 CJS_PropValue& vp,
887 CFX_WideString& sError) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800888 if (!m_pDocument || !m_pDocument->GetUnderlyingDocument())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889 return FALSE;
890
tonikitooea3ff9e2016-08-02 11:52:28 -0700891 return getPropertyInternal(cc, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700892}
893
Tom Sepezba038bc2015-10-08 12:03:00 -0700894FX_BOOL Document::numPages(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700895 CJS_PropValue& vp,
896 CFX_WideString& sError) {
897 if (vp.IsSetting()) {
898 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
899 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
900 return FALSE;
901 }
902 vp << m_pDocument->GetPageCount();
903 return TRUE;
904}
905
Tom Sepezba038bc2015-10-08 12:03:00 -0700906FX_BOOL Document::external(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907 CJS_PropValue& vp,
908 CFX_WideString& sError) {
909 // In Chrome case,should always return true.
910 if (vp.IsGetting()) {
911 vp << true;
912 }
913 return TRUE;
914}
915
Tom Sepezba038bc2015-10-08 12:03:00 -0700916FX_BOOL Document::filesize(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917 CJS_PropValue& vp,
918 CFX_WideString& sError) {
919 if (vp.IsSetting()) {
920 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
921 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
922 return FALSE;
923 }
924 vp << 0;
925 return TRUE;
926}
927
Tom Sepezba038bc2015-10-08 12:03:00 -0700928FX_BOOL Document::mouseX(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700929 CJS_PropValue& vp,
930 CFX_WideString& sError) {
931 return TRUE;
932}
933
Tom Sepezba038bc2015-10-08 12:03:00 -0700934FX_BOOL Document::mouseY(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700935 CJS_PropValue& vp,
936 CFX_WideString& sError) {
937 return TRUE;
938}
939
tonikitoo33c4cdb2016-08-08 10:52:51 -0700940FX_BOOL Document::URL(IJS_Context* cc,
941 CJS_PropValue& vp,
942 CFX_WideString& sError) {
943 if (vp.IsSetting()) {
944 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
945 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
946 return FALSE;
947 }
948 vp << m_pDocument->GetPath();
949 return TRUE;
950}
951
Tom Sepezba038bc2015-10-08 12:03:00 -0700952FX_BOOL Document::baseURL(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700953 CJS_PropValue& vp,
954 CFX_WideString& sError) {
955 if (vp.IsGetting()) {
956 vp << m_cwBaseURL;
957 } else {
958 vp >> m_cwBaseURL;
959 }
960 return TRUE;
961}
962
Tom Sepezba038bc2015-10-08 12:03:00 -0700963FX_BOOL Document::calculate(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700964 CJS_PropValue& vp,
965 CFX_WideString& sError) {
tsepez23ae4a52016-06-08 20:44:54 -0700966 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700967 if (vp.IsGetting()) {
968 if (pInterForm->IsCalculateEnabled())
969 vp << true;
970 else
971 vp << false;
972 } else {
973 bool bCalculate;
974 vp >> bCalculate;
975
976 pInterForm->EnableCalculate(bCalculate);
977 }
978
979 return TRUE;
980}
981
Tom Sepezba038bc2015-10-08 12:03:00 -0700982FX_BOOL Document::documentFileName(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700983 CJS_PropValue& vp,
984 CFX_WideString& sError) {
985 if (vp.IsSetting()) {
986 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
987 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
988 return FALSE;
989 }
990 CFX_WideString wsFilePath = m_pDocument->GetPath();
991 int32_t i = wsFilePath.GetLength() - 1;
992 for (; i >= 0; i--) {
993 if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
994 break;
995 }
996 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
997 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
998 } else {
999 vp << L"";
1000 }
1001 return TRUE;
1002}
1003
Tom Sepezba038bc2015-10-08 12:03:00 -07001004FX_BOOL Document::path(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 CJS_PropValue& vp,
1006 CFX_WideString& sError) {
1007 if (vp.IsSetting()) {
1008 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1009 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
1010 return FALSE;
1011 }
1012 vp << app::SysPathToPDFPath(m_pDocument->GetPath());
1013 return TRUE;
1014}
1015
Tom Sepezba038bc2015-10-08 12:03:00 -07001016FX_BOOL Document::pageWindowRect(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001017 CJS_PropValue& vp,
1018 CFX_WideString& sError) {
1019 return TRUE;
1020}
1021
Tom Sepezba038bc2015-10-08 12:03:00 -07001022FX_BOOL Document::layout(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001023 CJS_PropValue& vp,
1024 CFX_WideString& sError) {
1025 return TRUE;
1026}
1027
Tom Sepezba038bc2015-10-08 12:03:00 -07001028FX_BOOL Document::addLink(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001029 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001030 CJS_Value& vRet,
1031 CFX_WideString& sError) {
1032 return TRUE;
1033}
1034
Tom Sepezba038bc2015-10-08 12:03:00 -07001035FX_BOOL Document::closeDoc(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001036 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001037 CJS_Value& vRet,
1038 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001039 return TRUE;
1040}
1041
Tom Sepezba038bc2015-10-08 12:03:00 -07001042FX_BOOL Document::getPageBox(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001043 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044 CJS_Value& vRet,
1045 CFX_WideString& sError) {
1046 return TRUE;
1047}
1048
Tom Sepezba038bc2015-10-08 12:03:00 -07001049FX_BOOL Document::getAnnot(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001050 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051 CJS_Value& vRet,
1052 CFX_WideString& sError) {
1053 return TRUE;
1054}
1055
Tom Sepezba038bc2015-10-08 12:03:00 -07001056FX_BOOL Document::getAnnots(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001057 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001058 CJS_Value& vRet,
1059 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001060 vRet.SetNull(CJS_Runtime::FromContext(cc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001061 return TRUE;
1062}
1063
Tom Sepezba038bc2015-10-08 12:03:00 -07001064FX_BOOL Document::getAnnot3D(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001065 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 CJS_Value& vRet,
1067 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001068 vRet.SetNull(CJS_Runtime::FromContext(cc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069 return TRUE;
1070}
1071
Tom Sepezba038bc2015-10-08 12:03:00 -07001072FX_BOOL Document::getAnnots3D(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001073 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001074 CJS_Value& vRet,
1075 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076 return TRUE;
1077}
1078
Tom Sepezba038bc2015-10-08 12:03:00 -07001079FX_BOOL Document::getOCGs(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001080 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001081 CJS_Value& vRet,
1082 CFX_WideString& sError) {
1083 return TRUE;
1084}
1085
Tom Sepezba038bc2015-10-08 12:03:00 -07001086FX_BOOL Document::getLinks(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001087 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001088 CJS_Value& vRet,
1089 CFX_WideString& sError) {
1090 return TRUE;
1091}
1092
1093bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1094 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1095 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1096}
1097
Tom Sepezba038bc2015-10-08 12:03:00 -07001098FX_BOOL Document::addIcon(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001099 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001100 CJS_Value& vRet,
1101 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001102 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1103
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001104 if (params.size() != 2) {
1105 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1106 return FALSE;
1107 }
tsepezf3dc8c62016-08-10 06:29:29 -07001108
1109 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1110 v8::Isolate* pIsolate = pRuntime->GetIsolate();
1111
1112 CFX_WideString swIconName = params[0].ToCFXWideString(pIsolate);
Tom Sepezaecd9d02015-08-31 15:04:12 -07001113
Tom Sepez39bfe122015-09-17 15:25:23 -07001114 if (params[1].GetType() != CJS_Value::VT_object) {
Tom Sepezaecd9d02015-08-31 15:04:12 -07001115 sError = JSGetStringFromID(pContext, IDS_STRING_JSTYPEERROR);
1116 return FALSE;
1117 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001118
tsepezf3dc8c62016-08-10 06:29:29 -07001119 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pIsolate);
Tom Sepezcd56a7d2015-10-06 11:45:28 -07001120 if (FXJS_GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001121 sError = JSGetStringFromID(pContext, IDS_STRING_JSTYPEERROR);
1122 return FALSE;
1123 }
1124
tsepezf3dc8c62016-08-10 06:29:29 -07001125 CJS_EmbedObj* pEmbedObj = params[1].ToCJSObject(pIsolate)->GetEmbedObject();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001126 if (!pEmbedObj) {
1127 sError = JSGetStringFromID(pContext, IDS_STRING_JSTYPEERROR);
1128 return FALSE;
1129 }
1130
Tom Sepez6ba32d92016-01-05 16:26:32 -08001131 m_IconList.push_back(std::unique_ptr<IconElement>(
1132 new IconElement(swIconName, (Icon*)pEmbedObj)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001133 return TRUE;
1134}
1135
Tom Sepezba038bc2015-10-08 12:03:00 -07001136FX_BOOL Document::icons(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001137 CJS_PropValue& vp,
1138 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001139 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001140 if (vp.IsSetting()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001141 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
1142 return FALSE;
1143 }
1144
tsepezf3dc8c62016-08-10 06:29:29 -07001145 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Tom Sepez6ba32d92016-01-05 16:26:32 -08001146 if (m_IconList.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001147 vp.GetJSValue()->SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001148 return TRUE;
1149 }
1150
tsepeze5aff742016-08-08 09:49:42 -07001151 CJS_Array Icons;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001152
Tom Sepez6ba32d92016-01-05 16:26:32 -08001153 int i = 0;
1154 for (const auto& pIconElement : m_IconList) {
Tom Sepez39bfe122015-09-17 15:25:23 -07001155 v8::Local<v8::Object> pObj = FXJS_NewFxDynamicObj(
Tom Sepez33420902015-10-13 15:00:10 -07001156 pRuntime->GetIsolate(), pRuntime, CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 if (pObj.IsEmpty())
1158 return FALSE;
1159
Tom Sepezd5a0e952015-09-17 15:40:06 -07001160 CJS_Icon* pJS_Icon = (CJS_Icon*)FXJS_GetPrivate(m_isolate, pObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001161 if (!pJS_Icon)
1162 return FALSE;
1163
1164 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1165 if (!pIcon)
1166 return FALSE;
1167
1168 pIcon->SetStream(pIconElement->IconStream->GetStream());
1169 pIcon->SetIconName(pIconElement->IconName);
tsepeze5aff742016-08-08 09:49:42 -07001170 Icons.SetElement(pRuntime->GetIsolate(), i++,
1171 CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001172 }
1173
1174 vp << Icons;
1175 return TRUE;
1176}
1177
Tom Sepezba038bc2015-10-08 12:03:00 -07001178FX_BOOL Document::getIcon(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001179 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001180 CJS_Value& vRet,
1181 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001182 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001183 if (params.size() != 1) {
1184 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1185 return FALSE;
1186 }
1187
Tom Sepez6ba32d92016-01-05 16:26:32 -08001188 if (m_IconList.empty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001189 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001190
1191 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -07001192 v8::Isolate* pIsolate = pRuntime->GetIsolate();
1193
1194 CFX_WideString swIconName = params[0].ToCFXWideString(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001195
Tom Sepez6ba32d92016-01-05 16:26:32 -08001196 for (const auto& pIconElement : m_IconList) {
1197 if (pIconElement->IconName == swIconName) {
1198 Icon* pRetIcon = pIconElement->IconStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001199
Tom Sepez39bfe122015-09-17 15:25:23 -07001200 v8::Local<v8::Object> pObj = FXJS_NewFxDynamicObj(
Tom Sepez33420902015-10-13 15:00:10 -07001201 pRuntime->GetIsolate(), pRuntime, CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001202 if (pObj.IsEmpty())
1203 return FALSE;
1204
Tom Sepezd5a0e952015-09-17 15:40:06 -07001205 CJS_Icon* pJS_Icon = (CJS_Icon*)FXJS_GetPrivate(m_isolate, pObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001206 if (!pJS_Icon)
1207 return FALSE;
1208
1209 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1210 if (!pIcon)
1211 return FALSE;
1212
1213 pIcon->SetIconName(swIconName);
1214 pIcon->SetStream(pRetIcon->GetStream());
tsepezf3dc8c62016-08-10 06:29:29 -07001215 vRet = CJS_Value(pRuntime, pJS_Icon);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001216 return TRUE;
1217 }
1218 }
1219
1220 return FALSE;
1221}
1222
Tom Sepezba038bc2015-10-08 12:03:00 -07001223FX_BOOL Document::removeIcon(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001224 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001225 CJS_Value& vRet,
1226 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001227 // Unsafe, no supported.
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001228 return TRUE;
1229}
1230
Tom Sepezba038bc2015-10-08 12:03:00 -07001231FX_BOOL Document::createDataObject(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001232 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001233 CJS_Value& vRet,
1234 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001235 // Unsafe, not implemented.
1236 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001237}
1238
Tom Sepezba038bc2015-10-08 12:03:00 -07001239FX_BOOL Document::media(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001240 CJS_PropValue& vp,
1241 CFX_WideString& sError) {
1242 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001243}
1244
Tom Sepezba038bc2015-10-08 12:03:00 -07001245FX_BOOL Document::calculateNow(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001246 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001247 CJS_Value& vRet,
1248 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001249 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001250 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM) ||
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001251 m_pDocument->GetPermissions(FPDFPERM_FILL_FORM)))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001252 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001253
tsepez23ae4a52016-06-08 20:44:54 -07001254 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001255 pInterForm->OnCalculate();
1256 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001257}
1258
Tom Sepezba038bc2015-10-08 12:03:00 -07001259FX_BOOL Document::Collab(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001260 CJS_PropValue& vp,
1261 CFX_WideString& sError) {
1262 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001263}
1264
Tom Sepezba038bc2015-10-08 12:03:00 -07001265FX_BOOL Document::getPageNthWord(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001266 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267 CJS_Value& vRet,
1268 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001269 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1270
1271 // TODO(tsepez): check maximum allowable params.
1272
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001273 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS))
1274 return FALSE;
Tom Sepeze5350ef2015-04-23 18:14:26 -07001275
tsepezf3dc8c62016-08-10 06:29:29 -07001276 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1277 v8::Isolate* pIsolate = pRuntime->GetIsolate();
1278
1279 int nPageNo = params.size() > 0 ? params[0].ToInt(pIsolate) : 0;
1280 int nWordNo = params.size() > 1 ? params[1].ToInt(pIsolate) : 0;
1281 bool bStrip = params.size() > 2 ? params[2].ToBool(pIsolate) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001282
Tom Sepez50d12ad2015-11-24 09:50:51 -08001283 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001284 if (!pDocument)
1285 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001286
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001287 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
1288 sError = JSGetStringFromID(pContext, IDS_STRING_JSVALUEERROR);
1289 return FALSE;
1290 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001291
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001292 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1293 if (!pPageDict)
1294 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001295
thestig5cc24652016-04-26 11:46:02 -07001296 CPDF_Page page(pDocument, pPageDict, true);
1297 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001298
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001299 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001300 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001301 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001302 if (pPageObj->IsText()) {
1303 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001304 int nObjWords = CountWords(pTextObj);
1305 if (nWords + nObjWords >= nWordNo) {
1306 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1307 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001308 }
Tom Sepez2398d892016-02-17 16:46:26 -08001309 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001310 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001311 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001312
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001313 if (bStrip) {
1314 swRet.TrimLeft();
1315 swRet.TrimRight();
1316 }
1317
tsepezf3dc8c62016-08-10 06:29:29 -07001318 vRet = CJS_Value(pRuntime, swRet.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001319 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001320}
1321
Tom Sepezba038bc2015-10-08 12:03:00 -07001322FX_BOOL Document::getPageNthWordQuads(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001323 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001324 CJS_Value& vRet,
1325 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001326 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS))
1327 return FALSE;
1328
1329 return FALSE;
1330}
1331
Tom Sepezba038bc2015-10-08 12:03:00 -07001332FX_BOOL Document::getPageNumWords(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001333 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001334 CJS_Value& vRet,
1335 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001336 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1337
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001338 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS))
1339 return FALSE;
1340
tsepezf3dc8c62016-08-10 06:29:29 -07001341 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1342 v8::Isolate* pIsolate = pRuntime->GetIsolate();
1343
1344 int nPageNo = params.size() > 0 ? params[0].ToInt(pIsolate) : 0;
Tom Sepez50d12ad2015-11-24 09:50:51 -08001345 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001346 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
1347 sError = JSGetStringFromID(pContext, IDS_STRING_JSVALUEERROR);
1348 return FALSE;
1349 }
1350
1351 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1352 if (!pPageDict)
1353 return FALSE;
1354
thestig5cc24652016-04-26 11:46:02 -07001355 CPDF_Page page(pDocument, pPageDict, true);
1356 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001357
1358 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001359 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001360 if (pPageObj->IsText())
1361 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001362 }
1363
tsepezf3dc8c62016-08-10 06:29:29 -07001364 vRet = CJS_Value(pRuntime, nWords);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001365 return TRUE;
1366}
1367
Tom Sepezba038bc2015-10-08 12:03:00 -07001368FX_BOOL Document::getPrintParams(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001369 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001370 CJS_Value& vRet,
1371 CFX_WideString& sError) {
1372 CJS_Context* pContext = (CJS_Context*)cc;
1373 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
Tom Sepez39bfe122015-09-17 15:25:23 -07001374 v8::Local<v8::Object> pRetObj = FXJS_NewFxDynamicObj(
Tom Sepez33420902015-10-13 15:00:10 -07001375 pRuntime->GetIsolate(), pRuntime, CJS_PrintParamsObj::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001376
1377 // Not implemented yet.
1378
tsepezf3dc8c62016-08-10 06:29:29 -07001379 vRet = CJS_Value(pRuntime, pRetObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001380 return TRUE;
1381}
1382
1383#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1384
1385int Document::CountWords(CPDF_TextObject* pTextObj) {
1386 if (!pTextObj)
1387 return 0;
1388
1389 int nWords = 0;
1390
1391 CPDF_Font* pFont = pTextObj->GetFont();
1392 if (!pFont)
1393 return 0;
1394
1395 FX_BOOL bIsLatin = FALSE;
1396
1397 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001398 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001399 FX_FLOAT kerning;
1400
1401 pTextObj->GetCharInfo(i, charcode, kerning);
1402 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1403
Tom Sepez62a70f92016-03-21 15:00:20 -07001404 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001405 if (swUnicode.GetLength() > 0)
1406 unicode = swUnicode[0];
1407
1408 if (ISLATINWORD(unicode) && bIsLatin)
1409 continue;
1410
1411 bIsLatin = ISLATINWORD(unicode);
1412 if (unicode != 0x20)
1413 nWords++;
1414 }
1415
1416 return nWords;
1417}
1418
1419CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1420 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001421 CFX_WideString swRet;
1422
1423 CPDF_Font* pFont = pTextObj->GetFont();
1424 if (!pFont)
1425 return L"";
1426
1427 int nWords = 0;
1428 FX_BOOL bIsLatin = FALSE;
1429
1430 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001431 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001432 FX_FLOAT kerning;
1433
1434 pTextObj->GetCharInfo(i, charcode, kerning);
1435 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1436
Tom Sepez62a70f92016-03-21 15:00:20 -07001437 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001438 if (swUnicode.GetLength() > 0)
1439 unicode = swUnicode[0];
1440
1441 if (ISLATINWORD(unicode) && bIsLatin) {
1442 } else {
1443 bIsLatin = ISLATINWORD(unicode);
1444 if (unicode != 0x20)
1445 nWords++;
1446 }
1447
1448 if (nWords - 1 == nWordIndex)
1449 swRet += unicode;
1450 }
1451
1452 return swRet;
1453}
1454
Tom Sepezba038bc2015-10-08 12:03:00 -07001455FX_BOOL Document::zoom(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001456 CJS_PropValue& vp,
1457 CFX_WideString& sError) {
1458 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001459}
1460
1461/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001462(none, NoVary)
1463(fitP, FitPage)
1464(fitW, FitWidth)
1465(fitH, FitHeight)
1466(fitV, FitVisibleWidth)
1467(pref, Preferred)
1468(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001469*/
1470
Tom Sepezba038bc2015-10-08 12:03:00 -07001471FX_BOOL Document::zoomType(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001472 CJS_PropValue& vp,
1473 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001474 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001475}
1476
Tom Sepezba038bc2015-10-08 12:03:00 -07001477FX_BOOL Document::deletePages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001478 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001479 CJS_Value& vRet,
1480 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001481 // Unsafe, no supported.
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001482 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001483}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001484
Tom Sepezba038bc2015-10-08 12:03:00 -07001485FX_BOOL Document::extractPages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001486 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001487 CJS_Value& vRet,
1488 CFX_WideString& sError) {
1489 // Unsafe, not supported.
1490 return TRUE;
1491}
1492
Tom Sepezba038bc2015-10-08 12:03:00 -07001493FX_BOOL Document::insertPages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001494 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001495 CJS_Value& vRet,
1496 CFX_WideString& sError) {
1497 // Unsafe, not supported.
1498 return TRUE;
1499}
1500
Tom Sepezba038bc2015-10-08 12:03:00 -07001501FX_BOOL Document::replacePages(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001502 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001503 CJS_Value& vRet,
1504 CFX_WideString& sError) {
1505 // Unsafe, not supported.
1506 return TRUE;
1507}
1508
Tom Sepezba038bc2015-10-08 12:03:00 -07001509FX_BOOL Document::getURL(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001510 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001511 CJS_Value& vRet,
1512 CFX_WideString& sError) {
1513 // Unsafe, not supported.
1514 return TRUE;
1515}
1516
tonikitoo1c836752016-08-08 16:14:05 -07001517FX_BOOL Document::gotoNamedDest(IJS_Context* cc,
1518 const std::vector<CJS_Value>& params,
1519 CJS_Value& vRet,
1520 CFX_WideString& sError) {
1521 CJS_Context* context = (CJS_Context*)cc;
tsepezf3dc8c62016-08-10 06:29:29 -07001522
tonikitoo1c836752016-08-08 16:14:05 -07001523 if (params.size() != 1) {
1524 sError = JSGetStringFromID(context, IDS_STRING_JSPARAMERROR);
1525 return FALSE;
1526 }
1527
tsepezf3dc8c62016-08-10 06:29:29 -07001528 CJS_Runtime* runtime = context->GetJSRuntime();
1529 CFX_WideString wideName = params[0].ToCFXWideString(runtime->GetIsolate());
1530 CFX_ByteString utf8Name = wideName.UTF8Encode();
1531
tonikitoo1c836752016-08-08 16:14:05 -07001532 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
1533 if (!pDocument)
1534 return FALSE;
1535
tonikitoo1c836752016-08-08 16:14:05 -07001536 CPDF_NameTree nameTree(pDocument, "Dests");
1537 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, utf8Name);
1538 if (!destArray)
1539 return FALSE;
1540
1541 CPDF_Dest dest(destArray);
1542 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
1543
1544 std::unique_ptr<float[]> scrollPositionArray;
1545 int scrollPositionArraySize = 0;
1546
1547 if (arrayObject) {
1548 scrollPositionArray.reset(new float[arrayObject->GetCount()]);
1549 int j = 0;
1550 for (size_t i = 2; i < arrayObject->GetCount(); i++)
1551 scrollPositionArray[j++] = arrayObject->GetFloatAt(i);
1552 scrollPositionArraySize = j;
1553 }
1554
tonikitoo1c836752016-08-08 16:14:05 -07001555 runtime->BeginBlock();
1556 CPDFDoc_Environment* pApp = m_pDocument->GetEnv();
1557 pApp->FFI_DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
1558 scrollPositionArray.get(), scrollPositionArraySize);
1559 runtime->EndBlock();
1560
1561 return TRUE;
1562}
1563
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001564void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001565 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001566}
1567
1568void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1569 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001570 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1571 auto iter = m_DelayData.begin();
1572 while (iter != m_DelayData.end()) {
1573 auto old = iter++;
1574 if ((*old)->sFieldName == sFieldName &&
1575 (*old)->nControlIndex == nControlIndex) {
1576 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1577 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001578 }
1579 }
1580
Tom Sepez56c10192016-03-15 12:34:17 -07001581 for (const auto& pData : DelayDataForFieldAndControlIndex)
1582 Field::DoDelay(m_pDocument, pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001583}
1584
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001585CJS_Document* Document::GetCJSDoc() const {
1586 return static_cast<CJS_Document*>(m_pJSObject);
JUN FANG33f6f0d2015-04-06 12:39:51 -07001587}