blob: 39441c4aef00c7907d112d282d0915df379b7d4c [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
Tom Sepez1ed8a212015-05-11 15:25:39 -07007#include "../../public/fpdf_flatten.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07008#include "../include/fsdk_define.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
10typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray;
11typedef CFX_ArrayTemplate<CPDF_Rect> CPDF_RectArray;
12
13enum FPDF_TYPE { MAX, MIN };
14enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM };
15
Nico Weber9d8ec5a2015-08-04 13:00:21 -070016FX_BOOL IsValiableRect(CPDF_Rect rect, CPDF_Rect rcPage) {
17 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f)
18 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070019
Nico Weber9d8ec5a2015-08-04 13:00:21 -070020 if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f &&
21 rect.bottom == 0.0f)
22 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070023
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 if (!rcPage.IsEmpty()) {
25 if (rect.left - rcPage.left < -10.000001f ||
26 rect.right - rcPage.right > 10.000001f ||
27 rect.top - rcPage.top > 10.000001f ||
28 rect.bottom - rcPage.bottom < -10.000001f)
29 return FALSE;
30 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070031
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033}
34
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035FX_BOOL GetContentsRect(CPDF_Document* pDoc,
36 CPDF_Dictionary* pDict,
37 CPDF_RectArray* pRectArray) {
Tom Sepezae51c812015-08-05 12:34:06 -070038 CPDF_Page* pPDFPage = new CPDF_Page;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 pPDFPage->Load(pDoc, pDict, FALSE);
40 pPDFPage->ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 FX_POSITION pos = pPDFPage->GetFirstObjectPosition();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 while (pos) {
45 CPDF_PageObject* pPageObject = pPDFPage->GetNextObject(pos);
46 if (!pPageObject)
47 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070048
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 CPDF_Rect rc;
50 rc.left = pPageObject->m_Left;
51 rc.right = pPageObject->m_Right;
52 rc.bottom = pPageObject->m_Bottom;
53 rc.top = pPageObject->m_Top;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070054
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 if (IsValiableRect(rc, pDict->GetRect("MediaBox"))) {
56 pRectArray->Add(rc);
Tom Sepez3c3201f2015-05-20 10:20:35 -070057 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 }
59
60 delete pPDFPage;
61 return TRUE;
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,
66 CPDF_RectArray* pRectArray,
67 CPDF_ObjectArray* pObjectArray) {
68 if (!pStream)
69 return;
70 CPDF_Rect rect;
71 if (pStream->KeyExist("Rect"))
72 rect = pStream->GetRect("Rect");
73 else if (pStream->KeyExist("BBox"))
74 rect = pStream->GetRect("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 if (IsValiableRect(rect, pPageDic->GetRect("MediaBox")))
77 pRectArray->Add(rect);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070078
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 pObjectArray->Add(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,
84 CPDF_RectArray* pRectArray,
85 CPDF_ObjectArray* pObjectArray,
86 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);
91 CPDF_Array* pAnnots = pPageDic->GetArray("Annots");
92 if (!pAnnots)
93 return FLATTEN_NOTHINGTODO;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070094
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 FX_DWORD dwSize = pAnnots->GetCount();
96 for (int i = 0; i < (int)dwSize; i++) {
Dan Sinclairf1251c12015-10-20 16:24:45 -040097 CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnots->GetElementValue(i));
98 if (!pAnnotDic)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700100
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 CFX_ByteString sSubtype = pAnnotDic->GetString("Subtype");
102 if (sSubtype == "Popup")
103 continue;
104
105 int nAnnotFlag = pAnnotDic->GetInteger("F");
106 if (nAnnotFlag & ANNOTFLAG_HIDDEN)
107 continue;
108
109 if (nUsage == FLAT_NORMALDISPLAY) {
110 if (nAnnotFlag & ANNOTFLAG_INVISIBLE)
111 continue;
112
113 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
114 } else {
115 if (nAnnotFlag & ANNOTFLAG_PRINT)
116 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
117 }
118 }
119 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120}
121
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122FX_FLOAT GetMinMaxValue(CPDF_RectArray& array,
123 FPDF_TYPE type,
124 FPDF_VALUE value) {
125 int nRects = array.GetSize();
126 FX_FLOAT fRet = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700127
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 if (nRects <= 0)
129 return 0.0f;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 FX_FLOAT* pArray = new FX_FLOAT[nRects];
132 switch (value) {
133 case LEFT: {
134 for (int i = 0; i < nRects; i++)
135 pArray[i] = CPDF_Rect(array.GetAt(i)).left;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700136
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 break;
138 }
139 case TOP: {
140 for (int i = 0; i < nRects; i++)
141 pArray[i] = CPDF_Rect(array.GetAt(i)).top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143 break;
144 }
145 case RIGHT: {
146 for (int i = 0; i < nRects; i++)
147 pArray[i] = CPDF_Rect(array.GetAt(i)).right;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700148
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149 break;
150 }
151 case BOTTOM: {
152 for (int i = 0; i < nRects; i++)
153 pArray[i] = CPDF_Rect(array.GetAt(i)).bottom;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700154
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 break;
156 }
157 default:
158 break;
159 }
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
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174CPDF_Rect CalculateRect(CPDF_RectArray* pRectArray) {
175 CPDF_Rect 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) {
188 CPDF_Object* pContentsObj = pPage->GetStream("Contents");
189 if (!pContentsObj) {
190 pContentsObj = pPage->GetArray("Contents");
191 }
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()) {
Nico Weber077f1a32015-08-06 15:08:57 -0700196 CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 pPage->SetAtReference("Contents", pDocument,
198 pDocument->AddIndirectObject(pNewContents));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 CFX_ByteString sStream;
201 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
202 pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
203 FALSE);
204 }
205 return;
206 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700207
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 int iType = pContentsObj->GetType();
209 CPDF_Array* pContentsArray = NULL;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700210
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 switch (iType) {
212 case PDFOBJ_STREAM: {
Tom Sepezae51c812015-08-05 12:34:06 -0700213 pContentsArray = new CPDF_Array;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400214 CPDF_Stream* pContents = pContentsObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContents);
216 CPDF_StreamAcc acc;
217 acc.LoadAllData(pContents);
218 CFX_ByteString sStream = "q\n";
219 CFX_ByteString sBody =
220 CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize());
221 sStream = sStream + sBody + "\nQ";
222 pContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
223 FALSE);
224 pContentsArray->AddReference(pDocument, dwObjNum);
225 break;
226 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700227
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 case PDFOBJ_ARRAY: {
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400229 pContentsArray = pContentsObj->AsArray();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 break;
231 }
232 default:
233 break;
234 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700235
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 if (!pContentsArray)
237 return;
238
239 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContentsArray);
240 pPage->SetAtReference("Contents", pDocument, dwObjNum);
241
242 if (!key.IsEmpty()) {
Nico Weber077f1a32015-08-06 15:08:57 -0700243 CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 dwObjNum = pDocument->AddIndirectObject(pNewContents);
245 pContentsArray->AddReference(pDocument, dwObjNum);
246
247 CFX_ByteString sStream;
248 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
249 pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
250 FALSE);
251 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252}
253
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254CFX_AffineMatrix GetMatrix(CPDF_Rect rcAnnot,
255 CPDF_Rect rcStream,
256 CFX_AffineMatrix matrix) {
257 if (rcStream.IsEmpty())
258 return CFX_AffineMatrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260 matrix.TransformRect(rcStream);
261 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
264 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700265
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
267 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
268 return CFX_AffineMatrix(a, 0, 0, d, e, f);
269}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700270
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271void GetOffset(FX_FLOAT& fa,
272 FX_FLOAT& fd,
273 FX_FLOAT& fe,
274 FX_FLOAT& ff,
275 CPDF_Rect rcAnnot,
276 CPDF_Rect rcStream,
277 CFX_AffineMatrix matrix) {
278 FX_FLOAT fStreamWidth = 0.0f;
279 FX_FLOAT fStreamHeight = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700280
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 if (matrix.a != 0 && matrix.d != 0) {
282 fStreamWidth = rcStream.right - rcStream.left;
283 fStreamHeight = rcStream.top - rcStream.bottom;
284 } else {
285 fStreamWidth = rcStream.top - rcStream.bottom;
286 fStreamHeight = rcStream.right - rcStream.left;
287 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700288
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 FX_FLOAT x1 =
290 matrix.a * rcStream.left + matrix.c * rcStream.bottom + matrix.e;
291 FX_FLOAT y1 =
292 matrix.b * rcStream.left + matrix.d * rcStream.bottom + matrix.f;
293 FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matrix.e;
294 FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matrix.f;
295 FX_FLOAT x3 =
296 matrix.a * rcStream.right + matrix.c * rcStream.bottom + matrix.e;
297 FX_FLOAT y3 =
298 matrix.b * rcStream.right + matrix.d * rcStream.bottom + matrix.f;
299 FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matrix.e;
300 FX_FLOAT y4 = matrix.b * rcStream.right + matrix.d * rcStream.top + matrix.f;
Tom Sepez3c3201f2015-05-20 10:20:35 -0700301
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302 FX_FLOAT left = FX_MIN(FX_MIN(x1, x2), FX_MIN(x3, x4));
303 FX_FLOAT bottom = FX_MIN(FX_MIN(y1, y2), FX_MIN(y3, y4));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700304
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 fa = (rcAnnot.right - rcAnnot.left) / fStreamWidth;
306 fd = (rcAnnot.top - rcAnnot.bottom) / fStreamHeight;
307 fe = rcAnnot.left - left * fa;
308 ff = rcAnnot.bottom - bottom * fd;
309}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700310
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700312 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 if (!page) {
314 return FLATTEN_FAIL;
315 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700316
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 CPDF_Document* pDocument = pPage->m_pDocument;
318 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700319
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 if (!pDocument || !pPageDict) {
321 return FLATTEN_FAIL;
322 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700323
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324 CPDF_ObjectArray ObjectArray;
325 CPDF_RectArray RectArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700326
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327 int iRet = FLATTEN_FAIL;
328 iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
329 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
330 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700331
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332 CPDF_Rect rcOriginalCB;
333 CPDF_Rect rcMerger = CalculateRect(&RectArray);
334 CPDF_Rect rcOriginalMB = pPageDict->GetRect("MediaBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700335
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 if (pPageDict->KeyExist("CropBox"))
337 rcOriginalMB = pPageDict->GetRect("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700338
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700339 if (rcOriginalMB.IsEmpty()) {
340 rcOriginalMB = CPDF_Rect(0.0f, 0.0f, 612.0f, 792.0f);
341 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700342
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343 rcMerger.left =
344 rcMerger.left < rcOriginalMB.left ? rcOriginalMB.left : rcMerger.left;
345 rcMerger.right =
346 rcMerger.right > rcOriginalMB.right ? rcOriginalMB.right : rcMerger.right;
347 rcMerger.top =
348 rcMerger.top > rcOriginalMB.top ? rcOriginalMB.top : rcMerger.top;
349 rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom ? rcOriginalMB.bottom
350 : rcMerger.bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700351
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352 if (pPageDict->KeyExist("ArtBox"))
353 rcOriginalCB = pPageDict->GetRect("ArtBox");
354 else
355 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700356
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357 if (!rcOriginalMB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700358 CPDF_Array* pMediaBox = new CPDF_Array();
Tom Sepezae51c812015-08-05 12:34:06 -0700359 pMediaBox->Add(new CPDF_Number(rcOriginalMB.left));
360 pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom));
361 pMediaBox->Add(new CPDF_Number(rcOriginalMB.right));
362 pMediaBox->Add(new CPDF_Number(rcOriginalMB.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363 pPageDict->SetAt("MediaBox", pMediaBox);
364 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700365
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 if (!rcOriginalCB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700367 CPDF_Array* pCropBox = new CPDF_Array();
368 pCropBox->Add(new CPDF_Number(rcOriginalCB.left));
369 pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom));
370 pCropBox->Add(new CPDF_Number(rcOriginalCB.right));
371 pCropBox->Add(new CPDF_Number(rcOriginalCB.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700372 pPageDict->SetAt("ArtBox", pCropBox);
373 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700374
Tom Sepez468e5892015-10-13 15:49:36 -0700375 CPDF_Dictionary* pRes = pPageDict->GetDict("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 if (!pRes) {
Tom Sepezae51c812015-08-05 12:34:06 -0700377 pRes = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 pPageDict->SetAt("Resources", pRes);
379 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700380
Nico Weber077f1a32015-08-06 15:08:57 -0700381 CPDF_Stream* pNewXObject = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pNewXObject);
383 CPDF_Dictionary* pPageXObject = pRes->GetDict("XObject");
384 if (!pPageXObject) {
Tom Sepezae51c812015-08-05 12:34:06 -0700385 pPageXObject = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386 pRes->SetAt("XObject", pPageXObject);
387 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700388
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 CFX_ByteString key = "";
390 int nStreams = ObjectArray.GetSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700391
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392 if (nStreams > 0) {
393 for (int iKey = 0; /*iKey < 100*/; iKey++) {
394 char sExtend[5] = {};
395 FXSYS_itoa(iKey, sExtend, 10);
396 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700397
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398 if (!pPageXObject->KeyExist(key))
399 break;
400 }
401 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700402
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700404
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 CPDF_Dictionary* pNewXORes = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700406
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700407 if (!key.IsEmpty()) {
408 pPageXObject->SetAtReference(key, pDocument, dwObjNum);
409 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
Tom Sepezae51c812015-08-05 12:34:06 -0700410 pNewXORes = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 pNewOXbjectDic->SetAt("Resources", pNewXORes);
412 pNewOXbjectDic->SetAtName("Type", "XObject");
413 pNewOXbjectDic->SetAtName("Subtype", "Form");
414 pNewOXbjectDic->SetAtInteger("FormType", 1);
415 pNewOXbjectDic->SetAtName("Name", "FRM");
416 CPDF_Rect rcBBox = pPageDict->GetRect("ArtBox");
417 pNewOXbjectDic->SetAtRect("BBox", rcBBox);
418 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700419
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700420 for (int i = 0; i < nStreams; i++) {
421 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i);
422 if (!pAnnotDic)
423 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700424
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425 CPDF_Rect rcAnnot = pAnnotDic->GetRect("Rect");
426 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700427
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428 CFX_ByteString sAnnotState = pAnnotDic->GetString("AS");
429 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDict("AP");
430 if (!pAnnotAP)
431 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700432
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 CPDF_Stream* pAPStream = pAnnotAP->GetStream("N");
434 if (!pAPStream) {
435 CPDF_Dictionary* pAPDic = pAnnotAP->GetDict("N");
436 if (!pAPDic)
437 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700438
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 if (!sAnnotState.IsEmpty()) {
440 pAPStream = pAPDic->GetStream(sAnnotState);
441 } else {
442 FX_POSITION pos = pAPDic->GetStartPos();
443 if (pos) {
444 CFX_ByteString sKey;
445 CPDF_Object* pFirstObj = pAPDic->GetNextElement(pos, sKey);
446 if (pFirstObj) {
447 if (pFirstObj->GetType() == PDFOBJ_REFERENCE)
448 pFirstObj = pFirstObj->GetDirect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700449
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400450 if (!pFirstObj->IsStream())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700451 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700452
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400453 pAPStream = pFirstObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 }
455 }
456 }
457 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700458 if (!pAPStream)
459 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700460
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
462 CFX_AffineMatrix matrix = pAPDic->GetMatrix("Matrix");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700463
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700464 CPDF_Rect rcStream;
465 if (pAPDic->KeyExist("Rect"))
466 rcStream = pAPDic->GetRect("Rect");
467 else if (pAPDic->KeyExist("BBox"))
468 rcStream = pAPDic->GetRect("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700469
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470 if (rcStream.IsEmpty())
471 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700472
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 CPDF_Object* pObj = pAPStream;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700474
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475 if (pObj) {
476 CPDF_Dictionary* pObjDic = pObj->GetDict();
477 if (pObjDic) {
478 pObjDic->SetAtName("Type", "XObject");
479 pObjDic->SetAtName("Subtype", "Form");
480 }
481 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700482
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700483 CPDF_Dictionary* pXObject = pNewXORes->GetDict("XObject");
484 if (!pXObject) {
Tom Sepezae51c812015-08-05 12:34:06 -0700485 pXObject = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486 pNewXORes->SetAt("XObject", pXObject);
487 }
488
489 CFX_ByteString sFormName;
490 sFormName.Format("F%d", i);
491 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pObj);
492 pXObject->SetAtReference(sFormName, pDocument, dwObjNum);
493
494 CPDF_StreamAcc acc;
495 acc.LoadAllData(pNewXObject);
496
497 const uint8_t* pData = acc.GetData();
498 CFX_ByteString sStream(pData, acc.GetSize());
499 CFX_ByteString sTemp;
500
501 if (matrix.IsIdentity()) {
502 matrix.a = 1.0f;
503 matrix.b = 0.0f;
504 matrix.c = 0.0f;
505 matrix.d = 1.0f;
506 matrix.e = 0.0f;
507 matrix.f = 0.0f;
508 }
509
510 CFX_AffineMatrix m = GetMatrix(rcAnnot, rcStream, matrix);
511 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
512 sFormName.c_str());
513 sStream += sTemp;
514
515 pNewXObject->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
516 FALSE);
517 }
518 pPageDict->RemoveAt("Annots");
519
520 ObjectArray.RemoveAll();
521 RectArray.RemoveAll();
522
523 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700524}