blob: e530553f5a47c90a2b19a113d00d752d8c942078 [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
dsinclair38fd8442016-09-15 10:15:32 -0700100 CFX_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
tsepez381fc832016-10-10 14:06:44 -0700171uint32_t NewIndirectContentsStream(const CFX_ByteString& key,
172 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()));
tsepez381fc832016-10-10 14:06:44 -0700176 CFX_ByteString sStream;
177 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
182void SetPageContents(const CFX_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();
tsepez381fc832016-10-10 14:06:44 -0700202 CFX_ByteString sStream = "q\n";
Tom Sepezc25a4212016-10-14 17:45:56 -0700203 CFX_ByteString sBody =
Tom Sepezafd0d1f2017-04-04 14:37:18 -0700204 CFX_ByteString((const char*)pAcc->GetData(), pAcc->GetSize());
tsepez381fc832016-10-10 14:06:44 -0700205 sStream = sStream + sBody + "\nQ";
206 pContentsStream->SetData(sStream.raw_str(), sStream.GetLength());
tsepez8a3aa452016-11-16 12:26:06 -0800207 pContentsArray->AddNew<CPDF_Reference>(pDocument,
208 pContentsStream->GetObjNum());
tsepez0e606b52016-11-18 16:22:41 -0800209 pPage->SetNewFor<CPDF_Reference>("Contents", pDocument,
210 pContentsArray->GetObjNum());
tsepez381fc832016-10-10 14:06:44 -0700211 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 if (!key.IsEmpty()) {
tsepez8a3aa452016-11-16 12:26:06 -0800213 pContentsArray->AddNew<CPDF_Reference>(
214 pDocument, NewIndirectContentsStream(key, pDocument));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700216}
217
Tom Sepez281a9ea2016-02-26 14:24:28 -0800218CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot,
219 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800220 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 if (rcStream.IsEmpty())
Tom Sepez60d909e2015-12-10 15:34:55 -0800222 return CFX_Matrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700223
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 matrix.TransformRect(rcStream);
225 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700226
Dan Sinclair05df0752017-03-14 14:43:42 -0400227 float a = rcAnnot.Width() / rcStream.Width();
228 float d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700229
Dan Sinclair05df0752017-03-14 14:43:42 -0400230 float e = rcAnnot.left - rcStream.left * a;
231 float f = rcAnnot.bottom - rcStream.bottom * d;
Tom Sepez60d909e2015-12-10 15:34:55 -0800232 return CFX_Matrix(a, 0, 0, d, e, f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700234
tsepez381fc832016-10-10 14:06:44 -0700235} // namespace
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700236
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700238 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
tsepez6173c9d2016-11-09 16:38:03 -0800239 if (!page)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 return FLATTEN_FAIL;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700241
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700242 CPDF_Document* pDocument = pPage->m_pDocument.Get();
243 CPDF_Dictionary* pPageDict = pPage->m_pFormDict.Get();
tsepez6173c9d2016-11-09 16:38:03 -0800244 if (!pDocument || !pPageDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246
tsepez74b8c6e2016-10-12 09:38:41 -0700247 std::vector<CPDF_Dictionary*> ObjectArray;
tsepez6173c9d2016-11-09 16:38:03 -0800248 std::vector<CFX_FloatRect> RectArray;
249 int iRet =
250 ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
252 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700253
Tom Sepez281a9ea2016-02-26 14:24:28 -0800254 CFX_FloatRect rcOriginalCB;
255 CFX_FloatRect rcMerger = CalculateRect(&RectArray);
dsinclair38fd8442016-09-15 10:15:32 -0700256 CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 if (pPageDict->KeyExist("CropBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700258 rcOriginalMB = pPageDict->GetRectFor("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259
tsepez6173c9d2016-11-09 16:38:03 -0800260 if (rcOriginalMB.IsEmpty())
Tom Sepez281a9ea2016-02-26 14:24:28 -0800261 rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262
tsepez6173c9d2016-11-09 16:38:03 -0800263 rcMerger.left = std::max(rcMerger.left, rcOriginalMB.left);
264 rcMerger.right = std::min(rcMerger.right, rcOriginalMB.right);
265 rcMerger.bottom = std::max(rcMerger.bottom, rcOriginalMB.bottom);
266 rcMerger.top = std::min(rcMerger.top, rcOriginalMB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 if (pPageDict->KeyExist("ArtBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700268 rcOriginalCB = pPageDict->GetRectFor("ArtBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 else
270 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 if (!rcOriginalMB.IsEmpty()) {
tsepez0e606b52016-11-18 16:22:41 -0800273 CPDF_Array* pMediaBox = pPageDict->SetNewFor<CPDF_Array>("MediaBox");
tsepez8a3aa452016-11-16 12:26:06 -0800274 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.left);
275 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.bottom);
276 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.right);
277 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 if (!rcOriginalCB.IsEmpty()) {
tsepez0e606b52016-11-18 16:22:41 -0800281 CPDF_Array* pCropBox = pPageDict->SetNewFor<CPDF_Array>("ArtBox");
tsepez8a3aa452016-11-16 12:26:06 -0800282 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.left);
283 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.bottom);
284 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.right);
285 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700287
dsinclair38fd8442016-09-15 10:15:32 -0700288 CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources");
tsepez0e606b52016-11-18 16:22:41 -0800289 if (!pRes)
290 pRes = pPageDict->SetNewFor<CPDF_Dictionary>("Resources");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291
tsepez70c4afd2016-11-15 11:33:44 -0800292 CPDF_Stream* pNewXObject = pDocument->NewIndirect<CPDF_Stream>(
tsepez9e05ee12016-11-21 13:19:10 -0800293 nullptr, 0,
294 pdfium::MakeUnique<CPDF_Dictionary>(pDocument->GetByteStringPool()));
tsepez698c5712016-09-28 16:47:07 -0700295
tsepez70c4afd2016-11-15 11:33:44 -0800296 uint32_t dwObjNum = pNewXObject->GetObjNum();
dsinclair38fd8442016-09-15 10:15:32 -0700297 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject");
tsepez0e606b52016-11-18 16:22:41 -0800298 if (!pPageXObject)
299 pPageXObject = pRes->SetNewFor<CPDF_Dictionary>("XObject");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700300
Lei Zhangf0f2a2a2017-06-13 22:57:46 -0700301 CFX_ByteString key;
tsepez74b8c6e2016-10-12 09:38:41 -0700302 int nStreams = pdfium::CollectionSize<int>(ObjectArray);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 if (nStreams > 0) {
Lei Zhangf0f2a2a2017-06-13 22:57:46 -0700304 CFX_ByteString sKey;
305 int i = 0;
306 while (i < INT_MAX) {
307 sKey.Format("FFT%d", i);
308 if (!pPageXObject->KeyExist(sKey)) {
309 key = sKey;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 break;
Lei Zhangf0f2a2a2017-06-13 22:57:46 -0700311 }
312 ++i;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 }
314 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700315
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700317
thestig1cd352e2016-06-07 17:53:06 -0700318 CPDF_Dictionary* pNewXORes = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 if (!key.IsEmpty()) {
tsepez0e606b52016-11-18 16:22:41 -0800320 pPageXObject->SetNewFor<CPDF_Reference>(key, pDocument, dwObjNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
tsepez0e606b52016-11-18 16:22:41 -0800322 pNewXORes = pNewOXbjectDic->SetNewFor<CPDF_Dictionary>("Resources");
323 pNewOXbjectDic->SetNewFor<CPDF_Name>("Type", "XObject");
324 pNewOXbjectDic->SetNewFor<CPDF_Name>("Subtype", "Form");
325 pNewOXbjectDic->SetNewFor<CPDF_Number>("FormType", 1);
dsinclair38fd8442016-09-15 10:15:32 -0700326 CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox");
327 pNewOXbjectDic->SetRectFor("BBox", rcBBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700329
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 for (int i = 0; i < nStreams; i++) {
tsepez74b8c6e2016-10-12 09:38:41 -0700331 CPDF_Dictionary* pAnnotDic = ObjectArray[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332 if (!pAnnotDic)
333 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700334
dsinclair38fd8442016-09-15 10:15:32 -0700335 CFX_FloatRect rcAnnot = pAnnotDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700337
dsinclair38fd8442016-09-15 10:15:32 -0700338 CFX_ByteString sAnnotState = pAnnotDic->GetStringFor("AS");
339 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictFor("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 if (!pAnnotAP)
341 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700342
dsinclair38fd8442016-09-15 10:15:32 -0700343 CPDF_Stream* pAPStream = pAnnotAP->GetStreamFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 if (!pAPStream) {
dsinclair38fd8442016-09-15 10:15:32 -0700345 CPDF_Dictionary* pAPDic = pAnnotAP->GetDictFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700346 if (!pAPDic)
347 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700348
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 if (!sAnnotState.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700350 pAPStream = pAPDic->GetStreamFor(sAnnotState);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 } else {
Lei Zhangd3610052017-06-19 12:25:54 -0700352 if (pAPDic->GetCount() > 0) {
353 CPDF_Object* pFirstObj = pAPDic->begin()->second.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 if (pFirstObj) {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400355 if (pFirstObj->IsReference())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356 pFirstObj = pFirstObj->GetDirect();
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400357 if (!pFirstObj->IsStream())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700358 continue;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400359 pAPStream = pFirstObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 }
361 }
362 }
363 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364 if (!pAPStream)
365 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700366
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800368 CFX_FloatRect rcStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 if (pAPDic->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -0700370 rcStream = pAPDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 else if (pAPDic->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700372 rcStream = pAPDic->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700373
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374 if (rcStream.IsEmpty())
375 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700376
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377 CPDF_Object* pObj = pAPStream;
tsepezd0ecd892016-11-08 17:30:04 -0800378 if (pObj->IsInline()) {
tsepez335cf092016-11-09 13:28:26 -0800379 std::unique_ptr<CPDF_Object> pNew = pObj->Clone();
380 pObj = pNew.get();
tsepez70c4afd2016-11-15 11:33:44 -0800381 pDocument->AddIndirectObject(std::move(pNew));
tsepezd0ecd892016-11-08 17:30:04 -0800382 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700383
tsepezd0ecd892016-11-08 17:30:04 -0800384 CPDF_Dictionary* pObjDic = pObj->GetDict();
385 if (pObjDic) {
tsepez0e606b52016-11-18 16:22:41 -0800386 pObjDic->SetNewFor<CPDF_Name>("Type", "XObject");
387 pObjDic->SetNewFor<CPDF_Name>("Subtype", "Form");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700389
dsinclair38fd8442016-09-15 10:15:32 -0700390 CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject");
tsepez0e606b52016-11-18 16:22:41 -0800391 if (!pXObject)
392 pXObject = pNewXORes->SetNewFor<CPDF_Dictionary>("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393
394 CFX_ByteString sFormName;
395 sFormName.Format("F%d", i);
tsepez0e606b52016-11-18 16:22:41 -0800396 pXObject->SetNewFor<CPDF_Reference>(sFormName, pDocument,
397 pObj->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398
Tom Sepezafd0d1f2017-04-04 14:37:18 -0700399 auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pNewXObject);
400 pAcc->LoadAllData();
401 CFX_ByteString sStream(pAcc->GetData(), pAcc->GetSize());
tsepezd0ecd892016-11-08 17:30:04 -0800402 CFX_Matrix matrix = pAPDic->GetMatrixFor("Matrix");
Tom Sepez60d909e2015-12-10 15:34:55 -0800403 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix);
Lei Zhangd3610052017-06-19 12:25:54 -0700404 CFX_ByteString sTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
406 sFormName.c_str());
407 sStream += sTemp;
tsepeze6db16e2016-09-19 10:45:09 -0700408 pNewXObject->SetData(sStream.raw_str(), sStream.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 }
dsinclair38fd8442016-09-15 10:15:32 -0700410 pPageDict->RemoveFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700412}