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 <map> | 7 #include <map> |
8 #include <memory> | |
9 #include <utility> | |
8 | 10 |
9 #include "core/fpdfapi/parser/cpdf_array.h" | 11 #include "core/fpdfapi/parser/cpdf_array.h" |
10 #include "core/fpdfapi/parser/cpdf_dictionary.h" | 12 #include "core/fpdfapi/parser/cpdf_dictionary.h" |
11 #include "core/fpdfapi/parser/cpdf_document.h" | 13 #include "core/fpdfapi/parser/cpdf_document.h" |
12 #include "core/fpdfapi/parser/cpdf_name.h" | 14 #include "core/fpdfapi/parser/cpdf_name.h" |
13 #include "core/fpdfapi/parser/cpdf_number.h" | 15 #include "core/fpdfapi/parser/cpdf_number.h" |
14 #include "core/fpdfapi/parser/cpdf_reference.h" | 16 #include "core/fpdfapi/parser/cpdf_reference.h" |
15 #include "core/fpdfapi/parser/cpdf_stream.h" | 17 #include "core/fpdfapi/parser/cpdf_stream.h" |
16 #include "core/fpdfdoc/cpdf_numbertree.h" | 18 #include "core/fpdfdoc/cpdf_numbertree.h" |
17 #include "core/fpdfdoc/fpdf_tagged.h" | 19 #include "core/fpdfdoc/fpdf_tagged.h" |
18 #include "core/fpdfdoc/tagged_int.h" | 20 #include "core/fpdfdoc/tagged_int.h" |
21 #include "third_party/base/ptr_util.h" | |
19 | 22 |
20 namespace { | 23 namespace { |
21 | 24 |
22 const int nMaxRecursion = 32; | 25 const int nMaxRecursion = 32; |
23 | 26 |
24 bool IsTagged(const CPDF_Document* pDoc) { | 27 bool IsTagged(const CPDF_Document* pDoc) { |
25 CPDF_Dictionary* pCatalog = pDoc->GetRoot(); | 28 CPDF_Dictionary* pCatalog = pDoc->GetRoot(); |
26 CPDF_Dictionary* pMarkInfo = pCatalog->GetDictFor("MarkInfo"); | 29 CPDF_Dictionary* pMarkInfo = pCatalog->GetDictFor("MarkInfo"); |
27 return pMarkInfo && pMarkInfo->GetIntegerFor("Marked"); | 30 return pMarkInfo && pMarkInfo->GetIntegerFor("Marked"); |
28 } | 31 } |
29 | 32 |
30 } // namespace | 33 } // namespace |
31 | 34 |
32 // static | 35 // static |
33 IPDF_StructTree* IPDF_StructTree::LoadPage(const CPDF_Document* pDoc, | 36 std::unique_ptr<IPDF_StructTree> IPDF_StructTree::LoadPage( |
34 const CPDF_Dictionary* pPageDict) { | 37 const CPDF_Document* pDoc, |
38 const CPDF_Dictionary* pPageDict) { | |
35 if (!IsTagged(pDoc)) | 39 if (!IsTagged(pDoc)) |
36 return nullptr; | 40 return nullptr; |
37 | 41 |
38 CPDF_StructTreeImpl* pTree = new CPDF_StructTreeImpl(pDoc); | 42 auto pTree = pdfium::MakeUnique<CPDF_StructTreeImpl>(pDoc); |
39 pTree->LoadPageTree(pPageDict); | 43 pTree->LoadPageTree(pPageDict); |
40 return pTree; | 44 return std::move(pTree); |
Wei Li
2016/12/16 20:26:33
Nit: move() is not needed here
Tom Sepez
2016/12/16 22:45:17
Sadly, it is required, because the return type is
| |
41 } | |
42 | |
43 // static. | |
44 IPDF_StructTree* IPDF_StructTree::LoadDoc(const CPDF_Document* pDoc) { | |
45 if (!IsTagged(pDoc)) | |
46 return nullptr; | |
47 | |
48 CPDF_StructTreeImpl* pTree = new CPDF_StructTreeImpl(pDoc); | |
49 pTree->LoadDocTree(); | |
50 return pTree; | |
51 } | 45 } |
52 | 46 |
53 CPDF_StructTreeImpl::CPDF_StructTreeImpl(const CPDF_Document* pDoc) | 47 CPDF_StructTreeImpl::CPDF_StructTreeImpl(const CPDF_Document* pDoc) |
54 : m_pTreeRoot(pDoc->GetRoot()->GetDictFor("StructTreeRoot")), | 48 : m_pTreeRoot(pDoc->GetRoot()->GetDictFor("StructTreeRoot")), |
55 m_pRoleMap(m_pTreeRoot ? m_pTreeRoot->GetDictFor("RoleMap") : nullptr), | 49 m_pRoleMap(m_pTreeRoot ? m_pTreeRoot->GetDictFor("RoleMap") : nullptr), |
56 m_pPage(nullptr) {} | 50 m_pPage(nullptr) {} |
57 | 51 |
58 CPDF_StructTreeImpl::~CPDF_StructTreeImpl() {} | 52 CPDF_StructTreeImpl::~CPDF_StructTreeImpl() {} |
59 | 53 |
60 int CPDF_StructTreeImpl::CountTopElements() const { | 54 int CPDF_StructTreeImpl::CountTopElements() const { |
61 return pdfium::CollectionSize<int>(m_Kids); | 55 return pdfium::CollectionSize<int>(m_Kids); |
62 } | 56 } |
63 | 57 |
64 IPDF_StructElement* CPDF_StructTreeImpl::GetTopElement(int i) const { | 58 IPDF_StructElement* CPDF_StructTreeImpl::GetTopElement(int i) const { |
65 return m_Kids[i].Get(); | 59 return m_Kids[i].Get(); |
66 } | 60 } |
67 | 61 |
68 void CPDF_StructTreeImpl::LoadDocTree() { | |
69 m_pPage = nullptr; | |
70 if (!m_pTreeRoot) | |
71 return; | |
72 | |
73 CPDF_Object* pKids = m_pTreeRoot->GetDirectObjectFor("K"); | |
74 if (!pKids) | |
75 return; | |
76 | |
77 if (CPDF_Dictionary* pDict = pKids->AsDictionary()) { | |
78 m_Kids.push_back(CFX_RetainPtr<CPDF_StructElementImpl>( | |
79 new CPDF_StructElementImpl(this, nullptr, pDict))); | |
80 return; | |
81 } | |
82 | |
83 CPDF_Array* pArray = pKids->AsArray(); | |
84 if (!pArray) | |
85 return; | |
86 | |
87 for (size_t i = 0; i < pArray->GetCount(); i++) { | |
88 m_Kids.push_back(CFX_RetainPtr<CPDF_StructElementImpl>( | |
89 new CPDF_StructElementImpl(this, nullptr, pArray->GetDictAt(i)))); | |
90 } | |
91 } | |
92 | |
93 void CPDF_StructTreeImpl::LoadPageTree(const CPDF_Dictionary* pPageDict) { | 62 void CPDF_StructTreeImpl::LoadPageTree(const CPDF_Dictionary* pPageDict) { |
94 m_pPage = pPageDict; | 63 m_pPage = pPageDict; |
95 if (!m_pTreeRoot) | 64 if (!m_pTreeRoot) |
96 return; | 65 return; |
97 | 66 |
98 CPDF_Object* pKids = m_pTreeRoot->GetDirectObjectFor("K"); | 67 CPDF_Object* pKids = m_pTreeRoot->GetDirectObjectFor("K"); |
99 if (!pKids) | 68 if (!pKids) |
100 return; | 69 return; |
101 | 70 |
102 uint32_t dwKids = 0; | 71 uint32_t dwKids = 0; |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
452 return ToNumber(pAttr) ? pAttr->GetNumber() : default_value; | 421 return ToNumber(pAttr) ? pAttr->GetNumber() : default_value; |
453 } | 422 } |
454 int CPDF_StructElementImpl::GetInteger(const CFX_ByteStringC& owner, | 423 int CPDF_StructElementImpl::GetInteger(const CFX_ByteStringC& owner, |
455 const CFX_ByteStringC& name, | 424 const CFX_ByteStringC& name, |
456 int default_value, | 425 int default_value, |
457 bool bInheritable, | 426 bool bInheritable, |
458 int subindex) { | 427 int subindex) { |
459 CPDF_Object* pAttr = GetAttr(owner, name, bInheritable, subindex); | 428 CPDF_Object* pAttr = GetAttr(owner, name, bInheritable, subindex); |
460 return ToNumber(pAttr) ? pAttr->GetInteger() : default_value; | 429 return ToNumber(pAttr) ? pAttr->GetInteger() : default_value; |
461 } | 430 } |
OLD | NEW |