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

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

Issue 2392603004: Move core/fpdfapi/fpdf_parser to core/fpdfapi/parser (Closed)
Patch Set: Rebase to master Created 4 years, 2 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
« no previous file with comments | « core/fpdfapi/fpdf_parser/cpdf_dictionary.h ('k') | core/fpdfapi/fpdf_parser/cpdf_document.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/fpdfapi/fpdf_parser/cpdf_dictionary.h"
8
9 #include <set>
10 #include <utility>
11
12 #include "core/fpdfapi/fpdf_parser/cpdf_array.h"
13 #include "core/fpdfapi/fpdf_parser/cpdf_boolean.h"
14 #include "core/fpdfapi/fpdf_parser/cpdf_name.h"
15 #include "core/fpdfapi/fpdf_parser/cpdf_number.h"
16 #include "core/fpdfapi/fpdf_parser/cpdf_reference.h"
17 #include "core/fpdfapi/fpdf_parser/cpdf_stream.h"
18 #include "core/fpdfapi/fpdf_parser/cpdf_string.h"
19 #include "third_party/base/logging.h"
20 #include "third_party/base/stl_util.h"
21
22 CPDF_Dictionary::CPDF_Dictionary()
23 : CPDF_Dictionary(CFX_WeakPtr<CFX_ByteStringPool>()) {}
24
25 CPDF_Dictionary::CPDF_Dictionary(const CFX_WeakPtr<CFX_ByteStringPool>& pPool)
26 : m_pPool(pPool) {}
27
28 CPDF_Dictionary::~CPDF_Dictionary() {
29 // Mark the object as deleted so that it will not be deleted again
30 // in case of cyclic references.
31 m_ObjNum = kInvalidObjNum;
32 for (const auto& it : m_Map) {
33 if (it.second)
34 it.second->Release();
35 }
36 }
37
38 CPDF_Object::Type CPDF_Dictionary::GetType() const {
39 return DICTIONARY;
40 }
41
42 CPDF_Dictionary* CPDF_Dictionary::GetDict() const {
43 // The method should be made non-const if we want to not be const.
44 // See bug #234.
45 return const_cast<CPDF_Dictionary*>(this);
46 }
47
48 bool CPDF_Dictionary::IsDictionary() const {
49 return true;
50 }
51
52 CPDF_Dictionary* CPDF_Dictionary::AsDictionary() {
53 return this;
54 }
55
56 const CPDF_Dictionary* CPDF_Dictionary::AsDictionary() const {
57 return this;
58 }
59
60 CPDF_Object* CPDF_Dictionary::Clone() const {
61 return CloneObjectNonCyclic(false);
62 }
63
64 CPDF_Object* CPDF_Dictionary::CloneNonCyclic(
65 bool bDirect,
66 std::set<const CPDF_Object*>* pVisited) const {
67 pVisited->insert(this);
68 CPDF_Dictionary* pCopy = new CPDF_Dictionary(m_pPool);
69 for (const auto& it : *this) {
70 CPDF_Object* value = it.second;
71 if (!pdfium::ContainsKey(*pVisited, value)) {
72 pCopy->m_Map.insert(
73 std::make_pair(it.first, value->CloneNonCyclic(bDirect, pVisited)));
74 }
75 }
76 return pCopy;
77 }
78
79 CPDF_Object* CPDF_Dictionary::GetObjectFor(const CFX_ByteString& key) const {
80 auto it = m_Map.find(key);
81 return it != m_Map.end() ? it->second : nullptr;
82 }
83
84 CPDF_Object* CPDF_Dictionary::GetDirectObjectFor(
85 const CFX_ByteString& key) const {
86 CPDF_Object* p = GetObjectFor(key);
87 return p ? p->GetDirect() : nullptr;
88 }
89
90 CFX_ByteString CPDF_Dictionary::GetStringFor(const CFX_ByteString& key) const {
91 CPDF_Object* p = GetObjectFor(key);
92 return p ? p->GetString() : CFX_ByteString();
93 }
94
95 CFX_WideString CPDF_Dictionary::GetUnicodeTextFor(
96 const CFX_ByteString& key) const {
97 CPDF_Object* p = GetObjectFor(key);
98 if (CPDF_Reference* pRef = ToReference(p))
99 p = pRef->GetDirect();
100 return p ? p->GetUnicodeText() : CFX_WideString();
101 }
102
103 CFX_ByteString CPDF_Dictionary::GetStringFor(const CFX_ByteString& key,
104 const CFX_ByteString& def) const {
105 CPDF_Object* p = GetObjectFor(key);
106 return p ? p->GetString() : CFX_ByteString(def);
107 }
108
109 int CPDF_Dictionary::GetIntegerFor(const CFX_ByteString& key) const {
110 CPDF_Object* p = GetObjectFor(key);
111 return p ? p->GetInteger() : 0;
112 }
113
114 int CPDF_Dictionary::GetIntegerFor(const CFX_ByteString& key, int def) const {
115 CPDF_Object* p = GetObjectFor(key);
116 return p ? p->GetInteger() : def;
117 }
118
119 FX_FLOAT CPDF_Dictionary::GetNumberFor(const CFX_ByteString& key) const {
120 CPDF_Object* p = GetObjectFor(key);
121 return p ? p->GetNumber() : 0;
122 }
123
124 bool CPDF_Dictionary::GetBooleanFor(const CFX_ByteString& key,
125 bool bDefault) const {
126 CPDF_Object* p = GetObjectFor(key);
127 return ToBoolean(p) ? p->GetInteger() != 0 : bDefault;
128 }
129
130 CPDF_Dictionary* CPDF_Dictionary::GetDictFor(const CFX_ByteString& key) const {
131 CPDF_Object* p = GetDirectObjectFor(key);
132 if (!p)
133 return nullptr;
134 if (CPDF_Dictionary* pDict = p->AsDictionary())
135 return pDict;
136 if (CPDF_Stream* pStream = p->AsStream())
137 return pStream->GetDict();
138 return nullptr;
139 }
140
141 CPDF_Array* CPDF_Dictionary::GetArrayFor(const CFX_ByteString& key) const {
142 return ToArray(GetDirectObjectFor(key));
143 }
144
145 CPDF_Stream* CPDF_Dictionary::GetStreamFor(const CFX_ByteString& key) const {
146 return ToStream(GetDirectObjectFor(key));
147 }
148
149 CFX_FloatRect CPDF_Dictionary::GetRectFor(const CFX_ByteString& key) const {
150 CFX_FloatRect rect;
151 CPDF_Array* pArray = GetArrayFor(key);
152 if (pArray)
153 rect = pArray->GetRect();
154 return rect;
155 }
156
157 CFX_Matrix CPDF_Dictionary::GetMatrixFor(const CFX_ByteString& key) const {
158 CFX_Matrix matrix;
159 CPDF_Array* pArray = GetArrayFor(key);
160 if (pArray)
161 matrix = pArray->GetMatrix();
162 return matrix;
163 }
164
165 FX_BOOL CPDF_Dictionary::KeyExist(const CFX_ByteString& key) const {
166 return pdfium::ContainsKey(m_Map, key);
167 }
168
169 bool CPDF_Dictionary::IsSignatureDict() const {
170 CPDF_Object* pType = GetDirectObjectFor("Type");
171 if (!pType)
172 pType = GetDirectObjectFor("FT");
173 return pType && pType->GetString() == "Sig";
174 }
175
176 void CPDF_Dictionary::SetFor(const CFX_ByteString& key, CPDF_Object* pObj) {
177 CHECK(!pObj || pObj->GetObjNum() == 0);
178 auto it = m_Map.find(key);
179 if (it == m_Map.end()) {
180 if (pObj)
181 m_Map.insert(std::make_pair(MaybeIntern(key), pObj));
182 return;
183 }
184
185 if (it->second == pObj)
186 return;
187 it->second->Release();
188
189 if (pObj)
190 it->second = pObj;
191 else
192 m_Map.erase(it);
193 }
194
195 void CPDF_Dictionary::RemoveFor(const CFX_ByteString& key) {
196 auto it = m_Map.find(key);
197 if (it == m_Map.end())
198 return;
199
200 it->second->Release();
201 m_Map.erase(it);
202 }
203
204 void CPDF_Dictionary::ReplaceKey(const CFX_ByteString& oldkey,
205 const CFX_ByteString& newkey) {
206 auto old_it = m_Map.find(oldkey);
207 if (old_it == m_Map.end())
208 return;
209
210 auto new_it = m_Map.find(newkey);
211 if (new_it == old_it)
212 return;
213
214 if (new_it != m_Map.end()) {
215 new_it->second->Release();
216 new_it->second = old_it->second;
217 } else {
218 m_Map.insert(std::make_pair(MaybeIntern(newkey), old_it->second));
219 }
220 m_Map.erase(old_it);
221 }
222
223 void CPDF_Dictionary::SetIntegerFor(const CFX_ByteString& key, int i) {
224 SetFor(key, new CPDF_Number(i));
225 }
226
227 void CPDF_Dictionary::SetNameFor(const CFX_ByteString& key,
228 const CFX_ByteString& name) {
229 SetFor(key, new CPDF_Name(MaybeIntern(name)));
230 }
231
232 void CPDF_Dictionary::SetStringFor(const CFX_ByteString& key,
233 const CFX_ByteString& str) {
234 SetFor(key, new CPDF_String(MaybeIntern(str), FALSE));
235 }
236
237 void CPDF_Dictionary::SetReferenceFor(const CFX_ByteString& key,
238 CPDF_IndirectObjectHolder* pDoc,
239 uint32_t objnum) {
240 SetFor(key, new CPDF_Reference(pDoc, objnum));
241 }
242
243 void CPDF_Dictionary::SetNumberFor(const CFX_ByteString& key, FX_FLOAT f) {
244 SetFor(key, new CPDF_Number(f));
245 }
246
247 void CPDF_Dictionary::SetBooleanFor(const CFX_ByteString& key, bool bValue) {
248 SetFor(key, new CPDF_Boolean(bValue));
249 }
250
251 void CPDF_Dictionary::SetRectFor(const CFX_ByteString& 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 SetFor(key, pArray);
259 }
260
261 void CPDF_Dictionary::SetMatrixFor(const CFX_ByteString& 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 SetFor(key, pArray);
271 }
272
273 CFX_ByteString CPDF_Dictionary::MaybeIntern(const CFX_ByteString& str) {
274 return m_pPool ? m_pPool->Intern(str) : str;
275 }
OLDNEW
« no previous file with comments | « core/fpdfapi/fpdf_parser/cpdf_dictionary.h ('k') | core/fpdfapi/fpdf_parser/cpdf_document.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698