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 | |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 9 | #include "../include/fpdfxfa/fpdfxfa_doc.h" |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 10 | #include "fpdfsdk/include/fsdk_define.h" |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 11 | #include "third_party/base/nonstd_unique_ptr.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 | |
| 47 | // Set the document information//////////////////////////////////////////// |
| 48 | |
| 49 | CPDF_Dictionary* DInfoDict = pDestPDFDoc->GetInfo(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 50 | if (!DInfoDict) |
| 51 | return FALSE; |
| 52 | |
| 53 | CFX_ByteString producerstr; |
| 54 | producerstr.Format("PDFium"); |
| 55 | DInfoDict->SetAt("Producer", new CPDF_String(producerstr)); |
| 56 | |
| 57 | // Set type//////////////////////////////////////////////////////////////// |
| 58 | CFX_ByteString cbRootType = pNewRoot->GetString("Type", ""); |
| 59 | if (cbRootType.Equal("")) { |
| 60 | pNewRoot->SetAt("Type", new CPDF_Name("Catalog")); |
| 61 | } |
| 62 | |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 63 | CPDF_Object* pElement = pNewRoot->GetElement("Pages"); |
| 64 | CPDF_Dictionary* pNewPages = |
| 65 | pElement ? ToDictionary(pElement->GetDirect()) : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 66 | if (!pNewPages) { |
| 67 | pNewPages = new CPDF_Dictionary; |
| 68 | FX_DWORD NewPagesON = pDestPDFDoc->AddIndirectObject(pNewPages); |
| 69 | pNewRoot->SetAt("Pages", new CPDF_Reference(pDestPDFDoc, NewPagesON)); |
| 70 | } |
| 71 | |
| 72 | CFX_ByteString cbPageType = pNewPages->GetString("Type", ""); |
| 73 | if (cbPageType.Equal("")) { |
| 74 | pNewPages->SetAt("Type", new CPDF_Name("Pages")); |
| 75 | } |
| 76 | |
| 77 | CPDF_Array* pKeysArray = pNewPages->GetArray("Kids"); |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 78 | if (!pKeysArray) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 79 | CPDF_Array* pNewKids = new CPDF_Array; |
| 80 | FX_DWORD Kidsobjnum = -1; |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 81 | Kidsobjnum = pDestPDFDoc->AddIndirectObject(pNewKids); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 82 | |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 83 | pNewPages->SetAt("Kids", new CPDF_Reference(pDestPDFDoc, Kidsobjnum)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 84 | pNewPages->SetAt("Count", new CPDF_Number(0)); |
| 85 | } |
| 86 | |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 87 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 90 | FX_BOOL CPDF_PageOrganizer::ExportPage(CPDF_Document* pSrcPDFDoc, |
| 91 | CFX_WordArray* nPageNum, |
| 92 | CPDF_Document* pDestPDFDoc, |
| 93 | int nIndex) { |
| 94 | int curpage = nIndex; |
| 95 | |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 96 | nonstd::unique_ptr<ObjectNumberMap> pObjNumberMap(new ObjectNumberMap); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 97 | |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 98 | for (int i = 0; i < nPageNum->GetSize(); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 99 | CPDF_Dictionary* pCurPageDict = pDestPDFDoc->CreateNewPage(curpage); |
| 100 | CPDF_Dictionary* pSrcPageDict = pSrcPDFDoc->GetPage(nPageNum->GetAt(i) - 1); |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 101 | if (!pSrcPageDict || !pCurPageDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 102 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 103 | |
| 104 | // Clone the page dictionary/////////// |
| 105 | FX_POSITION SrcPos = pSrcPageDict->GetStartPos(); |
| 106 | while (SrcPos) { |
| 107 | CFX_ByteString cbSrcKeyStr; |
| 108 | CPDF_Object* pObj = pSrcPageDict->GetNextElement(SrcPos, cbSrcKeyStr); |
| 109 | if (cbSrcKeyStr.Compare(("Type")) && cbSrcKeyStr.Compare(("Parent"))) { |
| 110 | if (pCurPageDict->KeyExist(cbSrcKeyStr)) |
| 111 | pCurPageDict->RemoveAt(cbSrcKeyStr); |
| 112 | pCurPageDict->SetAt(cbSrcKeyStr, pObj->Clone()); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // inheritable item/////////////////////// |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 117 | CPDF_Object* pInheritable = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 118 | // 1 MediaBox //required |
| 119 | if (!pCurPageDict->KeyExist("MediaBox")) { |
| 120 | pInheritable = PageDictGetInheritableTag(pSrcPageDict, "MediaBox"); |
| 121 | if (!pInheritable) { |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 122 | // Search the "CropBox" from source page dictionary, |
| 123 | // if not exists,we take the letter size. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 124 | pInheritable = PageDictGetInheritableTag(pSrcPageDict, "CropBox"); |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 125 | if (pInheritable) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 126 | pCurPageDict->SetAt("MediaBox", pInheritable->Clone()); |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 127 | } else { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 128 | // Make the default size to be letter size (8.5'x11') |
| 129 | CPDF_Array* pArray = new CPDF_Array; |
| 130 | pArray->AddNumber(0); |
| 131 | pArray->AddNumber(0); |
| 132 | pArray->AddNumber(612); |
| 133 | pArray->AddNumber(792); |
| 134 | pCurPageDict->SetAt("MediaBox", pArray); |
| 135 | } |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 136 | } else { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 137 | pCurPageDict->SetAt("MediaBox", pInheritable->Clone()); |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 138 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 139 | } |
| 140 | // 2 Resources //required |
| 141 | if (!pCurPageDict->KeyExist("Resources")) { |
| 142 | pInheritable = PageDictGetInheritableTag(pSrcPageDict, "Resources"); |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame^] | 143 | if (!pInheritable) |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 144 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 145 | pCurPageDict->SetAt("Resources", pInheritable->Clone()); |
| 146 | } |
| 147 | // 3 CropBox //Optional |
| 148 | if (!pCurPageDict->KeyExist("CropBox")) { |
| 149 | pInheritable = PageDictGetInheritableTag(pSrcPageDict, "CropBox"); |
| 150 | if (pInheritable) |
| 151 | pCurPageDict->SetAt("CropBox", pInheritable->Clone()); |
| 152 | } |
| 153 | // 4 Rotate //Optional |
| 154 | if (!pCurPageDict->KeyExist("Rotate")) { |
| 155 | pInheritable = PageDictGetInheritableTag(pSrcPageDict, "Rotate"); |
| 156 | if (pInheritable) |
| 157 | pCurPageDict->SetAt("Rotate", pInheritable->Clone()); |
| 158 | } |
| 159 | |
| 160 | ///////////////////////////////////////////// |
| 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()) { |
| 209 | case PDFOBJ_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 | } |
| 217 | case PDFOBJ_DICTIONARY: { |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 218 | CPDF_Dictionary* pDict = pObj->AsDictionary(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 219 | |
| 220 | FX_POSITION pos = pDict->GetStartPos(); |
| 221 | while (pos) { |
| 222 | CFX_ByteString key(""); |
| 223 | CPDF_Object* pNextObj = pDict->GetNextElement(pos, key); |
| 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 | } |
| 237 | case PDFOBJ_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 | } |
| 249 | case PDFOBJ_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")) { |
| 291 | CFX_ByteString strType = pDictClone->GetString("Type"); |
| 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, |
| 312 | CFX_WordArray* pageArray, |
| 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; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 335 | pageArray->Add((FX_WORD)lPageNum); |
| 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) { |
| 352 | pageArray->Add(i); |
| 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 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 373 | CFX_WordArray 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) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 380 | pageArray.Add(i); |
| 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(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 400 | pSrcDict = pSrcDict->GetDict(FX_BSTRC("ViewerPreferences")); |
| 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 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 408 | pDstDict->SetAt(FX_BSTRC("ViewerPreferences"), pSrcDict->Clone(TRUE)); |
| 409 | return TRUE; |
| 410 | } |