| OLD | NEW |
| 1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "core/fpdfapi/parser/cpdf_document.h" | 5 #include "core/fpdfapi/parser/cpdf_document.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "core/fpdfapi/cpdf_modulemgr.h" | 9 #include "core/fpdfapi/cpdf_modulemgr.h" |
| 10 #include "core/fpdfapi/parser/cpdf_array.h" | 10 #include "core/fpdfapi/parser/cpdf_array.h" |
| 11 #include "core/fpdfapi/parser/cpdf_dictionary.h" | 11 #include "core/fpdfapi/parser/cpdf_dictionary.h" |
| 12 #include "core/fpdfapi/parser/cpdf_linearized_header.h" | 12 #include "core/fpdfapi/parser/cpdf_linearized_header.h" |
| 13 #include "core/fpdfapi/parser/cpdf_parser.h" | 13 #include "core/fpdfapi/parser/cpdf_parser.h" |
| 14 #include "core/fxcrt/fx_memory.h" | 14 #include "core/fxcrt/fx_memory.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/base/ptr_util.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 CPDF_Dictionary* CreatePageTreeNode(CPDF_Array* kids, | 20 CPDF_Dictionary* CreatePageTreeNode(std::unique_ptr<CPDF_Array> kids, |
| 20 CPDF_Document* pDoc, | 21 CPDF_Document* pDoc, |
| 21 int count) { | 22 int count) { |
| 22 CPDF_Dictionary* pageNode = new CPDF_Dictionary(); | 23 CPDF_Array* pUnowned = pDoc->AddIndirectObject(std::move(kids))->AsArray(); |
| 24 CPDF_Dictionary* pageNode = pDoc->NewIndirect<CPDF_Dictionary>(); |
| 23 pageNode->SetStringFor("Type", "Pages"); | 25 pageNode->SetStringFor("Type", "Pages"); |
| 24 pageNode->SetReferenceFor("Kids", pDoc, pDoc->AddIndirectObject(kids)); | 26 pageNode->SetReferenceFor("Kids", pDoc, pUnowned); |
| 25 pageNode->SetIntegerFor("Count", count); | 27 pageNode->SetIntegerFor("Count", count); |
| 26 uint32_t pageNodeRef = pDoc->AddIndirectObject(pageNode); | 28 for (size_t i = 0; i < pUnowned->GetCount(); i++) |
| 27 for (size_t i = 0; i < kids->GetCount(); i++) | 29 pUnowned->GetDictAt(i)->SetReferenceFor("Parent", pDoc, pageNode); |
| 28 kids->GetDictAt(i)->SetReferenceFor("Parent", pDoc, pageNodeRef); | |
| 29 return pageNode; | 30 return pageNode; |
| 30 } | 31 } |
| 31 | 32 |
| 32 CPDF_Dictionary* CreateNumberedPage(size_t number) { | 33 std::unique_ptr<CPDF_Dictionary> CreateNumberedPage(size_t number) { |
| 33 CPDF_Dictionary* page = new CPDF_Dictionary(); | 34 auto page = pdfium::MakeUnique<CPDF_Dictionary>(); |
| 34 page->SetStringFor("Type", "Page"); | 35 page->SetStringFor("Type", "Page"); |
| 35 page->SetIntegerFor("PageNumbering", number); | 36 page->SetIntegerFor("PageNumbering", number); |
| 36 return page; | 37 return page; |
| 37 } | 38 } |
| 38 | 39 |
| 39 class CPDF_TestDocumentForPages : public CPDF_Document { | 40 class CPDF_TestDocumentForPages : public CPDF_Document { |
| 40 public: | 41 public: |
| 41 CPDF_TestDocumentForPages() : CPDF_Document(nullptr) { | 42 CPDF_TestDocumentForPages() : CPDF_Document(nullptr) { |
| 42 // Set up test | 43 // Set up test |
| 43 CPDF_Array* zeroToTwo = new CPDF_Array(); | 44 auto zeroToTwo = pdfium::MakeUnique<CPDF_Array>(); |
| 44 zeroToTwo->AddReference(this, AddIndirectObject(CreateNumberedPage(0))); | 45 zeroToTwo->AddReference(this, AddIndirectObject(CreateNumberedPage(0))); |
| 45 zeroToTwo->AddReference(this, AddIndirectObject(CreateNumberedPage(1))); | 46 zeroToTwo->AddReference(this, AddIndirectObject(CreateNumberedPage(1))); |
| 46 zeroToTwo->AddReference(this, AddIndirectObject(CreateNumberedPage(2))); | 47 zeroToTwo->AddReference(this, AddIndirectObject(CreateNumberedPage(2))); |
| 47 CPDF_Dictionary* branch1 = CreatePageTreeNode(zeroToTwo, this, 3); | 48 CPDF_Dictionary* branch1 = |
| 49 CreatePageTreeNode(std::move(zeroToTwo), this, 3); |
| 48 | 50 |
| 49 CPDF_Array* zeroToThree = new CPDF_Array(); | 51 auto zeroToThree = pdfium::MakeUnique<CPDF_Array>(); |
| 50 zeroToThree->AddReference(this, branch1->GetObjNum()); | 52 zeroToThree->AddReference(this, branch1->GetObjNum()); |
| 51 zeroToThree->AddReference(this, AddIndirectObject(CreateNumberedPage(3))); | 53 zeroToThree->AddReference(this, AddIndirectObject(CreateNumberedPage(3))); |
| 52 CPDF_Dictionary* branch2 = CreatePageTreeNode(zeroToThree, this, 4); | 54 CPDF_Dictionary* branch2 = |
| 55 CreatePageTreeNode(std::move(zeroToThree), this, 4); |
| 53 | 56 |
| 54 CPDF_Array* fourFive = new CPDF_Array(); | 57 auto fourFive = pdfium::MakeUnique<CPDF_Array>(); |
| 55 fourFive->AddReference(this, AddIndirectObject(CreateNumberedPage(4))); | 58 fourFive->AddReference(this, AddIndirectObject(CreateNumberedPage(4))); |
| 56 fourFive->AddReference(this, AddIndirectObject(CreateNumberedPage(5))); | 59 fourFive->AddReference(this, AddIndirectObject(CreateNumberedPage(5))); |
| 57 CPDF_Dictionary* branch3 = CreatePageTreeNode(fourFive, this, 2); | 60 CPDF_Dictionary* branch3 = CreatePageTreeNode(std::move(fourFive), this, 2); |
| 58 | 61 |
| 59 CPDF_Array* justSix = new CPDF_Array(); | 62 auto justSix = pdfium::MakeUnique<CPDF_Array>(); |
| 60 justSix->AddReference(this, AddIndirectObject(CreateNumberedPage(6))); | 63 justSix->AddReference(this, AddIndirectObject(CreateNumberedPage(6))); |
| 61 CPDF_Dictionary* branch4 = CreatePageTreeNode(justSix, this, 1); | 64 CPDF_Dictionary* branch4 = CreatePageTreeNode(std::move(justSix), this, 1); |
| 62 | 65 |
| 63 CPDF_Array* allPages = new CPDF_Array(); | 66 auto allPages = pdfium::MakeUnique<CPDF_Array>(); |
| 64 allPages->AddReference(this, branch2->GetObjNum()); | 67 allPages->AddReference(this, branch2); |
| 65 allPages->AddReference(this, branch3->GetObjNum()); | 68 allPages->AddReference(this, branch3); |
| 66 allPages->AddReference(this, branch4->GetObjNum()); | 69 allPages->AddReference(this, branch4); |
| 67 CPDF_Dictionary* pagesDict = CreatePageTreeNode(allPages, this, 7); | 70 CPDF_Dictionary* pagesDict = |
| 71 CreatePageTreeNode(std::move(allPages), this, 7); |
| 68 | 72 |
| 69 m_pOwnedRootDict.reset(new CPDF_Dictionary()); | 73 m_pOwnedRootDict = pdfium::MakeUnique<CPDF_Dictionary>(); |
| 70 m_pOwnedRootDict->SetReferenceFor("Pages", this, pagesDict->GetObjNum()); | 74 m_pOwnedRootDict->SetReferenceFor("Pages", this, pagesDict); |
| 71 m_pRootDict = m_pOwnedRootDict.get(); | 75 m_pRootDict = m_pOwnedRootDict.get(); |
| 72 m_PageList.SetSize(7); | 76 m_PageList.SetSize(7); |
| 73 } | 77 } |
| 74 | 78 |
| 75 private: | 79 private: |
| 76 std::unique_ptr<CPDF_Dictionary> m_pOwnedRootDict; | 80 std::unique_ptr<CPDF_Dictionary> m_pOwnedRootDict; |
| 77 }; | 81 }; |
| 78 | 82 |
| 79 class CPDF_TestDocumentWithPageWithoutPageNum : public CPDF_Document { | 83 class CPDF_TestDocumentWithPageWithoutPageNum : public CPDF_Document { |
| 80 public: | 84 public: |
| 81 CPDF_TestDocumentWithPageWithoutPageNum() : CPDF_Document(nullptr) { | 85 CPDF_TestDocumentWithPageWithoutPageNum() : CPDF_Document(nullptr) { |
| 82 // Set up test | 86 // Set up test |
| 83 CPDF_Array* allPages = new CPDF_Array(); | 87 auto allPages = pdfium::MakeUnique<CPDF_Array>(); |
| 84 allPages->AddReference(this, AddIndirectObject(CreateNumberedPage(0))); | 88 allPages->AddReference(this, AddIndirectObject(CreateNumberedPage(0))); |
| 85 allPages->AddReference(this, AddIndirectObject(CreateNumberedPage(1))); | 89 allPages->AddReference(this, AddIndirectObject(CreateNumberedPage(1))); |
| 86 // Page without pageNum. | 90 // Page without pageNum. |
| 87 allPages->Add(CreateNumberedPage(2)); | 91 allPages->Add(CreateNumberedPage(2).release()); |
| 88 CPDF_Dictionary* pagesDict = CreatePageTreeNode(allPages, this, 3); | 92 CPDF_Dictionary* pagesDict = |
| 89 | 93 CreatePageTreeNode(std::move(allPages), this, 3); |
| 90 m_pOwnedRootDict.reset(new CPDF_Dictionary()); | 94 m_pOwnedRootDict.reset(new CPDF_Dictionary()); |
| 91 m_pOwnedRootDict->SetReferenceFor("Pages", this, pagesDict->GetObjNum()); | 95 m_pOwnedRootDict->SetReferenceFor("Pages", this, pagesDict->GetObjNum()); |
| 92 m_pRootDict = m_pOwnedRootDict.get(); | 96 m_pRootDict = m_pOwnedRootDict.get(); |
| 93 m_PageList.SetSize(3); | 97 m_PageList.SetSize(3); |
| 94 } | 98 } |
| 95 | 99 |
| 96 private: | 100 private: |
| 97 std::unique_ptr<CPDF_Dictionary> m_pOwnedRootDict; | 101 std::unique_ptr<CPDF_Dictionary> m_pOwnedRootDict; |
| 98 }; | 102 }; |
| 99 | 103 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 // can be not exists in this case. | 183 // can be not exists in this case. |
| 180 // (case, when hint table is used to page check in CPDF_DataAvail). | 184 // (case, when hint table is used to page check in CPDF_DataAvail). |
| 181 CPDF_Document document(pdfium::MakeUnique<CPDF_Parser>()); | 185 CPDF_Document document(pdfium::MakeUnique<CPDF_Parser>()); |
| 182 auto dict = pdfium::MakeUnique<CPDF_Dictionary>(); | 186 auto dict = pdfium::MakeUnique<CPDF_Dictionary>(); |
| 183 dict->SetBooleanFor("Linearized", true); | 187 dict->SetBooleanFor("Linearized", true); |
| 184 const int page_count = 100; | 188 const int page_count = 100; |
| 185 dict->SetIntegerFor("N", page_count); | 189 dict->SetIntegerFor("N", page_count); |
| 186 TestLinearized linearized(dict.get()); | 190 TestLinearized linearized(dict.get()); |
| 187 document.LoadLinearizedDoc(&linearized); | 191 document.LoadLinearizedDoc(&linearized); |
| 188 ASSERT_EQ(page_count, document.GetPageCount()); | 192 ASSERT_EQ(page_count, document.GetPageCount()); |
| 189 CPDF_Object* page_stub = new CPDF_Dictionary(); | 193 CPDF_Object* page_stub = document.NewIndirect<CPDF_Dictionary>(); |
| 190 const uint32_t obj_num = document.AddIndirectObject(page_stub); | 194 const uint32_t obj_num = page_stub->GetObjNum(); |
| 191 const int test_page_num = 33; | 195 const int test_page_num = 33; |
| 192 | 196 |
| 193 EXPECT_FALSE(document.IsPageLoaded(test_page_num)); | 197 EXPECT_FALSE(document.IsPageLoaded(test_page_num)); |
| 194 EXPECT_EQ(nullptr, document.GetPage(test_page_num)); | 198 EXPECT_EQ(nullptr, document.GetPage(test_page_num)); |
| 195 | 199 |
| 196 document.SetPageObjNum(test_page_num, obj_num); | 200 document.SetPageObjNum(test_page_num, obj_num); |
| 197 | |
| 198 EXPECT_TRUE(document.IsPageLoaded(test_page_num)); | 201 EXPECT_TRUE(document.IsPageLoaded(test_page_num)); |
| 199 EXPECT_EQ(page_stub, document.GetPage(test_page_num)); | 202 EXPECT_EQ(page_stub, document.GetPage(test_page_num)); |
| 200 } | 203 } |
| OLD | NEW |