Merge to XFA: Create top-level public/ header directory.
[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 = FPDFXFA_GetApp();
53         CPDFXFA_Document* document = FX_NEW CPDFXFA_Document(pDoc, pApp);
54
55         return document;
56 }
57
58 DLLEXPORT void STDCALL FPDFPage_Delete(FPDF_DOCUMENT document, int page_index)
59 {
60         CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
61         if (pDoc == NULL) 
62                 return;
63         if (page_index < 0 || page_index >= pDoc->GetPageCount()) 
64                 return;
65
66         pDoc->DeletePage(page_index);
67 }
68
69 DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document, int page_index, double width, double height)
70 {
71         if (!document)
72                 return NULL;
73
74 //      CPDF_Parser* pParser = (CPDF_Parser*)document;
75         CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
76         if(page_index < 0)
77                 page_index = 0;
78         if(pDoc->GetPageCount()<page_index)
79                 page_index = pDoc->GetPageCount();
80 //      if (page_index < 0 || page_index >= pDoc->GetPageCount()) 
81 //              return NULL;
82
83         CPDF_Dictionary* pPageDict = pDoc->CreateNewPage(page_index);
84         if(!pPageDict)
85                 return NULL;
86         CPDF_Array* pMediaBoxArray = FX_NEW CPDF_Array;
87         pMediaBoxArray->Add(FX_NEW CPDF_Number(0));
88         pMediaBoxArray->Add(FX_NEW CPDF_Number(0));
89         pMediaBoxArray->Add(FX_NEW CPDF_Number(FX_FLOAT(width)));
90         pMediaBoxArray->Add(FX_NEW CPDF_Number(FX_FLOAT(height)));
91
92         pPageDict->SetAt("MediaBox", pMediaBoxArray);
93         pPageDict->SetAt("Rotate", FX_NEW CPDF_Number(0));
94         pPageDict->SetAt("Resources", FX_NEW CPDF_Dictionary);
95
96 //      CPDF_Page* pPage = FX_NEW CPDF_Page;
97 //      pPage->Load(pDoc,pPageDict);
98 //      pPage->ParseContent();
99
100         CPDFXFA_Page* pPage = FX_NEW CPDFXFA_Page((CPDFXFA_Document*)document, page_index);
101         pPage->LoadPDFPage(pPageDict);
102
103         return pPage;
104 }
105
106 DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page)
107 {
108         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
109         if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
110                 || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
111         {
112                 return -1;
113         }
114         CPDF_Dictionary* pDict = pPage->m_pFormDict;
115
116         int rotate = 0;
117         if(pDict != NULL)
118         {
119                 if (pDict->KeyExist("Rotate"))
120                         rotate = pDict->GetElement("Rotate")->GetDirect()? pDict->GetElement("Rotate")->GetDirect()->GetInteger() / 90 : 0;
121                 else
122                 {
123                         if(pDict->KeyExist("Parent"))
124                         {
125                                 CPDF_Dictionary* pPages = (CPDF_Dictionary*)pDict->GetElement("Parent")->GetDirect();
126                                 while(pPages)
127                                 {
128                                         if(pPages->KeyExist("Rotate"))
129                                         {
130                                                 rotate = pPages->GetElement("Rotate")->GetDirect()? pPages->GetElement("Rotate")->GetDirect()->GetInteger() / 90 : 0;
131                                                 break;
132                                         }
133                                         else if(pPages->KeyExist("Parent"))
134                                                 pPages = (CPDF_Dictionary*)pPages->GetElement("Parent")->GetDirect();
135                                         else break;
136                                 }
137                         }
138                 }
139         }
140         else
141         {
142                 return -1;
143         }
144         
145         return rotate;
146 }
147
148 DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page, FPDF_PAGEOBJECT page_obj)
149 {
150         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
151         if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
152                 || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
153         {
154                 return;
155         }
156         CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_obj;
157         if(pPageObj == NULL)
158                 return;
159         FX_POSITION LastPersition = pPage->GetLastObjectPosition();
160
161         pPage->InsertObject(LastPersition, pPageObj);
162         switch(pPageObj->m_Type)
163         {
164         case FPDF_PAGEOBJ_PATH:
165                 {
166                         CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
167                         pPathObj->CalcBoundingBox();
168                         break;
169                 }
170         case FPDF_PAGEOBJ_TEXT:
171                 {
172                         //      CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
173                         //      pPathObj->CalcBoundingBox();
174                         break;
175                 }
176         case FPDF_PAGEOBJ_IMAGE:
177                 {
178                         CPDF_ImageObject* pImageObj = (CPDF_ImageObject*)pPageObj;
179                         pImageObj->CalcBoundingBox();
180                         break;
181                 }
182         case FPDF_PAGEOBJ_SHADING:
183                 {
184                         CPDF_ShadingObject* pShadingObj = (CPDF_ShadingObject*)pPageObj;
185                         pShadingObj->CalcBoundingBox();
186                         break;
187                 }
188         case FPDF_PAGEOBJ_FORM:
189                 {
190                         CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
191                         pFormObj->CalcBoundingBox();
192                         break;
193                 }
194         default:
195                 break;
196         }
197
198         //      pPage->ParseContent();
199         //pPage->GenerateContent();
200
201 }
202
203 DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page)
204 {
205         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
206         if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
207                 || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
208         {
209                 return -1;
210         }
211         return pPage->CountObjects();
212 //      return 0;
213 }
214
215 DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page, int index)
216 {
217         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
218         if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type")
219                 || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
220         {
221                 return NULL;
222         }
223         return pPage->GetObjectByIndex(index);
224 //      return NULL;
225 }
226
227 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page)
228 {
229         if(!page) return FALSE;
230         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
231         if (!pPage) return FALSE;
232
233         return pPage->BackgroundAlphaNeeded();
234 }
235
236 DLLEXPORT FPDF_BOOL STDCALL FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject)
237 {
238         if(!pageObject) return FALSE;
239         CPDF_PageObject* pPageObj = (CPDF_PageObject*)pageObject;
240
241         const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;
242         int blend_type = pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;
243         if (blend_type != FXDIB_BLEND_NORMAL) return TRUE;
244
245         CPDF_Dictionary* pSMaskDict = pGeneralState ? (CPDF_Dictionary*)pGeneralState->m_pSoftMask : NULL;
246         if(pSMaskDict) return TRUE;
247
248         if(pGeneralState && pGeneralState->m_FillAlpha != 1.0f)
249                 return TRUE;
250
251         if(pPageObj->m_Type == PDFPAGE_PATH)
252         {
253                 if(pGeneralState && pGeneralState->m_StrokeAlpha != 1.0f)
254                         return TRUE;
255         }
256
257         if(pPageObj->m_Type == PDFPAGE_FORM)
258         {
259                 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
260                 if(pFormObj->m_pForm && (pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED))
261                         return TRUE;
262                 if(pFormObj->m_pForm && (!(pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED) && (pFormObj->m_pForm->m_Transparency & PDFTRANS_GROUP)))
263                         return TRUE;
264         }
265         return FALSE;
266 }
267
268 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page)
269 {
270         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
271         if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
272                 || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
273         {
274                 return FALSE;
275         }
276         CPDF_PageContentGenerate CG(pPage);
277         CG.GenerateContent();
278
279         return TRUE;
280 }
281
282 DLLEXPORT void STDCALL FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
283                          double a, double b, double c, double d, double e, double f)  
284 {
285         CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
286         if(pPageObj == NULL)
287                 return;
288 //PDF_ImageObject* pImageObj = FX_NEW CPDF_ImageObject;
289         CFX_AffineMatrix matrix((FX_FLOAT)a,(FX_FLOAT)b,(FX_FLOAT)c,(FX_FLOAT)d,(FX_FLOAT)e,(FX_FLOAT)f);
290         pPageObj->Transform(matrix);
291 }
292 DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
293                                                                                            double a, double b, double c, double d, double e, double f)
294 {
295         if(page == NULL)
296                 return;
297         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
298         if (!pPage) return;
299         CPDF_AnnotList AnnotList(pPage);
300         for (int i=0; i<AnnotList.Count();i++)
301         {
302                 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
303                 // transformAnnots Rectangle
304                 CPDF_Rect rect;
305                 pAnnot->GetRect(rect);
306                 CFX_AffineMatrix matrix((FX_FLOAT)a,(FX_FLOAT)b,(FX_FLOAT)c,(FX_FLOAT)d,(FX_FLOAT)e,(FX_FLOAT)f);
307                 rect.Transform(&matrix);
308                 CPDF_Array *pRectArray = NULL;
309                 pRectArray = pAnnot->m_pAnnotDict->GetArray("Rect");
310                 if (!pRectArray) pRectArray=CPDF_Array::Create();
311                 pRectArray->SetAt(0,FX_NEW CPDF_Number(rect.left));
312                 pRectArray->SetAt(1,FX_NEW CPDF_Number(rect.bottom));
313                 pRectArray->SetAt(2,FX_NEW CPDF_Number(rect.right));
314                 pRectArray->SetAt(3,FX_NEW CPDF_Number(rect.top));
315                 pAnnot->m_pAnnotDict->SetAt("Rect",pRectArray);
316
317                 //Transform AP's rectangle
318                 //To Do
319
320         }
321
322 }
323
324 DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate)
325 {
326         if (page == NULL)
327                 return;
328         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
329         if (!pPage) return;
330
331         if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
332                 || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
333         {
334                 return;
335         }
336         CPDF_Dictionary* pDict = pPage->m_pFormDict;
337         rotate %=4;
338
339         pDict->SetAt("Rotate", FX_NEW CPDF_Number(rotate * 90));
340 }