blob: aa21b280edcc5d3a5ae85a656ec7a85ec4b44aaa [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
Dan Sinclair455a4192016-03-16 09:48:56 -040011#include "core/fpdfapi/fpdf_page/include/cpdf_page.h"
Dan Sinclair584b1e62016-03-21 09:15:45 -040012#include "core/fpdfapi/fpdf_page/include/cpdf_pageobject.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040013#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
14#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
15#include "core/fpdfapi/fpdf_parser/include/cpdf_number.h"
dan sinclair61b2fc72016-03-23 19:21:44 -040016#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h"
17#include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h"
dsinclaircac704d2016-07-28 12:59:09 -070018#include "core/fpdfdoc/include/cpdf_annot.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080019#include "fpdfsdk/include/fsdk_define.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
21typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray;
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
Tom Sepez281a9ea2016-02-26 14:24:28 -080027FX_BOOL IsValiableRect(CFX_FloatRect rect, CFX_FloatRect rcPage) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f)
29 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070030
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031 if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f &&
32 rect.bottom == 0.0f)
33 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070034
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035 if (!rcPage.IsEmpty()) {
36 if (rect.left - rcPage.left < -10.000001f ||
37 rect.right - rcPage.right > 10.000001f ||
38 rect.top - rcPage.top > 10.000001f ||
39 rect.bottom - rcPage.bottom < -10.000001f)
40 return FALSE;
41 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070042
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044}
45
Tom Sepez2398d892016-02-17 16:46:26 -080046void GetContentsRect(CPDF_Document* pDoc,
47 CPDF_Dictionary* pDict,
48 CPDF_RectArray* pRectArray) {
thestig5cc24652016-04-26 11:46:02 -070049 std::unique_ptr<CPDF_Page> pPDFPage(new CPDF_Page(pDoc, pDict, false));
50 pPDFPage->ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
thestig2fad11a2016-06-16 16:39:25 -070052 for (const auto& pPageObject : *pPDFPage->GetPageObjectList()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -080053 CFX_FloatRect rc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 rc.left = pPageObject->m_Left;
55 rc.right = pPageObject->m_Right;
56 rc.bottom = pPageObject->m_Bottom;
57 rc.top = pPageObject->m_Top;
dsinclair38fd8442016-09-15 10:15:32 -070058 if (IsValiableRect(rc, pDict->GetRectFor("MediaBox")))
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 pRectArray->Add(rc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061}
62
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063void ParserStream(CPDF_Dictionary* pPageDic,
64 CPDF_Dictionary* pStream,
65 CPDF_RectArray* pRectArray,
66 CPDF_ObjectArray* pObjectArray) {
67 if (!pStream)
68 return;
Tom Sepez281a9ea2016-02-26 14:24:28 -080069 CFX_FloatRect rect;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 if (pStream->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -070071 rect = pStream->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 else if (pStream->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -070073 rect = pStream->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074
dsinclair38fd8442016-09-15 10:15:32 -070075 if (IsValiableRect(rect, pPageDic->GetRectFor("MediaBox")))
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 pRectArray->Add(rect);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070077
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 pObjectArray->Add(pStream);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079}
80
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081int ParserAnnots(CPDF_Document* pSourceDoc,
82 CPDF_Dictionary* pPageDic,
83 CPDF_RectArray* pRectArray,
84 CPDF_ObjectArray* pObjectArray,
85 int nUsage) {
86 if (!pSourceDoc || !pPageDic)
87 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 GetContentsRect(pSourceDoc, pPageDic, pRectArray);
dsinclair38fd8442016-09-15 10:15:32 -070090 CPDF_Array* pAnnots = pPageDic->GetArrayFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 if (!pAnnots)
92 return FLATTEN_NOTHINGTODO;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070093
tsepezc3255f52016-03-25 14:52:27 -070094 uint32_t dwSize = pAnnots->GetCount();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 for (int i = 0; i < (int)dwSize; i++) {
tsepezbd567552016-03-29 14:51:50 -070096 CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnots->GetDirectObjectAt(i));
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
108 if (nUsage == FLAT_NORMALDISPLAY) {
109 if (nAnnotFlag & ANNOTFLAG_INVISIBLE)
110 continue;
111
112 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
113 } else {
114 if (nAnnotFlag & ANNOTFLAG_PRINT)
115 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
116 }
117 }
118 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700119}
120
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121FX_FLOAT GetMinMaxValue(CPDF_RectArray& array,
122 FPDF_TYPE type,
123 FPDF_VALUE value) {
124 int nRects = array.GetSize();
125 FX_FLOAT fRet = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700126
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 if (nRects <= 0)
128 return 0.0f;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 FX_FLOAT* pArray = new FX_FLOAT[nRects];
131 switch (value) {
132 case LEFT: {
133 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800134 pArray[i] = CFX_FloatRect(array.GetAt(i)).left;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 break;
137 }
138 case TOP: {
139 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800140 pArray[i] = CFX_FloatRect(array.GetAt(i)).top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142 break;
143 }
144 case RIGHT: {
145 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800146 pArray[i] = CFX_FloatRect(array.GetAt(i)).right;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700147
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148 break;
149 }
150 case BOTTOM: {
151 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800152 pArray[i] = CFX_FloatRect(array.GetAt(i)).bottom;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 break;
155 }
156 default:
weili12367cb2016-06-03 11:22:16 -0700157 // Not reachable.
158 return 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 }
160 fRet = pArray[0];
161 if (type == MAX) {
162 for (int i = 1; i < nRects; i++)
163 if (fRet <= pArray[i])
164 fRet = pArray[i];
165 } else {
166 for (int i = 1; i < nRects; i++)
167 if (fRet >= pArray[i])
168 fRet = pArray[i];
169 }
170 delete[] pArray;
171 return fRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700172}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700173
Tom Sepez281a9ea2016-02-26 14:24:28 -0800174CFX_FloatRect CalculateRect(CPDF_RectArray* pRectArray) {
175 CFX_FloatRect rcRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700176
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT);
178 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP);
179 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT);
180 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700181
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 return rcRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183}
184
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185void SetPageContents(CFX_ByteString key,
186 CPDF_Dictionary* pPage,
187 CPDF_Document* pDocument) {
dsinclair38fd8442016-09-15 10:15:32 -0700188 CPDF_Object* pContentsObj = pPage->GetStreamFor("Contents");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189 if (!pContentsObj) {
dsinclair38fd8442016-09-15 10:15:32 -0700190 pContentsObj = pPage->GetArrayFor("Contents");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 if (!pContentsObj) {
194 // Create a new contents dictionary
195 if (!key.IsEmpty()) {
thestig1cd352e2016-06-07 17:53:06 -0700196 CPDF_Stream* pNewContents =
197 new CPDF_Stream(nullptr, 0, new CPDF_Dictionary);
dsinclair38fd8442016-09-15 10:15:32 -0700198 pPage->SetReferenceFor("Contents", pDocument,
199 pDocument->AddIndirectObject(pNewContents));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 CFX_ByteString sStream;
202 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
tsepeze6db16e2016-09-19 10:45:09 -0700203 pNewContents->SetData(sStream.raw_str(), sStream.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 }
205 return;
206 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700207
thestig1cd352e2016-06-07 17:53:06 -0700208 CPDF_Array* pContentsArray = nullptr;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700209
Tom Sepez8e5cd192016-01-26 13:20:26 -0800210 switch (pContentsObj->GetType()) {
211 case CPDF_Object::STREAM: {
Tom Sepezae51c812015-08-05 12:34:06 -0700212 pContentsArray = new CPDF_Array;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400213 CPDF_Stream* pContents = pContentsObj->AsStream();
tsepezc3255f52016-03-25 14:52:27 -0700214 uint32_t dwObjNum = pDocument->AddIndirectObject(pContents);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 CPDF_StreamAcc acc;
216 acc.LoadAllData(pContents);
217 CFX_ByteString sStream = "q\n";
218 CFX_ByteString sBody =
219 CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize());
220 sStream = sStream + sBody + "\nQ";
tsepeze6db16e2016-09-19 10:45:09 -0700221 pContents->SetData(sStream.raw_str(), sStream.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 pContentsArray->AddReference(pDocument, dwObjNum);
223 break;
224 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700225
Tom Sepez8e5cd192016-01-26 13:20:26 -0800226 case CPDF_Object::ARRAY: {
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400227 pContentsArray = pContentsObj->AsArray();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 break;
229 }
230 default:
231 break;
232 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700233
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 if (!pContentsArray)
235 return;
236
tsepezc3255f52016-03-25 14:52:27 -0700237 uint32_t dwObjNum = pDocument->AddIndirectObject(pContentsArray);
dsinclair38fd8442016-09-15 10:15:32 -0700238 pPage->SetReferenceFor("Contents", pDocument, dwObjNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239
240 if (!key.IsEmpty()) {
thestig1cd352e2016-06-07 17:53:06 -0700241 CPDF_Stream* pNewContents =
242 new CPDF_Stream(nullptr, 0, new CPDF_Dictionary);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 dwObjNum = pDocument->AddIndirectObject(pNewContents);
244 pContentsArray->AddReference(pDocument, dwObjNum);
245
246 CFX_ByteString sStream;
247 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
tsepeze6db16e2016-09-19 10:45:09 -0700248 pNewContents->SetData(sStream.raw_str(), sStream.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250}
251
Tom Sepez281a9ea2016-02-26 14:24:28 -0800252CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot,
253 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800254 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255 if (rcStream.IsEmpty())
Tom Sepez60d909e2015-12-10 15:34:55 -0800256 return CFX_Matrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700257
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 matrix.TransformRect(rcStream);
259 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
262 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700263
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
265 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
Tom Sepez60d909e2015-12-10 15:34:55 -0800266 return CFX_Matrix(a, 0, 0, d, e, f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700268
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269void GetOffset(FX_FLOAT& fa,
270 FX_FLOAT& fd,
271 FX_FLOAT& fe,
272 FX_FLOAT& ff,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800273 CFX_FloatRect rcAnnot,
274 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800275 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 FX_FLOAT fStreamWidth = 0.0f;
277 FX_FLOAT fStreamHeight = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 if (matrix.a != 0 && matrix.d != 0) {
280 fStreamWidth = rcStream.right - rcStream.left;
281 fStreamHeight = rcStream.top - rcStream.bottom;
282 } else {
283 fStreamWidth = rcStream.top - rcStream.bottom;
284 fStreamHeight = rcStream.right - rcStream.left;
285 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700286
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 FX_FLOAT x1 =
288 matrix.a * rcStream.left + matrix.c * rcStream.bottom + matrix.e;
289 FX_FLOAT y1 =
290 matrix.b * rcStream.left + matrix.d * rcStream.bottom + matrix.f;
291 FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matrix.e;
292 FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matrix.f;
293 FX_FLOAT x3 =
294 matrix.a * rcStream.right + matrix.c * rcStream.bottom + matrix.e;
295 FX_FLOAT y3 =
296 matrix.b * rcStream.right + matrix.d * rcStream.bottom + matrix.f;
297 FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matrix.e;
298 FX_FLOAT y4 = matrix.b * rcStream.right + matrix.d * rcStream.top + matrix.f;
Tom Sepez3c3201f2015-05-20 10:20:35 -0700299
Lei Zhang375a8642016-01-11 11:59:17 -0800300 FX_FLOAT left = std::min(std::min(x1, x2), std::min(x3, x4));
301 FX_FLOAT bottom = std::min(std::min(y1, y2), std::min(y3, y4));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700302
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 fa = (rcAnnot.right - rcAnnot.left) / fStreamWidth;
304 fd = (rcAnnot.top - rcAnnot.bottom) / fStreamHeight;
305 fe = rcAnnot.left - left * fa;
306 ff = rcAnnot.bottom - bottom * fd;
307}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700308
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700310 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 if (!page) {
312 return FLATTEN_FAIL;
313 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700314
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 CPDF_Document* pDocument = pPage->m_pDocument;
316 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700317
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318 if (!pDocument || !pPageDict) {
319 return FLATTEN_FAIL;
320 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700321
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 CPDF_ObjectArray ObjectArray;
323 CPDF_RectArray RectArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700324
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 int iRet = FLATTEN_FAIL;
326 iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
327 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
328 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700329
Tom Sepez281a9ea2016-02-26 14:24:28 -0800330 CFX_FloatRect rcOriginalCB;
331 CFX_FloatRect rcMerger = CalculateRect(&RectArray);
dsinclair38fd8442016-09-15 10:15:32 -0700332 CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700333
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 if (pPageDict->KeyExist("CropBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700335 rcOriginalMB = pPageDict->GetRectFor("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700336
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 if (rcOriginalMB.IsEmpty()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800338 rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700339 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700340
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 rcMerger.left =
342 rcMerger.left < rcOriginalMB.left ? rcOriginalMB.left : rcMerger.left;
343 rcMerger.right =
344 rcMerger.right > rcOriginalMB.right ? rcOriginalMB.right : rcMerger.right;
345 rcMerger.top =
346 rcMerger.top > rcOriginalMB.top ? rcOriginalMB.top : rcMerger.top;
347 rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom ? rcOriginalMB.bottom
348 : rcMerger.bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700349
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700350 if (pPageDict->KeyExist("ArtBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700351 rcOriginalCB = pPageDict->GetRectFor("ArtBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352 else
353 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700354
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 if (!rcOriginalMB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700356 CPDF_Array* pMediaBox = new CPDF_Array();
Tom Sepezae51c812015-08-05 12:34:06 -0700357 pMediaBox->Add(new CPDF_Number(rcOriginalMB.left));
358 pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom));
359 pMediaBox->Add(new CPDF_Number(rcOriginalMB.right));
360 pMediaBox->Add(new CPDF_Number(rcOriginalMB.top));
dsinclair38fd8442016-09-15 10:15:32 -0700361 pPageDict->SetFor("MediaBox", pMediaBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700363
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364 if (!rcOriginalCB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700365 CPDF_Array* pCropBox = new CPDF_Array();
366 pCropBox->Add(new CPDF_Number(rcOriginalCB.left));
367 pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom));
368 pCropBox->Add(new CPDF_Number(rcOriginalCB.right));
369 pCropBox->Add(new CPDF_Number(rcOriginalCB.top));
dsinclair38fd8442016-09-15 10:15:32 -0700370 pPageDict->SetFor("ArtBox", pCropBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700372
dsinclair38fd8442016-09-15 10:15:32 -0700373 CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374 if (!pRes) {
Tom Sepezae51c812015-08-05 12:34:06 -0700375 pRes = new CPDF_Dictionary;
dsinclair38fd8442016-09-15 10:15:32 -0700376 pPageDict->SetFor("Resources", pRes);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700378
thestig1cd352e2016-06-07 17:53:06 -0700379 CPDF_Stream* pNewXObject = new CPDF_Stream(nullptr, 0, new CPDF_Dictionary);
tsepezc3255f52016-03-25 14:52:27 -0700380 uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject);
dsinclair38fd8442016-09-15 10:15:32 -0700381 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382 if (!pPageXObject) {
Tom Sepezae51c812015-08-05 12:34:06 -0700383 pPageXObject = new CPDF_Dictionary;
dsinclair38fd8442016-09-15 10:15:32 -0700384 pRes->SetFor("XObject", pPageXObject);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700386
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387 CFX_ByteString key = "";
388 int nStreams = ObjectArray.GetSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700389
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 if (nStreams > 0) {
391 for (int iKey = 0; /*iKey < 100*/; iKey++) {
392 char sExtend[5] = {};
393 FXSYS_itoa(iKey, sExtend, 10);
394 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
tsepez7b1ccf92016-04-14 11:04:57 -0700395 if (!pPageXObject->KeyExist(key))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 break;
397 }
398 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700399
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700401
thestig1cd352e2016-06-07 17:53:06 -0700402 CPDF_Dictionary* pNewXORes = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700403
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404 if (!key.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700405 pPageXObject->SetReferenceFor(key, pDocument, dwObjNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
Tom Sepezae51c812015-08-05 12:34:06 -0700407 pNewXORes = new CPDF_Dictionary;
dsinclair38fd8442016-09-15 10:15:32 -0700408 pNewOXbjectDic->SetFor("Resources", pNewXORes);
409 pNewOXbjectDic->SetNameFor("Type", "XObject");
410 pNewOXbjectDic->SetNameFor("Subtype", "Form");
411 pNewOXbjectDic->SetIntegerFor("FormType", 1);
412 pNewOXbjectDic->SetNameFor("Name", "FRM");
413 CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox");
414 pNewOXbjectDic->SetRectFor("BBox", rcBBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700416
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700417 for (int i = 0; i < nStreams; i++) {
418 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i);
419 if (!pAnnotDic)
420 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700421
dsinclair38fd8442016-09-15 10:15:32 -0700422 CFX_FloatRect rcAnnot = pAnnotDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700424
dsinclair38fd8442016-09-15 10:15:32 -0700425 CFX_ByteString sAnnotState = pAnnotDic->GetStringFor("AS");
426 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictFor("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427 if (!pAnnotAP)
428 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700429
dsinclair38fd8442016-09-15 10:15:32 -0700430 CPDF_Stream* pAPStream = pAnnotAP->GetStreamFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 if (!pAPStream) {
dsinclair38fd8442016-09-15 10:15:32 -0700432 CPDF_Dictionary* pAPDic = pAnnotAP->GetDictFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 if (!pAPDic)
434 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700435
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436 if (!sAnnotState.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700437 pAPStream = pAPDic->GetStreamFor(sAnnotState);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700438 } else {
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800439 auto it = pAPDic->begin();
440 if (it != pAPDic->end()) {
441 CPDF_Object* pFirstObj = it->second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 if (pFirstObj) {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400443 if (pFirstObj->IsReference())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444 pFirstObj = pFirstObj->GetDirect();
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400445 if (!pFirstObj->IsStream())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 continue;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400447 pAPStream = pFirstObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 }
449 }
450 }
451 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 if (!pAPStream)
453 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700454
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
dsinclair38fd8442016-09-15 10:15:32 -0700456 CFX_Matrix matrix = pAPDic->GetMatrixFor("Matrix");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700457
Tom Sepez281a9ea2016-02-26 14:24:28 -0800458 CFX_FloatRect rcStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459 if (pAPDic->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -0700460 rcStream = pAPDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461 else if (pAPDic->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700462 rcStream = pAPDic->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700463
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700464 if (rcStream.IsEmpty())
465 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700466
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467 CPDF_Object* pObj = pAPStream;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700468
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469 if (pObj) {
470 CPDF_Dictionary* pObjDic = pObj->GetDict();
471 if (pObjDic) {
dsinclair38fd8442016-09-15 10:15:32 -0700472 pObjDic->SetNameFor("Type", "XObject");
473 pObjDic->SetNameFor("Subtype", "Form");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474 }
475 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700476
dsinclair38fd8442016-09-15 10:15:32 -0700477 CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478 if (!pXObject) {
Tom Sepezae51c812015-08-05 12:34:06 -0700479 pXObject = new CPDF_Dictionary;
dsinclair38fd8442016-09-15 10:15:32 -0700480 pNewXORes->SetFor("XObject", pXObject);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481 }
482
483 CFX_ByteString sFormName;
484 sFormName.Format("F%d", i);
weilidb444d22016-06-02 15:48:15 -0700485 uint32_t dwStreamObjNum = pDocument->AddIndirectObject(pObj);
dsinclair38fd8442016-09-15 10:15:32 -0700486 pXObject->SetReferenceFor(sFormName, pDocument, dwStreamObjNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700487
488 CPDF_StreamAcc acc;
489 acc.LoadAllData(pNewXObject);
490
491 const uint8_t* pData = acc.GetData();
492 CFX_ByteString sStream(pData, acc.GetSize());
493 CFX_ByteString sTemp;
494
495 if (matrix.IsIdentity()) {
496 matrix.a = 1.0f;
497 matrix.b = 0.0f;
498 matrix.c = 0.0f;
499 matrix.d = 1.0f;
500 matrix.e = 0.0f;
501 matrix.f = 0.0f;
502 }
503
Tom Sepez60d909e2015-12-10 15:34:55 -0800504 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
506 sFormName.c_str());
507 sStream += sTemp;
tsepeze6db16e2016-09-19 10:45:09 -0700508 pNewXObject->SetData(sStream.raw_str(), sStream.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509 }
dsinclair38fd8442016-09-15 10:15:32 -0700510 pPageDict->RemoveFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700511
512 ObjectArray.RemoveAll();
513 RectArray.RemoveAll();
514
515 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700516}