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.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef FPDFSDK_INCLUDE_JAVASCRIPT_DOCUMENT_H_
8 #define FPDFSDK_INCLUDE_JAVASCRIPT_DOCUMENT_H_
9 
10 #include "JS_Define.h"
11 
12 class PrintParamsObj : public CJS_EmbedObj
13 {
14 public:
15 	PrintParamsObj(CJS_Object* pJSObject);
~PrintParamsObj()16 	virtual ~PrintParamsObj(){}
17 
18 public:
19 	FX_BOOL bUI;
20 	int nStart;
21 	int nEnd;
22 	FX_BOOL bSilent;
23 	FX_BOOL bShrinkToFit;
24 	FX_BOOL bPrintAsImage;
25 	FX_BOOL bReverse;
26 	FX_BOOL bAnnotations;
27 };
28 
29 class CJS_PrintParamsObj : public CJS_Object
30 {
31 public:
CJS_PrintParamsObj(JSFXObject pObject)32 	CJS_PrintParamsObj(JSFXObject pObject) : CJS_Object(pObject) {}
~CJS_PrintParamsObj()33 	virtual ~CJS_PrintParamsObj(){}
34 
35 	DECLARE_JS_CLASS(CJS_PrintParamsObj);
36 };
37 
38 
39 class Icon;
40 class Field;
41 
42 struct IconElement
43 {
IconElementIconElement44 	IconElement() : IconName(L""), NextIcon(NULL), IconStream(NULL) {}
~IconElementIconElement45 	virtual ~IconElement()
46 	{
47 	}
48 	CFX_WideString	IconName;
49 	IconElement*	NextIcon;
50 	Icon*			IconStream;
51 };
52 
53 class IconTree
54 {
55 public:
IconTree()56 	IconTree():m_pHead(NULL), m_pEnd(NULL), m_iLength(0)
57 	{
58 
59 	}
60 
~IconTree()61 	virtual ~IconTree()
62 	{
63 	}
64 
65 public:
66 	void			InsertIconElement(IconElement* pNewIcon);
67 	void			DeleteIconElement(CFX_WideString swIconName);
68 	void			DeleteIconTree();
69 	int				GetLength();
70 	IconElement*	operator[](int iIndex);
71 
72 private:
73 	IconElement*	m_pHead;
74 	IconElement*	m_pEnd;
75 	int				m_iLength;
76 };
77 
78 struct CJS_DelayData;
79 struct CJS_DelayAnnot;
80 struct CJS_AnnotObj;
81 
82 class Document : public CJS_EmbedObj
83 {
84 public:
85 	Document(CJS_Object* pJSObject);
86 	virtual ~Document();
87 
88 public:
89 	FX_BOOL	ADBE(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
90 	FX_BOOL	author(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
91 	FX_BOOL	baseURL(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
92 	FX_BOOL	bookmarkRoot(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
93 	FX_BOOL	calculate(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
94 	FX_BOOL	Collab(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
95 	FX_BOOL	creationDate(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
96 	FX_BOOL	creator(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
97 	FX_BOOL	delay(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
98 	FX_BOOL	dirty(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
99 	FX_BOOL	documentFileName(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
100 	FX_BOOL external(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
101 	FX_BOOL	filesize(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
102 	FX_BOOL	icons(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
103 	FX_BOOL	info(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
104 	FX_BOOL	keywords(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
105 	FX_BOOL	layout(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
106 	FX_BOOL	media(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
107 	FX_BOOL	modDate(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
108 	FX_BOOL	mouseX(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
109 	FX_BOOL	mouseY(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
110 	FX_BOOL	numFields(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
111 	FX_BOOL	numPages(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
112 	FX_BOOL	pageNum(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
113 	FX_BOOL	pageWindowRect(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
114 	FX_BOOL	path(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
115 	FX_BOOL	producer(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
116 	FX_BOOL	subject(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
117 	FX_BOOL	title(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
118 	FX_BOOL	zoom(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
119 	FX_BOOL	zoomType(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
120 
121 	FX_BOOL addAnnot(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
122 	FX_BOOL	addField(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
123 	FX_BOOL	addLink(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
124 	FX_BOOL	addIcon(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
125 	FX_BOOL	calculateNow(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
126 	FX_BOOL	closeDoc(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
127 	FX_BOOL	createDataObject(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
128 	FX_BOOL deletePages(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
129 	FX_BOOL	exportAsText(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
130 	FX_BOOL	exportAsFDF(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
131 	FX_BOOL	exportAsXFDF(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
132 	FX_BOOL extractPages(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
133 	FX_BOOL	getAnnot(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
134 	FX_BOOL	getAnnots(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
135 	FX_BOOL	getAnnot3D(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
136 	FX_BOOL	getAnnots3D(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
137 	FX_BOOL	getField(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
138 	FX_BOOL	getIcon(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
139 	FX_BOOL	getLinks(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
140 	FX_BOOL	getNthFieldName(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
141 	FX_BOOL	getOCGs(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
142 	FX_BOOL	getPageBox(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
143 	FX_BOOL	getPageNthWord(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
144 	FX_BOOL	getPageNthWordQuads(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
145 	FX_BOOL	getPageNumWords(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
146 	FX_BOOL getPrintParams(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
147 	FX_BOOL getURL(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
148 	FX_BOOL	importAnFDF(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
149 	FX_BOOL	importAnXFDF(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
150 	FX_BOOL	importTextData(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
151 	FX_BOOL insertPages(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
152 	FX_BOOL	mailForm(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
153 	FX_BOOL	print(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
154 	FX_BOOL	removeField(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
155 	FX_BOOL replacePages(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
156 	FX_BOOL	resetForm(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
157 	FX_BOOL	saveAs(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
158 	FX_BOOL	submitForm(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
159 	FX_BOOL	mailDoc(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
160 	FX_BOOL	removeIcon(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError);
161 
162 public:
163 	void                                    AttachDoc(CPDFSDK_Document* pDoc);
164 	CPDFSDK_Document*                       GetReaderDoc();
165 	static FX_BOOL                          ExtractFileName(CPDFSDK_Document* pDoc, CFX_ByteString& strFileName);
166 	static FX_BOOL                          ExtractFolderName(CPDFSDK_Document* pDoc, CFX_ByteString& strFolderName);
167 	void                                    AddDelayData(CJS_DelayData* pData);
168 	void                                    DoFieldDelay(const CFX_WideString& sFieldName, int nControlIndex);
169 	void                                    AddDelayAnnotData(CJS_AnnotObj *pData);
170 	void                                    DoAnnotDelay();
SetIsolate(v8::Isolate * isolate)171 	void                                    SetIsolate(v8::Isolate* isolate) {m_isolate = isolate;}
172 	CJS_Document*                           GetCJSDoc() const;
173 
174 private:
175 	CFX_WideString                          ReversalStr(CFX_WideString cbFrom);
176 	CFX_WideString                          CutString(CFX_WideString cbFrom);
177 	bool                                    IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect);
178 	int                                     CountWords(CPDF_TextObject* pTextObj);
179 	CFX_WideString                          GetObjWordStr(CPDF_TextObject* pTextObj, int nWordIndex);
180 	FX_BOOL                                 ParserParams(JSObject *pObj,CJS_AnnotObj& annotobj);
181 
182 	v8::Isolate*                            m_isolate;
183 	IconTree*                               m_pIconTree;
184 	CPDFSDK_Document*                       m_pDocument;
185 	CFX_WideString                          m_cwBaseURL;
186 	FX_BOOL                                 m_bDelay;
187 	CFX_ArrayTemplate<CJS_DelayData*>       m_DelayData;
188 	CFX_ArrayTemplate<CJS_AnnotObj*>        m_DelayAnnotData;
189 };
190 
191 class CJS_Document : public CJS_Object
192 {
193 public:
CJS_Document(JSFXObject pObject)194 	CJS_Document(JSFXObject pObject) : CJS_Object(pObject) {};
~CJS_Document()195 	virtual ~CJS_Document(){};
196 
197 	virtual FX_BOOL	InitInstance(IFXJS_Context* cc);
198 
199 	DECLARE_JS_CLASS(CJS_Document);
200 
201 	JS_STATIC_PROP(ADBE, Document);
202 	JS_STATIC_PROP(author, Document);
203 	JS_STATIC_PROP(baseURL, Document);
204 	JS_STATIC_PROP(bookmarkRoot, Document);
205 	JS_STATIC_PROP(calculate, Document);
206 	JS_STATIC_PROP(Collab, Document);
207 	JS_STATIC_PROP(creationDate, Document);
208 	JS_STATIC_PROP(creator, Document);
209 	JS_STATIC_PROP(delay, Document);
210 	JS_STATIC_PROP(dirty, Document);
211 	JS_STATIC_PROP(documentFileName, Document);
212 	JS_STATIC_PROP(external, Document);
213 	JS_STATIC_PROP(filesize, Document);
214 	JS_STATIC_PROP(icons, Document);
215 	JS_STATIC_PROP(info, Document);
216 	JS_STATIC_PROP(keywords, Document);
217 	JS_STATIC_PROP(layout, Document);
218 	JS_STATIC_PROP(media, Document);
219 	JS_STATIC_PROP(modDate, Document);
220 	JS_STATIC_PROP(mouseX, Document);
221 	JS_STATIC_PROP(mouseY, Document);
222 	JS_STATIC_PROP(numFields, Document);
223 	JS_STATIC_PROP(numPages, Document);
224 	JS_STATIC_PROP(pageNum, Document);
225 	JS_STATIC_PROP(pageWindowRect, Document);
226 	JS_STATIC_PROP(path, Document);
227 	JS_STATIC_PROP(producer, Document);
228 	JS_STATIC_PROP(subject, Document);
229 	JS_STATIC_PROP(title, Document);
230 	JS_STATIC_PROP(zoom, Document);
231 	JS_STATIC_PROP(zoomType, Document);
232 
233 	JS_STATIC_METHOD(addAnnot,Document);
234 	JS_STATIC_METHOD(addField, Document);
235 	JS_STATIC_METHOD(addLink, Document);
236 	JS_STATIC_METHOD(addIcon, Document);
237 	JS_STATIC_METHOD(calculateNow, Document);
238 	JS_STATIC_METHOD(closeDoc, Document);
239 	JS_STATIC_METHOD(createDataObject, Document);
240 	JS_STATIC_METHOD(deletePages, Document);
241 	JS_STATIC_METHOD(exportAsText, Document);
242 	JS_STATIC_METHOD(exportAsFDF, Document);
243 	JS_STATIC_METHOD(exportAsXFDF, Document);
244 	JS_STATIC_METHOD(extractPages, Document);
245 	JS_STATIC_METHOD(getAnnot, Document);
246 	JS_STATIC_METHOD(getAnnots, Document);
247 	JS_STATIC_METHOD(getAnnot3D, Document);
248 	JS_STATIC_METHOD(getAnnots3D, Document);
249 	JS_STATIC_METHOD(getField, Document);
250 	JS_STATIC_METHOD(getIcon, Document);
251 	JS_STATIC_METHOD(getLinks, Document);
252 	JS_STATIC_METHOD(getNthFieldName, Document);
253 	JS_STATIC_METHOD(getOCGs, Document);
254 	JS_STATIC_METHOD(getPageBox, Document);
255 	JS_STATIC_METHOD(getPageNthWord, Document);
256 	JS_STATIC_METHOD(getPageNthWordQuads, Document);
257 	JS_STATIC_METHOD(getPageNumWords, Document);
258 	JS_STATIC_METHOD(getPrintParams, Document);
259 	JS_STATIC_METHOD(getURL, Document);
260 	JS_STATIC_METHOD(importAnFDF, Document);
261 	JS_STATIC_METHOD(importAnXFDF, Document);
262 	JS_STATIC_METHOD(importTextData, Document);
263 	JS_STATIC_METHOD(insertPages, Document);
264 	JS_STATIC_METHOD(mailForm, Document);
265 	JS_STATIC_METHOD(print, Document);
266 	JS_STATIC_METHOD(removeField, Document);
267 	JS_STATIC_METHOD(replacePages, Document);
268 	JS_STATIC_METHOD(removeIcon, Document);
269 	JS_STATIC_METHOD(resetForm, Document);
270 	JS_STATIC_METHOD(saveAs, Document);
271 	JS_STATIC_METHOD(submitForm, Document);
272 	JS_STATIC_METHOD(mailDoc, Document);
273 };
274 
275 #endif  // FPDFSDK_INCLUDE_JAVASCRIPT_DOCUMENT_H_
276