blob: 3b3d340a9326dca17e5881d290c97b6ffe894d5f [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
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08009#include <memory>
10
Tom Sepez310438f2016-03-08 13:10:55 -080011#include "core/include/fpdfapi/cpdf_document.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080012#include "fpdfsdk/include/fsdk_define.h"
Tom Sepez11d93552016-02-09 09:55:54 -080013#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014
Nico Weber9d8ec5a2015-08-04 13:00:21 -070015class CPDF_PageOrganizer {
16 public:
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080017 using ObjectNumberMap = std::map<FX_DWORD, FX_DWORD>;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070018 CPDF_PageOrganizer();
19 ~CPDF_PageOrganizer();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070020
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021 FX_BOOL PDFDocInit(CPDF_Document* pDestPDFDoc, CPDF_Document* pSrcPDFDoc);
22 FX_BOOL ExportPage(CPDF_Document* pSrcPDFDoc,
Tom Sepez11d93552016-02-09 09:55:54 -080023 std::vector<FX_WORD>* pPageNums,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 CPDF_Document* pDestPDFDoc,
25 int nIndex);
26 CPDF_Object* PageDictGetInheritableTag(CPDF_Dictionary* pDict,
27 CFX_ByteString nSrctag);
28 FX_BOOL UpdateReference(CPDF_Object* pObj,
29 CPDF_Document* pDoc,
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080030 ObjectNumberMap* pObjNumberMap);
31 FX_DWORD GetNewObjId(CPDF_Document* pDoc,
32 ObjectNumberMap* pObjNumberMap,
33 CPDF_Reference* pRef);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034};
35
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036CPDF_PageOrganizer::CPDF_PageOrganizer() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070037
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 -070040FX_BOOL CPDF_PageOrganizer::PDFDocInit(CPDF_Document* pDestPDFDoc,
41 CPDF_Document* pSrcPDFDoc) {
42 if (!pDestPDFDoc || !pSrcPDFDoc)
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080043 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 CPDF_Dictionary* pNewRoot = pDestPDFDoc->GetRoot();
46 if (!pNewRoot)
Tom Sepez2f2ffec2015-07-23 14:42:09 -070047 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048
Lei Zhang4880d1a2015-12-18 17:05:11 -080049 // Set the document information
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 CPDF_Dictionary* DInfoDict = pDestPDFDoc->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 if (!DInfoDict)
52 return FALSE;
53
54 CFX_ByteString producerstr;
55 producerstr.Format("PDFium");
Lei Zhang4880d1a2015-12-18 17:05:11 -080056 DInfoDict->SetAt("Producer", new CPDF_String(producerstr, FALSE));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057
Lei Zhang4880d1a2015-12-18 17:05:11 -080058 // Set type
Wei Li9b761132016-01-29 15:44:20 -080059 CFX_ByteString cbRootType = pNewRoot->GetStringBy("Type", "");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 if (cbRootType.Equal("")) {
61 pNewRoot->SetAt("Type", new CPDF_Name("Catalog"));
62 }
63
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080064 CPDF_Object* pElement = pNewRoot->GetElement("Pages");
65 CPDF_Dictionary* pNewPages =
66 pElement ? ToDictionary(pElement->GetDirect()) : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 if (!pNewPages) {
68 pNewPages = new CPDF_Dictionary;
69 FX_DWORD NewPagesON = pDestPDFDoc->AddIndirectObject(pNewPages);
70 pNewRoot->SetAt("Pages", new CPDF_Reference(pDestPDFDoc, NewPagesON));
71 }
72
Wei Li9b761132016-01-29 15:44:20 -080073 CFX_ByteString cbPageType = pNewPages->GetStringBy("Type", "");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 if (cbPageType.Equal("")) {
75 pNewPages->SetAt("Type", new CPDF_Name("Pages"));
76 }
77
Wei Li9b761132016-01-29 15:44:20 -080078 CPDF_Array* pKeysArray = pNewPages->GetArrayBy("Kids");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080079 if (!pKeysArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 CPDF_Array* pNewKids = new CPDF_Array;
81 FX_DWORD Kidsobjnum = -1;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080082 Kidsobjnum = pDestPDFDoc->AddIndirectObject(pNewKids);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080084 pNewPages->SetAt("Kids", new CPDF_Reference(pDestPDFDoc, Kidsobjnum));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 pNewPages->SetAt("Count", new CPDF_Number(0));
86 }
87
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080088 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089}
90
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091FX_BOOL CPDF_PageOrganizer::ExportPage(CPDF_Document* pSrcPDFDoc,
Tom Sepez11d93552016-02-09 09:55:54 -080092 std::vector<FX_WORD>* pPageNums,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 CPDF_Document* pDestPDFDoc,
94 int nIndex) {
95 int curpage = nIndex;
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080096 std::unique_ptr<ObjectNumberMap> pObjNumberMap(new ObjectNumberMap);
Tom Sepez11d93552016-02-09 09:55:54 -080097 int nSize = pdfium::CollectionSize<int>(*pPageNums);
98 for (int i = 0; i < nSize; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 CPDF_Dictionary* pCurPageDict = pDestPDFDoc->CreateNewPage(curpage);
Tom Sepez11d93552016-02-09 09:55:54 -0800100 CPDF_Dictionary* pSrcPageDict = pSrcPDFDoc->GetPage(pPageNums->at(i) - 1);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800101 if (!pSrcPageDict || !pCurPageDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103
Lei Zhang4880d1a2015-12-18 17:05:11 -0800104 // Clone the page dictionary
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800105 for (const auto& it : *pSrcPageDict) {
106 const CFX_ByteString& cbSrcKeyStr = it.first;
107 CPDF_Object* pObj = it.second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 if (cbSrcKeyStr.Compare(("Type")) && cbSrcKeyStr.Compare(("Parent"))) {
109 if (pCurPageDict->KeyExist(cbSrcKeyStr))
110 pCurPageDict->RemoveAt(cbSrcKeyStr);
111 pCurPageDict->SetAt(cbSrcKeyStr, pObj->Clone());
112 }
113 }
114
Lei Zhang4880d1a2015-12-18 17:05:11 -0800115 // inheritable item
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800116 CPDF_Object* pInheritable = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117 // 1 MediaBox //required
118 if (!pCurPageDict->KeyExist("MediaBox")) {
119 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "MediaBox");
120 if (!pInheritable) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800121 // Search the "CropBox" from source page dictionary,
122 // if not exists,we take the letter size.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "CropBox");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800124 if (pInheritable) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 pCurPageDict->SetAt("MediaBox", pInheritable->Clone());
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800126 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 // Make the default size to be letter size (8.5'x11')
128 CPDF_Array* pArray = new CPDF_Array;
129 pArray->AddNumber(0);
130 pArray->AddNumber(0);
131 pArray->AddNumber(612);
132 pArray->AddNumber(792);
133 pCurPageDict->SetAt("MediaBox", pArray);
134 }
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800135 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 pCurPageDict->SetAt("MediaBox", pInheritable->Clone());
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800137 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 }
139 // 2 Resources //required
140 if (!pCurPageDict->KeyExist("Resources")) {
141 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "Resources");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800142 if (!pInheritable)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700143 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 pCurPageDict->SetAt("Resources", pInheritable->Clone());
145 }
146 // 3 CropBox //Optional
147 if (!pCurPageDict->KeyExist("CropBox")) {
148 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "CropBox");
149 if (pInheritable)
150 pCurPageDict->SetAt("CropBox", pInheritable->Clone());
151 }
152 // 4 Rotate //Optional
153 if (!pCurPageDict->KeyExist("Rotate")) {
154 pInheritable = PageDictGetInheritableTag(pSrcPageDict, "Rotate");
155 if (pInheritable)
156 pCurPageDict->SetAt("Rotate", pInheritable->Clone());
157 }
158
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 // Update the reference
160 FX_DWORD dwOldPageObj = pSrcPageDict->GetObjNum();
161 FX_DWORD dwNewPageObj = pCurPageDict->GetObjNum();
162
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800163 (*pObjNumberMap)[dwOldPageObj] = dwNewPageObj;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800165 UpdateReference(pCurPageDict, pDestPDFDoc, pObjNumberMap.get());
166 ++curpage;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 }
168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 return TRUE;
170}
171
172CPDF_Object* CPDF_PageOrganizer::PageDictGetInheritableTag(
173 CPDF_Dictionary* pDict,
174 CFX_ByteString nSrctag) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800175 if (!pDict || nSrctag.IsEmpty())
176 return nullptr;
177 if (!pDict->KeyExist("Parent") || !pDict->KeyExist("Type"))
178 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179
180 CPDF_Object* pType = pDict->GetElement("Type")->GetDirect();
Dan Sinclair710c9092015-10-21 15:46:10 -0400181 if (!ToName(pType))
182 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 if (pType->GetString().Compare("Page"))
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800184 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185
Dan Sinclairf1251c12015-10-20 16:24:45 -0400186 CPDF_Dictionary* pp = ToDictionary(pDict->GetElement("Parent")->GetDirect());
187 if (!pp)
188 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189
190 if (pDict->KeyExist((const char*)nSrctag))
191 return pDict->GetElement((const char*)nSrctag);
Dan Sinclairf1251c12015-10-20 16:24:45 -0400192
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 while (pp) {
194 if (pp->KeyExist((const char*)nSrctag))
195 return pp->GetElement((const char*)nSrctag);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800196 if (!pp->KeyExist("Parent"))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 break;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400198 pp = ToDictionary(pp->GetElement("Parent")->GetDirect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 }
Dan Sinclairf1251c12015-10-20 16:24:45 -0400200 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201}
202
203FX_BOOL CPDF_PageOrganizer::UpdateReference(CPDF_Object* pObj,
204 CPDF_Document* pDoc,
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800205 ObjectNumberMap* pObjNumberMap) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 switch (pObj->GetType()) {
Tom Sepez8e5cd192016-01-26 13:20:26 -0800207 case CPDF_Object::REFERENCE: {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400208 CPDF_Reference* pReference = pObj->AsReference();
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800209 FX_DWORD newobjnum = GetNewObjId(pDoc, pObjNumberMap, pReference);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 if (newobjnum == 0)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700211 return FALSE;
Dan Sinclairbf81c142015-10-26 16:54:39 -0400212 pReference->SetRef(pDoc, newobjnum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 break;
214 }
Tom Sepez8e5cd192016-01-26 13:20:26 -0800215 case CPDF_Object::DICTIONARY: {
Dan Sinclairf1251c12015-10-20 16:24:45 -0400216 CPDF_Dictionary* pDict = pObj->AsDictionary();
Oliver Changbd292ae2016-01-13 18:46:09 -0800217 auto it = pDict->begin();
218 while (it != pDict->end()) {
219 const CFX_ByteString& key = it->first;
220 CPDF_Object* pNextObj = it->second;
221 ++it;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 if (!FXSYS_strcmp(key, "Parent") || !FXSYS_strcmp(key, "Prev") ||
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800223 !FXSYS_strcmp(key, "First")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 continue;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800225 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 if (pNextObj) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800227 if (!UpdateReference(pNextObj, pDoc, pObjNumberMap))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 pDict->RemoveAt(key);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800229 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800231 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 }
233 break;
234 }
Tom Sepez8e5cd192016-01-26 13:20:26 -0800235 case CPDF_Object::ARRAY: {
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400236 CPDF_Array* pArray = pObj->AsArray();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 FX_DWORD count = pArray->GetCount();
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800238 for (FX_DWORD i = 0; i < count; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239 CPDF_Object* pNextObj = pArray->GetElement(i);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800240 if (!pNextObj)
241 return FALSE;
242 if (!UpdateReference(pNextObj, pDoc, pObjNumberMap))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 return FALSE;
244 }
245 break;
246 }
Tom Sepez8e5cd192016-01-26 13:20:26 -0800247 case CPDF_Object::STREAM: {
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400248 CPDF_Stream* pStream = pObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 CPDF_Dictionary* pDict = pStream->GetDict();
250 if (pDict) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800251 if (!UpdateReference(pDict, pDoc, pObjNumberMap))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800253 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800255 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 break;
257 }
258 default:
259 break;
260 }
261
262 return TRUE;
263}
264
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800265FX_DWORD CPDF_PageOrganizer::GetNewObjId(CPDF_Document* pDoc,
266 ObjectNumberMap* pObjNumberMap,
267 CPDF_Reference* pRef) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 if (!pRef)
269 return 0;
270
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800271 FX_DWORD dwObjnum = pRef->GetRefObjNum();
272 FX_DWORD dwNewObjNum = 0;
273 const auto it = pObjNumberMap->find(dwObjnum);
274 if (it != pObjNumberMap->end())
275 dwNewObjNum = it->second;
276 if (dwNewObjNum)
277 return dwNewObjNum;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278
279 CPDF_Object* pDirect = pRef->GetDirect();
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800280 if (!pDirect)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282
283 CPDF_Object* pClone = pDirect->Clone();
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800284 if (!pClone)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286
Dan Sinclairf1251c12015-10-20 16:24:45 -0400287 if (CPDF_Dictionary* pDictClone = pClone->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 if (pDictClone->KeyExist("Type")) {
Wei Li9b761132016-01-29 15:44:20 -0800289 CFX_ByteString strType = pDictClone->GetStringBy("Type");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 if (!FXSYS_stricmp(strType, "Pages")) {
291 pDictClone->Release();
292 return 4;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800293 }
294 if (!FXSYS_stricmp(strType, "Page")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 pDictClone->Release();
296 return 0;
297 }
298 }
299 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300 dwNewObjNum = pDoc->AddIndirectObject(pClone);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800301 (*pObjNumberMap)[dwObjnum] = dwNewObjNum;
302 if (!UpdateReference(pClone, pDoc, pObjNumberMap)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 pClone->Release();
304 return 0;
305 }
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800306 return dwNewObjNum;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307}
308
309FPDF_BOOL ParserPageRangeString(CFX_ByteString rangstring,
Tom Sepez11d93552016-02-09 09:55:54 -0800310 std::vector<FX_WORD>* pageArray,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 int nCount) {
312 if (rangstring.GetLength() != 0) {
313 rangstring.Remove(' ');
314 int nLength = rangstring.GetLength();
315 CFX_ByteString cbCompareString("0123456789-,");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800316 for (int i = 0; i < nLength; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 if (cbCompareString.Find(rangstring[i]) == -1)
318 return FALSE;
319 }
320 CFX_ByteString cbMidRange;
321 int nStringFrom = 0;
322 int nStringTo = 0;
323 while (nStringTo < nLength) {
324 nStringTo = rangstring.Find(',', nStringFrom);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800325 if (nStringTo == -1)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 nStringTo = nLength;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327 cbMidRange = rangstring.Mid(nStringFrom, nStringTo - nStringFrom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 int nMid = cbMidRange.Find('-');
329 if (nMid == -1) {
330 long lPageNum = atol(cbMidRange);
331 if (lPageNum <= 0 || lPageNum > nCount)
332 return FALSE;
Tom Sepez11d93552016-02-09 09:55:54 -0800333 pageArray->push_back((FX_WORD)lPageNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 } else {
335 int nStartPageNum = atol(cbMidRange.Mid(0, nMid));
336 if (nStartPageNum == 0)
337 return FALSE;
338
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800339 ++nMid;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 int nEnd = cbMidRange.GetLength() - nMid;
341 if (nEnd == 0)
342 return FALSE;
343
344 int nEndPageNum = atol(cbMidRange.Mid(nMid, nEnd));
345 if (nStartPageNum < 0 || nStartPageNum > nEndPageNum ||
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800346 nEndPageNum > nCount) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800348 }
349 for (int i = nStartPageNum; i <= nEndPageNum; ++i) {
Tom Sepez11d93552016-02-09 09:55:54 -0800350 pageArray->push_back(i);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800351 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352 }
353 nStringFrom = nStringTo + 1;
354 }
355 }
356 return TRUE;
357}
358
359DLLEXPORT FPDF_BOOL STDCALL FPDF_ImportPages(FPDF_DOCUMENT dest_doc,
360 FPDF_DOCUMENT src_doc,
361 FPDF_BYTESTRING pagerange,
362 int index) {
Tom Sepez471a1032015-10-15 16:17:18 -0700363 CPDF_Document* pDestDoc = CPDFDocumentFromFPDFDocument(dest_doc);
364 if (!dest_doc)
365 return FALSE;
366
367 CPDF_Document* pSrcDoc = CPDFDocumentFromFPDFDocument(src_doc);
368 if (!pSrcDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 return FALSE;
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800370
Tom Sepez11d93552016-02-09 09:55:54 -0800371 std::vector<FX_WORD> pageArray;
Tom Sepez471a1032015-10-15 16:17:18 -0700372 int nCount = pSrcDoc->GetPageCount();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373 if (pagerange) {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800374 if (!ParserPageRangeString(pagerange, &pageArray, nCount))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 return FALSE;
376 } else {
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800377 for (int i = 1; i <= nCount; ++i) {
Tom Sepez11d93552016-02-09 09:55:54 -0800378 pageArray.push_back(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 }
380 }
381
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382 CPDF_PageOrganizer pageOrg;
Tom Sepez471a1032015-10-15 16:17:18 -0700383 pageOrg.PDFDocInit(pDestDoc, pSrcDoc);
384 return pageOrg.ExportPage(pSrcDoc, &pageArray, pDestDoc, index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700385}
386
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387DLLEXPORT FPDF_BOOL STDCALL FPDF_CopyViewerPreferences(FPDF_DOCUMENT dest_doc,
388 FPDF_DOCUMENT src_doc) {
Tom Sepez471a1032015-10-15 16:17:18 -0700389 CPDF_Document* pDstDoc = CPDFDocumentFromFPDFDocument(dest_doc);
390 if (!pDstDoc)
391 return FALSE;
392
393 CPDF_Document* pSrcDoc = CPDFDocumentFromFPDFDocument(src_doc);
394 if (!pSrcDoc)
395 return FALSE;
396
397 CPDF_Dictionary* pSrcDict = pSrcDoc->GetRoot();
Wei Li9b761132016-01-29 15:44:20 -0800398 pSrcDict = pSrcDict->GetDictBy("ViewerPreferences");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 if (!pSrcDict)
400 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700401
402 CPDF_Dictionary* pDstDict = pDstDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 if (!pDstDict)
404 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700405
Lei Zhangd983b092015-12-14 16:58:33 -0800406 pDstDict->SetAt("ViewerPreferences", pSrcDict->Clone(TRUE));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700407 return TRUE;
408}