blob: 2fdc7de3f581df3ead3027bf3fe82f500d7c08a1 [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.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepez1ed8a212015-05-11 15:25:39 -07007#include "../../public/fpdf_edit.h"
8#include "../../public/fpdf_formfill.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009#include "../include/fsdk_define.h"
Bo Xufdc00a72014-10-28 23:03:33 -070010#include "../include/fpdfxfa/fpdfxfa_doc.h"
11#include "../include/fpdfxfa/fpdfxfa_app.h"
12#include "../include/fpdfxfa/fpdfxfa_page.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014#if _FX_OS_ == _FX_ANDROID_
15#include "time.h"
16#else
17#include <ctime>
18#endif
19
20DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_CreateNewDocument()
21{
22 CPDF_Document* pDoc = FX_NEW CPDF_Document;
23 if (!pDoc)
24 return NULL;
25 pDoc->CreateNewDoc();
26 time_t currentTime;
27
28 CFX_ByteString DateStr;
29
30 if(FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
31 {
32 if ( -1 != time( &currentTime ) )
33 {
34 tm * pTM = localtime( &currentTime );
35 if ( pTM )
36 {
37 DateStr.Format( "D:%04d%02d%02d%02d%02d%02d", pTM->tm_year+1900, pTM->tm_mon+1,
38 pTM->tm_mday, pTM->tm_hour, pTM->tm_min, pTM->tm_sec );
39 }
40 }
41 }
Tom Sepezbdeeb8a2015-05-27 12:25:00 -070042
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043 CPDF_Dictionary* pInfoDict = NULL;
44 pInfoDict = pDoc->GetInfo();
45 if (pInfoDict)
46 {
47 if(FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
48 pInfoDict->SetAt("CreationDate", new CPDF_String(DateStr));
John Abd-El-Malek207299b2014-12-15 12:13:45 -080049 pInfoDict->SetAt("Creator",FX_NEW CPDF_String(L"PDFium"));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050 }
51
Tom Sepezbdeeb8a2015-05-27 12:25:00 -070052 CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
Bo Xufdc00a72014-10-28 23:03:33 -070053 CPDFXFA_Document* document = FX_NEW CPDFXFA_Document(pDoc, pApp);
Bo Xufdc00a72014-10-28 23:03:33 -070054 return document;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055}
56
57DLLEXPORT void STDCALL FPDFPage_Delete(FPDF_DOCUMENT document, int page_index)
58{
Bo Xufdc00a72014-10-28 23:03:33 -070059 CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060 if (pDoc == NULL)
61 return;
62 if (page_index < 0 || page_index >= pDoc->GetPageCount())
63 return;
64
65 pDoc->DeletePage(page_index);
66}
67
68DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document, int page_index, double width, double height)
69{
70 if (!document)
71 return NULL;
72
73// CPDF_Parser* pParser = (CPDF_Parser*)document;
Bo Xufdc00a72014-10-28 23:03:33 -070074 CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075 if(page_index < 0)
76 page_index = 0;
77 if(pDoc->GetPageCount()<page_index)
78 page_index = pDoc->GetPageCount();
79// if (page_index < 0 || page_index >= pDoc->GetPageCount())
80// return NULL;
81
82 CPDF_Dictionary* pPageDict = pDoc->CreateNewPage(page_index);
83 if(!pPageDict)
84 return NULL;
85 CPDF_Array* pMediaBoxArray = FX_NEW CPDF_Array;
86 pMediaBoxArray->Add(FX_NEW CPDF_Number(0));
87 pMediaBoxArray->Add(FX_NEW CPDF_Number(0));
88 pMediaBoxArray->Add(FX_NEW CPDF_Number(FX_FLOAT(width)));
89 pMediaBoxArray->Add(FX_NEW CPDF_Number(FX_FLOAT(height)));
90
91 pPageDict->SetAt("MediaBox", pMediaBoxArray);
92 pPageDict->SetAt("Rotate", FX_NEW CPDF_Number(0));
93 pPageDict->SetAt("Resources", FX_NEW CPDF_Dictionary);
94
Bo Xufdc00a72014-10-28 23:03:33 -070095// CPDF_Page* pPage = FX_NEW CPDF_Page;
96// pPage->Load(pDoc,pPageDict);
97// pPage->ParseContent();
98
99 CPDFXFA_Page* pPage = FX_NEW CPDFXFA_Page((CPDFXFA_Document*)document, page_index);
100 pPage->LoadPDFPage(pPageDict);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101
102 return pPage;
103}
104
105DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page)
106{
Bo Xufdc00a72014-10-28 23:03:33 -0700107 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
Bo Xu287e11a2014-06-30 09:49:21 -0700108 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700109 || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
110 {
111 return -1;
112 }
113 CPDF_Dictionary* pDict = pPage->m_pFormDict;
114
115 int rotate = 0;
116 if(pDict != NULL)
117 {
Bo Xu287e11a2014-06-30 09:49:21 -0700118 if (pDict->KeyExist("Rotate"))
119 rotate = pDict->GetElement("Rotate")->GetDirect()? pDict->GetElement("Rotate")->GetDirect()->GetInteger() / 90 : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120 else
121 {
122 if(pDict->KeyExist("Parent"))
123 {
124 CPDF_Dictionary* pPages = (CPDF_Dictionary*)pDict->GetElement("Parent")->GetDirect();
125 while(pPages)
126 {
127 if(pPages->KeyExist("Rotate"))
128 {
Bo Xu287e11a2014-06-30 09:49:21 -0700129 rotate = pPages->GetElement("Rotate")->GetDirect()? pPages->GetElement("Rotate")->GetDirect()->GetInteger() / 90 : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700130 break;
131 }
132 else if(pPages->KeyExist("Parent"))
133 pPages = (CPDF_Dictionary*)pPages->GetElement("Parent")->GetDirect();
134 else break;
135 }
136 }
137 }
138 }
139 else
140 {
141 return -1;
142 }
143
144 return rotate;
145}
146
147DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page, FPDF_PAGEOBJECT page_obj)
148{
Bo Xufdc00a72014-10-28 23:03:33 -0700149 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
Bo Xu287e11a2014-06-30 09:49:21 -0700150 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700151 || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
152 {
153 return;
154 }
155 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_obj;
156 if(pPageObj == NULL)
157 return;
158 FX_POSITION LastPersition = pPage->GetLastObjectPosition();
159
160 pPage->InsertObject(LastPersition, pPageObj);
161 switch(pPageObj->m_Type)
162 {
163 case FPDF_PAGEOBJ_PATH:
164 {
165 CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
166 pPathObj->CalcBoundingBox();
167 break;
168 }
169 case FPDF_PAGEOBJ_TEXT:
170 {
171 // CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
172 // pPathObj->CalcBoundingBox();
173 break;
174 }
175 case FPDF_PAGEOBJ_IMAGE:
176 {
177 CPDF_ImageObject* pImageObj = (CPDF_ImageObject*)pPageObj;
178 pImageObj->CalcBoundingBox();
179 break;
180 }
181 case FPDF_PAGEOBJ_SHADING:
182 {
183 CPDF_ShadingObject* pShadingObj = (CPDF_ShadingObject*)pPageObj;
184 pShadingObj->CalcBoundingBox();
185 break;
186 }
187 case FPDF_PAGEOBJ_FORM:
188 {
189 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
190 pFormObj->CalcBoundingBox();
191 break;
192 }
193 default:
194 break;
195 }
196
197 // pPage->ParseContent();
198 //pPage->GenerateContent();
199
200}
201
202DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page)
203{
Bo Xufdc00a72014-10-28 23:03:33 -0700204 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
Bo Xu287e11a2014-06-30 09:49:21 -0700205 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700206 || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
207 {
208 return -1;
209 }
210 return pPage->CountObjects();
211// return 0;
212}
213
214DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page, int index)
215{
Bo Xufdc00a72014-10-28 23:03:33 -0700216 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type")
218 || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
219 {
220 return NULL;
221 }
222 return pPage->GetObjectByIndex(index);
223// return NULL;
224}
225
226DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page)
227{
228 if(!page) return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700229 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
230 if (!pPage) return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700231
232 return pPage->BackgroundAlphaNeeded();
233}
234
235DLLEXPORT FPDF_BOOL STDCALL FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject)
236{
237 if(!pageObject) return FALSE;
238 CPDF_PageObject* pPageObj = (CPDF_PageObject*)pageObject;
239
240 const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;
241 int blend_type = pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;
242 if (blend_type != FXDIB_BLEND_NORMAL) return TRUE;
243
244 CPDF_Dictionary* pSMaskDict = pGeneralState ? (CPDF_Dictionary*)pGeneralState->m_pSoftMask : NULL;
245 if(pSMaskDict) return TRUE;
246
247 if(pGeneralState && pGeneralState->m_FillAlpha != 1.0f)
248 return TRUE;
249
250 if(pPageObj->m_Type == PDFPAGE_PATH)
251 {
252 if(pGeneralState && pGeneralState->m_StrokeAlpha != 1.0f)
253 return TRUE;
254 }
255
256 if(pPageObj->m_Type == PDFPAGE_FORM)
257 {
258 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
259 if(pFormObj->m_pForm && (pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED))
260 return TRUE;
261 if(pFormObj->m_pForm && (!(pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED) && (pFormObj->m_pForm->m_Transparency & PDFTRANS_GROUP)))
262 return TRUE;
263 }
264 return FALSE;
265}
266
267DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page)
268{
Bo Xufdc00a72014-10-28 23:03:33 -0700269 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
Bo Xu287e11a2014-06-30 09:49:21 -0700270 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271 || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
272 {
273 return FALSE;
274 }
275 CPDF_PageContentGenerate CG(pPage);
276 CG.GenerateContent();
277
278 return TRUE;
279}
280
281DLLEXPORT void STDCALL FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
282 double a, double b, double c, double d, double e, double f)
283{
284 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
285 if(pPageObj == NULL)
286 return;
287//PDF_ImageObject* pImageObj = FX_NEW CPDF_ImageObject;
288 CFX_AffineMatrix matrix((FX_FLOAT)a,(FX_FLOAT)b,(FX_FLOAT)c,(FX_FLOAT)d,(FX_FLOAT)e,(FX_FLOAT)f);
289 pPageObj->Transform(matrix);
290}
291DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
292 double a, double b, double c, double d, double e, double f)
293{
294 if(page == NULL)
295 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700296 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
297 if (!pPage) return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700298 CPDF_AnnotList AnnotList(pPage);
299 for (int i=0; i<AnnotList.Count();i++)
300 {
301 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
302 // transformAnnots Rectangle
303 CPDF_Rect rect;
304 pAnnot->GetRect(rect);
305 CFX_AffineMatrix matrix((FX_FLOAT)a,(FX_FLOAT)b,(FX_FLOAT)c,(FX_FLOAT)d,(FX_FLOAT)e,(FX_FLOAT)f);
306 rect.Transform(&matrix);
307 CPDF_Array *pRectArray = NULL;
308 pRectArray = pAnnot->m_pAnnotDict->GetArray("Rect");
309 if (!pRectArray) pRectArray=CPDF_Array::Create();
310 pRectArray->SetAt(0,FX_NEW CPDF_Number(rect.left));
311 pRectArray->SetAt(1,FX_NEW CPDF_Number(rect.bottom));
312 pRectArray->SetAt(2,FX_NEW CPDF_Number(rect.right));
313 pRectArray->SetAt(3,FX_NEW CPDF_Number(rect.top));
314 pAnnot->m_pAnnotDict->SetAt("Rect",pRectArray);
315
316 //Transform AP's rectangle
317 //To Do
318
319 }
320
321}
Bo Xu394010d2014-06-12 13:41:50 -0700322
323DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate)
324{
Bo Xufdc00a72014-10-28 23:03:33 -0700325 if (page == NULL)
326 return;
327 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
328 if (!pPage) return;
329
Bo Xu287e11a2014-06-30 09:49:21 -0700330 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
Bo Xu394010d2014-06-12 13:41:50 -0700331 || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
332 {
333 return;
334 }
335 CPDF_Dictionary* pDict = pPage->m_pFormDict;
336 rotate %=4;
337
338 pDict->SetAt("Rotate", FX_NEW CPDF_Number(rotate * 90));
Nico Weber0ce77e32014-07-16 13:19:08 -0700339}