2fdc7de3f581df3ead3027bf3fe82f500d7c08a1
[pdfium.git] / fpdfsdk / src / fpdfeditpage.cpp
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 #include "../../public/fpdf_edit.h"
8 #include "../../public/fpdf_formfill.h"
9 #include "../include/fsdk_define.h"
10 #include "../include/fpdfxfa/fpdfxfa_doc.h"
11 #include "../include/fpdfxfa/fpdfxfa_app.h"
12 #include "../include/fpdfxfa/fpdfxfa_page.h"
13
14 #if _FX_OS_ == _FX_ANDROID_
15 #include "time.h"
16 #else
17 #include <ctime>
18 #endif
19
20 DLLEXPORT 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         }
42
43         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));
49                 pInfoDict->SetAt("Creator",FX_NEW CPDF_String(L"PDFium"));
50         }
51
52         CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
53         CPDFXFA_Document* document = FX_NEW CPDFXFA_Document(pDoc, pApp);
54         return document;
55 }
56
57 DLLEXPORT void STDCALL FPDFPage_Delete(FPDF_DOCUMENT document, int page_index)
58 {
59         CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
60         if (pDoc == NULL) 
61                 return;
62         if (page_index < 0 || page_index >= pDoc->GetPageCount()) 
63                 return;
64
65         pDoc->DeletePage(page_index);
66 }
67
68 DLLEXPORT 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;
74         CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
75         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
95 //      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);
101
102         return pPage;
103 }
104
105 DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page)
106 {
107         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
108         if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
109                 || 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         {
118                 if (pDict->KeyExist("Rotate"))
119                         rotate = pDict->GetElement("Rotate")->GetDirect()? pDict->GetElement("Rotate")->GetDirect()->GetInteger() / 90 : 0;
120                 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                                         {
129                                                 rotate = pPages->GetElement("Rotate")->GetDirect()? pPages->GetElement("Rotate")->GetDirect()->GetInteger() / 90 : 0;
130                                                 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
147 DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page, FPDF_PAGEOBJECT page_obj)
148 {
149         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
150         if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
151                 || 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
202 DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page)
203 {
204         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
205         if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
206                 || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
207         {
208                 return -1;
209         }
210         return pPage->CountObjects();
211 //      return 0;
212 }
213
214 DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page, int index)
215 {
216         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
217         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
226 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page)
227 {
228         if(!page) return FALSE;
229         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
230         if (!pPage) return FALSE;
231
232         return pPage->BackgroundAlphaNeeded();
233 }
234
235 DLLEXPORT 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
267 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page)
268 {
269         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
270         if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
271                 || 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
281 DLLEXPORT 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 }
291 DLLEXPORT 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;
296         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
297         if (!pPage) return;
298         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 }
322
323 DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate)
324 {
325         if (page == NULL)
326                 return;
327         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
328         if (!pPage) return;
329
330         if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
331                 || 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));
339 }