OLD | NEW |
1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/fpdfapi/parser/cpdf_array.h" | 7 #include "core/fpdfapi/parser/cpdf_array.h" |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "core/fpdfapi/parser/cpdf_name.h" | 11 #include "core/fpdfapi/parser/cpdf_name.h" |
12 #include "core/fpdfapi/parser/cpdf_number.h" | 12 #include "core/fpdfapi/parser/cpdf_number.h" |
13 #include "core/fpdfapi/parser/cpdf_reference.h" | 13 #include "core/fpdfapi/parser/cpdf_reference.h" |
14 #include "core/fpdfapi/parser/cpdf_stream.h" | 14 #include "core/fpdfapi/parser/cpdf_stream.h" |
15 #include "core/fpdfapi/parser/cpdf_string.h" | 15 #include "core/fpdfapi/parser/cpdf_string.h" |
16 #include "third_party/base/logging.h" | 16 #include "third_party/base/logging.h" |
17 #include "third_party/base/stl_util.h" | 17 #include "third_party/base/stl_util.h" |
18 | 18 |
19 CPDF_Array::CPDF_Array() {} | 19 CPDF_Array::CPDF_Array() {} |
20 | 20 |
21 CPDF_Array::~CPDF_Array() { | 21 CPDF_Array::~CPDF_Array() { |
22 // Mark the object as deleted so that it will not be deleted again | 22 // Mark the object as deleted so that it will not be deleted again |
23 // in case of cyclic references, then break cycles. | 23 // in case of cyclic references. |
24 m_ObjNum = kInvalidObjNum; | 24 m_ObjNum = kInvalidObjNum; |
25 for (auto& it : m_Objects) { | 25 for (auto& it : m_Objects) { |
26 if (it && it->GetObjNum() == kInvalidObjNum) | 26 if (it && it->GetObjNum() != kInvalidObjNum) |
27 it.release(); | 27 it->Release(); |
28 } | 28 } |
29 } | 29 } |
30 | 30 |
31 CPDF_Object::Type CPDF_Array::GetType() const { | 31 CPDF_Object::Type CPDF_Array::GetType() const { |
32 return ARRAY; | 32 return ARRAY; |
33 } | 33 } |
34 | 34 |
35 bool CPDF_Array::IsArray() const { | 35 bool CPDF_Array::IsArray() const { |
36 return true; | 36 return true; |
37 } | 37 } |
38 | 38 |
39 CPDF_Array* CPDF_Array::AsArray() { | 39 CPDF_Array* CPDF_Array::AsArray() { |
40 return this; | 40 return this; |
41 } | 41 } |
42 | 42 |
43 const CPDF_Array* CPDF_Array::AsArray() const { | 43 const CPDF_Array* CPDF_Array::AsArray() const { |
44 return this; | 44 return this; |
45 } | 45 } |
46 | 46 |
47 CPDF_Object* CPDF_Array::Clone() const { | 47 CPDF_Object* CPDF_Array::Clone() const { |
48 return CloneObjectNonCyclic(false); | 48 return CloneObjectNonCyclic(false); |
49 } | 49 } |
50 | 50 |
51 CPDF_Object* CPDF_Array::CloneNonCyclic( | 51 CPDF_Object* CPDF_Array::CloneNonCyclic( |
52 bool bDirect, | 52 bool bDirect, |
53 std::set<const CPDF_Object*>* pVisited) const { | 53 std::set<const CPDF_Object*>* pVisited) const { |
54 pVisited->insert(this); | 54 pVisited->insert(this); |
55 CPDF_Array* pCopy = new CPDF_Array(); | 55 CPDF_Array* pCopy = new CPDF_Array(); |
56 for (const auto& pObj : m_Objects) { | 56 for (size_t i = 0; i < GetCount(); i++) { |
57 if (!pdfium::ContainsKey(*pVisited, pObj.get())) { | 57 CPDF_Object* value = m_Objects[i]; |
58 pCopy->m_Objects.push_back( | 58 if (!pdfium::ContainsKey(*pVisited, value)) |
59 UniqueObject(pObj->CloneNonCyclic(bDirect, pVisited))); | 59 pCopy->m_Objects.push_back(value->CloneNonCyclic(bDirect, pVisited)); |
60 } | |
61 } | 60 } |
62 return pCopy; | 61 return pCopy; |
63 } | 62 } |
64 | 63 |
65 CFX_FloatRect CPDF_Array::GetRect() { | 64 CFX_FloatRect CPDF_Array::GetRect() { |
66 CFX_FloatRect rect; | 65 CFX_FloatRect rect; |
67 if (!IsArray() || m_Objects.size() != 4) | 66 if (!IsArray() || m_Objects.size() != 4) |
68 return rect; | 67 return rect; |
69 | 68 |
70 rect.left = GetNumberAt(0); | 69 rect.left = GetNumberAt(0); |
71 rect.bottom = GetNumberAt(1); | 70 rect.bottom = GetNumberAt(1); |
72 rect.right = GetNumberAt(2); | 71 rect.right = GetNumberAt(2); |
73 rect.top = GetNumberAt(3); | 72 rect.top = GetNumberAt(3); |
74 return rect; | 73 return rect; |
75 } | 74 } |
76 | 75 |
77 CFX_Matrix CPDF_Array::GetMatrix() { | 76 CFX_Matrix CPDF_Array::GetMatrix() { |
78 CFX_Matrix matrix; | 77 CFX_Matrix matrix; |
79 if (!IsArray() || m_Objects.size() != 6) | 78 if (!IsArray() || m_Objects.size() != 6) |
80 return matrix; | 79 return matrix; |
81 | 80 |
82 matrix.Set(GetNumberAt(0), GetNumberAt(1), GetNumberAt(2), GetNumberAt(3), | 81 matrix.Set(GetNumberAt(0), GetNumberAt(1), GetNumberAt(2), GetNumberAt(3), |
83 GetNumberAt(4), GetNumberAt(5)); | 82 GetNumberAt(4), GetNumberAt(5)); |
84 return matrix; | 83 return matrix; |
85 } | 84 } |
86 | 85 |
87 CPDF_Object* CPDF_Array::GetObjectAt(size_t i) const { | 86 CPDF_Object* CPDF_Array::GetObjectAt(size_t i) const { |
88 if (i >= m_Objects.size()) | 87 if (i >= m_Objects.size()) |
89 return nullptr; | 88 return nullptr; |
90 return m_Objects[i].get(); | 89 return m_Objects[i]; |
91 } | 90 } |
92 | 91 |
93 CPDF_Object* CPDF_Array::GetDirectObjectAt(size_t i) const { | 92 CPDF_Object* CPDF_Array::GetDirectObjectAt(size_t i) const { |
94 if (i >= m_Objects.size()) | 93 if (i >= m_Objects.size()) |
95 return nullptr; | 94 return nullptr; |
96 return m_Objects[i]->GetDirect(); | 95 return m_Objects[i]->GetDirect(); |
97 } | 96 } |
98 | 97 |
99 CFX_ByteString CPDF_Array::GetStringAt(size_t i) const { | 98 CFX_ByteString CPDF_Array::GetStringAt(size_t i) const { |
100 if (i >= m_Objects.size()) | 99 if (i >= m_Objects.size()) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 return ToArray(GetDirectObjectAt(i)); | 132 return ToArray(GetDirectObjectAt(i)); |
134 } | 133 } |
135 | 134 |
136 void CPDF_Array::RemoveAt(size_t i, size_t nCount) { | 135 void CPDF_Array::RemoveAt(size_t i, size_t nCount) { |
137 if (i >= m_Objects.size()) | 136 if (i >= m_Objects.size()) |
138 return; | 137 return; |
139 | 138 |
140 if (nCount <= 0 || nCount > m_Objects.size() - i) | 139 if (nCount <= 0 || nCount > m_Objects.size() - i) |
141 return; | 140 return; |
142 | 141 |
143 auto it = m_Objects.begin() + i; | 142 for (size_t j = 0; j < nCount; ++j) { |
144 m_Objects.erase(it, it + nCount); | 143 if (CPDF_Object* p = m_Objects[i + j]) |
| 144 p->Release(); |
| 145 } |
| 146 m_Objects.erase(m_Objects.begin() + i, m_Objects.begin() + i + nCount); |
145 } | 147 } |
146 | 148 |
147 void CPDF_Array::ConvertToIndirectObjectAt(size_t i, | 149 void CPDF_Array::ConvertToIndirectObjectAt(size_t i, |
148 CPDF_IndirectObjectHolder* pHolder) { | 150 CPDF_IndirectObjectHolder* pHolder) { |
149 if (i >= m_Objects.size()) | 151 if (i >= m_Objects.size()) |
150 return; | 152 return; |
151 | 153 |
152 if (!m_Objects[i] || m_Objects[i]->IsReference()) | 154 CPDF_Object* pObj = m_Objects[i]; |
| 155 if (!pObj || pObj->IsReference()) |
153 return; | 156 return; |
154 | 157 |
155 uint32_t dwObjNum = pHolder->AddIndirectObject(m_Objects[i].release()); | 158 uint32_t dwObjNum = pHolder->AddIndirectObject(pObj); |
156 m_Objects[i] = UniqueObject(new CPDF_Reference(pHolder, dwObjNum)); | 159 m_Objects[i] = new CPDF_Reference(pHolder, dwObjNum); |
157 } | 160 } |
158 | 161 |
159 void CPDF_Array::SetAt(size_t i, CPDF_Object* pObj) { | 162 void CPDF_Array::SetAt(size_t i, CPDF_Object* pObj) { |
160 ASSERT(IsArray()); | 163 ASSERT(IsArray()); |
161 CHECK(!pObj || pObj->IsInline()); | 164 CHECK(!pObj || pObj->IsInline()); |
162 if (i >= m_Objects.size()) { | 165 if (i >= m_Objects.size()) { |
163 ASSERT(false); | 166 ASSERT(false); |
164 return; | 167 return; |
165 } | 168 } |
166 m_Objects[i] = UniqueObject(pObj); | 169 if (CPDF_Object* pOld = m_Objects[i]) |
| 170 pOld->Release(); |
| 171 |
| 172 m_Objects[i] = pObj; |
167 } | 173 } |
168 | 174 |
169 void CPDF_Array::InsertAt(size_t index, CPDF_Object* pObj) { | 175 void CPDF_Array::InsertAt(size_t index, CPDF_Object* pObj) { |
170 ASSERT(IsArray()); | 176 ASSERT(IsArray()); |
171 CHECK(!pObj || pObj->IsInline()); | 177 CHECK(!pObj || pObj->IsInline()); |
172 if (index >= m_Objects.size()) { | 178 if (index >= m_Objects.size()) { |
173 // Allocate space first. | 179 // Allocate space first. |
174 m_Objects.resize(index + 1); | 180 m_Objects.resize(index + 1, nullptr); |
175 m_Objects[index] = UniqueObject(pObj); | 181 m_Objects[index] = pObj; |
176 } else { | 182 } else { |
177 // Directly insert. | 183 // Directly insert. |
178 m_Objects.insert(m_Objects.begin() + index, UniqueObject(pObj)); | 184 m_Objects.insert(m_Objects.begin() + index, pObj); |
179 } | 185 } |
180 } | 186 } |
181 | 187 |
182 void CPDF_Array::Add(CPDF_Object* pObj) { | 188 void CPDF_Array::Add(CPDF_Object* pObj) { |
183 ASSERT(IsArray()); | 189 ASSERT(IsArray()); |
184 CHECK(!pObj || pObj->IsInline()); | 190 CHECK(!pObj || pObj->IsInline()); |
185 m_Objects.push_back(UniqueObject(pObj)); | 191 m_Objects.push_back(pObj); |
186 } | 192 } |
187 | 193 |
188 void CPDF_Array::AddName(const CFX_ByteString& str) { | 194 void CPDF_Array::AddName(const CFX_ByteString& str) { |
189 Add(new CPDF_Name(str)); | 195 Add(new CPDF_Name(str)); |
190 } | 196 } |
191 | 197 |
192 void CPDF_Array::AddString(const CFX_ByteString& str) { | 198 void CPDF_Array::AddString(const CFX_ByteString& str) { |
193 Add(new CPDF_String(str, FALSE)); | 199 Add(new CPDF_String(str, FALSE)); |
194 } | 200 } |
195 | 201 |
196 void CPDF_Array::AddInteger(int i) { | 202 void CPDF_Array::AddInteger(int i) { |
197 Add(new CPDF_Number(i)); | 203 Add(new CPDF_Number(i)); |
198 } | 204 } |
199 | 205 |
200 void CPDF_Array::AddNumber(FX_FLOAT f) { | 206 void CPDF_Array::AddNumber(FX_FLOAT f) { |
201 Add(new CPDF_Number(f)); | 207 Add(new CPDF_Number(f)); |
202 } | 208 } |
203 | 209 |
204 void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc, | 210 void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc, |
205 uint32_t objnum) { | 211 uint32_t objnum) { |
206 Add(new CPDF_Reference(pDoc, objnum)); | 212 Add(new CPDF_Reference(pDoc, objnum)); |
207 } | 213 } |
OLD | NEW |