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 |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT); | 179 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT); |
180 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP); | 180 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP); |
181 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT); | 181 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT); |
182 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM); | 182 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM); |
183 | 183 |
184 return rcRet; | 184 return rcRet; |
185 } | 185 } |
186 | 186 |
187 uint32_t NewIndirectContentsStream(const CFX_ByteString& key, | 187 uint32_t NewIndirectContentsStream(const CFX_ByteString& key, |
188 CPDF_Document* pDocument) { | 188 CPDF_Document* pDocument) { |
189 CPDF_Stream* pNewContents = pDocument->AddIndirectStream( | 189 CPDF_Stream* pNewContents = new CPDF_Stream( |
190 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); | 190 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); |
191 CFX_ByteString sStream; | 191 CFX_ByteString sStream; |
192 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str()); | 192 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str()); |
193 pNewContents->SetData(sStream.raw_str(), sStream.GetLength()); | 193 pNewContents->SetData(sStream.raw_str(), sStream.GetLength()); |
194 return pNewContents->GetObjNum(); | 194 return pDocument->AddIndirectObject(pNewContents); |
195 } | 195 } |
196 | 196 |
197 void SetPageContents(const CFX_ByteString& key, | 197 void SetPageContents(const CFX_ByteString& key, |
198 CPDF_Dictionary* pPage, | 198 CPDF_Dictionary* pPage, |
199 CPDF_Document* pDocument) { | 199 CPDF_Document* pDocument) { |
200 CPDF_Array* pContentsArray = nullptr; | 200 CPDF_Array* pContentsArray = nullptr; |
201 CPDF_Stream* pContentsStream = pPage->GetStreamFor("Contents"); | 201 CPDF_Stream* pContentsStream = pPage->GetStreamFor("Contents"); |
202 if (!pContentsStream) { | 202 if (!pContentsStream) { |
203 pContentsArray = pPage->GetArrayFor("Contents"); | 203 pContentsArray = pPage->GetArrayFor("Contents"); |
204 if (!pContentsArray) { | 204 if (!pContentsArray) { |
205 if (!key.IsEmpty()) { | 205 if (!key.IsEmpty()) { |
206 pPage->SetReferenceFor("Contents", pDocument, | 206 pPage->SetReferenceFor("Contents", pDocument, |
207 NewIndirectContentsStream(key, pDocument)); | 207 NewIndirectContentsStream(key, pDocument)); |
208 } | 208 } |
209 return; | 209 return; |
210 } | 210 } |
211 } | 211 } |
212 pPage->ConvertToIndirectObjectFor("Contents", pDocument); | 212 pPage->ConvertToIndirectObjectFor("Contents", pDocument); |
213 if (!pContentsArray) { | 213 if (!pContentsArray) { |
214 pContentsArray = pDocument->AddIndirectArray(); | 214 pContentsArray = new CPDF_Array; |
215 CPDF_StreamAcc acc; | 215 CPDF_StreamAcc acc; |
216 acc.LoadAllData(pContentsStream); | 216 acc.LoadAllData(pContentsStream); |
217 CFX_ByteString sStream = "q\n"; | 217 CFX_ByteString sStream = "q\n"; |
218 CFX_ByteString sBody = CFX_ByteString(acc.GetData(), acc.GetSize()); | 218 CFX_ByteString sBody = |
| 219 CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize()); |
219 sStream = sStream + sBody + "\nQ"; | 220 sStream = sStream + sBody + "\nQ"; |
220 pContentsStream->SetData(sStream.raw_str(), sStream.GetLength()); | 221 pContentsStream->SetData(sStream.raw_str(), sStream.GetLength()); |
221 pContentsArray->AddReference(pDocument, pContentsStream->GetObjNum()); | 222 pContentsArray->AddReference(pDocument, pContentsStream->GetObjNum()); |
222 pPage->SetReferenceFor("Contents", pDocument, pContentsArray); | 223 pPage->SetReferenceFor("Contents", pDocument, |
| 224 pDocument->AddIndirectObject(pContentsArray)); |
223 } | 225 } |
224 if (!key.IsEmpty()) { | 226 if (!key.IsEmpty()) { |
225 pContentsArray->AddReference(pDocument, | 227 pContentsArray->AddReference(pDocument, |
226 NewIndirectContentsStream(key, pDocument)); | 228 NewIndirectContentsStream(key, pDocument)); |
227 } | 229 } |
228 } | 230 } |
229 | 231 |
230 CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot, | 232 CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot, |
231 CFX_FloatRect rcStream, | 233 CFX_FloatRect rcStream, |
232 const CFX_Matrix& matrix) { | 234 const CFX_Matrix& matrix) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 pCropBox->Add(new CPDF_Number(rcOriginalCB.top)); | 311 pCropBox->Add(new CPDF_Number(rcOriginalCB.top)); |
310 pPageDict->SetFor("ArtBox", pCropBox); | 312 pPageDict->SetFor("ArtBox", pCropBox); |
311 } | 313 } |
312 | 314 |
313 CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources"); | 315 CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources"); |
314 if (!pRes) { | 316 if (!pRes) { |
315 pRes = new CPDF_Dictionary(pDocument->GetByteStringPool()); | 317 pRes = new CPDF_Dictionary(pDocument->GetByteStringPool()); |
316 pPageDict->SetFor("Resources", pRes); | 318 pPageDict->SetFor("Resources", pRes); |
317 } | 319 } |
318 | 320 |
319 CPDF_Stream* pNewXObject = pDocument->AddIndirectStream( | 321 CPDF_Stream* pNewXObject = new CPDF_Stream( |
320 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); | 322 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); |
321 | 323 |
322 uint32_t dwObjNum = pNewXObject->GetObjNum(); | 324 uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject); |
323 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject"); | 325 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject"); |
324 if (!pPageXObject) { | 326 if (!pPageXObject) { |
325 pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); | 327 pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); |
326 pRes->SetFor("XObject", pPageXObject); | 328 pRes->SetFor("XObject", pPageXObject); |
327 } | 329 } |
328 | 330 |
329 CFX_ByteString key = ""; | 331 CFX_ByteString key = ""; |
330 int nStreams = pdfium::CollectionSize<int>(ObjectArray); | 332 int nStreams = pdfium::CollectionSize<int>(ObjectArray); |
331 | 333 |
332 if (nStreams > 0) { | 334 if (nStreams > 0) { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 } | 419 } |
418 | 420 |
419 CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject"); | 421 CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject"); |
420 if (!pXObject) { | 422 if (!pXObject) { |
421 pXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); | 423 pXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); |
422 pNewXORes->SetFor("XObject", pXObject); | 424 pNewXORes->SetFor("XObject", pXObject); |
423 } | 425 } |
424 | 426 |
425 CFX_ByteString sFormName; | 427 CFX_ByteString sFormName; |
426 sFormName.Format("F%d", i); | 428 sFormName.Format("F%d", i); |
427 | |
428 // TODO(tsepez): check |pObj| ownership. | |
429 pXObject->SetReferenceFor(sFormName, pDocument, | 429 pXObject->SetReferenceFor(sFormName, pDocument, |
430 pDocument->AddIndirectObject(UniqueObject(pObj))); | 430 pDocument->AddIndirectObject(pObj)); |
431 | 431 |
432 CPDF_StreamAcc acc; | 432 CPDF_StreamAcc acc; |
433 acc.LoadAllData(pNewXObject); | 433 acc.LoadAllData(pNewXObject); |
434 | 434 |
435 const uint8_t* pData = acc.GetData(); | 435 const uint8_t* pData = acc.GetData(); |
436 CFX_ByteString sStream(pData, acc.GetSize()); | 436 CFX_ByteString sStream(pData, acc.GetSize()); |
437 | 437 |
438 if (matrix.IsIdentity()) { | 438 if (matrix.IsIdentity()) { |
439 matrix.a = 1.0f; | 439 matrix.a = 1.0f; |
440 matrix.b = 0.0f; | 440 matrix.b = 0.0f; |
441 matrix.c = 0.0f; | 441 matrix.c = 0.0f; |
442 matrix.d = 1.0f; | 442 matrix.d = 1.0f; |
443 matrix.e = 0.0f; | 443 matrix.e = 0.0f; |
444 matrix.f = 0.0f; | 444 matrix.f = 0.0f; |
445 } | 445 } |
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 RectArray.RemoveAll(); | 456 RectArray.RemoveAll(); |
457 return FLATTEN_SUCCESS; | 457 return FLATTEN_SUCCESS; |
458 } | 458 } |
OLD | NEW |