blob: c2ecbc84a629a1f09c54d885308e294fc18336ff [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) {
48 std::unique_ptr<CPDF_Page> pPDFPage(new CPDF_Page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 pPDFPage->Load(pDoc, pDict, FALSE);
Tom Sepezb5b2a912016-01-21 11:04:37 -080050 pPDFPage->ParseContent(nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
Tom Sepez2398d892016-02-17 16:46:26 -080052 for (auto& pPageObject : *pPDFPage->GetPageObjectList()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 if (!pPageObject)
54 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070055
Tom Sepez281a9ea2016-02-26 14:24:28 -080056 CFX_FloatRect rc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 rc.left = pPageObject->m_Left;
58 rc.right = pPageObject->m_Right;
59 rc.bottom = pPageObject->m_Bottom;
60 rc.top = pPageObject->m_Top;
Tom Sepez2398d892016-02-17 16:46:26 -080061 if (IsValiableRect(rc, pDict->GetRectBy("MediaBox")))
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 pRectArray->Add(rc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 }
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;
Tom Sepez281a9ea2016-02-26 14:24:28 -080072 CFX_FloatRect rect;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 if (pStream->KeyExist("Rect"))
Wei Li9b761132016-01-29 15:44:20 -080074 rect = pStream->GetRectBy("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 else if (pStream->KeyExist("BBox"))
Wei Li9b761132016-01-29 15:44:20 -080076 rect = pStream->GetRectBy("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077
Wei Li9b761132016-01-29 15:44:20 -080078 if (IsValiableRect(rect, pPageDic->GetRectBy("MediaBox")))
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 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);
Wei Li9b761132016-01-29 15:44:20 -080093 CPDF_Array* pAnnots = pPageDic->GetArrayBy("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 if (!pAnnots)
95 return FLATTEN_NOTHINGTODO;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070096
tsepezc3255f52016-03-25 14:52:27 -070097 uint32_t dwSize = pAnnots->GetCount();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 for (int i = 0; i < (int)dwSize; i++) {
Dan Sinclairf1251c12015-10-20 16:24:45 -040099 CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnots->GetElementValue(i));
100 if (!pAnnotDic)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700102
Wei Li9b761132016-01-29 15:44:20 -0800103 CFX_ByteString sSubtype = pAnnotDic->GetStringBy("Subtype");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 if (sSubtype == "Popup")
105 continue;
106
Wei Li9b761132016-01-29 15:44:20 -0800107 int nAnnotFlag = pAnnotDic->GetIntegerBy("F");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 if (nAnnotFlag & ANNOTFLAG_HIDDEN)
109 continue;
110
111 if (nUsage == FLAT_NORMALDISPLAY) {
112 if (nAnnotFlag & ANNOTFLAG_INVISIBLE)
113 continue;
114
115 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
116 } else {
117 if (nAnnotFlag & ANNOTFLAG_PRINT)
118 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
119 }
120 }
121 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700122}
123
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124FX_FLOAT GetMinMaxValue(CPDF_RectArray& array,
125 FPDF_TYPE type,
126 FPDF_VALUE value) {
127 int nRects = array.GetSize();
128 FX_FLOAT fRet = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 if (nRects <= 0)
131 return 0.0f;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700132
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 FX_FLOAT* pArray = new FX_FLOAT[nRects];
134 switch (value) {
135 case LEFT: {
136 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800137 pArray[i] = CFX_FloatRect(array.GetAt(i)).left;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700138
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 break;
140 }
141 case TOP: {
142 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800143 pArray[i] = CFX_FloatRect(array.GetAt(i)).top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 break;
146 }
147 case RIGHT: {
148 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800149 pArray[i] = CFX_FloatRect(array.GetAt(i)).right;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700150
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 break;
152 }
153 case BOTTOM: {
154 for (int i = 0; i < nRects; i++)
Tom Sepez281a9ea2016-02-26 14:24:28 -0800155 pArray[i] = CFX_FloatRect(array.GetAt(i)).bottom;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700156
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 break;
158 }
159 default:
160 break;
161 }
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());
204 pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
205 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";
223 pContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
224 FALSE);
225 pContentsArray->AddReference(pDocument, dwObjNum);
226 break;
227 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700228
Tom Sepez8e5cd192016-01-26 13:20:26 -0800229 case CPDF_Object::ARRAY: {
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400230 pContentsArray = pContentsObj->AsArray();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 break;
232 }
233 default:
234 break;
235 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700236
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 if (!pContentsArray)
238 return;
239
tsepezc3255f52016-03-25 14:52:27 -0700240 uint32_t dwObjNum = pDocument->AddIndirectObject(pContentsArray);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 pPage->SetAtReference("Contents", pDocument, dwObjNum);
242
243 if (!key.IsEmpty()) {
Nico Weber077f1a32015-08-06 15:08:57 -0700244 CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 dwObjNum = pDocument->AddIndirectObject(pNewContents);
246 pContentsArray->AddReference(pDocument, dwObjNum);
247
248 CFX_ByteString sStream;
249 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
250 pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
251 FALSE);
252 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700253}
254
Tom Sepez281a9ea2016-02-26 14:24:28 -0800255CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot,
256 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800257 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 if (rcStream.IsEmpty())
Tom Sepez60d909e2015-12-10 15:34:55 -0800259 return CFX_Matrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 matrix.TransformRect(rcStream);
262 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
265 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700266
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
268 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
Tom Sepez60d909e2015-12-10 15:34:55 -0800269 return CFX_Matrix(a, 0, 0, d, e, f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272void GetOffset(FX_FLOAT& fa,
273 FX_FLOAT& fd,
274 FX_FLOAT& fe,
275 FX_FLOAT& ff,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800276 CFX_FloatRect rcAnnot,
277 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800278 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 FX_FLOAT fStreamWidth = 0.0f;
280 FX_FLOAT fStreamHeight = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700281
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 if (matrix.a != 0 && matrix.d != 0) {
283 fStreamWidth = rcStream.right - rcStream.left;
284 fStreamHeight = rcStream.top - rcStream.bottom;
285 } else {
286 fStreamWidth = rcStream.top - rcStream.bottom;
287 fStreamHeight = rcStream.right - rcStream.left;
288 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700289
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 FX_FLOAT x1 =
291 matrix.a * rcStream.left + matrix.c * rcStream.bottom + matrix.e;
292 FX_FLOAT y1 =
293 matrix.b * rcStream.left + matrix.d * rcStream.bottom + matrix.f;
294 FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matrix.e;
295 FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matrix.f;
296 FX_FLOAT x3 =
297 matrix.a * rcStream.right + matrix.c * rcStream.bottom + matrix.e;
298 FX_FLOAT y3 =
299 matrix.b * rcStream.right + matrix.d * rcStream.bottom + matrix.f;
300 FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matrix.e;
301 FX_FLOAT y4 = matrix.b * rcStream.right + matrix.d * rcStream.top + matrix.f;
Tom Sepez3c3201f2015-05-20 10:20:35 -0700302
Lei Zhang375a8642016-01-11 11:59:17 -0800303 FX_FLOAT left = std::min(std::min(x1, x2), std::min(x3, x4));
304 FX_FLOAT bottom = std::min(std::min(y1, y2), std::min(y3, y4));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700305
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700306 fa = (rcAnnot.right - rcAnnot.left) / fStreamWidth;
307 fd = (rcAnnot.top - rcAnnot.bottom) / fStreamHeight;
308 fe = rcAnnot.left - left * fa;
309 ff = rcAnnot.bottom - bottom * fd;
310}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700311
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700313 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314 if (!page) {
315 return FLATTEN_FAIL;
316 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700317
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318 CPDF_Document* pDocument = pPage->m_pDocument;
319 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700320
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 if (!pDocument || !pPageDict) {
322 return FLATTEN_FAIL;
323 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700324
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 CPDF_ObjectArray ObjectArray;
326 CPDF_RectArray RectArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700327
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 int iRet = FLATTEN_FAIL;
329 iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
330 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
331 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700332
Tom Sepez281a9ea2016-02-26 14:24:28 -0800333 CFX_FloatRect rcOriginalCB;
334 CFX_FloatRect rcMerger = CalculateRect(&RectArray);
335 CFX_FloatRect rcOriginalMB = pPageDict->GetRectBy("MediaBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700336
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 if (pPageDict->KeyExist("CropBox"))
Wei Li9b761132016-01-29 15:44:20 -0800338 rcOriginalMB = pPageDict->GetRectBy("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700339
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 if (rcOriginalMB.IsEmpty()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800341 rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700343
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 rcMerger.left =
345 rcMerger.left < rcOriginalMB.left ? rcOriginalMB.left : rcMerger.left;
346 rcMerger.right =
347 rcMerger.right > rcOriginalMB.right ? rcOriginalMB.right : rcMerger.right;
348 rcMerger.top =
349 rcMerger.top > rcOriginalMB.top ? rcOriginalMB.top : rcMerger.top;
350 rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom ? rcOriginalMB.bottom
351 : rcMerger.bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353 if (pPageDict->KeyExist("ArtBox"))
Wei Li9b761132016-01-29 15:44:20 -0800354 rcOriginalCB = pPageDict->GetRectBy("ArtBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 else
356 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700357
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700358 if (!rcOriginalMB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700359 CPDF_Array* pMediaBox = new CPDF_Array();
Tom Sepezae51c812015-08-05 12:34:06 -0700360 pMediaBox->Add(new CPDF_Number(rcOriginalMB.left));
361 pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom));
362 pMediaBox->Add(new CPDF_Number(rcOriginalMB.right));
363 pMediaBox->Add(new CPDF_Number(rcOriginalMB.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364 pPageDict->SetAt("MediaBox", pMediaBox);
365 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700366
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 if (!rcOriginalCB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700368 CPDF_Array* pCropBox = new CPDF_Array();
369 pCropBox->Add(new CPDF_Number(rcOriginalCB.left));
370 pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom));
371 pCropBox->Add(new CPDF_Number(rcOriginalCB.right));
372 pCropBox->Add(new CPDF_Number(rcOriginalCB.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373 pPageDict->SetAt("ArtBox", pCropBox);
374 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700375
Wei Li9b761132016-01-29 15:44:20 -0800376 CPDF_Dictionary* pRes = pPageDict->GetDictBy("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377 if (!pRes) {
Tom Sepezae51c812015-08-05 12:34:06 -0700378 pRes = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 pPageDict->SetAt("Resources", pRes);
380 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700381
Nico Weber077f1a32015-08-06 15:08:57 -0700382 CPDF_Stream* pNewXObject = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
tsepezc3255f52016-03-25 14:52:27 -0700383 uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject);
Wei Li9b761132016-01-29 15:44:20 -0800384 CPDF_Dictionary* pPageXObject = pRes->GetDictBy("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 if (!pPageXObject) {
Tom Sepezae51c812015-08-05 12:34:06 -0700386 pPageXObject = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387 pRes->SetAt("XObject", pPageXObject);
388 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700389
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 CFX_ByteString key = "";
391 int nStreams = ObjectArray.GetSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700392
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393 if (nStreams > 0) {
394 for (int iKey = 0; /*iKey < 100*/; iKey++) {
395 char sExtend[5] = {};
396 FXSYS_itoa(iKey, sExtend, 10);
397 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700398
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 if (!pPageXObject->KeyExist(key))
400 break;
401 }
402 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700403
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700405
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 CPDF_Dictionary* pNewXORes = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700407
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700408 if (!key.IsEmpty()) {
409 pPageXObject->SetAtReference(key, pDocument, dwObjNum);
410 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
Tom Sepezae51c812015-08-05 12:34:06 -0700411 pNewXORes = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412 pNewOXbjectDic->SetAt("Resources", pNewXORes);
413 pNewOXbjectDic->SetAtName("Type", "XObject");
414 pNewOXbjectDic->SetAtName("Subtype", "Form");
415 pNewOXbjectDic->SetAtInteger("FormType", 1);
416 pNewOXbjectDic->SetAtName("Name", "FRM");
Tom Sepez281a9ea2016-02-26 14:24:28 -0800417 CFX_FloatRect rcBBox = pPageDict->GetRectBy("ArtBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 pNewOXbjectDic->SetAtRect("BBox", rcBBox);
419 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700420
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421 for (int i = 0; i < nStreams; i++) {
422 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i);
423 if (!pAnnotDic)
424 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700425
Tom Sepez281a9ea2016-02-26 14:24:28 -0800426 CFX_FloatRect rcAnnot = pAnnotDic->GetRectBy("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700428
Wei Li9b761132016-01-29 15:44:20 -0800429 CFX_ByteString sAnnotState = pAnnotDic->GetStringBy("AS");
430 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictBy("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 if (!pAnnotAP)
432 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700433
Wei Li9b761132016-01-29 15:44:20 -0800434 CPDF_Stream* pAPStream = pAnnotAP->GetStreamBy("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 if (!pAPStream) {
Wei Li9b761132016-01-29 15:44:20 -0800436 CPDF_Dictionary* pAPDic = pAnnotAP->GetDictBy("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437 if (!pAPDic)
438 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700439
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440 if (!sAnnotState.IsEmpty()) {
Wei Li9b761132016-01-29 15:44:20 -0800441 pAPStream = pAPDic->GetStreamBy(sAnnotState);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 } else {
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800443 auto it = pAPDic->begin();
444 if (it != pAPDic->end()) {
445 CPDF_Object* pFirstObj = it->second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 if (pFirstObj) {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400447 if (pFirstObj->IsReference())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 pFirstObj = pFirstObj->GetDirect();
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400449 if (!pFirstObj->IsStream())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450 continue;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400451 pAPStream = pFirstObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 }
453 }
454 }
455 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 if (!pAPStream)
457 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700458
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
Wei Li9b761132016-01-29 15:44:20 -0800460 CFX_Matrix matrix = pAPDic->GetMatrixBy("Matrix");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700461
Tom Sepez281a9ea2016-02-26 14:24:28 -0800462 CFX_FloatRect rcStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463 if (pAPDic->KeyExist("Rect"))
Wei Li9b761132016-01-29 15:44:20 -0800464 rcStream = pAPDic->GetRectBy("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465 else if (pAPDic->KeyExist("BBox"))
Wei Li9b761132016-01-29 15:44:20 -0800466 rcStream = pAPDic->GetRectBy("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700467
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700468 if (rcStream.IsEmpty())
469 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700470
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471 CPDF_Object* pObj = pAPStream;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700472
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 if (pObj) {
474 CPDF_Dictionary* pObjDic = pObj->GetDict();
475 if (pObjDic) {
476 pObjDic->SetAtName("Type", "XObject");
477 pObjDic->SetAtName("Subtype", "Form");
478 }
479 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700480
Wei Li9b761132016-01-29 15:44:20 -0800481 CPDF_Dictionary* pXObject = pNewXORes->GetDictBy("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482 if (!pXObject) {
Tom Sepezae51c812015-08-05 12:34:06 -0700483 pXObject = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700484 pNewXORes->SetAt("XObject", pXObject);
485 }
486
487 CFX_ByteString sFormName;
488 sFormName.Format("F%d", i);
tsepezc3255f52016-03-25 14:52:27 -0700489 uint32_t dwObjNum = pDocument->AddIndirectObject(pObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490 pXObject->SetAtReference(sFormName, pDocument, dwObjNum);
491
492 CPDF_StreamAcc acc;
493 acc.LoadAllData(pNewXObject);
494
495 const uint8_t* pData = acc.GetData();
496 CFX_ByteString sStream(pData, acc.GetSize());
497 CFX_ByteString sTemp;
498
499 if (matrix.IsIdentity()) {
500 matrix.a = 1.0f;
501 matrix.b = 0.0f;
502 matrix.c = 0.0f;
503 matrix.d = 1.0f;
504 matrix.e = 0.0f;
505 matrix.f = 0.0f;
506 }
507
Tom Sepez60d909e2015-12-10 15:34:55 -0800508 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
510 sFormName.c_str());
511 sStream += sTemp;
512
513 pNewXObject->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
514 FALSE);
515 }
516 pPageDict->RemoveAt("Annots");
517
518 ObjectArray.RemoveAll();
519 RectArray.RemoveAll();
520
521 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700522}