Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / pdfium / core / src / fpdfdoc / doc_tagged.cpp
1 // Copyright 2014 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 "../../include/fpdfapi/fpdf_parser.h"
8 #include "../../include/fpdfapi/fpdf_page.h"
9 #include "../../include/fpdfdoc/fpdf_tagged.h"
10 #include "tagged_int.h"
11 const int nMaxRecursion = 32;
12 static FX_BOOL IsTagged(const CPDF_Document* pDoc)
13 {
14     CPDF_Dictionary* pCatalog = pDoc->GetRoot();
15     CPDF_Dictionary* pMarkInfo = pCatalog->GetDict(FX_BSTRC("MarkInfo"));
16     return pMarkInfo != NULL && pMarkInfo->GetInteger(FX_BSTRC("Marked"));
17 }
18 CPDF_StructTree* CPDF_StructTree::LoadPage(const CPDF_Document* pDoc, const CPDF_Dictionary* pPageDict)
19 {
20     if (!IsTagged(pDoc)) {
21         return NULL;
22     }
23     CPDF_StructTreeImpl* pTree = FX_NEW CPDF_StructTreeImpl(pDoc);
24     if (pTree == NULL) {
25         return NULL;
26     }
27     pTree->LoadPageTree(pPageDict);
28     return pTree;
29 }
30 CPDF_StructTree* CPDF_StructTree::LoadDoc(const CPDF_Document* pDoc)
31 {
32     if (!IsTagged(pDoc)) {
33         return NULL;
34     }
35     CPDF_StructTreeImpl* pTree = FX_NEW CPDF_StructTreeImpl(pDoc);
36     if (pTree == NULL) {
37         return NULL;
38     }
39     pTree->LoadDocTree();
40     return pTree;
41 }
42 CPDF_StructTreeImpl::CPDF_StructTreeImpl(const CPDF_Document* pDoc)
43 {
44     CPDF_Dictionary* pCatalog = pDoc->GetRoot();
45     m_pTreeRoot = pCatalog->GetDict(FX_BSTRC("StructTreeRoot"));
46     if (m_pTreeRoot == NULL) {
47         return;
48     }
49     m_pRoleMap = m_pTreeRoot->GetDict(FX_BSTRC("RoleMap"));
50 }
51 CPDF_StructTreeImpl::~CPDF_StructTreeImpl()
52 {
53     for (int i = 0; i < m_Kids.GetSize(); i ++)
54         if (m_Kids[i]) {
55             m_Kids[i]->Release();
56         }
57 }
58 void CPDF_StructTreeImpl::LoadDocTree()
59 {
60     m_pPage = NULL;
61     if (m_pTreeRoot == NULL) {
62         return;
63     }
64     CPDF_Object* pKids = m_pTreeRoot->GetElementValue(FX_BSTRC("K"));
65     if (pKids == NULL) {
66         return;
67     }
68     if (pKids->GetType() == PDFOBJ_DICTIONARY) {
69         CPDF_StructElementImpl* pStructElementImpl = FX_NEW CPDF_StructElementImpl(this, NULL, (CPDF_Dictionary*)pKids);
70         if (pStructElementImpl == NULL) {
71             return;
72         }
73         m_Kids.Add(pStructElementImpl);
74         return;
75     }
76     if (pKids->GetType() != PDFOBJ_ARRAY) {
77         return;
78     }
79     CPDF_Array* pArray = (CPDF_Array*)pKids;
80     for (FX_DWORD i = 0; i < pArray->GetCount(); i ++) {
81         CPDF_Dictionary* pKid = pArray->GetDict(i);
82         CPDF_StructElementImpl* pStructElementImpl = FX_NEW CPDF_StructElementImpl(this, NULL, pKid);
83         if (pStructElementImpl == NULL) {
84             return;
85         }
86         m_Kids.Add(pStructElementImpl);
87     }
88 }
89 void CPDF_StructTreeImpl::LoadPageTree(const CPDF_Dictionary* pPageDict)
90 {
91     m_pPage = pPageDict;
92     if (m_pTreeRoot == NULL) {
93         return;
94     }
95     CPDF_Object* pKids = m_pTreeRoot->GetElementValue(FX_BSTRC("K"));
96     if (pKids == NULL) {
97         return;
98     }
99     FX_DWORD dwKids = 0;
100     if (pKids->GetType() == PDFOBJ_DICTIONARY) {
101         dwKids = 1;
102     } else if (pKids->GetType() == PDFOBJ_ARRAY) {
103         dwKids = ((CPDF_Array*)pKids)->GetCount();
104     } else {
105         return;
106     }
107     FX_DWORD i;
108     m_Kids.SetSize(dwKids);
109     for (i = 0; i < dwKids; i ++) {
110         m_Kids[i] = NULL;
111     }
112     CFX_MapPtrToPtr element_map;
113     CPDF_Dictionary* pParentTree = m_pTreeRoot->GetDict(FX_BSTRC("ParentTree"));
114     if (pParentTree == NULL) {
115         return;
116     }
117     CPDF_NumberTree parent_tree(pParentTree);
118     int parents_id = pPageDict->GetInteger(FX_BSTRC("StructParents"), -1);
119     if (parents_id >= 0) {
120         CPDF_Object* pParents = parent_tree.LookupValue(parents_id);
121         if (pParents == NULL || pParents->GetType() != PDFOBJ_ARRAY) {
122             return;
123         }
124         CPDF_Array* pParentArray = (CPDF_Array*)pParents;
125         for (i = 0; i < pParentArray->GetCount(); i ++) {
126             CPDF_Dictionary* pParent = pParentArray->GetDict(i);
127             if (pParent == NULL) {
128                 continue;
129             }
130             AddPageNode(pParent, element_map);
131         }
132     }
133 }
134 CPDF_StructElementImpl* CPDF_StructTreeImpl::AddPageNode(CPDF_Dictionary* pDict, CFX_MapPtrToPtr& map, int nLevel)
135 {
136     if (nLevel > nMaxRecursion) {
137         return NULL;
138     }
139     CPDF_StructElementImpl* pElement = NULL;
140     if (map.Lookup(pDict, (FX_LPVOID&)pElement)) {
141         return pElement;
142     }
143     pElement = FX_NEW CPDF_StructElementImpl(this, NULL, pDict);
144     if (pElement == NULL) {
145         return NULL;
146     }
147     map.SetAt(pDict, pElement);
148     CPDF_Dictionary* pParent = pDict->GetDict(FX_BSTRC("P"));
149     if (pParent == NULL || pParent->GetString(FX_BSTRC("Type")) == FX_BSTRC("StructTreeRoot")) {
150         if (!AddTopLevelNode(pDict, pElement)) {
151             pElement->Release();
152             map.RemoveKey(pDict);
153         }
154     } else {
155         CPDF_StructElementImpl* pParentElement = AddPageNode(pParent, map, nLevel + 1);
156         FX_BOOL bSave = FALSE;
157         for (int i = 0; i < pParentElement->m_Kids.GetSize(); i ++) {
158             if (pParentElement->m_Kids[i].m_Type != CPDF_StructKid::Element) {
159                 continue;
160             }
161             if (pParentElement->m_Kids[i].m_Element.m_pDict != pDict) {
162                 continue;
163             }
164             pParentElement->m_Kids[i].m_Element.m_pElement = pElement->Retain();
165             bSave = TRUE;
166         }
167         if (!bSave) {
168             pElement->Release();
169             map.RemoveKey(pDict);
170         }
171     }
172     return pElement;
173 }
174 FX_BOOL CPDF_StructTreeImpl::AddTopLevelNode(CPDF_Dictionary* pDict, CPDF_StructElementImpl* pElement)
175 {
176     CPDF_Object *pObj = m_pTreeRoot->GetElementValue(FX_BSTRC("K"));
177     if (!pObj) {
178         return FALSE;
179     }
180     if (pObj->GetType() == PDFOBJ_DICTIONARY) {
181         if (pObj->GetObjNum() == pDict->GetObjNum()) {
182             if (m_Kids[0]) {
183                 m_Kids[0]->Release();
184             }
185             m_Kids[0] = pElement->Retain();
186         } else {
187             return FALSE;
188         }
189     }
190     if (pObj->GetType() == PDFOBJ_ARRAY) {
191         CPDF_Array* pTopKids = (CPDF_Array*)pObj;
192         FX_DWORD i;
193         FX_BOOL bSave = FALSE;
194         for (i = 0; i < pTopKids->GetCount(); i ++) {
195             CPDF_Object* pKidRef = pTopKids->GetElement(i);
196             if (pKidRef == NULL || pKidRef->GetType() != PDFOBJ_REFERENCE) {
197                 continue;
198             }
199             if (((CPDF_Reference*) pKidRef)->GetRefObjNum() != pDict->GetObjNum()) {
200                 continue;
201             }
202             if (m_Kids[i]) {
203                 m_Kids[i]->Release();
204             }
205             m_Kids[i] = pElement->Retain();
206             bSave = TRUE;
207         }
208         if (!bSave) {
209             return FALSE;
210         }
211     }
212     return TRUE;
213 }
214 CPDF_StructElementImpl::CPDF_StructElementImpl(CPDF_StructTreeImpl* pTree, CPDF_StructElementImpl* pParent, CPDF_Dictionary* pDict)
215     : m_RefCount(0)
216 {
217     m_pTree = pTree;
218     m_pDict = pDict;
219     m_Type = pDict->GetString(FX_BSTRC("S"));
220     CFX_ByteString mapped = pTree->m_pRoleMap->GetString(m_Type);
221     if (!mapped.IsEmpty()) {
222         m_Type = mapped;
223     }
224     m_pParent = pParent;
225     LoadKids(pDict);
226 }
227 CPDF_StructElementImpl::~CPDF_StructElementImpl()
228 {
229     for (int i = 0; i < m_Kids.GetSize(); i ++) {
230         if (m_Kids[i].m_Type == CPDF_StructKid::Element && m_Kids[i].m_Element.m_pElement) {
231             ((CPDF_StructElementImpl*)m_Kids[i].m_Element.m_pElement)->Release();
232         }
233     }
234 }
235 CPDF_StructElementImpl* CPDF_StructElementImpl::Retain()
236 {
237     m_RefCount++;
238     return this;
239 }
240 void CPDF_StructElementImpl::Release()
241 {
242     if(--m_RefCount < 1) {
243         delete this;
244     }
245 }
246 void CPDF_StructElementImpl::LoadKids(CPDF_Dictionary* pDict)
247 {
248     CPDF_Object* pObj = pDict->GetElement(FX_BSTRC("Pg"));
249     FX_DWORD PageObjNum = 0;
250     if (pObj && pObj->GetType() == PDFOBJ_REFERENCE) {
251         PageObjNum = ((CPDF_Reference*)pObj)->GetRefObjNum();
252     }
253     CPDF_Object* pKids = pDict->GetElementValue(FX_BSTRC("K"));
254     if (pKids == NULL) {
255         return;
256     }
257     if (pKids->GetType() == PDFOBJ_ARRAY) {
258         CPDF_Array* pArray = (CPDF_Array*)pKids;
259         m_Kids.SetSize(pArray->GetCount());
260         for (FX_DWORD i = 0; i < pArray->GetCount(); i ++) {
261             CPDF_Object* pKid = pArray->GetElementValue(i);
262             LoadKid(PageObjNum, pKid, &m_Kids[i]);
263         }
264     } else {
265         m_Kids.SetSize(1);
266         LoadKid(PageObjNum, pKids, &m_Kids[0]);
267     }
268 }
269 void CPDF_StructElementImpl::LoadKid(FX_DWORD PageObjNum, CPDF_Object* pKidObj, CPDF_StructKid* pKid)
270 {
271     pKid->m_Type = CPDF_StructKid::Invalid;
272     if (pKidObj == NULL) {
273         return;
274     }
275     if (pKidObj->GetType() == PDFOBJ_NUMBER) {
276         if (m_pTree->m_pPage && m_pTree->m_pPage->GetObjNum() != PageObjNum) {
277             return;
278         }
279         pKid->m_Type = CPDF_StructKid::PageContent;
280         pKid->m_PageContent.m_ContentId = pKidObj->GetInteger();
281         pKid->m_PageContent.m_PageObjNum = PageObjNum;
282         return;
283     }
284     if (pKidObj->GetType() != PDFOBJ_DICTIONARY) {
285         return;
286     }
287     CPDF_Dictionary* pKidDict = (CPDF_Dictionary*)pKidObj;
288     CPDF_Object* pPageObj = pKidDict->GetElement(FX_BSTRC("Pg"));
289     if (pPageObj && pPageObj->GetType() == PDFOBJ_REFERENCE) {
290         PageObjNum = ((CPDF_Reference*)pPageObj)->GetRefObjNum();
291     }
292     CFX_ByteString type = pKidDict->GetString(FX_BSTRC("Type"));
293     if (type == FX_BSTRC("MCR")) {
294         if (m_pTree->m_pPage && m_pTree->m_pPage->GetObjNum() != PageObjNum) {
295             return;
296         }
297         pKid->m_Type = CPDF_StructKid::StreamContent;
298         CPDF_Object* pStreamObj = pKidDict->GetElement(FX_BSTRC("Stm"));
299         if (pStreamObj && pStreamObj->GetType() == PDFOBJ_REFERENCE) {
300             pKid->m_StreamContent.m_RefObjNum = ((CPDF_Reference*)pStreamObj)->GetRefObjNum();
301         } else {
302             pKid->m_StreamContent.m_RefObjNum = 0;
303         }
304         pKid->m_StreamContent.m_PageObjNum = PageObjNum;
305         pKid->m_StreamContent.m_ContentId = pKidDict->GetInteger(FX_BSTRC("MCID"));
306     } else if (type == FX_BSTRC("OBJR")) {
307         if (m_pTree->m_pPage && m_pTree->m_pPage->GetObjNum() != PageObjNum) {
308             return;
309         }
310         pKid->m_Type = CPDF_StructKid::Object;
311         CPDF_Object* pObj = pKidDict->GetElement(FX_BSTRC("Obj"));
312         if (pObj && pObj->GetType() == PDFOBJ_REFERENCE) {
313             pKid->m_Object.m_RefObjNum = ((CPDF_Reference*)pObj)->GetRefObjNum();
314         } else {
315             pKid->m_Object.m_RefObjNum = 0;
316         }
317         pKid->m_Object.m_PageObjNum = PageObjNum;
318     } else {
319         pKid->m_Type = CPDF_StructKid::Element;
320         pKid->m_Element.m_pDict = pKidDict;
321         if (m_pTree->m_pPage == NULL) {
322             pKid->m_Element.m_pElement = FX_NEW CPDF_StructElementImpl(m_pTree, this, pKidDict);
323         } else {
324             pKid->m_Element.m_pElement = NULL;
325         }
326     }
327 }
328 static CPDF_Dictionary* FindAttrDict(CPDF_Object* pAttrs, FX_BSTR owner, FX_FLOAT nLevel = 0.0F)
329 {
330     if (nLevel > nMaxRecursion) {
331         return NULL;
332     }
333     if (pAttrs == NULL) {
334         return NULL;
335     }
336     CPDF_Dictionary* pDict = NULL;
337     if (pAttrs->GetType() == PDFOBJ_DICTIONARY) {
338         pDict = (CPDF_Dictionary*)pAttrs;
339     } else if (pAttrs->GetType() == PDFOBJ_STREAM) {
340         pDict = ((CPDF_Stream*)pAttrs)->GetDict();
341     } else if (pAttrs->GetType() == PDFOBJ_ARRAY) {
342         CPDF_Array* pArray = (CPDF_Array*)pAttrs;
343         for (FX_DWORD i = 0; i < pArray->GetCount(); i ++) {
344             CPDF_Object* pElement = pArray->GetElementValue(i);
345             pDict = FindAttrDict(pElement, owner, nLevel + 1);
346             if (pDict) {
347                 return pDict;
348             }
349         }
350     }
351     if (pDict && pDict->GetString(FX_BSTRC("O")) == owner) {
352         return pDict;
353     }
354     return NULL;
355 }
356 CPDF_Object* CPDF_StructElementImpl::GetAttr(FX_BSTR owner, FX_BSTR name, FX_BOOL bInheritable, FX_FLOAT fLevel)
357 {
358     if (fLevel > nMaxRecursion) {
359         return NULL;
360     }
361     if (bInheritable) {
362         CPDF_Object* pAttr = GetAttr(owner, name, FALSE);
363         if (pAttr) {
364             return pAttr;
365         }
366         if (m_pParent == NULL) {
367             return NULL;
368         }
369         return m_pParent->GetAttr(owner, name, TRUE, fLevel + 1);
370     }
371     CPDF_Object* pA = m_pDict->GetElementValue(FX_BSTRC("A"));
372     if (pA) {
373         CPDF_Dictionary* pAttrDict = FindAttrDict(pA, owner);
374         if (pAttrDict) {
375             CPDF_Object* pAttr = pAttrDict->GetElementValue(name);
376             if (pAttr) {
377                 return pAttr;
378             }
379         }
380     }
381     CPDF_Object* pC = m_pDict->GetElementValue(FX_BSTRC("C"));
382     if (pC == NULL) {
383         return NULL;
384     }
385     CPDF_Dictionary* pClassMap = m_pTree->m_pTreeRoot->GetDict(FX_BSTRC("ClassMap"));
386     if (pClassMap == NULL) {
387         return NULL;
388     }
389     if (pC->GetType() == PDFOBJ_ARRAY) {
390         CPDF_Array* pArray = (CPDF_Array*)pC;
391         for (FX_DWORD i = 0; i < pArray->GetCount(); i ++) {
392             CFX_ByteString class_name = pArray->GetString(i);
393             CPDF_Dictionary* pClassDict = pClassMap->GetDict(class_name);
394             if (pClassDict && pClassDict->GetString(FX_BSTRC("O")) == owner) {
395                 return pClassDict->GetElementValue(name);
396             }
397         }
398         return NULL;
399     }
400     CFX_ByteString class_name = pC->GetString();
401     CPDF_Dictionary* pClassDict = pClassMap->GetDict(class_name);
402     if (pClassDict && pClassDict->GetString(FX_BSTRC("O")) == owner) {
403         return pClassDict->GetElementValue(name);
404     }
405     return NULL;
406 }
407 CPDF_Object* CPDF_StructElementImpl::GetAttr(FX_BSTR owner, FX_BSTR name, FX_BOOL bInheritable, int subindex)
408 {
409     CPDF_Object* pAttr = GetAttr(owner, name, bInheritable);
410     if (pAttr == NULL || subindex == -1 || pAttr->GetType() != PDFOBJ_ARRAY) {
411         return pAttr;
412     }
413     CPDF_Array* pArray = (CPDF_Array*)pAttr;
414     if (subindex >= (int)pArray->GetCount()) {
415         return pAttr;
416     }
417     return pArray->GetElementValue(subindex);
418 }
419 CFX_ByteString CPDF_StructElementImpl::GetName(FX_BSTR owner, FX_BSTR name, FX_BSTR default_value, FX_BOOL bInheritable, int subindex)
420 {
421     CPDF_Object* pAttr = GetAttr(owner, name, bInheritable, subindex);
422     if (pAttr == NULL || pAttr->GetType() != PDFOBJ_NAME) {
423         return default_value;
424     }
425     return pAttr->GetString();
426 }
427 FX_ARGB CPDF_StructElementImpl::GetColor(FX_BSTR owner, FX_BSTR name, FX_ARGB default_value, FX_BOOL bInheritable, int subindex)
428 {
429     CPDF_Object* pAttr = GetAttr(owner, name, bInheritable, subindex);
430     if (pAttr == NULL || pAttr->GetType() != PDFOBJ_ARRAY) {
431         return default_value;
432     }
433     CPDF_Array* pArray = (CPDF_Array*)pAttr;
434     return 0xff000000 | ((int)(pArray->GetNumber(0) * 255) << 16) | ((int)(pArray->GetNumber(1) * 255) << 8) | (int)(pArray->GetNumber(2) * 255);
435 }
436 FX_FLOAT CPDF_StructElementImpl::GetNumber(FX_BSTR owner, FX_BSTR name, FX_FLOAT default_value, FX_BOOL bInheritable, int subindex)
437 {
438     CPDF_Object* pAttr = GetAttr(owner, name, bInheritable, subindex);
439     if (pAttr == NULL || pAttr->GetType() != PDFOBJ_NUMBER) {
440         return default_value;
441     }
442     return pAttr->GetNumber();
443 }
444 int     CPDF_StructElementImpl::GetInteger(FX_BSTR owner, FX_BSTR name, int default_value, FX_BOOL bInheritable, int subindex)
445 {
446     CPDF_Object* pAttr = GetAttr(owner, name, bInheritable, subindex);
447     if (pAttr == NULL || pAttr->GetType() != PDFOBJ_NUMBER) {
448         return default_value;
449     }
450     return pAttr->GetInteger();
451 }