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

Side by Side Diff: fpdfsdk/fpdfppo.cpp

Issue 2510223002: Make CPDF_Dictionary use unique pointers. (Closed)
Patch Set: Plug leaks Created 4 years, 1 month 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
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 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 "public/fpdf_ppo.h" 7 #include "public/fpdf_ppo.h"
8 8
9 #include <map> 9 #include <map>
10 #include <memory> 10 #include <memory>
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 bool CopyInheritable(CPDF_Dictionary* pCurPageDict, 57 bool CopyInheritable(CPDF_Dictionary* pCurPageDict,
58 CPDF_Dictionary* pSrcPageDict, 58 CPDF_Dictionary* pSrcPageDict,
59 const CFX_ByteString& key) { 59 const CFX_ByteString& key) {
60 if (pCurPageDict->KeyExist(key)) 60 if (pCurPageDict->KeyExist(key))
61 return true; 61 return true;
62 62
63 CPDF_Object* pInheritable = PageDictGetInheritableTag(pSrcPageDict, key); 63 CPDF_Object* pInheritable = PageDictGetInheritableTag(pSrcPageDict, key);
64 if (!pInheritable) 64 if (!pInheritable)
65 return false; 65 return false;
66 66
67 pCurPageDict->SetFor(key, pInheritable->Clone().release()); 67 pCurPageDict->SetFor(key, pInheritable->Clone());
68 return true; 68 return true;
69 } 69 }
70 70
71 bool ParserPageRangeString(CFX_ByteString rangstring, 71 bool ParserPageRangeString(CFX_ByteString rangstring,
72 std::vector<uint16_t>* pageArray, 72 std::vector<uint16_t>* pageArray,
73 int nCount) { 73 int nCount) {
74 if (rangstring.IsEmpty()) 74 if (rangstring.IsEmpty())
75 return true; 75 return true;
76 76
77 rangstring.Remove(' '); 77 rangstring.Remove(' ');
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 ASSERT(m_pSrcPDFDoc); 151 ASSERT(m_pSrcPDFDoc);
152 152
153 CPDF_Dictionary* pNewRoot = m_pDestPDFDoc->GetRoot(); 153 CPDF_Dictionary* pNewRoot = m_pDestPDFDoc->GetRoot();
154 if (!pNewRoot) 154 if (!pNewRoot)
155 return false; 155 return false;
156 156
157 CPDF_Dictionary* pDocInfoDict = m_pDestPDFDoc->GetInfo(); 157 CPDF_Dictionary* pDocInfoDict = m_pDestPDFDoc->GetInfo();
158 if (!pDocInfoDict) 158 if (!pDocInfoDict)
159 return false; 159 return false;
160 160
161 CFX_ByteString producerstr; 161 pDocInfoDict->SetNewFor<CPDF_String>("Producer", "PDFium", false);
162 producerstr.Format("PDFium");
163 pDocInfoDict->SetFor("Producer",
164 new CPDF_String(nullptr, producerstr, false));
165 162
166 CFX_ByteString cbRootType = pNewRoot->GetStringFor("Type", ""); 163 CFX_ByteString cbRootType = pNewRoot->GetStringFor("Type", "");
167 if (cbRootType.IsEmpty()) 164 if (cbRootType.IsEmpty())
168 pNewRoot->SetFor("Type", new CPDF_Name(nullptr, "Catalog")); 165 pNewRoot->SetNewFor<CPDF_Name>("Type", "Catalog");
169 166
170 CPDF_Object* pElement = pNewRoot->GetObjectFor("Pages"); 167 CPDF_Object* pElement = pNewRoot->GetObjectFor("Pages");
171 CPDF_Dictionary* pNewPages = 168 CPDF_Dictionary* pNewPages =
172 pElement ? ToDictionary(pElement->GetDirect()) : nullptr; 169 pElement ? ToDictionary(pElement->GetDirect()) : nullptr;
173 if (!pNewPages) { 170 if (!pNewPages) {
174 pNewPages = m_pDestPDFDoc->NewIndirect<CPDF_Dictionary>(); 171 pNewPages = m_pDestPDFDoc->NewIndirect<CPDF_Dictionary>();
175 pNewRoot->SetReferenceFor("Pages", m_pDestPDFDoc, pNewPages); 172 pNewRoot->SetNewFor<CPDF_Reference>("Pages", m_pDestPDFDoc,
173 pNewPages->GetObjNum());
176 } 174 }
177 175
178 CFX_ByteString cbPageType = pNewPages->GetStringFor("Type", ""); 176 CFX_ByteString cbPageType = pNewPages->GetStringFor("Type", "");
179 if (cbPageType.IsEmpty()) 177 if (cbPageType.IsEmpty())
180 pNewPages->SetFor("Type", new CPDF_Name(nullptr, "Pages")); 178 pNewPages->SetNewFor<CPDF_Name>("Type", "Pages");
181 179
182 if (!pNewPages->GetArrayFor("Kids")) { 180 if (!pNewPages->GetArrayFor("Kids")) {
183 pNewPages->SetIntegerFor("Count", 0); 181 pNewPages->SetNewFor<CPDF_Number>("Count", 0);
184 pNewPages->SetReferenceFor("Kids", m_pDestPDFDoc, 182 pNewPages->SetNewFor<CPDF_Reference>(
185 m_pDestPDFDoc->NewIndirect<CPDF_Array>()); 183 "Kids", m_pDestPDFDoc,
184 m_pDestPDFDoc->NewIndirect<CPDF_Array>()->GetObjNum());
186 } 185 }
187 186
188 return true; 187 return true;
189 } 188 }
190 189
191 bool CPDF_PageOrganizer::ExportPage(const std::vector<uint16_t>& pageNums, 190 bool CPDF_PageOrganizer::ExportPage(const std::vector<uint16_t>& pageNums,
192 int nIndex) { 191 int nIndex) {
193 int curpage = nIndex; 192 int curpage = nIndex;
194 auto pObjNumberMap = pdfium::MakeUnique<ObjectNumberMap>(); 193 auto pObjNumberMap = pdfium::MakeUnique<ObjectNumberMap>();
195 int nSize = pdfium::CollectionSize<int>(pageNums); 194 int nSize = pdfium::CollectionSize<int>(pageNums);
196 for (int i = 0; i < nSize; ++i) { 195 for (int i = 0; i < nSize; ++i) {
197 CPDF_Dictionary* pCurPageDict = m_pDestPDFDoc->CreateNewPage(curpage); 196 CPDF_Dictionary* pCurPageDict = m_pDestPDFDoc->CreateNewPage(curpage);
198 CPDF_Dictionary* pSrcPageDict = m_pSrcPDFDoc->GetPage(pageNums[i] - 1); 197 CPDF_Dictionary* pSrcPageDict = m_pSrcPDFDoc->GetPage(pageNums[i] - 1);
199 if (!pSrcPageDict || !pCurPageDict) 198 if (!pSrcPageDict || !pCurPageDict)
200 return false; 199 return false;
201 200
202 // Clone the page dictionary 201 // Clone the page dictionary
203 for (const auto& it : *pSrcPageDict) { 202 for (const auto& it : *pSrcPageDict) {
204 const CFX_ByteString& cbSrcKeyStr = it.first; 203 const CFX_ByteString& cbSrcKeyStr = it.first;
205 CPDF_Object* pObj = it.second;
206 if (cbSrcKeyStr == "Type" || cbSrcKeyStr == "Parent") 204 if (cbSrcKeyStr == "Type" || cbSrcKeyStr == "Parent")
207 continue; 205 continue;
208 206
209 pCurPageDict->SetFor(cbSrcKeyStr, pObj->Clone().release()); 207 CPDF_Object* pObj = it.second.get();
208 pCurPageDict->SetFor(cbSrcKeyStr, pObj->Clone());
210 } 209 }
211 210
212 // inheritable item 211 // inheritable item
213 // 1 MediaBox - required 212 // 1 MediaBox - required
214 if (!CopyInheritable(pCurPageDict, pSrcPageDict, "MediaBox")) { 213 if (!CopyInheritable(pCurPageDict, pSrcPageDict, "MediaBox")) {
215 // Search for "CropBox" in the source page dictionary, 214 // Search for "CropBox" in the source page dictionary,
216 // if it does not exists, use the default letter size. 215 // if it does not exists, use the default letter size.
217 CPDF_Object* pInheritable = 216 CPDF_Object* pInheritable =
218 PageDictGetInheritableTag(pSrcPageDict, "CropBox"); 217 PageDictGetInheritableTag(pSrcPageDict, "CropBox");
219 if (pInheritable) { 218 if (pInheritable) {
220 pCurPageDict->SetFor("MediaBox", pInheritable->Clone().release()); 219 pCurPageDict->SetFor("MediaBox", pInheritable->Clone());
221 } else { 220 } else {
222 // Make the default size to be letter size (8.5'x11') 221 // Make the default size to be letter size (8.5'x11')
223 CPDF_Array* pArray = new CPDF_Array; 222 CPDF_Array* pArray = pCurPageDict->SetNewFor<CPDF_Array>("MediaBox");
224 pArray->AddNew<CPDF_Number>(0); 223 pArray->AddNew<CPDF_Number>(0);
225 pArray->AddNew<CPDF_Number>(0); 224 pArray->AddNew<CPDF_Number>(0);
226 pArray->AddNew<CPDF_Number>(612); 225 pArray->AddNew<CPDF_Number>(612);
227 pArray->AddNew<CPDF_Number>(792); 226 pArray->AddNew<CPDF_Number>(792);
228 pCurPageDict->SetFor("MediaBox", pArray);
229 } 227 }
230 } 228 }
231 229
232 // 2 Resources - required 230 // 2 Resources - required
233 if (!CopyInheritable(pCurPageDict, pSrcPageDict, "Resources")) 231 if (!CopyInheritable(pCurPageDict, pSrcPageDict, "Resources"))
234 return false; 232 return false;
235 233
236 // 3 CropBox - optional 234 // 3 CropBox - optional
237 CopyInheritable(pCurPageDict, pSrcPageDict, "CropBox"); 235 CopyInheritable(pCurPageDict, pSrcPageDict, "CropBox");
238 // 4 Rotate - optional 236 // 4 Rotate - optional
(...skipping 19 matching lines...) Expand all
258 if (newobjnum == 0) 256 if (newobjnum == 0)
259 return false; 257 return false;
260 pReference->SetRef(m_pDestPDFDoc, newobjnum); 258 pReference->SetRef(m_pDestPDFDoc, newobjnum);
261 break; 259 break;
262 } 260 }
263 case CPDF_Object::DICTIONARY: { 261 case CPDF_Object::DICTIONARY: {
264 CPDF_Dictionary* pDict = pObj->AsDictionary(); 262 CPDF_Dictionary* pDict = pObj->AsDictionary();
265 auto it = pDict->begin(); 263 auto it = pDict->begin();
266 while (it != pDict->end()) { 264 while (it != pDict->end()) {
267 const CFX_ByteString& key = it->first; 265 const CFX_ByteString& key = it->first;
268 CPDF_Object* pNextObj = it->second; 266 CPDF_Object* pNextObj = it->second.get();
269 ++it; 267 ++it;
270 if (key == "Parent" || key == "Prev" || key == "First") 268 if (key == "Parent" || key == "Prev" || key == "First")
271 continue; 269 continue;
272 if (!pNextObj) 270 if (!pNextObj)
273 return false; 271 return false;
274 if (!UpdateReference(pNextObj, pObjNumberMap)) 272 if (!UpdateReference(pNextObj, pObjNumberMap))
275 pDict->RemoveFor(key); 273 pDict->RemoveFor(key);
276 } 274 }
277 break; 275 break;
278 } 276 }
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 377
380 CPDF_Dictionary* pSrcDict = pSrcDoc->GetRoot(); 378 CPDF_Dictionary* pSrcDict = pSrcDoc->GetRoot();
381 pSrcDict = pSrcDict->GetDictFor("ViewerPreferences"); 379 pSrcDict = pSrcDict->GetDictFor("ViewerPreferences");
382 if (!pSrcDict) 380 if (!pSrcDict)
383 return false; 381 return false;
384 382
385 CPDF_Dictionary* pDstDict = pDstDoc->GetRoot(); 383 CPDF_Dictionary* pDstDict = pDstDoc->GetRoot();
386 if (!pDstDict) 384 if (!pDstDict)
387 return false; 385 return false;
388 386
389 pDstDict->SetFor("ViewerPreferences", 387 pDstDict->SetFor("ViewerPreferences", pSrcDict->CloneDirectObject());
390 pSrcDict->CloneDirectObject().release());
391 return true; 388 return true;
392 } 389 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698