Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(615)

Side by Side Diff: fpdfsdk/src/fpdf_flatten.cpp

Issue 1265503005: clang-format all pdfium code. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: sigh Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "../../public/fpdf_flatten.h" 7 #include "../../public/fpdf_flatten.h"
8 #include "../include/fsdk_define.h" 8 #include "../include/fsdk_define.h"
9 9
10 typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray; 10 typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray;
11 typedef CFX_ArrayTemplate<CPDF_Rect> CPDF_RectArray; 11 typedef CFX_ArrayTemplate<CPDF_Rect> CPDF_RectArray;
12 12
13 enum FPDF_TYPE { MAX, MIN }; 13 enum FPDF_TYPE { MAX, MIN };
14 enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM }; 14 enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM };
15 15
16 FX_BOOL IsValiableRect(CPDF_Rect rect, CPDF_Rect rcPage) 16 FX_BOOL IsValiableRect(CPDF_Rect rect, CPDF_Rect rcPage) {
17 { 17 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f)
18 if ( rect.left - rect.right > 0.000001f || 18 return FALSE;
19 rect.bottom - rect.top > 0.000001f) 19
20 return FALSE; 20 if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f &&
21 21 rect.bottom == 0.0f)
22 if (rect.left == 0.0f && 22 return FALSE;
23 rect.top == 0.0f && 23
24 rect.right == 0.0f && 24 if (!rcPage.IsEmpty()) {
25 rect.bottom == 0.0f) 25 if (rect.left - rcPage.left < -10.000001f ||
26 return FALSE; 26 rect.right - rcPage.right > 10.000001f ||
27 27 rect.top - rcPage.top > 10.000001f ||
28 if (!rcPage.IsEmpty()) 28 rect.bottom - rcPage.bottom < -10.000001f)
29 { 29 return FALSE;
30 if (rect.left - rcPage.left < -10.000001f || 30 }
31 rect.right - rcPage.right > 10.000001f || 31
32 rect.top - rcPage.top > 10.000001f || 32 return TRUE;
33 rect.bottom - rcPage.bottom < -10.000001f) 33 }
34 return FALSE; 34
35 } 35 FX_BOOL GetContentsRect(CPDF_Document* pDoc,
36 36 CPDF_Dictionary* pDict,
37 return TRUE; 37 CPDF_RectArray* pRectArray) {
38 } 38 CPDF_Page* pPDFPage = new CPDF_Page;
39 39 pPDFPage->Load(pDoc, pDict, FALSE);
40 40 pPDFPage->ParseContent();
41 FX_BOOL GetContentsRect( CPDF_Document * pDoc, CPDF_Dictionary* pDict, CPDF_Rect Array * pRectArray ) 41
42 { 42 FX_POSITION pos = pPDFPage->GetFirstObjectPosition();
43 CPDF_Page* pPDFPage = new CPDF_Page; 43
44 pPDFPage->Load( pDoc, pDict, FALSE ); 44 while (pos) {
45 pPDFPage->ParseContent(); 45 CPDF_PageObject* pPageObject = pPDFPage->GetNextObject(pos);
46 46 if (!pPageObject)
47 FX_POSITION pos = pPDFPage->GetFirstObjectPosition(); 47 continue;
48 48
49 while (pos) 49 CPDF_Rect rc;
50 { 50 rc.left = pPageObject->m_Left;
51 CPDF_PageObject* pPageObject = pPDFPage->GetNextObject(pos); 51 rc.right = pPageObject->m_Right;
52 if (!pPageObject)continue; 52 rc.bottom = pPageObject->m_Bottom;
53 53 rc.top = pPageObject->m_Top;
54 CPDF_Rect rc; 54
55 rc.left = pPageObject->m_Left; 55 if (IsValiableRect(rc, pDict->GetRect("MediaBox"))) {
56 rc.right = pPageObject->m_Right; 56 pRectArray->Add(rc);
57 rc.bottom = pPageObject->m_Bottom; 57 }
58 rc.top = pPageObject->m_Top; 58 }
59 59
60 if (IsValiableRect(rc, pDict->GetRect("MediaBox"))) 60 delete pPDFPage;
61 { 61 return TRUE;
62 pRectArray->Add(rc); 62 }
63 } 63
64 } 64 void ParserStream(CPDF_Dictionary* pPageDic,
65 65 CPDF_Dictionary* pStream,
66 delete pPDFPage; 66 CPDF_RectArray* pRectArray,
67 return TRUE; 67 CPDF_ObjectArray* pObjectArray) {
68 } 68 if (!pStream)
69 69 return;
70 70 CPDF_Rect rect;
71 void ParserStream( CPDF_Dictionary * pPageDic, CPDF_Dictionary* pStream, CPDF_Re ctArray * pRectArray, CPDF_ObjectArray * pObjectArray ) 71 if (pStream->KeyExist("Rect"))
72 { 72 rect = pStream->GetRect("Rect");
73 if (!pStream)return; 73 else if (pStream->KeyExist("BBox"))
74 CPDF_Rect rect; 74 rect = pStream->GetRect("BBox");
75 if (pStream->KeyExist("Rect")) 75
76 rect = pStream->GetRect("Rect"); 76 if (IsValiableRect(rect, pPageDic->GetRect("MediaBox")))
77 else if (pStream->KeyExist("BBox")) 77 pRectArray->Add(rect);
78 rect = pStream->GetRect("BBox"); 78
79 79 pObjectArray->Add(pStream);
80 if (IsValiableRect(rect, pPageDic->GetRect("MediaBox"))) 80 }
81 pRectArray->Add(rect); 81
82 82 int ParserAnnots(CPDF_Document* pSourceDoc,
83 pObjectArray->Add(pStream); 83 CPDF_Dictionary* pPageDic,
84 } 84 CPDF_RectArray* pRectArray,
85 85 CPDF_ObjectArray* pObjectArray,
86 86 int nUsage) {
87 int ParserAnnots( CPDF_Document* pSourceDoc, CPDF_Dictionary * pPageDic, CPDF_Re ctArray * pRectArray, CPDF_ObjectArray * pObjectArray, int nUsage) 87 if (!pSourceDoc || !pPageDic)
88 { 88 return FLATTEN_FAIL;
89 if (!pSourceDoc || !pPageDic) 89
90 return FLATTEN_FAIL; 90 GetContentsRect(pSourceDoc, pPageDic, pRectArray);
91 91 CPDF_Array* pAnnots = pPageDic->GetArray("Annots");
92 GetContentsRect( pSourceDoc, pPageDic, pRectArray ); 92 if (!pAnnots)
93 CPDF_Array* pAnnots = pPageDic->GetArray("Annots"); 93 return FLATTEN_NOTHINGTODO;
94 if (!pAnnots) 94
95 return FLATTEN_NOTHINGTODO; 95 FX_DWORD dwSize = pAnnots->GetCount();
96 96 for (int i = 0; i < (int)dwSize; i++) {
97 FX_DWORD dwSize = pAnnots->GetCount(); 97 CPDF_Object* pObj = pAnnots->GetElementValue(i);
98 for (int i = 0; i < (int)dwSize; i++) 98 if (!pObj || pObj->GetType() != PDFOBJ_DICTIONARY)
99 { 99 continue;
100 CPDF_Object* pObj = pAnnots->GetElementValue(i); 100
101 if (!pObj || pObj->GetType() != PDFOBJ_DICTIONARY) 101 CPDF_Dictionary* pAnnotDic = (CPDF_Dictionary*)pObj;
102 continue; 102 CFX_ByteString sSubtype = pAnnotDic->GetString("Subtype");
103 103 if (sSubtype == "Popup")
104 CPDF_Dictionary* pAnnotDic = (CPDF_Dictionary*)pObj; 104 continue;
105 CFX_ByteString sSubtype = pAnnotDic->GetString("Subtype"); 105
106 if (sSubtype == "Popup") 106 int nAnnotFlag = pAnnotDic->GetInteger("F");
107 continue; 107 if (nAnnotFlag & ANNOTFLAG_HIDDEN)
108 108 continue;
109 int nAnnotFlag = pAnnotDic->GetInteger("F"); 109
110 if (nAnnotFlag & ANNOTFLAG_HIDDEN) 110 if (nUsage == FLAT_NORMALDISPLAY) {
111 continue; 111 if (nAnnotFlag & ANNOTFLAG_INVISIBLE)
112 112 continue;
113 if(nUsage == FLAT_NORMALDISPLAY) 113
114 { 114 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
115 if (nAnnotFlag & ANNOTFLAG_INVISIBLE) 115 } else {
116 continue; 116 if (nAnnotFlag & ANNOTFLAG_PRINT)
117 117 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
118 ParserStream( pPageDic, pAnnotDic, pRectArray, pObjectArray ); 118 }
119 }
120 return FLATTEN_SUCCESS;
121 }
122
123 FX_FLOAT GetMinMaxValue(CPDF_RectArray& array,
124 FPDF_TYPE type,
125 FPDF_VALUE value) {
126 int nRects = array.GetSize();
127 FX_FLOAT fRet = 0.0f;
128
129 if (nRects <= 0)
130 return 0.0f;
131
132 FX_FLOAT* pArray = new FX_FLOAT[nRects];
133 switch (value) {
134 case LEFT: {
135 for (int i = 0; i < nRects; i++)
136 pArray[i] = CPDF_Rect(array.GetAt(i)).left;
137
138 break;
139 }
140 case TOP: {
141 for (int i = 0; i < nRects; i++)
142 pArray[i] = CPDF_Rect(array.GetAt(i)).top;
143
144 break;
145 }
146 case RIGHT: {
147 for (int i = 0; i < nRects; i++)
148 pArray[i] = CPDF_Rect(array.GetAt(i)).right;
149
150 break;
151 }
152 case BOTTOM: {
153 for (int i = 0; i < nRects; i++)
154 pArray[i] = CPDF_Rect(array.GetAt(i)).bottom;
155
156 break;
157 }
158 default:
159 break;
160 }
161 fRet = pArray[0];
162 if (type == MAX) {
163 for (int i = 1; i < nRects; i++)
164 if (fRet <= pArray[i])
165 fRet = pArray[i];
166 } else {
167 for (int i = 1; i < nRects; i++)
168 if (fRet >= pArray[i])
169 fRet = pArray[i];
170 }
171 delete[] pArray;
172 return fRet;
173 }
174
175 CPDF_Rect CalculateRect(CPDF_RectArray* pRectArray) {
176 CPDF_Rect rcRet;
177
178 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT);
179 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP);
180 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT);
181 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM);
182
183 return rcRet;
184 }
185
186 void SetPageContents(CFX_ByteString key,
187 CPDF_Dictionary* pPage,
188 CPDF_Document* pDocument) {
189 CPDF_Object* pContentsObj = pPage->GetStream("Contents");
190 if (!pContentsObj) {
191 pContentsObj = pPage->GetArray("Contents");
192 }
193
194 if (!pContentsObj) {
195 // Create a new contents dictionary
196 if (!key.IsEmpty()) {
197 CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
198 pPage->SetAtReference("Contents", pDocument,
199 pDocument->AddIndirectObject(pNewContents));
200
201 CFX_ByteString sStream;
202 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
203 pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
204 FALSE);
205 }
206 return;
207 }
208
209 int iType = pContentsObj->GetType();
210 CPDF_Array* pContentsArray = NULL;
211
212 switch (iType) {
213 case PDFOBJ_STREAM: {
214 pContentsArray = new CPDF_Array;
215 CPDF_Stream* pContents = (CPDF_Stream*)pContentsObj;
216 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContents);
217 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 }
228
229 case PDFOBJ_ARRAY: {
230 pContentsArray = (CPDF_Array*)pContentsObj;
231 break;
232 }
233 default:
234 break;
235 }
236
237 if (!pContentsArray)
238 return;
239
240 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContentsArray);
241 pPage->SetAtReference("Contents", pDocument, dwObjNum);
242
243 if (!key.IsEmpty()) {
244 CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
245 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 }
253 }
254
255 CFX_AffineMatrix GetMatrix(CPDF_Rect rcAnnot,
256 CPDF_Rect rcStream,
257 CFX_AffineMatrix matrix) {
258 if (rcStream.IsEmpty())
259 return CFX_AffineMatrix();
260
261 matrix.TransformRect(rcStream);
262 rcStream.Normalize();
263
264 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
265 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
266
267 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
268 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
269 return CFX_AffineMatrix(a, 0, 0, d, e, f);
270 }
271
272 void GetOffset(FX_FLOAT& fa,
273 FX_FLOAT& fd,
274 FX_FLOAT& fe,
275 FX_FLOAT& ff,
276 CPDF_Rect rcAnnot,
277 CPDF_Rect rcStream,
278 CFX_AffineMatrix matrix) {
279 FX_FLOAT fStreamWidth = 0.0f;
280 FX_FLOAT fStreamHeight = 0.0f;
281
282 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 }
289
290 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;
302
303 FX_FLOAT left = FX_MIN(FX_MIN(x1, x2), FX_MIN(x3, x4));
304 FX_FLOAT bottom = FX_MIN(FX_MIN(y1, y2), FX_MIN(y3, y4));
305
306 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 }
311
312 DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
313 if (!page) {
314 return FLATTEN_FAIL;
315 }
316
317 CPDF_Page* pPage = (CPDF_Page*)(page);
318 CPDF_Document* pDocument = pPage->m_pDocument;
319 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
320
321 if (!pDocument || !pPageDict) {
322 return FLATTEN_FAIL;
323 }
324
325 CPDF_ObjectArray ObjectArray;
326 CPDF_RectArray RectArray;
327
328 int iRet = FLATTEN_FAIL;
329 iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
330 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
331 return iRet;
332
333 CPDF_Rect rcOriginalCB;
334 CPDF_Rect rcMerger = CalculateRect(&RectArray);
335 CPDF_Rect rcOriginalMB = pPageDict->GetRect("MediaBox");
336
337 if (pPageDict->KeyExist("CropBox"))
338 rcOriginalMB = pPageDict->GetRect("CropBox");
339
340 if (rcOriginalMB.IsEmpty()) {
341 rcOriginalMB = CPDF_Rect(0.0f, 0.0f, 612.0f, 792.0f);
342 }
343
344 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;
352
353 if (pPageDict->KeyExist("ArtBox"))
354 rcOriginalCB = pPageDict->GetRect("ArtBox");
355 else
356 rcOriginalCB = rcOriginalMB;
357
358 if (!rcOriginalMB.IsEmpty()) {
359 CPDF_Array* pMediaBox = new CPDF_Array();
360 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));
364 pPageDict->SetAt("MediaBox", pMediaBox);
365 }
366
367 if (!rcOriginalCB.IsEmpty()) {
368 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));
373 pPageDict->SetAt("ArtBox", pCropBox);
374 }
375
376 CPDF_Dictionary* pRes = pPageDict->GetDict("Resources");
377 if (!pRes) {
378 pRes = new CPDF_Dictionary;
379 pPageDict->SetAt("Resources", pRes);
380 }
381
382 CPDF_Stream* pNewXObject = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
383 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pNewXObject);
384 CPDF_Dictionary* pPageXObject = pRes->GetDict("XObject");
385 if (!pPageXObject) {
386 pPageXObject = new CPDF_Dictionary;
387 pRes->SetAt("XObject", pPageXObject);
388 }
389
390 CFX_ByteString key = "";
391 int nStreams = ObjectArray.GetSize();
392
393 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);
398
399 if (!pPageXObject->KeyExist(key))
400 break;
401 }
402 }
403
404 SetPageContents(key, pPageDict, pDocument);
405
406 CPDF_Dictionary* pNewXORes = NULL;
407
408 if (!key.IsEmpty()) {
409 pPageXObject->SetAtReference(key, pDocument, dwObjNum);
410 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
411 pNewXORes = new CPDF_Dictionary;
412 pNewOXbjectDic->SetAt("Resources", pNewXORes);
413 pNewOXbjectDic->SetAtName("Type", "XObject");
414 pNewOXbjectDic->SetAtName("Subtype", "Form");
415 pNewOXbjectDic->SetAtInteger("FormType", 1);
416 pNewOXbjectDic->SetAtName("Name", "FRM");
417 CPDF_Rect rcBBox = pPageDict->GetRect("ArtBox");
418 pNewOXbjectDic->SetAtRect("BBox", rcBBox);
419 }
420
421 for (int i = 0; i < nStreams; i++) {
422 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i);
423 if (!pAnnotDic)
424 continue;
425
426 CPDF_Rect rcAnnot = pAnnotDic->GetRect("Rect");
427 rcAnnot.Normalize();
428
429 CFX_ByteString sAnnotState = pAnnotDic->GetString("AS");
430 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDict("AP");
431 if (!pAnnotAP)
432 continue;
433
434 CPDF_Stream* pAPStream = pAnnotAP->GetStream("N");
435 if (!pAPStream) {
436 CPDF_Dictionary* pAPDic = pAnnotAP->GetDict("N");
437 if (!pAPDic)
438 continue;
439
440 if (!sAnnotState.IsEmpty()) {
441 pAPStream = pAPDic->GetStream(sAnnotState);
442 } else {
443 FX_POSITION pos = pAPDic->GetStartPos();
444 if (pos) {
445 CFX_ByteString sKey;
446 CPDF_Object* pFirstObj = pAPDic->GetNextElement(pos, sKey);
447 if (pFirstObj) {
448 if (pFirstObj->GetType() == PDFOBJ_REFERENCE)
449 pFirstObj = pFirstObj->GetDirect();
450
451 if (pFirstObj->GetType() != PDFOBJ_STREAM)
452 continue;
453
454 pAPStream = (CPDF_Stream*)pFirstObj;
455 }
119 } 456 }
120 else 457 }
121 { 458 }
122 if (nAnnotFlag & ANNOTFLAG_PRINT) 459
123 ParserStream( pPageDic, pAnnotDic, pRectArray, pObjectArray ); 460 if (!pAPStream)
124 } 461 continue;
125 } 462
126 return FLATTEN_SUCCESS; 463 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
127 } 464 CFX_AffineMatrix matrix = pAPDic->GetMatrix("Matrix");
128 465
129 466 CPDF_Rect rcStream;
130 FX_FLOAT GetMinMaxValue( CPDF_RectArray& array, FPDF_TYPE type, FPDF_VALUE value ) 467 if (pAPDic->KeyExist("Rect"))
131 { 468 rcStream = pAPDic->GetRect("Rect");
132 int nRects = array.GetSize(); 469 else if (pAPDic->KeyExist("BBox"))
133 FX_FLOAT fRet = 0.0f; 470 rcStream = pAPDic->GetRect("BBox");
134 471
135 if (nRects <= 0)return 0.0f; 472 if (rcStream.IsEmpty())
136 473 continue;
137 FX_FLOAT* pArray = new FX_FLOAT[nRects]; 474
138 switch(value) 475 CPDF_Object* pObj = pAPStream;
139 { 476
140 case LEFT: 477 if (pObj) {
141 { 478 CPDF_Dictionary* pObjDic = pObj->GetDict();
142 for (int i = 0; i < nRects; i++) 479 if (pObjDic) {
143 pArray[i] = CPDF_Rect(array.GetAt(i)).left; 480 pObjDic->SetAtName("Type", "XObject");
144 481 pObjDic->SetAtName("Subtype", "Form");
145 break; 482 }
146 } 483 }
147 case TOP: 484
148 { 485 CPDF_Dictionary* pXObject = pNewXORes->GetDict("XObject");
149 for (int i = 0; i < nRects; i++) 486 if (!pXObject) {
150 pArray[i] = CPDF_Rect(array.GetAt(i)).top; 487 pXObject = new CPDF_Dictionary;
151 488 pNewXORes->SetAt("XObject", pXObject);
152 break; 489 }
153 } 490
154 case RIGHT: 491 CFX_ByteString sFormName;
155 { 492 sFormName.Format("F%d", i);
156 for (int i = 0; i < nRects; i++) 493 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pObj);
157 pArray[i] = CPDF_Rect(array.GetAt(i)).right; 494 pXObject->SetAtReference(sFormName, pDocument, dwObjNum);
158 495
159 break; 496 CPDF_StreamAcc acc;
160 } 497 acc.LoadAllData(pNewXObject);
161 case BOTTOM: 498
162 { 499 const uint8_t* pData = acc.GetData();
163 for (int i = 0; i < nRects; i++) 500 CFX_ByteString sStream(pData, acc.GetSize());
164 pArray[i] = CPDF_Rect(array.GetAt(i)).bottom; 501 CFX_ByteString sTemp;
165 502
166 break; 503 if (matrix.IsIdentity()) {
167 } 504 matrix.a = 1.0f;
168 default: 505 matrix.b = 0.0f;
169 break; 506 matrix.c = 0.0f;
170 } 507 matrix.d = 1.0f;
171 fRet = pArray[0]; 508 matrix.e = 0.0f;
172 if (type == MAX) 509 matrix.f = 0.0f;
173 { 510 }
174 for (int i = 1; i < nRects; i++) 511
175 if (fRet <= pArray[i]) 512 CFX_AffineMatrix m = GetMatrix(rcAnnot, rcStream, matrix);
176 fRet = pArray[i]; 513 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
177 } 514 sFormName.c_str());
178 else 515 sStream += sTemp;
179 { 516
180 for (int i = 1; i < nRects; i++) 517 pNewXObject->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
181 if (fRet >= pArray[i]) 518 FALSE);
182 fRet = pArray[i]; 519 }
183 } 520 pPageDict->RemoveAt("Annots");
184 delete[] pArray; 521
185 return fRet; 522 ObjectArray.RemoveAll();
186 } 523 RectArray.RemoveAll();
187 524
188 CPDF_Rect CalculateRect( CPDF_RectArray * pRectArray ) 525 return FLATTEN_SUCCESS;
189 { 526 }
190
191 CPDF_Rect rcRet;
192
193 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT);
194 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP);
195 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT);
196 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM);
197
198 return rcRet;
199 }
200
201
202 void SetPageContents(CFX_ByteString key, CPDF_Dictionary* pPage, CPDF_Document* pDocument)
203 {
204 CPDF_Object* pContentsObj = pPage->GetStream("Contents");
205 if (!pContentsObj)
206 {
207 pContentsObj = pPage->GetArray("Contents");
208 }
209
210 if (!pContentsObj)
211 {
212 //Create a new contents dictionary
213 if (!key.IsEmpty())
214 {
215 CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
216 pPage->SetAtReference("Contents", pDocument, pDocument-> AddIndirectObject(pNewContents));
217
218 CFX_ByteString sStream;
219 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str()) ;
220 pNewContents->SetData((const uint8_t*)sStream, sStream.G etLength(), FALSE, FALSE);
221 }
222 return;
223 }
224
225 int iType = pContentsObj->GetType();
226 CPDF_Array* pContentsArray = NULL;
227
228 switch(iType)
229 {
230 case PDFOBJ_STREAM:
231 {
232 pContentsArray = new CPDF_Array;
233 CPDF_Stream* pContents = (CPDF_Stream*)pContentsObj;
234 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pConten ts);
235 CPDF_StreamAcc acc;
236 acc.LoadAllData(pContents);
237 CFX_ByteString sStream = "q\n";
238 CFX_ByteString sBody = CFX_ByteString((const FX_CHAR*)ac c.GetData(), acc.GetSize());
239 sStream = sStream + sBody + "\nQ";
240 pContents->SetData((const uint8_t*)sStream, sStream.GetL ength(), FALSE, FALSE);
241 pContentsArray->AddReference(pDocument, dwObjNum);
242 break;
243 }
244
245 case PDFOBJ_ARRAY:
246 {
247 pContentsArray = (CPDF_Array*)pContentsObj;
248 break;
249 }
250 default:
251 break;
252 }
253
254 if (!pContentsArray)return;
255
256 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContentsArray);
257 pPage->SetAtReference("Contents", pDocument, dwObjNum);
258
259 if (!key.IsEmpty())
260 {
261 CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Di ctionary);
262 dwObjNum = pDocument->AddIndirectObject(pNewContents);
263 pContentsArray->AddReference(pDocument, dwObjNum);
264
265 CFX_ByteString sStream;
266 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
267 pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength (), FALSE, FALSE);
268 }
269 }
270
271 CFX_AffineMatrix GetMatrix(CPDF_Rect rcAnnot, CPDF_Rect rcStream, CFX_AffineMatr ix matrix)
272 {
273 if(rcStream.IsEmpty())
274 return CFX_AffineMatrix();
275
276 matrix.TransformRect(rcStream);
277 rcStream.Normalize();
278
279 FX_FLOAT a = rcAnnot.Width()/rcStream.Width();
280 FX_FLOAT d = rcAnnot.Height()/rcStream.Height();
281
282 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
283 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
284 return CFX_AffineMatrix(a, 0, 0, d, e, f);
285 }
286
287 void GetOffset(FX_FLOAT& fa, FX_FLOAT& fd, FX_FLOAT& fe, FX_FLOAT& ff, CPDF_Rect rcAnnot, CPDF_Rect rcStream, CFX_AffineMatrix matrix)
288 {
289 FX_FLOAT fStreamWidth = 0.0f;
290 FX_FLOAT fStreamHeight = 0.0f;
291
292
293
294 if (matrix.a != 0 && matrix.d != 0)
295 {
296 fStreamWidth = rcStream.right - rcStream.left;
297 fStreamHeight = rcStream.top - rcStream.bottom;
298 }
299 else
300 {
301 fStreamWidth = rcStream.top - rcStream.bottom;
302 fStreamHeight = rcStream.right - rcStream.left;
303 }
304
305 FX_FLOAT x1 = matrix.a * rcStream.left + matrix.c * rcStream.bottom + ma trix.e;
306 FX_FLOAT y1 = matrix.b * rcStream.left + matrix.d * rcStream.bottom + ma trix.f;
307 FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matri x.e;
308 FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matri x.f;
309 FX_FLOAT x3 = matrix.a * rcStream.right + matrix.c * rcStream.bottom + m atrix.e;
310 FX_FLOAT y3 = matrix.b * rcStream.right + matrix.d * rcStream.bottom + m atrix.f;
311 FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matr ix.e;
312 FX_FLOAT y4 = matrix.b * rcStream.right + matrix.d * rcStream.top + matr ix.f;
313
314 FX_FLOAT left = FX_MIN(FX_MIN(x1, x2), FX_MIN(x3, x4));
315 FX_FLOAT bottom = FX_MIN(FX_MIN(y1, y2), FX_MIN(y3, y4));
316
317 fa = (rcAnnot.right - rcAnnot.left)/fStreamWidth;
318 fd = (rcAnnot.top - rcAnnot.bottom)/fStreamHeight;
319 fe = rcAnnot.left - left * fa;
320 ff = rcAnnot.bottom - bottom * fd;
321 }
322
323
324 DLLEXPORT int STDCALL FPDFPage_Flatten( FPDF_PAGE page, int nFlag)
325 {
326 if (!page)
327 {
328 return FLATTEN_FAIL;
329 }
330
331 CPDF_Page * pPage = (CPDF_Page*)( page );
332 CPDF_Document * pDocument = pPage->m_pDocument;
333 CPDF_Dictionary * pPageDict = pPage->m_pFormDict;
334
335 if ( !pDocument || !pPageDict )
336 {
337 return FLATTEN_FAIL;
338 }
339
340 CPDF_ObjectArray ObjectArray;
341 CPDF_RectArray RectArray;
342
343 int iRet = FLATTEN_FAIL;
344 iRet = ParserAnnots( pDocument, pPageDict, &RectArray, &ObjectArray, nFl ag);
345 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
346 return iRet;
347
348 CPDF_Rect rcOriginalCB;
349 CPDF_Rect rcMerger = CalculateRect( &RectArray );
350 CPDF_Rect rcOriginalMB = pPageDict->GetRect("MediaBox");
351
352 if (pPageDict->KeyExist("CropBox"))
353 rcOriginalMB = pPageDict->GetRect("CropBox");
354
355 if (rcOriginalMB.IsEmpty())
356 {
357 rcOriginalMB = CPDF_Rect(0.0f, 0.0f, 612.0f, 792.0f);
358 }
359
360 rcMerger.left = rcMerger.left < rcOriginalMB.left? rcOriginalMB.left : r cMerger.left;
361 rcMerger.right = rcMerger.right > rcOriginalMB.right? rcOriginalMB.right : rcMerger.right;
362 rcMerger.top = rcMerger.top > rcOriginalMB.top? rcOriginalMB.top : rcMer ger.top;
363 rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom? rcOriginalMB.bo ttom : rcMerger.bottom;
364
365 if (pPageDict->KeyExist("ArtBox"))
366 rcOriginalCB = pPageDict->GetRect("ArtBox");
367 else
368 rcOriginalCB = rcOriginalMB;
369
370 if (!rcOriginalMB.IsEmpty())
371 {
372 CPDF_Array* pMediaBox = new CPDF_Array();
373 pMediaBox->Add(new CPDF_Number(rcOriginalMB.left));
374 pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom));
375 pMediaBox->Add(new CPDF_Number(rcOriginalMB.right));
376 pMediaBox->Add(new CPDF_Number(rcOriginalMB.top));
377 pPageDict->SetAt("MediaBox",pMediaBox);
378 }
379
380 if (!rcOriginalCB.IsEmpty())
381 {
382 CPDF_Array* pCropBox = new CPDF_Array();
383 pCropBox->Add(new CPDF_Number(rcOriginalCB.left));
384 pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom));
385 pCropBox->Add(new CPDF_Number(rcOriginalCB.right));
386 pCropBox->Add(new CPDF_Number(rcOriginalCB.top));
387 pPageDict->SetAt("ArtBox", pCropBox);
388 }
389
390 CPDF_Dictionary* pRes = pPageDict->GetDict("Resources");
391 if (!pRes)
392 {
393 pRes = new CPDF_Dictionary;
394 pPageDict->SetAt("Resources", pRes );
395 }
396
397 CPDF_Stream* pNewXObject = new CPDF_Stream(NULL, 0, new CPDF_Dictionary) ;
398 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pNewXObject);
399 CPDF_Dictionary* pPageXObject = pRes->GetDict("XObject");
400 if (!pPageXObject)
401 {
402 pPageXObject = new CPDF_Dictionary;
403 pRes->SetAt("XObject", pPageXObject);
404 }
405
406 CFX_ByteString key = "";
407 int nStreams = ObjectArray.GetSize();
408
409 if (nStreams > 0)
410 {
411 for (int iKey = 0; /*iKey < 100*/; iKey++)
412 {
413 char sExtend[5] = {};
414 FXSYS_itoa(iKey, sExtend, 10);
415 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
416
417 if (!pPageXObject->KeyExist(key))
418 break;
419 }
420 }
421
422 SetPageContents(key, pPageDict, pDocument);
423
424 CPDF_Dictionary* pNewXORes = NULL;
425
426 if (!key.IsEmpty())
427 {
428 pPageXObject->SetAtReference(key, pDocument, dwObjNum);
429 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
430 pNewXORes = new CPDF_Dictionary;
431 pNewOXbjectDic->SetAt("Resources", pNewXORes);
432 pNewOXbjectDic->SetAtName("Type", "XObject");
433 pNewOXbjectDic->SetAtName("Subtype", "Form");
434 pNewOXbjectDic->SetAtInteger("FormType", 1);
435 pNewOXbjectDic->SetAtName("Name", "FRM");
436 CPDF_Rect rcBBox = pPageDict->GetRect("ArtBox");
437 pNewOXbjectDic->SetAtRect("BBox", rcBBox);
438 }
439
440 for (int i = 0; i < nStreams; i++)
441 {
442 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i);
443 if (!pAnnotDic)continue;
444
445 CPDF_Rect rcAnnot = pAnnotDic->GetRect("Rect");
446 rcAnnot.Normalize();
447
448 CFX_ByteString sAnnotState = pAnnotDic->GetString("AS");
449 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDict("AP");
450 if (!pAnnotAP)continue;
451
452 CPDF_Stream* pAPStream = pAnnotAP->GetStream("N");
453 if (!pAPStream)
454 {
455 CPDF_Dictionary* pAPDic = pAnnotAP->GetDict("N");
456 if (!pAPDic)continue;
457
458 if (!sAnnotState.IsEmpty())
459 {
460 pAPStream = pAPDic->GetStream(sAnnotState);
461 }
462 else
463 {
464 FX_POSITION pos = pAPDic->GetStartPos();
465 if (pos)
466 {
467 CFX_ByteString sKey;
468 CPDF_Object* pFirstObj = pAPDic->GetNext Element(pos, sKey);
469 if (pFirstObj)
470 {
471 if (pFirstObj->GetType() == PDFO BJ_REFERENCE)
472 pFirstObj = pFirstObj->G etDirect();
473
474 if (pFirstObj->GetType() != PDFO BJ_STREAM)
475 continue;
476
477 pAPStream = (CPDF_Stream*)pFirst Obj;
478 }
479 }
480 }
481 }
482
483 if (!pAPStream)continue;
484
485 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
486 CFX_AffineMatrix matrix = pAPDic->GetMatrix("Matrix");
487
488 CPDF_Rect rcStream;
489 if (pAPDic->KeyExist("Rect"))
490 rcStream = pAPDic->GetRect("Rect");
491 else if (pAPDic->KeyExist("BBox"))
492 rcStream = pAPDic->GetRect("BBox");
493
494 if (rcStream.IsEmpty())continue;
495
496 CPDF_Object* pObj = pAPStream;
497
498 if (pObj)
499 {
500 CPDF_Dictionary* pObjDic = pObj->GetDict();
501 if (pObjDic)
502 {
503 pObjDic->SetAtName("Type", "XObject");
504 pObjDic->SetAtName("Subtype", "Form");
505 }
506 }
507
508 CPDF_Dictionary* pXObject = pNewXORes->GetDict("XObject");
509 if (!pXObject)
510 {
511 pXObject = new CPDF_Dictionary;
512 pNewXORes->SetAt("XObject", pXObject);
513 }
514
515 CFX_ByteString sFormName;
516 sFormName.Format("F%d", i);
517 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pObj);
518 pXObject->SetAtReference(sFormName, pDocument, dwObjNum);
519
520 CPDF_StreamAcc acc;
521 acc.LoadAllData(pNewXObject);
522
523 const uint8_t* pData = acc.GetData();
524 CFX_ByteString sStream(pData, acc.GetSize());
525 CFX_ByteString sTemp;
526
527 if (matrix.IsIdentity())
528 {
529 matrix.a = 1.0f;
530 matrix.b = 0.0f;
531 matrix.c = 0.0f;
532 matrix.d = 1.0f;
533 matrix.e = 0.0f;
534 matrix.f = 0.0f;
535 }
536
537 CFX_AffineMatrix m = GetMatrix(rcAnnot, rcStream, matrix);
538 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m .f, sFormName.c_str());
539 sStream += sTemp;
540
541 pNewXObject->SetData((const uint8_t*)sStream, sStream.GetLength( ), FALSE, FALSE);
542 }
543 pPageDict->RemoveAt( "Annots" );
544
545 ObjectArray.RemoveAll();
546 RectArray.RemoveAll();
547
548 return FLATTEN_SUCCESS;
549 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698