Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: core/src/fpdfapi/fpdf_parser/cpdf_dictionary.cpp

Issue 1776913007: Split fpdf_parser_objects.cpp into per-class .cpp/.h files. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Address comments Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 }
24
25 CPDF_Object::Type CPDF_Dictionary::GetType() const {
26 return DICTIONARY;
27 }
28
29 CPDF_Dictionary* CPDF_Dictionary::GetDict() const {
30 // The method should be made non-const if we want to not be const.
31 // See bug #234.
32 return const_cast<CPDF_Dictionary*>(this);
33 }
34
35 bool CPDF_Dictionary::IsDictionary() const {
36 return true;
37 }
38
39 CPDF_Dictionary* CPDF_Dictionary::AsDictionary() {
40 return this;
41 }
42
43 const CPDF_Dictionary* CPDF_Dictionary::AsDictionary() const {
44 return this;
45 }
46
47 CPDF_Object* CPDF_Dictionary::Clone(FX_BOOL bDirect) const {
48 CPDF_Dictionary* pCopy = new CPDF_Dictionary();
49 for (const auto& it : *this)
50 pCopy->m_Map.insert(std::make_pair(it.first, it.second->Clone(bDirect)));
51 return pCopy;
52 }
53
54 CPDF_Object* CPDF_Dictionary::GetElement(const CFX_ByteStringC& key) const {
55 auto it = m_Map.find(key);
56 if (it == m_Map.end())
57 return nullptr;
58 return it->second;
59 }
60 CPDF_Object* CPDF_Dictionary::GetElementValue(
61 const CFX_ByteStringC& key) const {
62 CPDF_Object* p = GetElement(key);
63 return p ? p->GetDirect() : nullptr;
64 }
65
66 CFX_ByteString CPDF_Dictionary::GetStringBy(const CFX_ByteStringC& key) const {
67 CPDF_Object* p = GetElement(key);
68 return p ? p->GetString() : CFX_ByteString();
69 }
70
71 CFX_ByteStringC CPDF_Dictionary::GetConstStringBy(
72 const CFX_ByteStringC& key) const {
73 CPDF_Object* p = GetElement(key);
74 return p ? p->GetConstString() : CFX_ByteStringC();
75 }
76
77 CFX_WideString CPDF_Dictionary::GetUnicodeTextBy(
78 const CFX_ByteStringC& key) const {
79 CPDF_Object* p = GetElement(key);
80 if (CPDF_Reference* pRef = ToReference(p))
81 p = pRef->GetDirect();
82 return p ? p->GetUnicodeText() : CFX_WideString();
83 }
84
85 CFX_ByteString CPDF_Dictionary::GetStringBy(const CFX_ByteStringC& key,
86 const CFX_ByteStringC& def) const {
87 CPDF_Object* p = GetElement(key);
88 return p ? p->GetString() : CFX_ByteString(def);
89 }
90
91 CFX_ByteStringC CPDF_Dictionary::GetConstStringBy(
92 const CFX_ByteStringC& key,
93 const CFX_ByteStringC& def) const {
94 CPDF_Object* p = GetElement(key);
95 return p ? p->GetConstString() : CFX_ByteStringC(def);
96 }
97
98 int CPDF_Dictionary::GetIntegerBy(const CFX_ByteStringC& key) const {
99 CPDF_Object* p = GetElement(key);
100 return p ? p->GetInteger() : 0;
101 }
102
103 int CPDF_Dictionary::GetIntegerBy(const CFX_ByteStringC& key, int def) const {
104 CPDF_Object* p = GetElement(key);
105 return p ? p->GetInteger() : def;
106 }
107
108 FX_FLOAT CPDF_Dictionary::GetNumberBy(const CFX_ByteStringC& key) const {
109 CPDF_Object* p = GetElement(key);
110 return p ? p->GetNumber() : 0;
111 }
112
113 FX_BOOL CPDF_Dictionary::GetBooleanBy(const CFX_ByteStringC& key,
114 FX_BOOL bDefault) const {
115 CPDF_Object* p = GetElement(key);
116 return ToBoolean(p) ? p->GetInteger() : bDefault;
117 }
118
119 CPDF_Dictionary* CPDF_Dictionary::GetDictBy(const CFX_ByteStringC& key) const {
120 CPDF_Object* p = GetElementValue(key);
121 if (!p)
122 return nullptr;
123 if (CPDF_Dictionary* pDict = p->AsDictionary())
124 return pDict;
125 if (CPDF_Stream* pStream = p->AsStream())
126 return pStream->GetDict();
127 return nullptr;
128 }
129
130 CPDF_Array* CPDF_Dictionary::GetArrayBy(const CFX_ByteStringC& key) const {
131 return ToArray(GetElementValue(key));
132 }
133
134 CPDF_Stream* CPDF_Dictionary::GetStreamBy(const CFX_ByteStringC& key) const {
135 return ToStream(GetElementValue(key));
136 }
137
138 CFX_FloatRect CPDF_Dictionary::GetRectBy(const CFX_ByteStringC& key) const {
139 CFX_FloatRect rect;
140 CPDF_Array* pArray = GetArrayBy(key);
141 if (pArray)
142 rect = pArray->GetRect();
143 return rect;
144 }
145
146 CFX_Matrix CPDF_Dictionary::GetMatrixBy(const CFX_ByteStringC& key) const {
147 CFX_Matrix matrix;
148 CPDF_Array* pArray = GetArrayBy(key);
149 if (pArray)
150 matrix = pArray->GetMatrix();
151 return matrix;
152 }
153
154 FX_BOOL CPDF_Dictionary::KeyExist(const CFX_ByteStringC& key) const {
155 return pdfium::ContainsKey(m_Map, key);
156 }
157
158 bool CPDF_Dictionary::IsSignatureDict() const {
159 CPDF_Object* pType = GetElementValue("Type");
160 if (!pType)
161 pType = GetElementValue("FT");
162 return pType && pType->GetString() == "Sig";
163 }
164
165 void CPDF_Dictionary::SetAt(const CFX_ByteStringC& key, CPDF_Object* pObj) {
166 ASSERT(IsDictionary());
167 // Avoid 2 constructions of CFX_ByteString.
168 CFX_ByteString key_bytestring = key;
169 auto it = m_Map.find(key_bytestring);
170 if (it == m_Map.end()) {
171 if (pObj)
172 m_Map.insert(std::make_pair(key_bytestring, pObj));
173 return;
174 }
175
176 if (it->second == pObj)
177 return;
178 it->second->Release();
179
180 if (pObj)
181 it->second = pObj;
182 else
183 m_Map.erase(it);
184 }
185
186 void CPDF_Dictionary::RemoveAt(const CFX_ByteStringC& key) {
187 auto it = m_Map.find(key);
188 if (it == m_Map.end())
189 return;
190
191 it->second->Release();
192 m_Map.erase(it);
193 }
194
195 void CPDF_Dictionary::ReplaceKey(const CFX_ByteStringC& oldkey,
196 const CFX_ByteStringC& newkey) {
197 auto old_it = m_Map.find(oldkey);
198 if (old_it == m_Map.end())
199 return;
200
201 // Avoid 2 constructions of CFX_ByteString.
202 CFX_ByteString newkey_bytestring = newkey;
203 auto new_it = m_Map.find(newkey_bytestring);
204 if (new_it == old_it)
205 return;
206
207 if (new_it != m_Map.end()) {
208 new_it->second->Release();
209 new_it->second = old_it->second;
210 } else {
211 m_Map.insert(std::make_pair(newkey_bytestring, old_it->second));
212 }
213 m_Map.erase(old_it);
214 }
215
216 void CPDF_Dictionary::SetAtInteger(const CFX_ByteStringC& key, int i) {
217 SetAt(key, new CPDF_Number(i));
218 }
219
220 void CPDF_Dictionary::SetAtName(const CFX_ByteStringC& key,
221 const CFX_ByteString& name) {
222 SetAt(key, new CPDF_Name(name));
223 }
224
225 void CPDF_Dictionary::SetAtString(const CFX_ByteStringC& key,
226 const CFX_ByteString& str) {
227 SetAt(key, new CPDF_String(str, FALSE));
228 }
229
230 void CPDF_Dictionary::SetAtReference(const CFX_ByteStringC& key,
231 CPDF_IndirectObjectHolder* pDoc,
232 FX_DWORD objnum) {
233 SetAt(key, new CPDF_Reference(pDoc, objnum));
234 }
235
236 void CPDF_Dictionary::AddReference(const CFX_ByteStringC& key,
237 CPDF_IndirectObjectHolder* pDoc,
238 FX_DWORD objnum) {
239 SetAt(key, new CPDF_Reference(pDoc, objnum));
240 }
241
242 void CPDF_Dictionary::SetAtNumber(const CFX_ByteStringC& key, FX_FLOAT f) {
243 CPDF_Number* pNumber = new CPDF_Number(f);
244 SetAt(key, pNumber);
245 }
246
247 void CPDF_Dictionary::SetAtBoolean(const CFX_ByteStringC& key, FX_BOOL bValue) {
248 SetAt(key, new CPDF_Boolean(bValue));
249 }
250
251 void CPDF_Dictionary::SetAtRect(const CFX_ByteStringC& key,
252 const CFX_FloatRect& rect) {
253 CPDF_Array* pArray = new CPDF_Array;
254 pArray->AddNumber(rect.left);
255 pArray->AddNumber(rect.bottom);
256 pArray->AddNumber(rect.right);
257 pArray->AddNumber(rect.top);
258 SetAt(key, pArray);
259 }
260
261 void CPDF_Dictionary::SetAtMatrix(const CFX_ByteStringC& key,
262 const CFX_Matrix& matrix) {
263 CPDF_Array* pArray = new CPDF_Array;
264 pArray->AddNumber(matrix.a);
265 pArray->AddNumber(matrix.b);
266 pArray->AddNumber(matrix.c);
267 pArray->AddNumber(matrix.d);
268 pArray->AddNumber(matrix.e);
269 pArray->AddNumber(matrix.f);
270 SetAt(key, pArray);
271 }
OLDNEW
« no previous file with comments | « core/src/fpdfapi/fpdf_parser/cpdf_data_avail.cpp ('k') | core/src/fpdfapi/fpdf_parser/cpdf_document.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698