blob: 3049384db6a0919398fc4c8bd7fa21c259661613 [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"
Bo Xufdc00a72014-10-28 23:03:33 -07009#include "../include/fpdfxfa/fpdfxfa_doc.h"
10#include "../include/fpdfxfa/fpdfxfa_page.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070011
12typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray;
13typedef CFX_ArrayTemplate<CPDF_Rect> CPDF_RectArray;
14
15enum FPDF_TYPE { MAX, MIN };
16enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM };
17
Nico Weber9d8ec5a2015-08-04 13:00:21 -070018FX_BOOL IsValiableRect(CPDF_Rect rect, CPDF_Rect rcPage) {
19 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f)
20 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070021
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022 if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f &&
23 rect.bottom == 0.0f)
24 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070025
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 if (!rcPage.IsEmpty()) {
27 if (rect.left - rcPage.left < -10.000001f ||
28 rect.right - rcPage.right > 10.000001f ||
29 rect.top - rcPage.top > 10.000001f ||
30 rect.bottom - rcPage.bottom < -10.000001f)
31 return FALSE;
32 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070033
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035}
36
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037FX_BOOL GetContentsRect(CPDF_Document* pDoc,
38 CPDF_Dictionary* pDict,
39 CPDF_RectArray* pRectArray) {
Tom Sepezae51c812015-08-05 12:34:06 -070040 CPDF_Page* pPDFPage = new CPDF_Page;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 pPDFPage->Load(pDoc, pDict, FALSE);
42 pPDFPage->ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 FX_POSITION pos = pPDFPage->GetFirstObjectPosition();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 while (pos) {
47 CPDF_PageObject* pPageObject = pPDFPage->GetNextObject(pos);
48 if (!pPageObject)
49 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070050
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 CPDF_Rect rc;
52 rc.left = pPageObject->m_Left;
53 rc.right = pPageObject->m_Right;
54 rc.bottom = pPageObject->m_Bottom;
55 rc.top = pPageObject->m_Top;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070056
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 if (IsValiableRect(rc, pDict->GetRect("MediaBox"))) {
58 pRectArray->Add(rc);
Tom Sepez3c3201f2015-05-20 10:20:35 -070059 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 }
61
62 delete pPDFPage;
63 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064}
65
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066void ParserStream(CPDF_Dictionary* pPageDic,
67 CPDF_Dictionary* pStream,
68 CPDF_RectArray* pRectArray,
69 CPDF_ObjectArray* pObjectArray) {
70 if (!pStream)
71 return;
72 CPDF_Rect rect;
73 if (pStream->KeyExist("Rect"))
74 rect = pStream->GetRect("Rect");
75 else if (pStream->KeyExist("BBox"))
76 rect = pStream->GetRect("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 if (IsValiableRect(rect, pPageDic->GetRect("MediaBox")))
79 pRectArray->Add(rect);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070080
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 pObjectArray->Add(pStream);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082}
83
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084int ParserAnnots(CPDF_Document* pSourceDoc,
85 CPDF_Dictionary* pPageDic,
86 CPDF_RectArray* pRectArray,
87 CPDF_ObjectArray* pObjectArray,
88 int nUsage) {
89 if (!pSourceDoc || !pPageDic)
90 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 GetContentsRect(pSourceDoc, pPageDic, pRectArray);
93 CPDF_Array* pAnnots = pPageDic->GetArray("Annots");
94 if (!pAnnots)
95 return FLATTEN_NOTHINGTODO;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070096
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 FX_DWORD dwSize = pAnnots->GetCount();
98 for (int i = 0; i < (int)dwSize; i++) {
99 CPDF_Object* pObj = pAnnots->GetElementValue(i);
100 if (!pObj || pObj->GetType() != PDFOBJ_DICTIONARY)
101 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700102
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 CPDF_Dictionary* pAnnotDic = (CPDF_Dictionary*)pObj;
104 CFX_ByteString sSubtype = pAnnotDic->GetString("Subtype");
105 if (sSubtype == "Popup")
106 continue;
107
108 int nAnnotFlag = pAnnotDic->GetInteger("F");
109 if (nAnnotFlag & ANNOTFLAG_HIDDEN)
110 continue;
111
112 if (nUsage == FLAT_NORMALDISPLAY) {
113 if (nAnnotFlag & ANNOTFLAG_INVISIBLE)
114 continue;
115
116 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
117 } else {
118 if (nAnnotFlag & ANNOTFLAG_PRINT)
119 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
120 }
121 }
122 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700123}
124
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125FX_FLOAT GetMinMaxValue(CPDF_RectArray& array,
126 FPDF_TYPE type,
127 FPDF_VALUE value) {
128 int nRects = array.GetSize();
129 FX_FLOAT fRet = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 if (nRects <= 0)
132 return 0.0f;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700133
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 FX_FLOAT* pArray = new FX_FLOAT[nRects];
135 switch (value) {
136 case LEFT: {
137 for (int i = 0; i < nRects; i++)
138 pArray[i] = CPDF_Rect(array.GetAt(i)).left;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700139
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 break;
141 }
142 case TOP: {
143 for (int i = 0; i < nRects; i++)
144 pArray[i] = CPDF_Rect(array.GetAt(i)).top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700145
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 break;
147 }
148 case RIGHT: {
149 for (int i = 0; i < nRects; i++)
150 pArray[i] = CPDF_Rect(array.GetAt(i)).right;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700151
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 break;
153 }
154 case BOTTOM: {
155 for (int i = 0; i < nRects; i++)
156 pArray[i] = CPDF_Rect(array.GetAt(i)).bottom;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700157
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 break;
159 }
160 default:
161 break;
162 }
163 fRet = pArray[0];
164 if (type == MAX) {
165 for (int i = 1; i < nRects; i++)
166 if (fRet <= pArray[i])
167 fRet = pArray[i];
168 } else {
169 for (int i = 1; i < nRects; i++)
170 if (fRet >= pArray[i])
171 fRet = pArray[i];
172 }
173 delete[] pArray;
174 return fRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700175}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700176
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177CPDF_Rect CalculateRect(CPDF_RectArray* pRectArray) {
178 CPDF_Rect rcRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700179
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT);
181 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP);
182 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT);
183 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700184
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 return rcRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700186}
187
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188void SetPageContents(CFX_ByteString key,
189 CPDF_Dictionary* pPage,
190 CPDF_Document* pDocument) {
191 CPDF_Object* pContentsObj = pPage->GetStream("Contents");
192 if (!pContentsObj) {
193 pContentsObj = pPage->GetArray("Contents");
194 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 if (!pContentsObj) {
197 // Create a new contents dictionary
198 if (!key.IsEmpty()) {
Nico Weber077f1a32015-08-06 15:08:57 -0700199 CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 if (!pNewContents)
201 return;
202 pPage->SetAtReference("Contents", pDocument,
203 pDocument->AddIndirectObject(pNewContents));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700204
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 CFX_ByteString sStream;
206 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
207 pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
208 FALSE);
209 }
210 return;
211 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700212
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 int iType = pContentsObj->GetType();
214 CPDF_Array* pContentsArray = NULL;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700215
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 switch (iType) {
217 case PDFOBJ_STREAM: {
Tom Sepezae51c812015-08-05 12:34:06 -0700218 pContentsArray = new CPDF_Array;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 CPDF_Stream* pContents = (CPDF_Stream*)pContentsObj;
220 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContents);
221 CPDF_StreamAcc acc;
222 acc.LoadAllData(pContents);
223 CFX_ByteString sStream = "q\n";
224 CFX_ByteString sBody =
225 CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize());
226 sStream = sStream + sBody + "\nQ";
227 pContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
228 FALSE);
229 pContentsArray->AddReference(pDocument, dwObjNum);
230 break;
231 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700232
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 case PDFOBJ_ARRAY: {
234 pContentsArray = (CPDF_Array*)pContentsObj;
235 break;
236 }
237 default:
238 break;
239 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700240
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 if (!pContentsArray)
242 return;
243
244 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContentsArray);
245 pPage->SetAtReference("Contents", pDocument, dwObjNum);
246
247 if (!key.IsEmpty()) {
Nico Weber077f1a32015-08-06 15:08:57 -0700248 CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 dwObjNum = pDocument->AddIndirectObject(pNewContents);
250 pContentsArray->AddReference(pDocument, dwObjNum);
251
252 CFX_ByteString sStream;
253 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
254 pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
255 FALSE);
256 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700257}
258
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259CFX_AffineMatrix GetMatrix(CPDF_Rect rcAnnot,
260 CPDF_Rect rcStream,
261 CFX_AffineMatrix matrix) {
262 if (rcStream.IsEmpty())
263 return CFX_AffineMatrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 matrix.TransformRect(rcStream);
266 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700267
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
269 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700270
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
272 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
273 return CFX_AffineMatrix(a, 0, 0, d, e, f);
274}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700275
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276void GetOffset(FX_FLOAT& fa,
277 FX_FLOAT& fd,
278 FX_FLOAT& fe,
279 FX_FLOAT& ff,
280 CPDF_Rect rcAnnot,
281 CPDF_Rect rcStream,
282 CFX_AffineMatrix matrix) {
283 FX_FLOAT fStreamWidth = 0.0f;
284 FX_FLOAT fStreamHeight = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 if (matrix.a != 0 && matrix.d != 0) {
287 fStreamWidth = rcStream.right - rcStream.left;
288 fStreamHeight = rcStream.top - rcStream.bottom;
289 } else {
290 fStreamWidth = rcStream.top - rcStream.bottom;
291 fStreamHeight = rcStream.right - rcStream.left;
292 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700293
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 FX_FLOAT x1 =
295 matrix.a * rcStream.left + matrix.c * rcStream.bottom + matrix.e;
296 FX_FLOAT y1 =
297 matrix.b * rcStream.left + matrix.d * rcStream.bottom + matrix.f;
298 FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matrix.e;
299 FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matrix.f;
300 FX_FLOAT x3 =
301 matrix.a * rcStream.right + matrix.c * rcStream.bottom + matrix.e;
302 FX_FLOAT y3 =
303 matrix.b * rcStream.right + matrix.d * rcStream.bottom + matrix.f;
304 FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matrix.e;
305 FX_FLOAT y4 = matrix.b * rcStream.right + matrix.d * rcStream.top + matrix.f;
Tom Sepez3c3201f2015-05-20 10:20:35 -0700306
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307 FX_FLOAT left = FX_MIN(FX_MIN(x1, x2), FX_MIN(x3, x4));
308 FX_FLOAT bottom = FX_MIN(FX_MIN(y1, y2), FX_MIN(y3, y4));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700309
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 fa = (rcAnnot.right - rcAnnot.left) / fStreamWidth;
311 fd = (rcAnnot.top - rcAnnot.bottom) / fStreamHeight;
312 fe = rcAnnot.left - left * fa;
313 ff = rcAnnot.bottom - bottom * fd;
314}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700315
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
317 if (!page) {
318 return FLATTEN_FAIL;
319 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700320
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 CPDF_Page* pPage = ((CPDFXFA_Page*)(page))->GetPDFPage();
322 if (!pPage)
323 return FLATTEN_FAIL;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700324
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 CPDF_Document* pDocument = pPage->m_pDocument;
326 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700327
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 if (!pDocument || !pPageDict) {
329 return FLATTEN_FAIL;
330 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700331
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332 CPDF_ObjectArray ObjectArray;
333 CPDF_RectArray RectArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700334
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 int iRet = FLATTEN_FAIL;
336 iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
337 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
338 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700339
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 CPDF_Rect rcOriginalCB;
341 CPDF_Rect rcMerger = CalculateRect(&RectArray);
342 CPDF_Rect rcOriginalMB = pPageDict->GetRect("MediaBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700343
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 if (pPageDict->KeyExist("CropBox"))
345 rcOriginalMB = pPageDict->GetRect("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700346
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347 if (rcOriginalMB.IsEmpty()) {
348 rcOriginalMB = CPDF_Rect(0.0f, 0.0f, 612.0f, 792.0f);
349 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700350
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 rcMerger.left =
352 rcMerger.left < rcOriginalMB.left ? rcOriginalMB.left : rcMerger.left;
353 rcMerger.right =
354 rcMerger.right > rcOriginalMB.right ? rcOriginalMB.right : rcMerger.right;
355 rcMerger.top =
356 rcMerger.top > rcOriginalMB.top ? rcOriginalMB.top : rcMerger.top;
357 rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom ? rcOriginalMB.bottom
358 : rcMerger.bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700359
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 if (pPageDict->KeyExist("ArtBox"))
361 rcOriginalCB = pPageDict->GetRect("ArtBox");
362 else
363 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700364
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 if (!rcOriginalMB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700366 CPDF_Array* pMediaBox = new CPDF_Array();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700367
Tom Sepezae51c812015-08-05 12:34:06 -0700368 pMediaBox->Add(new CPDF_Number(rcOriginalMB.left));
369 pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom));
370 pMediaBox->Add(new CPDF_Number(rcOriginalMB.right));
371 pMediaBox->Add(new CPDF_Number(rcOriginalMB.top));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700372
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373 pPageDict->SetAt("MediaBox", pMediaBox);
374 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700375
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 if (!rcOriginalCB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700377 CPDF_Array* pCropBox = new CPDF_Array();
378 pCropBox->Add(new CPDF_Number(rcOriginalCB.left));
379 pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom));
380 pCropBox->Add(new CPDF_Number(rcOriginalCB.right));
381 pCropBox->Add(new CPDF_Number(rcOriginalCB.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382 pPageDict->SetAt("ArtBox", pCropBox);
383 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700384
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 CPDF_Dictionary* pRes = NULL;
386 pRes = pPageDict->GetDict("Resources");
387 if (!pRes) {
Tom Sepezae51c812015-08-05 12:34:06 -0700388 pRes = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 pPageDict->SetAt("Resources", pRes);
390 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700391
Nico Weber077f1a32015-08-06 15:08:57 -0700392 CPDF_Stream* pNewXObject = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pNewXObject);
394 CPDF_Dictionary* pPageXObject = pRes->GetDict("XObject");
395 if (!pPageXObject) {
Tom Sepezae51c812015-08-05 12:34:06 -0700396 pPageXObject = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 pRes->SetAt("XObject", pPageXObject);
398 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700399
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 CFX_ByteString key = "";
401 int nStreams = ObjectArray.GetSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700402
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 if (nStreams > 0) {
404 for (int iKey = 0; /*iKey < 100*/; iKey++) {
405 char sExtend[5] = {};
406 FXSYS_itoa(iKey, sExtend, 10);
407 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700408
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 if (!pPageXObject->KeyExist(key))
410 break;
411 }
412 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700413
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700415
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416 CPDF_Dictionary* pNewXORes = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700417
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 if (!key.IsEmpty()) {
419 pPageXObject->SetAtReference(key, pDocument, dwObjNum);
420 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
Tom Sepezae51c812015-08-05 12:34:06 -0700421 pNewXORes = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700422 pNewOXbjectDic->SetAt("Resources", pNewXORes);
423 pNewOXbjectDic->SetAtName("Type", "XObject");
424 pNewOXbjectDic->SetAtName("Subtype", "Form");
425 pNewOXbjectDic->SetAtInteger("FormType", 1);
426 pNewOXbjectDic->SetAtName("Name", "FRM");
427 CPDF_Rect rcBBox = pPageDict->GetRect("ArtBox");
428 pNewOXbjectDic->SetAtRect("BBox", rcBBox);
429 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700430
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 for (int i = 0; i < nStreams; i++) {
432 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i);
433 if (!pAnnotDic)
434 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700435
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436 CPDF_Rect rcAnnot = pAnnotDic->GetRect("Rect");
437 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700438
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 CFX_ByteString sAnnotState = pAnnotDic->GetString("AS");
440 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDict("AP");
441 if (!pAnnotAP)
442 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700443
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444 CPDF_Stream* pAPStream = pAnnotAP->GetStream("N");
445 if (!pAPStream) {
446 CPDF_Dictionary* pAPDic = pAnnotAP->GetDict("N");
447 if (!pAPDic)
448 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700449
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450 if (!sAnnotState.IsEmpty()) {
451 pAPStream = pAPDic->GetStream(sAnnotState);
452 } else {
453 FX_POSITION pos = pAPDic->GetStartPos();
454 if (pos) {
455 CFX_ByteString sKey;
456 CPDF_Object* pFirstObj = pAPDic->GetNextElement(pos, sKey);
457 if (pFirstObj) {
458 if (pFirstObj->GetType() == PDFOBJ_REFERENCE)
459 pFirstObj = pFirstObj->GetDirect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700460
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461 if (pFirstObj->GetType() != PDFOBJ_STREAM)
462 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700463
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700464 pAPStream = (CPDF_Stream*)pFirstObj;
465 }
466 }
467 }
468 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700469
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470 if (!pAPStream)
471 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700472
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
474 CFX_AffineMatrix matrix = pAPDic->GetMatrix("Matrix");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700475
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700476 CPDF_Rect rcStream;
477 if (pAPDic->KeyExist("Rect"))
478 rcStream = pAPDic->GetRect("Rect");
479 else if (pAPDic->KeyExist("BBox"))
480 rcStream = pAPDic->GetRect("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700481
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482 if (rcStream.IsEmpty())
483 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700484
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485 CPDF_Object* pObj = pAPStream;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700486
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700487 if (pObj) {
488 CPDF_Dictionary* pObjDic = pObj->GetDict();
489 if (pObjDic) {
490 pObjDic->SetAtName("Type", "XObject");
491 pObjDic->SetAtName("Subtype", "Form");
492 }
493 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700494
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495 CPDF_Dictionary* pXObject = pNewXORes->GetDict("XObject");
496 if (!pXObject) {
Tom Sepezae51c812015-08-05 12:34:06 -0700497 pXObject = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498 pNewXORes->SetAt("XObject", pXObject);
499 }
500
501 CFX_ByteString sFormName;
502 sFormName.Format("F%d", i);
503 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pObj);
504 pXObject->SetAtReference(sFormName, pDocument, dwObjNum);
505
506 CPDF_StreamAcc acc;
507 acc.LoadAllData(pNewXObject);
508
509 const uint8_t* pData = acc.GetData();
510 CFX_ByteString sStream(pData, acc.GetSize());
511 CFX_ByteString sTemp;
512
513 if (matrix.IsIdentity()) {
514 matrix.a = 1.0f;
515 matrix.b = 0.0f;
516 matrix.c = 0.0f;
517 matrix.d = 1.0f;
518 matrix.e = 0.0f;
519 matrix.f = 0.0f;
520 }
521
522 CFX_AffineMatrix m = GetMatrix(rcAnnot, rcStream, matrix);
523 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
524 sFormName.c_str());
525 sStream += sTemp;
526
527 pNewXObject->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
528 FALSE);
529 }
530 pPageDict->RemoveAt("Annots");
531
532 ObjectArray.RemoveAll();
533 RectArray.RemoveAll();
534
535 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700536}