1 // 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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../include/javascript/JavaScript.h"
8 #include "../../include/javascript/IJavaScript.h"
9 #include "../../include/javascript/JS_Define.h"
10 #include "../../include/javascript/JS_Object.h"
11 #include "../../include/javascript/JS_Value.h"
12 #include "../../include/javascript/Document.h"
13 #include "../../include/javascript/JS_EventHandler.h"
14 #include "../../include/javascript/JS_Context.h"
15 #include "../../include/javascript/JS_Runtime.h"
16 #include "../../include/javascript/app.h"
17 #include "../../include/javascript/Field.h"
18 #include "../../include/javascript/Icon.h"
19 #include "../../include/javascript/resource.h"
21 #include "../../../third_party/base/numerics/safe_math.h"
23 static v8::Isolate* GetIsolate(IFXJS_Context* cc)
25 CJS_Context* pContext = (CJS_Context *)cc;
26 ASSERT(pContext != NULL);
28 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
29 ASSERT(pRuntime != NULL);
31 return pRuntime->GetIsolate();
34 BEGIN_JS_STATIC_CONST(CJS_PrintParamsObj)
37 BEGIN_JS_STATIC_PROP(CJS_PrintParamsObj)
40 BEGIN_JS_STATIC_METHOD(CJS_PrintParamsObj)
41 END_JS_STATIC_METHOD()
43 IMPLEMENT_JS_CLASS(CJS_PrintParamsObj, PrintParamsObj)
45 PrintParamsObj::PrintParamsObj(CJS_Object* pJSObject)
46 : CJS_EmbedObj(pJSObject)
53 bPrintAsImage = FALSE;
58 /* ---------------------- Document ---------------------- */
61 #define MINHEIGHT 5.0f
63 BEGIN_JS_STATIC_CONST(CJS_Document)
66 BEGIN_JS_STATIC_PROP(CJS_Document)
67 JS_STATIC_PROP_ENTRY(ADBE)
68 JS_STATIC_PROP_ENTRY(author)
69 JS_STATIC_PROP_ENTRY(baseURL)
70 JS_STATIC_PROP_ENTRY(bookmarkRoot)
71 JS_STATIC_PROP_ENTRY(calculate)
72 JS_STATIC_PROP_ENTRY(Collab)
73 JS_STATIC_PROP_ENTRY(creationDate)
74 JS_STATIC_PROP_ENTRY(creator)
75 JS_STATIC_PROP_ENTRY(delay)
76 JS_STATIC_PROP_ENTRY(dirty)
77 JS_STATIC_PROP_ENTRY(documentFileName)
78 JS_STATIC_PROP_ENTRY(external)
79 JS_STATIC_PROP_ENTRY(filesize)
80 JS_STATIC_PROP_ENTRY(icons)
81 JS_STATIC_PROP_ENTRY(info)
82 JS_STATIC_PROP_ENTRY(keywords)
83 JS_STATIC_PROP_ENTRY(layout)
84 JS_STATIC_PROP_ENTRY(media)
85 JS_STATIC_PROP_ENTRY(modDate)
86 JS_STATIC_PROP_ENTRY(mouseX)
87 JS_STATIC_PROP_ENTRY(mouseY)
88 JS_STATIC_PROP_ENTRY(numFields)
89 JS_STATIC_PROP_ENTRY(numPages)
90 JS_STATIC_PROP_ENTRY(pageNum)
91 JS_STATIC_PROP_ENTRY(pageWindowRect)
92 JS_STATIC_PROP_ENTRY(path)
93 JS_STATIC_PROP_ENTRY(producer)
94 JS_STATIC_PROP_ENTRY(subject)
95 JS_STATIC_PROP_ENTRY(title)
96 JS_STATIC_PROP_ENTRY(zoom)
97 JS_STATIC_PROP_ENTRY(zoomType)
100 BEGIN_JS_STATIC_METHOD(CJS_Document)
101 JS_STATIC_METHOD_ENTRY(addAnnot,0)
102 JS_STATIC_METHOD_ENTRY(addField, 4)
103 JS_STATIC_METHOD_ENTRY(addLink, 0)
104 JS_STATIC_METHOD_ENTRY(addIcon, 0)
105 JS_STATIC_METHOD_ENTRY(calculateNow, 0)
106 JS_STATIC_METHOD_ENTRY(closeDoc, 0)
107 JS_STATIC_METHOD_ENTRY(createDataObject, 0)
108 JS_STATIC_METHOD_ENTRY(deletePages, 2)
109 JS_STATIC_METHOD_ENTRY(exportAsText, 3)
110 JS_STATIC_METHOD_ENTRY(exportAsFDF, 6)
111 JS_STATIC_METHOD_ENTRY(exportAsXFDF, 5)
112 JS_STATIC_METHOD_ENTRY(extractPages, 3)
113 JS_STATIC_METHOD_ENTRY(getAnnot, 0)
114 JS_STATIC_METHOD_ENTRY(getAnnots, 2)
115 JS_STATIC_METHOD_ENTRY(getAnnot3D, 2)
116 JS_STATIC_METHOD_ENTRY(getAnnots3D, 1)
117 JS_STATIC_METHOD_ENTRY(getField, 1)
118 JS_STATIC_METHOD_ENTRY(getIcon, 0)
119 JS_STATIC_METHOD_ENTRY(getLinks, 0)
120 JS_STATIC_METHOD_ENTRY(getNthFieldName, 1)
121 JS_STATIC_METHOD_ENTRY(getOCGs, 0)
122 JS_STATIC_METHOD_ENTRY(getPageBox, 0)
123 JS_STATIC_METHOD_ENTRY(getPageNthWord, 3)
124 JS_STATIC_METHOD_ENTRY(getPageNthWordQuads, 2)
125 JS_STATIC_METHOD_ENTRY(getPageNumWords, 1)
126 JS_STATIC_METHOD_ENTRY(getPrintParams, 0)
127 JS_STATIC_METHOD_ENTRY(getURL, 2)
128 JS_STATIC_METHOD_ENTRY(importAnFDF, 1)
129 JS_STATIC_METHOD_ENTRY(importAnXFDF, 1)
130 JS_STATIC_METHOD_ENTRY(importTextData, 2)
131 JS_STATIC_METHOD_ENTRY(insertPages, 4)
132 JS_STATIC_METHOD_ENTRY(mailForm, 6)
133 JS_STATIC_METHOD_ENTRY(print, 9)
134 JS_STATIC_METHOD_ENTRY(removeField, 1)
135 JS_STATIC_METHOD_ENTRY(replacePages, 4)
136 JS_STATIC_METHOD_ENTRY(resetForm, 1)
137 JS_STATIC_METHOD_ENTRY(removeIcon, 0)
138 JS_STATIC_METHOD_ENTRY(saveAs, 5)
139 JS_STATIC_METHOD_ENTRY(submitForm, 23)
140 JS_STATIC_METHOD_ENTRY(mailDoc, 0)
141 END_JS_STATIC_METHOD()
143 IMPLEMENT_JS_CLASS(CJS_Document, Document)
145 FX_BOOL CJS_Document::InitInstance(IFXJS_Context* cc)
147 CJS_Context* pContext = (CJS_Context*)cc;
148 ASSERT(pContext != NULL);
150 Document* pDoc = (Document*)GetEmbedObject();
151 ASSERT(pDoc != NULL);
153 pDoc->AttachDoc(pContext->GetReaderDocument());
154 pDoc->SetIsolate(pContext->GetJSRuntime()->GetIsolate());
158 /* --------------------------------- Document --------------------------------- */
160 Document::Document(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject),
169 Document::~Document()
173 m_pIconTree->DeleteIconTree();
177 for (int i=0; i<m_DelayData.GetSize(); i++)
179 if (CJS_DelayData* pData = m_DelayData.GetAt(i))
183 m_DelayData.SetAt(i, NULL);
188 m_DelayData.RemoveAll();
189 m_DelayAnnotData.RemoveAll();
192 //the total number of fileds in document.
193 FX_BOOL Document::numFields(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
195 if (vp.IsSetting()) {
196 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
197 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
200 CPDFSDK_InterForm *pInterForm = m_pDocument->GetInterForm();
201 CPDF_InterForm *pPDFForm = pInterForm->GetInterForm();
202 vp << (int)pPDFForm->CountFields();
206 FX_BOOL Document::dirty(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
208 ASSERT(m_pDocument != NULL);
212 if (m_pDocument->GetChangeMark())
219 bool bChanged = false;
224 m_pDocument->SetChangeMark();
226 m_pDocument->ClearChangeMark();
232 FX_BOOL Document::ADBE(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
234 ASSERT(m_pDocument != NULL);
247 FX_BOOL Document::pageNum(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
249 ASSERT(m_pDocument != NULL);
253 if (CPDFSDK_PageView* pPageView = m_pDocument->GetCurrentView())
255 vp << pPageView->GetPageIndex();
260 int iPageCount = m_pDocument->GetPageCount();
265 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
269 if (iPageNum >= 0 && iPageNum < iPageCount)
271 pEnv->JS_docgotoPage(iPageNum);
273 else if (iPageNum >= iPageCount)
275 pEnv->JS_docgotoPage(iPageCount-1);
277 else if (iPageNum < 0)
279 pEnv->JS_docgotoPage(0);
286 FX_BOOL Document::ParserParams(JSObject* pObj,CJS_AnnotObj& annotobj)
292 FX_BOOL Document::addAnnot(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
298 FX_BOOL Document::addField(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
304 FX_BOOL Document::exportAsText(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
306 // Unsafe, not supported.
310 FX_BOOL Document::exportAsFDF(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
312 // Unsafe, not supported.
316 FX_BOOL Document::exportAsXFDF(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
318 // Unsafe, not supported.
322 //Maps a field object in PDF document to a JavaScript variable
324 //note: the paremter cName, this is clue how to treat if the cName is not a valiable filed name in this document
326 FX_BOOL Document::getField(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
328 v8::Isolate* isolate = GetIsolate(cc);
329 ASSERT(m_pDocument != NULL);
331 if (params.size() < 1) return FALSE;
333 CFX_WideString wideName = params[0].operator CFX_WideString();
335 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
336 ASSERT(pInterForm != NULL);
338 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
339 ASSERT(pPDFForm != NULL);
341 if (pPDFForm->CountFields(wideName) <= 0)
347 CJS_Context* pContext = (CJS_Context*)cc;
348 ASSERT(pContext != NULL);
349 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
350 ASSERT(pRuntime != NULL);
352 JSFXObject pFieldObj = JS_NewFxDynamicObj(*pRuntime, pContext, JS_GetObjDefnID(*pRuntime, L"Field"));
354 CJS_Field * pJSField = (CJS_Field*)JS_GetPrivate(isolate,pFieldObj);
355 ASSERT(pJSField != NULL);
357 Field * pField = (Field *)pJSField->GetEmbedObject();
358 ASSERT(pField != NULL);
360 pField->AttachField(this, wideName);
366 //Gets the name of the nth field in the document
367 FX_BOOL Document::getNthFieldName(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
369 ASSERT(m_pDocument != NULL);
371 int nIndex = params.size() > 0 ? (int)params[0] : -1;
372 if (nIndex == -1) return FALSE;
374 CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
375 ASSERT(pInterForm != NULL);
377 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
378 ASSERT(pPDFForm != NULL);
380 CPDF_FormField* pField = pPDFForm->GetField(nIndex);
384 vRet = pField->GetFullName();
388 FX_BOOL Document::importAnFDF(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
390 // Unsafe, not supported.
394 FX_BOOL Document::importAnXFDF(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
396 // Unsafe, not supported.
400 FX_BOOL Document::importTextData(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
402 // Unsafe, not supported.
406 //exports the form data and mails the resulting fdf file as an attachment to all recipients.
407 //comment: need reader supports
409 //int CPDFSDK_Document::mailForm(FX_BOOL bUI,String cto,string ccc,string cbcc,string cSubject,string cms);
411 FX_BOOL Document::mailForm(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
413 ASSERT(m_pDocument != NULL);
415 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) return FALSE;
417 int iLength = params.size();
419 FX_BOOL bUI = iLength > 0 ? (FX_BOOL)params[0] : TRUE;
420 CFX_WideString cTo = iLength > 1 ? (FX_LPCWSTR)params[1].operator CFX_WideString() : L"";
421 CFX_WideString cCc = iLength > 2 ? (FX_LPCWSTR)params[2].operator CFX_WideString() : L"";
422 CFX_WideString cBcc = iLength > 3 ? (FX_LPCWSTR)params[3].operator CFX_WideString() : L"";
423 CFX_WideString cSubject = iLength > 4 ? (FX_LPCWSTR)params[4].operator CFX_WideString() : L"";
424 CFX_WideString cMsg = iLength > 5 ? (FX_LPCWSTR)params[5].operator CFX_WideString() : L"";
426 CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)m_pDocument->GetInterForm();
427 ASSERT(pInterForm != NULL);
429 CFX_ByteTextBuf textBuf;
430 if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
433 CJS_Context* pContext = (CJS_Context*)cc;
434 ASSERT(pContext != NULL);
435 CPDFDoc_Environment* pEnv = pContext->GetReaderApp();
436 ASSERT(pEnv != NULL);
437 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
438 ASSERT(pRuntime != NULL);
440 pRuntime->BeginBlock();
441 pEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI, cTo.c_str(), cSubject.c_str(), cCc.c_str(), cBcc.c_str(), cMsg.c_str());
442 pRuntime->EndBlock();
446 FX_BOOL Document::print(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
448 CJS_Context* pContext = (CJS_Context*)cc;
449 ASSERT(pContext != NULL);
450 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
451 ASSERT(pRuntime != NULL);
456 FX_BOOL bSilent = FALSE;
457 FX_BOOL bShrinkToFit = FALSE;
458 FX_BOOL bPrintAsImage = FALSE;
459 FX_BOOL bReverse = FALSE;
460 FX_BOOL bAnnotations = FALSE;
462 int nlength = params.size();
465 if (params[8].GetType() == VT_fxobject)
467 JSFXObject pObj = (JSFXObject)params[8];
469 if (JS_GetObjDefnID(pObj) == JS_GetObjDefnID(*pRuntime, L"PrintParamsObj"))
471 if (CJS_Object* pJSObj = (CJS_Object*)params[8])
473 if (PrintParamsObj* pprintparamsObj = (PrintParamsObj*)pJSObj->GetEmbedObject())
475 bUI = pprintparamsObj->bUI;
476 nStart = pprintparamsObj->nStart;
477 nEnd = pprintparamsObj->nEnd;
478 bSilent = pprintparamsObj->bSilent;
479 bShrinkToFit = pprintparamsObj->bShrinkToFit;
480 bPrintAsImage = pprintparamsObj->bPrintAsImage;
481 bReverse = pprintparamsObj->bReverse;
482 bAnnotations = pprintparamsObj->bAnnotations;
494 nStart = (int)params[1];
496 nEnd = (int)params[2];
500 bShrinkToFit = params[4];
502 bPrintAsImage = params[5];
504 bReverse = params[6];
506 bAnnotations = params[7];
509 ASSERT(m_pDocument != NULL);
511 if (CPDFDoc_Environment* pEnv = m_pDocument->GetEnv())
513 pEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit, bPrintAsImage, bReverse, bAnnotations);
519 //removes the specified field from the document.
521 //note: if the filed name is not retional, adobe is dumb for it.
523 FX_BOOL Document::removeField(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
525 ASSERT(m_pDocument != NULL);
527 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
528 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM))) return FALSE;
530 if (params.size() < 1)
533 CFX_WideString sFieldName = params[0].operator CFX_WideString();
535 CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)m_pDocument->GetInterForm();
536 ASSERT(pInterForm != NULL);
538 CFX_PtrArray widgets;
539 pInterForm->GetWidgets(sFieldName, widgets);
541 int nSize = widgets.GetSize();
545 for (int i=0; i<nSize; i++)
547 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)widgets[i];
548 ASSERT(pWidget != NULL);
550 CPDF_Rect rcAnnot = pWidget->GetRect();
556 CFX_RectArray aRefresh;
557 aRefresh.Add(rcAnnot);
559 CPDFXFA_Page* pPage = pWidget->GetPDFXFAPage();
560 ASSERT(pPage != NULL);
562 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage);
563 pPageView->DeleteAnnot(pWidget);
565 pPageView->UpdateRects(aRefresh);
567 m_pDocument->SetChangeMark();
573 //reset filed values within a document.
575 //note: if the fields names r not rational, aodbe is dumb for it.
577 FX_BOOL Document::resetForm(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
579 ASSERT(m_pDocument != NULL);
581 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
582 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM) ||
583 m_pDocument->GetPermissions(FPDFPERM_FILL_FORM))) return FALSE;
585 CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)m_pDocument->GetInterForm();
586 ASSERT(pInterForm != NULL);
588 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
589 ASSERT(pPDFForm != NULL);
591 v8::Isolate* isolate = GetIsolate(cc);
592 CJS_Array aName(isolate);
594 if (params.size() > 0)
596 switch (params[0].GetType())
599 aName.Attach(params[0]);
602 aName.SetElement(0,params[0]);
606 CFX_PtrArray aFields;
608 for (int i=0,isz=aName.GetLength(); i<isz; i++)
610 CJS_Value valElement(isolate);
611 aName.GetElement(i,valElement);
612 CFX_WideString swVal = valElement.operator CFX_WideString();
614 for (int j=0,jsz=pPDFForm->CountFields(swVal); j<jsz; j++)
616 aFields.Add((void*)pPDFForm->GetField(j,swVal));
620 if (aFields.GetSize() > 0)
622 pPDFForm->ResetForm(aFields, TRUE, TRUE);
623 m_pDocument->SetChangeMark();
629 pPDFForm->ResetForm(TRUE);
630 m_pDocument->SetChangeMark();
638 FX_BOOL Document::saveAs(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
640 // Unsafe, not supported.
645 FX_BOOL Document::submitForm(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
647 ASSERT(m_pDocument != NULL);
649 // if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) return FALSE;
651 int nSize = params.size();
652 if (nSize < 1) return FALSE;
654 CFX_WideString strURL;
656 FX_BOOL bEmpty = FALSE;
657 v8::Isolate* isolate = GetIsolate(cc);
658 CJS_Array aFields(isolate);
660 CJS_Value v = params[0];
661 if (v.GetType() == VT_string)
663 strURL = params[0].operator CFX_WideString();
669 aFields.Attach(params[3]);
671 else if (v.GetType() == VT_object)
673 JSObject pObj = (JSObject)params[0];
674 v8::Handle<v8::Value> pValue = JS_GetObjectElement(isolate,pObj, L"cURL");
675 if (!pValue.IsEmpty())
676 strURL = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue));
677 pValue = JS_GetObjectElement(isolate,pObj, L"bFDF");
678 bFDF = CJS_Value(isolate,pValue, GET_VALUE_TYPE(pValue));
679 pValue = JS_GetObjectElement(isolate,pObj, L"bEmpty");
680 bEmpty = CJS_Value(isolate,pValue, GET_VALUE_TYPE(pValue));
681 pValue = JS_GetObjectElement(isolate,pObj,L"aFields");
682 aFields.Attach(CJS_Value(isolate,pValue, GET_VALUE_TYPE(pValue)));
685 CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)m_pDocument->GetInterForm();
686 ASSERT(pInterForm != NULL);
687 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
688 ASSERT(pPDFInterForm != NULL);
690 FX_BOOL bAll = (aFields.GetLength() == 0);
694 CJS_Context* pContext = (CJS_Context*)cc;
695 ASSERT(pContext != NULL);
696 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
697 ASSERT(pRuntime != NULL);
700 if (pPDFInterForm->CheckRequiredFields())
702 pRuntime->BeginBlock();
703 pInterForm->SubmitForm(strURL, FALSE);
704 pRuntime->EndBlock();
711 CFX_PtrArray fieldObjects;
713 for (int i=0,sz=aFields.GetLength(); i<sz; i++)
715 CJS_Value valName(isolate);
716 aFields.GetElement(i, valName);
717 CFX_WideString sName = valName.operator CFX_WideString();
719 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
720 ASSERT(pPDFForm != NULL);
722 for (int j=0, jsz=pPDFForm->CountFields(sName); j<jsz; j++)
724 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
725 if (!bEmpty && pField->GetValue().IsEmpty())
728 fieldObjects.Add(pField);
732 CJS_Context* pContext = (CJS_Context*)cc;
733 ASSERT(pContext != NULL);
734 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
735 ASSERT(pRuntime != NULL);
738 if (pPDFInterForm->CheckRequiredFields(&fieldObjects, TRUE))
740 pRuntime->BeginBlock();
741 pInterForm->SubmitFields(strURL, fieldObjects, TRUE, !bFDF);
742 pRuntime->EndBlock();
750 //////////////////////////////////////////////////////////////////////////////////////////////
752 void Document::AttachDoc(CPDFSDK_Document *pDoc)
757 CPDFSDK_Document * Document::GetReaderDoc()
762 FX_BOOL Document::ExtractFileName(CPDFSDK_Document *pDoc,CFX_ByteString &strFileName)
767 FX_BOOL Document::ExtractFolderName(CPDFSDK_Document *pDoc,CFX_ByteString &strFolderName)
772 FX_BOOL Document::bookmarkRoot(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
777 FX_BOOL Document::mailDoc(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
779 ASSERT(m_pDocument != NULL);
782 CFX_WideString cTo = L"";
783 CFX_WideString cCc = L"";
784 CFX_WideString cBcc = L"";
785 CFX_WideString cSubject = L"";
786 CFX_WideString cMsg = L"";
789 bUI = params.size()>=1?static_cast<FX_BOOL>(params[0]):TRUE;
790 cTo = params.size()>=2?(const wchar_t*)params[1].operator CFX_WideString():L"";
791 cCc = params.size()>=3?(const wchar_t*)params[2].operator CFX_WideString():L"";
792 cBcc = params.size()>=4?(const wchar_t*)params[3].operator CFX_WideString():L"";
793 cSubject = params.size()>=5?(const wchar_t*)params[4].operator CFX_WideString():L"";
794 cMsg = params.size()>=6?(const wchar_t*)params[5].operator CFX_WideString():L"";
796 v8::Isolate* isolate = GetIsolate(cc);
798 if(params.size()>=1 && params[0].GetType() == VT_object)
800 JSObject pObj = (JSObject )params[0];
802 v8::Handle<v8::Value> pValue = JS_GetObjectElement(isolate,pObj, L"bUI");
803 bUI = (int)CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue));
805 pValue = JS_GetObjectElement(isolate,pObj, L"cTo");
806 cTo = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
808 pValue = JS_GetObjectElement(isolate,pObj, L"cCc");
809 cCc = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
811 pValue = JS_GetObjectElement(isolate,pObj, L"cBcc");
812 cBcc = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
814 pValue = JS_GetObjectElement(isolate,pObj, L"cSubject");
815 cSubject = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
817 pValue = JS_GetObjectElement(isolate,pObj, L"cMsg");
818 cMsg = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
822 CJS_Context* pContext = (CJS_Context*)cc;
823 ASSERT(pContext != NULL);
824 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
825 ASSERT(pRuntime != NULL);
827 pRuntime->BeginBlock();
828 CPDFDoc_Environment* pEnv = pRuntime->GetReaderApp();
829 pEnv->JS_docmailForm(NULL, 0, bUI, cTo.c_str(), cSubject.c_str(), cCc.c_str(), cBcc.c_str(), cMsg.c_str());
830 pRuntime->EndBlock();
835 FX_BOOL Document::author(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
837 ASSERT(m_pDocument != NULL);
839 CPDF_Dictionary* pDictionary = m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
840 if (!pDictionary)return FALSE;
844 vp << pDictionary->GetUnicodeText("Author");
849 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) return FALSE;
851 CFX_WideString csAuthor;
853 pDictionary->SetAtString("Author", PDF_EncodeText(csAuthor));
854 m_pDocument->SetChangeMark();
859 FX_BOOL Document::info(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
861 ASSERT(m_pDocument != NULL);
863 CPDF_Dictionary* pDictionary = m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
864 if (!pDictionary)return FALSE;
866 CFX_WideString cwAuthor = pDictionary->GetUnicodeText("Author");
867 CFX_WideString cwTitle = pDictionary->GetUnicodeText("Title");
868 CFX_WideString cwSubject = pDictionary->GetUnicodeText("Subject");
869 CFX_WideString cwKeywords = pDictionary->GetUnicodeText("Keywords");
870 CFX_WideString cwCreator = pDictionary->GetUnicodeText("Creator");
871 CFX_WideString cwProducer = pDictionary->GetUnicodeText("Producer");
872 CFX_WideString cwCreationDate = pDictionary->GetUnicodeText("CreationDate");
873 CFX_WideString cwModDate = pDictionary->GetUnicodeText("ModDate");
874 CFX_WideString cwTrapped = pDictionary->GetUnicodeText("Trapped");
876 v8::Isolate* isolate = GetIsolate(cc);
879 CJS_Context* pContext = (CJS_Context *)cc;
880 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
882 JSFXObject pObj = JS_NewFxDynamicObj(*pRuntime, pContext, -1);
884 JS_PutObjectString(isolate,pObj, L"Author", cwAuthor);
885 JS_PutObjectString(isolate,pObj, L"Title", cwTitle);
886 JS_PutObjectString(isolate,pObj, L"Subject", cwSubject);
887 JS_PutObjectString(isolate,pObj, L"Keywords", cwKeywords);
888 JS_PutObjectString(isolate,pObj, L"Creator", cwCreator);
889 JS_PutObjectString(isolate,pObj, L"Producer", cwProducer);
890 JS_PutObjectString(isolate,pObj, L"CreationDate", cwCreationDate);
891 JS_PutObjectString(isolate,pObj, L"ModDate", cwModDate);
892 JS_PutObjectString(isolate,pObj, L"Trapped", cwTrapped);
894 // It's to be compatible to non-standard info dictionary.
895 FX_POSITION pos = pDictionary->GetStartPos();
898 CFX_ByteString bsKey;
899 CPDF_Object* pValueObj = pDictionary->GetNextElement(pos, bsKey);
900 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey, bsKey.GetLength());
901 if((pValueObj->GetType()==PDFOBJ_STRING) || (pValueObj->GetType()==PDFOBJ_NAME) )
902 JS_PutObjectString(isolate,pObj, wsKey, pValueObj->GetUnicodeText());
903 if(pValueObj->GetType()==PDFOBJ_NUMBER)
904 JS_PutObjectNumber(isolate,pObj, wsKey, (float)pValueObj->GetNumber());
905 if(pValueObj->GetType()==PDFOBJ_BOOLEAN)
906 JS_PutObjectBoolean(isolate,pObj, wsKey, (bool)pValueObj->GetInteger());
918 FX_BOOL Document::creationDate(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
920 ASSERT(m_pDocument != NULL);
922 CPDF_Dictionary* pDictionary = m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
923 if (!pDictionary)return FALSE;
927 vp << pDictionary->GetUnicodeText("CreationDate");
932 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) return FALSE;
934 CFX_WideString csCreationDate;
935 vp >> csCreationDate;
936 pDictionary->SetAtString("CreationDate", PDF_EncodeText(csCreationDate));
937 m_pDocument->SetChangeMark();
943 FX_BOOL Document::creator(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
945 ASSERT(m_pDocument != NULL);
947 CPDF_Dictionary* pDictionary = m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
948 if (!pDictionary)return FALSE;
952 vp << pDictionary->GetUnicodeText("Creator");
957 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) return FALSE;
959 CFX_WideString csCreator;
961 pDictionary->SetAtString("Creator", PDF_EncodeText(csCreator));
962 m_pDocument->SetChangeMark();
967 FX_BOOL Document::delay(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
976 ASSERT(m_pDocument != NULL);
978 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) return FALSE;
987 for (int i=0,sz=m_DelayData.GetSize(); i<sz; i++)
988 delete m_DelayData.GetAt(i);
990 m_DelayData.RemoveAll();
994 for (int i=0,sz=m_DelayData.GetSize(); i<sz; i++)
996 if (CJS_DelayData* pData = m_DelayData.GetAt(i))
998 Field::DoDelay(m_pDocument, pData);
999 delete m_DelayData.GetAt(i);
1002 m_DelayData.RemoveAll();
1009 FX_BOOL Document::keywords(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1011 ASSERT(m_pDocument != NULL);
1013 CPDF_Dictionary* pDictionary = m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
1014 if (!pDictionary)return FALSE;
1018 vp << pDictionary->GetUnicodeText("Keywords");
1023 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) return FALSE;
1025 CFX_WideString csKeywords;
1027 pDictionary->SetAtString("Keywords", PDF_EncodeText(csKeywords));
1028 m_pDocument->SetChangeMark();
1033 FX_BOOL Document::modDate(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1035 ASSERT(m_pDocument != NULL);
1037 CPDF_Dictionary* pDictionary = m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
1038 if (!pDictionary)return FALSE;
1042 vp << pDictionary->GetUnicodeText("ModDate");
1047 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) return FALSE;
1049 CFX_WideString csmodDate;
1051 pDictionary->SetAtString("ModDate", PDF_EncodeText(csmodDate));
1052 m_pDocument->SetChangeMark();
1057 FX_BOOL Document::producer(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1059 ASSERT(m_pDocument != NULL);
1061 CPDF_Dictionary* pDictionary = m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
1062 if (!pDictionary)return FALSE;
1066 vp << pDictionary->GetUnicodeText("Producer");
1071 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) return FALSE;
1073 CFX_WideString csproducer;
1075 pDictionary->SetAtString("Producer", PDF_EncodeText(csproducer));
1076 m_pDocument->SetChangeMark();
1081 FX_BOOL Document::subject(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1083 ASSERT(m_pDocument != NULL);
1085 CPDF_Dictionary* pDictionary = m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
1086 if (!pDictionary)return FALSE;
1090 vp << pDictionary->GetUnicodeText("Subject");
1095 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) return FALSE;
1097 CFX_WideString cssubject;
1099 pDictionary->SetAtString("Subject", PDF_EncodeText(cssubject));
1100 m_pDocument->SetChangeMark();
1105 FX_BOOL Document::title(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1107 ASSERT(m_pDocument != NULL);
1109 if (m_pDocument == NULL || m_pDocument->GetDocument() == NULL)
1112 CPDF_Dictionary* pDictionary = m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
1113 if (!pDictionary)return FALSE;
1117 vp << pDictionary->GetUnicodeText("Title");
1122 if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) return FALSE;
1124 CFX_WideString cstitle;
1126 pDictionary->SetAtString("Title", PDF_EncodeText(cstitle));
1127 m_pDocument->SetChangeMark();
1132 FX_BOOL Document::numPages(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1134 if (vp.IsSetting()) {
1135 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1136 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
1139 vp << m_pDocument->GetPageCount();
1143 FX_BOOL Document::external(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1145 //In Chrome case,should always return true.
1150 FX_BOOL Document::filesize(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1152 if (vp.IsSetting()) {
1153 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1154 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
1161 FX_BOOL Document::mouseX(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1166 FX_BOOL Document::mouseY(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1171 FX_BOOL Document::baseURL(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1185 FX_BOOL Document::calculate(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1187 ASSERT(m_pDocument != NULL);
1189 CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)m_pDocument->GetInterForm();
1190 ASSERT(pInterForm != NULL);
1194 if (pInterForm->IsCalculateEnabled())
1204 pInterForm->EnableCalculate(bCalculate);
1210 FX_BOOL Document::documentFileName(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1212 if (vp.IsSetting()) {
1213 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1214 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
1217 CFX_WideString wsFilePath = m_pDocument->GetPath();
1218 FX_INT32 i = wsFilePath.GetLength() - 1;
1219 for ( ; i >= 0; i-- )
1221 if ( wsFilePath.GetAt( i ) == L'\\' || wsFilePath.GetAt( i ) == L'/' )
1224 if ( i >= 0 && i < wsFilePath.GetLength() - 1 )
1226 vp << ( wsFilePath.GetBuffer( wsFilePath.GetLength() ) + i + 1 );
1233 CFX_WideString Document::ReversalStr(CFX_WideString cbFrom)
1235 size_t iLength = cbFrom.GetLength();
1236 pdfium::base::CheckedNumeric<size_t> iSize = sizeof(wchar_t);
1237 iSize *= (iLength + 1);
1238 wchar_t* pResult = (wchar_t*)malloc(iSize.ValueOrDie());
1239 wchar_t* pFrom = (wchar_t*)cbFrom.GetBuffer(iLength);
1241 for (size_t i = 0; i < iLength; i++)
1243 pResult[i] = *(pFrom + iLength - i - 1);
1245 pResult[iLength] = L'\0';
1247 cbFrom.ReleaseBuffer();
1248 CFX_WideString cbRet = CFX_WideString(pResult);
1254 CFX_WideString Document::CutString(CFX_WideString cbFrom)
1256 size_t iLength = cbFrom.GetLength();
1257 pdfium::base::CheckedNumeric<size_t> iSize = sizeof(wchar_t);
1258 iSize *= (iLength + 1);
1259 wchar_t* pResult = (wchar_t*)malloc(iSize.ValueOrDie());
1260 wchar_t* pFrom = (wchar_t*)cbFrom.GetBuffer(iLength);
1262 for (int i = 0; i < iLength; i++)
1264 if (pFrom[i] == L'\\' || pFrom[i] == L'/')
1269 pResult[i] = pFrom[i];
1271 pResult[iLength] = L'\0';
1273 cbFrom.ReleaseBuffer();
1274 CFX_WideString cbRet = CFX_WideString(pResult);
1280 FX_BOOL Document::path(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1282 if (vp.IsSetting()) {
1283 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1284 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
1287 vp << app::SysPathToPDFPath(m_pDocument->GetPath());
1291 FX_BOOL Document::pageWindowRect(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1296 FX_BOOL Document::layout(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1301 FX_BOOL Document::addLink(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1306 FX_BOOL Document::closeDoc(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1308 ASSERT(m_pDocument != NULL);
1317 FX_BOOL Document::getPageBox(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1323 FX_BOOL Document::getAnnot(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1328 FX_BOOL Document::getAnnots(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1334 FX_BOOL Document::getAnnot3D(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1340 FX_BOOL Document::getAnnots3D(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1342 vRet = VT_undefined;
1346 FX_BOOL Document::getOCGs(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1351 FX_BOOL Document::getLinks(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1356 bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect)
1358 if (rect.left <= LinkRect.left
1359 && rect.top <= LinkRect.top
1360 && rect.right >= LinkRect.right
1361 && rect.bottom >= LinkRect.bottom)
1367 void IconTree::InsertIconElement(IconElement* pNewIcon)
1369 if (!pNewIcon)return;
1371 if (m_pHead == NULL && m_pEnd == NULL)
1373 m_pHead = m_pEnd = pNewIcon;
1378 m_pEnd->NextIcon = pNewIcon;
1384 void IconTree::DeleteIconTree()
1386 if (!m_pHead || !m_pEnd)return;
1388 IconElement* pTemp = NULL;
1389 while(m_pEnd != m_pHead)
1392 m_pHead = m_pHead->NextIcon;
1401 int IconTree::GetLength()
1406 IconElement* IconTree::operator [](int iIndex)
1408 if (iIndex >= 0 && iIndex <= m_iLength)
1410 IconElement* pTemp = m_pHead;
1411 for (int i = 0; i < iIndex; i++)
1413 pTemp = pTemp->NextIcon;
1421 void IconTree::DeleteIconElement(CFX_WideString swIconName)
1423 IconElement* pTemp = m_pHead;
1424 int iLoopCount = m_iLength;
1425 for (int i = 0; i < iLoopCount - 1; i++)
1427 if (pTemp == m_pEnd)
1430 if (m_pHead->IconName == swIconName)
1432 m_pHead = m_pHead->NextIcon;
1437 if (pTemp->NextIcon->IconName == swIconName)
1439 if (pTemp->NextIcon == m_pEnd)
1442 delete pTemp->NextIcon;
1444 pTemp->NextIcon = NULL;
1448 IconElement* pElement = pTemp->NextIcon;
1449 pTemp->NextIcon = pTemp->NextIcon->NextIcon;
1458 pTemp = pTemp->NextIcon;
1462 FX_BOOL Document::addIcon(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1464 if (params.size() != 2)return FALSE;
1466 CJS_Context* pContext = (CJS_Context*)cc;
1467 ASSERT(pContext != NULL);
1468 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1469 ASSERT(pRuntime != NULL);
1471 CFX_WideString swIconName = params[0].operator CFX_WideString();
1473 JSFXObject pJSIcon = (JSFXObject)params[1];
1474 if (JS_GetObjDefnID(pJSIcon) != JS_GetObjDefnID(*pRuntime, L"Icon")) return FALSE;
1476 CJS_EmbedObj* pEmbedObj = ((CJS_Object*)params[1])->GetEmbedObject();
1477 if (!pEmbedObj)return FALSE;
1478 Icon* pIcon = (Icon*)pEmbedObj;
1481 m_pIconTree = new IconTree();
1483 IconElement* pNewIcon = new IconElement();
1484 pNewIcon->IconName = swIconName;
1485 pNewIcon->NextIcon = NULL;
1486 pNewIcon->IconStream = pIcon;
1487 m_pIconTree->InsertIconElement(pNewIcon);
1491 FX_BOOL Document::icons(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1493 if (vp.IsSetting()) {
1494 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1495 sError = JSGetStringFromID(pContext, IDS_STRING_JSREADONLY);
1505 CJS_Array Icons(m_isolate);
1506 IconElement* pIconElement = NULL;
1507 int iIconTreeLength = m_pIconTree->GetLength();
1509 CJS_Context* pContext = (CJS_Context *)cc;
1510 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1512 for (int i = 0; i < iIconTreeLength; i++)
1514 pIconElement = (*m_pIconTree)[i];
1516 JSFXObject pObj = JS_NewFxDynamicObj(*pRuntime, pContext, JS_GetObjDefnID(*pRuntime, L"Icon"));
1517 if (pObj.IsEmpty()) return FALSE;
1519 CJS_Icon * pJS_Icon = (CJS_Icon *)JS_GetPrivate(pObj);
1520 if (!pJS_Icon) return FALSE;
1522 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1523 if (!pIcon)return FALSE;
1525 pIcon->SetStream(pIconElement->IconStream->GetStream());
1526 pIcon->SetIconName(pIconElement->IconName);
1527 Icons.SetElement(i, CJS_Value(m_isolate,pJS_Icon));
1534 FX_BOOL Document::getIcon(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1536 if (params.size() != 1)return FALSE;
1539 CFX_WideString swIconName = params[0].operator CFX_WideString();
1540 int iIconCounts = m_pIconTree->GetLength();
1542 CJS_Context* pContext = (CJS_Context *)cc;
1543 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1545 for (int i = 0; i < iIconCounts; i++)
1547 if ((*m_pIconTree)[i]->IconName == swIconName)
1549 Icon* pRetIcon = (*m_pIconTree)[i]->IconStream;
1551 JSFXObject pObj = JS_NewFxDynamicObj(*pRuntime, pContext, JS_GetObjDefnID(*pRuntime, L"Icon"));
1552 if (pObj.IsEmpty()) return FALSE;
1554 CJS_Icon * pJS_Icon = (CJS_Icon *)JS_GetPrivate(pObj);
1555 if (!pJS_Icon) return FALSE;
1557 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1558 if (!pIcon)return FALSE;
1560 pIcon->SetIconName(swIconName);
1561 pIcon->SetStream(pRetIcon->GetStream());
1570 FX_BOOL Document::removeIcon(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1572 if (params.size() != 1)return FALSE;
1575 CFX_WideString swIconName = params[0].operator CFX_WideString();
1579 FX_BOOL Document::createDataObject(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1581 // Unsafe, not implemented.
1585 FX_BOOL Document::media(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1590 FX_BOOL Document::calculateNow(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1592 ASSERT(m_pDocument != NULL);
1594 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
1595 m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM) ||
1596 m_pDocument->GetPermissions(FPDFPERM_FILL_FORM))) return FALSE;
1598 CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)m_pDocument->GetInterForm();
1599 ASSERT(pInterForm != NULL);
1600 pInterForm->OnCalculate();
1604 FX_BOOL Document::Collab(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1609 FX_BOOL Document::getPageNthWord(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1611 ASSERT(m_pDocument != NULL);
1613 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) return FALSE;
1615 int nPageNo = params.GetSize() > 0 ? (int)params[0] : 0;
1616 int nWordNo = params.GetSize() > 1 ? (int)params[1] : 0;
1617 bool bStrip = params.GetSize() > 2 ? (bool)params[2] : true;
1619 CPDF_Document* pDocument = m_pDocument->GetDocument()->GetPDFDoc();
1620 if (!pDocument) return FALSE;
1622 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount())
1624 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1625 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1629 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1630 if (!pPageDict) return FALSE;
1633 page.Load(pDocument, pPageDict);
1635 page.ParseContent();
1637 FX_POSITION pos = page.GetFirstObjectPosition();
1641 CFX_WideString swRet;
1645 if (CPDF_PageObject* pPageObj = page.GetNextObject(pos))
1647 if (pPageObj->m_Type == PDFPAGE_TEXT)
1649 int nObjWords = CountWords((CPDF_TextObject*)pPageObj);
1651 if (nWords + nObjWords >= nWordNo)
1653 swRet = GetObjWordStr((CPDF_TextObject*)pPageObj, nWordNo - nWords);
1657 nWords += nObjWords;
1672 FX_BOOL Document::getPageNthWordQuads(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1674 ASSERT(m_pDocument != NULL);
1676 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) return FALSE;
1681 FX_BOOL Document::getPageNumWords(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1683 ASSERT(m_pDocument != NULL);
1685 if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) return FALSE;
1687 int nPageNo = params.GetSize() > 0 ? (int)params[0] : 0;
1689 CPDF_Document* pDocument = m_pDocument->GetDocument()->GetPDFDoc();
1690 ASSERT(pDocument != NULL);
1692 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount())
1694 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1695 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1699 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1700 if (!pPageDict) return FALSE;
1703 page.Load(pDocument, pPageDict);
1705 page.ParseContent();
1707 FX_POSITION pos = page.GetFirstObjectPosition();
1713 if (CPDF_PageObject* pPageObj = page.GetNextObject(pos))
1715 if (pPageObj->m_Type == PDFPAGE_TEXT)
1717 CPDF_TextObject* pTextObj = (CPDF_TextObject*)pPageObj;
1718 nWords += CountWords(pTextObj);
1728 FX_BOOL Document::getPrintParams(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1730 CJS_Context* pContext = (CJS_Context*)cc;
1731 ASSERT(pContext != NULL);
1732 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1733 ASSERT(pRuntime != NULL);
1734 JSFXObject pRetObj = JS_NewFxDynamicObj(*pRuntime, pContext, JS_GetObjDefnID(*pRuntime, L"PrintParamsObj"));
1735 //not implemented yet.
1741 #define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1743 int Document::CountWords(CPDF_TextObject* pTextObj)
1745 if (!pTextObj) return 0;
1749 CPDF_Font* pFont = pTextObj->GetFont();
1750 if (!pFont) return 0;
1752 FX_BOOL bIsLatin = FALSE;
1754 for (int i=0, sz=pTextObj->CountChars(); i<sz; i++)
1756 FX_DWORD charcode = -1;
1759 pTextObj->GetCharInfo(i, charcode, kerning);
1760 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1762 FX_WORD unicode = 0;
1763 if (swUnicode.GetLength() > 0)
1764 unicode = swUnicode[0];
1766 if (ISLATINWORD(unicode) && bIsLatin)
1769 bIsLatin = ISLATINWORD(unicode);
1770 if (unicode != 0x20)
1777 CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj, int nWordIndex)
1779 ASSERT(pTextObj != NULL);
1781 CFX_WideString swRet;
1783 CPDF_Font* pFont = pTextObj->GetFont();
1784 if (!pFont) return L"";
1787 FX_BOOL bIsLatin = FALSE;
1789 for (int i=0, sz=pTextObj->CountChars(); i<sz; i++)
1791 FX_DWORD charcode = -1;
1794 pTextObj->GetCharInfo(i, charcode, kerning);
1795 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1797 FX_WORD unicode = 0;
1798 if (swUnicode.GetLength() > 0)
1799 unicode = swUnicode[0];
1801 if (ISLATINWORD(unicode) && bIsLatin)
1806 bIsLatin = ISLATINWORD(unicode);
1807 if (unicode != 0x20)
1811 if (nWords-1 == nWordIndex)
1818 FX_BOOL Document::zoom(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1829 (fitV, FitVisibleWidth)
1834 FX_BOOL Document::zoomType(IFXJS_Context* cc, CJS_PropValue& vp, JS_ErrorString& sError)
1839 FX_BOOL Document::deletePages(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1841 v8::Isolate* isolate = GetIsolate(cc);
1842 ASSERT(m_pDocument != NULL);
1844 if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
1845 m_pDocument->GetPermissions(FPDFPERM_ASSEMBLE))) return FALSE;
1847 int iSize = params.size();
1855 else if (iSize == 1)
1857 if (params[0].GetType() == VT_object)
1859 JSObject pObj = (JSObject )params[0];
1860 v8::Handle<v8::Value> pValue = JS_GetObjectElement(isolate,pObj, L"nStart");
1861 nStart = (int)CJS_Value(m_isolate,pValue,GET_VALUE_TYPE(pValue));
1863 pValue = JS_GetObjectElement(isolate,pObj, L"nEnd");
1864 nEnd = (int)CJS_Value(m_isolate,pValue,GET_VALUE_TYPE(pValue));
1868 nStart = (int)params[0];
1873 nStart = (int)params[0];
1874 nEnd = (int)params[1];
1877 int nTotal = m_pDocument->GetPageCount();
1879 if (nStart < 0) nStart = 0;
1880 if (nStart >= nTotal) nStart = nTotal - 1;
1882 if (nEnd < 0) nEnd = 0;
1883 if (nEnd >= nTotal) nEnd = nTotal - 1;
1885 if (nEnd < nStart) nEnd = nStart;
1892 FX_BOOL Document::extractPages(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1894 // Unsafe, not supported.
1898 FX_BOOL Document::insertPages(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1900 // Unsafe, not supported.
1904 FX_BOOL Document::replacePages(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1906 // Unsafe, not supported.
1910 FX_BOOL Document::getURL(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
1912 // Unsafe, not supported.
1916 void Document::AddDelayData(CJS_DelayData* pData)
1918 m_DelayData.Add(pData);
1921 void Document::DoFieldDelay(const CFX_WideString& sFieldName, int nControlIndex)
1923 CFX_DWordArray DelArray;
1925 for (int i=0,sz=m_DelayData.GetSize(); i<sz; i++)
1927 if (CJS_DelayData* pData = m_DelayData.GetAt(i))
1929 if (pData->sFieldName == sFieldName && pData->nControlIndex == nControlIndex)
1931 Field::DoDelay(m_pDocument, pData);
1933 m_DelayData.SetAt(i, NULL);
1939 for (int j=DelArray.GetSize()-1; j>=0; j--)
1941 m_DelayData.RemoveAt(DelArray[j]);
1945 void Document::AddDelayAnnotData(CJS_AnnotObj *pData)
1947 m_DelayAnnotData.Add(pData);
1950 void Document::DoAnnotDelay()
1952 CFX_DWordArray DelArray;
1954 for (int j=DelArray.GetSize()-1; j>=0; j--)
1956 m_DelayData.RemoveAt(DelArray[j]);