blob: 42ab3eb69431f649323d74ce3da2daf930adcb29 [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) {
Tom Sepezdb0be962015-10-16 14:00:21 -070092 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 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 =
Dan Sinclairf1251c12015-10-20 16:24:45 -0400110 ToDictionary(pDict->GetElement("Parent")->GetDirect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 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"))
Dan Sinclairf1251c12015-10-20 16:24:45 -0400120 pPages = ToDictionary(pPages->GetElement("Parent")->GetDirect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121 else
122 break;
123 }
124 }
125 }
126 } else {
127 return -1;
128 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 return rotate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700131}
132
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page,
134 FPDF_PAGEOBJECT page_obj) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700135 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
137 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
138 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
139 "Page")) {
140 return;
141 }
142 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_obj;
143 if (pPageObj == NULL)
144 return;
145 FX_POSITION LastPersition = pPage->GetLastObjectPosition();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 pPage->InsertObject(LastPersition, pPageObj);
148 switch (pPageObj->m_Type) {
149 case FPDF_PAGEOBJ_PATH: {
150 CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
151 pPathObj->CalcBoundingBox();
152 break;
153 }
154 case FPDF_PAGEOBJ_TEXT: {
155 // CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
156 // pPathObj->CalcBoundingBox();
157 break;
158 }
159 case FPDF_PAGEOBJ_IMAGE: {
160 CPDF_ImageObject* pImageObj = (CPDF_ImageObject*)pPageObj;
161 pImageObj->CalcBoundingBox();
162 break;
163 }
164 case FPDF_PAGEOBJ_SHADING: {
165 CPDF_ShadingObject* pShadingObj = (CPDF_ShadingObject*)pPageObj;
166 pShadingObj->CalcBoundingBox();
167 break;
168 }
169 case FPDF_PAGEOBJ_FORM: {
170 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
171 pFormObj->CalcBoundingBox();
172 break;
173 }
174 default:
175 break;
176 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700177}
178
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
180 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
181 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
182 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
183 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
184 "Page")) {
185 return -1;
186 }
187 return pPage->CountObjects();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700188}
189
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page,
191 int index) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700192 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
194 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
195 "Page")) {
196 return NULL;
197 }
198 return pPage->GetObjectByIndex(index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199}
200
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700202 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
203 return pPage && pPage->BackgroundAlphaNeeded();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700204}
205
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206DLLEXPORT FPDF_BOOL STDCALL
207FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
208 if (!pageObject)
209 return FALSE;
210 CPDF_PageObject* pPageObj = (CPDF_PageObject*)pageObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700211
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;
213 int blend_type =
214 pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;
215 if (blend_type != FXDIB_BLEND_NORMAL)
216 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 CPDF_Dictionary* pSMaskDict =
Dan Sinclairf1251c12015-10-20 16:24:45 -0400219 pGeneralState ? ToDictionary(pGeneralState->m_pSoftMask) : NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 if (pSMaskDict)
221 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700222
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 if (pGeneralState && pGeneralState->m_FillAlpha != 1.0f)
224 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700225
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 if (pPageObj->m_Type == PDFPAGE_PATH) {
227 if (pGeneralState && pGeneralState->m_StrokeAlpha != 1.0f)
228 return TRUE;
229 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 if (pPageObj->m_Type == PDFPAGE_FORM) {
232 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
233 if (pFormObj->m_pForm &&
234 (pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED))
235 return TRUE;
236 if (pFormObj->m_pForm &&
237 (!(pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED) &&
238 (pFormObj->m_pForm->m_Transparency & PDFTRANS_GROUP)))
239 return TRUE;
240 }
241 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700242}
243
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700245 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
247 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
248 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
249 "Page")) {
250 return FALSE;
251 }
252 CPDF_PageContentGenerate CG(pPage);
253 CG.GenerateContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700254
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256}
257
258DLLEXPORT void STDCALL FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 double a,
260 double b,
261 double c,
262 double d,
263 double e,
264 double f) {
265 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
266 if (pPageObj == NULL)
267 return;
Lei Zhangcb78ef52015-10-02 10:10:49 -0700268
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
270 (FX_FLOAT)e, (FX_FLOAT)f);
271 pPageObj->Transform(matrix);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272}
273DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 double a,
275 double b,
276 double c,
277 double d,
278 double e,
279 double f) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700280 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 if (!pPage)
282 return;
283 CPDF_AnnotList AnnotList(pPage);
284 for (int i = 0; i < AnnotList.Count(); i++) {
285 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
286 // transformAnnots Rectangle
287 CPDF_Rect rect;
288 pAnnot->GetRect(rect);
289 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
290 (FX_FLOAT)e, (FX_FLOAT)f);
291 rect.Transform(&matrix);
292 CPDF_Array* pRectArray = NULL;
293 pRectArray = pAnnot->GetAnnotDict()->GetArray("Rect");
294 if (!pRectArray)
295 pRectArray = CPDF_Array::Create();
296 pRectArray->SetAt(0, new CPDF_Number(rect.left));
297 pRectArray->SetAt(1, new CPDF_Number(rect.bottom));
298 pRectArray->SetAt(2, new CPDF_Number(rect.right));
299 pRectArray->SetAt(3, new CPDF_Number(rect.top));
300 pAnnot->GetAnnotDict()->SetAt("Rect", pRectArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700301
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302 // Transform AP's rectangle
303 // To Do
304 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700305}
Bo Xu394010d2014-06-12 13:41:50 -0700306
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700308 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
310 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
311 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
312 "Page")) {
313 return;
314 }
315 CPDF_Dictionary* pDict = pPage->m_pFormDict;
316 rotate %= 4;
Bo Xu394010d2014-06-12 13:41:50 -0700317
Tom Sepezae51c812015-08-05 12:34:06 -0700318 pDict->SetAt("Rotate", new CPDF_Number(rotate * 90));
Nico Weber0ce77e32014-07-16 13:19:08 -0700319}