| OLD | NEW |
| (Empty) |
| 1 // 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. | |
| 4 | |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
| 6 | |
| 7 #include "core/fpdfapi/fpdf_edit/include/cpdf_creator.h" | |
| 8 #include "core/fpdfapi/fpdf_page/pageint.h" | |
| 9 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | |
| 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" | |
| 11 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" | |
| 12 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" | |
| 13 #include "core/include/fpdfapi/fpdf_page.h" | |
| 14 | |
| 15 CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& ar, CFX_Matrix& matrix) { | |
| 16 ar << matrix.a << " " << matrix.b << " " << matrix.c << " " << matrix.d << " " | |
| 17 << matrix.e << " " << matrix.f; | |
| 18 return ar; | |
| 19 } | |
| 20 | |
| 21 CPDF_PageContentGenerator::CPDF_PageContentGenerator(CPDF_Page* pPage) | |
| 22 : m_pPage(pPage), m_pDocument(m_pPage->m_pDocument) { | |
| 23 for (const auto& pObj : *pPage->GetPageObjectList()) | |
| 24 InsertPageObject(pObj.get()); | |
| 25 } | |
| 26 | |
| 27 FX_BOOL CPDF_PageContentGenerator::InsertPageObject( | |
| 28 CPDF_PageObject* pPageObject) { | |
| 29 return pPageObject && m_pageObjects.Add(pPageObject); | |
| 30 } | |
| 31 | |
| 32 void CPDF_PageContentGenerator::GenerateContent() { | |
| 33 CFX_ByteTextBuf buf; | |
| 34 CPDF_Dictionary* pPageDict = m_pPage->m_pFormDict; | |
| 35 for (int i = 0; i < m_pageObjects.GetSize(); ++i) { | |
| 36 CPDF_PageObject* pPageObj = m_pageObjects[i]; | |
| 37 if (!pPageObj || !pPageObj->IsImage()) { | |
| 38 continue; | |
| 39 } | |
| 40 ProcessImage(buf, pPageObj->AsImage()); | |
| 41 } | |
| 42 CPDF_Object* pContent = | |
| 43 pPageDict ? pPageDict->GetElementValue("Contents") : NULL; | |
| 44 if (pContent) { | |
| 45 pPageDict->RemoveAt("Contents"); | |
| 46 } | |
| 47 CPDF_Stream* pStream = new CPDF_Stream(NULL, 0, NULL); | |
| 48 pStream->SetData(buf.GetBuffer(), buf.GetLength(), FALSE, FALSE); | |
| 49 m_pDocument->AddIndirectObject(pStream); | |
| 50 pPageDict->SetAtReference("Contents", m_pDocument, pStream->GetObjNum()); | |
| 51 } | |
| 52 CFX_ByteString CPDF_PageContentGenerator::RealizeResource( | |
| 53 CPDF_Object* pResourceObj, | |
| 54 const FX_CHAR* szType) { | |
| 55 if (!m_pPage->m_pResources) { | |
| 56 m_pPage->m_pResources = new CPDF_Dictionary; | |
| 57 int objnum = m_pDocument->AddIndirectObject(m_pPage->m_pResources); | |
| 58 m_pPage->m_pFormDict->SetAtReference("Resources", m_pDocument, objnum); | |
| 59 } | |
| 60 CPDF_Dictionary* pResList = m_pPage->m_pResources->GetDictBy(szType); | |
| 61 if (!pResList) { | |
| 62 pResList = new CPDF_Dictionary; | |
| 63 m_pPage->m_pResources->SetAt(szType, pResList); | |
| 64 } | |
| 65 m_pDocument->AddIndirectObject(pResourceObj); | |
| 66 CFX_ByteString name; | |
| 67 int idnum = 1; | |
| 68 while (1) { | |
| 69 name.Format("FX%c%d", szType[0], idnum); | |
| 70 if (!pResList->KeyExist(name)) { | |
| 71 break; | |
| 72 } | |
| 73 idnum++; | |
| 74 } | |
| 75 pResList->AddReference(name, m_pDocument, pResourceObj->GetObjNum()); | |
| 76 return name; | |
| 77 } | |
| 78 void CPDF_PageContentGenerator::ProcessImage(CFX_ByteTextBuf& buf, | |
| 79 CPDF_ImageObject* pImageObj) { | |
| 80 if ((pImageObj->m_Matrix.a == 0 && pImageObj->m_Matrix.b == 0) || | |
| 81 (pImageObj->m_Matrix.c == 0 && pImageObj->m_Matrix.d == 0)) { | |
| 82 return; | |
| 83 } | |
| 84 buf << "q " << pImageObj->m_Matrix << " cm "; | |
| 85 if (!pImageObj->m_pImage->IsInline()) { | |
| 86 CPDF_Stream* pStream = pImageObj->m_pImage->GetStream(); | |
| 87 FX_DWORD dwSavedObjNum = pStream->GetObjNum(); | |
| 88 CFX_ByteString name = RealizeResource(pStream, "XObject"); | |
| 89 if (dwSavedObjNum == 0) { | |
| 90 if (pImageObj->m_pImage) | |
| 91 pImageObj->m_pImage->Release(); | |
| 92 pImageObj->m_pImage = m_pDocument->GetPageData()->GetImage(pStream); | |
| 93 } | |
| 94 buf << "/" << PDF_NameEncode(name) << " Do Q\n"; | |
| 95 } | |
| 96 } | |
| 97 void CPDF_PageContentGenerator::ProcessForm(CFX_ByteTextBuf& buf, | |
| 98 const uint8_t* data, | |
| 99 FX_DWORD size, | |
| 100 CFX_Matrix& matrix) { | |
| 101 if (!data || !size) { | |
| 102 return; | |
| 103 } | |
| 104 CPDF_Stream* pStream = new CPDF_Stream(NULL, 0, NULL); | |
| 105 CPDF_Dictionary* pFormDict = new CPDF_Dictionary; | |
| 106 pFormDict->SetAtName("Type", "XObject"); | |
| 107 pFormDict->SetAtName("Subtype", "Form"); | |
| 108 CFX_FloatRect bbox = m_pPage->GetPageBBox(); | |
| 109 matrix.TransformRect(bbox); | |
| 110 pFormDict->SetAtRect("BBox", bbox); | |
| 111 pStream->InitStream((uint8_t*)data, size, pFormDict); | |
| 112 buf << "q " << matrix << " cm "; | |
| 113 CFX_ByteString name = RealizeResource(pStream, "XObject"); | |
| 114 buf << "/" << PDF_NameEncode(name) << " Do Q\n"; | |
| 115 } | |
| 116 void CPDF_PageContentGenerator::TransformContent(CFX_Matrix& matrix) { | |
| 117 CPDF_Dictionary* pDict = m_pPage->m_pFormDict; | |
| 118 CPDF_Object* pContent = pDict ? pDict->GetElementValue("Contents") : NULL; | |
| 119 if (!pContent) | |
| 120 return; | |
| 121 | |
| 122 CFX_ByteTextBuf buf; | |
| 123 if (CPDF_Array* pArray = pContent->AsArray()) { | |
| 124 int iCount = pArray->GetCount(); | |
| 125 CPDF_StreamAcc** pContentArray = FX_Alloc(CPDF_StreamAcc*, iCount); | |
| 126 int size = 0; | |
| 127 int i = 0; | |
| 128 for (i = 0; i < iCount; ++i) { | |
| 129 pContent = pArray->GetElement(i); | |
| 130 CPDF_Stream* pStream = ToStream(pContent); | |
| 131 if (!pStream) | |
| 132 continue; | |
| 133 | |
| 134 CPDF_StreamAcc* pStreamAcc = new CPDF_StreamAcc(); | |
| 135 pStreamAcc->LoadAllData(pStream); | |
| 136 pContentArray[i] = pStreamAcc; | |
| 137 size += pContentArray[i]->GetSize() + 1; | |
| 138 } | |
| 139 int pos = 0; | |
| 140 uint8_t* pBuf = FX_Alloc(uint8_t, size); | |
| 141 for (i = 0; i < iCount; ++i) { | |
| 142 FXSYS_memcpy(pBuf + pos, pContentArray[i]->GetData(), | |
| 143 pContentArray[i]->GetSize()); | |
| 144 pos += pContentArray[i]->GetSize() + 1; | |
| 145 pBuf[pos - 1] = ' '; | |
| 146 delete pContentArray[i]; | |
| 147 } | |
| 148 ProcessForm(buf, pBuf, size, matrix); | |
| 149 FX_Free(pBuf); | |
| 150 FX_Free(pContentArray); | |
| 151 } else if (CPDF_Stream* pStream = pContent->AsStream()) { | |
| 152 CPDF_StreamAcc contentStream; | |
| 153 contentStream.LoadAllData(pStream); | |
| 154 ProcessForm(buf, contentStream.GetData(), contentStream.GetSize(), matrix); | |
| 155 } | |
| 156 CPDF_Stream* pStream = new CPDF_Stream(NULL, 0, NULL); | |
| 157 pStream->SetData(buf.GetBuffer(), buf.GetLength(), FALSE, FALSE); | |
| 158 m_pDocument->AddIndirectObject(pStream); | |
| 159 m_pPage->m_pFormDict->SetAtReference("Contents", m_pDocument, | |
| 160 pStream->GetObjNum()); | |
| 161 } | |
| OLD | NEW |