blob: 96d40b41c23198afa13c5b845f43aaff91c66863 [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) {
18 return reinterpret_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) {
22 return reinterpret_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;
dan sinclair7f389612017-04-06 13:38:54 -040044 return CPDF_StructTree::LoadPage(pPage->m_pDocument, pPage->m_pFormDict)
tsepezd5b81ce2016-12-16 14:45:46 -080045 .release();
thestig9067fd62016-11-23 14:10:06 -080046}
47
48DLLEXPORT void STDCALL FPDF_StructTree_Close(FPDF_STRUCTTREE struct_tree) {
dan sinclair7f389612017-04-06 13:38:54 -040049 std::unique_ptr<CPDF_StructTree>(ToStructTree(struct_tree));
thestig9067fd62016-11-23 14:10:06 -080050}
51
52DLLEXPORT int STDCALL
53FPDF_StructTree_CountChildren(FPDF_STRUCTTREE struct_tree) {
dan sinclair7f389612017-04-06 13:38:54 -040054 CPDF_StructTree* tree = ToStructTree(struct_tree);
thestig9067fd62016-11-23 14:10:06 -080055 return tree ? tree->CountTopElements() : -1;
56}
57
58DLLEXPORT FPDF_STRUCTELEMENT STDCALL
59FPDF_StructTree_GetChildAtIndex(FPDF_STRUCTTREE struct_tree, int index) {
dan sinclair7f389612017-04-06 13:38:54 -040060 CPDF_StructTree* tree = ToStructTree(struct_tree);
thestig9067fd62016-11-23 14:10:06 -080061 if (!tree || index < 0 || index >= tree->CountTopElements())
62 return nullptr;
63 return tree->GetTopElement(index);
64}
65
66DLLEXPORT unsigned long STDCALL
67FPDF_StructElement_GetAltText(FPDF_STRUCTELEMENT struct_element,
68 void* buffer,
69 unsigned long buflen) {
dan sinclair7f389612017-04-06 13:38:54 -040070 CPDF_StructElement* elem = ToStructTreeElement(struct_element);
Dan Sinclair29479f62017-04-04 10:48:19 -040071 return (elem && elem->GetDict())
72 ? WideStringToBuffer(elem->GetDict()->GetUnicodeTextFor("Alt"),
73 buffer, buflen)
74 : 0;
75}
thestig9067fd62016-11-23 14:10:06 -080076
Dan Sinclair29479f62017-04-04 10:48:19 -040077DLLEXPORT unsigned long STDCALL
78FPDF_StructElement_GetType(FPDF_STRUCTELEMENT struct_element,
79 void* buffer,
80 unsigned long buflen) {
dan sinclair7f389612017-04-06 13:38:54 -040081 CPDF_StructElement* elem = ToStructTreeElement(struct_element);
Dan Sinclair29479f62017-04-04 10:48:19 -040082 return elem ? WideStringToBuffer(elem->GetType().UTF8Decode(), buffer, buflen)
83 : 0;
thestig9067fd62016-11-23 14:10:06 -080084}
85
86DLLEXPORT int STDCALL
87FPDF_StructElement_CountChildren(FPDF_STRUCTELEMENT struct_element) {
dan sinclair7f389612017-04-06 13:38:54 -040088 CPDF_StructElement* elem = ToStructTreeElement(struct_element);
thestig9067fd62016-11-23 14:10:06 -080089 return elem ? elem->CountKids() : -1;
90}
91
92DLLEXPORT FPDF_STRUCTELEMENT STDCALL
93FPDF_StructElement_GetChildAtIndex(FPDF_STRUCTELEMENT struct_element,
94 int index) {
dan sinclair7f389612017-04-06 13:38:54 -040095 CPDF_StructElement* elem = ToStructTreeElement(struct_element);
thestig9067fd62016-11-23 14:10:06 -080096 if (!elem || index < 0 || index >= elem->CountKids())
97 return nullptr;
98
tsepez0370d6b2017-01-26 12:15:34 -080099 return elem->GetKidIfElement(index);
thestig9067fd62016-11-23 14:10:06 -0800100}