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