blob: 145d8ca5c3a6a2199b6c517f53c7e6d753ff2f4b [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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// 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
Nico Weber9d8ec5a2015-08-04 13:00:21 -070020DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_CreateNewDocument() {
Tom Sepezae51c812015-08-05 12:34:06 -070021 CPDF_Document* pDoc = new CPDF_Document;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022 pDoc->CreateNewDoc();
23 time_t currentTime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025 CFX_ByteString DateStr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) {
28 if (-1 != time(&currentTime)) {
29 tm* pTM = localtime(&currentTime);
30 if (pTM) {
31 DateStr.Format("D:%04d%02d%02d%02d%02d%02d", pTM->tm_year + 1900,
32 pTM->tm_mon + 1, pTM->tm_mday, pTM->tm_hour, pTM->tm_min,
33 pTM->tm_sec);
34 }
35 }
36 }
Tom Sepezbdeeb8a2015-05-27 12:25:00 -070037
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038 CPDF_Dictionary* pInfoDict = NULL;
39 pInfoDict = pDoc->GetInfo();
40 if (pInfoDict) {
41 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
42 pInfoDict->SetAt("CreationDate", new CPDF_String(DateStr));
Tom Sepezae51c812015-08-05 12:34:06 -070043 pInfoDict->SetAt("Creator", new CPDF_String(L"PDFium"));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
Lei Zhangcb78ef52015-10-02 10:10:49 -070047 return new CPDFXFA_Document(pDoc, pApp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048}
49
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050DLLEXPORT void STDCALL FPDFPage_Delete(FPDF_DOCUMENT document, int page_index) {
51 CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
52 if (pDoc == NULL)
53 return;
54 if (page_index < 0 || page_index >= pDoc->GetPageCount())
55 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 pDoc->DeletePage(page_index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070058}
59
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document,
61 int page_index,
62 double width,
63 double height) {
64 if (!document)
65 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 // CPDF_Parser* pParser = (CPDF_Parser*)document;
68 CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
69 if (page_index < 0)
70 page_index = 0;
71 if (pDoc->GetPageCount() < page_index)
72 page_index = pDoc->GetPageCount();
73 // if (page_index < 0 || page_index >= pDoc->GetPageCount())
74 // return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 CPDF_Dictionary* pPageDict = pDoc->CreateNewPage(page_index);
77 if (!pPageDict)
78 return NULL;
Tom Sepezae51c812015-08-05 12:34:06 -070079 CPDF_Array* pMediaBoxArray = new CPDF_Array;
80 pMediaBoxArray->Add(new CPDF_Number(0));
81 pMediaBoxArray->Add(new CPDF_Number(0));
82 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(width)));
83 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(height)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 pPageDict->SetAt("MediaBox", pMediaBoxArray);
Tom Sepezae51c812015-08-05 12:34:06 -070086 pPageDict->SetAt("Rotate", new CPDF_Number(0));
87 pPageDict->SetAt("Resources", new CPDF_Dictionary);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 CPDFXFA_Page* pPage =
Tom Sepezae51c812015-08-05 12:34:06 -070090 new CPDFXFA_Page((CPDFXFA_Document*)document, page_index);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 pPage->LoadPDFPage(pPageDict);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 return pPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094}
95
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page) {
97 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
98 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
99 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
100 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
101 "Page")) {
102 return -1;
103 }
104 CPDF_Dictionary* pDict = pPage->m_pFormDict;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700105
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 int rotate = 0;
107 if (pDict != NULL) {
108 if (pDict->KeyExist("Rotate"))
109 rotate = pDict->GetElement("Rotate")->GetDirect()
110 ? pDict->GetElement("Rotate")->GetDirect()->GetInteger() / 90
111 : 0;
112 else {
113 if (pDict->KeyExist("Parent")) {
114 CPDF_Dictionary* pPages =
115 (CPDF_Dictionary*)pDict->GetElement("Parent")->GetDirect();
116 while (pPages) {
117 if (pPages->KeyExist("Rotate")) {
118 rotate =
119 pPages->GetElement("Rotate")->GetDirect()
120 ? pPages->GetElement("Rotate")->GetDirect()->GetInteger() /
121 90
122 : 0;
123 break;
124 } else if (pPages->KeyExist("Parent"))
125 pPages =
126 (CPDF_Dictionary*)pPages->GetElement("Parent")->GetDirect();
127 else
128 break;
129 }
130 }
131 }
132 } else {
133 return -1;
134 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 return rotate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137}
138
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page,
140 FPDF_PAGEOBJECT page_obj) {
141 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
142 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
143 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
144 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
145 "Page")) {
146 return;
147 }
148 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_obj;
149 if (pPageObj == NULL)
150 return;
151 FX_POSITION LastPersition = pPage->GetLastObjectPosition();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 pPage->InsertObject(LastPersition, pPageObj);
154 switch (pPageObj->m_Type) {
155 case FPDF_PAGEOBJ_PATH: {
156 CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
157 pPathObj->CalcBoundingBox();
158 break;
159 }
160 case FPDF_PAGEOBJ_TEXT: {
161 // CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
162 // pPathObj->CalcBoundingBox();
163 break;
164 }
165 case FPDF_PAGEOBJ_IMAGE: {
166 CPDF_ImageObject* pImageObj = (CPDF_ImageObject*)pPageObj;
167 pImageObj->CalcBoundingBox();
168 break;
169 }
170 case FPDF_PAGEOBJ_SHADING: {
171 CPDF_ShadingObject* pShadingObj = (CPDF_ShadingObject*)pPageObj;
172 pShadingObj->CalcBoundingBox();
173 break;
174 }
175 case FPDF_PAGEOBJ_FORM: {
176 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
177 pFormObj->CalcBoundingBox();
178 break;
179 }
180 default:
181 break;
182 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 // pPage->ParseContent();
185 // pPage->GenerateContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700186}
187
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
189 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
190 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
191 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
192 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
193 "Page")) {
194 return -1;
195 }
196 return pPage->CountObjects();
197 // return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198}
199
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page,
201 int index) {
202 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
203 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
204 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
205 "Page")) {
206 return NULL;
207 }
208 return pPage->GetObjectByIndex(index);
209 // return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700210}
211
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page) {
213 if (!page)
214 return FALSE;
215 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
216 if (!pPage)
217 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700218
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 return pPage->BackgroundAlphaNeeded();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700220}
221
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222DLLEXPORT FPDF_BOOL STDCALL
223FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
224 if (!pageObject)
225 return FALSE;
226 CPDF_PageObject* pPageObj = (CPDF_PageObject*)pageObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700227
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;
229 int blend_type =
230 pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;
231 if (blend_type != FXDIB_BLEND_NORMAL)
232 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 CPDF_Dictionary* pSMaskDict =
235 pGeneralState ? (CPDF_Dictionary*)pGeneralState->m_pSoftMask : NULL;
236 if (pSMaskDict)
237 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700238
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239 if (pGeneralState && pGeneralState->m_FillAlpha != 1.0f)
240 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700241
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 if (pPageObj->m_Type == PDFPAGE_PATH) {
243 if (pGeneralState && pGeneralState->m_StrokeAlpha != 1.0f)
244 return TRUE;
245 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 if (pPageObj->m_Type == PDFPAGE_FORM) {
248 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
249 if (pFormObj->m_pForm &&
250 (pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED))
251 return TRUE;
252 if (pFormObj->m_pForm &&
253 (!(pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED) &&
254 (pFormObj->m_pForm->m_Transparency & PDFTRANS_GROUP)))
255 return TRUE;
256 }
257 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258}
259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page) {
261 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
262 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
263 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
264 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
265 "Page")) {
266 return FALSE;
267 }
268 CPDF_PageContentGenerate CG(pPage);
269 CG.GenerateContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700270
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272}
273
274DLLEXPORT void STDCALL FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275 double a,
276 double b,
277 double c,
278 double d,
279 double e,
280 double f) {
281 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
282 if (pPageObj == NULL)
283 return;
Lei Zhangcb78ef52015-10-02 10:10:49 -0700284
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
286 (FX_FLOAT)e, (FX_FLOAT)f);
287 pPageObj->Transform(matrix);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700288}
289DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 double a,
291 double b,
292 double c,
293 double d,
294 double e,
295 double f) {
296 if (page == NULL)
297 return;
298 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
299 if (!pPage)
300 return;
301 CPDF_AnnotList AnnotList(pPage);
302 for (int i = 0; i < AnnotList.Count(); i++) {
303 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
304 // transformAnnots Rectangle
305 CPDF_Rect rect;
306 pAnnot->GetRect(rect);
307 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
308 (FX_FLOAT)e, (FX_FLOAT)f);
309 rect.Transform(&matrix);
310 CPDF_Array* pRectArray = NULL;
311 pRectArray = pAnnot->GetAnnotDict()->GetArray("Rect");
312 if (!pRectArray)
313 pRectArray = CPDF_Array::Create();
314 pRectArray->SetAt(0, new CPDF_Number(rect.left));
315 pRectArray->SetAt(1, new CPDF_Number(rect.bottom));
316 pRectArray->SetAt(2, new CPDF_Number(rect.right));
317 pRectArray->SetAt(3, new CPDF_Number(rect.top));
318 pAnnot->GetAnnotDict()->SetAt("Rect", pRectArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700319
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 // Transform AP's rectangle
321 // To Do
322 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700323}
Bo Xu394010d2014-06-12 13:41:50 -0700324
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate) {
326 if (page == NULL)
327 return;
328 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
329 if (!pPage)
330 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700331
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
333 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
334 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
335 "Page")) {
336 return;
337 }
338 CPDF_Dictionary* pDict = pPage->m_pFormDict;
339 rotate %= 4;
Bo Xu394010d2014-06-12 13:41:50 -0700340
Tom Sepezae51c812015-08-05 12:34:06 -0700341 pDict->SetAt("Rotate", new CPDF_Number(rotate * 90));
Nico Weber0ce77e32014-07-16 13:19:08 -0700342}