• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "core/fpdfdoc/cpdf_structelement.h"
8 
9 #include <utility>
10 
11 #include "core/fpdfapi/parser/cpdf_array.h"
12 #include "core/fpdfapi/parser/cpdf_dictionary.h"
13 #include "core/fpdfapi/parser/cpdf_name.h"
14 #include "core/fpdfapi/parser/cpdf_number.h"
15 #include "core/fpdfapi/parser/cpdf_object.h"
16 #include "core/fpdfapi/parser/cpdf_reference.h"
17 #include "core/fpdfapi/parser/cpdf_stream.h"
18 #include "core/fpdfdoc/cpdf_structtree.h"
19 
20 namespace {
21 
GetStructElementType(CPDF_StructTree * pTree,const CPDF_Dictionary * pDict)22 ByteString GetStructElementType(CPDF_StructTree* pTree,
23                                 const CPDF_Dictionary* pDict) {
24   ByteString type = pDict->GetStringFor("S");
25   if (pTree->GetRoleMap()) {
26     ByteString mapped = pTree->GetRoleMap()->GetStringFor(type);
27     if (!mapped.IsEmpty())
28       type = std::move(mapped);
29   }
30   return type;
31 }
32 
33 }  // namespace
34 
35 CPDF_StructKid::CPDF_StructKid() = default;
36 
37 CPDF_StructKid::CPDF_StructKid(const CPDF_StructKid& that) = default;
38 
39 CPDF_StructKid::~CPDF_StructKid() = default;
40 
CPDF_StructElement(CPDF_StructTree * pTree,CPDF_StructElement * pParent,const CPDF_Dictionary * pDict)41 CPDF_StructElement::CPDF_StructElement(CPDF_StructTree* pTree,
42                                        CPDF_StructElement* pParent,
43                                        const CPDF_Dictionary* pDict)
44     : m_pTree(pTree),
45       m_pParent(pParent),
46       m_pDict(pDict),
47       m_Type(GetStructElementType(m_pTree.Get(), m_pDict.Get())) {
48   LoadKids(m_pDict.Get());
49 }
50 
51 CPDF_StructElement::~CPDF_StructElement() = default;
52 
GetAltText() const53 WideString CPDF_StructElement::GetAltText() const {
54   return GetDict()->GetUnicodeTextFor("Alt");
55 }
56 
GetTitle() const57 WideString CPDF_StructElement::GetTitle() const {
58   return GetDict()->GetUnicodeTextFor("T");
59 }
60 
CountKids() const61 size_t CPDF_StructElement::CountKids() const {
62   return m_Kids.size();
63 }
64 
GetKidIfElement(size_t index) const65 CPDF_StructElement* CPDF_StructElement::GetKidIfElement(size_t index) const {
66   return m_Kids[index].m_Type == CPDF_StructKid::kElement
67              ? m_Kids[index].m_pElement.Get()
68              : nullptr;
69 }
70 
LoadKids(const CPDF_Dictionary * pDict)71 void CPDF_StructElement::LoadKids(const CPDF_Dictionary* pDict) {
72   const CPDF_Object* pObj = pDict->GetObjectFor("Pg");
73   uint32_t PageObjNum = 0;
74   if (const CPDF_Reference* pRef = ToReference(pObj))
75     PageObjNum = pRef->GetRefObjNum();
76 
77   const CPDF_Object* pKids = pDict->GetDirectObjectFor("K");
78   if (!pKids)
79     return;
80 
81   m_Kids.clear();
82   if (const CPDF_Array* pArray = pKids->AsArray()) {
83     m_Kids.resize(pArray->size());
84     for (uint32_t i = 0; i < pArray->size(); i++) {
85       const CPDF_Object* pKid = pArray->GetDirectObjectAt(i);
86       LoadKid(PageObjNum, pKid, &m_Kids[i]);
87     }
88     return;
89   }
90 
91   m_Kids.resize(1);
92   LoadKid(PageObjNum, pKids, &m_Kids[0]);
93 }
94 
LoadKid(uint32_t PageObjNum,const CPDF_Object * pKidObj,CPDF_StructKid * pKid)95 void CPDF_StructElement::LoadKid(uint32_t PageObjNum,
96                                  const CPDF_Object* pKidObj,
97                                  CPDF_StructKid* pKid) {
98   pKid->m_Type = CPDF_StructKid::kInvalid;
99   if (!pKidObj)
100     return;
101 
102   if (pKidObj->IsNumber()) {
103     if (m_pTree->GetPage()->GetObjNum() != PageObjNum)
104       return;
105 
106     pKid->m_Type = CPDF_StructKid::kPageContent;
107     pKid->m_ContentId = pKidObj->GetInteger();
108     pKid->m_PageObjNum = PageObjNum;
109     return;
110   }
111 
112   const CPDF_Dictionary* pKidDict = pKidObj->AsDictionary();
113   if (!pKidDict)
114     return;
115   if (const CPDF_Reference* pRef = ToReference(pKidDict->GetObjectFor("Pg")))
116     PageObjNum = pRef->GetRefObjNum();
117 
118   ByteString type = pKidDict->GetStringFor("Type");
119   if ((type == "MCR" || type == "OBJR") &&
120       m_pTree->GetPage()->GetObjNum() != PageObjNum) {
121     return;
122   }
123 
124   if (type == "MCR") {
125     pKid->m_Type = CPDF_StructKid::kStreamContent;
126     const CPDF_Reference* pRef = ToReference(pKidDict->GetObjectFor("Stm"));
127     pKid->m_RefObjNum = pRef ? pRef->GetRefObjNum() : 0;
128     pKid->m_PageObjNum = PageObjNum;
129     pKid->m_ContentId = pKidDict->GetIntegerFor("MCID");
130     return;
131   }
132 
133   if (type == "OBJR") {
134     pKid->m_Type = CPDF_StructKid::kObject;
135     const CPDF_Reference* pObj = ToReference(pKidDict->GetObjectFor("Obj"));
136     pKid->m_RefObjNum = pObj ? pObj->GetRefObjNum() : 0;
137     pKid->m_PageObjNum = PageObjNum;
138     return;
139   }
140 
141   pKid->m_Type = CPDF_StructKid::kElement;
142   pKid->m_pDict.Reset(pKidDict);
143   pKid->m_pElement = nullptr;
144 }
145