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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../../include/fpdfapi/fpdf_page.h"
8 #include "../../../include/fpdfapi/fpdf_serial.h"
9 #include "../../../include/fpdfapi/fpdf_module.h"
10 #include "../../../include/fxcodec/fx_codec.h"
11 #include "../fpdf_page/pageint.h"
12 CFX_ByteTextBuf& operator << (CFX_ByteTextBuf& ar, CFX_AffineMatrix& matrix)
14 ar << matrix.a << " " << matrix.b << " " << matrix.c << " " << matrix.d << " " << matrix.e << " " << matrix.f;
17 CPDF_PageContentGenerate::CPDF_PageContentGenerate(CPDF_Page* pPage) : m_pPage(pPage)
21 m_pDocument = m_pPage->m_pDocument;
23 FX_POSITION pos = pPage->GetFirstObjectPosition();
25 InsertPageObject(pPage->GetNextObject(pos));
28 CPDF_PageContentGenerate::~CPDF_PageContentGenerate()
31 FX_BOOL CPDF_PageContentGenerate::InsertPageObject(CPDF_PageObject* pPageObject)
36 return m_pageObjects.Add(pPageObject);
38 void CPDF_PageContentGenerate::GenerateContent()
41 CPDF_Dictionary* pPageDict = m_pPage->m_pFormDict;
42 for (int i = 0; i < m_pageObjects.GetSize(); ++i) {
43 CPDF_PageObject* pPageObj = (CPDF_PageObject*)m_pageObjects[i];
44 if (!pPageObj || pPageObj->m_Type != PDFPAGE_IMAGE) {
47 ProcessImage(buf, (CPDF_ImageObject*)pPageObj);
49 CPDF_Object* pContent = pPageDict ? pPageDict->GetElementValue("Contents") : NULL;
50 if (pContent != NULL) {
51 pPageDict->RemoveAt("Contents");
53 CPDF_Stream* pStream = FX_NEW CPDF_Stream(NULL, 0, NULL);
54 pStream->SetData(buf.GetBuffer(), buf.GetLength(), FALSE, FALSE);
55 m_pDocument->AddIndirectObject(pStream);
56 pPageDict->SetAtReference("Contents", m_pDocument, pStream->GetObjNum());
58 CFX_ByteString CPDF_PageContentGenerate::RealizeResource(CPDF_Object* pResourceObj, const FX_CHAR* szType)
60 if (m_pPage->m_pResources == NULL) {
61 m_pPage->m_pResources = FX_NEW CPDF_Dictionary;
62 int objnum = m_pDocument->AddIndirectObject(m_pPage->m_pResources);
63 m_pPage->m_pFormDict->SetAtReference("Resources", m_pDocument, objnum);
65 CPDF_Dictionary* pResList = m_pPage->m_pResources->GetDict(szType);
66 if (pResList == NULL) {
67 pResList = FX_NEW CPDF_Dictionary;
68 m_pPage->m_pResources->SetAt(szType, pResList);
70 m_pDocument->AddIndirectObject(pResourceObj);
74 name.Format("FX%c%d", szType[0], idnum);
75 if (!pResList->KeyExist(name)) {
80 pResList->AddReference(name, m_pDocument, pResourceObj->GetObjNum());
83 void CPDF_PageContentGenerate::ProcessImage(CFX_ByteTextBuf& buf, CPDF_ImageObject* pImageObj)
85 if ((pImageObj->m_Matrix.a == 0 && pImageObj->m_Matrix.b == 0) ||
86 (pImageObj->m_Matrix.c == 0 && pImageObj->m_Matrix.d == 0)) {
89 buf << "q " << pImageObj->m_Matrix << " cm ";
90 if (!pImageObj->m_pImage->IsInline()) {
91 CPDF_Stream* pStream = pImageObj->m_pImage->GetStream();
92 FX_DWORD dwSavedObjNum = pStream->GetObjNum();
93 CFX_ByteString name = RealizeResource(pStream, "XObject");
94 if (dwSavedObjNum == 0) {
95 if (pImageObj->m_pImage)
96 pImageObj->m_pImage->Release();
97 pImageObj->m_pImage = m_pDocument->GetPageData()->GetImage(pStream);
99 buf << "/" << PDF_NameEncode(name) << " Do Q\n";
102 void CPDF_PageContentGenerate::ProcessForm(CFX_ByteTextBuf& buf, FX_LPCBYTE data, FX_DWORD size, CFX_Matrix& matrix)
104 if (!data || !size) {
107 CPDF_Stream* pStream = FX_NEW CPDF_Stream(NULL, 0, NULL);
108 CPDF_Dictionary* pFormDict = CPDF_Dictionary::Create();
109 pFormDict->SetAtName(FX_BSTR("Type"), FX_BSTR("XObject"));
110 pFormDict->SetAtName(FX_BSTR("Subtype"), FX_BSTR("Form"));
111 CFX_FloatRect bbox = m_pPage->GetPageBBox();
112 matrix.TransformRect(bbox);
113 pFormDict->SetAtRect(FX_BSTR("BBox"), bbox);
114 pStream->InitStream((FX_LPBYTE)data, size, pFormDict);
115 buf << "q " << matrix << " cm ";
116 CFX_ByteString name = RealizeResource(pStream, "XObject");
117 buf << "/" << PDF_NameEncode(name) << " Do Q\n";
119 void CPDF_PageContentGenerate::TransformContent(CFX_Matrix& matrix)
121 CPDF_Dictionary* pDict = m_pPage->m_pFormDict;
122 CPDF_Object* pContent = pDict ? pDict->GetElementValue("Contents") : NULL;
127 int type = pContent->GetType();
128 if (type == PDFOBJ_ARRAY) {
129 CPDF_Array* pArray = (CPDF_Array*)pContent;
130 int iCount = pArray->GetCount();
131 CPDF_StreamAcc** pContentArray = (CPDF_StreamAcc**)FX_Alloc(CPDF_StreamAcc*, iCount);
134 for (i = 0; i < iCount; ++i) {
135 pContent = pArray->GetElement(i);
136 if (!pContent || pContent->GetType() != PDFOBJ_STREAM) {
139 CPDF_StreamAcc* pStream = FX_NEW CPDF_StreamAcc();
140 pStream->LoadAllData((CPDF_Stream*)pContent);
141 pContentArray[i] = pStream;
142 size += pContentArray[i]->GetSize() + 1;
145 FX_LPBYTE pBuf = FX_Alloc(FX_BYTE, size);
146 for (i = 0; i < iCount; ++i) {
147 FXSYS_memcpy32(pBuf + pos, pContentArray[i]->GetData(), pContentArray[i]->GetSize());
148 pos += pContentArray[i]->GetSize() + 1;
150 delete pContentArray[i];
152 ProcessForm(buf, pBuf, size, matrix);
154 FX_Free(pContentArray);
155 } else if (type == PDFOBJ_STREAM) {
156 CPDF_StreamAcc contentStream;
157 contentStream.LoadAllData((CPDF_Stream*)pContent);
158 ProcessForm(buf, contentStream.GetData(), contentStream.GetSize(), matrix);
160 CPDF_Stream* pStream = FX_NEW CPDF_Stream(NULL, 0, NULL);
161 pStream->SetData(buf.GetBuffer(), buf.GetLength(), FALSE, FALSE);
162 m_pDocument->AddIndirectObject(pStream);
163 m_pPage->m_pFormDict->SetAtReference("Contents", m_pDocument, pStream->GetObjNum());