blob: bb87c4caeb48d75fbdeee50d6a077d221e4288d0 [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
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_edit.h"
8
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"
Lei Zhangb4e7f302015-11-06 15:52:32 -080013#include "public/fpdf_formfill.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015#if _FX_OS_ == _FX_ANDROID_
16#include "time.h"
17#else
18#include <ctime>
19#endif
20
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_CreateNewDocument() {
Tom Sepezae51c812015-08-05 12:34:06 -070022 CPDF_Document* pDoc = new CPDF_Document;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023 pDoc->CreateNewDoc();
24 time_t currentTime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 CFX_ByteString DateStr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) {
29 if (-1 != time(&currentTime)) {
30 tm* pTM = localtime(&currentTime);
31 if (pTM) {
32 DateStr.Format("D:%04d%02d%02d%02d%02d%02d", pTM->tm_year + 1900,
33 pTM->tm_mon + 1, pTM->tm_mday, pTM->tm_hour, pTM->tm_min,
34 pTM->tm_sec);
35 }
36 }
37 }
Tom Sepezbdeeb8a2015-05-27 12:25:00 -070038
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 CPDF_Dictionary* pInfoDict = NULL;
40 pInfoDict = pDoc->GetInfo();
41 if (pInfoDict) {
42 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
43 pInfoDict->SetAt("CreationDate", new CPDF_String(DateStr));
Tom Sepezae51c812015-08-05 12:34:06 -070044 pInfoDict->SetAt("Creator", new CPDF_String(L"PDFium"));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046
Tom Sepezbf59a072015-10-21 14:07:23 -070047 return FPDFDocumentFromCPDFDocument(pDoc);
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;
Lei Zhang997de612015-11-04 18:17:53 -0800100 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 return -1;
Lei Zhang997de612015-11-04 18:17:53 -0800102
103 while (pDict) {
104 if (pDict->KeyExist("Rotate")) {
105 CPDF_Object* pRotateObj = pDict->GetElement("Rotate")->GetDirect();
106 return pRotateObj ? pRotateObj->GetInteger() / 90 : 0;
107 }
108 if (!pDict->KeyExist("Parent"))
109 break;
110
111 pDict = ToDictionary(pDict->GetElement("Parent")->GetDirect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700113
Lei Zhang997de612015-11-04 18:17:53 -0800114 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115}
116
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page,
118 FPDF_PAGEOBJECT page_obj) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700119 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
121 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
122 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
123 "Page")) {
124 return;
125 }
126 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_obj;
Lei Zhang997de612015-11-04 18:17:53 -0800127 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 return;
129 FX_POSITION LastPersition = pPage->GetLastObjectPosition();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 pPage->InsertObject(LastPersition, pPageObj);
132 switch (pPageObj->m_Type) {
133 case FPDF_PAGEOBJ_PATH: {
134 CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
135 pPathObj->CalcBoundingBox();
136 break;
137 }
138 case FPDF_PAGEOBJ_TEXT: {
139 // CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
140 // pPathObj->CalcBoundingBox();
141 break;
142 }
143 case FPDF_PAGEOBJ_IMAGE: {
144 CPDF_ImageObject* pImageObj = (CPDF_ImageObject*)pPageObj;
145 pImageObj->CalcBoundingBox();
146 break;
147 }
148 case FPDF_PAGEOBJ_SHADING: {
149 CPDF_ShadingObject* pShadingObj = (CPDF_ShadingObject*)pPageObj;
150 pShadingObj->CalcBoundingBox();
151 break;
152 }
153 case FPDF_PAGEOBJ_FORM: {
154 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
155 pFormObj->CalcBoundingBox();
156 break;
157 }
158 default:
159 break;
160 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161}
162
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
Tom Sepezbf59a072015-10-21 14:07:23 -0700164 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
166 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
167 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
168 "Page")) {
169 return -1;
170 }
171 return pPage->CountObjects();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700172}
173
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page,
175 int index) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700176 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
178 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
179 "Page")) {
180 return NULL;
181 }
182 return pPage->GetObjectByIndex(index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183}
184
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700186 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
187 return pPage && pPage->BackgroundAlphaNeeded();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700188}
189
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190DLLEXPORT FPDF_BOOL STDCALL
191FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
192 if (!pageObject)
193 return FALSE;
194 CPDF_PageObject* pPageObj = (CPDF_PageObject*)pageObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;
197 int blend_type =
198 pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;
199 if (blend_type != FXDIB_BLEND_NORMAL)
200 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 CPDF_Dictionary* pSMaskDict =
Dan Sinclairf1251c12015-10-20 16:24:45 -0400203 pGeneralState ? ToDictionary(pGeneralState->m_pSoftMask) : NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 if (pSMaskDict)
205 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700206
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207 if (pGeneralState && pGeneralState->m_FillAlpha != 1.0f)
208 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700209
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 if (pPageObj->m_Type == PDFPAGE_PATH) {
211 if (pGeneralState && pGeneralState->m_StrokeAlpha != 1.0f)
212 return TRUE;
213 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700214
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 if (pPageObj->m_Type == PDFPAGE_FORM) {
216 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
217 if (pFormObj->m_pForm &&
218 (pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED))
219 return TRUE;
220 if (pFormObj->m_pForm &&
221 (!(pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED) &&
222 (pFormObj->m_pForm->m_Transparency & PDFTRANS_GROUP)))
223 return TRUE;
224 }
225 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700226}
227
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700229 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
231 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
232 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
233 "Page")) {
234 return FALSE;
235 }
236 CPDF_PageContentGenerate CG(pPage);
237 CG.GenerateContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700238
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700240}
241
242DLLEXPORT void STDCALL FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 double a,
244 double b,
245 double c,
246 double d,
247 double e,
248 double f) {
249 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
Lei Zhang997de612015-11-04 18:17:53 -0800250 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 return;
Lei Zhangcb78ef52015-10-02 10:10:49 -0700252
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
254 (FX_FLOAT)e, (FX_FLOAT)f);
255 pPageObj->Transform(matrix);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256}
257DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
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) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700264 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 if (!pPage)
266 return;
267 CPDF_AnnotList AnnotList(pPage);
Lei Zhang1b700c32015-10-30 23:55:35 -0700268 for (size_t i = 0; i < AnnotList.Count(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
270 // transformAnnots Rectangle
271 CPDF_Rect rect;
272 pAnnot->GetRect(rect);
273 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
274 (FX_FLOAT)e, (FX_FLOAT)f);
275 rect.Transform(&matrix);
276 CPDF_Array* pRectArray = NULL;
277 pRectArray = pAnnot->GetAnnotDict()->GetArray("Rect");
278 if (!pRectArray)
279 pRectArray = CPDF_Array::Create();
280 pRectArray->SetAt(0, new CPDF_Number(rect.left));
281 pRectArray->SetAt(1, new CPDF_Number(rect.bottom));
282 pRectArray->SetAt(2, new CPDF_Number(rect.right));
283 pRectArray->SetAt(3, new CPDF_Number(rect.top));
284 pAnnot->GetAnnotDict()->SetAt("Rect", pRectArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 // Transform AP's rectangle
287 // To Do
288 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700289}
Bo Xu394010d2014-06-12 13:41:50 -0700290
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700292 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
294 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
295 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
296 "Page")) {
297 return;
298 }
299 CPDF_Dictionary* pDict = pPage->m_pFormDict;
300 rotate %= 4;
Bo Xu394010d2014-06-12 13:41:50 -0700301
Tom Sepezae51c812015-08-05 12:34:06 -0700302 pDict->SetAt("Rotate", new CPDF_Number(rotate * 90));
Nico Weber0ce77e32014-07-16 13:19:08 -0700303}