blob: f41f904e174bf80c3e17cb252880f2d4bc68dd51 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_ppo.h"
8
Dan Sinclair3ebd1212016-03-09 09:59:23 -05009#include <map>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080010#include <memory>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050011#include <vector>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080012
dsinclair488b7ad2016-10-04 11:55:50 -070013#include "core/fpdfapi/parser/cpdf_array.h"
14#include "core/fpdfapi/parser/cpdf_document.h"
15#include "core/fpdfapi/parser/cpdf_name.h"
16#include "core/fpdfapi/parser/cpdf_number.h"
17#include "core/fpdfapi/parser/cpdf_reference.h"
18#include "core/fpdfapi/parser/cpdf_stream.h"
19#include "core/fpdfapi/parser/cpdf_string.h"
dsinclair114e46a2016-09-29 17:18:21 -070020#include "fpdfsdk/fsdk_define.h"
Tom Sepez11d93552016-02-09 09:55:54 -080021#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023class CPDF_PageOrganizer {
24 public:
tsepezc3255f52016-03-25 14:52:27 -070025 using ObjectNumberMap = std::map<uint32_t, uint32_t>;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 CPDF_PageOrganizer();
27 ~CPDF_PageOrganizer();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070028
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029 FX_BOOL PDFDocInit(CPDF_Document* pDestPDFDoc, CPDF_Document* pSrcPDFDoc);
30 FX_BOOL ExportPage(CPDF_Document* pSrcPDFDoc,
Tom Sepez62a70f92016-03-21 15:00:20 -070031 std::vector<uint16_t>* pPageNums,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 CPDF_Document* pDestPDFDoc,
33 int nIndex);
34 CPDF_Object* PageDictGetInheritableTag(CPDF_Dictionary* pDict,
tsepez71a452f2016-05-13 17:51:27 -070035 const CFX_ByteString& bsSrctag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 FX_BOOL UpdateReference(CPDF_Object* pObj,
37 CPDF_Document* pDoc,
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080038 ObjectNumberMap* pObjNumberMap);
tsepezc3255f52016-03-25 14:52:27 -070039 uint32_t GetNewObjId(CPDF_Document* pDoc,
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080040 ObjectNumberMap* pObjNumberMap,
41 CPDF_Reference* pRef);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042};
43
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044CPDF_PageOrganizer::CPDF_PageOrganizer() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046CPDF_PageOrganizer::~CPDF_PageOrganizer() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048FX_BOOL CPDF_PageOrganizer::PDFDocInit(CPDF_Document* pDestPDFDoc,
49 CPDF_Document* pSrcPDFDoc) {
50 if (!pDestPDFDoc || !pSrcPDFDoc)
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080051 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 CPDF_Dictionary* pNewRoot = pDestPDFDoc->GetRoot();
54 if (!pNewRoot)
Tom Sepez2f2ffec2015-07-23 14:42:09 -070055 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 CPDF_Dictionary* DInfoDict = pDestPDFDoc->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 if (!DInfoDict)
59 return FALSE;
60
61 CFX_ByteString producerstr;
62 producerstr.Format("PDFium");
dsinclair38fd8442016-09-15 10:15:32 -070063 DInfoDict->SetFor("Producer", new CPDF_String(producerstr, FALSE));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064
dsinclair38fd8442016-09-15 10:15:32 -070065 CFX_ByteString cbRootType = pNewRoot->GetStringFor("Type", "");
tsepez9f2970c2016-04-01 10:23:04 -070066 if (cbRootType.IsEmpty())
dsinclair38fd8442016-09-15 10:15:32 -070067 pNewRoot->SetFor("Type", new CPDF_Name("Catalog"));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068
dsinclair38fd8442016-09-15 10:15:32 -070069 CPDF_Object* pElement = pNewRoot->GetObjectFor("Pages");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080070 CPDF_Dictionary* pNewPages =
71 pElement ? ToDictionary(pElement->GetDirect()) : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 if (!pNewPages) {
Tom Sepezc25a4212016-10-14 17:45:56 -070073 pNewPages = new CPDF_Dictionary(pDestPDFDoc->GetByteStringPool());
74 pNewRoot->SetReferenceFor("Pages", pDestPDFDoc,
75 pDestPDFDoc->AddIndirectObject(pNewPages));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 }
77
dsinclair38fd8442016-09-15 10:15:32 -070078 CFX_ByteString cbPageType = pNewPages->GetStringFor("Type", "");
tsepez9f2970c2016-04-01 10:23:04 -070079 if (cbPageType == "") {
dsinclair38fd8442016-09-15 10:15:32 -070080 pNewPages->SetFor("Type", new CPDF_Name("Pages"));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 }
82
tsepezbb577af2016-09-21 19:10:19 -070083 if (!pNewPages->GetArrayFor("Kids")) {
84 pNewPages->SetIntegerFor("Count", 0);
85 pNewPages->SetReferenceFor("Kids", pDestPDFDoc,
Tom Sepezc25a4212016-10-14 17:45:56 -070086 pDestPDFDoc->AddIndirectObject(new CPDF_Array));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 }
88
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080089 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090}
91
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092FX_BOOL CPDF_PageOrganizer::ExportPage(CPDF_Document* pSrcPDFDoc,
Tom Sepez62a70f92016-03-21 15:00:20 -070093 std::vector<uint16_t>* pPageNums,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 CPDF_Document* pDestPDFDoc,
95 int nIndex) {
96 int curpage = nIndex;
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080097 std::unique_ptr<ObjectNumberMap> pObjNumberMap(new ObjectNumberMap);
Tom Sepez11d93552016-02-09 09:55:54 -080098 int nSize = pdfium::CollectionSize<int>(*pPageNums);
99 for (int i = 0; i < nSize; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 CPDF_Dictionary* pCurPageDict = pDestPDFDoc->CreateNewPage(curpage);
Tom Sepez11d93552016-02-09 09:55:54 -0800101 CPDF_Dictionary* pSrcPageDict = pSrcPDFDoc->GetPage(pPageNums->at(i) - 1);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800102 if (!pSrcPageDict || !pCurPageDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104
Lei Zhang4880d1a2015-12-18 17:05:11 -0800105 // Clone the page dictionary
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800106 for (const auto& it : *pSrcPageDict) {
107 const CFX_ByteString& cbSrcKeyStr = it.first;
tsepezd5bd8a12016-10-17 11:13:54 -0700108 CPDF_Object* pObj = it.second;
109 if (cbSrcKeyStr.Compare(("Type")) && cbSrcKeyStr.Compare(("Parent"))) {
tsepez7b1ccf92016-04-14 11:04:57 -0700110 if (pCurPageDict->KeyExist(cbSrcKeyStr))
dsinclair38fd8442016-09-15 10:15:32 -0700111 pCurPageDict->RemoveFor(cbSrcKeyStr);
112 pCurPageDict->SetFor(cbSrcKeyStr, pObj->Clone());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 }
114 }
115
Lei Zhang4880d1a2015-12-18 17:05:11 -0800116 // inheritable item
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800117 CPDF_Object* pInheritable = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 // 1 MediaBox //required
119 if (!pCurPageDict->KeyExist("MediaBox")) {
120 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "MediaBox");
121 if (!pInheritable) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800122 // Search the "CropBox" from source page dictionary,
123 // if not exists,we take the letter size.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "CropBox");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800125 if (pInheritable) {
dsinclair38fd8442016-09-15 10:15:32 -0700126 pCurPageDict->SetFor("MediaBox", pInheritable->Clone());
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800127 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 // Make the default size to be letter size (8.5'x11')
129 CPDF_Array* pArray = new CPDF_Array;
130 pArray->AddNumber(0);
131 pArray->AddNumber(0);
132 pArray->AddNumber(612);
133 pArray->AddNumber(792);
dsinclair38fd8442016-09-15 10:15:32 -0700134 pCurPageDict->SetFor("MediaBox", pArray);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 }
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800136 } else {
dsinclair38fd8442016-09-15 10:15:32 -0700137 pCurPageDict->SetFor("MediaBox", pInheritable->Clone());
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800138 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 }
140 // 2 Resources //required
141 if (!pCurPageDict->KeyExist("Resources")) {
142 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "Resources");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800143 if (!pInheritable)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700144 return FALSE;
dsinclair38fd8442016-09-15 10:15:32 -0700145 pCurPageDict->SetFor("Resources", pInheritable->Clone());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 }
147 // 3 CropBox //Optional
148 if (!pCurPageDict->KeyExist("CropBox")) {
149 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "CropBox");
150 if (pInheritable)
dsinclair38fd8442016-09-15 10:15:32 -0700151 pCurPageDict->SetFor("CropBox", pInheritable->Clone());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 }
153 // 4 Rotate //Optional
154 if (!pCurPageDict->KeyExist("Rotate")) {
155 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "Rotate");
156 if (pInheritable)
dsinclair38fd8442016-09-15 10:15:32 -0700157 pCurPageDict->SetFor("Rotate", pInheritable->Clone());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 }
159
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 // Update the reference
tsepezc3255f52016-03-25 14:52:27 -0700161 uint32_t dwOldPageObj = pSrcPageDict->GetObjNum();
162 uint32_t dwNewPageObj = pCurPageDict->GetObjNum();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800164 (*pObjNumberMap)[dwOldPageObj] = dwNewPageObj;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800166 UpdateReference(pCurPageDict, pDestPDFDoc, pObjNumberMap.get());
167 ++curpage;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 }
169
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170 return TRUE;
171}
172
173CPDF_Object* CPDF_PageOrganizer::PageDictGetInheritableTag(
174 CPDF_Dictionary* pDict,
tsepez71a452f2016-05-13 17:51:27 -0700175 const CFX_ByteString& bsSrcTag) {
176 if (!pDict || bsSrcTag.IsEmpty())
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800177 return nullptr;
178 if (!pDict->KeyExist("Parent") || !pDict->KeyExist("Type"))
179 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180
dsinclair38fd8442016-09-15 10:15:32 -0700181 CPDF_Object* pType = pDict->GetObjectFor("Type")->GetDirect();
Dan Sinclair710c9092015-10-21 15:46:10 -0400182 if (!ToName(pType))
183 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 if (pType->GetString().Compare("Page"))
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800185 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186
dsinclair38fd8442016-09-15 10:15:32 -0700187 CPDF_Dictionary* pp =
188 ToDictionary(pDict->GetObjectFor("Parent")->GetDirect());
Dan Sinclairf1251c12015-10-20 16:24:45 -0400189 if (!pp)
190 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191
tsepez71a452f2016-05-13 17:51:27 -0700192 if (pDict->KeyExist(bsSrcTag))
dsinclair38fd8442016-09-15 10:15:32 -0700193 return pDict->GetObjectFor(bsSrcTag);
Dan Sinclairf1251c12015-10-20 16:24:45 -0400194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 while (pp) {
tsepez71a452f2016-05-13 17:51:27 -0700196 if (pp->KeyExist(bsSrcTag))
dsinclair38fd8442016-09-15 10:15:32 -0700197 return pp->GetObjectFor(bsSrcTag);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800198 if (!pp->KeyExist("Parent"))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 break;
dsinclair38fd8442016-09-15 10:15:32 -0700200 pp = ToDictionary(pp->GetObjectFor("Parent")->GetDirect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 }
Dan Sinclairf1251c12015-10-20 16:24:45 -0400202 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203}
204
205FX_BOOL CPDF_PageOrganizer::UpdateReference(CPDF_Object* pObj,
206 CPDF_Document* pDoc,
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800207 ObjectNumberMap* pObjNumberMap) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 switch (pObj->GetType()) {
Tom Sepez8e5cd192016-01-26 13:20:26 -0800209 case CPDF_Object::REFERENCE: {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400210 CPDF_Reference* pReference = pObj->AsReference();
tsepezc3255f52016-03-25 14:52:27 -0700211 uint32_t newobjnum = GetNewObjId(pDoc, pObjNumberMap, pReference);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 if (newobjnum == 0)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700213 return FALSE;
Dan Sinclairbf81c142015-10-26 16:54:39 -0400214 pReference->SetRef(pDoc, newobjnum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 break;
216 }
Tom Sepez8e5cd192016-01-26 13:20:26 -0800217 case CPDF_Object::DICTIONARY: {
Dan Sinclairf1251c12015-10-20 16:24:45 -0400218 CPDF_Dictionary* pDict = pObj->AsDictionary();
Oliver Changbd292ae2016-01-13 18:46:09 -0800219 auto it = pDict->begin();
220 while (it != pDict->end()) {
221 const CFX_ByteString& key = it->first;
tsepezd5bd8a12016-10-17 11:13:54 -0700222 CPDF_Object* pNextObj = it->second;
Oliver Changbd292ae2016-01-13 18:46:09 -0800223 ++it;
tsepezf86ca382016-09-13 12:23:30 -0700224 if (key == "Parent" || key == "Prev" || key == "First")
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 continue;
tsepezf86ca382016-09-13 12:23:30 -0700226 if (!pNextObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 return FALSE;
tsepezf86ca382016-09-13 12:23:30 -0700228 if (!UpdateReference(pNextObj, pDoc, pObjNumberMap))
dsinclair38fd8442016-09-15 10:15:32 -0700229 pDict->RemoveFor(key);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 }
231 break;
232 }
Tom Sepez8e5cd192016-01-26 13:20:26 -0800233 case CPDF_Object::ARRAY: {
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400234 CPDF_Array* pArray = pObj->AsArray();
Wei Lie1aebd42016-04-11 10:02:09 -0700235 for (size_t i = 0; i < pArray->GetCount(); ++i) {
tsepezbd567552016-03-29 14:51:50 -0700236 CPDF_Object* pNextObj = pArray->GetObjectAt(i);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800237 if (!pNextObj)
238 return FALSE;
239 if (!UpdateReference(pNextObj, pDoc, pObjNumberMap))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 return FALSE;
241 }
242 break;
243 }
Tom Sepez8e5cd192016-01-26 13:20:26 -0800244 case CPDF_Object::STREAM: {
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400245 CPDF_Stream* pStream = pObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 CPDF_Dictionary* pDict = pStream->GetDict();
247 if (pDict) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800248 if (!UpdateReference(pDict, pDoc, pObjNumberMap))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800250 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800252 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 break;
254 }
255 default:
256 break;
257 }
258
259 return TRUE;
260}
261
tsepezc3255f52016-03-25 14:52:27 -0700262uint32_t CPDF_PageOrganizer::GetNewObjId(CPDF_Document* pDoc,
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800263 ObjectNumberMap* pObjNumberMap,
264 CPDF_Reference* pRef) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 if (!pRef)
266 return 0;
267
tsepezc3255f52016-03-25 14:52:27 -0700268 uint32_t dwObjnum = pRef->GetRefObjNum();
269 uint32_t dwNewObjNum = 0;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800270 const auto it = pObjNumberMap->find(dwObjnum);
271 if (it != pObjNumberMap->end())
272 dwNewObjNum = it->second;
273 if (dwNewObjNum)
274 return dwNewObjNum;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275
276 CPDF_Object* pDirect = pRef->GetDirect();
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800277 if (!pDirect)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279
Tom Sepezc25a4212016-10-14 17:45:56 -0700280 CPDF_Object* pClone = pDirect->Clone();
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800281 if (!pClone)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283
Dan Sinclairf1251c12015-10-20 16:24:45 -0400284 if (CPDF_Dictionary* pDictClone = pClone->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 if (pDictClone->KeyExist("Type")) {
dsinclair38fd8442016-09-15 10:15:32 -0700286 CFX_ByteString strType = pDictClone->GetStringFor("Type");
tsepezb4c9f3f2016-04-13 15:41:21 -0700287 if (!FXSYS_stricmp(strType.c_str(), "Pages")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 pDictClone->Release();
289 return 4;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800290 }
tsepezb4c9f3f2016-04-13 15:41:21 -0700291 if (!FXSYS_stricmp(strType.c_str(), "Page")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 pDictClone->Release();
293 return 0;
294 }
295 }
296 }
Tom Sepezc25a4212016-10-14 17:45:56 -0700297 dwNewObjNum = pDoc->AddIndirectObject(pClone);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800298 (*pObjNumberMap)[dwObjnum] = dwNewObjNum;
Tom Sepezc25a4212016-10-14 17:45:56 -0700299 if (!UpdateReference(pClone, pDoc, pObjNumberMap)) {
300 pClone->Release();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 return 0;
302 }
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800303 return dwNewObjNum;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304}
305
306FPDF_BOOL ParserPageRangeString(CFX_ByteString rangstring,
Tom Sepez62a70f92016-03-21 15:00:20 -0700307 std::vector<uint16_t>* pageArray,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 int nCount) {
309 if (rangstring.GetLength() != 0) {
310 rangstring.Remove(' ');
311 int nLength = rangstring.GetLength();
312 CFX_ByteString cbCompareString("0123456789-,");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800313 for (int i = 0; i < nLength; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314 if (cbCompareString.Find(rangstring[i]) == -1)
315 return FALSE;
316 }
317 CFX_ByteString cbMidRange;
318 int nStringFrom = 0;
319 int nStringTo = 0;
320 while (nStringTo < nLength) {
321 nStringTo = rangstring.Find(',', nStringFrom);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800322 if (nStringTo == -1)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 nStringTo = nLength;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324 cbMidRange = rangstring.Mid(nStringFrom, nStringTo - nStringFrom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 int nMid = cbMidRange.Find('-');
326 if (nMid == -1) {
tsepezb4c9f3f2016-04-13 15:41:21 -0700327 long lPageNum = atol(cbMidRange.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 if (lPageNum <= 0 || lPageNum > nCount)
329 return FALSE;
Tom Sepez62a70f92016-03-21 15:00:20 -0700330 pageArray->push_back((uint16_t)lPageNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331 } else {
tsepezb4c9f3f2016-04-13 15:41:21 -0700332 int nStartPageNum = atol(cbMidRange.Mid(0, nMid).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 if (nStartPageNum == 0)
334 return FALSE;
335
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800336 ++nMid;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 int nEnd = cbMidRange.GetLength() - nMid;
338 if (nEnd == 0)
339 return FALSE;
340
tsepezb4c9f3f2016-04-13 15:41:21 -0700341 int nEndPageNum = atol(cbMidRange.Mid(nMid, nEnd).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 if (nStartPageNum < 0 || nStartPageNum > nEndPageNum ||
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800343 nEndPageNum > nCount) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800345 }
346 for (int i = nStartPageNum; i <= nEndPageNum; ++i) {
Tom Sepez11d93552016-02-09 09:55:54 -0800347 pageArray->push_back(i);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800348 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 }
350 nStringFrom = nStringTo + 1;
351 }
352 }
353 return TRUE;
354}
355
356DLLEXPORT FPDF_BOOL STDCALL FPDF_ImportPages(FPDF_DOCUMENT dest_doc,
357 FPDF_DOCUMENT src_doc,
358 FPDF_BYTESTRING pagerange,
359 int index) {
Tom Sepez471a1032015-10-15 16:17:18 -0700360 CPDF_Document* pDestDoc = CPDFDocumentFromFPDFDocument(dest_doc);
361 if (!dest_doc)
362 return FALSE;
363
364 CPDF_Document* pSrcDoc = CPDFDocumentFromFPDFDocument(src_doc);
365 if (!pSrcDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800367
Tom Sepez62a70f92016-03-21 15:00:20 -0700368 std::vector<uint16_t> pageArray;
Tom Sepez471a1032015-10-15 16:17:18 -0700369 int nCount = pSrcDoc->GetPageCount();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 if (pagerange) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800371 if (!ParserPageRangeString(pagerange, &pageArray, nCount))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700372 return FALSE;
373 } else {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800374 for (int i = 1; i <= nCount; ++i) {
Tom Sepez11d93552016-02-09 09:55:54 -0800375 pageArray.push_back(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 }
377 }
378
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 CPDF_PageOrganizer pageOrg;
Tom Sepez471a1032015-10-15 16:17:18 -0700380 pageOrg.PDFDocInit(pDestDoc, pSrcDoc);
381 return pageOrg.ExportPage(pSrcDoc, &pageArray, pDestDoc, index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700382}
383
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384DLLEXPORT FPDF_BOOL STDCALL FPDF_CopyViewerPreferences(FPDF_DOCUMENT dest_doc,
385 FPDF_DOCUMENT src_doc) {
Tom Sepez471a1032015-10-15 16:17:18 -0700386 CPDF_Document* pDstDoc = CPDFDocumentFromFPDFDocument(dest_doc);
387 if (!pDstDoc)
388 return FALSE;
389
390 CPDF_Document* pSrcDoc = CPDFDocumentFromFPDFDocument(src_doc);
391 if (!pSrcDoc)
392 return FALSE;
393
394 CPDF_Dictionary* pSrcDict = pSrcDoc->GetRoot();
dsinclair38fd8442016-09-15 10:15:32 -0700395 pSrcDict = pSrcDict->GetDictFor("ViewerPreferences");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 if (!pSrcDict)
397 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700398
399 CPDF_Dictionary* pDstDict = pDstDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 if (!pDstDict)
401 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700402
dsinclair38fd8442016-09-15 10:15:32 -0700403 pDstDict->SetFor("ViewerPreferences", pSrcDict->CloneDirectObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404 return TRUE;
405}