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