Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cpdf_contentparser.cpp
Go to the documentation of this file.
1// Copyright 2016 The PDFium Authors
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/page/cpdf_contentparser.h"
8
9#include <utility>
10
11#include "constants/page_object.h"
12#include "core/fpdfapi/font/cpdf_type3char.h"
13#include "core/fpdfapi/page/cpdf_allstates.h"
14#include "core/fpdfapi/page/cpdf_page.h"
15#include "core/fpdfapi/page/cpdf_pageobject.h"
16#include "core/fpdfapi/page/cpdf_path.h"
17#include "core/fpdfapi/parser/cpdf_array.h"
18#include "core/fpdfapi/parser/cpdf_dictionary.h"
19#include "core/fpdfapi/parser/cpdf_stream.h"
20#include "core/fpdfapi/parser/cpdf_stream_acc.h"
21#include "core/fxcrt/fixed_size_data_vector.h"
22#include "core/fxcrt/fx_safe_types.h"
23#include "core/fxcrt/pauseindicator_iface.h"
24#include "core/fxcrt/span_util.h"
25#include "core/fxcrt/stl_util.h"
26#include "core/fxge/cfx_fillrenderoptions.h"
27#include "third_party/base/check.h"
28#include "third_party/base/check_op.h"
29
31 : m_CurrentStage(Stage::kGetContent), m_pPageObjectHolder(pPage) {
32 DCHECK(pPage);
33 if (!pPage->GetDocument()) {
34 m_CurrentStage = Stage::kComplete;
35 return;
36 }
37
38 RetainPtr<CPDF_Object> pContent =
39 pPage->GetMutableDict()->GetMutableDirectObjectFor(
40 pdfium::page_object::kContents);
41 if (!pContent) {
42 HandlePageContentFailure();
43 return;
44 }
45
46 const CPDF_Stream* pStream = pContent->AsStream();
47 if (pStream) {
48 HandlePageContentStream(pStream);
49 return;
50 }
51
52 const CPDF_Array* pArray = pContent->AsArray();
53 if (pArray && HandlePageContentArray(pArray))
54 return;
55
56 HandlePageContentFailure();
57}
58
60 RetainPtr<const CPDF_Stream> pStream,
61 CPDF_PageObjectHolder* pPageObjectHolder,
62 const CPDF_AllStates* pGraphicStates,
63 const CFX_Matrix* pParentMatrix,
64 CPDF_Type3Char* pType3Char,
65 CPDF_Form::RecursionState* recursion_state)
66 : m_CurrentStage(Stage::kParse),
69 DCHECK(m_pPageObjectHolder);
70 CFX_Matrix form_matrix =
71 m_pPageObjectHolder->GetDict()->GetMatrixFor("Matrix");
72 if (pGraphicStates)
73 form_matrix.Concat(pGraphicStates->current_transformation_matrix());
74
75 RetainPtr<const CPDF_Array> pBBox =
76 m_pPageObjectHolder->GetDict()->GetArrayFor("BBox");
77 CFX_FloatRect form_bbox;
78 CPDF_Path ClipPath;
79 if (pBBox) {
80 form_bbox = pBBox->GetRect();
81 ClipPath.Emplace();
82 ClipPath.AppendFloatRect(form_bbox);
83 ClipPath.Transform(form_matrix);
84 if (pParentMatrix)
85 ClipPath.Transform(*pParentMatrix);
86
87 form_bbox = form_matrix.TransformRect(form_bbox);
88 if (pParentMatrix)
89 form_bbox = pParentMatrix->TransformRect(form_bbox);
90 }
91
92 RetainPtr<CPDF_Dictionary> pResources =
93 m_pPageObjectHolder->GetMutableDict()->GetMutableDictFor("Resources");
94 m_pParser = std::make_unique<CPDF_StreamContentParser>(
95 m_pPageObjectHolder->GetDocument(),
96 m_pPageObjectHolder->GetMutablePageResources(),
97 m_pPageObjectHolder->GetMutableResources(), pParentMatrix,
98 m_pPageObjectHolder, std::move(pResources), form_bbox, pGraphicStates,
99 recursion_state);
100 m_pParser->GetCurStates()->set_current_transformation_matrix(form_matrix);
101 m_pParser->GetCurStates()->set_parent_matrix(form_matrix);
102 if (ClipPath.HasRef()) {
103 m_pParser->GetCurStates()->mutable_clip_path().AppendPathWithAutoMerge(
104 ClipPath, CFX_FillRenderOptions::FillType::kWinding);
105 }
106 if (m_pPageObjectHolder->GetTransparency().IsGroup()) {
107 CPDF_GeneralState& state =
108 m_pParser->GetCurStates()->mutable_general_state();
110 state.SetStrokeAlpha(1.0f);
111 state.SetFillAlpha(1.0f);
112 state.SetSoftMask(nullptr);
113 }
114 m_pSingleStream = pdfium::MakeRetain<CPDF_StreamAcc>(std::move(pStream));
115 m_pSingleStream->LoadAllDataFiltered();
116 m_Data = m_pSingleStream->GetSpan();
117}
118
120
121// Returning |true| means that there is more content to be processed and
122// Continue() should be called again. Returning |false| means that we've
123// completed the parse and Continue() is complete.
125 while (m_CurrentStage == Stage::kGetContent) {
126 m_CurrentStage = GetContent();
127 if (pPause && pPause->NeedToPauseNow())
128 return true;
129 }
130
131 if (m_CurrentStage == Stage::kPrepareContent)
132 m_CurrentStage = PrepareContent();
133
134 while (m_CurrentStage == Stage::kParse) {
135 m_CurrentStage = Parse();
136 if (pPause && pPause->NeedToPauseNow())
137 return true;
138 }
139
140 if (m_CurrentStage == Stage::kCheckClip)
141 m_CurrentStage = CheckClip();
142
143 DCHECK_EQ(m_CurrentStage, Stage::kComplete);
144 return false;
145}
146
147CPDF_ContentParser::Stage CPDF_ContentParser::GetContent() {
148 DCHECK_EQ(m_CurrentStage, Stage::kGetContent);
149 DCHECK(m_pPageObjectHolder->IsPage());
150 RetainPtr<const CPDF_Array> pContent =
151 m_pPageObjectHolder->GetDict()->GetArrayFor(
152 pdfium::page_object::kContents);
153 RetainPtr<const CPDF_Stream> pStreamObj = ToStream(
154 pContent ? pContent->GetDirectObjectAt(m_CurrentOffset) : nullptr);
155 m_StreamArray[m_CurrentOffset] =
156 pdfium::MakeRetain<CPDF_StreamAcc>(std::move(pStreamObj));
157 m_StreamArray[m_CurrentOffset]->LoadAllDataFiltered();
158 m_CurrentOffset++;
159
160 return m_CurrentOffset == m_nStreams ? Stage::kPrepareContent
161 : Stage::kGetContent;
162}
163
164CPDF_ContentParser::Stage CPDF_ContentParser::PrepareContent() {
165 m_CurrentOffset = 0;
166
167 if (m_StreamArray.empty()) {
168 m_Data = m_pSingleStream->GetSpan();
169 return Stage::kParse;
170 }
171
172 FX_SAFE_UINT32 safe_size = 0;
173 for (const auto& stream : m_StreamArray) {
174 m_StreamSegmentOffsets.push_back(safe_size.ValueOrDie());
175 safe_size += stream->GetSize();
176 safe_size += 1;
177 if (!safe_size.IsValid())
178 return Stage::kComplete;
179 }
180
181 const size_t buffer_size = safe_size.ValueOrDie();
182 auto buffer = FixedSizeDataVector<uint8_t>::TryZeroed(buffer_size);
183 if (buffer.empty()) {
184 m_Data.emplace<pdfium::span<const uint8_t>>();
185 return Stage::kComplete;
186 }
187
188 size_t pos = 0;
189 auto data_span = buffer.span();
190 for (const auto& stream : m_StreamArray) {
191 fxcrt::spancpy(data_span.subspan(pos), stream->GetSpan());
192 pos += stream->GetSize();
193 data_span[pos++] = ' ';
194 }
195 m_StreamArray.clear();
196 m_Data = std::move(buffer);
197 return Stage::kParse;
198}
199
201 if (!m_pParser) {
202 m_RecursionState.parsed_set.clear();
203 m_RecursionState.form_count = 0;
204 m_pParser = std::make_unique<CPDF_StreamContentParser>(
205 m_pPageObjectHolder->GetDocument(),
206 m_pPageObjectHolder->GetMutablePageResources(), nullptr, nullptr,
207 m_pPageObjectHolder, m_pPageObjectHolder->GetMutableResources(),
208 m_pPageObjectHolder->GetBBox(), nullptr, &m_RecursionState);
209 m_pParser->GetCurStates()->mutable_color_state().SetDefault();
210 }
211 if (m_CurrentOffset >= GetData().size())
212 return Stage::kCheckClip;
213
214 if (m_StreamSegmentOffsets.empty())
215 m_StreamSegmentOffsets.push_back(0);
216
217 static constexpr uint32_t kParseStepLimit = 100;
218 m_CurrentOffset += m_pParser->Parse(GetData(), m_CurrentOffset,
219 kParseStepLimit, m_StreamSegmentOffsets);
220 return Stage::kParse;
221}
222
223CPDF_ContentParser::Stage CPDF_ContentParser::CheckClip() {
224 if (m_pType3Char) {
225 m_pType3Char->InitializeFromStreamData(m_pParser->IsColored(),
226 m_pParser->GetType3Data());
227 }
228
229 for (auto& pObj : *m_pPageObjectHolder) {
230 CPDF_ClipPath& clip_path = pObj->mutable_clip_path();
231 if (!clip_path.HasRef()) {
232 continue;
233 }
234 if (clip_path.GetPathCount() != 1) {
235 continue;
236 }
237 if (clip_path.GetTextCount() > 0) {
238 continue;
239 }
240
241 CPDF_Path path = clip_path.GetPath(0);
242 if (!path.IsRect() || pObj->IsShading()) {
243 continue;
244 }
245
246 CFX_PointF point0 = path.GetPoint(0);
247 CFX_PointF point2 = path.GetPoint(2);
248 CFX_FloatRect old_rect(point0.x, point0.y, point2.x, point2.y);
249 if (old_rect.Contains(pObj->GetRect()))
250 clip_path.SetNull();
251 }
252 return Stage::kComplete;
253}
254
255void CPDF_ContentParser::HandlePageContentStream(const CPDF_Stream* pStream) {
256 m_pSingleStream =
257 pdfium::MakeRetain<CPDF_StreamAcc>(pdfium::WrapRetain(pStream));
258 m_pSingleStream->LoadAllDataFiltered();
259 m_CurrentStage = Stage::kPrepareContent;
260}
261
262bool CPDF_ContentParser::HandlePageContentArray(const CPDF_Array* pArray) {
263 m_nStreams = fxcrt::CollectionSize<uint32_t>(*pArray);
264 if (m_nStreams == 0)
265 return false;
266
267 m_StreamArray.resize(m_nStreams);
268 return true;
269}
270
271void CPDF_ContentParser::HandlePageContentFailure() {
272 m_CurrentStage = Stage::kComplete;
273}
274
275pdfium::span<const uint8_t> CPDF_ContentParser::GetData() const {
276 if (is_owned()) {
277 return absl::get<FixedSizeDataVector<uint8_t>>(m_Data).span();
278 }
279 return absl::get<pdfium::span<const uint8_t>>(m_Data);
280}
CFX_FloatRect & operator=(const CFX_FloatRect &that)=default
CFX_FloatRect TransformRect(const CFX_FloatRect &rect) const
void Concat(const CFX_Matrix &right)
const CFX_Matrix & current_transformation_matrix() const
CPDF_ContentParser(CPDF_Page *pPage)
bool Continue(PauseIndicatorIface *pPause)
CPDF_ContentParser(RetainPtr< const CPDF_Stream > pStream, CPDF_PageObjectHolder *pPageObjectHolder, const CPDF_AllStates *pGraphicStates, const CFX_Matrix *pParentMatrix, CPDF_Type3Char *pType3Char, CPDF_Form::RecursionState *recursion_state)
void SetBlendType(BlendMode type)
void SetFillAlpha(float alpha)
void SetStrokeAlpha(float alpha)
void SetSoftMask(RetainPtr< CPDF_Dictionary > pDict)
CPDF_Document * GetDocument() const override
Definition cpdf_page.cpp:51
bool HasRef() const
Definition cpdf_path.h:22
void AppendFloatRect(const CFX_FloatRect &rect)
Definition cpdf_path.cpp:49
void Emplace()
Definition cpdf_path.h:21
void Transform(const CFX_Matrix &matrix)
Definition cpdf_path.cpp:41
virtual bool NeedToPauseNow()=0
BlendMode
Definition fx_dib.h:49