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