| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "core/fpdfapi/parser/cpdf_document.h" | 7 #include "core/fpdfapi/parser/cpdf_document.h" |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 int size = end - start + 1; | 233 int size = end - start + 1; |
| 234 int* widths = FX_Alloc(int, size); | 234 int* widths = FX_Alloc(int, size); |
| 235 int i; | 235 int i; |
| 236 for (i = 0; i < size; i++) { | 236 for (i = 0; i < size; i++) { |
| 237 int glyph_index = pEncoding->GlyphFromCharCode(start + i); | 237 int glyph_index = pEncoding->GlyphFromCharCode(start + i); |
| 238 widths[i] = pFont->GetGlyphWidth(glyph_index); | 238 widths[i] = pFont->GetGlyphWidth(glyph_index); |
| 239 } | 239 } |
| 240 InsertWidthArrayImpl(widths, size, pWidthArray); | 240 InsertWidthArrayImpl(widths, size, pWidthArray); |
| 241 } | 241 } |
| 242 | 242 |
| 243 int InsertDeletePDFPage(CPDF_Document* pDoc, | |
| 244 CPDF_Dictionary* pPages, | |
| 245 int nPagesToGo, | |
| 246 CPDF_Dictionary* pPage, | |
| 247 bool bInsert, | |
| 248 std::set<CPDF_Dictionary*>* pVisited) { | |
| 249 CPDF_Array* pKidList = pPages->GetArrayFor("Kids"); | |
| 250 if (!pKidList) | |
| 251 return -1; | |
| 252 | |
| 253 for (size_t i = 0; i < pKidList->GetCount(); i++) { | |
| 254 CPDF_Dictionary* pKid = pKidList->GetDictAt(i); | |
| 255 if (pKid->GetStringFor("Type") == "Page") { | |
| 256 if (nPagesToGo == 0) { | |
| 257 if (bInsert) { | |
| 258 pKidList->InsertAt(i, new CPDF_Reference(pDoc, pPage->GetObjNum())); | |
| 259 pPage->SetReferenceFor("Parent", pDoc, pPages->GetObjNum()); | |
| 260 } else { | |
| 261 pKidList->RemoveAt(i); | |
| 262 } | |
| 263 pPages->SetIntegerFor( | |
| 264 "Count", pPages->GetIntegerFor("Count") + (bInsert ? 1 : -1)); | |
| 265 return 1; | |
| 266 } | |
| 267 nPagesToGo--; | |
| 268 } else { | |
| 269 int nPages = pKid->GetIntegerFor("Count"); | |
| 270 if (nPagesToGo < nPages) { | |
| 271 if (pdfium::ContainsKey(*pVisited, pKid)) | |
| 272 return -1; | |
| 273 | |
| 274 pdfium::ScopedSetInsertion<CPDF_Dictionary*> insertion(pVisited, pKid); | |
| 275 if (InsertDeletePDFPage(pDoc, pKid, nPagesToGo, pPage, bInsert, | |
| 276 pVisited) < 0) { | |
| 277 return -1; | |
| 278 } | |
| 279 pPages->SetIntegerFor( | |
| 280 "Count", pPages->GetIntegerFor("Count") + (bInsert ? 1 : -1)); | |
| 281 return 1; | |
| 282 } | |
| 283 nPagesToGo -= nPages; | |
| 284 } | |
| 285 } | |
| 286 return 0; | |
| 287 } | |
| 288 | |
| 289 int InsertNewPage(CPDF_Document* pDoc, | |
| 290 int iPage, | |
| 291 CPDF_Dictionary* pPageDict, | |
| 292 CFX_ArrayTemplate<uint32_t>& pageList) { | |
| 293 CPDF_Dictionary* pRoot = pDoc->GetRoot(); | |
| 294 CPDF_Dictionary* pPages = pRoot ? pRoot->GetDictFor("Pages") : nullptr; | |
| 295 if (!pPages) | |
| 296 return -1; | |
| 297 | |
| 298 int nPages = pDoc->GetPageCount(); | |
| 299 if (iPage < 0 || iPage > nPages) | |
| 300 return -1; | |
| 301 | |
| 302 if (iPage == nPages) { | |
| 303 CPDF_Array* pPagesList = pPages->GetArrayFor("Kids"); | |
| 304 if (!pPagesList) { | |
| 305 pPagesList = new CPDF_Array; | |
| 306 pPages->SetFor("Kids", pPagesList); | |
| 307 } | |
| 308 pPagesList->Add(new CPDF_Reference(pDoc, pPageDict->GetObjNum())); | |
| 309 pPages->SetIntegerFor("Count", nPages + 1); | |
| 310 pPageDict->SetReferenceFor("Parent", pDoc, pPages->GetObjNum()); | |
| 311 } else { | |
| 312 std::set<CPDF_Dictionary*> stack = {pPages}; | |
| 313 if (InsertDeletePDFPage(pDoc, pPages, iPage, pPageDict, true, &stack) < 0) | |
| 314 return -1; | |
| 315 } | |
| 316 pageList.InsertAt(iPage, pPageDict->GetObjNum()); | |
| 317 return iPage; | |
| 318 } | |
| 319 | |
| 320 int CountPages(CPDF_Dictionary* pPages, | 243 int CountPages(CPDF_Dictionary* pPages, |
| 321 std::set<CPDF_Dictionary*>* visited_pages) { | 244 std::set<CPDF_Dictionary*>* visited_pages) { |
| 322 int count = pPages->GetIntegerFor("Count"); | 245 int count = pPages->GetIntegerFor("Count"); |
| 323 if (count > 0 && count < FPDF_PAGE_MAX_NUM) | 246 if (count > 0 && count < FPDF_PAGE_MAX_NUM) |
| 324 return count; | 247 return count; |
| 325 CPDF_Array* pKidList = pPages->GetArrayFor("Kids"); | 248 CPDF_Array* pKidList = pPages->GetArrayFor("Kids"); |
| 326 if (!pKidList) | 249 if (!pKidList) |
| 327 return 0; | 250 return 0; |
| 328 count = 0; | 251 count = 0; |
| 329 for (size_t i = 0; i < pKidList->GetCount(); i++) { | 252 for (size_t i = 0; i < pKidList->GetCount(); i++) { |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 703 pPages->SetFor("Kids", new CPDF_Array); | 626 pPages->SetFor("Kids", new CPDF_Array); |
| 704 m_pRootDict->SetReferenceFor("Pages", this, AddIndirectObject(pPages)); | 627 m_pRootDict->SetReferenceFor("Pages", this, AddIndirectObject(pPages)); |
| 705 m_pInfoDict = new CPDF_Dictionary(m_pByteStringPool); | 628 m_pInfoDict = new CPDF_Dictionary(m_pByteStringPool); |
| 706 AddIndirectObject(m_pInfoDict); | 629 AddIndirectObject(m_pInfoDict); |
| 707 } | 630 } |
| 708 | 631 |
| 709 CPDF_Dictionary* CPDF_Document::CreateNewPage(int iPage) { | 632 CPDF_Dictionary* CPDF_Document::CreateNewPage(int iPage) { |
| 710 CPDF_Dictionary* pDict = new CPDF_Dictionary(m_pByteStringPool); | 633 CPDF_Dictionary* pDict = new CPDF_Dictionary(m_pByteStringPool); |
| 711 pDict->SetNameFor("Type", "Page"); | 634 pDict->SetNameFor("Type", "Page"); |
| 712 uint32_t dwObjNum = AddIndirectObject(pDict); | 635 uint32_t dwObjNum = AddIndirectObject(pDict); |
| 713 if (InsertNewPage(this, iPage, pDict, m_PageList) < 0) { | 636 if (!InsertNewPage(iPage, pDict)) { |
| 714 ReleaseIndirectObject(dwObjNum); | 637 ReleaseIndirectObject(dwObjNum); |
| 715 return nullptr; | 638 return nullptr; |
| 716 } | 639 } |
| 717 return pDict; | 640 return pDict; |
| 718 } | 641 } |
| 719 | 642 |
| 643 bool CPDF_Document::InsertDeletePDFPage(CPDF_Dictionary* pPages, |
| 644 int nPagesToGo, |
| 645 CPDF_Dictionary* pPageDict, |
| 646 bool bInsert, |
| 647 std::set<CPDF_Dictionary*>* pVisited) { |
| 648 CPDF_Array* pKidList = pPages->GetArrayFor("Kids"); |
| 649 if (!pKidList) |
| 650 return false; |
| 651 |
| 652 for (size_t i = 0; i < pKidList->GetCount(); i++) { |
| 653 CPDF_Dictionary* pKid = pKidList->GetDictAt(i); |
| 654 if (pKid->GetStringFor("Type") == "Page") { |
| 655 if (nPagesToGo != 0) { |
| 656 nPagesToGo--; |
| 657 continue; |
| 658 } |
| 659 if (bInsert) { |
| 660 pKidList->InsertAt(i, new CPDF_Reference(this, pPageDict->GetObjNum())); |
| 661 pPageDict->SetReferenceFor("Parent", this, pPages->GetObjNum()); |
| 662 } else { |
| 663 pKidList->RemoveAt(i); |
| 664 } |
| 665 pPages->SetIntegerFor( |
| 666 "Count", pPages->GetIntegerFor("Count") + (bInsert ? 1 : -1)); |
| 667 break; |
| 668 } |
| 669 int nPages = pKid->GetIntegerFor("Count"); |
| 670 if (nPagesToGo >= nPages) { |
| 671 nPagesToGo -= nPages; |
| 672 continue; |
| 673 } |
| 674 if (pdfium::ContainsKey(*pVisited, pKid)) |
| 675 return false; |
| 676 |
| 677 pdfium::ScopedSetInsertion<CPDF_Dictionary*> insertion(pVisited, pKid); |
| 678 if (!InsertDeletePDFPage(pKid, nPagesToGo, pPageDict, bInsert, pVisited)) |
| 679 return false; |
| 680 |
| 681 pPages->SetIntegerFor("Count", |
| 682 pPages->GetIntegerFor("Count") + (bInsert ? 1 : -1)); |
| 683 break; |
| 684 } |
| 685 return true; |
| 686 } |
| 687 |
| 688 bool CPDF_Document::InsertNewPage(int iPage, CPDF_Dictionary* pPageDict) { |
| 689 CPDF_Dictionary* pRoot = GetRoot(); |
| 690 CPDF_Dictionary* pPages = pRoot ? pRoot->GetDictFor("Pages") : nullptr; |
| 691 if (!pPages) |
| 692 return false; |
| 693 |
| 694 int nPages = GetPageCount(); |
| 695 if (iPage < 0 || iPage > nPages) |
| 696 return false; |
| 697 |
| 698 if (iPage == nPages) { |
| 699 CPDF_Array* pPagesList = pPages->GetArrayFor("Kids"); |
| 700 if (!pPagesList) { |
| 701 pPagesList = new CPDF_Array; |
| 702 pPages->SetFor("Kids", pPagesList); |
| 703 } |
| 704 pPagesList->Add(new CPDF_Reference(this, pPageDict->GetObjNum())); |
| 705 pPages->SetIntegerFor("Count", nPages + 1); |
| 706 pPageDict->SetReferenceFor("Parent", this, pPages->GetObjNum()); |
| 707 } else { |
| 708 std::set<CPDF_Dictionary*> stack = {pPages}; |
| 709 if (!InsertDeletePDFPage(pPages, iPage, pPageDict, true, &stack)) |
| 710 return false; |
| 711 } |
| 712 m_PageList.InsertAt(iPage, pPageDict->GetObjNum()); |
| 713 return true; |
| 714 } |
| 715 |
| 720 void CPDF_Document::DeletePage(int iPage) { | 716 void CPDF_Document::DeletePage(int iPage) { |
| 721 CPDF_Dictionary* pPages = GetPagesDict(); | 717 CPDF_Dictionary* pPages = GetPagesDict(); |
| 722 if (!pPages) | 718 if (!pPages) |
| 723 return; | 719 return; |
| 724 | 720 |
| 725 int nPages = pPages->GetIntegerFor("Count"); | 721 int nPages = pPages->GetIntegerFor("Count"); |
| 726 if (iPage < 0 || iPage >= nPages) | 722 if (iPage < 0 || iPage >= nPages) |
| 727 return; | 723 return; |
| 728 | 724 |
| 729 std::set<CPDF_Dictionary*> stack = {pPages}; | 725 std::set<CPDF_Dictionary*> stack = {pPages}; |
| 730 if (InsertDeletePDFPage(this, pPages, iPage, nullptr, false, &stack) < 0) | 726 if (!InsertDeletePDFPage(pPages, iPage, nullptr, false, &stack)) |
| 731 return; | 727 return; |
| 732 | 728 |
| 733 m_PageList.RemoveAt(iPage); | 729 m_PageList.RemoveAt(iPage); |
| 734 } | 730 } |
| 735 | 731 |
| 736 CPDF_Font* CPDF_Document::AddStandardFont(const FX_CHAR* font, | 732 CPDF_Font* CPDF_Document::AddStandardFont(const FX_CHAR* font, |
| 737 CPDF_FontEncoding* pEncoding) { | 733 CPDF_FontEncoding* pEncoding) { |
| 738 CFX_ByteString name(font); | 734 CFX_ByteString name(font); |
| 739 if (PDF_GetStandardFontName(&name) < 0) | 735 if (PDF_GetStandardFontName(&name) < 0) |
| 740 return nullptr; | 736 return nullptr; |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1013 pBBox, pLogFont->lfWeight / 5); | 1009 pBBox, pLogFont->lfWeight / 5); |
| 1014 pFontDesc->SetIntegerFor("CapHeight", capheight); | 1010 pFontDesc->SetIntegerFor("CapHeight", capheight); |
| 1015 pFontDict->SetReferenceFor("FontDescriptor", this, | 1011 pFontDict->SetReferenceFor("FontDescriptor", this, |
| 1016 AddIndirectObject(pFontDesc)); | 1012 AddIndirectObject(pFontDesc)); |
| 1017 hFont = SelectObject(hDC, hFont); | 1013 hFont = SelectObject(hDC, hFont); |
| 1018 DeleteObject(hFont); | 1014 DeleteObject(hFont); |
| 1019 DeleteDC(hDC); | 1015 DeleteDC(hDC); |
| 1020 return LoadFont(pBaseDict); | 1016 return LoadFont(pBaseDict); |
| 1021 } | 1017 } |
| 1022 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 1018 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| OLD | NEW |