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