blob: f39a50aad4b73728aff34ab281c7290ae0f24e56 [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>
10
dsinclair41872fa2016-10-04 11:29:35 -070011#include "core/fpdfapi/page/cpdf_page.h"
12#include "core/fpdfapi/page/cpdf_pageobject.h"
dsinclair488b7ad2016-10-04 11:55:50 -070013#include "core/fpdfapi/parser/cpdf_array.h"
14#include "core/fpdfapi/parser/cpdf_document.h"
15#include "core/fpdfapi/parser/cpdf_number.h"
16#include "core/fpdfapi/parser/cpdf_stream.h"
17#include "core/fpdfapi/parser/cpdf_stream_acc.h"
dsinclair1727aee2016-09-29 13:12:56 -070018#include "core/fpdfdoc/cpdf_annot.h"
dsinclair114e46a2016-09-29 17:18:21 -070019#include "fpdfsdk/fsdk_define.h"
tsepez74b8c6e2016-10-12 09:38:41 -070020#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070021
Tom Sepez281a9ea2016-02-26 14:24:28 -080022typedef CFX_ArrayTemplate<CFX_FloatRect> CPDF_RectArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023
24enum FPDF_TYPE { MAX, MIN };
25enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM };
26
tsepez381fc832016-10-10 14:06:44 -070027namespace {
28
tsepez4cf55152016-11-02 14:37:54 -070029bool IsValiableRect(CFX_FloatRect rect, CFX_FloatRect rcPage) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f)
tsepez4cf55152016-11-02 14:37:54 -070031 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070032
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f &&
34 rect.bottom == 0.0f)
tsepez4cf55152016-11-02 14:37:54 -070035 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070036
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037 if (!rcPage.IsEmpty()) {
38 if (rect.left - rcPage.left < -10.000001f ||
39 rect.right - rcPage.right > 10.000001f ||
40 rect.top - rcPage.top > 10.000001f ||
41 rect.bottom - rcPage.bottom < -10.000001f)
tsepez4cf55152016-11-02 14:37:54 -070042 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070044
tsepez4cf55152016-11-02 14:37:54 -070045 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046}
47
Tom Sepez2398d892016-02-17 16:46:26 -080048void GetContentsRect(CPDF_Document* pDoc,
49 CPDF_Dictionary* pDict,
50 CPDF_RectArray* pRectArray) {
thestig5cc24652016-04-26 11:46:02 -070051 std::unique_ptr<CPDF_Page> pPDFPage(new CPDF_Page(pDoc, pDict, false));
52 pPDFPage->ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070053
thestig2fad11a2016-06-16 16:39:25 -070054 for (const auto& pPageObject : *pPDFPage->GetPageObjectList()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -080055 CFX_FloatRect rc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 rc.left = pPageObject->m_Left;
57 rc.right = pPageObject->m_Right;
58 rc.bottom = pPageObject->m_Bottom;
59 rc.top = pPageObject->m_Top;
dsinclair38fd8442016-09-15 10:15:32 -070060 if (IsValiableRect(rc, pDict->GetRectFor("MediaBox")))
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 pRectArray->Add(rc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063}
64
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065void ParserStream(CPDF_Dictionary* pPageDic,
66 CPDF_Dictionary* pStream,
67 CPDF_RectArray* pRectArray,
tsepez74b8c6e2016-10-12 09:38:41 -070068 std::vector<CPDF_Dictionary*>* pObjectArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 if (!pStream)
70 return;
Tom Sepez281a9ea2016-02-26 14:24:28 -080071 CFX_FloatRect rect;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 if (pStream->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -070073 rect = pStream->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 else if (pStream->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -070075 rect = pStream->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076
dsinclair38fd8442016-09-15 10:15:32 -070077 if (IsValiableRect(rect, pPageDic->GetRectFor("MediaBox")))
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 pRectArray->Add(rect);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070079
tsepez74b8c6e2016-10-12 09:38:41 -070080 pObjectArray->push_back(pStream);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070081}
82
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083int ParserAnnots(CPDF_Document* pSourceDoc,
84 CPDF_Dictionary* pPageDic,
85 CPDF_RectArray* pRectArray,
tsepez74b8c6e2016-10-12 09:38:41 -070086 std::vector<CPDF_Dictionary*>* pObjectArray,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 int nUsage) {
88 if (!pSourceDoc || !pPageDic)
89 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 GetContentsRect(pSourceDoc, pPageDic, pRectArray);
dsinclair38fd8442016-09-15 10:15:32 -070092 CPDF_Array* pAnnots = pPageDic->GetArrayFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 if (!pAnnots)
94 return FLATTEN_NOTHINGTODO;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070095
tsepezc3255f52016-03-25 14:52:27 -070096 uint32_t dwSize = pAnnots->GetCount();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 for (int i = 0; i < (int)dwSize; i++) {
tsepezbd567552016-03-29 14:51:50 -070098 CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnots->GetDirectObjectAt(i));
Dan Sinclairf1251c12015-10-20 16:24:45 -040099 if (!pAnnotDic)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700101
dsinclair38fd8442016-09-15 10:15:32 -0700102 CFX_ByteString sSubtype = pAnnotDic->GetStringFor("Subtype");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 if (sSubtype == "Popup")
104 continue;
105
dsinclair38fd8442016-09-15 10:15:32 -0700106 int nAnnotFlag = pAnnotDic->GetIntegerFor("F");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 if (nAnnotFlag & ANNOTFLAG_HIDDEN)
108 continue;
109
110 if (nUsage == FLAT_NORMALDISPLAY) {
111 if (nAnnotFlag & ANNOTFLAG_INVISIBLE)
112 continue;
113
114 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
115 } else {
116 if (nAnnotFlag & ANNOTFLAG_PRINT)
117 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
118 }
119 }
120 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700121}
122
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123FX_FLOAT GetMinMaxValue(CPDF_RectArray& array,
124 FPDF_TYPE type,
125 FPDF_VALUE value) {
126 int nRects = array.GetSize();
127 FX_FLOAT fRet = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700128
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 if (nRects <= 0)
130 return 0.0f;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 FX_FLOAT* pArray = new FX_FLOAT[nRects];
133 switch (value) {
134 case LEFT: {
135 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800136 pArray[i] = CFX_FloatRect(array.GetAt(i)).left;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 break;
139 }
140 case TOP: {
141 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800142 pArray[i] = CFX_FloatRect(array.GetAt(i)).top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700143
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 break;
145 }
146 case RIGHT: {
147 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800148 pArray[i] = CFX_FloatRect(array.GetAt(i)).right;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700149
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 break;
151 }
152 case BOTTOM: {
153 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800154 pArray[i] = CFX_FloatRect(array.GetAt(i)).bottom;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700155
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 break;
157 }
158 default:
weili12367cb2016-06-03 11:22:16 -0700159 // Not reachable.
160 return 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 }
162 fRet = pArray[0];
163 if (type == MAX) {
164 for (int i = 1; i < nRects; i++)
165 if (fRet <= pArray[i])
166 fRet = pArray[i];
167 } else {
168 for (int i = 1; i < nRects; i++)
169 if (fRet >= pArray[i])
170 fRet = pArray[i];
171 }
172 delete[] pArray;
173 return fRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700175
Tom Sepez281a9ea2016-02-26 14:24:28 -0800176CFX_FloatRect CalculateRect(CPDF_RectArray* pRectArray) {
177 CFX_FloatRect rcRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700178
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT);
180 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP);
181 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT);
182 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700183
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 return rcRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700185}
186
tsepez381fc832016-10-10 14:06:44 -0700187uint32_t NewIndirectContentsStream(const CFX_ByteString& key,
188 CPDF_Document* pDocument) {
Tom Sepezc25a4212016-10-14 17:45:56 -0700189 CPDF_Stream* pNewContents = new CPDF_Stream(
tsepez381fc832016-10-10 14:06:44 -0700190 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool()));
191 CFX_ByteString sStream;
192 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
193 pNewContents->SetData(sStream.raw_str(), sStream.GetLength());
Tom Sepezc25a4212016-10-14 17:45:56 -0700194 return pDocument->AddIndirectObject(pNewContents);
tsepez381fc832016-10-10 14:06:44 -0700195}
196
197void SetPageContents(const CFX_ByteString& key,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 CPDF_Dictionary* pPage,
199 CPDF_Document* pDocument) {
thestig1cd352e2016-06-07 17:53:06 -0700200 CPDF_Array* pContentsArray = nullptr;
tsepez381fc832016-10-10 14:06:44 -0700201 CPDF_Stream* pContentsStream = pPage->GetStreamFor("Contents");
202 if (!pContentsStream) {
203 pContentsArray = pPage->GetArrayFor("Contents");
204 if (!pContentsArray) {
205 if (!key.IsEmpty()) {
206 pPage->SetReferenceFor("Contents", pDocument,
207 NewIndirectContentsStream(key, pDocument));
208 }
209 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 }
tsepez381fc832016-10-10 14:06:44 -0700212 pPage->ConvertToIndirectObjectFor("Contents", pDocument);
213 if (!pContentsArray) {
Tom Sepezc25a4212016-10-14 17:45:56 -0700214 pContentsArray = new CPDF_Array;
tsepez381fc832016-10-10 14:06:44 -0700215 CPDF_StreamAcc acc;
216 acc.LoadAllData(pContentsStream);
217 CFX_ByteString sStream = "q\n";
Tom Sepezc25a4212016-10-14 17:45:56 -0700218 CFX_ByteString sBody =
219 CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize());
tsepez381fc832016-10-10 14:06:44 -0700220 sStream = sStream + sBody + "\nQ";
221 pContentsStream->SetData(sStream.raw_str(), sStream.GetLength());
222 pContentsArray->AddReference(pDocument, pContentsStream->GetObjNum());
Tom Sepezc25a4212016-10-14 17:45:56 -0700223 pPage->SetReferenceFor("Contents", pDocument,
224 pDocument->AddIndirectObject(pContentsArray));
tsepez381fc832016-10-10 14:06:44 -0700225 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 if (!key.IsEmpty()) {
tsepezbb577af2016-09-21 19:10:19 -0700227 pContentsArray->AddReference(pDocument,
tsepez381fc832016-10-10 14:06:44 -0700228 NewIndirectContentsStream(key, pDocument));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700230}
231
Tom Sepez281a9ea2016-02-26 14:24:28 -0800232CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot,
233 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800234 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 if (rcStream.IsEmpty())
Tom Sepez60d909e2015-12-10 15:34:55 -0800236 return CFX_Matrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 matrix.TransformRect(rcStream);
239 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700240
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
242 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700243
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
245 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
Tom Sepez60d909e2015-12-10 15:34:55 -0800246 return CFX_Matrix(a, 0, 0, d, e, f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700248
tsepez381fc832016-10-10 14:06:44 -0700249} // namespace
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700250
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700252 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 if (!page) {
254 return FLATTEN_FAIL;
255 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700256
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 CPDF_Document* pDocument = pPage->m_pDocument;
258 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260 if (!pDocument || !pPageDict) {
261 return FLATTEN_FAIL;
262 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263
tsepez74b8c6e2016-10-12 09:38:41 -0700264 std::vector<CPDF_Dictionary*> ObjectArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 CPDF_RectArray RectArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700266
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 int iRet = FLATTEN_FAIL;
268 iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
269 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
270 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700271
Tom Sepez281a9ea2016-02-26 14:24:28 -0800272 CFX_FloatRect rcOriginalCB;
273 CFX_FloatRect rcMerger = CalculateRect(&RectArray);
dsinclair38fd8442016-09-15 10:15:32 -0700274 CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 if (pPageDict->KeyExist("CropBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700277 rcOriginalMB = pPageDict->GetRectFor("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 if (rcOriginalMB.IsEmpty()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800280 rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 rcMerger.left =
284 rcMerger.left < rcOriginalMB.left ? rcOriginalMB.left : rcMerger.left;
285 rcMerger.right =
286 rcMerger.right > rcOriginalMB.right ? rcOriginalMB.right : rcMerger.right;
287 rcMerger.top =
288 rcMerger.top > rcOriginalMB.top ? rcOriginalMB.top : rcMerger.top;
289 rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom ? rcOriginalMB.bottom
290 : rcMerger.bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 if (pPageDict->KeyExist("ArtBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700293 rcOriginalCB = pPageDict->GetRectFor("ArtBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 else
295 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700296
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297 if (!rcOriginalMB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700298 CPDF_Array* pMediaBox = new CPDF_Array();
Tom Sepezae51c812015-08-05 12:34:06 -0700299 pMediaBox->Add(new CPDF_Number(rcOriginalMB.left));
300 pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom));
301 pMediaBox->Add(new CPDF_Number(rcOriginalMB.right));
302 pMediaBox->Add(new CPDF_Number(rcOriginalMB.top));
dsinclair38fd8442016-09-15 10:15:32 -0700303 pPageDict->SetFor("MediaBox", pMediaBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700305
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700306 if (!rcOriginalCB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700307 CPDF_Array* pCropBox = new CPDF_Array();
308 pCropBox->Add(new CPDF_Number(rcOriginalCB.left));
309 pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom));
310 pCropBox->Add(new CPDF_Number(rcOriginalCB.right));
311 pCropBox->Add(new CPDF_Number(rcOriginalCB.top));
dsinclair38fd8442016-09-15 10:15:32 -0700312 pPageDict->SetFor("ArtBox", pCropBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700314
dsinclair38fd8442016-09-15 10:15:32 -0700315 CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316 if (!pRes) {
tsepez698c5712016-09-28 16:47:07 -0700317 pRes = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700318 pPageDict->SetFor("Resources", pRes);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700320
Tom Sepezc25a4212016-10-14 17:45:56 -0700321 CPDF_Stream* pNewXObject = new CPDF_Stream(
tsepez698c5712016-09-28 16:47:07 -0700322 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool()));
323
Tom Sepezc25a4212016-10-14 17:45:56 -0700324 uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject);
dsinclair38fd8442016-09-15 10:15:32 -0700325 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 if (!pPageXObject) {
tsepez698c5712016-09-28 16:47:07 -0700327 pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700328 pRes->SetFor("XObject", pPageXObject);
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 CFX_ByteString key = "";
tsepez74b8c6e2016-10-12 09:38:41 -0700332 int nStreams = pdfium::CollectionSize<int>(ObjectArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700333
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 if (nStreams > 0) {
335 for (int iKey = 0; /*iKey < 100*/; iKey++) {
336 char sExtend[5] = {};
337 FXSYS_itoa(iKey, sExtend, 10);
338 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
tsepez7b1ccf92016-04-14 11:04:57 -0700339 if (!pPageXObject->KeyExist(key))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 break;
341 }
342 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700343
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700345
thestig1cd352e2016-06-07 17:53:06 -0700346 CPDF_Dictionary* pNewXORes = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700347
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348 if (!key.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700349 pPageXObject->SetReferenceFor(key, pDocument, dwObjNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700350 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
tsepez698c5712016-09-28 16:47:07 -0700351 pNewXORes = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700352 pNewOXbjectDic->SetFor("Resources", pNewXORes);
353 pNewOXbjectDic->SetNameFor("Type", "XObject");
354 pNewOXbjectDic->SetNameFor("Subtype", "Form");
355 pNewOXbjectDic->SetIntegerFor("FormType", 1);
356 pNewOXbjectDic->SetNameFor("Name", "FRM");
357 CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox");
358 pNewOXbjectDic->SetRectFor("BBox", rcBBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700360
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361 for (int i = 0; i < nStreams; i++) {
tsepez74b8c6e2016-10-12 09:38:41 -0700362 CPDF_Dictionary* pAnnotDic = ObjectArray[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363 if (!pAnnotDic)
364 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700365
dsinclair38fd8442016-09-15 10:15:32 -0700366 CFX_FloatRect rcAnnot = pAnnotDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700368
dsinclair38fd8442016-09-15 10:15:32 -0700369 CFX_ByteString sAnnotState = pAnnotDic->GetStringFor("AS");
370 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictFor("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 if (!pAnnotAP)
372 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700373
dsinclair38fd8442016-09-15 10:15:32 -0700374 CPDF_Stream* pAPStream = pAnnotAP->GetStreamFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 if (!pAPStream) {
dsinclair38fd8442016-09-15 10:15:32 -0700376 CPDF_Dictionary* pAPDic = pAnnotAP->GetDictFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377 if (!pAPDic)
378 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700379
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380 if (!sAnnotState.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700381 pAPStream = pAPDic->GetStreamFor(sAnnotState);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382 } else {
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800383 auto it = pAPDic->begin();
384 if (it != pAPDic->end()) {
tsepezd5bd8a12016-10-17 11:13:54 -0700385 CPDF_Object* pFirstObj = it->second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386 if (pFirstObj) {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400387 if (pFirstObj->IsReference())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388 pFirstObj = pFirstObj->GetDirect();
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400389 if (!pFirstObj->IsStream())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 continue;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400391 pAPStream = pFirstObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392 }
393 }
394 }
395 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 if (!pAPStream)
397 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700398
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800400 CFX_FloatRect rcStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401 if (pAPDic->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -0700402 rcStream = pAPDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 else if (pAPDic->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700404 rcStream = pAPDic->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700405
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 if (rcStream.IsEmpty())
407 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700408
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 CPDF_Object* pObj = pAPStream;
tsepezd0ecd892016-11-08 17:30:04 -0800410 if (pObj->IsInline()) {
tsepez335cf092016-11-09 13:28:26 -0800411 std::unique_ptr<CPDF_Object> pNew = pObj->Clone();
412 pObj = pNew.get();
413 pDocument->AddIndirectObject(pNew.release());
tsepezd0ecd892016-11-08 17:30:04 -0800414 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700415
tsepezd0ecd892016-11-08 17:30:04 -0800416 CPDF_Dictionary* pObjDic = pObj->GetDict();
417 if (pObjDic) {
418 pObjDic->SetNameFor("Type", "XObject");
419 pObjDic->SetNameFor("Subtype", "Form");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700420 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700421
dsinclair38fd8442016-09-15 10:15:32 -0700422 CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423 if (!pXObject) {
tsepez698c5712016-09-28 16:47:07 -0700424 pXObject = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700425 pNewXORes->SetFor("XObject", pXObject);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 }
427
428 CFX_ByteString sFormName;
429 sFormName.Format("F%d", i);
tsepezd0ecd892016-11-08 17:30:04 -0800430 pXObject->SetReferenceFor(sFormName, pDocument, pObj->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431
432 CPDF_StreamAcc acc;
433 acc.LoadAllData(pNewXObject);
434
435 const uint8_t* pData = acc.GetData();
436 CFX_ByteString sStream(pData, acc.GetSize());
tsepezd0ecd892016-11-08 17:30:04 -0800437 CFX_Matrix matrix = pAPDic->GetMatrixFor("Matrix");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700438 if (matrix.IsIdentity()) {
439 matrix.a = 1.0f;
440 matrix.b = 0.0f;
441 matrix.c = 0.0f;
442 matrix.d = 1.0f;
443 matrix.e = 0.0f;
444 matrix.f = 0.0f;
445 }
446
tsepezbb577af2016-09-21 19:10:19 -0700447 CFX_ByteString sTemp;
Tom Sepez60d909e2015-12-10 15:34:55 -0800448 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
450 sFormName.c_str());
451 sStream += sTemp;
tsepeze6db16e2016-09-19 10:45:09 -0700452 pNewXObject->SetData(sStream.raw_str(), sStream.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 }
dsinclair38fd8442016-09-15 10:15:32 -0700454 pPageDict->RemoveFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 RectArray.RemoveAll();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700458}