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