blob: 489053080c84ded8c5b6d545979f9ec2be0e6e82 [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
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070037#if _FX_OS_ == _FX_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;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 CFX_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) {
Tom Sepezbf59a072015-10-21 14:07:23 -0700186 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
thestigc54bb432016-07-29 19:34:20 -0700187 if (!IsPageObject(pPage))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 return -1;
Tom Sepez2398d892016-02-17 16:46:26 -0800189 return pdfium::CollectionSize<int>(*pPage->GetPageObjectList());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700190}
191
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400192FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPage_GetObject(FPDF_PAGE page,
193 int index) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700194 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
thestigc54bb432016-07-29 19:34:20 -0700195 if (!IsPageObject(pPage))
Tom Sepez2398d892016-02-17 16:46:26 -0800196 return nullptr;
Tom Sepez2398d892016-02-17 16:46:26 -0800197 return pPage->GetPageObjectList()->GetPageObjectByIndex(index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198}
199
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400200FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV 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
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400205FPDF_EXPORT void FPDF_CALLCONV FPDFPageObj_Destroy(FPDF_PAGEOBJECT page_obj) {
Jane Liu2e5f0ae2017-08-08 15:23:27 -0400206 delete CPDFPageObjectFromFPDFPageObject(page_obj);
207}
208
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400209FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
211 if (!pageObject)
tsepez4cf55152016-11-02 14:37:54 -0700212 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700213
Jane Liu1a084022017-06-29 19:47:12 -0400214 CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(pageObject);
tsepezbbee4452016-09-02 15:22:00 -0700215 int blend_type = pPageObj->m_GeneralState.GetBlendType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 if (blend_type != FXDIB_BLEND_NORMAL)
tsepez4cf55152016-11-02 14:37:54 -0700217 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700218
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 CPDF_Dictionary* pSMaskDict =
tsepezbbee4452016-09-02 15:22:00 -0700220 ToDictionary(pPageObj->m_GeneralState.GetSoftMask());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 if (pSMaskDict)
tsepez4cf55152016-11-02 14:37:54 -0700222 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700223
tsepezbbee4452016-09-02 15:22:00 -0700224 if (pPageObj->m_GeneralState.GetFillAlpha() != 1.0f)
tsepez4cf55152016-11-02 14:37:54 -0700225 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700226
tsepezbbee4452016-09-02 15:22:00 -0700227 if (pPageObj->IsPath() && pPageObj->m_GeneralState.GetStrokeAlpha() != 1.0f) {
tsepez4cf55152016-11-02 14:37:54 -0700228 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700230
Wei Li7cf13c92016-02-19 11:53:03 -0800231 if (pPageObj->IsForm()) {
thestigc54bb432016-07-29 19:34:20 -0700232 const CPDF_Form* pForm = pPageObj->AsForm()->form();
233 if (pForm) {
234 int trans = pForm->m_Transparency;
235 if ((trans & PDFTRANS_ISOLATED) || (trans & PDFTRANS_GROUP))
tsepez4cf55152016-11-02 14:37:54 -0700236 return true;
thestigc54bb432016-07-29 19:34:20 -0700237 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 }
thestigc54bb432016-07-29 19:34:20 -0700239
tsepez4cf55152016-11-02 14:37:54 -0700240 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700241}
242
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400243FPDF_EXPORT int FPDF_CALLCONV FPDFPageObj_GetType(FPDF_PAGEOBJECT pageObject) {
Miklos Vajna14233192017-04-03 16:02:39 +0200244 if (!pageObject)
245 return FPDF_PAGEOBJ_UNKNOWN;
246
Jane Liu1a084022017-06-29 19:47:12 -0400247 CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(pageObject);
Miklos Vajna14233192017-04-03 16:02:39 +0200248 return pPageObj->GetType();
249}
250
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400251FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_GenerateContent(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700252 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
thestigc54bb432016-07-29 19:34:20 -0700253 if (!IsPageObject(pPage))
tsepez4cf55152016-11-02 14:37:54 -0700254 return false;
thestigc54bb432016-07-29 19:34:20 -0700255
Tom Sepeze19e06e2016-01-21 10:49:56 -0800256 CPDF_PageContentGenerator CG(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 CG.GenerateContent();
tsepez4cf55152016-11-02 14:37:54 -0700258 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259}
260
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400261FPDF_EXPORT void FPDF_CALLCONV
262FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
263 double a,
264 double b,
265 double c,
266 double d,
267 double e,
268 double f) {
Jane Liu1a084022017-06-29 19:47:12 -0400269 CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
Lei Zhang997de612015-11-04 18:17:53 -0800270 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 return;
Lei Zhangcb78ef52015-10-02 10:10:49 -0700272
Dan Sinclair05df0752017-03-14 14:43:42 -0400273 CFX_Matrix matrix((float)a, (float)b, (float)c, (float)d, (float)e, (float)f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 pPageObj->Transform(matrix);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275}
thestigc54bb432016-07-29 19:34:20 -0700276
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400277FPDF_EXPORT void FPDF_CALLCONV
278FPDFPageObj_SetBlendMode(FPDF_PAGEOBJECT page_object,
279 FPDF_BYTESTRING blend_mode) {
Jane Liu1a084022017-06-29 19:47:12 -0400280 CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
wileyrya06bbdef2017-05-26 15:20:23 -0500281 if (!pPageObj)
282 return;
283
284 pPageObj->m_GeneralState.SetBlendMode(blend_mode);
wileyryae858aa42017-05-31 14:49:05 -0500285 pPageObj->SetDirty(true);
wileyrya06bbdef2017-05-26 15:20:23 -0500286}
287
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400288FPDF_EXPORT void FPDF_CALLCONV FPDFPage_TransformAnnots(FPDF_PAGE page,
289 double a,
290 double b,
291 double c,
292 double d,
293 double e,
294 double f) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700295 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296 if (!pPage)
297 return;
thestigc54bb432016-07-29 19:34:20 -0700298
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 CPDF_AnnotList AnnotList(pPage);
Lei Zhang1b700c32015-10-30 23:55:35 -0700300 for (size_t i = 0; i < AnnotList.Count(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
tsepez8021a642016-10-17 16:13:21 -0700302 CFX_FloatRect rect = pAnnot->GetRect(); // transformAnnots Rectangle
Dan Sinclair05df0752017-03-14 14:43:42 -0400303 CFX_Matrix matrix((float)a, (float)b, (float)c, (float)d, (float)e,
304 (float)f);
Dan Sinclair118a8e22017-02-09 10:16:07 -0500305 matrix.TransformRect(rect);
tsepez8021a642016-10-17 16:13:21 -0700306
Lei Zhang59c1ac02017-06-09 11:04:41 -0700307 CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict();
308 CPDF_Array* pRectArray = pAnnotDict->GetArrayFor("Rect");
Jane Liueda65252017-06-07 11:31:27 -0400309 if (pRectArray)
Lei Zhang59c1ac02017-06-09 11:04:41 -0700310 pRectArray->Clear();
Jane Liueda65252017-06-07 11:31:27 -0400311 else
Lei Zhang59c1ac02017-06-09 11:04:41 -0700312 pRectArray = pAnnotDict->SetNewFor<CPDF_Array>("Rect");
tsepez0e606b52016-11-18 16:22:41 -0800313
Jane Liueda65252017-06-07 11:31:27 -0400314 pRectArray->AddNew<CPDF_Number>(rect.left);
315 pRectArray->AddNew<CPDF_Number>(rect.bottom);
316 pRectArray->AddNew<CPDF_Number>(rect.right);
317 pRectArray->AddNew<CPDF_Number>(rect.top);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700318
Dan Sinclair85c8e7f2016-11-21 13:50:32 -0500319 // TODO(unknown): Transform AP's rectangle
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700321}
Bo Xu394010d2014-06-12 13:41:50 -0700322
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400323FPDF_EXPORT void FPDF_CALLCONV FPDFPage_SetRotation(FPDF_PAGE page,
324 int rotate) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700325 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
thestigc54bb432016-07-29 19:34:20 -0700326 if (!IsPageObject(pPage))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327 return;
thestigc54bb432016-07-29 19:34:20 -0700328
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329 rotate %= 4;
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700330 pPage->m_pFormDict->SetNewFor<CPDF_Number>("Rotate", rotate * 90);
Nico Weber0ce77e32014-07-16 13:19:08 -0700331}
wileyrya864e9fb2017-05-26 11:38:14 -0500332
333FPDF_BOOL FPDFPageObj_SetFillColor(FPDF_PAGEOBJECT page_object,
334 unsigned int R,
335 unsigned int G,
336 unsigned int B,
337 unsigned int A) {
338 if (!page_object || R > 255 || G > 255 || B > 255 || A > 255)
339 return false;
340
341 float rgb[3] = {R / 255.f, G / 255.f, B / 255.f};
Jane Liu1a084022017-06-29 19:47:12 -0400342 auto* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
wileyrya864e9fb2017-05-26 11:38:14 -0500343 pPageObj->m_GeneralState.SetFillAlpha(A / 255.f);
344 pPageObj->m_ColorState.SetFillColor(
345 CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB), rgb, 3);
wileyryae858aa42017-05-31 14:49:05 -0500346 pPageObj->SetDirty(true);
wileyrya864e9fb2017-05-26 11:38:14 -0500347 return true;
348}
wileyryaf1697fa2017-05-26 12:27:40 -0500349
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400350FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
351FPDFPageObj_GetBounds(FPDF_PAGEOBJECT pageObject,
352 float* left,
353 float* bottom,
354 float* right,
355 float* top) {
wileyryaf1697fa2017-05-26 12:27:40 -0500356 if (!pageObject)
357 return false;
358
Jane Liu1a084022017-06-29 19:47:12 -0400359 CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(pageObject);
wileyryaf1697fa2017-05-26 12:27:40 -0500360 CFX_FloatRect bbox = pPageObj->GetRect();
361 *left = bbox.left;
362 *bottom = bbox.bottom;
363 *right = bbox.right;
364 *top = bbox.top;
365 return true;
366}