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