John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // 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 Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 7 | #include "public/fpdf_ppo.h" |
| 8 | |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 9 | #include <map> |
Lei Zhang | aa8bf7e | 2015-12-24 19:13:32 -0800 | [diff] [blame] | 10 | #include <memory> |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 11 | #include <vector> |
Lei Zhang | aa8bf7e | 2015-12-24 19:13:32 -0800 | [diff] [blame] | 12 | |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 13 | #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" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 20 | #include "fpdfsdk/fsdk_define.h" |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 21 | #include "third_party/base/ptr_util.h" |
Tom Sepez | 11d9355 | 2016-02-09 09:55:54 -0800 | [diff] [blame] | 22 | #include "third_party/base/stl_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 23 | |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 24 | namespace { |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 25 | |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 26 | CPDF_Object* PageDictGetInheritableTag(CPDF_Dictionary* pDict, |
| 27 | const CFX_ByteString& bsSrcTag) { |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 28 | if (!pDict || bsSrcTag.IsEmpty()) |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 29 | return nullptr; |
| 30 | if (!pDict->KeyExist("Parent") || !pDict->KeyExist("Type")) |
| 31 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 32 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 33 | CPDF_Object* pType = pDict->GetObjectFor("Type")->GetDirect(); |
Dan Sinclair | 710c909 | 2015-10-21 15:46:10 -0400 | [diff] [blame] | 34 | if (!ToName(pType)) |
| 35 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 36 | if (pType->GetString().Compare("Page")) |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 37 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 38 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 39 | CPDF_Dictionary* pp = |
| 40 | ToDictionary(pDict->GetObjectFor("Parent")->GetDirect()); |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 41 | if (!pp) |
| 42 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 43 | |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 44 | if (pDict->KeyExist(bsSrcTag)) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 45 | return pDict->GetObjectFor(bsSrcTag); |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 46 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 47 | while (pp) { |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 48 | if (pp->KeyExist(bsSrcTag)) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 49 | return pp->GetObjectFor(bsSrcTag); |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 50 | if (!pp->KeyExist("Parent")) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 51 | break; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 52 | pp = ToDictionary(pp->GetObjectFor("Parent")->GetDirect()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 53 | } |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 54 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 55 | } |
| 56 | |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 57 | bool CopyInheritable(CPDF_Dictionary* pCurPageDict, |
| 58 | CPDF_Dictionary* pSrcPageDict, |
| 59 | const CFX_ByteString& key) { |
| 60 | if (pCurPageDict->KeyExist(key)) |
| 61 | return true; |
| 62 | |
| 63 | CPDF_Object* pInheritable = PageDictGetInheritableTag(pSrcPageDict, key); |
| 64 | if (!pInheritable) |
| 65 | return false; |
| 66 | |
| 67 | pCurPageDict->SetFor(key, pInheritable->Clone().release()); |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | bool ParserPageRangeString(CFX_ByteString rangstring, |
| 72 | std::vector<uint16_t>* pageArray, |
| 73 | int nCount) { |
| 74 | if (rangstring.IsEmpty()) |
| 75 | return true; |
| 76 | |
| 77 | rangstring.Remove(' '); |
| 78 | int nLength = rangstring.GetLength(); |
| 79 | CFX_ByteString cbCompareString("0123456789-,"); |
| 80 | for (int i = 0; i < nLength; ++i) { |
| 81 | if (cbCompareString.Find(rangstring[i]) == -1) |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | CFX_ByteString cbMidRange; |
| 86 | int nStringFrom = 0; |
| 87 | int nStringTo = 0; |
| 88 | while (nStringTo < nLength) { |
| 89 | nStringTo = rangstring.Find(',', nStringFrom); |
| 90 | if (nStringTo == -1) |
| 91 | nStringTo = nLength; |
| 92 | cbMidRange = rangstring.Mid(nStringFrom, nStringTo - nStringFrom); |
| 93 | int nMid = cbMidRange.Find('-'); |
| 94 | if (nMid == -1) { |
| 95 | long lPageNum = atol(cbMidRange.c_str()); |
| 96 | if (lPageNum <= 0 || lPageNum > nCount) |
| 97 | return false; |
| 98 | pageArray->push_back((uint16_t)lPageNum); |
| 99 | } else { |
| 100 | int nStartPageNum = atol(cbMidRange.Mid(0, nMid).c_str()); |
| 101 | if (nStartPageNum == 0) |
| 102 | return false; |
| 103 | |
| 104 | ++nMid; |
| 105 | int nEnd = cbMidRange.GetLength() - nMid; |
| 106 | if (nEnd == 0) |
| 107 | return false; |
| 108 | |
| 109 | int nEndPageNum = atol(cbMidRange.Mid(nMid, nEnd).c_str()); |
| 110 | if (nStartPageNum < 0 || nStartPageNum > nEndPageNum || |
| 111 | nEndPageNum > nCount) { |
| 112 | return false; |
| 113 | } |
| 114 | for (int i = nStartPageNum; i <= nEndPageNum; ++i) { |
| 115 | pageArray->push_back(i); |
| 116 | } |
| 117 | } |
| 118 | nStringFrom = nStringTo + 1; |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | } // namespace |
| 124 | |
| 125 | class CPDF_PageOrganizer { |
| 126 | public: |
| 127 | CPDF_PageOrganizer(CPDF_Document* pDestPDFDoc, CPDF_Document* pSrcPDFDoc); |
| 128 | ~CPDF_PageOrganizer(); |
| 129 | |
| 130 | bool PDFDocInit(); |
| 131 | bool ExportPage(const std::vector<uint16_t>& pageNums, int nIndex); |
| 132 | |
| 133 | private: |
| 134 | using ObjectNumberMap = std::map<uint32_t, uint32_t>; |
| 135 | |
| 136 | bool UpdateReference(CPDF_Object* pObj, ObjectNumberMap* pObjNumberMap); |
| 137 | uint32_t GetNewObjId(ObjectNumberMap* pObjNumberMap, CPDF_Reference* pRef); |
| 138 | |
| 139 | CPDF_Document* m_pDestPDFDoc; |
| 140 | CPDF_Document* m_pSrcPDFDoc; |
| 141 | }; |
| 142 | |
| 143 | CPDF_PageOrganizer::CPDF_PageOrganizer(CPDF_Document* pDestPDFDoc, |
| 144 | CPDF_Document* pSrcPDFDoc) |
| 145 | : m_pDestPDFDoc(pDestPDFDoc), m_pSrcPDFDoc(pSrcPDFDoc) {} |
| 146 | |
| 147 | CPDF_PageOrganizer::~CPDF_PageOrganizer() {} |
| 148 | |
| 149 | bool CPDF_PageOrganizer::PDFDocInit() { |
| 150 | ASSERT(m_pDestPDFDoc); |
| 151 | ASSERT(m_pSrcPDFDoc); |
| 152 | |
| 153 | CPDF_Dictionary* pNewRoot = m_pDestPDFDoc->GetRoot(); |
| 154 | if (!pNewRoot) |
| 155 | return false; |
| 156 | |
| 157 | CPDF_Dictionary* pDocInfoDict = m_pDestPDFDoc->GetInfo(); |
| 158 | if (!pDocInfoDict) |
| 159 | return false; |
| 160 | |
| 161 | CFX_ByteString producerstr; |
| 162 | producerstr.Format("PDFium"); |
tsepez | 5913a6c | 2016-11-16 17:31:18 -0800 | [diff] [blame^] | 163 | pDocInfoDict->SetFor("Producer", |
| 164 | new CPDF_String(nullptr, producerstr, false)); |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 165 | |
| 166 | CFX_ByteString cbRootType = pNewRoot->GetStringFor("Type", ""); |
| 167 | if (cbRootType.IsEmpty()) |
tsepez | 5913a6c | 2016-11-16 17:31:18 -0800 | [diff] [blame^] | 168 | pNewRoot->SetFor("Type", new CPDF_Name(nullptr, "Catalog")); |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 169 | |
| 170 | CPDF_Object* pElement = pNewRoot->GetObjectFor("Pages"); |
| 171 | CPDF_Dictionary* pNewPages = |
| 172 | pElement ? ToDictionary(pElement->GetDirect()) : nullptr; |
| 173 | if (!pNewPages) { |
tsepez | 5913a6c | 2016-11-16 17:31:18 -0800 | [diff] [blame^] | 174 | pNewPages = m_pDestPDFDoc->NewIndirect<CPDF_Dictionary>(); |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 175 | pNewRoot->SetReferenceFor("Pages", m_pDestPDFDoc, pNewPages); |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | CFX_ByteString cbPageType = pNewPages->GetStringFor("Type", ""); |
| 179 | if (cbPageType.IsEmpty()) |
tsepez | 5913a6c | 2016-11-16 17:31:18 -0800 | [diff] [blame^] | 180 | pNewPages->SetFor("Type", new CPDF_Name(nullptr, "Pages")); |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 181 | |
| 182 | if (!pNewPages->GetArrayFor("Kids")) { |
| 183 | pNewPages->SetIntegerFor("Count", 0); |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 184 | pNewPages->SetReferenceFor("Kids", m_pDestPDFDoc, |
| 185 | m_pDestPDFDoc->NewIndirect<CPDF_Array>()); |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | bool CPDF_PageOrganizer::ExportPage(const std::vector<uint16_t>& pageNums, |
| 192 | int nIndex) { |
| 193 | int curpage = nIndex; |
| 194 | auto pObjNumberMap = pdfium::MakeUnique<ObjectNumberMap>(); |
| 195 | int nSize = pdfium::CollectionSize<int>(pageNums); |
| 196 | for (int i = 0; i < nSize; ++i) { |
| 197 | CPDF_Dictionary* pCurPageDict = m_pDestPDFDoc->CreateNewPage(curpage); |
| 198 | CPDF_Dictionary* pSrcPageDict = m_pSrcPDFDoc->GetPage(pageNums[i] - 1); |
| 199 | if (!pSrcPageDict || !pCurPageDict) |
| 200 | return false; |
| 201 | |
| 202 | // Clone the page dictionary |
| 203 | for (const auto& it : *pSrcPageDict) { |
| 204 | const CFX_ByteString& cbSrcKeyStr = it.first; |
| 205 | CPDF_Object* pObj = it.second; |
| 206 | if (cbSrcKeyStr == "Type" || cbSrcKeyStr == "Parent") |
| 207 | continue; |
| 208 | |
| 209 | pCurPageDict->SetFor(cbSrcKeyStr, pObj->Clone().release()); |
| 210 | } |
| 211 | |
| 212 | // inheritable item |
| 213 | // 1 MediaBox - required |
| 214 | if (!CopyInheritable(pCurPageDict, pSrcPageDict, "MediaBox")) { |
| 215 | // Search for "CropBox" in the source page dictionary, |
| 216 | // if it does not exists, use the default letter size. |
| 217 | CPDF_Object* pInheritable = |
| 218 | PageDictGetInheritableTag(pSrcPageDict, "CropBox"); |
| 219 | if (pInheritable) { |
| 220 | pCurPageDict->SetFor("MediaBox", pInheritable->Clone().release()); |
| 221 | } else { |
| 222 | // Make the default size to be letter size (8.5'x11') |
| 223 | CPDF_Array* pArray = new CPDF_Array; |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 224 | pArray->AddNew<CPDF_Number>(0); |
| 225 | pArray->AddNew<CPDF_Number>(0); |
| 226 | pArray->AddNew<CPDF_Number>(612); |
| 227 | pArray->AddNew<CPDF_Number>(792); |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 228 | pCurPageDict->SetFor("MediaBox", pArray); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // 2 Resources - required |
| 233 | if (!CopyInheritable(pCurPageDict, pSrcPageDict, "Resources")) |
| 234 | return false; |
| 235 | |
| 236 | // 3 CropBox - optional |
| 237 | CopyInheritable(pCurPageDict, pSrcPageDict, "CropBox"); |
| 238 | // 4 Rotate - optional |
| 239 | CopyInheritable(pCurPageDict, pSrcPageDict, "Rotate"); |
| 240 | |
| 241 | // Update the reference |
| 242 | uint32_t dwOldPageObj = pSrcPageDict->GetObjNum(); |
| 243 | uint32_t dwNewPageObj = pCurPageDict->GetObjNum(); |
| 244 | (*pObjNumberMap)[dwOldPageObj] = dwNewPageObj; |
| 245 | UpdateReference(pCurPageDict, pObjNumberMap.get()); |
| 246 | ++curpage; |
| 247 | } |
| 248 | |
| 249 | return true; |
| 250 | } |
| 251 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 252 | bool CPDF_PageOrganizer::UpdateReference(CPDF_Object* pObj, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 253 | ObjectNumberMap* pObjNumberMap) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 254 | switch (pObj->GetType()) { |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 255 | case CPDF_Object::REFERENCE: { |
Dan Sinclair | bf81c14 | 2015-10-26 16:54:39 -0400 | [diff] [blame] | 256 | CPDF_Reference* pReference = pObj->AsReference(); |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 257 | uint32_t newobjnum = GetNewObjId(pObjNumberMap, pReference); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 258 | if (newobjnum == 0) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 259 | return false; |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 260 | pReference->SetRef(m_pDestPDFDoc, newobjnum); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 261 | break; |
| 262 | } |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 263 | case CPDF_Object::DICTIONARY: { |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 264 | CPDF_Dictionary* pDict = pObj->AsDictionary(); |
Oliver Chang | bd292ae | 2016-01-13 18:46:09 -0800 | [diff] [blame] | 265 | auto it = pDict->begin(); |
| 266 | while (it != pDict->end()) { |
| 267 | const CFX_ByteString& key = it->first; |
tsepez | d5bd8a1 | 2016-10-17 11:13:54 -0700 | [diff] [blame] | 268 | CPDF_Object* pNextObj = it->second; |
Oliver Chang | bd292ae | 2016-01-13 18:46:09 -0800 | [diff] [blame] | 269 | ++it; |
tsepez | f86ca38 | 2016-09-13 12:23:30 -0700 | [diff] [blame] | 270 | if (key == "Parent" || key == "Prev" || key == "First") |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 271 | continue; |
tsepez | f86ca38 | 2016-09-13 12:23:30 -0700 | [diff] [blame] | 272 | if (!pNextObj) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 273 | return false; |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 274 | if (!UpdateReference(pNextObj, pObjNumberMap)) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 275 | pDict->RemoveFor(key); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 276 | } |
| 277 | break; |
| 278 | } |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 279 | case CPDF_Object::ARRAY: { |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 280 | CPDF_Array* pArray = pObj->AsArray(); |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 281 | for (size_t i = 0; i < pArray->GetCount(); ++i) { |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 282 | CPDF_Object* pNextObj = pArray->GetObjectAt(i); |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 283 | if (!pNextObj) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 284 | return false; |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 285 | if (!UpdateReference(pNextObj, pObjNumberMap)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 286 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 287 | } |
| 288 | break; |
| 289 | } |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 290 | case CPDF_Object::STREAM: { |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 291 | CPDF_Stream* pStream = pObj->AsStream(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 292 | CPDF_Dictionary* pDict = pStream->GetDict(); |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 293 | if (!pDict) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 294 | return false; |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 295 | if (!UpdateReference(pDict, pObjNumberMap)) |
| 296 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 297 | break; |
| 298 | } |
| 299 | default: |
| 300 | break; |
| 301 | } |
| 302 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 303 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 304 | } |
| 305 | |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 306 | uint32_t CPDF_PageOrganizer::GetNewObjId(ObjectNumberMap* pObjNumberMap, |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 307 | CPDF_Reference* pRef) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 308 | if (!pRef) |
| 309 | return 0; |
| 310 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 311 | uint32_t dwObjnum = pRef->GetRefObjNum(); |
| 312 | uint32_t dwNewObjNum = 0; |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 313 | const auto it = pObjNumberMap->find(dwObjnum); |
| 314 | if (it != pObjNumberMap->end()) |
| 315 | dwNewObjNum = it->second; |
| 316 | if (dwNewObjNum) |
| 317 | return dwNewObjNum; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 318 | |
| 319 | CPDF_Object* pDirect = pRef->GetDirect(); |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 320 | if (!pDirect) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 321 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 322 | |
tsepez | 335cf09 | 2016-11-09 13:28:26 -0800 | [diff] [blame] | 323 | std::unique_ptr<CPDF_Object> pClone = pDirect->Clone(); |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 324 | if (CPDF_Dictionary* pDictClone = pClone->AsDictionary()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 325 | if (pDictClone->KeyExist("Type")) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 326 | CFX_ByteString strType = pDictClone->GetStringFor("Type"); |
tsepez | 335cf09 | 2016-11-09 13:28:26 -0800 | [diff] [blame] | 327 | if (!FXSYS_stricmp(strType.c_str(), "Pages")) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 328 | return 4; |
tsepez | 335cf09 | 2016-11-09 13:28:26 -0800 | [diff] [blame] | 329 | if (!FXSYS_stricmp(strType.c_str(), "Page")) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 330 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 331 | } |
| 332 | } |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 333 | CPDF_Object* pUnownedClone = |
| 334 | m_pDestPDFDoc->AddIndirectObject(std::move(pClone)); |
| 335 | dwNewObjNum = pUnownedClone->GetObjNum(); |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 336 | (*pObjNumberMap)[dwObjnum] = dwNewObjNum; |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 337 | if (!UpdateReference(pUnownedClone, pObjNumberMap)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 338 | return 0; |
tsepez | 335cf09 | 2016-11-09 13:28:26 -0800 | [diff] [blame] | 339 | |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 340 | return dwNewObjNum; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 343 | DLLEXPORT FPDF_BOOL STDCALL FPDF_ImportPages(FPDF_DOCUMENT dest_doc, |
| 344 | FPDF_DOCUMENT src_doc, |
| 345 | FPDF_BYTESTRING pagerange, |
| 346 | int index) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 347 | CPDF_Document* pDestDoc = CPDFDocumentFromFPDFDocument(dest_doc); |
| 348 | if (!dest_doc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 349 | return false; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 350 | |
| 351 | CPDF_Document* pSrcDoc = CPDFDocumentFromFPDFDocument(src_doc); |
| 352 | if (!pSrcDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 353 | return false; |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 354 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 355 | std::vector<uint16_t> pageArray; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 356 | int nCount = pSrcDoc->GetPageCount(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 357 | if (pagerange) { |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 358 | if (!ParserPageRangeString(pagerange, &pageArray, nCount)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 359 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 360 | } else { |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 361 | for (int i = 1; i <= nCount; ++i) { |
Tom Sepez | 11d9355 | 2016-02-09 09:55:54 -0800 | [diff] [blame] | 362 | pageArray.push_back(i); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
thestig | 88d87c1 | 2016-11-14 14:06:20 -0800 | [diff] [blame] | 366 | CPDF_PageOrganizer pageOrg(pDestDoc, pSrcDoc); |
| 367 | return pageOrg.PDFDocInit() && pageOrg.ExportPage(pageArray, index); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 370 | DLLEXPORT FPDF_BOOL STDCALL FPDF_CopyViewerPreferences(FPDF_DOCUMENT dest_doc, |
| 371 | FPDF_DOCUMENT src_doc) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 372 | CPDF_Document* pDstDoc = CPDFDocumentFromFPDFDocument(dest_doc); |
| 373 | if (!pDstDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 374 | return false; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 375 | |
| 376 | CPDF_Document* pSrcDoc = CPDFDocumentFromFPDFDocument(src_doc); |
| 377 | if (!pSrcDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 378 | return false; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 379 | |
| 380 | CPDF_Dictionary* pSrcDict = pSrcDoc->GetRoot(); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 381 | pSrcDict = pSrcDict->GetDictFor("ViewerPreferences"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 382 | if (!pSrcDict) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 383 | return false; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 384 | |
| 385 | CPDF_Dictionary* pDstDict = pDstDoc->GetRoot(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 386 | if (!pDstDict) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 387 | return false; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 388 | |
tsepez | 335cf09 | 2016-11-09 13:28:26 -0800 | [diff] [blame] | 389 | pDstDict->SetFor("ViewerPreferences", |
| 390 | pSrcDict->CloneDirectObject().release()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 391 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 392 | } |