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