blob: a276e3a3682aac766bbabb323413363ad8e54e11 [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_flatten.h"
8
Lei Zhang375a8642016-01-11 11:59:17 -08009#include <algorithm>
tsepez0e606b52016-11-18 16:22:41 -080010#include <memory>
11#include <utility>
12#include <vector>
Lei Zhang375a8642016-01-11 11:59:17 -080013
dsinclair41872fa2016-10-04 11:29:35 -070014#include "core/fpdfapi/page/cpdf_page.h"
15#include "core/fpdfapi/page/cpdf_pageobject.h"
dsinclair488b7ad2016-10-04 11:55:50 -070016#include "core/fpdfapi/parser/cpdf_array.h"
17#include "core/fpdfapi/parser/cpdf_document.h"
tsepez0e606b52016-11-18 16:22:41 -080018#include "core/fpdfapi/parser/cpdf_name.h"
dsinclair488b7ad2016-10-04 11:55:50 -070019#include "core/fpdfapi/parser/cpdf_number.h"
tsepez8a3aa452016-11-16 12:26:06 -080020#include "core/fpdfapi/parser/cpdf_reference.h"
dsinclair488b7ad2016-10-04 11:55:50 -070021#include "core/fpdfapi/parser/cpdf_stream.h"
22#include "core/fpdfapi/parser/cpdf_stream_acc.h"
dsinclair1727aee2016-09-29 13:12:56 -070023#include "core/fpdfdoc/cpdf_annot.h"
dsinclair114e46a2016-09-29 17:18:21 -070024#include "fpdfsdk/fsdk_define.h"
tsepez74b8c6e2016-10-12 09:38:41 -070025#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027enum FPDF_TYPE { MAX, MIN };
28enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM };
29
tsepez381fc832016-10-10 14:06:44 -070030namespace {
31
Lei Zhangd3610052017-06-19 12:25:54 -070032bool IsValidRect(const CFX_FloatRect& rect, const CFX_FloatRect& rcPage) {
33 constexpr float kMinSize = 0.000001f;
34 if (rect.IsEmpty() || rect.Width() < kMinSize || rect.Height() < kMinSize)
tsepez4cf55152016-11-02 14:37:54 -070035 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070036
Lei Zhangd3610052017-06-19 12:25:54 -070037 if (rcPage.IsEmpty())
38 return true;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070039
Lei Zhangd3610052017-06-19 12:25:54 -070040 constexpr float kMinBorderSize = 10.000001f;
41 return rect.left - rcPage.left >= -kMinBorderSize &&
42 rect.right - rcPage.right <= kMinBorderSize &&
43 rect.top - rcPage.top <= kMinBorderSize &&
44 rect.bottom - rcPage.bottom >= -kMinBorderSize;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045}
46
Tom Sepez2398d892016-02-17 16:46:26 -080047void GetContentsRect(CPDF_Document* pDoc,
48 CPDF_Dictionary* pDict,
tsepez6173c9d2016-11-09 16:38:03 -080049 std::vector<CFX_FloatRect>* pRectArray) {
Dan Sinclair0bb13332017-03-30 16:12:02 -040050 auto pPDFPage = pdfium::MakeUnique<CPDF_Page>(pDoc, pDict, false);
thestig5cc24652016-04-26 11:46:02 -070051 pPDFPage->ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052
thestig2fad11a2016-06-16 16:39:25 -070053 for (const auto& pPageObject : *pPDFPage->GetPageObjectList()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -080054 CFX_FloatRect rc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 rc.left = pPageObject->m_Left;
56 rc.right = pPageObject->m_Right;
57 rc.bottom = pPageObject->m_Bottom;
58 rc.top = pPageObject->m_Top;
Lei Zhangd3610052017-06-19 12:25:54 -070059 if (IsValidRect(rc, pDict->GetRectFor("MediaBox")))
tsepez6173c9d2016-11-09 16:38:03 -080060 pRectArray->push_back(rc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062}
63
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064void ParserStream(CPDF_Dictionary* pPageDic,
65 CPDF_Dictionary* pStream,
tsepez6173c9d2016-11-09 16:38:03 -080066 std::vector<CFX_FloatRect>* pRectArray,
tsepez74b8c6e2016-10-12 09:38:41 -070067 std::vector<CPDF_Dictionary*>* pObjectArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 if (!pStream)
69 return;
Tom Sepez281a9ea2016-02-26 14:24:28 -080070 CFX_FloatRect rect;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 if (pStream->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -070072 rect = pStream->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 else if (pStream->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -070074 rect = pStream->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075
Lei Zhangd3610052017-06-19 12:25:54 -070076 if (IsValidRect(rect, pPageDic->GetRectFor("MediaBox")))
tsepez6173c9d2016-11-09 16:38:03 -080077 pRectArray->push_back(rect);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070078
tsepez74b8c6e2016-10-12 09:38:41 -070079 pObjectArray->push_back(pStream);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080}
81
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082int ParserAnnots(CPDF_Document* pSourceDoc,
83 CPDF_Dictionary* pPageDic,
tsepez6173c9d2016-11-09 16:38:03 -080084 std::vector<CFX_FloatRect>* pRectArray,
tsepez74b8c6e2016-10-12 09:38:41 -070085 std::vector<CPDF_Dictionary*>* pObjectArray,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 int nUsage) {
87 if (!pSourceDoc || !pPageDic)
88 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 GetContentsRect(pSourceDoc, pPageDic, pRectArray);
dsinclair38fd8442016-09-15 10:15:32 -070091 CPDF_Array* pAnnots = pPageDic->GetArrayFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 if (!pAnnots)
93 return FLATTEN_NOTHINGTODO;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070094
Lei Zhangd3610052017-06-19 12:25:54 -070095 for (const auto& pAnnot : *pAnnots) {
96 CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnot->GetDirect());
Dan Sinclairf1251c12015-10-20 16:24:45 -040097 if (!pAnnotDic)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070099
Ryan Harrison275e2602017-09-18 14:23:18 -0400100 ByteString sSubtype = pAnnotDic->GetStringFor("Subtype");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 if (sSubtype == "Popup")
102 continue;
103
dsinclair38fd8442016-09-15 10:15:32 -0700104 int nAnnotFlag = pAnnotDic->GetIntegerFor("F");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 if (nAnnotFlag & ANNOTFLAG_HIDDEN)
106 continue;
107
Lei Zhangd3610052017-06-19 12:25:54 -0700108 bool bParseStream;
109 if (nUsage == FLAT_NORMALDISPLAY)
110 bParseStream = !(nAnnotFlag & ANNOTFLAG_INVISIBLE);
111 else
112 bParseStream = !!(nAnnotFlag & ANNOTFLAG_PRINT);
113 if (bParseStream)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 }
116 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117}
118
Dan Sinclair05df0752017-03-14 14:43:42 -0400119float GetMinMaxValue(const std::vector<CFX_FloatRect>& array,
120 FPDF_TYPE type,
121 FPDF_VALUE value) {
Lei Zhangd3610052017-06-19 12:25:54 -0700122 if (array.empty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123 return 0.0f;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700124
Lei Zhangd3610052017-06-19 12:25:54 -0700125 size_t nRects = array.size();
Dan Sinclair05df0752017-03-14 14:43:42 -0400126 std::vector<float> pArray(nRects);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 switch (value) {
tsepez6173c9d2016-11-09 16:38:03 -0800128 case LEFT:
129 for (size_t i = 0; i < nRects; i++)
130 pArray[i] = array[i].left;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 break;
tsepez6173c9d2016-11-09 16:38:03 -0800132 case TOP:
133 for (size_t i = 0; i < nRects; i++)
134 pArray[i] = array[i].top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 break;
tsepez6173c9d2016-11-09 16:38:03 -0800136 case RIGHT:
137 for (size_t i = 0; i < nRects; i++)
138 pArray[i] = array[i].right;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 break;
tsepez6173c9d2016-11-09 16:38:03 -0800140 case BOTTOM:
141 for (size_t i = 0; i < nRects; i++)
142 pArray[i] = array[i].bottom;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 default:
Lei Zhangd3610052017-06-19 12:25:54 -0700145 NOTREACHED();
weili12367cb2016-06-03 11:22:16 -0700146 return 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 }
tsepez6173c9d2016-11-09 16:38:03 -0800148
Dan Sinclair05df0752017-03-14 14:43:42 -0400149 float fRet = pArray[0];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 if (type == MAX) {
tsepez6173c9d2016-11-09 16:38:03 -0800151 for (size_t i = 1; i < nRects; i++)
152 fRet = std::max(fRet, pArray[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 } else {
tsepez6173c9d2016-11-09 16:38:03 -0800154 for (size_t i = 1; i < nRects; i++)
155 fRet = std::min(fRet, pArray[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 return fRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700158}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700159
tsepez6173c9d2016-11-09 16:38:03 -0800160CFX_FloatRect CalculateRect(std::vector<CFX_FloatRect>* pRectArray) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800161 CFX_FloatRect rcRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700162
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT);
164 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP);
165 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT);
166 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700167
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 return rcRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700169}
170
Ryan Harrison275e2602017-09-18 14:23:18 -0400171uint32_t NewIndirectContentsStream(const ByteString& key,
tsepez381fc832016-10-10 14:06:44 -0700172 CPDF_Document* pDocument) {
tsepez70c4afd2016-11-15 11:33:44 -0800173 CPDF_Stream* pNewContents = pDocument->NewIndirect<CPDF_Stream>(
tsepez9e05ee12016-11-21 13:19:10 -0800174 nullptr, 0,
175 pdfium::MakeUnique<CPDF_Dictionary>(pDocument->GetByteStringPool()));
Ryan Harrison275e2602017-09-18 14:23:18 -0400176 ByteString sStream;
tsepez381fc832016-10-10 14:06:44 -0700177 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
178 pNewContents->SetData(sStream.raw_str(), sStream.GetLength());
tsepez70c4afd2016-11-15 11:33:44 -0800179 return pNewContents->GetObjNum();
tsepez381fc832016-10-10 14:06:44 -0700180}
181
Ryan Harrison275e2602017-09-18 14:23:18 -0400182void SetPageContents(const ByteString& key,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 CPDF_Dictionary* pPage,
184 CPDF_Document* pDocument) {
thestig1cd352e2016-06-07 17:53:06 -0700185 CPDF_Array* pContentsArray = nullptr;
tsepez381fc832016-10-10 14:06:44 -0700186 CPDF_Stream* pContentsStream = pPage->GetStreamFor("Contents");
187 if (!pContentsStream) {
188 pContentsArray = pPage->GetArrayFor("Contents");
189 if (!pContentsArray) {
190 if (!key.IsEmpty()) {
tsepez0e606b52016-11-18 16:22:41 -0800191 pPage->SetNewFor<CPDF_Reference>(
192 "Contents", pDocument, NewIndirectContentsStream(key, pDocument));
tsepez381fc832016-10-10 14:06:44 -0700193 }
194 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 }
tsepez381fc832016-10-10 14:06:44 -0700197 pPage->ConvertToIndirectObjectFor("Contents", pDocument);
198 if (!pContentsArray) {
tsepez70c4afd2016-11-15 11:33:44 -0800199 pContentsArray = pDocument->NewIndirect<CPDF_Array>();
Tom Sepezafd0d1f2017-04-04 14:37:18 -0700200 auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pContentsStream);
201 pAcc->LoadAllData();
Ryan Harrison275e2602017-09-18 14:23:18 -0400202 ByteString sStream = "q\n";
203 ByteString sBody =
204 ByteString((const char*)pAcc->GetData(), pAcc->GetSize());
tsepez381fc832016-10-10 14:06:44 -0700205 sStream = sStream + sBody + "\nQ";
Artem Strygin90555e02017-07-28 19:41:59 +0300206 pContentsStream->SetDataAndRemoveFilter(sStream.raw_str(),
207 sStream.GetLength());
tsepez8a3aa452016-11-16 12:26:06 -0800208 pContentsArray->AddNew<CPDF_Reference>(pDocument,
209 pContentsStream->GetObjNum());
tsepez0e606b52016-11-18 16:22:41 -0800210 pPage->SetNewFor<CPDF_Reference>("Contents", pDocument,
211 pContentsArray->GetObjNum());
tsepez381fc832016-10-10 14:06:44 -0700212 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 if (!key.IsEmpty()) {
tsepez8a3aa452016-11-16 12:26:06 -0800214 pContentsArray->AddNew<CPDF_Reference>(
215 pDocument, NewIndirectContentsStream(key, pDocument));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217}
218
Tom Sepez281a9ea2016-02-26 14:24:28 -0800219CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot,
220 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800221 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 if (rcStream.IsEmpty())
Tom Sepez60d909e2015-12-10 15:34:55 -0800223 return CFX_Matrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700224
Jane Liu878b27d2017-08-22 10:50:06 -0400225 rcStream = matrix.TransformRect(rcStream);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700227
Dan Sinclair05df0752017-03-14 14:43:42 -0400228 float a = rcAnnot.Width() / rcStream.Width();
229 float d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700230
Dan Sinclair05df0752017-03-14 14:43:42 -0400231 float e = rcAnnot.left - rcStream.left * a;
232 float f = rcAnnot.bottom - rcStream.bottom * d;
Tom Sepez60d909e2015-12-10 15:34:55 -0800233 return CFX_Matrix(a, 0, 0, d, e, f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700235
tsepez381fc832016-10-10 14:06:44 -0700236} // namespace
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700237
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400238FPDF_EXPORT int FPDF_CALLCONV FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700239 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
tsepez6173c9d2016-11-09 16:38:03 -0800240 if (!page)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 return FLATTEN_FAIL;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700242
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700243 CPDF_Document* pDocument = pPage->m_pDocument.Get();
244 CPDF_Dictionary* pPageDict = pPage->m_pFormDict.Get();
tsepez6173c9d2016-11-09 16:38:03 -0800245 if (!pDocument || !pPageDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247
tsepez74b8c6e2016-10-12 09:38:41 -0700248 std::vector<CPDF_Dictionary*> ObjectArray;
tsepez6173c9d2016-11-09 16:38:03 -0800249 std::vector<CFX_FloatRect> RectArray;
250 int iRet =
251 ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
253 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700254
Tom Sepez281a9ea2016-02-26 14:24:28 -0800255 CFX_FloatRect rcOriginalCB;
256 CFX_FloatRect rcMerger = CalculateRect(&RectArray);
dsinclair38fd8442016-09-15 10:15:32 -0700257 CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 if (pPageDict->KeyExist("CropBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700259 rcOriginalMB = pPageDict->GetRectFor("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260
tsepez6173c9d2016-11-09 16:38:03 -0800261 if (rcOriginalMB.IsEmpty())
Tom Sepez281a9ea2016-02-26 14:24:28 -0800262 rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263
tsepez6173c9d2016-11-09 16:38:03 -0800264 rcMerger.left = std::max(rcMerger.left, rcOriginalMB.left);
265 rcMerger.right = std::min(rcMerger.right, rcOriginalMB.right);
266 rcMerger.bottom = std::max(rcMerger.bottom, rcOriginalMB.bottom);
267 rcMerger.top = std::min(rcMerger.top, rcOriginalMB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 if (pPageDict->KeyExist("ArtBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700269 rcOriginalCB = pPageDict->GetRectFor("ArtBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 else
271 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 if (!rcOriginalMB.IsEmpty()) {
tsepez0e606b52016-11-18 16:22:41 -0800274 CPDF_Array* pMediaBox = pPageDict->SetNewFor<CPDF_Array>("MediaBox");
tsepez8a3aa452016-11-16 12:26:06 -0800275 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.left);
276 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.bottom);
277 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.right);
278 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700280
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 if (!rcOriginalCB.IsEmpty()) {
tsepez0e606b52016-11-18 16:22:41 -0800282 CPDF_Array* pCropBox = pPageDict->SetNewFor<CPDF_Array>("ArtBox");
tsepez8a3aa452016-11-16 12:26:06 -0800283 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.left);
284 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.bottom);
285 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.right);
286 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700288
dsinclair38fd8442016-09-15 10:15:32 -0700289 CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources");
tsepez0e606b52016-11-18 16:22:41 -0800290 if (!pRes)
291 pRes = pPageDict->SetNewFor<CPDF_Dictionary>("Resources");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700292
tsepez70c4afd2016-11-15 11:33:44 -0800293 CPDF_Stream* pNewXObject = pDocument->NewIndirect<CPDF_Stream>(
tsepez9e05ee12016-11-21 13:19:10 -0800294 nullptr, 0,
295 pdfium::MakeUnique<CPDF_Dictionary>(pDocument->GetByteStringPool()));
tsepez698c5712016-09-28 16:47:07 -0700296
tsepez70c4afd2016-11-15 11:33:44 -0800297 uint32_t dwObjNum = pNewXObject->GetObjNum();
dsinclair38fd8442016-09-15 10:15:32 -0700298 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject");
tsepez0e606b52016-11-18 16:22:41 -0800299 if (!pPageXObject)
300 pPageXObject = pRes->SetNewFor<CPDF_Dictionary>("XObject");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700301
Ryan Harrison275e2602017-09-18 14:23:18 -0400302 ByteString key;
tsepez74b8c6e2016-10-12 09:38:41 -0700303 int nStreams = pdfium::CollectionSize<int>(ObjectArray);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 if (nStreams > 0) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400305 ByteString sKey;
Lei Zhangf0f2a2a2017-06-13 22:57:46 -0700306 int i = 0;
307 while (i < INT_MAX) {
308 sKey.Format("FFT%d", i);
309 if (!pPageXObject->KeyExist(sKey)) {
310 key = sKey;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 break;
Lei Zhangf0f2a2a2017-06-13 22:57:46 -0700312 }
313 ++i;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314 }
315 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700316
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700318
thestig1cd352e2016-06-07 17:53:06 -0700319 CPDF_Dictionary* pNewXORes = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 if (!key.IsEmpty()) {
tsepez0e606b52016-11-18 16:22:41 -0800321 pPageXObject->SetNewFor<CPDF_Reference>(key, pDocument, dwObjNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
tsepez0e606b52016-11-18 16:22:41 -0800323 pNewXORes = pNewOXbjectDic->SetNewFor<CPDF_Dictionary>("Resources");
324 pNewOXbjectDic->SetNewFor<CPDF_Name>("Type", "XObject");
325 pNewOXbjectDic->SetNewFor<CPDF_Name>("Subtype", "Form");
326 pNewOXbjectDic->SetNewFor<CPDF_Number>("FormType", 1);
dsinclair38fd8442016-09-15 10:15:32 -0700327 CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox");
328 pNewOXbjectDic->SetRectFor("BBox", rcBBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700330
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331 for (int i = 0; i < nStreams; i++) {
tsepez74b8c6e2016-10-12 09:38:41 -0700332 CPDF_Dictionary* pAnnotDic = ObjectArray[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 if (!pAnnotDic)
334 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700335
dsinclair38fd8442016-09-15 10:15:32 -0700336 CFX_FloatRect rcAnnot = pAnnotDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700338
Ryan Harrison275e2602017-09-18 14:23:18 -0400339 ByteString sAnnotState = pAnnotDic->GetStringFor("AS");
dsinclair38fd8442016-09-15 10:15:32 -0700340 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictFor("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 if (!pAnnotAP)
342 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700343
dsinclair38fd8442016-09-15 10:15:32 -0700344 CPDF_Stream* pAPStream = pAnnotAP->GetStreamFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345 if (!pAPStream) {
dsinclair38fd8442016-09-15 10:15:32 -0700346 CPDF_Dictionary* pAPDic = pAnnotAP->GetDictFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347 if (!pAPDic)
348 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700349
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700350 if (!sAnnotState.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700351 pAPStream = pAPDic->GetStreamFor(sAnnotState);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352 } else {
Lei Zhangd3610052017-06-19 12:25:54 -0700353 if (pAPDic->GetCount() > 0) {
354 CPDF_Object* pFirstObj = pAPDic->begin()->second.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 if (pFirstObj) {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400356 if (pFirstObj->IsReference())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357 pFirstObj = pFirstObj->GetDirect();
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400358 if (!pFirstObj->IsStream())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 continue;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400360 pAPStream = pFirstObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361 }
362 }
363 }
364 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 if (!pAPStream)
366 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700367
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800369 CFX_FloatRect rcStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 if (pAPDic->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -0700371 rcStream = pAPDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700372 else if (pAPDic->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700373 rcStream = pAPDic->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700374
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 if (rcStream.IsEmpty())
376 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700377
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 CPDF_Object* pObj = pAPStream;
tsepezd0ecd892016-11-08 17:30:04 -0800379 if (pObj->IsInline()) {
tsepez335cf092016-11-09 13:28:26 -0800380 std::unique_ptr<CPDF_Object> pNew = pObj->Clone();
381 pObj = pNew.get();
tsepez70c4afd2016-11-15 11:33:44 -0800382 pDocument->AddIndirectObject(std::move(pNew));
tsepezd0ecd892016-11-08 17:30:04 -0800383 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700384
tsepezd0ecd892016-11-08 17:30:04 -0800385 CPDF_Dictionary* pObjDic = pObj->GetDict();
386 if (pObjDic) {
tsepez0e606b52016-11-18 16:22:41 -0800387 pObjDic->SetNewFor<CPDF_Name>("Type", "XObject");
388 pObjDic->SetNewFor<CPDF_Name>("Subtype", "Form");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700390
dsinclair38fd8442016-09-15 10:15:32 -0700391 CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject");
tsepez0e606b52016-11-18 16:22:41 -0800392 if (!pXObject)
393 pXObject = pNewXORes->SetNewFor<CPDF_Dictionary>("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394
Ryan Harrison275e2602017-09-18 14:23:18 -0400395 ByteString sFormName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 sFormName.Format("F%d", i);
tsepez0e606b52016-11-18 16:22:41 -0800397 pXObject->SetNewFor<CPDF_Reference>(sFormName, pDocument,
398 pObj->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399
Tom Sepezafd0d1f2017-04-04 14:37:18 -0700400 auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pNewXObject);
401 pAcc->LoadAllData();
Ryan Harrison275e2602017-09-18 14:23:18 -0400402 ByteString sStream(pAcc->GetData(), pAcc->GetSize());
tsepezd0ecd892016-11-08 17:30:04 -0800403 CFX_Matrix matrix = pAPDic->GetMatrixFor("Matrix");
Tom Sepez60d909e2015-12-10 15:34:55 -0800404 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix);
Ryan Harrison275e2602017-09-18 14:23:18 -0400405 ByteString sTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
407 sFormName.c_str());
408 sStream += sTemp;
Artem Strygin90555e02017-07-28 19:41:59 +0300409 pNewXObject->SetDataAndRemoveFilter(sStream.raw_str(), sStream.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410 }
dsinclair38fd8442016-09-15 10:15:32 -0700411 pPageDict->RemoveFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700413}