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/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 "third_party/base/stl_util.h"
20
operator <(const GraphicsData & other) const21 bool GraphicsData::operator<(const GraphicsData& other) const {
22 if (!FXSYS_SafeEQ(fillAlpha, other.fillAlpha))
23 return FXSYS_SafeLT(fillAlpha, other.fillAlpha);
24 if (!FXSYS_SafeEQ(strokeAlpha, other.strokeAlpha))
25 return FXSYS_SafeLT(strokeAlpha, other.strokeAlpha);
26 return blendType < other.blendType;
27 }
28
operator <(const FontData & other) const29 bool FontData::operator<(const FontData& other) const {
30 if (baseFont != other.baseFont)
31 return baseFont < other.baseFont;
32 return type < other.type;
33 }
34
CPDF_PageObjectHolder(CPDF_Document * pDoc,CPDF_Dictionary * pDict,CPDF_Dictionary * pPageResources,CPDF_Dictionary * pResources)35 CPDF_PageObjectHolder::CPDF_PageObjectHolder(CPDF_Document* pDoc,
36 CPDF_Dictionary* pDict,
37 CPDF_Dictionary* pPageResources,
38 CPDF_Dictionary* pResources)
39 : m_pPageResources(pPageResources),
40 m_pResources(pResources),
41 m_pDict(pDict),
42 m_pDocument(pDoc) {
43 ASSERT(m_pDict);
44 }
45
46 CPDF_PageObjectHolder::~CPDF_PageObjectHolder() = default;
47
IsPage() const48 bool CPDF_PageObjectHolder::IsPage() const {
49 return false;
50 }
51
StartParse(std::unique_ptr<CPDF_ContentParser> pParser)52 void CPDF_PageObjectHolder::StartParse(
53 std::unique_ptr<CPDF_ContentParser> pParser) {
54 ASSERT(m_ParseState == ParseState::kNotParsed);
55 m_pParser = std::move(pParser);
56 m_ParseState = ParseState::kParsing;
57 }
58
ContinueParse(PauseIndicatorIface * pPause)59 void CPDF_PageObjectHolder::ContinueParse(PauseIndicatorIface* pPause) {
60 if (m_ParseState == ParseState::kParsed)
61 return;
62
63 ASSERT(m_ParseState == ParseState::kParsing);
64 if (m_pParser->Continue(pPause))
65 return;
66
67 m_ParseState = ParseState::kParsed;
68 m_pDocument->IncrementParsedPageCount();
69 if (m_pParser->GetCurStates())
70 m_LastCTM = m_pParser->GetCurStates()->m_CTM;
71
72 m_pParser.reset();
73 }
74
AddImageMaskBoundingBox(const CFX_FloatRect & box)75 void CPDF_PageObjectHolder::AddImageMaskBoundingBox(const CFX_FloatRect& box) {
76 m_MaskBoundingBoxes.push_back(box);
77 }
78
TakeDirtyStreams()79 std::set<int32_t> CPDF_PageObjectHolder::TakeDirtyStreams() {
80 auto dirty_streams = std::move(m_DirtyStreams);
81 m_DirtyStreams.clear();
82 return dirty_streams;
83 }
84
LoadTransparencyInfo()85 void CPDF_PageObjectHolder::LoadTransparencyInfo() {
86 CPDF_Dictionary* pGroup = m_pDict->GetDictFor("Group");
87 if (!pGroup)
88 return;
89
90 if (pGroup->GetStringFor(pdfium::transparency::kGroupSubType) !=
91 pdfium::transparency::kTransparency) {
92 return;
93 }
94 m_Transparency.SetGroup();
95 if (pGroup->GetIntegerFor(pdfium::transparency::kI))
96 m_Transparency.SetIsolated();
97 }
98
GetPageObjectByIndex(size_t index) const99 CPDF_PageObject* CPDF_PageObjectHolder::GetPageObjectByIndex(
100 size_t index) const {
101 return pdfium::IndexInBounds(m_PageObjectList, index)
102 ? m_PageObjectList[index].get()
103 : nullptr;
104 }
105
AppendPageObject(std::unique_ptr<CPDF_PageObject> pPageObj)106 void CPDF_PageObjectHolder::AppendPageObject(
107 std::unique_ptr<CPDF_PageObject> pPageObj) {
108 m_PageObjectList.push_back(std::move(pPageObj));
109 }
110
RemovePageObject(CPDF_PageObject * pPageObj)111 bool CPDF_PageObjectHolder::RemovePageObject(CPDF_PageObject* pPageObj) {
112 pdfium::FakeUniquePtr<CPDF_PageObject> p(pPageObj);
113
114 auto it =
115 std::find(std::begin(m_PageObjectList), std::end(m_PageObjectList), p);
116 if (it == std::end(m_PageObjectList))
117 return false;
118
119 it->release();
120 m_PageObjectList.erase(it);
121
122 int32_t content_stream = pPageObj->GetContentStream();
123 if (content_stream >= 0)
124 m_DirtyStreams.insert(content_stream);
125
126 return true;
127 }
128
ErasePageObjectAtIndex(size_t index)129 bool CPDF_PageObjectHolder::ErasePageObjectAtIndex(size_t index) {
130 if (index >= m_PageObjectList.size())
131 return false;
132
133 m_PageObjectList.erase(m_PageObjectList.begin() + index);
134 return true;
135 }
136