blob: e7bbf05f4a024ecd5b89a64dbcc628949a89f795 [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) {
Tom Sepez471a1032015-10-15 16:17:18 -070051 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
52 if (!pDoc || page_index < 0 || page_index >= pDoc->GetPageCount())
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070054
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 pDoc->DeletePage(page_index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056}
57
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document,
59 int page_index,
60 double width,
61 double height) {
Tom Sepez471a1032015-10-15 16:17:18 -070062 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
63 if (!pDoc)
64 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 if (page_index < 0)
67 page_index = 0;
68 if (pDoc->GetPageCount() < page_index)
69 page_index = pDoc->GetPageCount();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 CPDF_Dictionary* pPageDict = pDoc->CreateNewPage(page_index);
72 if (!pPageDict)
73 return NULL;
Tom Sepezae51c812015-08-05 12:34:06 -070074 CPDF_Array* pMediaBoxArray = new CPDF_Array;
75 pMediaBoxArray->Add(new CPDF_Number(0));
76 pMediaBoxArray->Add(new CPDF_Number(0));
77 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(width)));
78 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(height)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 pPageDict->SetAt("MediaBox", pMediaBoxArray);
Tom Sepezae51c812015-08-05 12:34:06 -070081 pPageDict->SetAt("Rotate", new CPDF_Number(0));
82 pPageDict->SetAt("Resources", new CPDF_Dictionary);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070083
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 CPDFXFA_Page* pPage =
Tom Sepezae51c812015-08-05 12:34:06 -070085 new CPDFXFA_Page((CPDFXFA_Document*)document, page_index);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 pPage->LoadPDFPage(pPageDict);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 return pPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089}
90
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page) {
92 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
93 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
94 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
95 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
96 "Page")) {
97 return -1;
98 }
99 CPDF_Dictionary* pDict = pPage->m_pFormDict;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 int rotate = 0;
102 if (pDict != NULL) {
103 if (pDict->KeyExist("Rotate"))
104 rotate = pDict->GetElement("Rotate")->GetDirect()
105 ? pDict->GetElement("Rotate")->GetDirect()->GetInteger() / 90
106 : 0;
107 else {
108 if (pDict->KeyExist("Parent")) {
109 CPDF_Dictionary* pPages =
110 (CPDF_Dictionary*)pDict->GetElement("Parent")->GetDirect();
111 while (pPages) {
112 if (pPages->KeyExist("Rotate")) {
113 rotate =
114 pPages->GetElement("Rotate")->GetDirect()
115 ? pPages->GetElement("Rotate")->GetDirect()->GetInteger() /
116 90
117 : 0;
118 break;
119 } else if (pPages->KeyExist("Parent"))
120 pPages =
121 (CPDF_Dictionary*)pPages->GetElement("Parent")->GetDirect();
122 else
123 break;
124 }
125 }
126 }
127 } else {
128 return -1;
129 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 return rotate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132}
133
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page,
135 FPDF_PAGEOBJECT page_obj) {
136 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
137 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
138 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
139 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
140 "Page")) {
141 return;
142 }
143 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_obj;
144 if (pPageObj == NULL)
145 return;
146 FX_POSITION LastPersition = pPage->GetLastObjectPosition();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700147
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148 pPage->InsertObject(LastPersition, pPageObj);
149 switch (pPageObj->m_Type) {
150 case FPDF_PAGEOBJ_PATH: {
151 CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
152 pPathObj->CalcBoundingBox();
153 break;
154 }
155 case FPDF_PAGEOBJ_TEXT: {
156 // CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
157 // pPathObj->CalcBoundingBox();
158 break;
159 }
160 case FPDF_PAGEOBJ_IMAGE: {
161 CPDF_ImageObject* pImageObj = (CPDF_ImageObject*)pPageObj;
162 pImageObj->CalcBoundingBox();
163 break;
164 }
165 case FPDF_PAGEOBJ_SHADING: {
166 CPDF_ShadingObject* pShadingObj = (CPDF_ShadingObject*)pPageObj;
167 pShadingObj->CalcBoundingBox();
168 break;
169 }
170 case FPDF_PAGEOBJ_FORM: {
171 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
172 pFormObj->CalcBoundingBox();
173 break;
174 }
175 default:
176 break;
177 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179 // pPage->ParseContent();
180 // pPage->GenerateContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700181}
182
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
184 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
185 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
186 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
187 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
188 "Page")) {
189 return -1;
190 }
191 return pPage->CountObjects();
192 // return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700193}
194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page,
196 int index) {
197 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
198 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
199 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
200 "Page")) {
201 return NULL;
202 }
203 return pPage->GetObjectByIndex(index);
204 // return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700205}
206
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page) {
208 if (!page)
209 return FALSE;
210 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
211 if (!pPage)
212 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700213
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 return pPage->BackgroundAlphaNeeded();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700215}
216
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217DLLEXPORT FPDF_BOOL STDCALL
218FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
219 if (!pageObject)
220 return FALSE;
221 CPDF_PageObject* pPageObj = (CPDF_PageObject*)pageObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700222
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;
224 int blend_type =
225 pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;
226 if (blend_type != FXDIB_BLEND_NORMAL)
227 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700228
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 CPDF_Dictionary* pSMaskDict =
230 pGeneralState ? (CPDF_Dictionary*)pGeneralState->m_pSoftMask : NULL;
231 if (pSMaskDict)
232 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 if (pGeneralState && pGeneralState->m_FillAlpha != 1.0f)
235 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700236
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 if (pPageObj->m_Type == PDFPAGE_PATH) {
238 if (pGeneralState && pGeneralState->m_StrokeAlpha != 1.0f)
239 return TRUE;
240 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700241
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 if (pPageObj->m_Type == PDFPAGE_FORM) {
243 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
244 if (pFormObj->m_pForm &&
245 (pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED))
246 return TRUE;
247 if (pFormObj->m_pForm &&
248 (!(pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED) &&
249 (pFormObj->m_pForm->m_Transparency & PDFTRANS_GROUP)))
250 return TRUE;
251 }
252 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700253}
254
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page) {
256 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
257 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
258 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
259 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
260 "Page")) {
261 return FALSE;
262 }
263 CPDF_PageContentGenerate CG(pPage);
264 CG.GenerateContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700267}
268
269DLLEXPORT void STDCALL FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 double a,
271 double b,
272 double c,
273 double d,
274 double e,
275 double f) {
276 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
277 if (pPageObj == NULL)
278 return;
Lei Zhangcb78ef52015-10-02 10:10:49 -0700279
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
281 (FX_FLOAT)e, (FX_FLOAT)f);
282 pPageObj->Transform(matrix);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283}
284DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 double a,
286 double b,
287 double c,
288 double d,
289 double e,
290 double f) {
291 if (page == NULL)
292 return;
293 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
294 if (!pPage)
295 return;
296 CPDF_AnnotList AnnotList(pPage);
297 for (int i = 0; i < AnnotList.Count(); i++) {
298 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
299 // transformAnnots Rectangle
300 CPDF_Rect rect;
301 pAnnot->GetRect(rect);
302 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
303 (FX_FLOAT)e, (FX_FLOAT)f);
304 rect.Transform(&matrix);
305 CPDF_Array* pRectArray = NULL;
306 pRectArray = pAnnot->GetAnnotDict()->GetArray("Rect");
307 if (!pRectArray)
308 pRectArray = CPDF_Array::Create();
309 pRectArray->SetAt(0, new CPDF_Number(rect.left));
310 pRectArray->SetAt(1, new CPDF_Number(rect.bottom));
311 pRectArray->SetAt(2, new CPDF_Number(rect.right));
312 pRectArray->SetAt(3, new CPDF_Number(rect.top));
313 pAnnot->GetAnnotDict()->SetAt("Rect", pRectArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700314
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 // Transform AP's rectangle
316 // To Do
317 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700318}
Bo Xu394010d2014-06-12 13:41:50 -0700319
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate) {
321 if (page == NULL)
322 return;
323 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
324 if (!pPage)
325 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700326
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
328 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
329 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
330 "Page")) {
331 return;
332 }
333 CPDF_Dictionary* pDict = pPage->m_pFormDict;
334 rotate %= 4;
Bo Xu394010d2014-06-12 13:41:50 -0700335
Tom Sepezae51c812015-08-05 12:34:06 -0700336 pDict->SetAt("Rotate", new CPDF_Number(rotate * 90));
Nico Weber0ce77e32014-07-16 13:19:08 -0700337}