| 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 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 if (!m_pRootDict) | 374 if (!m_pRootDict) |
| 375 return; | 375 return; |
| 376 | 376 |
| 377 CPDF_Object* pInfoObj = GetOrParseIndirectObject(m_pParser->GetInfoObjNum()); | 377 CPDF_Object* pInfoObj = GetOrParseIndirectObject(m_pParser->GetInfoObjNum()); |
| 378 if (pInfoObj) | 378 if (pInfoObj) |
| 379 m_pInfoDict = pInfoObj->GetDict(); | 379 m_pInfoDict = pInfoObj->GetDict(); |
| 380 } | 380 } |
| 381 | 381 |
| 382 void CPDF_Document::LoadDoc() { | 382 void CPDF_Document::LoadDoc() { |
| 383 LoadDocInternal(); | 383 LoadDocInternal(); |
| 384 m_PageList.SetSize(RetrievePageCount()); | 384 LoadPages(); |
| 385 } | 385 } |
| 386 | 386 |
| 387 void CPDF_Document::LoadLinearizedDoc( | 387 void CPDF_Document::LoadLinearizedDoc( |
| 388 const CPDF_LinearizedHeader* pLinearizationParams) { | 388 const CPDF_LinearizedHeader* pLinearizationParams) { |
| 389 m_bLinearized = true; | 389 m_bLinearized = true; |
| 390 LoadDocInternal(); | 390 LoadDocInternal(); |
| 391 m_PageList.SetSize(pLinearizationParams->GetPageCount()); | 391 m_PageList.resize(pLinearizationParams->GetPageCount()); |
| 392 m_iFirstPageNo = pLinearizationParams->GetFirstPageNo(); | 392 m_iFirstPageNo = pLinearizationParams->GetFirstPageNo(); |
| 393 m_dwFirstPageObjNum = pLinearizationParams->GetFirstPageObjNum(); | 393 m_dwFirstPageObjNum = pLinearizationParams->GetFirstPageObjNum(); |
| 394 } | 394 } |
| 395 | 395 |
| 396 void CPDF_Document::LoadPages() { | 396 void CPDF_Document::LoadPages() { |
| 397 m_PageList.SetSize(RetrievePageCount()); | 397 m_PageList.resize(RetrievePageCount()); |
| 398 } | 398 } |
| 399 | 399 |
| 400 CPDF_Dictionary* CPDF_Document::TraversePDFPages(int iPage, | 400 CPDF_Dictionary* CPDF_Document::TraversePDFPages(int iPage, |
| 401 int* nPagesToGo, | 401 int* nPagesToGo, |
| 402 size_t level) { | 402 size_t level) { |
| 403 if (*nPagesToGo < 0 || m_bReachedMaxPageLevel) | 403 if (*nPagesToGo < 0 || m_bReachedMaxPageLevel) |
| 404 return nullptr; | 404 return nullptr; |
| 405 |
| 405 CPDF_Dictionary* pPages = m_pTreeTraversal[level].first; | 406 CPDF_Dictionary* pPages = m_pTreeTraversal[level].first; |
| 406 CPDF_Array* pKidList = pPages->GetArrayFor("Kids"); | 407 CPDF_Array* pKidList = pPages->GetArrayFor("Kids"); |
| 407 if (!pKidList) { | 408 if (!pKidList) { |
| 408 if (*nPagesToGo != 1) | 409 if (*nPagesToGo != 1) |
| 409 return nullptr; | 410 return nullptr; |
| 410 m_PageList.SetAt(iPage, pPages->GetObjNum()); | 411 m_PageList[iPage] = pPages->GetObjNum(); |
| 411 return pPages; | 412 return pPages; |
| 412 } | 413 } |
| 413 | |
| 414 if (level >= FX_MAX_PAGE_LEVEL) { | 414 if (level >= FX_MAX_PAGE_LEVEL) { |
| 415 m_pTreeTraversal.pop_back(); | 415 m_pTreeTraversal.pop_back(); |
| 416 m_bReachedMaxPageLevel = true; | 416 m_bReachedMaxPageLevel = true; |
| 417 return nullptr; | 417 return nullptr; |
| 418 } | 418 } |
| 419 | |
| 420 CPDF_Dictionary* page = nullptr; | 419 CPDF_Dictionary* page = nullptr; |
| 421 for (size_t i = m_pTreeTraversal[level].second; i < pKidList->GetCount(); | 420 for (size_t i = m_pTreeTraversal[level].second; i < pKidList->GetCount(); |
| 422 i++) { | 421 i++) { |
| 423 if (*nPagesToGo == 0) | 422 if (*nPagesToGo == 0) |
| 424 break; | 423 break; |
| 425 CPDF_Dictionary* pKid = pKidList->GetDictAt(i); | 424 CPDF_Dictionary* pKid = pKidList->GetDictAt(i); |
| 426 if (!pKid) { | 425 if (!pKid) { |
| 427 (*nPagesToGo)--; | 426 (*nPagesToGo)--; |
| 428 m_pTreeTraversal[level].second++; | 427 m_pTreeTraversal[level].second++; |
| 429 continue; | 428 continue; |
| 430 } | 429 } |
| 431 if (pKid == pPages) { | 430 if (pKid == pPages) { |
| 432 m_pTreeTraversal[level].second++; | 431 m_pTreeTraversal[level].second++; |
| 433 continue; | 432 continue; |
| 434 } | 433 } |
| 435 if (!pKid->KeyExist("Kids")) { | 434 if (!pKid->KeyExist("Kids")) { |
| 436 m_PageList.SetAt(iPage - (*nPagesToGo) + 1, pKid->GetObjNum()); | 435 m_PageList[iPage - (*nPagesToGo) + 1] = pKid->GetObjNum(); |
| 437 (*nPagesToGo)--; | 436 (*nPagesToGo)--; |
| 438 m_pTreeTraversal[level].second++; | 437 m_pTreeTraversal[level].second++; |
| 439 if (*nPagesToGo == 0) { | 438 if (*nPagesToGo == 0) { |
| 440 page = pKid; | 439 page = pKid; |
| 441 break; | 440 break; |
| 442 } | 441 } |
| 443 } else { | 442 } else { |
| 444 // If the vector has size level+1, the child is not in yet | 443 // If the vector has size level+1, the child is not in yet |
| 445 if (m_pTreeTraversal.size() == level + 1) | 444 if (m_pTreeTraversal.size() == level + 1) |
| 446 m_pTreeTraversal.push_back(std::make_pair(pKid, 0)); | 445 m_pTreeTraversal.push_back(std::make_pair(pKid, 0)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 467 m_bReachedMaxPageLevel = false; | 466 m_bReachedMaxPageLevel = false; |
| 468 m_pTreeTraversal.clear(); | 467 m_pTreeTraversal.clear(); |
| 469 } | 468 } |
| 470 | 469 |
| 471 CPDF_Dictionary* CPDF_Document::GetPagesDict() const { | 470 CPDF_Dictionary* CPDF_Document::GetPagesDict() const { |
| 472 CPDF_Dictionary* pRoot = GetRoot(); | 471 CPDF_Dictionary* pRoot = GetRoot(); |
| 473 return pRoot ? pRoot->GetDictFor("Pages") : nullptr; | 472 return pRoot ? pRoot->GetDictFor("Pages") : nullptr; |
| 474 } | 473 } |
| 475 | 474 |
| 476 bool CPDF_Document::IsPageLoaded(int iPage) const { | 475 bool CPDF_Document::IsPageLoaded(int iPage) const { |
| 477 return !!m_PageList.GetAt(iPage); | 476 return !!m_PageList[iPage]; |
| 478 } | 477 } |
| 479 | 478 |
| 480 CPDF_Dictionary* CPDF_Document::GetPage(int iPage) { | 479 CPDF_Dictionary* CPDF_Document::GetPage(int iPage) { |
| 481 if (iPage < 0 || iPage >= m_PageList.GetSize()) | 480 if (iPage < 0 || iPage >= pdfium::CollectionSize<int>(m_PageList)) |
| 482 return nullptr; | 481 return nullptr; |
| 483 | 482 |
| 484 if (m_bLinearized && (iPage == m_iFirstPageNo)) { | 483 if (m_bLinearized && (iPage == m_iFirstPageNo)) { |
| 485 if (CPDF_Dictionary* pDict = | 484 if (CPDF_Dictionary* pDict = |
| 486 ToDictionary(GetOrParseIndirectObject(m_dwFirstPageObjNum))) { | 485 ToDictionary(GetOrParseIndirectObject(m_dwFirstPageObjNum))) { |
| 487 return pDict; | 486 return pDict; |
| 488 } | 487 } |
| 489 } | 488 } |
| 490 | 489 uint32_t objnum = m_PageList[iPage]; |
| 491 int objnum = m_PageList.GetAt(iPage); | 490 if (objnum) |
| 492 if (objnum) { | 491 return ToDictionary(GetOrParseIndirectObject(objnum)); |
| 493 if (CPDF_Dictionary* pDict = ToDictionary(GetOrParseIndirectObject(objnum))) | |
| 494 return pDict; | |
| 495 return nullptr; | |
| 496 } | |
| 497 | 492 |
| 498 CPDF_Dictionary* pPages = GetPagesDict(); | 493 CPDF_Dictionary* pPages = GetPagesDict(); |
| 499 if (!pPages) | 494 if (!pPages) |
| 500 return nullptr; | 495 return nullptr; |
| 501 | 496 |
| 502 if (iPage - m_iNextPageToTraverse + 1 <= 0) { | 497 if (iPage - m_iNextPageToTraverse + 1 <= 0) { |
| 503 // This can happen when the page does not have an object number. On repeated | 498 // This can happen when the page does not have an object number. On repeated |
| 504 // calls to this function for the same page index, this condition causes | 499 // calls to this function for the same page index, this condition causes |
| 505 // TraversePDFPages() to incorrectly return nullptr. | 500 // TraversePDFPages() to incorrectly return nullptr. |
| 506 // Example "testing/corpus/fx/other/jetman_std.pdf" | 501 // Example "testing/corpus/fx/other/jetman_std.pdf" |
| 507 // We should restart traversing in this case. | 502 // We should restart traversing in this case. |
| 508 // TODO(art-snake): optimize this. | 503 // TODO(art-snake): optimize this. |
| 509 ResetTraversal(); | 504 ResetTraversal(); |
| 510 } | 505 } |
| 511 int nPagesToGo = iPage - m_iNextPageToTraverse + 1; | 506 int nPagesToGo = iPage - m_iNextPageToTraverse + 1; |
| 512 if (m_pTreeTraversal.empty()) | 507 if (m_pTreeTraversal.empty()) |
| 513 m_pTreeTraversal.push_back(std::make_pair(pPages, 0)); | 508 m_pTreeTraversal.push_back(std::make_pair(pPages, 0)); |
| 514 CPDF_Dictionary* pPage = TraversePDFPages(iPage, &nPagesToGo, 0); | 509 CPDF_Dictionary* pPage = TraversePDFPages(iPage, &nPagesToGo, 0); |
| 515 m_iNextPageToTraverse = iPage + 1; | 510 m_iNextPageToTraverse = iPage + 1; |
| 516 return pPage; | 511 return pPage; |
| 517 } | 512 } |
| 518 | 513 |
| 519 void CPDF_Document::SetPageObjNum(int iPage, uint32_t objNum) { | 514 void CPDF_Document::SetPageObjNum(int iPage, uint32_t objNum) { |
| 520 m_PageList.SetAt(iPage, objNum); | 515 m_PageList[iPage] = objNum; |
| 521 } | 516 } |
| 522 | 517 |
| 523 int CPDF_Document::FindPageIndex(CPDF_Dictionary* pNode, | 518 int CPDF_Document::FindPageIndex(CPDF_Dictionary* pNode, |
| 524 uint32_t& skip_count, | 519 uint32_t& skip_count, |
| 525 uint32_t objnum, | 520 uint32_t objnum, |
| 526 int& index, | 521 int& index, |
| 527 int level) { | 522 int level) { |
| 528 if (!pNode->KeyExist("Kids")) { | 523 if (!pNode->KeyExist("Kids")) { |
| 529 if (objnum == pNode->GetObjNum()) | 524 if (objnum == pNode->GetObjNum()) |
| 530 return index; | 525 return index; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 547 if (count <= skip_count) { | 542 if (count <= skip_count) { |
| 548 skip_count -= count; | 543 skip_count -= count; |
| 549 index += count; | 544 index += count; |
| 550 return -1; | 545 return -1; |
| 551 } | 546 } |
| 552 | 547 |
| 553 if (count && count == pKidList->GetCount()) { | 548 if (count && count == pKidList->GetCount()) { |
| 554 for (size_t i = 0; i < count; i++) { | 549 for (size_t i = 0; i < count; i++) { |
| 555 if (CPDF_Reference* pKid = ToReference(pKidList->GetObjectAt(i))) { | 550 if (CPDF_Reference* pKid = ToReference(pKidList->GetObjectAt(i))) { |
| 556 if (pKid->GetRefObjNum() == objnum) { | 551 if (pKid->GetRefObjNum() == objnum) { |
| 557 m_PageList.SetAt(index + i, objnum); | 552 m_PageList[index + i] = objnum; |
| 558 return static_cast<int>(index + i); | 553 return static_cast<int>(index + i); |
| 559 } | 554 } |
| 560 } | 555 } |
| 561 } | 556 } |
| 562 } | 557 } |
| 563 | 558 |
| 564 for (size_t i = 0; i < pKidList->GetCount(); i++) { | 559 for (size_t i = 0; i < pKidList->GetCount(); i++) { |
| 565 CPDF_Dictionary* pKid = pKidList->GetDictAt(i); | 560 CPDF_Dictionary* pKid = pKidList->GetDictAt(i); |
| 566 if (!pKid || pKid == pNode) | 561 if (!pKid || pKid == pNode) |
| 567 continue; | 562 continue; |
| 568 | 563 |
| 569 int found_index = FindPageIndex(pKid, skip_count, objnum, index, level + 1); | 564 int found_index = FindPageIndex(pKid, skip_count, objnum, index, level + 1); |
| 570 if (found_index >= 0) | 565 if (found_index >= 0) |
| 571 return found_index; | 566 return found_index; |
| 572 } | 567 } |
| 573 return -1; | 568 return -1; |
| 574 } | 569 } |
| 575 | 570 |
| 576 int CPDF_Document::GetPageIndex(uint32_t objnum) { | 571 int CPDF_Document::GetPageIndex(uint32_t objnum) { |
| 577 uint32_t nPages = m_PageList.GetSize(); | 572 uint32_t nPages = m_PageList.size(); |
| 578 uint32_t skip_count = 0; | 573 uint32_t skip_count = 0; |
| 579 bool bSkipped = false; | 574 bool bSkipped = false; |
| 580 for (uint32_t i = 0; i < nPages; i++) { | 575 for (uint32_t i = 0; i < nPages; i++) { |
| 581 uint32_t objnum1 = m_PageList.GetAt(i); | 576 if (m_PageList[i] == objnum) |
| 582 if (objnum1 == objnum) | |
| 583 return i; | 577 return i; |
| 584 | 578 |
| 585 if (!bSkipped && objnum1 == 0) { | 579 if (!bSkipped && m_PageList[i] == 0) { |
| 586 skip_count = i; | 580 skip_count = i; |
| 587 bSkipped = true; | 581 bSkipped = true; |
| 588 } | 582 } |
| 589 } | 583 } |
| 590 CPDF_Dictionary* pPages = GetPagesDict(); | 584 CPDF_Dictionary* pPages = GetPagesDict(); |
| 591 if (!pPages) | 585 if (!pPages) |
| 592 return -1; | 586 return -1; |
| 593 | 587 |
| 594 int index = 0; | 588 int index = 0; |
| 595 return FindPageIndex(pPages, skip_count, objnum, index); | 589 return FindPageIndex(pPages, skip_count, objnum, index); |
| 596 } | 590 } |
| 597 | 591 |
| 598 int CPDF_Document::GetPageCount() const { | 592 int CPDF_Document::GetPageCount() const { |
| 599 return m_PageList.GetSize(); | 593 return pdfium::CollectionSize<int>(m_PageList); |
| 600 } | 594 } |
| 601 | 595 |
| 602 int CPDF_Document::RetrievePageCount() const { | 596 int CPDF_Document::RetrievePageCount() const { |
| 603 CPDF_Dictionary* pPages = GetPagesDict(); | 597 CPDF_Dictionary* pPages = GetPagesDict(); |
| 604 if (!pPages) | 598 if (!pPages) |
| 605 return 0; | 599 return 0; |
| 606 | 600 |
| 607 if (!pPages->KeyExist("Kids")) | 601 if (!pPages->KeyExist("Kids")) |
| 608 return 1; | 602 return 1; |
| 609 | 603 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 pPagesList = pPages->SetNewFor<CPDF_Array>("Kids"); | 734 pPagesList = pPages->SetNewFor<CPDF_Array>("Kids"); |
| 741 pPagesList->AddNew<CPDF_Reference>(this, pPageDict->GetObjNum()); | 735 pPagesList->AddNew<CPDF_Reference>(this, pPageDict->GetObjNum()); |
| 742 pPages->SetNewFor<CPDF_Number>("Count", nPages + 1); | 736 pPages->SetNewFor<CPDF_Number>("Count", nPages + 1); |
| 743 pPageDict->SetNewFor<CPDF_Reference>("Parent", this, pPages->GetObjNum()); | 737 pPageDict->SetNewFor<CPDF_Reference>("Parent", this, pPages->GetObjNum()); |
| 744 ResetTraversal(); | 738 ResetTraversal(); |
| 745 } else { | 739 } else { |
| 746 std::set<CPDF_Dictionary*> stack = {pPages}; | 740 std::set<CPDF_Dictionary*> stack = {pPages}; |
| 747 if (!InsertDeletePDFPage(pPages, iPage, pPageDict, true, &stack)) | 741 if (!InsertDeletePDFPage(pPages, iPage, pPageDict, true, &stack)) |
| 748 return false; | 742 return false; |
| 749 } | 743 } |
| 750 m_PageList.InsertAt(iPage, pPageDict->GetObjNum()); | 744 m_PageList.insert(m_PageList.begin() + iPage, pPageDict->GetObjNum()); |
| 751 return true; | 745 return true; |
| 752 } | 746 } |
| 753 | 747 |
| 754 void CPDF_Document::DeletePage(int iPage) { | 748 void CPDF_Document::DeletePage(int iPage) { |
| 755 CPDF_Dictionary* pPages = GetPagesDict(); | 749 CPDF_Dictionary* pPages = GetPagesDict(); |
| 756 if (!pPages) | 750 if (!pPages) |
| 757 return; | 751 return; |
| 758 | 752 |
| 759 int nPages = pPages->GetIntegerFor("Count"); | 753 int nPages = pPages->GetIntegerFor("Count"); |
| 760 if (iPage < 0 || iPage >= nPages) | 754 if (iPage < 0 || iPage >= nPages) |
| 761 return; | 755 return; |
| 762 | 756 |
| 763 std::set<CPDF_Dictionary*> stack = {pPages}; | 757 std::set<CPDF_Dictionary*> stack = {pPages}; |
| 764 if (!InsertDeletePDFPage(pPages, iPage, nullptr, false, &stack)) | 758 if (!InsertDeletePDFPage(pPages, iPage, nullptr, false, &stack)) |
| 765 return; | 759 return; |
| 766 | 760 |
| 767 m_PageList.RemoveAt(iPage); | 761 m_PageList.erase(m_PageList.begin() + iPage); |
| 768 } | 762 } |
| 769 | 763 |
| 770 CPDF_Font* CPDF_Document::AddStandardFont(const FX_CHAR* font, | 764 CPDF_Font* CPDF_Document::AddStandardFont(const FX_CHAR* font, |
| 771 CPDF_FontEncoding* pEncoding) { | 765 CPDF_FontEncoding* pEncoding) { |
| 772 CFX_ByteString name(font); | 766 CFX_ByteString name(font); |
| 773 if (PDF_GetStandardFontName(&name) < 0) | 767 if (PDF_GetStandardFontName(&name) < 0) |
| 774 return nullptr; | 768 return nullptr; |
| 775 return GetPageData()->GetStandardFont(name, pEncoding); | 769 return GetPageData()->GetStandardFont(name, pEncoding); |
| 776 } | 770 } |
| 777 | 771 |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1047 pFontDesc->SetNewFor<CPDF_Number>("CapHeight", capheight); | 1041 pFontDesc->SetNewFor<CPDF_Number>("CapHeight", capheight); |
| 1048 pFontDict->SetNewFor<CPDF_Reference>( | 1042 pFontDict->SetNewFor<CPDF_Reference>( |
| 1049 "FontDescriptor", this, | 1043 "FontDescriptor", this, |
| 1050 AddIndirectObject(std::move(pFontDesc))->GetObjNum()); | 1044 AddIndirectObject(std::move(pFontDesc))->GetObjNum()); |
| 1051 hFont = SelectObject(hDC, hFont); | 1045 hFont = SelectObject(hDC, hFont); |
| 1052 DeleteObject(hFont); | 1046 DeleteObject(hFont); |
| 1053 DeleteDC(hDC); | 1047 DeleteDC(hDC); |
| 1054 return LoadFont(pBaseDict); | 1048 return LoadFont(pBaseDict); |
| 1055 } | 1049 } |
| 1056 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 1050 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| OLD | NEW |