blob: b1fca65015b98b43b26e05b5456be52ea11309ac [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
Ryan Harrison275e2602017-09-18 14:23:18 -040025unsigned long WideStringToBuffer(const WideString& str,
Dan Sinclair29479f62017-04-04 10:48:19 -040026 void* buffer,
27 unsigned long buflen) {
28 if (str.IsEmpty())
29 return 0;
30
Ryan Harrison275e2602017-09-18 14:23:18 -040031 ByteString encodedStr = str.UTF16LE_Encode();
Dan Sinclair29479f62017-04-04 10:48:19 -040032 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
Dan Sinclair00d2ad12017-08-10 14:13:02 -040040FPDF_EXPORT FPDF_STRUCTTREE FPDF_CALLCONV
41FPDF_StructTree_GetForPage(FPDF_PAGE page) {
thestig9067fd62016-11-23 14:10:06 -080042 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
43 if (!pPage)
44 return nullptr;
Tom Sepez4cb82ee2017-05-22 15:15:30 -070045 return CPDF_StructTree::LoadPage(pPage->m_pDocument.Get(),
46 pPage->m_pFormDict.Get())
tsepezd5b81ce2016-12-16 14:45:46 -080047 .release();
thestig9067fd62016-11-23 14:10:06 -080048}
49
Dan Sinclair00d2ad12017-08-10 14:13:02 -040050FPDF_EXPORT void FPDF_CALLCONV
51FPDF_StructTree_Close(FPDF_STRUCTTREE struct_tree) {
dan sinclair7f389612017-04-06 13:38:54 -040052 std::unique_ptr<CPDF_StructTree>(ToStructTree(struct_tree));
thestig9067fd62016-11-23 14:10:06 -080053}
54
Dan Sinclair00d2ad12017-08-10 14:13:02 -040055FPDF_EXPORT int FPDF_CALLCONV
thestig9067fd62016-11-23 14:10:06 -080056FPDF_StructTree_CountChildren(FPDF_STRUCTTREE struct_tree) {
dan sinclair7f389612017-04-06 13:38:54 -040057 CPDF_StructTree* tree = ToStructTree(struct_tree);
thestig9067fd62016-11-23 14:10:06 -080058 return tree ? tree->CountTopElements() : -1;
59}
60
Dan Sinclair00d2ad12017-08-10 14:13:02 -040061FPDF_EXPORT FPDF_STRUCTELEMENT FPDF_CALLCONV
thestig9067fd62016-11-23 14:10:06 -080062FPDF_StructTree_GetChildAtIndex(FPDF_STRUCTTREE struct_tree, int index) {
dan sinclair7f389612017-04-06 13:38:54 -040063 CPDF_StructTree* tree = ToStructTree(struct_tree);
thestig9067fd62016-11-23 14:10:06 -080064 if (!tree || index < 0 || index >= tree->CountTopElements())
65 return nullptr;
66 return tree->GetTopElement(index);
67}
68
Dan Sinclair00d2ad12017-08-10 14:13:02 -040069FPDF_EXPORT unsigned long FPDF_CALLCONV
thestig9067fd62016-11-23 14:10:06 -080070FPDF_StructElement_GetAltText(FPDF_STRUCTELEMENT struct_element,
71 void* buffer,
72 unsigned long buflen) {
dan sinclair7f389612017-04-06 13:38:54 -040073 CPDF_StructElement* elem = ToStructTreeElement(struct_element);
Dan Sinclair29479f62017-04-04 10:48:19 -040074 return (elem && elem->GetDict())
75 ? WideStringToBuffer(elem->GetDict()->GetUnicodeTextFor("Alt"),
76 buffer, buflen)
77 : 0;
78}
thestig9067fd62016-11-23 14:10:06 -080079
Dan Sinclair00d2ad12017-08-10 14:13:02 -040080FPDF_EXPORT unsigned long FPDF_CALLCONV
Dan Sinclair29479f62017-04-04 10:48:19 -040081FPDF_StructElement_GetType(FPDF_STRUCTELEMENT struct_element,
82 void* buffer,
83 unsigned long buflen) {
dan sinclair7f389612017-04-06 13:38:54 -040084 CPDF_StructElement* elem = ToStructTreeElement(struct_element);
Dan Sinclair29479f62017-04-04 10:48:19 -040085 return elem ? WideStringToBuffer(elem->GetType().UTF8Decode(), buffer, buflen)
86 : 0;
thestig9067fd62016-11-23 14:10:06 -080087}
88
Dan Sinclair00d2ad12017-08-10 14:13:02 -040089FPDF_EXPORT unsigned long FPDF_CALLCONV
dan sinclaird9dad3a2017-04-06 14:44:02 -040090FPDF_StructElement_GetTitle(FPDF_STRUCTELEMENT struct_element,
91 void* buffer,
92 unsigned long buflen) {
93 CPDF_StructElement* elem = ToStructTreeElement(struct_element);
94 return elem
95 ? WideStringToBuffer(elem->GetTitle().UTF8Decode(), buffer, buflen)
96 : 0;
97}
98
Dan Sinclair00d2ad12017-08-10 14:13:02 -040099FPDF_EXPORT int FPDF_CALLCONV
thestig9067fd62016-11-23 14:10:06 -0800100FPDF_StructElement_CountChildren(FPDF_STRUCTELEMENT struct_element) {
dan sinclair7f389612017-04-06 13:38:54 -0400101 CPDF_StructElement* elem = ToStructTreeElement(struct_element);
thestig9067fd62016-11-23 14:10:06 -0800102 return elem ? elem->CountKids() : -1;
103}
104
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400105FPDF_EXPORT FPDF_STRUCTELEMENT FPDF_CALLCONV
thestig9067fd62016-11-23 14:10:06 -0800106FPDF_StructElement_GetChildAtIndex(FPDF_STRUCTELEMENT struct_element,
107 int index) {
dan sinclair7f389612017-04-06 13:38:54 -0400108 CPDF_StructElement* elem = ToStructTreeElement(struct_element);
thestig9067fd62016-11-23 14:10:06 -0800109 if (!elem || index < 0 || index >= elem->CountKids())
110 return nullptr;
111
tsepez0370d6b2017-01-26 12:15:34 -0800112 return elem->GetKidIfElement(index);
thestig9067fd62016-11-23 14:10:06 -0800113}