Chromium Code Reviews| 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/include/fpdfapi/cpdf_dictionary.h" | |
| 8 | |
| 9 #include "core/include/fpdfapi/cpdf_array.h" | |
| 10 #include "core/include/fpdfapi/cpdf_boolean.h" | |
| 11 #include "core/include/fpdfapi/cpdf_name.h" | |
| 12 #include "core/include/fpdfapi/cpdf_number.h" | |
| 13 #include "core/include/fpdfapi/cpdf_reference.h" | |
| 14 #include "core/include/fpdfapi/cpdf_stream.h" | |
| 15 #include "core/include/fpdfapi/cpdf_string.h" | |
| 16 #include "third_party/base/stl_util.h" | |
| 17 | |
| 18 CPDF_Dictionary::CPDF_Dictionary() {} | |
| 19 | |
| 20 CPDF_Dictionary::~CPDF_Dictionary() { | |
| 21 for (const auto& it : m_Map) { | |
| 22 it.second->Release(); | |
| 23 } | |
|
dsinclair
2016/03/10 21:23:44
nit: no {}s
Tom Sepez
2016/03/10 22:05:19
Done.
| |
| 24 } | |
| 25 | |
| 26 CPDF_Object::Type CPDF_Dictionary::GetType() const { | |
| 27 return DICTIONARY; | |
| 28 } | |
| 29 | |
| 30 CPDF_Dictionary* CPDF_Dictionary::GetDict() const { | |
| 31 // The method should be made non-const if we want to not be const. | |
| 32 // See bug #234. | |
| 33 return const_cast<CPDF_Dictionary*>(this); | |
| 34 } | |
| 35 | |
| 36 bool CPDF_Dictionary::IsDictionary() const { | |
| 37 return true; | |
| 38 } | |
| 39 | |
| 40 CPDF_Dictionary* CPDF_Dictionary::AsDictionary() { | |
| 41 return this; | |
| 42 } | |
| 43 | |
| 44 const CPDF_Dictionary* CPDF_Dictionary::AsDictionary() const { | |
| 45 return this; | |
| 46 } | |
| 47 | |
| 48 CPDF_Object* CPDF_Dictionary::Clone(FX_BOOL bDirect) const { | |
| 49 CPDF_Dictionary* pCopy = new CPDF_Dictionary(); | |
| 50 for (const auto& it : *this) | |
| 51 pCopy->m_Map.insert(std::make_pair(it.first, it.second->Clone(bDirect))); | |
| 52 return pCopy; | |
| 53 } | |
| 54 | |
| 55 CPDF_Object* CPDF_Dictionary::GetElement(const CFX_ByteStringC& key) const { | |
| 56 auto it = m_Map.find(key); | |
| 57 if (it == m_Map.end()) | |
| 58 return nullptr; | |
| 59 return it->second; | |
| 60 } | |
| 61 CPDF_Object* CPDF_Dictionary::GetElementValue( | |
| 62 const CFX_ByteStringC& key) const { | |
| 63 CPDF_Object* p = GetElement(key); | |
| 64 return p ? p->GetDirect() : nullptr; | |
| 65 } | |
| 66 | |
| 67 CFX_ByteString CPDF_Dictionary::GetStringBy(const CFX_ByteStringC& key) const { | |
| 68 CPDF_Object* p = GetElement(key); | |
| 69 if (p) { | |
| 70 return p->GetString(); | |
| 71 } | |
| 72 return CFX_ByteString(); | |
|
dsinclair
2016/03/10 21:23:45
return p ? p->GetString() : CFX_ByteString();
Tom Sepez
2016/03/10 22:05:19
Done.
| |
| 73 } | |
| 74 | |
| 75 CFX_ByteStringC CPDF_Dictionary::GetConstStringBy( | |
| 76 const CFX_ByteStringC& key) const { | |
| 77 CPDF_Object* p = GetElement(key); | |
| 78 if (p) { | |
| 79 return p->GetConstString(); | |
| 80 } | |
| 81 return CFX_ByteStringC(); | |
|
dsinclair
2016/03/10 21:23:45
return p ? p->GetConstString() : CFX_ByteStringC()
| |
| 82 } | |
| 83 | |
| 84 CFX_WideString CPDF_Dictionary::GetUnicodeTextBy( | |
| 85 const CFX_ByteStringC& key) const { | |
| 86 CPDF_Object* p = GetElement(key); | |
| 87 if (CPDF_Reference* pRef = ToReference(p)) | |
| 88 p = pRef->GetDirect(); | |
| 89 return p ? p->GetUnicodeText() : CFX_WideString(); | |
| 90 } | |
| 91 | |
| 92 CFX_ByteString CPDF_Dictionary::GetStringBy(const CFX_ByteStringC& key, | |
| 93 const CFX_ByteStringC& def) const { | |
| 94 CPDF_Object* p = GetElement(key); | |
| 95 if (p) { | |
| 96 return p->GetString(); | |
| 97 } | |
| 98 return CFX_ByteString(def); | |
|
dsinclair
2016/03/10 21:23:44
return p ? p->GetString() : CFX_ByteString(def);
| |
| 99 } | |
| 100 | |
| 101 CFX_ByteStringC CPDF_Dictionary::GetConstStringBy( | |
| 102 const CFX_ByteStringC& key, | |
| 103 const CFX_ByteStringC& def) const { | |
| 104 CPDF_Object* p = GetElement(key); | |
| 105 if (p) { | |
| 106 return p->GetConstString(); | |
| 107 } | |
| 108 return CFX_ByteStringC(def); | |
|
dsinclair
2016/03/10 21:23:44
return p ? p->GetConstString() : CFX_ByteStringC(d
| |
| 109 } | |
| 110 | |
| 111 int CPDF_Dictionary::GetIntegerBy(const CFX_ByteStringC& key) const { | |
| 112 CPDF_Object* p = GetElement(key); | |
| 113 if (p) { | |
| 114 return p->GetInteger(); | |
| 115 } | |
| 116 return 0; | |
|
dsinclair
2016/03/10 21:23:45
return p ? p->GetInteger() : 0;
| |
| 117 } | |
| 118 | |
| 119 int CPDF_Dictionary::GetIntegerBy(const CFX_ByteStringC& key, int def) const { | |
| 120 CPDF_Object* p = GetElement(key); | |
| 121 if (p) { | |
| 122 return p->GetInteger(); | |
| 123 } | |
| 124 return def; | |
|
dsinclair
2016/03/10 21:23:44
return p ? p->GetInteger() : def;
| |
| 125 } | |
| 126 | |
| 127 FX_FLOAT CPDF_Dictionary::GetNumberBy(const CFX_ByteStringC& key) const { | |
| 128 CPDF_Object* p = GetElement(key); | |
| 129 if (p) { | |
| 130 return p->GetNumber(); | |
| 131 } | |
| 132 return 0; | |
|
dsinclair
2016/03/10 21:23:45
return p ? p->GetNumber() : 0;
| |
| 133 } | |
| 134 | |
| 135 FX_BOOL CPDF_Dictionary::GetBooleanBy(const CFX_ByteStringC& key, | |
| 136 FX_BOOL bDefault) const { | |
| 137 CPDF_Object* p = GetElement(key); | |
| 138 if (ToBoolean(p)) | |
| 139 return p->GetInteger(); | |
| 140 return bDefault; | |
|
dsinclair
2016/03/10 21:23:45
return ToBoolean(p) ? p->GetInteger() : bDefault;
| |
| 141 } | |
| 142 | |
| 143 CPDF_Dictionary* CPDF_Dictionary::GetDictBy(const CFX_ByteStringC& key) const { | |
| 144 CPDF_Object* p = GetElementValue(key); | |
| 145 if (!p) | |
| 146 return nullptr; | |
| 147 if (CPDF_Dictionary* pDict = p->AsDictionary()) | |
| 148 return pDict; | |
| 149 if (CPDF_Stream* pStream = p->AsStream()) | |
| 150 return pStream->GetDict(); | |
| 151 return nullptr; | |
| 152 } | |
| 153 | |
| 154 CPDF_Array* CPDF_Dictionary::GetArrayBy(const CFX_ByteStringC& key) const { | |
| 155 return ToArray(GetElementValue(key)); | |
| 156 } | |
| 157 | |
| 158 CPDF_Stream* CPDF_Dictionary::GetStreamBy(const CFX_ByteStringC& key) const { | |
| 159 return ToStream(GetElementValue(key)); | |
| 160 } | |
| 161 | |
| 162 CFX_FloatRect CPDF_Dictionary::GetRectBy(const CFX_ByteStringC& key) const { | |
| 163 CFX_FloatRect rect; | |
| 164 CPDF_Array* pArray = GetArrayBy(key); | |
| 165 if (pArray) | |
| 166 rect = pArray->GetRect(); | |
| 167 return rect; | |
| 168 } | |
| 169 | |
| 170 CFX_Matrix CPDF_Dictionary::GetMatrixBy(const CFX_ByteStringC& key) const { | |
| 171 CFX_Matrix matrix; | |
| 172 CPDF_Array* pArray = GetArrayBy(key); | |
| 173 if (pArray) | |
| 174 matrix = pArray->GetMatrix(); | |
| 175 return matrix; | |
| 176 } | |
| 177 | |
| 178 FX_BOOL CPDF_Dictionary::KeyExist(const CFX_ByteStringC& key) const { | |
| 179 return pdfium::ContainsKey(m_Map, key); | |
| 180 } | |
| 181 | |
| 182 bool CPDF_Dictionary::IsSignatureDict() const { | |
| 183 CPDF_Object* pType = GetElementValue("Type"); | |
| 184 if (!pType) | |
| 185 pType = GetElementValue("FT"); | |
| 186 return pType && pType->GetString() == "Sig"; | |
| 187 } | |
| 188 | |
| 189 void CPDF_Dictionary::SetAt(const CFX_ByteStringC& key, CPDF_Object* pObj) { | |
| 190 ASSERT(IsDictionary()); | |
| 191 // Avoid 2 constructions of CFX_ByteString. | |
| 192 CFX_ByteString key_bytestring = key; | |
| 193 auto it = m_Map.find(key_bytestring); | |
| 194 if (it == m_Map.end()) { | |
| 195 if (pObj) { | |
| 196 m_Map.insert(std::make_pair(key_bytestring, pObj)); | |
| 197 } | |
|
dsinclair
2016/03/10 21:23:45
nit: {}s
| |
| 198 return; | |
| 199 } | |
| 200 | |
| 201 if (it->second == pObj) | |
| 202 return; | |
| 203 it->second->Release(); | |
| 204 | |
| 205 if (pObj) | |
| 206 it->second = pObj; | |
| 207 else | |
| 208 m_Map.erase(it); | |
| 209 } | |
| 210 | |
| 211 void CPDF_Dictionary::RemoveAt(const CFX_ByteStringC& key) { | |
| 212 auto it = m_Map.find(key); | |
| 213 if (it == m_Map.end()) | |
| 214 return; | |
| 215 | |
| 216 it->second->Release(); | |
| 217 m_Map.erase(it); | |
| 218 } | |
| 219 | |
| 220 void CPDF_Dictionary::ReplaceKey(const CFX_ByteStringC& oldkey, | |
| 221 const CFX_ByteStringC& newkey) { | |
| 222 auto old_it = m_Map.find(oldkey); | |
| 223 if (old_it == m_Map.end()) | |
| 224 return; | |
| 225 | |
| 226 // Avoid 2 constructions of CFX_ByteString. | |
| 227 CFX_ByteString newkey_bytestring = newkey; | |
| 228 auto new_it = m_Map.find(newkey_bytestring); | |
| 229 if (new_it == old_it) | |
| 230 return; | |
| 231 | |
| 232 if (new_it != m_Map.end()) { | |
| 233 new_it->second->Release(); | |
| 234 new_it->second = old_it->second; | |
| 235 } else { | |
| 236 m_Map.insert(std::make_pair(newkey_bytestring, old_it->second)); | |
| 237 } | |
| 238 m_Map.erase(old_it); | |
| 239 } | |
| 240 | |
| 241 void CPDF_Dictionary::SetAtInteger(const CFX_ByteStringC& key, int i) { | |
| 242 SetAt(key, new CPDF_Number(i)); | |
| 243 } | |
| 244 | |
| 245 void CPDF_Dictionary::SetAtName(const CFX_ByteStringC& key, | |
| 246 const CFX_ByteString& name) { | |
| 247 SetAt(key, new CPDF_Name(name)); | |
| 248 } | |
| 249 | |
| 250 void CPDF_Dictionary::SetAtString(const CFX_ByteStringC& key, | |
| 251 const CFX_ByteString& str) { | |
| 252 SetAt(key, new CPDF_String(str, FALSE)); | |
| 253 } | |
| 254 | |
| 255 void CPDF_Dictionary::SetAtReference(const CFX_ByteStringC& key, | |
| 256 CPDF_IndirectObjectHolder* pDoc, | |
| 257 FX_DWORD objnum) { | |
| 258 SetAt(key, new CPDF_Reference(pDoc, objnum)); | |
| 259 } | |
| 260 | |
| 261 void CPDF_Dictionary::AddReference(const CFX_ByteStringC& key, | |
| 262 CPDF_IndirectObjectHolder* pDoc, | |
| 263 FX_DWORD objnum) { | |
| 264 SetAt(key, new CPDF_Reference(pDoc, objnum)); | |
| 265 } | |
| 266 | |
| 267 void CPDF_Dictionary::SetAtNumber(const CFX_ByteStringC& key, FX_FLOAT f) { | |
| 268 CPDF_Number* pNumber = new CPDF_Number(f); | |
| 269 SetAt(key, pNumber); | |
| 270 } | |
| 271 | |
| 272 void CPDF_Dictionary::SetAtBoolean(const CFX_ByteStringC& key, FX_BOOL bValue) { | |
| 273 SetAt(key, new CPDF_Boolean(bValue)); | |
| 274 } | |
| 275 | |
| 276 void CPDF_Dictionary::SetAtRect(const CFX_ByteStringC& key, | |
| 277 const CFX_FloatRect& rect) { | |
| 278 CPDF_Array* pArray = new CPDF_Array; | |
| 279 pArray->AddNumber(rect.left); | |
| 280 pArray->AddNumber(rect.bottom); | |
| 281 pArray->AddNumber(rect.right); | |
| 282 pArray->AddNumber(rect.top); | |
| 283 SetAt(key, pArray); | |
| 284 } | |
| 285 | |
| 286 void CPDF_Dictionary::SetAtMatrix(const CFX_ByteStringC& key, | |
| 287 const CFX_Matrix& matrix) { | |
| 288 CPDF_Array* pArray = new CPDF_Array; | |
| 289 pArray->AddNumber(matrix.a); | |
| 290 pArray->AddNumber(matrix.b); | |
| 291 pArray->AddNumber(matrix.c); | |
| 292 pArray->AddNumber(matrix.d); | |
| 293 pArray->AddNumber(matrix.e); | |
| 294 pArray->AddNumber(matrix.f); | |
| 295 SetAt(key, pArray); | |
| 296 } | |
| OLD | NEW |