7#include "core/fpdfapi/page/cpdf_contentparser.h"
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"
34 m_CurrentStage = Stage::kComplete;
39 pPage->GetMutableDict()->GetMutableDirectObjectFor(
40 pdfium::page_object::kContents);
42 HandlePageContentFailure();
46 const CPDF_Stream* pStream = pContent->AsStream();
48 HandlePageContentStream(pStream);
52 const CPDF_Array* pArray = pContent->AsArray();
53 if (pArray && HandlePageContentArray(pArray))
56 HandlePageContentFailure();
66 : m_CurrentStage(Stage::kParse),
69 DCHECK(m_pPageObjectHolder);
71 m_pPageObjectHolder->GetDict()->GetMatrixFor(
"Matrix");
76 m_pPageObjectHolder->GetDict()->GetArrayFor(
"BBox");
80 form_bbox = pBBox->GetRect();
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,
100 m_pParser->GetCurStates()->set_current_transformation_matrix(form_matrix);
101 m_pParser->GetCurStates()->set_parent_matrix(form_matrix);
103 m_pParser->GetCurStates()->mutable_clip_path().AppendPathWithAutoMerge(
104 ClipPath, CFX_FillRenderOptions::FillType::kWinding);
106 if (m_pPageObjectHolder->GetTransparency().IsGroup()) {
108 m_pParser->GetCurStates()->mutable_general_state();
114 m_pSingleStream = pdfium::MakeRetain<CPDF_StreamAcc>(std::move(pStream));
115 m_pSingleStream->LoadAllDataFiltered();
116 m_Data = m_pSingleStream->GetSpan();
125 while (m_CurrentStage == Stage::kGetContent) {
126 m_CurrentStage = GetContent();
131 if (m_CurrentStage == Stage::kPrepareContent)
132 m_CurrentStage = PrepareContent();
134 while (m_CurrentStage == Stage::kParse) {
135 m_CurrentStage = Parse();
140 if (m_CurrentStage == Stage::kCheckClip)
141 m_CurrentStage = CheckClip();
143 DCHECK_EQ(m_CurrentStage, Stage::kComplete);
148 DCHECK_EQ(m_CurrentStage, Stage::kGetContent);
149 DCHECK(m_pPageObjectHolder->IsPage());
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();
160 return m_CurrentOffset == m_nStreams ? Stage::kPrepareContent
161 : Stage::kGetContent;
167 if (m_StreamArray.empty()) {
168 m_Data = m_pSingleStream->GetSpan();
169 return Stage::kParse;
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();
177 if (!safe_size.IsValid())
178 return Stage::kComplete;
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;
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++] =
' ';
195 m_StreamArray.clear();
196 m_Data = std::move(buffer);
197 return Stage::kParse;
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();
211 if (m_CurrentOffset >= GetData().size())
212 return Stage::kCheckClip;
214 if (m_StreamSegmentOffsets.empty())
215 m_StreamSegmentOffsets.push_back(0);
217 static constexpr uint32_t kParseStepLimit = 100;
218 m_CurrentOffset += m_pParser->Parse(GetData(), m_CurrentOffset,
219 kParseStepLimit, m_StreamSegmentOffsets);
220 return Stage::kParse;
225 m_pType3Char->InitializeFromStreamData(m_pParser->IsColored(),
226 m_pParser->GetType3Data());
229 for (
auto& pObj : *m_pPageObjectHolder) {
230 CPDF_ClipPath& clip_path = pObj->mutable_clip_path();
231 if (!clip_path.HasRef()) {
234 if (clip_path.GetPathCount() != 1) {
237 if (clip_path.GetTextCount() > 0) {
241 CPDF_Path path = clip_path.GetPath(0);
242 if (!path.IsRect() || pObj->IsShading()) {
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()))
252 return Stage::kComplete;
257 pdfium::MakeRetain<CPDF_StreamAcc>(pdfium::WrapRetain(pStream));
258 m_pSingleStream->LoadAllDataFiltered();
259 m_CurrentStage = Stage::kPrepareContent;
263 m_nStreams =
fxcrt::CollectionSize<uint32_t>(*pArray);
267 m_StreamArray.resize(m_nStreams);
272 m_CurrentStage = Stage::kComplete;
277 return absl::get<FixedSizeDataVector<uint8_t>>(m_Data).span();
279 return absl::get<pdfium::span<
const uint8_t>>(m_Data);
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
void AppendFloatRect(const CFX_FloatRect &rect)
void Transform(const CFX_Matrix &matrix)
virtual bool NeedToPauseNow()=0