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