blob: 27646255c8b83bceb297a99ca8d27b9451abb72a [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_flatten.h"
8
Lei Zhang375a8642016-01-11 11:59:17 -08009#include <algorithm>
10
Dan Sinclair455a4192016-03-16 09:48:56 -040011#include "core/fpdfapi/fpdf_page/include/cpdf_page.h"
Dan Sinclair584b1e62016-03-21 09:15:45 -040012#include "core/fpdfapi/fpdf_page/include/cpdf_pageobject.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040013#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
14#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
15#include "core/fpdfapi/fpdf_parser/include/cpdf_number.h"
dan sinclair61b2fc72016-03-23 19:21:44 -040016#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h"
17#include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080018#include "fpdfsdk/include/fsdk_define.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019
20typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray;
Tom Sepez281a9ea2016-02-26 14:24:28 -080021typedef CFX_ArrayTemplate<CFX_FloatRect> CPDF_RectArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022
23enum FPDF_TYPE { MAX, MIN };
24enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM };
25
Tom Sepez281a9ea2016-02-26 14:24:28 -080026FX_BOOL IsValiableRect(CFX_FloatRect rect, CFX_FloatRect rcPage) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f)
28 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070029
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030 if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f &&
31 rect.bottom == 0.0f)
32 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070033
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 if (!rcPage.IsEmpty()) {
35 if (rect.left - rcPage.left < -10.000001f ||
36 rect.right - rcPage.right > 10.000001f ||
37 rect.top - rcPage.top > 10.000001f ||
38 rect.bottom - rcPage.bottom < -10.000001f)
39 return FALSE;
40 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070041
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043}
44
Tom Sepez2398d892016-02-17 16:46:26 -080045void GetContentsRect(CPDF_Document* pDoc,
46 CPDF_Dictionary* pDict,
47 CPDF_RectArray* pRectArray) {
thestig5cc24652016-04-26 11:46:02 -070048 std::unique_ptr<CPDF_Page> pPDFPage(new CPDF_Page(pDoc, pDict, false));
49 pPDFPage->ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050
Tom Sepez2398d892016-02-17 16:46:26 -080051 for (auto& pPageObject : *pPDFPage->GetPageObjectList()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 if (!pPageObject)
53 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070054
Tom Sepez281a9ea2016-02-26 14:24:28 -080055 CFX_FloatRect rc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 rc.left = pPageObject->m_Left;
57 rc.right = pPageObject->m_Right;
58 rc.bottom = pPageObject->m_Bottom;
59 rc.top = pPageObject->m_Top;
Tom Sepez2398d892016-02-17 16:46:26 -080060 if (IsValiableRect(rc, pDict->GetRectBy("MediaBox")))
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 pRectArray->Add(rc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063}
64
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065void ParserStream(CPDF_Dictionary* pPageDic,
66 CPDF_Dictionary* pStream,
67 CPDF_RectArray* pRectArray,
68 CPDF_ObjectArray* pObjectArray) {
69 if (!pStream)
70 return;
Tom Sepez281a9ea2016-02-26 14:24:28 -080071 CFX_FloatRect rect;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 if (pStream->KeyExist("Rect"))
Wei Li9b761132016-01-29 15:44:20 -080073 rect = pStream->GetRectBy("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 else if (pStream->KeyExist("BBox"))
Wei Li9b761132016-01-29 15:44:20 -080075 rect = pStream->GetRectBy("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076
Wei Li9b761132016-01-29 15:44:20 -080077 if (IsValiableRect(rect, pPageDic->GetRectBy("MediaBox")))
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 pRectArray->Add(rect);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070079
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 pObjectArray->Add(pStream);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070081}
82
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083int ParserAnnots(CPDF_Document* pSourceDoc,
84 CPDF_Dictionary* pPageDic,
85 CPDF_RectArray* pRectArray,
86 CPDF_ObjectArray* pObjectArray,
87 int nUsage) {
88 if (!pSourceDoc || !pPageDic)
89 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 GetContentsRect(pSourceDoc, pPageDic, pRectArray);
Wei Li9b761132016-01-29 15:44:20 -080092 CPDF_Array* pAnnots = pPageDic->GetArrayBy("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 if (!pAnnots)
94 return FLATTEN_NOTHINGTODO;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070095
tsepezc3255f52016-03-25 14:52:27 -070096 uint32_t dwSize = pAnnots->GetCount();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 for (int i = 0; i < (int)dwSize; i++) {
tsepezbd567552016-03-29 14:51:50 -070098 CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnots->GetDirectObjectAt(i));
Dan Sinclairf1251c12015-10-20 16:24:45 -040099 if (!pAnnotDic)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700101
Wei Li9b761132016-01-29 15:44:20 -0800102 CFX_ByteString sSubtype = pAnnotDic->GetStringBy("Subtype");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 if (sSubtype == "Popup")
104 continue;
105
Wei Li9b761132016-01-29 15:44:20 -0800106 int nAnnotFlag = pAnnotDic->GetIntegerBy("F");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 if (nAnnotFlag & ANNOTFLAG_HIDDEN)
108 continue;
109
110 if (nUsage == FLAT_NORMALDISPLAY) {
111 if (nAnnotFlag & ANNOTFLAG_INVISIBLE)
112 continue;
113
114 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
115 } else {
116 if (nAnnotFlag & ANNOTFLAG_PRINT)
117 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
118 }
119 }
120 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700121}
122
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123FX_FLOAT GetMinMaxValue(CPDF_RectArray& array,
124 FPDF_TYPE type,
125 FPDF_VALUE value) {
126 int nRects = array.GetSize();
127 FX_FLOAT fRet = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700128
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 if (nRects <= 0)
130 return 0.0f;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 FX_FLOAT* pArray = new FX_FLOAT[nRects];
133 switch (value) {
134 case LEFT: {
135 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800136 pArray[i] = CFX_FloatRect(array.GetAt(i)).left;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 break;
139 }
140 case TOP: {
141 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800142 pArray[i] = CFX_FloatRect(array.GetAt(i)).top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700143
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 break;
145 }
146 case RIGHT: {
147 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800148 pArray[i] = CFX_FloatRect(array.GetAt(i)).right;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700149
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 break;
151 }
152 case BOTTOM: {
153 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800154 pArray[i] = CFX_FloatRect(array.GetAt(i)).bottom;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700155
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 break;
157 }
158 default:
weili12367cb2016-06-03 11:22:16 -0700159 // Not reachable.
160 return 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 }
162 fRet = pArray[0];
163 if (type == MAX) {
164 for (int i = 1; i < nRects; i++)
165 if (fRet <= pArray[i])
166 fRet = pArray[i];
167 } else {
168 for (int i = 1; i < nRects; i++)
169 if (fRet >= pArray[i])
170 fRet = pArray[i];
171 }
172 delete[] pArray;
173 return fRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700175
Tom Sepez281a9ea2016-02-26 14:24:28 -0800176CFX_FloatRect CalculateRect(CPDF_RectArray* pRectArray) {
177 CFX_FloatRect rcRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700178
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT);
180 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP);
181 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT);
182 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700183
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 return rcRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700185}
186
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187void SetPageContents(CFX_ByteString key,
188 CPDF_Dictionary* pPage,
189 CPDF_Document* pDocument) {
Wei Li9b761132016-01-29 15:44:20 -0800190 CPDF_Object* pContentsObj = pPage->GetStreamBy("Contents");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 if (!pContentsObj) {
Wei Li9b761132016-01-29 15:44:20 -0800192 pContentsObj = pPage->GetArrayBy("Contents");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 if (!pContentsObj) {
196 // Create a new contents dictionary
197 if (!key.IsEmpty()) {
Nico Weber077f1a32015-08-06 15:08:57 -0700198 CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 pPage->SetAtReference("Contents", pDocument,
200 pDocument->AddIndirectObject(pNewContents));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 CFX_ByteString sStream;
203 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
tsepezcfd4d572016-04-08 14:38:54 -0700204 pNewContents->SetData(sStream.raw_str(), sStream.GetLength(), FALSE,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 FALSE);
206 }
207 return;
208 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700209
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 CPDF_Array* pContentsArray = NULL;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700211
Tom Sepez8e5cd192016-01-26 13:20:26 -0800212 switch (pContentsObj->GetType()) {
213 case CPDF_Object::STREAM: {
Tom Sepezae51c812015-08-05 12:34:06 -0700214 pContentsArray = new CPDF_Array;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400215 CPDF_Stream* pContents = pContentsObj->AsStream();
tsepezc3255f52016-03-25 14:52:27 -0700216 uint32_t dwObjNum = pDocument->AddIndirectObject(pContents);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217 CPDF_StreamAcc acc;
218 acc.LoadAllData(pContents);
219 CFX_ByteString sStream = "q\n";
220 CFX_ByteString sBody =
221 CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize());
222 sStream = sStream + sBody + "\nQ";
tsepezcfd4d572016-04-08 14:38:54 -0700223 pContents->SetData(sStream.raw_str(), sStream.GetLength(), FALSE, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 pContentsArray->AddReference(pDocument, dwObjNum);
225 break;
226 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700227
Tom Sepez8e5cd192016-01-26 13:20:26 -0800228 case CPDF_Object::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
tsepezc3255f52016-03-25 14:52:27 -0700239 uint32_t dwObjNum = pDocument->AddIndirectObject(pContentsArray);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 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());
tsepezcfd4d572016-04-08 14:38:54 -0700249 pNewContents->SetData(sStream.raw_str(), sStream.GetLength(), FALSE, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700251}
252
Tom Sepez281a9ea2016-02-26 14:24:28 -0800253CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot,
254 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800255 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 if (rcStream.IsEmpty())
Tom Sepez60d909e2015-12-10 15:34:55 -0800257 return CFX_Matrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 matrix.TransformRect(rcStream);
260 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700261
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
263 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700264
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
266 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
Tom Sepez60d909e2015-12-10 15:34:55 -0800267 return CFX_Matrix(a, 0, 0, d, e, f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700269
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270void GetOffset(FX_FLOAT& fa,
271 FX_FLOAT& fd,
272 FX_FLOAT& fe,
273 FX_FLOAT& ff,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800274 CFX_FloatRect rcAnnot,
275 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800276 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 FX_FLOAT fStreamWidth = 0.0f;
278 FX_FLOAT fStreamHeight = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 if (matrix.a != 0 && matrix.d != 0) {
281 fStreamWidth = rcStream.right - rcStream.left;
282 fStreamHeight = rcStream.top - rcStream.bottom;
283 } else {
284 fStreamWidth = rcStream.top - rcStream.bottom;
285 fStreamHeight = rcStream.right - rcStream.left;
286 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 FX_FLOAT x1 =
289 matrix.a * rcStream.left + matrix.c * rcStream.bottom + matrix.e;
290 FX_FLOAT y1 =
291 matrix.b * rcStream.left + matrix.d * rcStream.bottom + matrix.f;
292 FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matrix.e;
293 FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matrix.f;
294 FX_FLOAT x3 =
295 matrix.a * rcStream.right + matrix.c * rcStream.bottom + matrix.e;
296 FX_FLOAT y3 =
297 matrix.b * rcStream.right + matrix.d * rcStream.bottom + matrix.f;
298 FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matrix.e;
299 FX_FLOAT y4 = matrix.b * rcStream.right + matrix.d * rcStream.top + matrix.f;
Tom Sepez3c3201f2015-05-20 10:20:35 -0700300
Lei Zhang375a8642016-01-11 11:59:17 -0800301 FX_FLOAT left = std::min(std::min(x1, x2), std::min(x3, x4));
302 FX_FLOAT bottom = std::min(std::min(y1, y2), std::min(y3, y4));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700303
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 fa = (rcAnnot.right - rcAnnot.left) / fStreamWidth;
305 fd = (rcAnnot.top - rcAnnot.bottom) / fStreamHeight;
306 fe = rcAnnot.left - left * fa;
307 ff = rcAnnot.bottom - bottom * fd;
308}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700309
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700311 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312 if (!page) {
313 return FLATTEN_FAIL;
314 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700315
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316 CPDF_Document* pDocument = pPage->m_pDocument;
317 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700318
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 if (!pDocument || !pPageDict) {
320 return FLATTEN_FAIL;
321 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700322
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 CPDF_ObjectArray ObjectArray;
324 CPDF_RectArray RectArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700325
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 int iRet = FLATTEN_FAIL;
327 iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
328 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
329 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700330
Tom Sepez281a9ea2016-02-26 14:24:28 -0800331 CFX_FloatRect rcOriginalCB;
332 CFX_FloatRect rcMerger = CalculateRect(&RectArray);
333 CFX_FloatRect rcOriginalMB = pPageDict->GetRectBy("MediaBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700334
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 if (pPageDict->KeyExist("CropBox"))
Wei Li9b761132016-01-29 15:44:20 -0800336 rcOriginalMB = pPageDict->GetRectBy("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700337
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338 if (rcOriginalMB.IsEmpty()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800339 rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700341
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 rcMerger.left =
343 rcMerger.left < rcOriginalMB.left ? rcOriginalMB.left : rcMerger.left;
344 rcMerger.right =
345 rcMerger.right > rcOriginalMB.right ? rcOriginalMB.right : rcMerger.right;
346 rcMerger.top =
347 rcMerger.top > rcOriginalMB.top ? rcOriginalMB.top : rcMerger.top;
348 rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom ? rcOriginalMB.bottom
349 : rcMerger.bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700350
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 if (pPageDict->KeyExist("ArtBox"))
Wei Li9b761132016-01-29 15:44:20 -0800352 rcOriginalCB = pPageDict->GetRectBy("ArtBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353 else
354 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700355
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356 if (!rcOriginalMB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700357 CPDF_Array* pMediaBox = new CPDF_Array();
Tom Sepezae51c812015-08-05 12:34:06 -0700358 pMediaBox->Add(new CPDF_Number(rcOriginalMB.left));
359 pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom));
360 pMediaBox->Add(new CPDF_Number(rcOriginalMB.right));
361 pMediaBox->Add(new CPDF_Number(rcOriginalMB.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362 pPageDict->SetAt("MediaBox", pMediaBox);
363 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700364
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 if (!rcOriginalCB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700366 CPDF_Array* pCropBox = new CPDF_Array();
367 pCropBox->Add(new CPDF_Number(rcOriginalCB.left));
368 pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom));
369 pCropBox->Add(new CPDF_Number(rcOriginalCB.right));
370 pCropBox->Add(new CPDF_Number(rcOriginalCB.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 pPageDict->SetAt("ArtBox", pCropBox);
372 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700373
Wei Li9b761132016-01-29 15:44:20 -0800374 CPDF_Dictionary* pRes = pPageDict->GetDictBy("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 if (!pRes) {
Tom Sepezae51c812015-08-05 12:34:06 -0700376 pRes = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377 pPageDict->SetAt("Resources", pRes);
378 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700379
Nico Weber077f1a32015-08-06 15:08:57 -0700380 CPDF_Stream* pNewXObject = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
tsepezc3255f52016-03-25 14:52:27 -0700381 uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject);
Wei Li9b761132016-01-29 15:44:20 -0800382 CPDF_Dictionary* pPageXObject = pRes->GetDictBy("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700383 if (!pPageXObject) {
Tom Sepezae51c812015-08-05 12:34:06 -0700384 pPageXObject = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 pRes->SetAt("XObject", pPageXObject);
386 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700387
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388 CFX_ByteString key = "";
389 int nStreams = ObjectArray.GetSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700390
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391 if (nStreams > 0) {
392 for (int iKey = 0; /*iKey < 100*/; iKey++) {
393 char sExtend[5] = {};
394 FXSYS_itoa(iKey, sExtend, 10);
395 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
tsepez7b1ccf92016-04-14 11:04:57 -0700396 if (!pPageXObject->KeyExist(key))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 break;
398 }
399 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700400
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700402
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 CPDF_Dictionary* pNewXORes = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700404
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 if (!key.IsEmpty()) {
tsepez7b1ccf92016-04-14 11:04:57 -0700406 pPageXObject->SetAtReference(key, pDocument, dwObjNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700407 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
Tom Sepezae51c812015-08-05 12:34:06 -0700408 pNewXORes = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 pNewOXbjectDic->SetAt("Resources", pNewXORes);
410 pNewOXbjectDic->SetAtName("Type", "XObject");
411 pNewOXbjectDic->SetAtName("Subtype", "Form");
412 pNewOXbjectDic->SetAtInteger("FormType", 1);
413 pNewOXbjectDic->SetAtName("Name", "FRM");
Tom Sepez281a9ea2016-02-26 14:24:28 -0800414 CFX_FloatRect rcBBox = pPageDict->GetRectBy("ArtBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415 pNewOXbjectDic->SetAtRect("BBox", rcBBox);
416 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700417
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 for (int i = 0; i < nStreams; i++) {
419 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i);
420 if (!pAnnotDic)
421 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700422
Tom Sepez281a9ea2016-02-26 14:24:28 -0800423 CFX_FloatRect rcAnnot = pAnnotDic->GetRectBy("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700425
Wei Li9b761132016-01-29 15:44:20 -0800426 CFX_ByteString sAnnotState = pAnnotDic->GetStringBy("AS");
427 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictBy("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428 if (!pAnnotAP)
429 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700430
Wei Li9b761132016-01-29 15:44:20 -0800431 CPDF_Stream* pAPStream = pAnnotAP->GetStreamBy("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700432 if (!pAPStream) {
Wei Li9b761132016-01-29 15:44:20 -0800433 CPDF_Dictionary* pAPDic = pAnnotAP->GetDictBy("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700434 if (!pAPDic)
435 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700436
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437 if (!sAnnotState.IsEmpty()) {
tsepez7b1ccf92016-04-14 11:04:57 -0700438 pAPStream = pAPDic->GetStreamBy(sAnnotState);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 } else {
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800440 auto it = pAPDic->begin();
441 if (it != pAPDic->end()) {
442 CPDF_Object* pFirstObj = it->second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443 if (pFirstObj) {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400444 if (pFirstObj->IsReference())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700445 pFirstObj = pFirstObj->GetDirect();
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400446 if (!pFirstObj->IsStream())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447 continue;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400448 pAPStream = pFirstObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449 }
450 }
451 }
452 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 if (!pAPStream)
454 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700455
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
Wei Li9b761132016-01-29 15:44:20 -0800457 CFX_Matrix matrix = pAPDic->GetMatrixBy("Matrix");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700458
Tom Sepez281a9ea2016-02-26 14:24:28 -0800459 CFX_FloatRect rcStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 if (pAPDic->KeyExist("Rect"))
Wei Li9b761132016-01-29 15:44:20 -0800461 rcStream = pAPDic->GetRectBy("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462 else if (pAPDic->KeyExist("BBox"))
Wei Li9b761132016-01-29 15:44:20 -0800463 rcStream = pAPDic->GetRectBy("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700464
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465 if (rcStream.IsEmpty())
466 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700467
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700468 CPDF_Object* pObj = pAPStream;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700469
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470 if (pObj) {
471 CPDF_Dictionary* pObjDic = pObj->GetDict();
472 if (pObjDic) {
473 pObjDic->SetAtName("Type", "XObject");
474 pObjDic->SetAtName("Subtype", "Form");
475 }
476 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700477
Wei Li9b761132016-01-29 15:44:20 -0800478 CPDF_Dictionary* pXObject = pNewXORes->GetDictBy("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700479 if (!pXObject) {
Tom Sepezae51c812015-08-05 12:34:06 -0700480 pXObject = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481 pNewXORes->SetAt("XObject", pXObject);
482 }
483
484 CFX_ByteString sFormName;
485 sFormName.Format("F%d", i);
weilidb444d22016-06-02 15:48:15 -0700486 uint32_t dwStreamObjNum = pDocument->AddIndirectObject(pObj);
487 pXObject->SetAtReference(sFormName, pDocument, dwStreamObjNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488
489 CPDF_StreamAcc acc;
490 acc.LoadAllData(pNewXObject);
491
492 const uint8_t* pData = acc.GetData();
493 CFX_ByteString sStream(pData, acc.GetSize());
494 CFX_ByteString sTemp;
495
496 if (matrix.IsIdentity()) {
497 matrix.a = 1.0f;
498 matrix.b = 0.0f;
499 matrix.c = 0.0f;
500 matrix.d = 1.0f;
501 matrix.e = 0.0f;
502 matrix.f = 0.0f;
503 }
504
Tom Sepez60d909e2015-12-10 15:34:55 -0800505 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
507 sFormName.c_str());
508 sStream += sTemp;
tsepezcfd4d572016-04-08 14:38:54 -0700509 pNewXObject->SetData(sStream.raw_str(), sStream.GetLength(), FALSE, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510 }
511 pPageDict->RemoveAt("Annots");
512
513 ObjectArray.RemoveAll();
514 RectArray.RemoveAll();
515
516 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700517}