blob: 8b4168fbf4d3a7aeabbbfba36134935d21e06e67 [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
Tom Sepezbf59a072015-10-21 14:07:23 -070046 return FPDFDocumentFromCPDFDocument(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047}
48
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049DLLEXPORT void STDCALL FPDFPage_Delete(FPDF_DOCUMENT document, int page_index) {
Tom Sepez471a1032015-10-15 16:17:18 -070050 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
51 if (!pDoc || page_index < 0 || page_index >= pDoc->GetPageCount())
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070053
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 pDoc->DeletePage(page_index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055}
56
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document,
58 int page_index,
59 double width,
60 double height) {
Tom Sepez471a1032015-10-15 16:17:18 -070061 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
62 if (!pDoc)
63 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 if (page_index < 0)
66 page_index = 0;
67 if (pDoc->GetPageCount() < page_index)
68 page_index = pDoc->GetPageCount();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070069
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 CPDF_Dictionary* pPageDict = pDoc->CreateNewPage(page_index);
71 if (!pPageDict)
72 return NULL;
Tom Sepezae51c812015-08-05 12:34:06 -070073 CPDF_Array* pMediaBoxArray = new CPDF_Array;
74 pMediaBoxArray->Add(new CPDF_Number(0));
75 pMediaBoxArray->Add(new CPDF_Number(0));
76 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(width)));
77 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(height)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 pPageDict->SetAt("MediaBox", pMediaBoxArray);
Tom Sepezae51c812015-08-05 12:34:06 -070080 pPageDict->SetAt("Rotate", new CPDF_Number(0));
81 pPageDict->SetAt("Resources", new CPDF_Dictionary);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 CPDFXFA_Page* pPage =
Tom Sepezae51c812015-08-05 12:34:06 -070084 new CPDFXFA_Page((CPDFXFA_Document*)document, page_index);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 pPage->LoadPDFPage(pPageDict);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 return pPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088}
89
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -070091 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
93 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
94 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
95 "Page")) {
96 return -1;
97 }
98 CPDF_Dictionary* pDict = pPage->m_pFormDict;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 int rotate = 0;
101 if (pDict != NULL) {
102 if (pDict->KeyExist("Rotate"))
103 rotate = pDict->GetElement("Rotate")->GetDirect()
104 ? pDict->GetElement("Rotate")->GetDirect()->GetInteger() / 90
105 : 0;
106 else {
107 if (pDict->KeyExist("Parent")) {
108 CPDF_Dictionary* pPages =
Dan Sinclairf1251c12015-10-20 16:24:45 -0400109 ToDictionary(pDict->GetElement("Parent")->GetDirect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 while (pPages) {
111 if (pPages->KeyExist("Rotate")) {
112 rotate =
113 pPages->GetElement("Rotate")->GetDirect()
114 ? pPages->GetElement("Rotate")->GetDirect()->GetInteger() /
115 90
116 : 0;
117 break;
118 } else if (pPages->KeyExist("Parent"))
Dan Sinclairf1251c12015-10-20 16:24:45 -0400119 pPages = ToDictionary(pPages->GetElement("Parent")->GetDirect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 else
121 break;
122 }
123 }
124 }
125 } else {
126 return -1;
127 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700128
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 return rotate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700130}
131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page,
133 FPDF_PAGEOBJECT page_obj) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700134 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
136 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
137 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
138 "Page")) {
139 return;
140 }
141 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_obj;
142 if (pPageObj == NULL)
143 return;
144 FX_POSITION LastPersition = pPage->GetLastObjectPosition();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700145
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 pPage->InsertObject(LastPersition, pPageObj);
147 switch (pPageObj->m_Type) {
148 case FPDF_PAGEOBJ_PATH: {
149 CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
150 pPathObj->CalcBoundingBox();
151 break;
152 }
153 case FPDF_PAGEOBJ_TEXT: {
154 // CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
155 // pPathObj->CalcBoundingBox();
156 break;
157 }
158 case FPDF_PAGEOBJ_IMAGE: {
159 CPDF_ImageObject* pImageObj = (CPDF_ImageObject*)pPageObj;
160 pImageObj->CalcBoundingBox();
161 break;
162 }
163 case FPDF_PAGEOBJ_SHADING: {
164 CPDF_ShadingObject* pShadingObj = (CPDF_ShadingObject*)pPageObj;
165 pShadingObj->CalcBoundingBox();
166 break;
167 }
168 case FPDF_PAGEOBJ_FORM: {
169 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
170 pFormObj->CalcBoundingBox();
171 break;
172 }
173 default:
174 break;
175 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700176}
177
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
Tom Sepezbf59a072015-10-21 14:07:23 -0700179 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
181 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
182 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
183 "Page")) {
184 return -1;
185 }
186 return pPage->CountObjects();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700187}
188
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page,
190 int index) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700191 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
193 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
194 "Page")) {
195 return NULL;
196 }
197 return pPage->GetObjectByIndex(index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198}
199
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700201 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
202 return pPage && pPage->BackgroundAlphaNeeded();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203}
204
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205DLLEXPORT FPDF_BOOL STDCALL
206FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
207 if (!pageObject)
208 return FALSE;
209 CPDF_PageObject* pPageObj = (CPDF_PageObject*)pageObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700210
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;
212 int blend_type =
213 pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;
214 if (blend_type != FXDIB_BLEND_NORMAL)
215 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700216
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217 CPDF_Dictionary* pSMaskDict =
Dan Sinclairf1251c12015-10-20 16:24:45 -0400218 pGeneralState ? ToDictionary(pGeneralState->m_pSoftMask) : NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 if (pSMaskDict)
220 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700221
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 if (pGeneralState && pGeneralState->m_FillAlpha != 1.0f)
223 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700224
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 if (pPageObj->m_Type == PDFPAGE_PATH) {
226 if (pGeneralState && pGeneralState->m_StrokeAlpha != 1.0f)
227 return TRUE;
228 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700229
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 if (pPageObj->m_Type == PDFPAGE_FORM) {
231 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
232 if (pFormObj->m_pForm &&
233 (pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED))
234 return TRUE;
235 if (pFormObj->m_pForm &&
236 (!(pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED) &&
237 (pFormObj->m_pForm->m_Transparency & PDFTRANS_GROUP)))
238 return TRUE;
239 }
240 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700241}
242
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700244 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
246 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
247 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
248 "Page")) {
249 return FALSE;
250 }
251 CPDF_PageContentGenerate CG(pPage);
252 CG.GenerateContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700253
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255}
256
257DLLEXPORT void STDCALL FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 double a,
259 double b,
260 double c,
261 double d,
262 double e,
263 double f) {
264 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
265 if (pPageObj == NULL)
266 return;
Lei Zhangcb78ef52015-10-02 10:10:49 -0700267
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
269 (FX_FLOAT)e, (FX_FLOAT)f);
270 pPageObj->Transform(matrix);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271}
272DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 double a,
274 double b,
275 double c,
276 double d,
277 double e,
278 double f) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700279 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 if (!pPage)
281 return;
282 CPDF_AnnotList AnnotList(pPage);
283 for (int i = 0; i < AnnotList.Count(); i++) {
284 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
285 // transformAnnots Rectangle
286 CPDF_Rect rect;
287 pAnnot->GetRect(rect);
288 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
289 (FX_FLOAT)e, (FX_FLOAT)f);
290 rect.Transform(&matrix);
291 CPDF_Array* pRectArray = NULL;
292 pRectArray = pAnnot->GetAnnotDict()->GetArray("Rect");
293 if (!pRectArray)
294 pRectArray = CPDF_Array::Create();
295 pRectArray->SetAt(0, new CPDF_Number(rect.left));
296 pRectArray->SetAt(1, new CPDF_Number(rect.bottom));
297 pRectArray->SetAt(2, new CPDF_Number(rect.right));
298 pRectArray->SetAt(3, new CPDF_Number(rect.top));
299 pAnnot->GetAnnotDict()->SetAt("Rect", pRectArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700300
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 // Transform AP's rectangle
302 // To Do
303 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700304}
Bo Xu394010d2014-06-12 13:41:50 -0700305
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700306DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700307 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
309 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
310 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
311 "Page")) {
312 return;
313 }
314 CPDF_Dictionary* pDict = pPage->m_pFormDict;
315 rotate %= 4;
Bo Xu394010d2014-06-12 13:41:50 -0700316
Tom Sepezae51c812015-08-05 12:34:06 -0700317 pDict->SetAt("Rotate", new CPDF_Number(rotate * 90));
Nico Weber0ce77e32014-07-16 13:19:08 -0700318}