blob: 4377c3ed94a2c4ba352dab9cd09f2061249f265a [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
Dan Sinclairaa403d32016-03-15 14:57:22 -040013#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
14#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
15#include "core/fpdfapi/fpdf_parser/include/cpdf_name.h"
16#include "core/fpdfapi/fpdf_parser/include/cpdf_number.h"
17#include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h"
Dan Sinclair584b1e62016-03-21 09:15:45 -040018#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040019#include "core/fpdfapi/fpdf_parser/include/cpdf_string.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080020#include "fpdfsdk/include/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:
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080025 using ObjectNumberMap = std::map<FX_DWORD, FX_DWORD>;
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 Sepez11d93552016-02-09 09:55:54 -080031 std::vector<FX_WORD>* pPageNums,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 CPDF_Document* pDestPDFDoc,
33 int nIndex);
34 CPDF_Object* PageDictGetInheritableTag(CPDF_Dictionary* pDict,
35 CFX_ByteString nSrctag);
36 FX_BOOL UpdateReference(CPDF_Object* pObj,
37 CPDF_Document* pDoc,
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080038 ObjectNumberMap* pObjNumberMap);
39 FX_DWORD GetNewObjId(CPDF_Document* pDoc,
40 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
Lei Zhang4880d1a2015-12-18 17:05:11 -080057 // Set the document information
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 CPDF_Dictionary* DInfoDict = pDestPDFDoc->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 if (!DInfoDict)
60 return FALSE;
61
62 CFX_ByteString producerstr;
63 producerstr.Format("PDFium");
Lei Zhang4880d1a2015-12-18 17:05:11 -080064 DInfoDict->SetAt("Producer", new CPDF_String(producerstr, FALSE));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065
Lei Zhang4880d1a2015-12-18 17:05:11 -080066 // Set type
Wei Li9b761132016-01-29 15:44:20 -080067 CFX_ByteString cbRootType = pNewRoot->GetStringBy("Type", "");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 if (cbRootType.Equal("")) {
69 pNewRoot->SetAt("Type", new CPDF_Name("Catalog"));
70 }
71
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080072 CPDF_Object* pElement = pNewRoot->GetElement("Pages");
73 CPDF_Dictionary* pNewPages =
74 pElement ? ToDictionary(pElement->GetDirect()) : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 if (!pNewPages) {
76 pNewPages = new CPDF_Dictionary;
77 FX_DWORD NewPagesON = pDestPDFDoc->AddIndirectObject(pNewPages);
78 pNewRoot->SetAt("Pages", new CPDF_Reference(pDestPDFDoc, NewPagesON));
79 }
80
Wei Li9b761132016-01-29 15:44:20 -080081 CFX_ByteString cbPageType = pNewPages->GetStringBy("Type", "");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 if (cbPageType.Equal("")) {
83 pNewPages->SetAt("Type", new CPDF_Name("Pages"));
84 }
85
Wei Li9b761132016-01-29 15:44:20 -080086 CPDF_Array* pKeysArray = pNewPages->GetArrayBy("Kids");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080087 if (!pKeysArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 CPDF_Array* pNewKids = new CPDF_Array;
89 FX_DWORD Kidsobjnum = -1;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080090 Kidsobjnum = pDestPDFDoc->AddIndirectObject(pNewKids);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080092 pNewPages->SetAt("Kids", new CPDF_Reference(pDestPDFDoc, Kidsobjnum));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 pNewPages->SetAt("Count", new CPDF_Number(0));
94 }
95
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080096 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097}
98
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099FX_BOOL CPDF_PageOrganizer::ExportPage(CPDF_Document* pSrcPDFDoc,
Tom Sepez11d93552016-02-09 09:55:54 -0800100 std::vector<FX_WORD>* pPageNums,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 CPDF_Document* pDestPDFDoc,
102 int nIndex) {
103 int curpage = nIndex;
Lei Zhangaa8bf7e2015-12-24 19:13:32 -0800104 std::unique_ptr<ObjectNumberMap> pObjNumberMap(new ObjectNumberMap);
Tom Sepez11d93552016-02-09 09:55:54 -0800105 int nSize = pdfium::CollectionSize<int>(*pPageNums);
106 for (int i = 0; i < nSize; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 CPDF_Dictionary* pCurPageDict = pDestPDFDoc->CreateNewPage(curpage);
Tom Sepez11d93552016-02-09 09:55:54 -0800108 CPDF_Dictionary* pSrcPageDict = pSrcPDFDoc->GetPage(pPageNums->at(i) - 1);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800109 if (!pSrcPageDict || !pCurPageDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111
Lei Zhang4880d1a2015-12-18 17:05:11 -0800112 // Clone the page dictionary
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800113 for (const auto& it : *pSrcPageDict) {
114 const CFX_ByteString& cbSrcKeyStr = it.first;
115 CPDF_Object* pObj = it.second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 if (cbSrcKeyStr.Compare(("Type")) && cbSrcKeyStr.Compare(("Parent"))) {
117 if (pCurPageDict->KeyExist(cbSrcKeyStr))
118 pCurPageDict->RemoveAt(cbSrcKeyStr);
119 pCurPageDict->SetAt(cbSrcKeyStr, pObj->Clone());
120 }
121 }
122
Lei Zhang4880d1a2015-12-18 17:05:11 -0800123 // inheritable item
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800124 CPDF_Object* pInheritable = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 // 1 MediaBox //required
126 if (!pCurPageDict->KeyExist("MediaBox")) {
127 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "MediaBox");
128 if (!pInheritable) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800129 // Search the "CropBox" from source page dictionary,
130 // if not exists,we take the letter size.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "CropBox");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800132 if (pInheritable) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 pCurPageDict->SetAt("MediaBox", pInheritable->Clone());
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800134 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 // Make the default size to be letter size (8.5'x11')
136 CPDF_Array* pArray = new CPDF_Array;
137 pArray->AddNumber(0);
138 pArray->AddNumber(0);
139 pArray->AddNumber(612);
140 pArray->AddNumber(792);
141 pCurPageDict->SetAt("MediaBox", pArray);
142 }
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800143 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 pCurPageDict->SetAt("MediaBox", pInheritable->Clone());
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800145 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 }
147 // 2 Resources //required
148 if (!pCurPageDict->KeyExist("Resources")) {
149 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "Resources");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800150 if (!pInheritable)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700151 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 pCurPageDict->SetAt("Resources", pInheritable->Clone());
153 }
154 // 3 CropBox //Optional
155 if (!pCurPageDict->KeyExist("CropBox")) {
156 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "CropBox");
157 if (pInheritable)
158 pCurPageDict->SetAt("CropBox", pInheritable->Clone());
159 }
160 // 4 Rotate //Optional
161 if (!pCurPageDict->KeyExist("Rotate")) {
162 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "Rotate");
163 if (pInheritable)
164 pCurPageDict->SetAt("Rotate", pInheritable->Clone());
165 }
166
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 // Update the reference
168 FX_DWORD dwOldPageObj = pSrcPageDict->GetObjNum();
169 FX_DWORD dwNewPageObj = pCurPageDict->GetObjNum();
170
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800171 (*pObjNumberMap)[dwOldPageObj] = dwNewPageObj;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800173 UpdateReference(pCurPageDict, pDestPDFDoc, pObjNumberMap.get());
174 ++curpage;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 }
176
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 return TRUE;
178}
179
180CPDF_Object* CPDF_PageOrganizer::PageDictGetInheritableTag(
181 CPDF_Dictionary* pDict,
182 CFX_ByteString nSrctag) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800183 if (!pDict || nSrctag.IsEmpty())
184 return nullptr;
185 if (!pDict->KeyExist("Parent") || !pDict->KeyExist("Type"))
186 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187
188 CPDF_Object* pType = pDict->GetElement("Type")->GetDirect();
Dan Sinclair710c9092015-10-21 15:46:10 -0400189 if (!ToName(pType))
190 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 if (pType->GetString().Compare("Page"))
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800192 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193
Dan Sinclairf1251c12015-10-20 16:24:45 -0400194 CPDF_Dictionary* pp = ToDictionary(pDict->GetElement("Parent")->GetDirect());
195 if (!pp)
196 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197
198 if (pDict->KeyExist((const char*)nSrctag))
199 return pDict->GetElement((const char*)nSrctag);
Dan Sinclairf1251c12015-10-20 16:24:45 -0400200
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 while (pp) {
202 if (pp->KeyExist((const char*)nSrctag))
203 return pp->GetElement((const char*)nSrctag);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800204 if (!pp->KeyExist("Parent"))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 break;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400206 pp = ToDictionary(pp->GetElement("Parent")->GetDirect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207 }
Dan Sinclairf1251c12015-10-20 16:24:45 -0400208 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209}
210
211FX_BOOL CPDF_PageOrganizer::UpdateReference(CPDF_Object* pObj,
212 CPDF_Document* pDoc,
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800213 ObjectNumberMap* pObjNumberMap) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 switch (pObj->GetType()) {
Tom Sepez8e5cd192016-01-26 13:20:26 -0800215 case CPDF_Object::REFERENCE: {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400216 CPDF_Reference* pReference = pObj->AsReference();
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800217 FX_DWORD newobjnum = GetNewObjId(pDoc, pObjNumberMap, pReference);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 if (newobjnum == 0)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700219 return FALSE;
Dan Sinclairbf81c142015-10-26 16:54:39 -0400220 pReference->SetRef(pDoc, newobjnum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 break;
222 }
Tom Sepez8e5cd192016-01-26 13:20:26 -0800223 case CPDF_Object::DICTIONARY: {
Dan Sinclairf1251c12015-10-20 16:24:45 -0400224 CPDF_Dictionary* pDict = pObj->AsDictionary();
Oliver Changbd292ae2016-01-13 18:46:09 -0800225 auto it = pDict->begin();
226 while (it != pDict->end()) {
227 const CFX_ByteString& key = it->first;
228 CPDF_Object* pNextObj = it->second;
229 ++it;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 if (!FXSYS_strcmp(key, "Parent") || !FXSYS_strcmp(key, "Prev") ||
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800231 !FXSYS_strcmp(key, "First")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 continue;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800233 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 if (pNextObj) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800235 if (!UpdateReference(pNextObj, pDoc, pObjNumberMap))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 pDict->RemoveAt(key);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800237 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800239 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 }
241 break;
242 }
Tom Sepez8e5cd192016-01-26 13:20:26 -0800243 case CPDF_Object::ARRAY: {
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400244 CPDF_Array* pArray = pObj->AsArray();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 FX_DWORD count = pArray->GetCount();
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800246 for (FX_DWORD i = 0; i < count; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 CPDF_Object* pNextObj = pArray->GetElement(i);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800248 if (!pNextObj)
249 return FALSE;
250 if (!UpdateReference(pNextObj, pDoc, pObjNumberMap))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 return FALSE;
252 }
253 break;
254 }
Tom Sepez8e5cd192016-01-26 13:20:26 -0800255 case CPDF_Object::STREAM: {
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400256 CPDF_Stream* pStream = pObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 CPDF_Dictionary* pDict = pStream->GetDict();
258 if (pDict) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800259 if (!UpdateReference(pDict, pDoc, pObjNumberMap))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800261 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800263 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 break;
265 }
266 default:
267 break;
268 }
269
270 return TRUE;
271}
272
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800273FX_DWORD CPDF_PageOrganizer::GetNewObjId(CPDF_Document* pDoc,
274 ObjectNumberMap* pObjNumberMap,
275 CPDF_Reference* pRef) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 if (!pRef)
277 return 0;
278
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800279 FX_DWORD dwObjnum = pRef->GetRefObjNum();
280 FX_DWORD dwNewObjNum = 0;
281 const auto it = pObjNumberMap->find(dwObjnum);
282 if (it != pObjNumberMap->end())
283 dwNewObjNum = it->second;
284 if (dwNewObjNum)
285 return dwNewObjNum;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286
287 CPDF_Object* pDirect = pRef->GetDirect();
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800288 if (!pDirect)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290
291 CPDF_Object* pClone = pDirect->Clone();
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800292 if (!pClone)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294
Dan Sinclairf1251c12015-10-20 16:24:45 -0400295 if (CPDF_Dictionary* pDictClone = pClone->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296 if (pDictClone->KeyExist("Type")) {
Wei Li9b761132016-01-29 15:44:20 -0800297 CFX_ByteString strType = pDictClone->GetStringBy("Type");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 if (!FXSYS_stricmp(strType, "Pages")) {
299 pDictClone->Release();
300 return 4;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800301 }
302 if (!FXSYS_stricmp(strType, "Page")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 pDictClone->Release();
304 return 0;
305 }
306 }
307 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 dwNewObjNum = pDoc->AddIndirectObject(pClone);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800309 (*pObjNumberMap)[dwObjnum] = dwNewObjNum;
310 if (!UpdateReference(pClone, pDoc, pObjNumberMap)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 pClone->Release();
312 return 0;
313 }
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800314 return dwNewObjNum;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315}
316
317FPDF_BOOL ParserPageRangeString(CFX_ByteString rangstring,
Tom Sepez11d93552016-02-09 09:55:54 -0800318 std::vector<FX_WORD>* pageArray,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 int nCount) {
320 if (rangstring.GetLength() != 0) {
321 rangstring.Remove(' ');
322 int nLength = rangstring.GetLength();
323 CFX_ByteString cbCompareString("0123456789-,");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800324 for (int i = 0; i < nLength; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 if (cbCompareString.Find(rangstring[i]) == -1)
326 return FALSE;
327 }
328 CFX_ByteString cbMidRange;
329 int nStringFrom = 0;
330 int nStringTo = 0;
331 while (nStringTo < nLength) {
332 nStringTo = rangstring.Find(',', nStringFrom);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800333 if (nStringTo == -1)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 nStringTo = nLength;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 cbMidRange = rangstring.Mid(nStringFrom, nStringTo - nStringFrom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 int nMid = cbMidRange.Find('-');
337 if (nMid == -1) {
338 long lPageNum = atol(cbMidRange);
339 if (lPageNum <= 0 || lPageNum > nCount)
340 return FALSE;
Tom Sepez11d93552016-02-09 09:55:54 -0800341 pageArray->push_back((FX_WORD)lPageNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 } else {
343 int nStartPageNum = atol(cbMidRange.Mid(0, nMid));
344 if (nStartPageNum == 0)
345 return FALSE;
346
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800347 ++nMid;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348 int nEnd = cbMidRange.GetLength() - nMid;
349 if (nEnd == 0)
350 return FALSE;
351
352 int nEndPageNum = atol(cbMidRange.Mid(nMid, nEnd));
353 if (nStartPageNum < 0 || nStartPageNum > nEndPageNum ||
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800354 nEndPageNum > nCount) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800356 }
357 for (int i = nStartPageNum; i <= nEndPageNum; ++i) {
Tom Sepez11d93552016-02-09 09:55:54 -0800358 pageArray->push_back(i);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800359 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 }
361 nStringFrom = nStringTo + 1;
362 }
363 }
364 return TRUE;
365}
366
367DLLEXPORT FPDF_BOOL STDCALL FPDF_ImportPages(FPDF_DOCUMENT dest_doc,
368 FPDF_DOCUMENT src_doc,
369 FPDF_BYTESTRING pagerange,
370 int index) {
Tom Sepez471a1032015-10-15 16:17:18 -0700371 CPDF_Document* pDestDoc = CPDFDocumentFromFPDFDocument(dest_doc);
372 if (!dest_doc)
373 return FALSE;
374
375 CPDF_Document* pSrcDoc = CPDFDocumentFromFPDFDocument(src_doc);
376 if (!pSrcDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800378
Tom Sepez11d93552016-02-09 09:55:54 -0800379 std::vector<FX_WORD> pageArray;
Tom Sepez471a1032015-10-15 16:17:18 -0700380 int nCount = pSrcDoc->GetPageCount();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 if (pagerange) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800382 if (!ParserPageRangeString(pagerange, &pageArray, nCount))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700383 return FALSE;
384 } else {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800385 for (int i = 1; i <= nCount; ++i) {
Tom Sepez11d93552016-02-09 09:55:54 -0800386 pageArray.push_back(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387 }
388 }
389
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 CPDF_PageOrganizer pageOrg;
Tom Sepez471a1032015-10-15 16:17:18 -0700391 pageOrg.PDFDocInit(pDestDoc, pSrcDoc);
392 return pageOrg.ExportPage(pSrcDoc, &pageArray, pDestDoc, index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700393}
394
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395DLLEXPORT FPDF_BOOL STDCALL FPDF_CopyViewerPreferences(FPDF_DOCUMENT dest_doc,
396 FPDF_DOCUMENT src_doc) {
Tom Sepez471a1032015-10-15 16:17:18 -0700397 CPDF_Document* pDstDoc = CPDFDocumentFromFPDFDocument(dest_doc);
398 if (!pDstDoc)
399 return FALSE;
400
401 CPDF_Document* pSrcDoc = CPDFDocumentFromFPDFDocument(src_doc);
402 if (!pSrcDoc)
403 return FALSE;
404
405 CPDF_Dictionary* pSrcDict = pSrcDoc->GetRoot();
Wei Li9b761132016-01-29 15:44:20 -0800406 pSrcDict = pSrcDict->GetDictBy("ViewerPreferences");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700407 if (!pSrcDict)
408 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700409
410 CPDF_Dictionary* pDstDict = pDstDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 if (!pDstDict)
412 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700413
Lei Zhangd983b092015-12-14 16:58:33 -0800414 pDstDict->SetAt("ViewerPreferences", pSrcDict->Clone(TRUE));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415 return TRUE;
416}