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

Side by Side Diff: fpdfsdk/fpdfppo.cpp

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

Powered by Google App Engine
This is Rietveld 408576698