blob: b3a52991d265589124b5e001439d8064424d826a [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;
Lei Zhang997de612015-11-04 18:17:53 -080099 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 return -1;
Lei Zhang997de612015-11-04 18:17:53 -0800101
102 while (pDict) {
103 if (pDict->KeyExist("Rotate")) {
104 CPDF_Object* pRotateObj = pDict->GetElement("Rotate")->GetDirect();
105 return pRotateObj ? pRotateObj->GetInteger() / 90 : 0;
106 }
107 if (!pDict->KeyExist("Parent"))
108 break;
109
110 pDict = ToDictionary(pDict->GetElement("Parent")->GetDirect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700112
Lei Zhang997de612015-11-04 18:17:53 -0800113 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700114}
115
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page,
117 FPDF_PAGEOBJECT page_obj) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700118 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
120 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
121 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
122 "Page")) {
123 return;
124 }
125 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_obj;
Lei Zhang997de612015-11-04 18:17:53 -0800126 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 return;
128 FX_POSITION LastPersition = pPage->GetLastObjectPosition();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 pPage->InsertObject(LastPersition, pPageObj);
131 switch (pPageObj->m_Type) {
132 case FPDF_PAGEOBJ_PATH: {
133 CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
134 pPathObj->CalcBoundingBox();
135 break;
136 }
137 case FPDF_PAGEOBJ_TEXT: {
138 // CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
139 // pPathObj->CalcBoundingBox();
140 break;
141 }
142 case FPDF_PAGEOBJ_IMAGE: {
143 CPDF_ImageObject* pImageObj = (CPDF_ImageObject*)pPageObj;
144 pImageObj->CalcBoundingBox();
145 break;
146 }
147 case FPDF_PAGEOBJ_SHADING: {
148 CPDF_ShadingObject* pShadingObj = (CPDF_ShadingObject*)pPageObj;
149 pShadingObj->CalcBoundingBox();
150 break;
151 }
152 case FPDF_PAGEOBJ_FORM: {
153 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
154 pFormObj->CalcBoundingBox();
155 break;
156 }
157 default:
158 break;
159 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160}
161
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
Tom Sepezbf59a072015-10-21 14:07:23 -0700163 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
165 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
166 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
167 "Page")) {
168 return -1;
169 }
170 return pPage->CountObjects();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171}
172
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page,
174 int index) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700175 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
177 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
178 "Page")) {
179 return NULL;
180 }
181 return pPage->GetObjectByIndex(index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700182}
183
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700185 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
186 return pPage && pPage->BackgroundAlphaNeeded();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700187}
188
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189DLLEXPORT FPDF_BOOL STDCALL
190FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
191 if (!pageObject)
192 return FALSE;
193 CPDF_PageObject* pPageObj = (CPDF_PageObject*)pageObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;
196 int blend_type =
197 pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;
198 if (blend_type != FXDIB_BLEND_NORMAL)
199 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 CPDF_Dictionary* pSMaskDict =
Dan Sinclairf1251c12015-10-20 16:24:45 -0400202 pGeneralState ? ToDictionary(pGeneralState->m_pSoftMask) : NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 if (pSMaskDict)
204 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700205
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 if (pGeneralState && pGeneralState->m_FillAlpha != 1.0f)
207 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700208
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 if (pPageObj->m_Type == PDFPAGE_PATH) {
210 if (pGeneralState && pGeneralState->m_StrokeAlpha != 1.0f)
211 return TRUE;
212 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700213
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 if (pPageObj->m_Type == PDFPAGE_FORM) {
215 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
216 if (pFormObj->m_pForm &&
217 (pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED))
218 return TRUE;
219 if (pFormObj->m_pForm &&
220 (!(pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED) &&
221 (pFormObj->m_pForm->m_Transparency & PDFTRANS_GROUP)))
222 return TRUE;
223 }
224 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700225}
226
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700228 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
230 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
231 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
232 "Page")) {
233 return FALSE;
234 }
235 CPDF_PageContentGenerate CG(pPage);
236 CG.GenerateContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239}
240
241DLLEXPORT void STDCALL FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 double a,
243 double b,
244 double c,
245 double d,
246 double e,
247 double f) {
248 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
Lei Zhang997de612015-11-04 18:17:53 -0800249 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 return;
Lei Zhangcb78ef52015-10-02 10:10:49 -0700251
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
253 (FX_FLOAT)e, (FX_FLOAT)f);
254 pPageObj->Transform(matrix);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255}
256DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 double a,
258 double b,
259 double c,
260 double d,
261 double e,
262 double f) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700263 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 if (!pPage)
265 return;
266 CPDF_AnnotList AnnotList(pPage);
Lei Zhang1b700c32015-10-30 23:55:35 -0700267 for (size_t i = 0; i < AnnotList.Count(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
269 // transformAnnots Rectangle
270 CPDF_Rect rect;
271 pAnnot->GetRect(rect);
272 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
273 (FX_FLOAT)e, (FX_FLOAT)f);
274 rect.Transform(&matrix);
275 CPDF_Array* pRectArray = NULL;
276 pRectArray = pAnnot->GetAnnotDict()->GetArray("Rect");
277 if (!pRectArray)
278 pRectArray = CPDF_Array::Create();
279 pRectArray->SetAt(0, new CPDF_Number(rect.left));
280 pRectArray->SetAt(1, new CPDF_Number(rect.bottom));
281 pRectArray->SetAt(2, new CPDF_Number(rect.right));
282 pRectArray->SetAt(3, new CPDF_Number(rect.top));
283 pAnnot->GetAnnotDict()->SetAt("Rect", pRectArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700284
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 // Transform AP's rectangle
286 // To Do
287 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700288}
Bo Xu394010d2014-06-12 13:41:50 -0700289
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700291 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
293 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
294 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
295 "Page")) {
296 return;
297 }
298 CPDF_Dictionary* pDict = pPage->m_pFormDict;
299 rotate %= 4;
Bo Xu394010d2014-06-12 13:41:50 -0700300
Tom Sepezae51c812015-08-05 12:34:06 -0700301 pDict->SetAt("Rotate", new CPDF_Number(rotate * 90));
Nico Weber0ce77e32014-07-16 13:19:08 -0700302}