Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: core/fpdfapi/fpdf_edit/cpdf_pagecontentgenerator.cpp

Issue 2386263003: Move core/fpdfapi/fpdf_edit to core/fpdfapi/edit (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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/cpdf_pagecontentgenerator.h"
8
9 #include "core/fpdfapi/fpdf_edit/cpdf_creator.h"
10 #include "core/fpdfapi/fpdf_page/cpdf_image.h"
11 #include "core/fpdfapi/fpdf_page/cpdf_imageobject.h"
12 #include "core/fpdfapi/fpdf_page/cpdf_page.h"
13 #include "core/fpdfapi/fpdf_page/cpdf_pageobject.h"
14 #include "core/fpdfapi/fpdf_page/pageint.h"
15 #include "core/fpdfapi/fpdf_parser/cpdf_array.h"
16 #include "core/fpdfapi/fpdf_parser/cpdf_dictionary.h"
17 #include "core/fpdfapi/fpdf_parser/cpdf_document.h"
18 #include "core/fpdfapi/fpdf_parser/cpdf_stream.h"
19 #include "core/fpdfapi/fpdf_parser/cpdf_stream_acc.h"
20 #include "core/fpdfapi/fpdf_parser/fpdf_parser_decode.h"
21
22 CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& ar, CFX_Matrix& matrix) {
23 ar << matrix.a << " " << matrix.b << " " << matrix.c << " " << matrix.d << " "
24 << matrix.e << " " << matrix.f;
25 return ar;
26 }
27
28 CPDF_PageContentGenerator::CPDF_PageContentGenerator(CPDF_Page* pPage)
29 : m_pPage(pPage), m_pDocument(m_pPage->m_pDocument) {
30 for (const auto& pObj : *pPage->GetPageObjectList())
31 InsertPageObject(pObj.get());
32 }
33
34 CPDF_PageContentGenerator::~CPDF_PageContentGenerator() {}
35
36 FX_BOOL CPDF_PageContentGenerator::InsertPageObject(
37 CPDF_PageObject* pPageObject) {
38 return pPageObject && m_pageObjects.Add(pPageObject);
39 }
40
41 void CPDF_PageContentGenerator::GenerateContent() {
42 CFX_ByteTextBuf buf;
43 CPDF_Dictionary* pPageDict = m_pPage->m_pFormDict;
44 for (int i = 0; i < m_pageObjects.GetSize(); ++i) {
45 CPDF_PageObject* pPageObj = m_pageObjects[i];
46 if (!pPageObj || !pPageObj->IsImage()) {
47 continue;
48 }
49 ProcessImage(buf, pPageObj->AsImage());
50 }
51 CPDF_Object* pContent =
52 pPageDict ? pPageDict->GetDirectObjectFor("Contents") : nullptr;
53 if (pContent)
54 pPageDict->RemoveFor("Contents");
55
56 CPDF_Stream* pStream = new CPDF_Stream;
57 pStream->SetData(buf.GetBuffer(), buf.GetLength());
58 pPageDict->SetReferenceFor("Contents", m_pDocument,
59 m_pDocument->AddIndirectObject(pStream));
60 }
61
62 CFX_ByteString CPDF_PageContentGenerator::RealizeResource(
63 CPDF_Object* pResourceObj,
64 const CFX_ByteString& bsType) {
65 if (!m_pPage->m_pResources) {
66 m_pPage->m_pResources =
67 new CPDF_Dictionary(m_pDocument->GetByteStringPool());
68 m_pPage->m_pFormDict->SetReferenceFor(
69 "Resources", m_pDocument,
70 m_pDocument->AddIndirectObject(m_pPage->m_pResources));
71 }
72 CPDF_Dictionary* pResList = m_pPage->m_pResources->GetDictFor(bsType);
73 if (!pResList) {
74 pResList = new CPDF_Dictionary(m_pDocument->GetByteStringPool());
75 m_pPage->m_pResources->SetFor(bsType, pResList);
76 }
77 CFX_ByteString name;
78 int idnum = 1;
79 while (1) {
80 name.Format("FX%c%d", bsType[0], idnum);
81 if (!pResList->KeyExist(name)) {
82 break;
83 }
84 idnum++;
85 }
86 pResList->SetReferenceFor(name, m_pDocument,
87 m_pDocument->AddIndirectObject(pResourceObj));
88 return name;
89 }
90
91 void CPDF_PageContentGenerator::ProcessImage(CFX_ByteTextBuf& buf,
92 CPDF_ImageObject* pImageObj) {
93 if ((pImageObj->m_Matrix.a == 0 && pImageObj->m_Matrix.b == 0) ||
94 (pImageObj->m_Matrix.c == 0 && pImageObj->m_Matrix.d == 0)) {
95 return;
96 }
97 buf << "q " << pImageObj->m_Matrix << " cm ";
98 CPDF_Image* pImage = pImageObj->GetImage();
99 if (!pImage->IsInline()) {
100 CPDF_Stream* pStream = pImage->GetStream();
101 uint32_t dwSavedObjNum = pStream->GetObjNum();
102 CFX_ByteString name = RealizeResource(pStream, "XObject");
103 if (dwSavedObjNum == 0) {
104 pImageObj->SetUnownedImage(m_pDocument->GetPageData()->GetImage(pStream));
105 }
106 buf << "/" << PDF_NameEncode(name) << " Do Q\n";
107 }
108 }
109
110 void CPDF_PageContentGenerator::ProcessForm(CFX_ByteTextBuf& buf,
111 const uint8_t* data,
112 uint32_t size,
113 CFX_Matrix& matrix) {
114 if (!data || !size)
115 return;
116
117 CPDF_Dictionary* pFormDict =
118 new CPDF_Dictionary(m_pDocument->GetByteStringPool());
119 pFormDict->SetNameFor("Type", "XObject");
120 pFormDict->SetNameFor("Subtype", "Form");
121
122 CFX_FloatRect bbox = m_pPage->GetPageBBox();
123 matrix.TransformRect(bbox);
124 pFormDict->SetRectFor("BBox", bbox);
125
126 CPDF_Stream* pStream = new CPDF_Stream;
127 pStream->InitStream(data, size, pFormDict);
128 buf << "q " << matrix << " cm ";
129
130 CFX_ByteString name = RealizeResource(pStream, "XObject");
131 buf << "/" << PDF_NameEncode(name) << " Do Q\n";
132 }
133
134 void CPDF_PageContentGenerator::TransformContent(CFX_Matrix& matrix) {
135 CPDF_Dictionary* pDict = m_pPage->m_pFormDict;
136 CPDF_Object* pContent =
137 pDict ? pDict->GetDirectObjectFor("Contents") : nullptr;
138 if (!pContent)
139 return;
140
141 CFX_ByteTextBuf buf;
142 if (CPDF_Array* pArray = pContent->AsArray()) {
143 size_t iCount = pArray->GetCount();
144 CPDF_StreamAcc** pContentArray = FX_Alloc(CPDF_StreamAcc*, iCount);
145 size_t size = 0;
146 for (size_t i = 0; i < iCount; ++i) {
147 pContent = pArray->GetObjectAt(i);
148 CPDF_Stream* pStream = ToStream(pContent);
149 if (!pStream)
150 continue;
151
152 CPDF_StreamAcc* pStreamAcc = new CPDF_StreamAcc();
153 pStreamAcc->LoadAllData(pStream);
154 pContentArray[i] = pStreamAcc;
155 size += pContentArray[i]->GetSize() + 1;
156 }
157 int pos = 0;
158 uint8_t* pBuf = FX_Alloc(uint8_t, size);
159 for (size_t i = 0; i < iCount; ++i) {
160 FXSYS_memcpy(pBuf + pos, pContentArray[i]->GetData(),
161 pContentArray[i]->GetSize());
162 pos += pContentArray[i]->GetSize() + 1;
163 pBuf[pos - 1] = ' ';
164 delete pContentArray[i];
165 }
166 ProcessForm(buf, pBuf, size, matrix);
167 FX_Free(pBuf);
168 FX_Free(pContentArray);
169 } else if (CPDF_Stream* pStream = pContent->AsStream()) {
170 CPDF_StreamAcc contentStream;
171 contentStream.LoadAllData(pStream);
172 ProcessForm(buf, contentStream.GetData(), contentStream.GetSize(), matrix);
173 }
174 CPDF_Stream* pStream = new CPDF_Stream;
175 pStream->SetData(buf.GetBuffer(), buf.GetLength());
176 m_pPage->m_pFormDict->SetReferenceFor(
177 "Contents", m_pDocument, m_pDocument->AddIndirectObject(pStream));
178 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698