blob: 2f3a23c548040273df2f030350ce9f87c110f453 [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
Tom Sepez310438f2016-03-08 13:10:55 -080013#include "core/include/fpdfapi/cpdf_document.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080014#include "fpdfsdk/include/fsdk_define.h"
Tom Sepez11d93552016-02-09 09:55:54 -080015#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016
Nico Weber9d8ec5a2015-08-04 13:00:21 -070017class CPDF_PageOrganizer {
18 public:
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080019 using ObjectNumberMap = std::map<FX_DWORD, FX_DWORD>;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070020 CPDF_PageOrganizer();
21 ~CPDF_PageOrganizer();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070022
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023 FX_BOOL PDFDocInit(CPDF_Document* pDestPDFDoc, CPDF_Document* pSrcPDFDoc);
24 FX_BOOL ExportPage(CPDF_Document* pSrcPDFDoc,
Tom Sepez11d93552016-02-09 09:55:54 -080025 std::vector<FX_WORD>* pPageNums,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 CPDF_Document* pDestPDFDoc,
27 int nIndex);
28 CPDF_Object* PageDictGetInheritableTag(CPDF_Dictionary* pDict,
29 CFX_ByteString nSrctag);
30 FX_BOOL UpdateReference(CPDF_Object* pObj,
31 CPDF_Document* pDoc,
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080032 ObjectNumberMap* pObjNumberMap);
33 FX_DWORD GetNewObjId(CPDF_Document* pDoc,
34 ObjectNumberMap* pObjNumberMap,
35 CPDF_Reference* pRef);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036};
37
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038CPDF_PageOrganizer::CPDF_PageOrganizer() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040CPDF_PageOrganizer::~CPDF_PageOrganizer() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042FX_BOOL CPDF_PageOrganizer::PDFDocInit(CPDF_Document* pDestPDFDoc,
43 CPDF_Document* pSrcPDFDoc) {
44 if (!pDestPDFDoc || !pSrcPDFDoc)
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080045 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 CPDF_Dictionary* pNewRoot = pDestPDFDoc->GetRoot();
48 if (!pNewRoot)
Tom Sepez2f2ffec2015-07-23 14:42:09 -070049 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050
Lei Zhang4880d1a2015-12-18 17:05:11 -080051 // Set the document information
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 CPDF_Dictionary* DInfoDict = pDestPDFDoc->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 if (!DInfoDict)
54 return FALSE;
55
56 CFX_ByteString producerstr;
57 producerstr.Format("PDFium");
Lei Zhang4880d1a2015-12-18 17:05:11 -080058 DInfoDict->SetAt("Producer", new CPDF_String(producerstr, FALSE));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059
Lei Zhang4880d1a2015-12-18 17:05:11 -080060 // Set type
Wei Li9b761132016-01-29 15:44:20 -080061 CFX_ByteString cbRootType = pNewRoot->GetStringBy("Type", "");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 if (cbRootType.Equal("")) {
63 pNewRoot->SetAt("Type", new CPDF_Name("Catalog"));
64 }
65
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080066 CPDF_Object* pElement = pNewRoot->GetElement("Pages");
67 CPDF_Dictionary* pNewPages =
68 pElement ? ToDictionary(pElement->GetDirect()) : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 if (!pNewPages) {
70 pNewPages = new CPDF_Dictionary;
71 FX_DWORD NewPagesON = pDestPDFDoc->AddIndirectObject(pNewPages);
72 pNewRoot->SetAt("Pages", new CPDF_Reference(pDestPDFDoc, NewPagesON));
73 }
74
Wei Li9b761132016-01-29 15:44:20 -080075 CFX_ByteString cbPageType = pNewPages->GetStringBy("Type", "");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 if (cbPageType.Equal("")) {
77 pNewPages->SetAt("Type", new CPDF_Name("Pages"));
78 }
79
Wei Li9b761132016-01-29 15:44:20 -080080 CPDF_Array* pKeysArray = pNewPages->GetArrayBy("Kids");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080081 if (!pKeysArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 CPDF_Array* pNewKids = new CPDF_Array;
83 FX_DWORD Kidsobjnum = -1;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080084 Kidsobjnum = pDestPDFDoc->AddIndirectObject(pNewKids);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080086 pNewPages->SetAt("Kids", new CPDF_Reference(pDestPDFDoc, Kidsobjnum));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 pNewPages->SetAt("Count", new CPDF_Number(0));
88 }
89
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080090 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091}
92
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093FX_BOOL CPDF_PageOrganizer::ExportPage(CPDF_Document* pSrcPDFDoc,
Tom Sepez11d93552016-02-09 09:55:54 -080094 std::vector<FX_WORD>* pPageNums,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 CPDF_Document* pDestPDFDoc,
96 int nIndex) {
97 int curpage = nIndex;
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080098 std::unique_ptr<ObjectNumberMap> pObjNumberMap(new ObjectNumberMap);
Tom Sepez11d93552016-02-09 09:55:54 -080099 int nSize = pdfium::CollectionSize<int>(*pPageNums);
100 for (int i = 0; i < nSize; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 CPDF_Dictionary* pCurPageDict = pDestPDFDoc->CreateNewPage(curpage);
Tom Sepez11d93552016-02-09 09:55:54 -0800102 CPDF_Dictionary* pSrcPageDict = pSrcPDFDoc->GetPage(pPageNums->at(i) - 1);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800103 if (!pSrcPageDict || !pCurPageDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105
Lei Zhang4880d1a2015-12-18 17:05:11 -0800106 // Clone the page dictionary
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800107 for (const auto& it : *pSrcPageDict) {
108 const CFX_ByteString& cbSrcKeyStr = it.first;
109 CPDF_Object* pObj = it.second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 if (cbSrcKeyStr.Compare(("Type")) && cbSrcKeyStr.Compare(("Parent"))) {
111 if (pCurPageDict->KeyExist(cbSrcKeyStr))
112 pCurPageDict->RemoveAt(cbSrcKeyStr);
113 pCurPageDict->SetAt(cbSrcKeyStr, pObj->Clone());
114 }
115 }
116
Lei Zhang4880d1a2015-12-18 17:05:11 -0800117 // inheritable item
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800118 CPDF_Object* pInheritable = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 // 1 MediaBox //required
120 if (!pCurPageDict->KeyExist("MediaBox")) {
121 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "MediaBox");
122 if (!pInheritable) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800123 // Search the "CropBox" from source page dictionary,
124 // if not exists,we take the letter size.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "CropBox");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800126 if (pInheritable) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 pCurPageDict->SetAt("MediaBox", pInheritable->Clone());
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800128 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 // Make the default size to be letter size (8.5'x11')
130 CPDF_Array* pArray = new CPDF_Array;
131 pArray->AddNumber(0);
132 pArray->AddNumber(0);
133 pArray->AddNumber(612);
134 pArray->AddNumber(792);
135 pCurPageDict->SetAt("MediaBox", pArray);
136 }
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800137 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 pCurPageDict->SetAt("MediaBox", pInheritable->Clone());
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800139 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 }
141 // 2 Resources //required
142 if (!pCurPageDict->KeyExist("Resources")) {
143 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "Resources");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800144 if (!pInheritable)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700145 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 pCurPageDict->SetAt("Resources", pInheritable->Clone());
147 }
148 // 3 CropBox //Optional
149 if (!pCurPageDict->KeyExist("CropBox")) {
150 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "CropBox");
151 if (pInheritable)
152 pCurPageDict->SetAt("CropBox", pInheritable->Clone());
153 }
154 // 4 Rotate //Optional
155 if (!pCurPageDict->KeyExist("Rotate")) {
156 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "Rotate");
157 if (pInheritable)
158 pCurPageDict->SetAt("Rotate", pInheritable->Clone());
159 }
160
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 // Update the reference
162 FX_DWORD dwOldPageObj = pSrcPageDict->GetObjNum();
163 FX_DWORD dwNewPageObj = pCurPageDict->GetObjNum();
164
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800165 (*pObjNumberMap)[dwOldPageObj] = dwNewPageObj;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800167 UpdateReference(pCurPageDict, pDestPDFDoc, pObjNumberMap.get());
168 ++curpage;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 }
170
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 return TRUE;
172}
173
174CPDF_Object* CPDF_PageOrganizer::PageDictGetInheritableTag(
175 CPDF_Dictionary* pDict,
176 CFX_ByteString nSrctag) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800177 if (!pDict || nSrctag.IsEmpty())
178 return nullptr;
179 if (!pDict->KeyExist("Parent") || !pDict->KeyExist("Type"))
180 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181
182 CPDF_Object* pType = pDict->GetElement("Type")->GetDirect();
Dan Sinclair710c9092015-10-21 15:46:10 -0400183 if (!ToName(pType))
184 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 if (pType->GetString().Compare("Page"))
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800186 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187
Dan Sinclairf1251c12015-10-20 16:24:45 -0400188 CPDF_Dictionary* pp = ToDictionary(pDict->GetElement("Parent")->GetDirect());
189 if (!pp)
190 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191
192 if (pDict->KeyExist((const char*)nSrctag))
193 return pDict->GetElement((const char*)nSrctag);
Dan Sinclairf1251c12015-10-20 16:24:45 -0400194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 while (pp) {
196 if (pp->KeyExist((const char*)nSrctag))
197 return pp->GetElement((const char*)nSrctag);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800198 if (!pp->KeyExist("Parent"))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 break;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400200 pp = ToDictionary(pp->GetElement("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();
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800211 FX_DWORD 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;
222 CPDF_Object* pNextObj = it->second;
223 ++it;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 if (!FXSYS_strcmp(key, "Parent") || !FXSYS_strcmp(key, "Prev") ||
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800225 !FXSYS_strcmp(key, "First")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 continue;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800227 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 if (pNextObj) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800229 if (!UpdateReference(pNextObj, pDoc, pObjNumberMap))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 pDict->RemoveAt(key);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800231 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800233 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 }
235 break;
236 }
Tom Sepez8e5cd192016-01-26 13:20:26 -0800237 case CPDF_Object::ARRAY: {
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400238 CPDF_Array* pArray = pObj->AsArray();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239 FX_DWORD count = pArray->GetCount();
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800240 for (FX_DWORD i = 0; i < count; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 CPDF_Object* pNextObj = pArray->GetElement(i);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800242 if (!pNextObj)
243 return FALSE;
244 if (!UpdateReference(pNextObj, pDoc, pObjNumberMap))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 return FALSE;
246 }
247 break;
248 }
Tom Sepez8e5cd192016-01-26 13:20:26 -0800249 case CPDF_Object::STREAM: {
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400250 CPDF_Stream* pStream = pObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 CPDF_Dictionary* pDict = pStream->GetDict();
252 if (pDict) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800253 if (!UpdateReference(pDict, pDoc, pObjNumberMap))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800255 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800257 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 break;
259 }
260 default:
261 break;
262 }
263
264 return TRUE;
265}
266
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800267FX_DWORD CPDF_PageOrganizer::GetNewObjId(CPDF_Document* pDoc,
268 ObjectNumberMap* pObjNumberMap,
269 CPDF_Reference* pRef) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 if (!pRef)
271 return 0;
272
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800273 FX_DWORD dwObjnum = pRef->GetRefObjNum();
274 FX_DWORD dwNewObjNum = 0;
275 const auto it = pObjNumberMap->find(dwObjnum);
276 if (it != pObjNumberMap->end())
277 dwNewObjNum = it->second;
278 if (dwNewObjNum)
279 return dwNewObjNum;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280
281 CPDF_Object* pDirect = pRef->GetDirect();
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800282 if (!pDirect)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284
285 CPDF_Object* pClone = pDirect->Clone();
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800286 if (!pClone)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288
Dan Sinclairf1251c12015-10-20 16:24:45 -0400289 if (CPDF_Dictionary* pDictClone = pClone->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 if (pDictClone->KeyExist("Type")) {
Wei Li9b761132016-01-29 15:44:20 -0800291 CFX_ByteString strType = pDictClone->GetStringBy("Type");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 if (!FXSYS_stricmp(strType, "Pages")) {
293 pDictClone->Release();
294 return 4;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800295 }
296 if (!FXSYS_stricmp(strType, "Page")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297 pDictClone->Release();
298 return 0;
299 }
300 }
301 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302 dwNewObjNum = pDoc->AddIndirectObject(pClone);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800303 (*pObjNumberMap)[dwObjnum] = dwNewObjNum;
304 if (!UpdateReference(pClone, pDoc, pObjNumberMap)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 pClone->Release();
306 return 0;
307 }
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800308 return dwNewObjNum;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309}
310
311FPDF_BOOL ParserPageRangeString(CFX_ByteString rangstring,
Tom Sepez11d93552016-02-09 09:55:54 -0800312 std::vector<FX_WORD>* pageArray,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 int nCount) {
314 if (rangstring.GetLength() != 0) {
315 rangstring.Remove(' ');
316 int nLength = rangstring.GetLength();
317 CFX_ByteString cbCompareString("0123456789-,");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800318 for (int i = 0; i < nLength; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 if (cbCompareString.Find(rangstring[i]) == -1)
320 return FALSE;
321 }
322 CFX_ByteString cbMidRange;
323 int nStringFrom = 0;
324 int nStringTo = 0;
325 while (nStringTo < nLength) {
326 nStringTo = rangstring.Find(',', nStringFrom);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800327 if (nStringTo == -1)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 nStringTo = nLength;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329 cbMidRange = rangstring.Mid(nStringFrom, nStringTo - nStringFrom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 int nMid = cbMidRange.Find('-');
331 if (nMid == -1) {
332 long lPageNum = atol(cbMidRange);
333 if (lPageNum <= 0 || lPageNum > nCount)
334 return FALSE;
Tom Sepez11d93552016-02-09 09:55:54 -0800335 pageArray->push_back((FX_WORD)lPageNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 } else {
337 int nStartPageNum = atol(cbMidRange.Mid(0, nMid));
338 if (nStartPageNum == 0)
339 return FALSE;
340
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800341 ++nMid;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 int nEnd = cbMidRange.GetLength() - nMid;
343 if (nEnd == 0)
344 return FALSE;
345
346 int nEndPageNum = atol(cbMidRange.Mid(nMid, nEnd));
347 if (nStartPageNum < 0 || nStartPageNum > nEndPageNum ||
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800348 nEndPageNum > nCount) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800350 }
351 for (int i = nStartPageNum; i <= nEndPageNum; ++i) {
Tom Sepez11d93552016-02-09 09:55:54 -0800352 pageArray->push_back(i);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800353 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 }
355 nStringFrom = nStringTo + 1;
356 }
357 }
358 return TRUE;
359}
360
361DLLEXPORT FPDF_BOOL STDCALL FPDF_ImportPages(FPDF_DOCUMENT dest_doc,
362 FPDF_DOCUMENT src_doc,
363 FPDF_BYTESTRING pagerange,
364 int index) {
Tom Sepez471a1032015-10-15 16:17:18 -0700365 CPDF_Document* pDestDoc = CPDFDocumentFromFPDFDocument(dest_doc);
366 if (!dest_doc)
367 return FALSE;
368
369 CPDF_Document* pSrcDoc = CPDFDocumentFromFPDFDocument(src_doc);
370 if (!pSrcDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800372
Tom Sepez11d93552016-02-09 09:55:54 -0800373 std::vector<FX_WORD> pageArray;
Tom Sepez471a1032015-10-15 16:17:18 -0700374 int nCount = pSrcDoc->GetPageCount();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 if (pagerange) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800376 if (!ParserPageRangeString(pagerange, &pageArray, nCount))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377 return FALSE;
378 } else {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800379 for (int i = 1; i <= nCount; ++i) {
Tom Sepez11d93552016-02-09 09:55:54 -0800380 pageArray.push_back(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 }
382 }
383
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384 CPDF_PageOrganizer pageOrg;
Tom Sepez471a1032015-10-15 16:17:18 -0700385 pageOrg.PDFDocInit(pDestDoc, pSrcDoc);
386 return pageOrg.ExportPage(pSrcDoc, &pageArray, pDestDoc, index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700387}
388
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389DLLEXPORT FPDF_BOOL STDCALL FPDF_CopyViewerPreferences(FPDF_DOCUMENT dest_doc,
390 FPDF_DOCUMENT src_doc) {
Tom Sepez471a1032015-10-15 16:17:18 -0700391 CPDF_Document* pDstDoc = CPDFDocumentFromFPDFDocument(dest_doc);
392 if (!pDstDoc)
393 return FALSE;
394
395 CPDF_Document* pSrcDoc = CPDFDocumentFromFPDFDocument(src_doc);
396 if (!pSrcDoc)
397 return FALSE;
398
399 CPDF_Dictionary* pSrcDict = pSrcDoc->GetRoot();
Wei Li9b761132016-01-29 15:44:20 -0800400 pSrcDict = pSrcDict->GetDictBy("ViewerPreferences");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401 if (!pSrcDict)
402 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700403
404 CPDF_Dictionary* pDstDict = pDstDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 if (!pDstDict)
406 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700407
Lei Zhangd983b092015-12-14 16:58:33 -0800408 pDstDict->SetAt("ViewerPreferences", pSrcDict->Clone(TRUE));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 return TRUE;
410}