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/fpdf_parser/include/cpdf_dictionary.h" | 7 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" |
8 | 8 |
9 #include "core/fpdfapi/fpdf_parser/cpdf_boolean.h" | 9 #include "core/fpdfapi/fpdf_parser/cpdf_boolean.h" |
10 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 return this; | 44 return this; |
45 } | 45 } |
46 | 46 |
47 CPDF_Object* CPDF_Dictionary::Clone(FX_BOOL bDirect) const { | 47 CPDF_Object* CPDF_Dictionary::Clone(FX_BOOL bDirect) const { |
48 CPDF_Dictionary* pCopy = new CPDF_Dictionary(); | 48 CPDF_Dictionary* pCopy = new CPDF_Dictionary(); |
49 for (const auto& it : *this) | 49 for (const auto& it : *this) |
50 pCopy->m_Map.insert(std::make_pair(it.first, it.second->Clone(bDirect))); | 50 pCopy->m_Map.insert(std::make_pair(it.first, it.second->Clone(bDirect))); |
51 return pCopy; | 51 return pCopy; |
52 } | 52 } |
53 | 53 |
54 CPDF_Object* CPDF_Dictionary::GetObjectBy(const CFX_ByteStringC& key) const { | 54 CPDF_Object* CPDF_Dictionary::GetObjectBy(const CFX_ByteString& key) const { |
55 auto it = m_Map.find(key); | 55 auto it = m_Map.find(key); |
56 if (it == m_Map.end()) | 56 return it != m_Map.end() ? it->second : nullptr; |
57 return nullptr; | |
58 return it->second; | |
59 } | 57 } |
| 58 |
60 CPDF_Object* CPDF_Dictionary::GetDirectObjectBy( | 59 CPDF_Object* CPDF_Dictionary::GetDirectObjectBy( |
61 const CFX_ByteStringC& key) const { | 60 const CFX_ByteString& key) const { |
62 CPDF_Object* p = GetObjectBy(key); | 61 CPDF_Object* p = GetObjectBy(key); |
63 return p ? p->GetDirect() : nullptr; | 62 return p ? p->GetDirect() : nullptr; |
64 } | 63 } |
65 | 64 |
66 CFX_ByteString CPDF_Dictionary::GetStringBy(const CFX_ByteStringC& key) const { | 65 CFX_ByteString CPDF_Dictionary::GetStringBy(const CFX_ByteString& key) const { |
67 CPDF_Object* p = GetObjectBy(key); | 66 CPDF_Object* p = GetObjectBy(key); |
68 return p ? p->GetString() : CFX_ByteString(); | 67 return p ? p->GetString() : CFX_ByteString(); |
69 } | 68 } |
70 | 69 |
71 CFX_WideString CPDF_Dictionary::GetUnicodeTextBy( | 70 CFX_WideString CPDF_Dictionary::GetUnicodeTextBy( |
72 const CFX_ByteStringC& key) const { | 71 const CFX_ByteString& key) const { |
73 CPDF_Object* p = GetObjectBy(key); | 72 CPDF_Object* p = GetObjectBy(key); |
74 if (CPDF_Reference* pRef = ToReference(p)) | 73 if (CPDF_Reference* pRef = ToReference(p)) |
75 p = pRef->GetDirect(); | 74 p = pRef->GetDirect(); |
76 return p ? p->GetUnicodeText() : CFX_WideString(); | 75 return p ? p->GetUnicodeText() : CFX_WideString(); |
77 } | 76 } |
78 | 77 |
79 CFX_ByteString CPDF_Dictionary::GetStringBy(const CFX_ByteStringC& key, | 78 CFX_ByteString CPDF_Dictionary::GetStringBy(const CFX_ByteString& key, |
80 const CFX_ByteStringC& def) const { | 79 const CFX_ByteString& def) const { |
81 CPDF_Object* p = GetObjectBy(key); | 80 CPDF_Object* p = GetObjectBy(key); |
82 return p ? p->GetString() : CFX_ByteString(def); | 81 return p ? p->GetString() : CFX_ByteString(def); |
83 } | 82 } |
84 | 83 |
85 int CPDF_Dictionary::GetIntegerBy(const CFX_ByteStringC& key) const { | 84 int CPDF_Dictionary::GetIntegerBy(const CFX_ByteString& key) const { |
86 CPDF_Object* p = GetObjectBy(key); | 85 CPDF_Object* p = GetObjectBy(key); |
87 return p ? p->GetInteger() : 0; | 86 return p ? p->GetInteger() : 0; |
88 } | 87 } |
89 | 88 |
90 int CPDF_Dictionary::GetIntegerBy(const CFX_ByteStringC& key, int def) const { | 89 int CPDF_Dictionary::GetIntegerBy(const CFX_ByteString& key, int def) const { |
91 CPDF_Object* p = GetObjectBy(key); | 90 CPDF_Object* p = GetObjectBy(key); |
92 return p ? p->GetInteger() : def; | 91 return p ? p->GetInteger() : def; |
93 } | 92 } |
94 | 93 |
95 FX_FLOAT CPDF_Dictionary::GetNumberBy(const CFX_ByteStringC& key) const { | 94 FX_FLOAT CPDF_Dictionary::GetNumberBy(const CFX_ByteString& key) const { |
96 CPDF_Object* p = GetObjectBy(key); | 95 CPDF_Object* p = GetObjectBy(key); |
97 return p ? p->GetNumber() : 0; | 96 return p ? p->GetNumber() : 0; |
98 } | 97 } |
99 | 98 |
100 FX_BOOL CPDF_Dictionary::GetBooleanBy(const CFX_ByteStringC& key, | 99 FX_BOOL CPDF_Dictionary::GetBooleanBy(const CFX_ByteString& key, |
101 FX_BOOL bDefault) const { | 100 FX_BOOL bDefault) const { |
102 CPDF_Object* p = GetObjectBy(key); | 101 CPDF_Object* p = GetObjectBy(key); |
103 return ToBoolean(p) ? p->GetInteger() : bDefault; | 102 return ToBoolean(p) ? p->GetInteger() : bDefault; |
104 } | 103 } |
105 | 104 |
106 CPDF_Dictionary* CPDF_Dictionary::GetDictBy(const CFX_ByteStringC& key) const { | 105 CPDF_Dictionary* CPDF_Dictionary::GetDictBy(const CFX_ByteString& key) const { |
107 CPDF_Object* p = GetDirectObjectBy(key); | 106 CPDF_Object* p = GetDirectObjectBy(key); |
108 if (!p) | 107 if (!p) |
109 return nullptr; | 108 return nullptr; |
110 if (CPDF_Dictionary* pDict = p->AsDictionary()) | 109 if (CPDF_Dictionary* pDict = p->AsDictionary()) |
111 return pDict; | 110 return pDict; |
112 if (CPDF_Stream* pStream = p->AsStream()) | 111 if (CPDF_Stream* pStream = p->AsStream()) |
113 return pStream->GetDict(); | 112 return pStream->GetDict(); |
114 return nullptr; | 113 return nullptr; |
115 } | 114 } |
116 | 115 |
117 CPDF_Array* CPDF_Dictionary::GetArrayBy(const CFX_ByteStringC& key) const { | 116 CPDF_Array* CPDF_Dictionary::GetArrayBy(const CFX_ByteString& key) const { |
118 return ToArray(GetDirectObjectBy(key)); | 117 return ToArray(GetDirectObjectBy(key)); |
119 } | 118 } |
120 | 119 |
121 CPDF_Stream* CPDF_Dictionary::GetStreamBy(const CFX_ByteStringC& key) const { | 120 CPDF_Stream* CPDF_Dictionary::GetStreamBy(const CFX_ByteString& key) const { |
122 return ToStream(GetDirectObjectBy(key)); | 121 return ToStream(GetDirectObjectBy(key)); |
123 } | 122 } |
124 | 123 |
125 CFX_FloatRect CPDF_Dictionary::GetRectBy(const CFX_ByteStringC& key) const { | 124 CFX_FloatRect CPDF_Dictionary::GetRectBy(const CFX_ByteString& key) const { |
126 CFX_FloatRect rect; | 125 CFX_FloatRect rect; |
127 CPDF_Array* pArray = GetArrayBy(key); | 126 CPDF_Array* pArray = GetArrayBy(key); |
128 if (pArray) | 127 if (pArray) |
129 rect = pArray->GetRect(); | 128 rect = pArray->GetRect(); |
130 return rect; | 129 return rect; |
131 } | 130 } |
132 | 131 |
133 CFX_Matrix CPDF_Dictionary::GetMatrixBy(const CFX_ByteStringC& key) const { | 132 CFX_Matrix CPDF_Dictionary::GetMatrixBy(const CFX_ByteString& key) const { |
134 CFX_Matrix matrix; | 133 CFX_Matrix matrix; |
135 CPDF_Array* pArray = GetArrayBy(key); | 134 CPDF_Array* pArray = GetArrayBy(key); |
136 if (pArray) | 135 if (pArray) |
137 matrix = pArray->GetMatrix(); | 136 matrix = pArray->GetMatrix(); |
138 return matrix; | 137 return matrix; |
139 } | 138 } |
140 | 139 |
141 FX_BOOL CPDF_Dictionary::KeyExist(const CFX_ByteStringC& key) const { | 140 FX_BOOL CPDF_Dictionary::KeyExist(const CFX_ByteString& key) const { |
142 return pdfium::ContainsKey(m_Map, key); | 141 return pdfium::ContainsKey(m_Map, key); |
143 } | 142 } |
144 | 143 |
145 bool CPDF_Dictionary::IsSignatureDict() const { | 144 bool CPDF_Dictionary::IsSignatureDict() const { |
146 CPDF_Object* pType = GetDirectObjectBy("Type"); | 145 CPDF_Object* pType = GetDirectObjectBy("Type"); |
147 if (!pType) | 146 if (!pType) |
148 pType = GetDirectObjectBy("FT"); | 147 pType = GetDirectObjectBy("FT"); |
149 return pType && pType->GetString() == "Sig"; | 148 return pType && pType->GetString() == "Sig"; |
150 } | 149 } |
151 | 150 |
152 void CPDF_Dictionary::SetAt(const CFX_ByteStringC& key, CPDF_Object* pObj) { | 151 void CPDF_Dictionary::SetAt(const CFX_ByteString& key, CPDF_Object* pObj) { |
153 ASSERT(IsDictionary()); | 152 auto it = m_Map.find(key); |
154 // Avoid 2 constructions of CFX_ByteString. | |
155 CFX_ByteString key_bytestring = key; | |
156 auto it = m_Map.find(key_bytestring); | |
157 if (it == m_Map.end()) { | 153 if (it == m_Map.end()) { |
158 if (pObj) | 154 if (pObj) |
159 m_Map.insert(std::make_pair(key_bytestring, pObj)); | 155 m_Map.insert(std::make_pair(key, pObj)); |
160 return; | 156 return; |
161 } | 157 } |
162 | 158 |
163 if (it->second == pObj) | 159 if (it->second == pObj) |
164 return; | 160 return; |
165 it->second->Release(); | 161 it->second->Release(); |
166 | 162 |
167 if (pObj) | 163 if (pObj) |
168 it->second = pObj; | 164 it->second = pObj; |
169 else | 165 else |
170 m_Map.erase(it); | 166 m_Map.erase(it); |
171 } | 167 } |
172 | 168 |
173 void CPDF_Dictionary::RemoveAt(const CFX_ByteStringC& key) { | 169 void CPDF_Dictionary::RemoveAt(const CFX_ByteString& key) { |
174 auto it = m_Map.find(key); | 170 auto it = m_Map.find(key); |
175 if (it == m_Map.end()) | 171 if (it == m_Map.end()) |
176 return; | 172 return; |
177 | 173 |
178 it->second->Release(); | 174 it->second->Release(); |
179 m_Map.erase(it); | 175 m_Map.erase(it); |
180 } | 176 } |
181 | 177 |
182 void CPDF_Dictionary::ReplaceKey(const CFX_ByteStringC& oldkey, | 178 void CPDF_Dictionary::ReplaceKey(const CFX_ByteString& oldkey, |
183 const CFX_ByteStringC& newkey) { | 179 const CFX_ByteString& newkey) { |
184 auto old_it = m_Map.find(oldkey); | 180 auto old_it = m_Map.find(oldkey); |
185 if (old_it == m_Map.end()) | 181 if (old_it == m_Map.end()) |
186 return; | 182 return; |
187 | 183 |
188 // Avoid 2 constructions of CFX_ByteString. | 184 auto new_it = m_Map.find(newkey); |
189 CFX_ByteString newkey_bytestring = newkey; | |
190 auto new_it = m_Map.find(newkey_bytestring); | |
191 if (new_it == old_it) | 185 if (new_it == old_it) |
192 return; | 186 return; |
193 | 187 |
194 if (new_it != m_Map.end()) { | 188 if (new_it != m_Map.end()) { |
195 new_it->second->Release(); | 189 new_it->second->Release(); |
196 new_it->second = old_it->second; | 190 new_it->second = old_it->second; |
197 } else { | 191 } else { |
198 m_Map.insert(std::make_pair(newkey_bytestring, old_it->second)); | 192 m_Map.insert(std::make_pair(newkey, old_it->second)); |
199 } | 193 } |
200 m_Map.erase(old_it); | 194 m_Map.erase(old_it); |
201 } | 195 } |
202 | 196 |
203 void CPDF_Dictionary::SetAtInteger(const CFX_ByteStringC& key, int i) { | 197 void CPDF_Dictionary::SetAtInteger(const CFX_ByteString& key, int i) { |
204 SetAt(key, new CPDF_Number(i)); | 198 SetAt(key, new CPDF_Number(i)); |
205 } | 199 } |
206 | 200 |
207 void CPDF_Dictionary::SetAtName(const CFX_ByteStringC& key, | 201 void CPDF_Dictionary::SetAtName(const CFX_ByteString& key, |
208 const CFX_ByteString& name) { | 202 const CFX_ByteString& name) { |
209 SetAt(key, new CPDF_Name(name)); | 203 SetAt(key, new CPDF_Name(name)); |
210 } | 204 } |
211 | 205 |
212 void CPDF_Dictionary::SetAtString(const CFX_ByteStringC& key, | 206 void CPDF_Dictionary::SetAtString(const CFX_ByteString& key, |
213 const CFX_ByteString& str) { | 207 const CFX_ByteString& str) { |
214 SetAt(key, new CPDF_String(str, FALSE)); | 208 SetAt(key, new CPDF_String(str, FALSE)); |
215 } | 209 } |
216 | 210 |
217 void CPDF_Dictionary::SetAtReference(const CFX_ByteStringC& key, | 211 void CPDF_Dictionary::SetAtReference(const CFX_ByteString& key, |
218 CPDF_IndirectObjectHolder* pDoc, | 212 CPDF_IndirectObjectHolder* pDoc, |
219 uint32_t objnum) { | 213 uint32_t objnum) { |
220 SetAt(key, new CPDF_Reference(pDoc, objnum)); | 214 SetAt(key, new CPDF_Reference(pDoc, objnum)); |
221 } | 215 } |
222 | 216 |
223 void CPDF_Dictionary::AddReference(const CFX_ByteStringC& key, | 217 void CPDF_Dictionary::AddReference(const CFX_ByteString& key, |
224 CPDF_IndirectObjectHolder* pDoc, | 218 CPDF_IndirectObjectHolder* pDoc, |
225 uint32_t objnum) { | 219 uint32_t objnum) { |
226 SetAt(key, new CPDF_Reference(pDoc, objnum)); | 220 SetAt(key, new CPDF_Reference(pDoc, objnum)); |
227 } | 221 } |
228 | 222 |
229 void CPDF_Dictionary::SetAtNumber(const CFX_ByteStringC& key, FX_FLOAT f) { | 223 void CPDF_Dictionary::SetAtNumber(const CFX_ByteString& key, FX_FLOAT f) { |
230 CPDF_Number* pNumber = new CPDF_Number(f); | 224 SetAt(key, new CPDF_Number(f)); |
231 SetAt(key, pNumber); | |
232 } | 225 } |
233 | 226 |
234 void CPDF_Dictionary::SetAtBoolean(const CFX_ByteStringC& key, FX_BOOL bValue) { | 227 void CPDF_Dictionary::SetAtBoolean(const CFX_ByteString& key, FX_BOOL bValue) { |
235 SetAt(key, new CPDF_Boolean(bValue)); | 228 SetAt(key, new CPDF_Boolean(bValue)); |
236 } | 229 } |
237 | 230 |
238 void CPDF_Dictionary::SetAtRect(const CFX_ByteStringC& key, | 231 void CPDF_Dictionary::SetAtRect(const CFX_ByteString& key, |
239 const CFX_FloatRect& rect) { | 232 const CFX_FloatRect& rect) { |
240 CPDF_Array* pArray = new CPDF_Array; | 233 CPDF_Array* pArray = new CPDF_Array; |
241 pArray->AddNumber(rect.left); | 234 pArray->AddNumber(rect.left); |
242 pArray->AddNumber(rect.bottom); | 235 pArray->AddNumber(rect.bottom); |
243 pArray->AddNumber(rect.right); | 236 pArray->AddNumber(rect.right); |
244 pArray->AddNumber(rect.top); | 237 pArray->AddNumber(rect.top); |
245 SetAt(key, pArray); | 238 SetAt(key, pArray); |
246 } | 239 } |
247 | 240 |
248 void CPDF_Dictionary::SetAtMatrix(const CFX_ByteStringC& key, | 241 void CPDF_Dictionary::SetAtMatrix(const CFX_ByteString& key, |
249 const CFX_Matrix& matrix) { | 242 const CFX_Matrix& matrix) { |
250 CPDF_Array* pArray = new CPDF_Array; | 243 CPDF_Array* pArray = new CPDF_Array; |
251 pArray->AddNumber(matrix.a); | 244 pArray->AddNumber(matrix.a); |
252 pArray->AddNumber(matrix.b); | 245 pArray->AddNumber(matrix.b); |
253 pArray->AddNumber(matrix.c); | 246 pArray->AddNumber(matrix.c); |
254 pArray->AddNumber(matrix.d); | 247 pArray->AddNumber(matrix.d); |
255 pArray->AddNumber(matrix.e); | 248 pArray->AddNumber(matrix.e); |
256 pArray->AddNumber(matrix.f); | 249 pArray->AddNumber(matrix.f); |
257 SetAt(key, pArray); | 250 SetAt(key, pArray); |
258 } | 251 } |
OLD | NEW |