| OLD | NEW |
| (Empty) |
| 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/fpdf_parser/cpdf_array.h" | |
| 8 | |
| 9 #include <set> | |
| 10 | |
| 11 #include "core/fpdfapi/fpdf_parser/cpdf_name.h" | |
| 12 #include "core/fpdfapi/fpdf_parser/cpdf_number.h" | |
| 13 #include "core/fpdfapi/fpdf_parser/cpdf_reference.h" | |
| 14 #include "core/fpdfapi/fpdf_parser/cpdf_stream.h" | |
| 15 #include "core/fpdfapi/fpdf_parser/cpdf_string.h" | |
| 16 #include "third_party/base/logging.h" | |
| 17 #include "third_party/base/stl_util.h" | |
| 18 | |
| 19 CPDF_Array::CPDF_Array() {} | |
| 20 | |
| 21 CPDF_Array::~CPDF_Array() { | |
| 22 // Mark the object as deleted so that it will not be deleted again | |
| 23 // in case of cyclic references. | |
| 24 m_ObjNum = kInvalidObjNum; | |
| 25 for (auto& it : m_Objects) { | |
| 26 if (it) | |
| 27 it->Release(); | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 CPDF_Object::Type CPDF_Array::GetType() const { | |
| 32 return ARRAY; | |
| 33 } | |
| 34 | |
| 35 bool CPDF_Array::IsArray() const { | |
| 36 return true; | |
| 37 } | |
| 38 | |
| 39 CPDF_Array* CPDF_Array::AsArray() { | |
| 40 return this; | |
| 41 } | |
| 42 | |
| 43 const CPDF_Array* CPDF_Array::AsArray() const { | |
| 44 return this; | |
| 45 } | |
| 46 | |
| 47 CPDF_Object* CPDF_Array::Clone() const { | |
| 48 return CloneObjectNonCyclic(false); | |
| 49 } | |
| 50 | |
| 51 CPDF_Object* CPDF_Array::CloneNonCyclic( | |
| 52 bool bDirect, | |
| 53 std::set<const CPDF_Object*>* pVisited) const { | |
| 54 pVisited->insert(this); | |
| 55 CPDF_Array* pCopy = new CPDF_Array(); | |
| 56 for (size_t i = 0; i < GetCount(); i++) { | |
| 57 CPDF_Object* value = m_Objects[i]; | |
| 58 if (!pdfium::ContainsKey(*pVisited, value)) | |
| 59 pCopy->m_Objects.push_back(value->CloneNonCyclic(bDirect, pVisited)); | |
| 60 } | |
| 61 return pCopy; | |
| 62 } | |
| 63 | |
| 64 CFX_FloatRect CPDF_Array::GetRect() { | |
| 65 CFX_FloatRect rect; | |
| 66 if (!IsArray() || m_Objects.size() != 4) | |
| 67 return rect; | |
| 68 | |
| 69 rect.left = GetNumberAt(0); | |
| 70 rect.bottom = GetNumberAt(1); | |
| 71 rect.right = GetNumberAt(2); | |
| 72 rect.top = GetNumberAt(3); | |
| 73 return rect; | |
| 74 } | |
| 75 | |
| 76 CFX_Matrix CPDF_Array::GetMatrix() { | |
| 77 CFX_Matrix matrix; | |
| 78 if (!IsArray() || m_Objects.size() != 6) | |
| 79 return matrix; | |
| 80 | |
| 81 matrix.Set(GetNumberAt(0), GetNumberAt(1), GetNumberAt(2), GetNumberAt(3), | |
| 82 GetNumberAt(4), GetNumberAt(5)); | |
| 83 return matrix; | |
| 84 } | |
| 85 | |
| 86 CPDF_Object* CPDF_Array::GetObjectAt(size_t i) const { | |
| 87 if (i >= m_Objects.size()) | |
| 88 return nullptr; | |
| 89 return m_Objects[i]; | |
| 90 } | |
| 91 | |
| 92 CPDF_Object* CPDF_Array::GetDirectObjectAt(size_t i) const { | |
| 93 if (i >= m_Objects.size()) | |
| 94 return nullptr; | |
| 95 return m_Objects[i]->GetDirect(); | |
| 96 } | |
| 97 | |
| 98 CFX_ByteString CPDF_Array::GetStringAt(size_t i) const { | |
| 99 if (i >= m_Objects.size()) | |
| 100 return CFX_ByteString(); | |
| 101 return m_Objects[i]->GetString(); | |
| 102 } | |
| 103 | |
| 104 int CPDF_Array::GetIntegerAt(size_t i) const { | |
| 105 if (i >= m_Objects.size()) | |
| 106 return 0; | |
| 107 return m_Objects[i]->GetInteger(); | |
| 108 } | |
| 109 | |
| 110 FX_FLOAT CPDF_Array::GetNumberAt(size_t i) const { | |
| 111 if (i >= m_Objects.size()) | |
| 112 return 0; | |
| 113 return m_Objects[i]->GetNumber(); | |
| 114 } | |
| 115 | |
| 116 CPDF_Dictionary* CPDF_Array::GetDictAt(size_t i) const { | |
| 117 CPDF_Object* p = GetDirectObjectAt(i); | |
| 118 if (!p) | |
| 119 return nullptr; | |
| 120 if (CPDF_Dictionary* pDict = p->AsDictionary()) | |
| 121 return pDict; | |
| 122 if (CPDF_Stream* pStream = p->AsStream()) | |
| 123 return pStream->GetDict(); | |
| 124 return nullptr; | |
| 125 } | |
| 126 | |
| 127 CPDF_Stream* CPDF_Array::GetStreamAt(size_t i) const { | |
| 128 return ToStream(GetDirectObjectAt(i)); | |
| 129 } | |
| 130 | |
| 131 CPDF_Array* CPDF_Array::GetArrayAt(size_t i) const { | |
| 132 return ToArray(GetDirectObjectAt(i)); | |
| 133 } | |
| 134 | |
| 135 void CPDF_Array::RemoveAt(size_t i, size_t nCount) { | |
| 136 if (i >= m_Objects.size()) | |
| 137 return; | |
| 138 | |
| 139 if (nCount <= 0 || nCount > m_Objects.size() - i) | |
| 140 return; | |
| 141 | |
| 142 for (size_t j = 0; j < nCount; ++j) { | |
| 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); | |
| 147 } | |
| 148 | |
| 149 void CPDF_Array::SetAt(size_t i, CPDF_Object* pObj) { | |
| 150 ASSERT(IsArray()); | |
| 151 CHECK(!pObj || pObj->GetObjNum() == 0); | |
| 152 if (i >= m_Objects.size()) { | |
| 153 ASSERT(false); | |
| 154 return; | |
| 155 } | |
| 156 if (CPDF_Object* pOld = m_Objects[i]) | |
| 157 pOld->Release(); | |
| 158 | |
| 159 m_Objects[i] = pObj; | |
| 160 } | |
| 161 | |
| 162 void CPDF_Array::InsertAt(size_t index, CPDF_Object* pObj) { | |
| 163 ASSERT(IsArray()); | |
| 164 CHECK(!pObj || pObj->GetObjNum() == 0); | |
| 165 if (index >= m_Objects.size()) { | |
| 166 // Allocate space first. | |
| 167 m_Objects.resize(index + 1, nullptr); | |
| 168 m_Objects[index] = pObj; | |
| 169 } else { | |
| 170 // Directly insert. | |
| 171 m_Objects.insert(m_Objects.begin() + index, pObj); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 void CPDF_Array::Add(CPDF_Object* pObj) { | |
| 176 ASSERT(IsArray()); | |
| 177 CHECK(!pObj || pObj->GetObjNum() == 0); | |
| 178 m_Objects.push_back(pObj); | |
| 179 } | |
| 180 | |
| 181 void CPDF_Array::AddName(const CFX_ByteString& str) { | |
| 182 Add(new CPDF_Name(str)); | |
| 183 } | |
| 184 | |
| 185 void CPDF_Array::AddString(const CFX_ByteString& str) { | |
| 186 Add(new CPDF_String(str, FALSE)); | |
| 187 } | |
| 188 | |
| 189 void CPDF_Array::AddInteger(int i) { | |
| 190 Add(new CPDF_Number(i)); | |
| 191 } | |
| 192 | |
| 193 void CPDF_Array::AddNumber(FX_FLOAT f) { | |
| 194 Add(new CPDF_Number(f)); | |
| 195 } | |
| 196 | |
| 197 void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc, | |
| 198 uint32_t objnum) { | |
| 199 Add(new CPDF_Reference(pDoc, objnum)); | |
| 200 } | |
| OLD | NEW |