OLD | NEW |
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 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "core/fpdfapi/page/cpdf_page.h" | 11 #include "core/fpdfapi/page/cpdf_page.h" |
12 #include "core/fpdfapi/page/cpdf_pageobject.h" | 12 #include "core/fpdfapi/page/cpdf_pageobject.h" |
13 #include "core/fpdfapi/parser/cpdf_array.h" | 13 #include "core/fpdfapi/parser/cpdf_array.h" |
14 #include "core/fpdfapi/parser/cpdf_document.h" | 14 #include "core/fpdfapi/parser/cpdf_document.h" |
15 #include "core/fpdfapi/parser/cpdf_number.h" | 15 #include "core/fpdfapi/parser/cpdf_number.h" |
16 #include "core/fpdfapi/parser/cpdf_stream.h" | 16 #include "core/fpdfapi/parser/cpdf_stream.h" |
17 #include "core/fpdfapi/parser/cpdf_stream_acc.h" | 17 #include "core/fpdfapi/parser/cpdf_stream_acc.h" |
18 #include "core/fpdfdoc/cpdf_annot.h" | 18 #include "core/fpdfdoc/cpdf_annot.h" |
19 #include "fpdfsdk/fsdk_define.h" | 19 #include "fpdfsdk/fsdk_define.h" |
| 20 #include "third_party/base/stl_util.h" |
20 | 21 |
21 typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray; | |
22 typedef CFX_ArrayTemplate<CFX_FloatRect> CPDF_RectArray; | 22 typedef CFX_ArrayTemplate<CFX_FloatRect> CPDF_RectArray; |
23 | 23 |
24 enum FPDF_TYPE { MAX, MIN }; | 24 enum FPDF_TYPE { MAX, MIN }; |
25 enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM }; | 25 enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM }; |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 FX_BOOL IsValiableRect(CFX_FloatRect rect, CFX_FloatRect rcPage) { | 29 FX_BOOL IsValiableRect(CFX_FloatRect rect, CFX_FloatRect rcPage) { |
30 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f) | 30 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f) |
31 return FALSE; | 31 return FALSE; |
(...skipping 26 matching lines...) Expand all Loading... |
58 rc.bottom = pPageObject->m_Bottom; | 58 rc.bottom = pPageObject->m_Bottom; |
59 rc.top = pPageObject->m_Top; | 59 rc.top = pPageObject->m_Top; |
60 if (IsValiableRect(rc, pDict->GetRectFor("MediaBox"))) | 60 if (IsValiableRect(rc, pDict->GetRectFor("MediaBox"))) |
61 pRectArray->Add(rc); | 61 pRectArray->Add(rc); |
62 } | 62 } |
63 } | 63 } |
64 | 64 |
65 void ParserStream(CPDF_Dictionary* pPageDic, | 65 void ParserStream(CPDF_Dictionary* pPageDic, |
66 CPDF_Dictionary* pStream, | 66 CPDF_Dictionary* pStream, |
67 CPDF_RectArray* pRectArray, | 67 CPDF_RectArray* pRectArray, |
68 CPDF_ObjectArray* pObjectArray) { | 68 std::vector<CPDF_Dictionary*>* pObjectArray) { |
69 if (!pStream) | 69 if (!pStream) |
70 return; | 70 return; |
71 CFX_FloatRect rect; | 71 CFX_FloatRect rect; |
72 if (pStream->KeyExist("Rect")) | 72 if (pStream->KeyExist("Rect")) |
73 rect = pStream->GetRectFor("Rect"); | 73 rect = pStream->GetRectFor("Rect"); |
74 else if (pStream->KeyExist("BBox")) | 74 else if (pStream->KeyExist("BBox")) |
75 rect = pStream->GetRectFor("BBox"); | 75 rect = pStream->GetRectFor("BBox"); |
76 | 76 |
77 if (IsValiableRect(rect, pPageDic->GetRectFor("MediaBox"))) | 77 if (IsValiableRect(rect, pPageDic->GetRectFor("MediaBox"))) |
78 pRectArray->Add(rect); | 78 pRectArray->Add(rect); |
79 | 79 |
80 pObjectArray->Add(pStream); | 80 pObjectArray->push_back(pStream); |
81 } | 81 } |
82 | 82 |
83 int ParserAnnots(CPDF_Document* pSourceDoc, | 83 int ParserAnnots(CPDF_Document* pSourceDoc, |
84 CPDF_Dictionary* pPageDic, | 84 CPDF_Dictionary* pPageDic, |
85 CPDF_RectArray* pRectArray, | 85 CPDF_RectArray* pRectArray, |
86 CPDF_ObjectArray* pObjectArray, | 86 std::vector<CPDF_Dictionary*>* pObjectArray, |
87 int nUsage) { | 87 int nUsage) { |
88 if (!pSourceDoc || !pPageDic) | 88 if (!pSourceDoc || !pPageDic) |
89 return FLATTEN_FAIL; | 89 return FLATTEN_FAIL; |
90 | 90 |
91 GetContentsRect(pSourceDoc, pPageDic, pRectArray); | 91 GetContentsRect(pSourceDoc, pPageDic, pRectArray); |
92 CPDF_Array* pAnnots = pPageDic->GetArrayFor("Annots"); | 92 CPDF_Array* pAnnots = pPageDic->GetArrayFor("Annots"); |
93 if (!pAnnots) | 93 if (!pAnnots) |
94 return FLATTEN_NOTHINGTODO; | 94 return FLATTEN_NOTHINGTODO; |
95 | 95 |
96 uint32_t dwSize = pAnnots->GetCount(); | 96 uint32_t dwSize = pAnnots->GetCount(); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 return FLATTEN_FAIL; | 254 return FLATTEN_FAIL; |
255 } | 255 } |
256 | 256 |
257 CPDF_Document* pDocument = pPage->m_pDocument; | 257 CPDF_Document* pDocument = pPage->m_pDocument; |
258 CPDF_Dictionary* pPageDict = pPage->m_pFormDict; | 258 CPDF_Dictionary* pPageDict = pPage->m_pFormDict; |
259 | 259 |
260 if (!pDocument || !pPageDict) { | 260 if (!pDocument || !pPageDict) { |
261 return FLATTEN_FAIL; | 261 return FLATTEN_FAIL; |
262 } | 262 } |
263 | 263 |
264 CPDF_ObjectArray ObjectArray; | 264 std::vector<CPDF_Dictionary*> ObjectArray; |
265 CPDF_RectArray RectArray; | 265 CPDF_RectArray RectArray; |
266 | 266 |
267 int iRet = FLATTEN_FAIL; | 267 int iRet = FLATTEN_FAIL; |
268 iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag); | 268 iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag); |
269 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL) | 269 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL) |
270 return iRet; | 270 return iRet; |
271 | 271 |
272 CFX_FloatRect rcOriginalCB; | 272 CFX_FloatRect rcOriginalCB; |
273 CFX_FloatRect rcMerger = CalculateRect(&RectArray); | 273 CFX_FloatRect rcMerger = CalculateRect(&RectArray); |
274 CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox"); | 274 CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox"); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); | 322 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); |
323 | 323 |
324 uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject); | 324 uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject); |
325 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject"); | 325 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject"); |
326 if (!pPageXObject) { | 326 if (!pPageXObject) { |
327 pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); | 327 pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); |
328 pRes->SetFor("XObject", pPageXObject); | 328 pRes->SetFor("XObject", pPageXObject); |
329 } | 329 } |
330 | 330 |
331 CFX_ByteString key = ""; | 331 CFX_ByteString key = ""; |
332 int nStreams = ObjectArray.GetSize(); | 332 int nStreams = pdfium::CollectionSize<int>(ObjectArray); |
333 | 333 |
334 if (nStreams > 0) { | 334 if (nStreams > 0) { |
335 for (int iKey = 0; /*iKey < 100*/; iKey++) { | 335 for (int iKey = 0; /*iKey < 100*/; iKey++) { |
336 char sExtend[5] = {}; | 336 char sExtend[5] = {}; |
337 FXSYS_itoa(iKey, sExtend, 10); | 337 FXSYS_itoa(iKey, sExtend, 10); |
338 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend); | 338 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend); |
339 if (!pPageXObject->KeyExist(key)) | 339 if (!pPageXObject->KeyExist(key)) |
340 break; | 340 break; |
341 } | 341 } |
342 } | 342 } |
343 | 343 |
344 SetPageContents(key, pPageDict, pDocument); | 344 SetPageContents(key, pPageDict, pDocument); |
345 | 345 |
346 CPDF_Dictionary* pNewXORes = nullptr; | 346 CPDF_Dictionary* pNewXORes = nullptr; |
347 | 347 |
348 if (!key.IsEmpty()) { | 348 if (!key.IsEmpty()) { |
349 pPageXObject->SetReferenceFor(key, pDocument, dwObjNum); | 349 pPageXObject->SetReferenceFor(key, pDocument, dwObjNum); |
350 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict(); | 350 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict(); |
351 pNewXORes = new CPDF_Dictionary(pDocument->GetByteStringPool()); | 351 pNewXORes = new CPDF_Dictionary(pDocument->GetByteStringPool()); |
352 pNewOXbjectDic->SetFor("Resources", pNewXORes); | 352 pNewOXbjectDic->SetFor("Resources", pNewXORes); |
353 pNewOXbjectDic->SetNameFor("Type", "XObject"); | 353 pNewOXbjectDic->SetNameFor("Type", "XObject"); |
354 pNewOXbjectDic->SetNameFor("Subtype", "Form"); | 354 pNewOXbjectDic->SetNameFor("Subtype", "Form"); |
355 pNewOXbjectDic->SetIntegerFor("FormType", 1); | 355 pNewOXbjectDic->SetIntegerFor("FormType", 1); |
356 pNewOXbjectDic->SetNameFor("Name", "FRM"); | 356 pNewOXbjectDic->SetNameFor("Name", "FRM"); |
357 CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox"); | 357 CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox"); |
358 pNewOXbjectDic->SetRectFor("BBox", rcBBox); | 358 pNewOXbjectDic->SetRectFor("BBox", rcBBox); |
359 } | 359 } |
360 | 360 |
361 for (int i = 0; i < nStreams; i++) { | 361 for (int i = 0; i < nStreams; i++) { |
362 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i); | 362 CPDF_Dictionary* pAnnotDic = ObjectArray[i]; |
363 if (!pAnnotDic) | 363 if (!pAnnotDic) |
364 continue; | 364 continue; |
365 | 365 |
366 CFX_FloatRect rcAnnot = pAnnotDic->GetRectFor("Rect"); | 366 CFX_FloatRect rcAnnot = pAnnotDic->GetRectFor("Rect"); |
367 rcAnnot.Normalize(); | 367 rcAnnot.Normalize(); |
368 | 368 |
369 CFX_ByteString sAnnotState = pAnnotDic->GetStringFor("AS"); | 369 CFX_ByteString sAnnotState = pAnnotDic->GetStringFor("AS"); |
370 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictFor("AP"); | 370 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictFor("AP"); |
371 if (!pAnnotAP) | 371 if (!pAnnotAP) |
372 continue; | 372 continue; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 | 446 |
447 CFX_ByteString sTemp; | 447 CFX_ByteString sTemp; |
448 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix); | 448 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix); |
449 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f, | 449 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f, |
450 sFormName.c_str()); | 450 sFormName.c_str()); |
451 sStream += sTemp; | 451 sStream += sTemp; |
452 pNewXObject->SetData(sStream.raw_str(), sStream.GetLength()); | 452 pNewXObject->SetData(sStream.raw_str(), sStream.GetLength()); |
453 } | 453 } |
454 pPageDict->RemoveFor("Annots"); | 454 pPageDict->RemoveFor("Annots"); |
455 | 455 |
456 ObjectArray.RemoveAll(); | |
457 RectArray.RemoveAll(); | 456 RectArray.RemoveAll(); |
458 | |
459 return FLATTEN_SUCCESS; | 457 return FLATTEN_SUCCESS; |
460 } | 458 } |
OLD | NEW |