blob: 50d50367241cca779dd42343b16ff809df7100cb [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
Lei Zhangbde53d22015-11-12 22:21:30 -080011#include "fpdfsdk/include/fsdk_define.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070012
13typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray;
14typedef CFX_ArrayTemplate<CPDF_Rect> CPDF_RectArray;
15
16enum FPDF_TYPE { MAX, MIN };
17enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM };
18
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019FX_BOOL IsValiableRect(CPDF_Rect rect, CPDF_Rect rcPage) {
20 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f)
21 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070022
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023 if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f &&
24 rect.bottom == 0.0f)
25 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070026
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027 if (!rcPage.IsEmpty()) {
28 if (rect.left - rcPage.left < -10.000001f ||
29 rect.right - rcPage.right > 10.000001f ||
30 rect.top - rcPage.top > 10.000001f ||
31 rect.bottom - rcPage.bottom < -10.000001f)
32 return FALSE;
33 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070034
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036}
37
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038FX_BOOL GetContentsRect(CPDF_Document* pDoc,
39 CPDF_Dictionary* pDict,
40 CPDF_RectArray* pRectArray) {
Tom Sepezae51c812015-08-05 12:34:06 -070041 CPDF_Page* pPDFPage = new CPDF_Page;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 pPDFPage->Load(pDoc, pDict, FALSE);
43 pPDFPage->ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 FX_POSITION pos = pPDFPage->GetFirstObjectPosition();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 while (pos) {
48 CPDF_PageObject* pPageObject = pPDFPage->GetNextObject(pos);
49 if (!pPageObject)
50 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070051
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 CPDF_Rect rc;
53 rc.left = pPageObject->m_Left;
54 rc.right = pPageObject->m_Right;
55 rc.bottom = pPageObject->m_Bottom;
56 rc.top = pPageObject->m_Top;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070057
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 if (IsValiableRect(rc, pDict->GetRect("MediaBox"))) {
59 pRectArray->Add(rc);
Tom Sepez3c3201f2015-05-20 10:20:35 -070060 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 }
62
63 delete pPDFPage;
64 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065}
66
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067void ParserStream(CPDF_Dictionary* pPageDic,
68 CPDF_Dictionary* pStream,
69 CPDF_RectArray* pRectArray,
70 CPDF_ObjectArray* pObjectArray) {
71 if (!pStream)
72 return;
73 CPDF_Rect rect;
74 if (pStream->KeyExist("Rect"))
75 rect = pStream->GetRect("Rect");
76 else if (pStream->KeyExist("BBox"))
77 rect = pStream->GetRect("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 if (IsValiableRect(rect, pPageDic->GetRect("MediaBox")))
80 pRectArray->Add(rect);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070081
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 pObjectArray->Add(pStream);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070083}
84
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085int ParserAnnots(CPDF_Document* pSourceDoc,
86 CPDF_Dictionary* pPageDic,
87 CPDF_RectArray* pRectArray,
88 CPDF_ObjectArray* pObjectArray,
89 int nUsage) {
90 if (!pSourceDoc || !pPageDic)
91 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 GetContentsRect(pSourceDoc, pPageDic, pRectArray);
94 CPDF_Array* pAnnots = pPageDic->GetArray("Annots");
95 if (!pAnnots)
96 return FLATTEN_NOTHINGTODO;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070097
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 FX_DWORD dwSize = pAnnots->GetCount();
99 for (int i = 0; i < (int)dwSize; i++) {
Dan Sinclairf1251c12015-10-20 16:24:45 -0400100 CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnots->GetElementValue(i));
101 if (!pAnnotDic)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 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 pPage->SetAtReference("Contents", pDocument,
201 pDocument->AddIndirectObject(pNewContents));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700202
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 CFX_ByteString sStream;
204 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
205 pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
206 FALSE);
207 }
208 return;
209 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700210
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 int iType = pContentsObj->GetType();
212 CPDF_Array* pContentsArray = NULL;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700213
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 switch (iType) {
215 case PDFOBJ_STREAM: {
Tom Sepezae51c812015-08-05 12:34:06 -0700216 pContentsArray = new CPDF_Array;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400217 CPDF_Stream* pContents = pContentsObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContents);
219 CPDF_StreamAcc acc;
220 acc.LoadAllData(pContents);
221 CFX_ByteString sStream = "q\n";
222 CFX_ByteString sBody =
223 CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize());
224 sStream = sStream + sBody + "\nQ";
225 pContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
226 FALSE);
227 pContentsArray->AddReference(pDocument, dwObjNum);
228 break;
229 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 case PDFOBJ_ARRAY: {
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400232 pContentsArray = pContentsObj->AsArray();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 break;
234 }
235 default:
236 break;
237 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700238
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239 if (!pContentsArray)
240 return;
241
242 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContentsArray);
243 pPage->SetAtReference("Contents", pDocument, dwObjNum);
244
245 if (!key.IsEmpty()) {
Nico Weber077f1a32015-08-06 15:08:57 -0700246 CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 dwObjNum = pDocument->AddIndirectObject(pNewContents);
248 pContentsArray->AddReference(pDocument, dwObjNum);
249
250 CFX_ByteString sStream;
251 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
252 pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
253 FALSE);
254 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255}
256
Tom Sepez60d909e2015-12-10 15:34:55 -0800257CFX_Matrix GetMatrix(CPDF_Rect rcAnnot,
258 CPDF_Rect rcStream,
259 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260 if (rcStream.IsEmpty())
Tom Sepez60d909e2015-12-10 15:34:55 -0800261 return CFX_Matrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 matrix.TransformRect(rcStream);
264 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
267 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700268
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
270 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
Tom Sepez60d909e2015-12-10 15:34:55 -0800271 return CFX_Matrix(a, 0, 0, d, e, f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700273
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274void GetOffset(FX_FLOAT& fa,
275 FX_FLOAT& fd,
276 FX_FLOAT& fe,
277 FX_FLOAT& ff,
278 CPDF_Rect rcAnnot,
279 CPDF_Rect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800280 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 FX_FLOAT fStreamWidth = 0.0f;
282 FX_FLOAT fStreamHeight = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 if (matrix.a != 0 && matrix.d != 0) {
285 fStreamWidth = rcStream.right - rcStream.left;
286 fStreamHeight = rcStream.top - rcStream.bottom;
287 } else {
288 fStreamWidth = rcStream.top - rcStream.bottom;
289 fStreamHeight = rcStream.right - rcStream.left;
290 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 FX_FLOAT x1 =
293 matrix.a * rcStream.left + matrix.c * rcStream.bottom + matrix.e;
294 FX_FLOAT y1 =
295 matrix.b * rcStream.left + matrix.d * rcStream.bottom + matrix.f;
296 FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matrix.e;
297 FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matrix.f;
298 FX_FLOAT x3 =
299 matrix.a * rcStream.right + matrix.c * rcStream.bottom + matrix.e;
300 FX_FLOAT y3 =
301 matrix.b * rcStream.right + matrix.d * rcStream.bottom + matrix.f;
302 FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matrix.e;
303 FX_FLOAT y4 = matrix.b * rcStream.right + matrix.d * rcStream.top + matrix.f;
Tom Sepez3c3201f2015-05-20 10:20:35 -0700304
Lei Zhang375a8642016-01-11 11:59:17 -0800305 FX_FLOAT left = std::min(std::min(x1, x2), std::min(x3, x4));
306 FX_FLOAT bottom = std::min(std::min(y1, y2), std::min(y3, y4));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700307
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 fa = (rcAnnot.right - rcAnnot.left) / fStreamWidth;
309 fd = (rcAnnot.top - rcAnnot.bottom) / fStreamHeight;
310 fe = rcAnnot.left - left * fa;
311 ff = rcAnnot.bottom - bottom * fd;
312}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700313
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700315 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316 if (!page) {
317 return FLATTEN_FAIL;
318 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700319
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 CPDF_Document* pDocument = pPage->m_pDocument;
321 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700322
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 if (!pDocument || !pPageDict) {
324 return FLATTEN_FAIL;
325 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700326
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327 CPDF_ObjectArray ObjectArray;
328 CPDF_RectArray RectArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700329
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 int iRet = FLATTEN_FAIL;
331 iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
332 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
333 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700334
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 CPDF_Rect rcOriginalCB;
336 CPDF_Rect rcMerger = CalculateRect(&RectArray);
337 CPDF_Rect rcOriginalMB = pPageDict->GetRect("MediaBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700338
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700339 if (pPageDict->KeyExist("CropBox"))
340 rcOriginalMB = pPageDict->GetRect("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700341
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 if (rcOriginalMB.IsEmpty()) {
343 rcOriginalMB = CPDF_Rect(0.0f, 0.0f, 612.0f, 792.0f);
344 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700345
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700346 rcMerger.left =
347 rcMerger.left < rcOriginalMB.left ? rcOriginalMB.left : rcMerger.left;
348 rcMerger.right =
349 rcMerger.right > rcOriginalMB.right ? rcOriginalMB.right : rcMerger.right;
350 rcMerger.top =
351 rcMerger.top > rcOriginalMB.top ? rcOriginalMB.top : rcMerger.top;
352 rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom ? rcOriginalMB.bottom
353 : rcMerger.bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700354
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 if (pPageDict->KeyExist("ArtBox"))
356 rcOriginalCB = pPageDict->GetRect("ArtBox");
357 else
358 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700359
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 if (!rcOriginalMB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700361 CPDF_Array* pMediaBox = new CPDF_Array();
Tom Sepezae51c812015-08-05 12:34:06 -0700362 pMediaBox->Add(new CPDF_Number(rcOriginalMB.left));
363 pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom));
364 pMediaBox->Add(new CPDF_Number(rcOriginalMB.right));
365 pMediaBox->Add(new CPDF_Number(rcOriginalMB.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 pPageDict->SetAt("MediaBox", pMediaBox);
367 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700368
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 if (!rcOriginalCB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700370 CPDF_Array* pCropBox = new CPDF_Array();
371 pCropBox->Add(new CPDF_Number(rcOriginalCB.left));
372 pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom));
373 pCropBox->Add(new CPDF_Number(rcOriginalCB.right));
374 pCropBox->Add(new CPDF_Number(rcOriginalCB.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 pPageDict->SetAt("ArtBox", pCropBox);
376 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700377
Tom Sepez468e5892015-10-13 15:49:36 -0700378 CPDF_Dictionary* pRes = pPageDict->GetDict("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 if (!pRes) {
Tom Sepezae51c812015-08-05 12:34:06 -0700380 pRes = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 pPageDict->SetAt("Resources", pRes);
382 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700383
Nico Weber077f1a32015-08-06 15:08:57 -0700384 CPDF_Stream* pNewXObject = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pNewXObject);
386 CPDF_Dictionary* pPageXObject = pRes->GetDict("XObject");
387 if (!pPageXObject) {
Tom Sepezae51c812015-08-05 12:34:06 -0700388 pPageXObject = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 pRes->SetAt("XObject", pPageXObject);
390 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700391
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392 CFX_ByteString key = "";
393 int nStreams = ObjectArray.GetSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700394
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 if (nStreams > 0) {
396 for (int iKey = 0; /*iKey < 100*/; iKey++) {
397 char sExtend[5] = {};
398 FXSYS_itoa(iKey, sExtend, 10);
399 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700400
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401 if (!pPageXObject->KeyExist(key))
402 break;
403 }
404 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700405
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700407
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700408 CPDF_Dictionary* pNewXORes = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700409
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410 if (!key.IsEmpty()) {
411 pPageXObject->SetAtReference(key, pDocument, dwObjNum);
412 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
Tom Sepezae51c812015-08-05 12:34:06 -0700413 pNewXORes = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414 pNewOXbjectDic->SetAt("Resources", pNewXORes);
415 pNewOXbjectDic->SetAtName("Type", "XObject");
416 pNewOXbjectDic->SetAtName("Subtype", "Form");
417 pNewOXbjectDic->SetAtInteger("FormType", 1);
418 pNewOXbjectDic->SetAtName("Name", "FRM");
419 CPDF_Rect rcBBox = pPageDict->GetRect("ArtBox");
420 pNewOXbjectDic->SetAtRect("BBox", rcBBox);
421 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700422
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423 for (int i = 0; i < nStreams; i++) {
424 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i);
425 if (!pAnnotDic)
426 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700427
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428 CPDF_Rect rcAnnot = pAnnotDic->GetRect("Rect");
429 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700430
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 CFX_ByteString sAnnotState = pAnnotDic->GetString("AS");
432 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDict("AP");
433 if (!pAnnotAP)
434 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700435
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436 CPDF_Stream* pAPStream = pAnnotAP->GetStream("N");
437 if (!pAPStream) {
438 CPDF_Dictionary* pAPDic = pAnnotAP->GetDict("N");
439 if (!pAPDic)
440 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700441
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 if (!sAnnotState.IsEmpty()) {
443 pAPStream = pAPDic->GetStream(sAnnotState);
444 } else {
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800445 auto it = pAPDic->begin();
446 if (it != pAPDic->end()) {
447 CPDF_Object* pFirstObj = it->second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 if (pFirstObj) {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400449 if (pFirstObj->IsReference())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450 pFirstObj = pFirstObj->GetDirect();
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400451 if (!pFirstObj->IsStream())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 continue;
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();
Tom Sepez60d909e2015-12-10 15:34:55 -0800462 CFX_Matrix 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
Tom Sepez60d909e2015-12-10 15:34:55 -0800510 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700511 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}