blob: bd6641f7cbdca813222fca19e2c5a4e9a01ce570 [file] [log] [blame]
thestig9067fd62016-11-23 14:10:06 -08001// 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
Tom Sepezf4a8c722017-01-26 16:37:20 -08007#include <memory>
8
thestig9067fd62016-11-23 14:10:06 -08009#include "core/fpdfapi/page/cpdf_page.h"
10#include "core/fpdfapi/parser/cpdf_dictionary.h"
dan sinclair7f389612017-04-06 13:38:54 -040011#include "core/fpdfdoc/cpdf_structelement.h"
12#include "core/fpdfdoc/cpdf_structtree.h"
thestig9067fd62016-11-23 14:10:06 -080013#include "fpdfsdk/fsdk_define.h"
14
15namespace {
16
dan sinclair7f389612017-04-06 13:38:54 -040017CPDF_StructTree* ToStructTree(FPDF_STRUCTTREE struct_tree) {
Nicolas Pena46abb662017-05-17 17:23:22 -040018 return static_cast<CPDF_StructTree*>(struct_tree);
thestig9067fd62016-11-23 14:10:06 -080019}
20
dan sinclair7f389612017-04-06 13:38:54 -040021CPDF_StructElement* ToStructTreeElement(FPDF_STRUCTELEMENT struct_element) {
Nicolas Pena46abb662017-05-17 17:23:22 -040022 return static_cast<CPDF_StructElement*>(struct_element);
thestig9067fd62016-11-23 14:10:06 -080023}
24
Dan Sinclair29479f62017-04-04 10:48:19 -040025unsigned long WideStringToBuffer(const CFX_WideString& str,
26 void* buffer,
27 unsigned long buflen) {
28 if (str.IsEmpty())
29 return 0;
30
31 CFX_ByteString encodedStr = str.UTF16LE_Encode();
32 const unsigned long len = encodedStr.GetLength();
33 if (buffer && len <= buflen)
34 memcpy(buffer, encodedStr.c_str(), len);
35 return len;
36}
37
thestig9067fd62016-11-23 14:10:06 -080038} // namespace
39
40DLLEXPORT FPDF_STRUCTTREE STDCALL FPDF_StructTree_GetForPage(FPDF_PAGE page) {
41 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
42 if (!pPage)
43 return nullptr;
Tom Sepez4cb82ee2017-05-22 15:15:30 -070044 return CPDF_StructTree::LoadPage(pPage->m_pDocument.Get(),
45 pPage->m_pFormDict.Get())
tsepezd5b81ce2016-12-16 14:45:46 -080046 .release();
thestig9067fd62016-11-23 14:10:06 -080047}
48
49DLLEXPORT void STDCALL FPDF_StructTree_Close(FPDF_STRUCTTREE struct_tree) {
dan sinclair7f389612017-04-06 13:38:54 -040050 std::unique_ptr<CPDF_StructTree>(ToStructTree(struct_tree));
thestig9067fd62016-11-23 14:10:06 -080051}
52
53DLLEXPORT int STDCALL
54FPDF_StructTree_CountChildren(FPDF_STRUCTTREE struct_tree) {
dan sinclair7f389612017-04-06 13:38:54 -040055 CPDF_StructTree* tree = ToStructTree(struct_tree);
thestig9067fd62016-11-23 14:10:06 -080056 return tree ? tree->CountTopElements() : -1;
57}
58
59DLLEXPORT FPDF_STRUCTELEMENT STDCALL
60FPDF_StructTree_GetChildAtIndex(FPDF_STRUCTTREE struct_tree, int index) {
dan sinclair7f389612017-04-06 13:38:54 -040061 CPDF_StructTree* tree = ToStructTree(struct_tree);
thestig9067fd62016-11-23 14:10:06 -080062 if (!tree || index < 0 || index >= tree->CountTopElements())
63 return nullptr;
64 return tree->GetTopElement(index);
65}
66
67DLLEXPORT unsigned long STDCALL
68FPDF_StructElement_GetAltText(FPDF_STRUCTELEMENT struct_element,
69 void* buffer,
70 unsigned long buflen) {
dan sinclair7f389612017-04-06 13:38:54 -040071 CPDF_StructElement* elem = ToStructTreeElement(struct_element);
Dan Sinclair29479f62017-04-04 10:48:19 -040072 return (elem && elem->GetDict())
73 ? WideStringToBuffer(elem->GetDict()->GetUnicodeTextFor("Alt"),
74 buffer, buflen)
75 : 0;
76}
thestig9067fd62016-11-23 14:10:06 -080077
Dan Sinclair29479f62017-04-04 10:48:19 -040078DLLEXPORT unsigned long STDCALL
79FPDF_StructElement_GetType(FPDF_STRUCTELEMENT struct_element,
80 void* buffer,
81 unsigned long buflen) {
dan sinclair7f389612017-04-06 13:38:54 -040082 CPDF_StructElement* elem = ToStructTreeElement(struct_element);
Dan Sinclair29479f62017-04-04 10:48:19 -040083 return elem ? WideStringToBuffer(elem->GetType().UTF8Decode(), buffer, buflen)
84 : 0;
thestig9067fd62016-11-23 14:10:06 -080085}
86
dan sinclaird9dad3a2017-04-06 14:44:02 -040087DLLEXPORT unsigned long STDCALL
88FPDF_StructElement_GetTitle(FPDF_STRUCTELEMENT struct_element,
89 void* buffer,
90 unsigned long buflen) {
91 CPDF_StructElement* elem = ToStructTreeElement(struct_element);
92 return elem
93 ? WideStringToBuffer(elem->GetTitle().UTF8Decode(), buffer, buflen)
94 : 0;
95}
96
thestig9067fd62016-11-23 14:10:06 -080097DLLEXPORT int STDCALL
98FPDF_StructElement_CountChildren(FPDF_STRUCTELEMENT struct_element) {
dan sinclair7f389612017-04-06 13:38:54 -040099 CPDF_StructElement* elem = ToStructTreeElement(struct_element);
thestig9067fd62016-11-23 14:10:06 -0800100 return elem ? elem->CountKids() : -1;
101}
102
103DLLEXPORT FPDF_STRUCTELEMENT STDCALL
104FPDF_StructElement_GetChildAtIndex(FPDF_STRUCTELEMENT struct_element,
105 int index) {
dan sinclair7f389612017-04-06 13:38:54 -0400106 CPDF_StructElement* elem = ToStructTreeElement(struct_element);
thestig9067fd62016-11-23 14:10:06 -0800107 if (!elem || index < 0 || index >= elem->CountKids())
108 return nullptr;
109
tsepez0370d6b2017-01-26 12:15:34 -0800110 return elem->GetKidIfElement(index);
thestig9067fd62016-11-23 14:10:06 -0800111}