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 #include "third_party/base/stl_util.h" |
21 | 21 |
22 typedef CFX_ArrayTemplate<CFX_FloatRect> CPDF_RectArray; | |
23 | |
24 enum FPDF_TYPE { MAX, MIN }; | 22 enum FPDF_TYPE { MAX, MIN }; |
25 enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM }; | 23 enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM }; |
26 | 24 |
27 namespace { | 25 namespace { |
28 | 26 |
29 bool IsValiableRect(CFX_FloatRect rect, CFX_FloatRect rcPage) { | 27 bool IsValiableRect(CFX_FloatRect rect, CFX_FloatRect rcPage) { |
30 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f) | 28 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f) |
31 return false; | 29 return false; |
32 | 30 |
33 if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f && | 31 if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f && |
34 rect.bottom == 0.0f) | 32 rect.bottom == 0.0f) |
35 return false; | 33 return false; |
36 | 34 |
37 if (!rcPage.IsEmpty()) { | 35 if (!rcPage.IsEmpty()) { |
38 if (rect.left - rcPage.left < -10.000001f || | 36 if (rect.left - rcPage.left < -10.000001f || |
39 rect.right - rcPage.right > 10.000001f || | 37 rect.right - rcPage.right > 10.000001f || |
40 rect.top - rcPage.top > 10.000001f || | 38 rect.top - rcPage.top > 10.000001f || |
41 rect.bottom - rcPage.bottom < -10.000001f) | 39 rect.bottom - rcPage.bottom < -10.000001f) |
42 return false; | 40 return false; |
43 } | 41 } |
44 | 42 |
45 return true; | 43 return true; |
46 } | 44 } |
47 | 45 |
48 void GetContentsRect(CPDF_Document* pDoc, | 46 void GetContentsRect(CPDF_Document* pDoc, |
49 CPDF_Dictionary* pDict, | 47 CPDF_Dictionary* pDict, |
50 CPDF_RectArray* pRectArray) { | 48 std::vector<CFX_FloatRect>* pRectArray) { |
51 std::unique_ptr<CPDF_Page> pPDFPage(new CPDF_Page(pDoc, pDict, false)); | 49 std::unique_ptr<CPDF_Page> pPDFPage(new CPDF_Page(pDoc, pDict, false)); |
52 pPDFPage->ParseContent(); | 50 pPDFPage->ParseContent(); |
53 | 51 |
54 for (const auto& pPageObject : *pPDFPage->GetPageObjectList()) { | 52 for (const auto& pPageObject : *pPDFPage->GetPageObjectList()) { |
55 CFX_FloatRect rc; | 53 CFX_FloatRect rc; |
56 rc.left = pPageObject->m_Left; | 54 rc.left = pPageObject->m_Left; |
57 rc.right = pPageObject->m_Right; | 55 rc.right = pPageObject->m_Right; |
58 rc.bottom = pPageObject->m_Bottom; | 56 rc.bottom = pPageObject->m_Bottom; |
59 rc.top = pPageObject->m_Top; | 57 rc.top = pPageObject->m_Top; |
60 if (IsValiableRect(rc, pDict->GetRectFor("MediaBox"))) | 58 if (IsValiableRect(rc, pDict->GetRectFor("MediaBox"))) |
61 pRectArray->Add(rc); | 59 pRectArray->push_back(rc); |
62 } | 60 } |
63 } | 61 } |
64 | 62 |
65 void ParserStream(CPDF_Dictionary* pPageDic, | 63 void ParserStream(CPDF_Dictionary* pPageDic, |
66 CPDF_Dictionary* pStream, | 64 CPDF_Dictionary* pStream, |
67 CPDF_RectArray* pRectArray, | 65 std::vector<CFX_FloatRect>* pRectArray, |
68 std::vector<CPDF_Dictionary*>* pObjectArray) { | 66 std::vector<CPDF_Dictionary*>* pObjectArray) { |
69 if (!pStream) | 67 if (!pStream) |
70 return; | 68 return; |
71 CFX_FloatRect rect; | 69 CFX_FloatRect rect; |
72 if (pStream->KeyExist("Rect")) | 70 if (pStream->KeyExist("Rect")) |
73 rect = pStream->GetRectFor("Rect"); | 71 rect = pStream->GetRectFor("Rect"); |
74 else if (pStream->KeyExist("BBox")) | 72 else if (pStream->KeyExist("BBox")) |
75 rect = pStream->GetRectFor("BBox"); | 73 rect = pStream->GetRectFor("BBox"); |
76 | 74 |
77 if (IsValiableRect(rect, pPageDic->GetRectFor("MediaBox"))) | 75 if (IsValiableRect(rect, pPageDic->GetRectFor("MediaBox"))) |
78 pRectArray->Add(rect); | 76 pRectArray->push_back(rect); |
79 | 77 |
80 pObjectArray->push_back(pStream); | 78 pObjectArray->push_back(pStream); |
81 } | 79 } |
82 | 80 |
83 int ParserAnnots(CPDF_Document* pSourceDoc, | 81 int ParserAnnots(CPDF_Document* pSourceDoc, |
84 CPDF_Dictionary* pPageDic, | 82 CPDF_Dictionary* pPageDic, |
85 CPDF_RectArray* pRectArray, | 83 std::vector<CFX_FloatRect>* pRectArray, |
86 std::vector<CPDF_Dictionary*>* pObjectArray, | 84 std::vector<CPDF_Dictionary*>* pObjectArray, |
87 int nUsage) { | 85 int nUsage) { |
88 if (!pSourceDoc || !pPageDic) | 86 if (!pSourceDoc || !pPageDic) |
89 return FLATTEN_FAIL; | 87 return FLATTEN_FAIL; |
90 | 88 |
91 GetContentsRect(pSourceDoc, pPageDic, pRectArray); | 89 GetContentsRect(pSourceDoc, pPageDic, pRectArray); |
92 CPDF_Array* pAnnots = pPageDic->GetArrayFor("Annots"); | 90 CPDF_Array* pAnnots = pPageDic->GetArrayFor("Annots"); |
93 if (!pAnnots) | 91 if (!pAnnots) |
94 return FLATTEN_NOTHINGTODO; | 92 return FLATTEN_NOTHINGTODO; |
95 | 93 |
(...skipping 17 matching lines...) Expand all Loading... |
113 | 111 |
114 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray); | 112 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray); |
115 } else { | 113 } else { |
116 if (nAnnotFlag & ANNOTFLAG_PRINT) | 114 if (nAnnotFlag & ANNOTFLAG_PRINT) |
117 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray); | 115 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray); |
118 } | 116 } |
119 } | 117 } |
120 return FLATTEN_SUCCESS; | 118 return FLATTEN_SUCCESS; |
121 } | 119 } |
122 | 120 |
123 FX_FLOAT GetMinMaxValue(CPDF_RectArray& array, | 121 FX_FLOAT GetMinMaxValue(const std::vector<CFX_FloatRect>& array, |
124 FPDF_TYPE type, | 122 FPDF_TYPE type, |
125 FPDF_VALUE value) { | 123 FPDF_VALUE value) { |
126 int nRects = array.GetSize(); | 124 size_t nRects = array.size(); |
127 FX_FLOAT fRet = 0.0f; | |
128 | |
129 if (nRects <= 0) | 125 if (nRects <= 0) |
130 return 0.0f; | 126 return 0.0f; |
131 | 127 |
132 FX_FLOAT* pArray = new FX_FLOAT[nRects]; | 128 std::vector<FX_FLOAT> pArray(nRects); |
133 switch (value) { | 129 switch (value) { |
134 case LEFT: { | 130 case LEFT: |
135 for (int i = 0; i < nRects; i++) | 131 for (size_t i = 0; i < nRects; i++) |
136 pArray[i] = CFX_FloatRect(array.GetAt(i)).left; | 132 pArray[i] = array[i].left; |
137 | |
138 break; | 133 break; |
139 } | 134 case TOP: |
140 case TOP: { | 135 for (size_t i = 0; i < nRects; i++) |
141 for (int i = 0; i < nRects; i++) | 136 pArray[i] = array[i].top; |
142 pArray[i] = CFX_FloatRect(array.GetAt(i)).top; | |
143 | |
144 break; | 137 break; |
145 } | 138 case RIGHT: |
146 case RIGHT: { | 139 for (size_t i = 0; i < nRects; i++) |
147 for (int i = 0; i < nRects; i++) | 140 pArray[i] = array[i].right; |
148 pArray[i] = CFX_FloatRect(array.GetAt(i)).right; | |
149 | |
150 break; | 141 break; |
151 } | 142 case BOTTOM: |
152 case BOTTOM: { | 143 for (size_t i = 0; i < nRects; i++) |
153 for (int i = 0; i < nRects; i++) | 144 pArray[i] = array[i].bottom; |
154 pArray[i] = CFX_FloatRect(array.GetAt(i)).bottom; | |
155 | |
156 break; | 145 break; |
157 } | |
158 default: | 146 default: |
159 // Not reachable. | 147 // Not reachable. |
160 return 0.0f; | 148 return 0.0f; |
161 } | 149 } |
162 fRet = pArray[0]; | 150 |
| 151 FX_FLOAT fRet = pArray[0]; |
163 if (type == MAX) { | 152 if (type == MAX) { |
164 for (int i = 1; i < nRects; i++) | 153 for (size_t i = 1; i < nRects; i++) |
165 if (fRet <= pArray[i]) | 154 fRet = std::max(fRet, pArray[i]); |
166 fRet = pArray[i]; | |
167 } else { | 155 } else { |
168 for (int i = 1; i < nRects; i++) | 156 for (size_t i = 1; i < nRects; i++) |
169 if (fRet >= pArray[i]) | 157 fRet = std::min(fRet, pArray[i]); |
170 fRet = pArray[i]; | |
171 } | 158 } |
172 delete[] pArray; | |
173 return fRet; | 159 return fRet; |
174 } | 160 } |
175 | 161 |
176 CFX_FloatRect CalculateRect(CPDF_RectArray* pRectArray) { | 162 CFX_FloatRect CalculateRect(std::vector<CFX_FloatRect>* pRectArray) { |
177 CFX_FloatRect rcRet; | 163 CFX_FloatRect rcRet; |
178 | 164 |
179 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT); | 165 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT); |
180 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP); | 166 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP); |
181 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT); | 167 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT); |
182 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM); | 168 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM); |
183 | 169 |
184 return rcRet; | 170 return rcRet; |
185 } | 171 } |
186 | 172 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 | 229 |
244 FX_FLOAT e = rcAnnot.left - rcStream.left * a; | 230 FX_FLOAT e = rcAnnot.left - rcStream.left * a; |
245 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d; | 231 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d; |
246 return CFX_Matrix(a, 0, 0, d, e, f); | 232 return CFX_Matrix(a, 0, 0, d, e, f); |
247 } | 233 } |
248 | 234 |
249 } // namespace | 235 } // namespace |
250 | 236 |
251 DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { | 237 DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { |
252 CPDF_Page* pPage = CPDFPageFromFPDFPage(page); | 238 CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
253 if (!page) { | 239 if (!page) |
254 return FLATTEN_FAIL; | 240 return FLATTEN_FAIL; |
255 } | |
256 | 241 |
257 CPDF_Document* pDocument = pPage->m_pDocument; | 242 CPDF_Document* pDocument = pPage->m_pDocument; |
258 CPDF_Dictionary* pPageDict = pPage->m_pFormDict; | 243 CPDF_Dictionary* pPageDict = pPage->m_pFormDict; |
259 | 244 if (!pDocument || !pPageDict) |
260 if (!pDocument || !pPageDict) { | |
261 return FLATTEN_FAIL; | 245 return FLATTEN_FAIL; |
262 } | |
263 | 246 |
264 std::vector<CPDF_Dictionary*> ObjectArray; | 247 std::vector<CPDF_Dictionary*> ObjectArray; |
265 CPDF_RectArray RectArray; | 248 std::vector<CFX_FloatRect> RectArray; |
266 | 249 int iRet = |
267 int iRet = FLATTEN_FAIL; | 250 ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag); |
268 iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag); | |
269 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL) | 251 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL) |
270 return iRet; | 252 return iRet; |
271 | 253 |
272 CFX_FloatRect rcOriginalCB; | 254 CFX_FloatRect rcOriginalCB; |
273 CFX_FloatRect rcMerger = CalculateRect(&RectArray); | 255 CFX_FloatRect rcMerger = CalculateRect(&RectArray); |
274 CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox"); | 256 CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox"); |
275 | |
276 if (pPageDict->KeyExist("CropBox")) | 257 if (pPageDict->KeyExist("CropBox")) |
277 rcOriginalMB = pPageDict->GetRectFor("CropBox"); | 258 rcOriginalMB = pPageDict->GetRectFor("CropBox"); |
278 | 259 |
279 if (rcOriginalMB.IsEmpty()) { | 260 if (rcOriginalMB.IsEmpty()) |
280 rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f); | 261 rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f); |
281 } | |
282 | 262 |
283 rcMerger.left = | 263 rcMerger.left = std::max(rcMerger.left, rcOriginalMB.left); |
284 rcMerger.left < rcOriginalMB.left ? rcOriginalMB.left : rcMerger.left; | 264 rcMerger.right = std::min(rcMerger.right, rcOriginalMB.right); |
285 rcMerger.right = | 265 rcMerger.bottom = std::max(rcMerger.bottom, rcOriginalMB.bottom); |
286 rcMerger.right > rcOriginalMB.right ? rcOriginalMB.right : rcMerger.right; | 266 rcMerger.top = std::min(rcMerger.top, rcOriginalMB.top); |
287 rcMerger.top = | |
288 rcMerger.top > rcOriginalMB.top ? rcOriginalMB.top : rcMerger.top; | |
289 rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom ? rcOriginalMB.bottom | |
290 : rcMerger.bottom; | |
291 | |
292 if (pPageDict->KeyExist("ArtBox")) | 267 if (pPageDict->KeyExist("ArtBox")) |
293 rcOriginalCB = pPageDict->GetRectFor("ArtBox"); | 268 rcOriginalCB = pPageDict->GetRectFor("ArtBox"); |
294 else | 269 else |
295 rcOriginalCB = rcOriginalMB; | 270 rcOriginalCB = rcOriginalMB; |
296 | 271 |
297 if (!rcOriginalMB.IsEmpty()) { | 272 if (!rcOriginalMB.IsEmpty()) { |
298 CPDF_Array* pMediaBox = new CPDF_Array(); | 273 CPDF_Array* pMediaBox = new CPDF_Array(); |
299 pMediaBox->Add(new CPDF_Number(rcOriginalMB.left)); | 274 pMediaBox->Add(new CPDF_Number(rcOriginalMB.left)); |
300 pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom)); | 275 pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom)); |
301 pMediaBox->Add(new CPDF_Number(rcOriginalMB.right)); | 276 pMediaBox->Add(new CPDF_Number(rcOriginalMB.right)); |
(...skipping 21 matching lines...) Expand all Loading... |
323 | 298 |
324 uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject); | 299 uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject); |
325 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject"); | 300 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject"); |
326 if (!pPageXObject) { | 301 if (!pPageXObject) { |
327 pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); | 302 pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); |
328 pRes->SetFor("XObject", pPageXObject); | 303 pRes->SetFor("XObject", pPageXObject); |
329 } | 304 } |
330 | 305 |
331 CFX_ByteString key = ""; | 306 CFX_ByteString key = ""; |
332 int nStreams = pdfium::CollectionSize<int>(ObjectArray); | 307 int nStreams = pdfium::CollectionSize<int>(ObjectArray); |
333 | |
334 if (nStreams > 0) { | 308 if (nStreams > 0) { |
335 for (int iKey = 0; /*iKey < 100*/; iKey++) { | 309 for (int iKey = 0; /*iKey < 100*/; iKey++) { |
336 char sExtend[5] = {}; | 310 char sExtend[5] = {}; |
337 FXSYS_itoa(iKey, sExtend, 10); | 311 FXSYS_itoa(iKey, sExtend, 10); |
338 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend); | 312 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend); |
339 if (!pPageXObject->KeyExist(key)) | 313 if (!pPageXObject->KeyExist(key)) |
340 break; | 314 break; |
341 } | 315 } |
342 } | 316 } |
343 | 317 |
344 SetPageContents(key, pPageDict, pDocument); | 318 SetPageContents(key, pPageDict, pDocument); |
345 | 319 |
346 CPDF_Dictionary* pNewXORes = nullptr; | 320 CPDF_Dictionary* pNewXORes = nullptr; |
347 | |
348 if (!key.IsEmpty()) { | 321 if (!key.IsEmpty()) { |
349 pPageXObject->SetReferenceFor(key, pDocument, dwObjNum); | 322 pPageXObject->SetReferenceFor(key, pDocument, dwObjNum); |
350 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict(); | 323 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict(); |
351 pNewXORes = new CPDF_Dictionary(pDocument->GetByteStringPool()); | 324 pNewXORes = new CPDF_Dictionary(pDocument->GetByteStringPool()); |
352 pNewOXbjectDic->SetFor("Resources", pNewXORes); | 325 pNewOXbjectDic->SetFor("Resources", pNewXORes); |
353 pNewOXbjectDic->SetNameFor("Type", "XObject"); | 326 pNewOXbjectDic->SetNameFor("Type", "XObject"); |
354 pNewOXbjectDic->SetNameFor("Subtype", "Form"); | 327 pNewOXbjectDic->SetNameFor("Subtype", "Form"); |
355 pNewOXbjectDic->SetIntegerFor("FormType", 1); | 328 pNewOXbjectDic->SetIntegerFor("FormType", 1); |
356 pNewOXbjectDic->SetNameFor("Name", "FRM"); | 329 pNewOXbjectDic->SetNameFor("Name", "FRM"); |
357 CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox"); | 330 CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox"); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 } | 418 } |
446 | 419 |
447 CFX_ByteString sTemp; | 420 CFX_ByteString sTemp; |
448 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix); | 421 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, | 422 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()); | 423 sFormName.c_str()); |
451 sStream += sTemp; | 424 sStream += sTemp; |
452 pNewXObject->SetData(sStream.raw_str(), sStream.GetLength()); | 425 pNewXObject->SetData(sStream.raw_str(), sStream.GetLength()); |
453 } | 426 } |
454 pPageDict->RemoveFor("Annots"); | 427 pPageDict->RemoveFor("Annots"); |
455 | |
456 RectArray.RemoveAll(); | |
457 return FLATTEN_SUCCESS; | 428 return FLATTEN_SUCCESS; |
458 } | 429 } |
OLD | NEW |