blob: 69c7a3220707266ec74280813df54289b60a1aac [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
dsinclair8a752ab2016-09-29 11:59:54 -070011#include "core/fpdfapi/fpdf_page/cpdf_page.h"
12#include "core/fpdfapi/fpdf_page/cpdf_pageobject.h"
dsinclairc6c425a2016-09-29 12:01:30 -070013#include "core/fpdfapi/fpdf_parser/cpdf_array.h"
14#include "core/fpdfapi/fpdf_parser/cpdf_document.h"
15#include "core/fpdfapi/fpdf_parser/cpdf_number.h"
16#include "core/fpdfapi/fpdf_parser/cpdf_stream.h"
17#include "core/fpdfapi/fpdf_parser/cpdf_stream_acc.h"
dsinclair1727aee2016-09-29 13:12:56 -070018#include "core/fpdfdoc/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()) {
tsepez698c5712016-09-28 16:47:07 -0700196 CPDF_Stream* pNewContents = new CPDF_Stream(
197 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 CFX_ByteString sStream;
199 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
tsepeze6db16e2016-09-19 10:45:09 -0700200 pNewContents->SetData(sStream.raw_str(), sStream.GetLength());
tsepezbb577af2016-09-21 19:10:19 -0700201 pPage->SetReferenceFor("Contents", pDocument,
202 pDocument->AddIndirectObject(pNewContents));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 }
204 return;
205 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700206
thestig1cd352e2016-06-07 17:53:06 -0700207 CPDF_Array* pContentsArray = nullptr;
Tom Sepez8e5cd192016-01-26 13:20:26 -0800208 switch (pContentsObj->GetType()) {
209 case CPDF_Object::STREAM: {
Tom Sepezae51c812015-08-05 12:34:06 -0700210 pContentsArray = new CPDF_Array;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400211 CPDF_Stream* pContents = pContentsObj->AsStream();
tsepezc3255f52016-03-25 14:52:27 -0700212 uint32_t dwObjNum = pDocument->AddIndirectObject(pContents);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 CPDF_StreamAcc acc;
214 acc.LoadAllData(pContents);
215 CFX_ByteString sStream = "q\n";
216 CFX_ByteString sBody =
217 CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize());
218 sStream = sStream + sBody + "\nQ";
tsepeze6db16e2016-09-19 10:45:09 -0700219 pContents->SetData(sStream.raw_str(), sStream.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 pContentsArray->AddReference(pDocument, dwObjNum);
221 break;
222 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700223
Tom Sepez8e5cd192016-01-26 13:20:26 -0800224 case CPDF_Object::ARRAY: {
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400225 pContentsArray = pContentsObj->AsArray();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 break;
227 }
228 default:
229 break;
230 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700231
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 if (!pContentsArray)
233 return;
234
tsepezbb577af2016-09-21 19:10:19 -0700235 pPage->SetReferenceFor("Contents", pDocument,
236 pDocument->AddIndirectObject(pContentsArray));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237
238 if (!key.IsEmpty()) {
tsepez698c5712016-09-28 16:47:07 -0700239 CPDF_Stream* pNewContents = new CPDF_Stream(
240 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 CFX_ByteString sStream;
242 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
tsepeze6db16e2016-09-19 10:45:09 -0700243 pNewContents->SetData(sStream.raw_str(), sStream.GetLength());
tsepezbb577af2016-09-21 19:10:19 -0700244 pContentsArray->AddReference(pDocument,
245 pDocument->AddIndirectObject(pNewContents));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247}
248
Tom Sepez281a9ea2016-02-26 14:24:28 -0800249CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot,
250 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800251 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 if (rcStream.IsEmpty())
Tom Sepez60d909e2015-12-10 15:34:55 -0800253 return CFX_Matrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700254
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255 matrix.TransformRect(rcStream);
256 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700257
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
259 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700260
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
262 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
Tom Sepez60d909e2015-12-10 15:34:55 -0800263 return CFX_Matrix(a, 0, 0, d, e, f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700265
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266void GetOffset(FX_FLOAT& fa,
267 FX_FLOAT& fd,
268 FX_FLOAT& fe,
269 FX_FLOAT& ff,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800270 CFX_FloatRect rcAnnot,
271 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800272 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 FX_FLOAT fStreamWidth = 0.0f;
274 FX_FLOAT fStreamHeight = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 if (matrix.a != 0 && matrix.d != 0) {
277 fStreamWidth = rcStream.right - rcStream.left;
278 fStreamHeight = rcStream.top - rcStream.bottom;
279 } else {
280 fStreamWidth = rcStream.top - rcStream.bottom;
281 fStreamHeight = rcStream.right - rcStream.left;
282 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 FX_FLOAT x1 =
285 matrix.a * rcStream.left + matrix.c * rcStream.bottom + matrix.e;
286 FX_FLOAT y1 =
287 matrix.b * rcStream.left + matrix.d * rcStream.bottom + matrix.f;
288 FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matrix.e;
289 FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matrix.f;
290 FX_FLOAT x3 =
291 matrix.a * rcStream.right + matrix.c * rcStream.bottom + matrix.e;
292 FX_FLOAT y3 =
293 matrix.b * rcStream.right + matrix.d * rcStream.bottom + matrix.f;
294 FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matrix.e;
295 FX_FLOAT y4 = matrix.b * rcStream.right + matrix.d * rcStream.top + matrix.f;
Tom Sepez3c3201f2015-05-20 10:20:35 -0700296
Lei Zhang375a8642016-01-11 11:59:17 -0800297 FX_FLOAT left = std::min(std::min(x1, x2), std::min(x3, x4));
298 FX_FLOAT bottom = std::min(std::min(y1, y2), std::min(y3, y4));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700299
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300 fa = (rcAnnot.right - rcAnnot.left) / fStreamWidth;
301 fd = (rcAnnot.top - rcAnnot.bottom) / fStreamHeight;
302 fe = rcAnnot.left - left * fa;
303 ff = rcAnnot.bottom - bottom * fd;
304}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700305
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700306DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700307 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 if (!page) {
309 return FLATTEN_FAIL;
310 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700311
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312 CPDF_Document* pDocument = pPage->m_pDocument;
313 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700314
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 if (!pDocument || !pPageDict) {
316 return FLATTEN_FAIL;
317 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700318
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 CPDF_ObjectArray ObjectArray;
320 CPDF_RectArray RectArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700321
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 int iRet = FLATTEN_FAIL;
323 iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
324 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
325 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700326
Tom Sepez281a9ea2016-02-26 14:24:28 -0800327 CFX_FloatRect rcOriginalCB;
328 CFX_FloatRect rcMerger = CalculateRect(&RectArray);
dsinclair38fd8442016-09-15 10:15:32 -0700329 CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700330
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331 if (pPageDict->KeyExist("CropBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700332 rcOriginalMB = pPageDict->GetRectFor("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700333
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 if (rcOriginalMB.IsEmpty()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800335 rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700337
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338 rcMerger.left =
339 rcMerger.left < rcOriginalMB.left ? rcOriginalMB.left : rcMerger.left;
340 rcMerger.right =
341 rcMerger.right > rcOriginalMB.right ? rcOriginalMB.right : rcMerger.right;
342 rcMerger.top =
343 rcMerger.top > rcOriginalMB.top ? rcOriginalMB.top : rcMerger.top;
344 rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom ? rcOriginalMB.bottom
345 : rcMerger.bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700346
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347 if (pPageDict->KeyExist("ArtBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700348 rcOriginalCB = pPageDict->GetRectFor("ArtBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 else
350 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700351
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352 if (!rcOriginalMB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700353 CPDF_Array* pMediaBox = new CPDF_Array();
Tom Sepezae51c812015-08-05 12:34:06 -0700354 pMediaBox->Add(new CPDF_Number(rcOriginalMB.left));
355 pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom));
356 pMediaBox->Add(new CPDF_Number(rcOriginalMB.right));
357 pMediaBox->Add(new CPDF_Number(rcOriginalMB.top));
dsinclair38fd8442016-09-15 10:15:32 -0700358 pPageDict->SetFor("MediaBox", pMediaBox);
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 if (!rcOriginalCB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700362 CPDF_Array* pCropBox = new CPDF_Array();
363 pCropBox->Add(new CPDF_Number(rcOriginalCB.left));
364 pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom));
365 pCropBox->Add(new CPDF_Number(rcOriginalCB.right));
366 pCropBox->Add(new CPDF_Number(rcOriginalCB.top));
dsinclair38fd8442016-09-15 10:15:32 -0700367 pPageDict->SetFor("ArtBox", pCropBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700369
dsinclair38fd8442016-09-15 10:15:32 -0700370 CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 if (!pRes) {
tsepez698c5712016-09-28 16:47:07 -0700372 pRes = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700373 pPageDict->SetFor("Resources", pRes);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700375
tsepez698c5712016-09-28 16:47:07 -0700376 CPDF_Stream* pNewXObject = new CPDF_Stream(
377 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool()));
378
tsepezc3255f52016-03-25 14:52:27 -0700379 uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject);
dsinclair38fd8442016-09-15 10:15:32 -0700380 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 if (!pPageXObject) {
tsepez698c5712016-09-28 16:47:07 -0700382 pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700383 pRes->SetFor("XObject", pPageXObject);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700385
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386 CFX_ByteString key = "";
387 int nStreams = ObjectArray.GetSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700388
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 if (nStreams > 0) {
390 for (int iKey = 0; /*iKey < 100*/; iKey++) {
391 char sExtend[5] = {};
392 FXSYS_itoa(iKey, sExtend, 10);
393 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
tsepez7b1ccf92016-04-14 11:04:57 -0700394 if (!pPageXObject->KeyExist(key))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 break;
396 }
397 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700398
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700400
thestig1cd352e2016-06-07 17:53:06 -0700401 CPDF_Dictionary* pNewXORes = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700402
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 if (!key.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700404 pPageXObject->SetReferenceFor(key, pDocument, dwObjNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
tsepez698c5712016-09-28 16:47:07 -0700406 pNewXORes = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700407 pNewOXbjectDic->SetFor("Resources", pNewXORes);
408 pNewOXbjectDic->SetNameFor("Type", "XObject");
409 pNewOXbjectDic->SetNameFor("Subtype", "Form");
410 pNewOXbjectDic->SetIntegerFor("FormType", 1);
411 pNewOXbjectDic->SetNameFor("Name", "FRM");
412 CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox");
413 pNewOXbjectDic->SetRectFor("BBox", rcBBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700415
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416 for (int i = 0; i < nStreams; i++) {
417 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i);
418 if (!pAnnotDic)
419 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700420
dsinclair38fd8442016-09-15 10:15:32 -0700421 CFX_FloatRect rcAnnot = pAnnotDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700422 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700423
dsinclair38fd8442016-09-15 10:15:32 -0700424 CFX_ByteString sAnnotState = pAnnotDic->GetStringFor("AS");
425 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictFor("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 if (!pAnnotAP)
427 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700428
dsinclair38fd8442016-09-15 10:15:32 -0700429 CPDF_Stream* pAPStream = pAnnotAP->GetStreamFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430 if (!pAPStream) {
dsinclair38fd8442016-09-15 10:15:32 -0700431 CPDF_Dictionary* pAPDic = pAnnotAP->GetDictFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700432 if (!pAPDic)
433 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700434
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 if (!sAnnotState.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700436 pAPStream = pAPDic->GetStreamFor(sAnnotState);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437 } else {
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800438 auto it = pAPDic->begin();
439 if (it != pAPDic->end()) {
440 CPDF_Object* pFirstObj = it->second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700441 if (pFirstObj) {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400442 if (pFirstObj->IsReference())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443 pFirstObj = pFirstObj->GetDirect();
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400444 if (!pFirstObj->IsStream())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700445 continue;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400446 pAPStream = pFirstObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447 }
448 }
449 }
450 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700451 if (!pAPStream)
452 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700453
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
dsinclair38fd8442016-09-15 10:15:32 -0700455 CFX_Matrix matrix = pAPDic->GetMatrixFor("Matrix");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700456
Tom Sepez281a9ea2016-02-26 14:24:28 -0800457 CFX_FloatRect rcStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700458 if (pAPDic->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -0700459 rcStream = pAPDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 else if (pAPDic->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700461 rcStream = pAPDic->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700462
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463 if (rcStream.IsEmpty())
464 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700465
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466 CPDF_Object* pObj = pAPStream;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700467
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700468 if (pObj) {
469 CPDF_Dictionary* pObjDic = pObj->GetDict();
470 if (pObjDic) {
dsinclair38fd8442016-09-15 10:15:32 -0700471 pObjDic->SetNameFor("Type", "XObject");
472 pObjDic->SetNameFor("Subtype", "Form");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 }
474 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700475
dsinclair38fd8442016-09-15 10:15:32 -0700476 CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700477 if (!pXObject) {
tsepez698c5712016-09-28 16:47:07 -0700478 pXObject = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700479 pNewXORes->SetFor("XObject", pXObject);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700480 }
481
482 CFX_ByteString sFormName;
483 sFormName.Format("F%d", i);
tsepezbb577af2016-09-21 19:10:19 -0700484 pXObject->SetReferenceFor(sFormName, pDocument,
485 pDocument->AddIndirectObject(pObj));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486
487 CPDF_StreamAcc acc;
488 acc.LoadAllData(pNewXObject);
489
490 const uint8_t* pData = acc.GetData();
491 CFX_ByteString sStream(pData, acc.GetSize());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700492
493 if (matrix.IsIdentity()) {
494 matrix.a = 1.0f;
495 matrix.b = 0.0f;
496 matrix.c = 0.0f;
497 matrix.d = 1.0f;
498 matrix.e = 0.0f;
499 matrix.f = 0.0f;
500 }
501
tsepezbb577af2016-09-21 19:10:19 -0700502 CFX_ByteString sTemp;
Tom Sepez60d909e2015-12-10 15:34:55 -0800503 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700504 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
505 sFormName.c_str());
506 sStream += sTemp;
tsepeze6db16e2016-09-19 10:45:09 -0700507 pNewXObject->SetData(sStream.raw_str(), sStream.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508 }
dsinclair38fd8442016-09-15 10:15:32 -0700509 pPageDict->RemoveFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510
511 ObjectArray.RemoveAll();
512 RectArray.RemoveAll();
513
514 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700515}