• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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 #include "public/fpdf_structtree.h"
6 
7 #include <memory>
8 
9 #include "core/fpdfapi/page/cpdf_page.h"
10 #include "core/fpdfapi/parser/cpdf_dictionary.h"
11 #include "core/fpdfdoc/fpdf_tagged.h"
12 #include "fpdfsdk/fsdk_define.h"
13 
14 namespace {
15 
ToStructTree(FPDF_STRUCTTREE struct_tree)16 IPDF_StructTree* ToStructTree(FPDF_STRUCTTREE struct_tree) {
17   return reinterpret_cast<IPDF_StructTree*>(struct_tree);
18 }
19 
ToStructTreeElement(FPDF_STRUCTELEMENT struct_element)20 IPDF_StructElement* ToStructTreeElement(FPDF_STRUCTELEMENT struct_element) {
21   return reinterpret_cast<IPDF_StructElement*>(struct_element);
22 }
23 
24 }  // namespace
25 
FPDF_StructTree_GetForPage(FPDF_PAGE page)26 DLLEXPORT FPDF_STRUCTTREE STDCALL FPDF_StructTree_GetForPage(FPDF_PAGE page) {
27   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
28   if (!pPage)
29     return nullptr;
30   return IPDF_StructTree::LoadPage(pPage->m_pDocument, pPage->m_pFormDict)
31       .release();
32 }
33 
FPDF_StructTree_Close(FPDF_STRUCTTREE struct_tree)34 DLLEXPORT void STDCALL FPDF_StructTree_Close(FPDF_STRUCTTREE struct_tree) {
35   std::unique_ptr<IPDF_StructTree>(ToStructTree(struct_tree));
36 }
37 
38 DLLEXPORT int STDCALL
FPDF_StructTree_CountChildren(FPDF_STRUCTTREE struct_tree)39 FPDF_StructTree_CountChildren(FPDF_STRUCTTREE struct_tree) {
40   IPDF_StructTree* tree = ToStructTree(struct_tree);
41   return tree ? tree->CountTopElements() : -1;
42 }
43 
44 DLLEXPORT FPDF_STRUCTELEMENT STDCALL
FPDF_StructTree_GetChildAtIndex(FPDF_STRUCTTREE struct_tree,int index)45 FPDF_StructTree_GetChildAtIndex(FPDF_STRUCTTREE struct_tree, int index) {
46   IPDF_StructTree* tree = ToStructTree(struct_tree);
47   if (!tree || index < 0 || index >= tree->CountTopElements())
48     return nullptr;
49   return tree->GetTopElement(index);
50 }
51 
52 DLLEXPORT unsigned long STDCALL
FPDF_StructElement_GetAltText(FPDF_STRUCTELEMENT struct_element,void * buffer,unsigned long buflen)53 FPDF_StructElement_GetAltText(FPDF_STRUCTELEMENT struct_element,
54                               void* buffer,
55                               unsigned long buflen) {
56   IPDF_StructElement* elem = ToStructTreeElement(struct_element);
57   if (!elem)
58     return 0;
59 
60   CPDF_Dictionary* dict = elem->GetDict();
61   if (!dict)
62     return 0;
63 
64   CFX_WideString str = elem->GetDict()->GetUnicodeTextFor("Alt");
65   if (str.IsEmpty())
66     return 0;
67 
68   CFX_ByteString encodedStr = str.UTF16LE_Encode();
69   const unsigned long len = encodedStr.GetLength();
70   if (buffer && len <= buflen)
71     FXSYS_memcpy(buffer, encodedStr.c_str(), len);
72   return len;
73 }
74 
75 DLLEXPORT int STDCALL
FPDF_StructElement_CountChildren(FPDF_STRUCTELEMENT struct_element)76 FPDF_StructElement_CountChildren(FPDF_STRUCTELEMENT struct_element) {
77   IPDF_StructElement* elem = ToStructTreeElement(struct_element);
78   return elem ? elem->CountKids() : -1;
79 }
80 
81 DLLEXPORT FPDF_STRUCTELEMENT STDCALL
FPDF_StructElement_GetChildAtIndex(FPDF_STRUCTELEMENT struct_element,int index)82 FPDF_StructElement_GetChildAtIndex(FPDF_STRUCTELEMENT struct_element,
83                                    int index) {
84   IPDF_StructElement* elem = ToStructTreeElement(struct_element);
85   if (!elem || index < 0 || index >= elem->CountKids())
86     return nullptr;
87 
88   return elem->GetKidIfElement(index);
89 }
90