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_pageobjectholder.h"
8
9 #include <algorithm>
10 #include <utility>
11
12 #include "constants/transparency.h"
13 #include "core/fpdfapi/page/cpdf_allstates.h"
14 #include "core/fpdfapi/page/cpdf_contentparser.h"
15 #include "core/fpdfapi/page/cpdf_pageobject.h"
16 #include "core/fpdfapi/parser/cpdf_dictionary.h"
17 #include "core/fpdfapi/parser/cpdf_document.h"
18 #include "core/fxcrt/fx_extension.h"
19 #include "core/fxcrt/stl_util.h"
20 #include "third_party/base/check.h"
21 #include "third_party/base/check_op.h"
22
operator <(const GraphicsData & other) const23 bool GraphicsData::operator<(const GraphicsData& other) const {
24 if (!FXSYS_SafeEQ(fillAlpha, other.fillAlpha))
25 return FXSYS_SafeLT(fillAlpha, other.fillAlpha);
26 if (!FXSYS_SafeEQ(strokeAlpha, other.strokeAlpha))
27 return FXSYS_SafeLT(strokeAlpha, other.strokeAlpha);
28 return blendType < other.blendType;
29 }
30
operator <(const FontData & other) const31 bool FontData::operator<(const FontData& other) const {
32 if (baseFont != other.baseFont)
33 return baseFont < other.baseFont;
34 return type < other.type;
35 }
36
CPDF_PageObjectHolder(CPDF_Document * pDoc,RetainPtr<CPDF_Dictionary> pDict,RetainPtr<CPDF_Dictionary> pPageResources,RetainPtr<CPDF_Dictionary> pResources)37 CPDF_PageObjectHolder::CPDF_PageObjectHolder(
38 CPDF_Document* pDoc,
39 RetainPtr<CPDF_Dictionary> pDict,
40 RetainPtr<CPDF_Dictionary> pPageResources,
41 RetainPtr<CPDF_Dictionary> pResources)
42 : m_pPageResources(std::move(pPageResources)),
43 m_pResources(std::move(pResources)),
44 m_pDict(std::move(pDict)),
45 m_pDocument(pDoc) {
46 DCHECK(m_pDict);
47 }
48
49 CPDF_PageObjectHolder::~CPDF_PageObjectHolder() = default;
50
IsPage() const51 bool CPDF_PageObjectHolder::IsPage() const {
52 return false;
53 }
54
StartParse(std::unique_ptr<CPDF_ContentParser> pParser)55 void CPDF_PageObjectHolder::StartParse(
56 std::unique_ptr<CPDF_ContentParser> pParser) {
57 DCHECK_EQ(m_ParseState, ParseState::kNotParsed);
58 m_pParser = std::move(pParser);
59 m_ParseState = ParseState::kParsing;
60 }
61
ContinueParse(PauseIndicatorIface * pPause)62 void CPDF_PageObjectHolder::ContinueParse(PauseIndicatorIface* pPause) {
63 if (m_ParseState == ParseState::kParsed)
64 return;
65
66 DCHECK_EQ(m_ParseState, ParseState::kParsing);
67 if (m_pParser->Continue(pPause))
68 return;
69
70 m_ParseState = ParseState::kParsed;
71 m_pDocument->IncrementParsedPageCount();
72 if (m_pParser->GetCurStates())
73 m_LastCTM = m_pParser->GetCurStates()->m_CTM;
74
75 m_pParser.reset();
76 }
77
AddImageMaskBoundingBox(const CFX_FloatRect & box)78 void CPDF_PageObjectHolder::AddImageMaskBoundingBox(const CFX_FloatRect& box) {
79 m_MaskBoundingBoxes.push_back(box);
80 }
81
TakeDirtyStreams()82 std::set<int32_t> CPDF_PageObjectHolder::TakeDirtyStreams() {
83 auto dirty_streams = std::move(m_DirtyStreams);
84 m_DirtyStreams.clear();
85 return dirty_streams;
86 }
87
GraphicsMapSearch(const GraphicsData & gd)88 absl::optional<ByteString> CPDF_PageObjectHolder::GraphicsMapSearch(
89 const GraphicsData& gd) {
90 auto it = m_GraphicsMap.find(gd);
91 if (it == m_GraphicsMap.end())
92 return absl::nullopt;
93
94 return it->second;
95 }
96
GraphicsMapInsert(const GraphicsData & gd,const ByteString & str)97 void CPDF_PageObjectHolder::GraphicsMapInsert(const GraphicsData& gd,
98 const ByteString& str) {
99 m_GraphicsMap[gd] = str;
100 }
101
FontsMapSearch(const FontData & fd)102 absl::optional<ByteString> CPDF_PageObjectHolder::FontsMapSearch(
103 const FontData& fd) {
104 auto it = m_FontsMap.find(fd);
105 if (it == m_FontsMap.end())
106 return absl::nullopt;
107
108 return it->second;
109 }
110
FontsMapInsert(const FontData & fd,const ByteString & str)111 void CPDF_PageObjectHolder::FontsMapInsert(const FontData& fd,
112 const ByteString& str) {
113 m_FontsMap[fd] = str;
114 }
115
LoadTransparencyInfo()116 void CPDF_PageObjectHolder::LoadTransparencyInfo() {
117 RetainPtr<const CPDF_Dictionary> pGroup = m_pDict->GetDictFor("Group");
118 if (!pGroup)
119 return;
120
121 if (pGroup->GetByteStringFor(pdfium::transparency::kGroupSubType) !=
122 pdfium::transparency::kTransparency) {
123 return;
124 }
125 m_Transparency.SetGroup();
126 if (pGroup->GetIntegerFor(pdfium::transparency::kI))
127 m_Transparency.SetIsolated();
128 }
129
GetPageObjectByIndex(size_t index) const130 CPDF_PageObject* CPDF_PageObjectHolder::GetPageObjectByIndex(
131 size_t index) const {
132 return fxcrt::IndexInBounds(m_PageObjectList, index)
133 ? m_PageObjectList[index].get()
134 : nullptr;
135 }
136
AppendPageObject(std::unique_ptr<CPDF_PageObject> pPageObj)137 void CPDF_PageObjectHolder::AppendPageObject(
138 std::unique_ptr<CPDF_PageObject> pPageObj) {
139 m_PageObjectList.push_back(std::move(pPageObj));
140 }
141
RemovePageObject(CPDF_PageObject * pPageObj)142 std::unique_ptr<CPDF_PageObject> CPDF_PageObjectHolder::RemovePageObject(
143 CPDF_PageObject* pPageObj) {
144 auto it = std::find(std::begin(m_PageObjectList), std::end(m_PageObjectList),
145 fxcrt::MakeFakeUniquePtr(pPageObj));
146 if (it == std::end(m_PageObjectList))
147 return nullptr;
148
149 std::unique_ptr<CPDF_PageObject> result = std::move(*it);
150 m_PageObjectList.erase(it);
151
152 int32_t content_stream = pPageObj->GetContentStream();
153 if (content_stream >= 0)
154 m_DirtyStreams.insert(content_stream);
155
156 return result;
157 }
158
ErasePageObjectAtIndex(size_t index)159 bool CPDF_PageObjectHolder::ErasePageObjectAtIndex(size_t index) {
160 if (index >= m_PageObjectList.size())
161 return false;
162
163 m_PageObjectList.erase(m_PageObjectList.begin() + index);
164 return true;
165 }
166