blob: a1f2f396a55a37d72ea19614bf04890c6220ce73 [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
thestigc54bb432016-07-29 19:34:20 -07009#include <algorithm>
10#include <memory>
11#include <utility>
12
dsinclair24154352016-10-04 11:01:48 -070013#include "core/fpdfapi/edit/cpdf_pagecontentgenerator.h"
dsinclair41872fa2016-10-04 11:29:35 -070014#include "core/fpdfapi/page/cpdf_form.h"
15#include "core/fpdfapi/page/cpdf_formobject.h"
16#include "core/fpdfapi/page/cpdf_imageobject.h"
17#include "core/fpdfapi/page/cpdf_page.h"
18#include "core/fpdfapi/page/cpdf_pageobject.h"
19#include "core/fpdfapi/page/cpdf_pathobject.h"
20#include "core/fpdfapi/page/cpdf_shadingobject.h"
dsinclair488b7ad2016-10-04 11:55:50 -070021#include "core/fpdfapi/parser/cpdf_array.h"
22#include "core/fpdfapi/parser/cpdf_document.h"
23#include "core/fpdfapi/parser/cpdf_number.h"
24#include "core/fpdfapi/parser/cpdf_string.h"
dsinclair1727aee2016-09-29 13:12:56 -070025#include "core/fpdfdoc/cpdf_annot.h"
26#include "core/fpdfdoc/cpdf_annotlist.h"
dsinclair114e46a2016-09-29 17:18:21 -070027#include "fpdfsdk/fsdk_define.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080028#include "public/fpdf_formfill.h"
Lei Zhangb45324b2017-05-22 17:05:40 -070029#include "third_party/base/logging.h"
Tom Sepez2398d892016-02-17 16:46:26 -080030#include "third_party/base/stl_util.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080031
Tom Sepez51da0932015-11-25 16:05:49 -080032#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070033#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair4d29e782016-10-04 14:02:47 -070034#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080035#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036
Dan Sinclair698aed72017-09-26 16:24:49 -040037#if _FX_OS_ == _FX_OS_ANDROID_
Dan Sinclair85c8e7f2016-11-21 13:50:32 -050038#include <time.h>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039#else
40#include <ctime>
41#endif
42
thestigc54bb432016-07-29 19:34:20 -070043namespace {
44
45static_assert(FPDF_PAGEOBJ_TEXT == CPDF_PageObject::TEXT,
46 "FPDF_PAGEOBJ_TEXT/CPDF_PageObject::TEXT mismatch");
47static_assert(FPDF_PAGEOBJ_PATH == CPDF_PageObject::PATH,
48 "FPDF_PAGEOBJ_PATH/CPDF_PageObject::PATH mismatch");
49static_assert(FPDF_PAGEOBJ_IMAGE == CPDF_PageObject::IMAGE,
50 "FPDF_PAGEOBJ_IMAGE/CPDF_PageObject::IMAGE mismatch");
51static_assert(FPDF_PAGEOBJ_SHADING == CPDF_PageObject::SHADING,
52 "FPDF_PAGEOBJ_SHADING/CPDF_PageObject::SHADING mismatch");
53static_assert(FPDF_PAGEOBJ_FORM == CPDF_PageObject::FORM,
54 "FPDF_PAGEOBJ_FORM/CPDF_PageObject::FORM mismatch");
55
56bool IsPageObject(CPDF_Page* pPage) {
57 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type"))
58 return false;
59
dsinclair38fd8442016-09-15 10:15:32 -070060 CPDF_Object* pObject = pPage->m_pFormDict->GetObjectFor("Type")->GetDirect();
thestigc54bb432016-07-29 19:34:20 -070061 return pObject && !pObject->GetString().Compare("Page");
62}
63
wileyryae858aa42017-05-31 14:49:05 -050064void CalcBoundingBox(CPDF_PageObject* pPageObj) {
65 switch (pPageObj->GetType()) {
66 case CPDF_PageObject::TEXT: {
67 break;
68 }
69 case CPDF_PageObject::PATH: {
70 CPDF_PathObject* pPathObj = pPageObj->AsPath();
71 pPathObj->CalcBoundingBox();
72 break;
73 }
74 case CPDF_PageObject::IMAGE: {
75 CPDF_ImageObject* pImageObj = pPageObj->AsImage();
76 pImageObj->CalcBoundingBox();
77 break;
78 }
79 case CPDF_PageObject::SHADING: {
80 CPDF_ShadingObject* pShadingObj = pPageObj->AsShading();
81 pShadingObj->CalcBoundingBox();
82 break;
83 }
84 case CPDF_PageObject::FORM: {
85 CPDF_FormObject* pFormObj = pPageObj->AsForm();
86 pFormObj->CalcBoundingBox();
87 break;
88 }
89 default: {
90 NOTREACHED();
91 break;
92 }
93 }
94}
95
thestigc54bb432016-07-29 19:34:20 -070096} // namespace
97
Dan Sinclair00d2ad12017-08-10 14:13:02 -040098FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV FPDF_CreateNewDocument() {
Tom Sepezfe91c6c2017-05-16 15:33:20 -070099 auto pDoc = pdfium::MakeUnique<CPDF_Document>(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 pDoc->CreateNewDoc();
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 time_t currentTime;
Ryan Harrison275e2602017-09-18 14:23:18 -0400103 ByteString DateStr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) {
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700105 if (time(&currentTime) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 tm* pTM = localtime(&currentTime);
107 if (pTM) {
108 DateStr.Format("D:%04d%02d%02d%02d%02d%02d", pTM->tm_year + 1900,
109 pTM->tm_mon + 1, pTM->tm_mday, pTM->tm_hour, pTM->tm_min,
110 pTM->tm_sec);
111 }
112 }
113 }
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700114
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700115 CPDF_Dictionary* pInfoDict = pDoc->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 if (pInfoDict) {
tsepez0e606b52016-11-18 16:22:41 -0800117 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
118 pInfoDict->SetNewFor<CPDF_String>("CreationDate", DateStr, false);
119 pInfoDict->SetNewFor<CPDF_String>("Creator", L"PDFium");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700121
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700122 // Caller takes ownership of pDoc.
123 return FPDFDocumentFromCPDFDocument(pDoc.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700124}
125
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400126FPDF_EXPORT void FPDF_CALLCONV FPDFPage_Delete(FPDF_DOCUMENT document,
127 int page_index) {
Tom Sepez744da702016-03-15 12:43:09 -0700128 if (UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document))
129 pDoc->DeletePage(page_index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700130}
131
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400132FPDF_EXPORT FPDF_PAGE FPDF_CALLCONV FPDFPage_New(FPDF_DOCUMENT document,
133 int page_index,
134 double width,
135 double height) {
Tom Sepez471a1032015-10-15 16:17:18 -0700136 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
137 if (!pDoc)
138 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700139
Lei Zhang85f019a2017-03-17 15:14:19 -0700140 page_index = pdfium::clamp(page_index, 0, pDoc->GetPageCount());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141 CPDF_Dictionary* pPageDict = pDoc->CreateNewPage(page_index);
142 if (!pPageDict)
thestig1cd352e2016-06-07 17:53:06 -0700143 return nullptr;
thestigc54bb432016-07-29 19:34:20 -0700144
tsepez0e606b52016-11-18 16:22:41 -0800145 CPDF_Array* pMediaBoxArray = pPageDict->SetNewFor<CPDF_Array>("MediaBox");
tsepez8a3aa452016-11-16 12:26:06 -0800146 pMediaBoxArray->AddNew<CPDF_Number>(0);
147 pMediaBoxArray->AddNew<CPDF_Number>(0);
Dan Sinclair05df0752017-03-14 14:43:42 -0400148 pMediaBoxArray->AddNew<CPDF_Number>(static_cast<float>(width));
149 pMediaBoxArray->AddNew<CPDF_Number>(static_cast<float>(height));
tsepez0e606b52016-11-18 16:22:41 -0800150 pPageDict->SetNewFor<CPDF_Number>("Rotate", 0);
151 pPageDict->SetNewFor<CPDF_Dictionary>("Resources");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152
Tom Sepez40e9ff32015-11-30 12:39:54 -0800153#ifdef PDF_ENABLE_XFA
Tom Sepez9792f162017-05-16 14:11:30 -0700154 auto pXFAPage = pdfium::MakeRetain<CPDFXFA_Page>(
155 static_cast<CPDFXFA_Context*>(document), page_index);
156 pXFAPage->LoadPDFPage(pPageDict);
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700157 return pXFAPage.Leak(); // Caller takes ownership.
Tom Sepez40e9ff32015-11-30 12:39:54 -0800158#else // PDF_ENABLE_XFA
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700159 auto pPage = pdfium::MakeUnique<CPDF_Page>(pDoc, pPageDict, true);
thestig5cc24652016-04-26 11:46:02 -0700160 pPage->ParseContent();
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700161 return pPage.release(); // Caller takes ownership.
Tom Sepez9792f162017-05-16 14:11:30 -0700162#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700163}
164
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400165FPDF_EXPORT int FPDF_CALLCONV FPDFPage_GetRotation(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700166 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700167 return IsPageObject(pPage) ? pPage->GetPageRotation() : -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168}
169
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400170FPDF_EXPORT void FPDF_CALLCONV FPDFPage_InsertObject(FPDF_PAGE page,
171 FPDF_PAGEOBJECT page_obj) {
Jane Liu1a084022017-06-29 19:47:12 -0400172 CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(page_obj);
Lei Zhang997de612015-11-04 18:17:53 -0800173 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700175
thestigc54bb432016-07-29 19:34:20 -0700176 std::unique_ptr<CPDF_PageObject> pPageObjHolder(pPageObj);
177 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
178 if (!IsPageObject(pPage))
179 return;
wileyryae858aa42017-05-31 14:49:05 -0500180 pPageObj->SetDirty(true);
thestigc54bb432016-07-29 19:34:20 -0700181 pPage->GetPageObjectList()->push_back(std::move(pPageObjHolder));
wileyryae858aa42017-05-31 14:49:05 -0500182 CalcBoundingBox(pPageObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183}
184
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400185FPDF_EXPORT int FPDF_CALLCONV FPDFPage_CountObject(FPDF_PAGE page) {
Miklos Vajna92627612017-09-25 12:59:29 +0200186 return FPDFPage_CountObjects(page);
187}
188
189FPDF_EXPORT int FPDF_CALLCONV FPDFPage_CountObjects(FPDF_PAGE page) {
Tom Sepezbf59a072015-10-21 14:07:23 -0700190 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
thestigc54bb432016-07-29 19:34:20 -0700191 if (!IsPageObject(pPage))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 return -1;
Tom Sepez2398d892016-02-17 16:46:26 -0800193 return pdfium::CollectionSize<int>(*pPage->GetPageObjectList());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700194}
195
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400196FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPage_GetObject(FPDF_PAGE page,
197 int index) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700198 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
thestigc54bb432016-07-29 19:34:20 -0700199 if (!IsPageObject(pPage))
Tom Sepez2398d892016-02-17 16:46:26 -0800200 return nullptr;
Tom Sepez2398d892016-02-17 16:46:26 -0800201 return pPage->GetPageObjectList()->GetPageObjectByIndex(index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700202}
203
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400204FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_HasTransparency(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700205 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
206 return pPage && pPage->BackgroundAlphaNeeded();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207}
208
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400209FPDF_EXPORT void FPDF_CALLCONV FPDFPageObj_Destroy(FPDF_PAGEOBJECT page_obj) {
Jane Liu2e5f0ae2017-08-08 15:23:27 -0400210 delete CPDFPageObjectFromFPDFPageObject(page_obj);
211}
212
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400213FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
215 if (!pageObject)
tsepez4cf55152016-11-02 14:37:54 -0700216 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217
Jane Liu1a084022017-06-29 19:47:12 -0400218 CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(pageObject);
tsepezbbee4452016-09-02 15:22:00 -0700219 int blend_type = pPageObj->m_GeneralState.GetBlendType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 if (blend_type != FXDIB_BLEND_NORMAL)
tsepez4cf55152016-11-02 14:37:54 -0700221 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700222
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 CPDF_Dictionary* pSMaskDict =
tsepezbbee4452016-09-02 15:22:00 -0700224 ToDictionary(pPageObj->m_GeneralState.GetSoftMask());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 if (pSMaskDict)
tsepez4cf55152016-11-02 14:37:54 -0700226 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700227
tsepezbbee4452016-09-02 15:22:00 -0700228 if (pPageObj->m_GeneralState.GetFillAlpha() != 1.0f)
tsepez4cf55152016-11-02 14:37:54 -0700229 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700230
tsepezbbee4452016-09-02 15:22:00 -0700231 if (pPageObj->IsPath() && pPageObj->m_GeneralState.GetStrokeAlpha() != 1.0f) {
tsepez4cf55152016-11-02 14:37:54 -0700232 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700234
Wei Li7cf13c92016-02-19 11:53:03 -0800235 if (pPageObj->IsForm()) {
thestigc54bb432016-07-29 19:34:20 -0700236 const CPDF_Form* pForm = pPageObj->AsForm()->form();
237 if (pForm) {
238 int trans = pForm->m_Transparency;
239 if ((trans & PDFTRANS_ISOLATED) || (trans & PDFTRANS_GROUP))
tsepez4cf55152016-11-02 14:37:54 -0700240 return true;
thestigc54bb432016-07-29 19:34:20 -0700241 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 }
thestigc54bb432016-07-29 19:34:20 -0700243
tsepez4cf55152016-11-02 14:37:54 -0700244 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700245}
246
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400247FPDF_EXPORT int FPDF_CALLCONV FPDFPageObj_GetType(FPDF_PAGEOBJECT pageObject) {
Miklos Vajna14233192017-04-03 16:02:39 +0200248 if (!pageObject)
249 return FPDF_PAGEOBJ_UNKNOWN;
250
Jane Liu1a084022017-06-29 19:47:12 -0400251 CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(pageObject);
Miklos Vajna14233192017-04-03 16:02:39 +0200252 return pPageObj->GetType();
253}
254
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400255FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_GenerateContent(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700256 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
thestigc54bb432016-07-29 19:34:20 -0700257 if (!IsPageObject(pPage))
tsepez4cf55152016-11-02 14:37:54 -0700258 return false;
thestigc54bb432016-07-29 19:34:20 -0700259
Tom Sepeze19e06e2016-01-21 10:49:56 -0800260 CPDF_PageContentGenerator CG(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 CG.GenerateContent();
tsepez4cf55152016-11-02 14:37:54 -0700262 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263}
264
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400265FPDF_EXPORT void FPDF_CALLCONV
266FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
267 double a,
268 double b,
269 double c,
270 double d,
271 double e,
272 double f) {
Jane Liu1a084022017-06-29 19:47:12 -0400273 CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
Lei Zhang997de612015-11-04 18:17:53 -0800274 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275 return;
Lei Zhangcb78ef52015-10-02 10:10:49 -0700276
Dan Sinclair05df0752017-03-14 14:43:42 -0400277 CFX_Matrix matrix((float)a, (float)b, (float)c, (float)d, (float)e, (float)f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 pPageObj->Transform(matrix);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279}
thestigc54bb432016-07-29 19:34:20 -0700280
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400281FPDF_EXPORT void FPDF_CALLCONV
282FPDFPageObj_SetBlendMode(FPDF_PAGEOBJECT page_object,
283 FPDF_BYTESTRING blend_mode) {
Jane Liu1a084022017-06-29 19:47:12 -0400284 CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
wileyrya06bbdef2017-05-26 15:20:23 -0500285 if (!pPageObj)
286 return;
287
288 pPageObj->m_GeneralState.SetBlendMode(blend_mode);
wileyryae858aa42017-05-31 14:49:05 -0500289 pPageObj->SetDirty(true);
wileyrya06bbdef2017-05-26 15:20:23 -0500290}
291
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400292FPDF_EXPORT void FPDF_CALLCONV FPDFPage_TransformAnnots(FPDF_PAGE page,
293 double a,
294 double b,
295 double c,
296 double d,
297 double e,
298 double f) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700299 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300 if (!pPage)
301 return;
thestigc54bb432016-07-29 19:34:20 -0700302
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 CPDF_AnnotList AnnotList(pPage);
Lei Zhang1b700c32015-10-30 23:55:35 -0700304 for (size_t i = 0; i < AnnotList.Count(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
Dan Sinclair05df0752017-03-14 14:43:42 -0400306 CFX_Matrix matrix((float)a, (float)b, (float)c, (float)d, (float)e,
307 (float)f);
Jane Liu878b27d2017-08-22 10:50:06 -0400308 CFX_FloatRect rect = matrix.TransformRect(pAnnot->GetRect());
tsepez8021a642016-10-17 16:13:21 -0700309
Lei Zhang59c1ac02017-06-09 11:04:41 -0700310 CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict();
311 CPDF_Array* pRectArray = pAnnotDict->GetArrayFor("Rect");
Jane Liueda65252017-06-07 11:31:27 -0400312 if (pRectArray)
Lei Zhang59c1ac02017-06-09 11:04:41 -0700313 pRectArray->Clear();
Jane Liueda65252017-06-07 11:31:27 -0400314 else
Lei Zhang59c1ac02017-06-09 11:04:41 -0700315 pRectArray = pAnnotDict->SetNewFor<CPDF_Array>("Rect");
tsepez0e606b52016-11-18 16:22:41 -0800316
Jane Liueda65252017-06-07 11:31:27 -0400317 pRectArray->AddNew<CPDF_Number>(rect.left);
318 pRectArray->AddNew<CPDF_Number>(rect.bottom);
319 pRectArray->AddNew<CPDF_Number>(rect.right);
320 pRectArray->AddNew<CPDF_Number>(rect.top);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700321
Dan Sinclair85c8e7f2016-11-21 13:50:32 -0500322 // TODO(unknown): Transform AP's rectangle
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700324}
Bo Xu394010d2014-06-12 13:41:50 -0700325
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400326FPDF_EXPORT void FPDF_CALLCONV FPDFPage_SetRotation(FPDF_PAGE page,
327 int rotate) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700328 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
thestigc54bb432016-07-29 19:34:20 -0700329 if (!IsPageObject(pPage))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 return;
thestigc54bb432016-07-29 19:34:20 -0700331
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332 rotate %= 4;
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700333 pPage->m_pFormDict->SetNewFor<CPDF_Number>("Rotate", rotate * 90);
Nico Weber0ce77e32014-07-16 13:19:08 -0700334}
wileyrya864e9fb2017-05-26 11:38:14 -0500335
336FPDF_BOOL FPDFPageObj_SetFillColor(FPDF_PAGEOBJECT page_object,
337 unsigned int R,
338 unsigned int G,
339 unsigned int B,
340 unsigned int A) {
341 if (!page_object || R > 255 || G > 255 || B > 255 || A > 255)
342 return false;
343
344 float rgb[3] = {R / 255.f, G / 255.f, B / 255.f};
Jane Liu1a084022017-06-29 19:47:12 -0400345 auto* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
wileyrya864e9fb2017-05-26 11:38:14 -0500346 pPageObj->m_GeneralState.SetFillAlpha(A / 255.f);
347 pPageObj->m_ColorState.SetFillColor(
348 CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB), rgb, 3);
wileyryae858aa42017-05-31 14:49:05 -0500349 pPageObj->SetDirty(true);
wileyrya864e9fb2017-05-26 11:38:14 -0500350 return true;
351}
wileyryaf1697fa2017-05-26 12:27:40 -0500352
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400353FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
354FPDFPageObj_GetBounds(FPDF_PAGEOBJECT pageObject,
355 float* left,
356 float* bottom,
357 float* right,
358 float* top) {
wileyryaf1697fa2017-05-26 12:27:40 -0500359 if (!pageObject)
360 return false;
361
Jane Liu1a084022017-06-29 19:47:12 -0400362 CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(pageObject);
wileyryaf1697fa2017-05-26 12:27:40 -0500363 CFX_FloatRect bbox = pPageObj->GetRect();
364 *left = bbox.left;
365 *bottom = bbox.bottom;
366 *right = bbox.right;
367 *top = bbox.top;
368 return true;
369}