thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 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 | |
Tom Sepez | f4a8c72 | 2017-01-26 16:37:20 -0800 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 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 | |
| 16 | IPDF_StructTree* ToStructTree(FPDF_STRUCTTREE struct_tree) { |
| 17 | return reinterpret_cast<IPDF_StructTree*>(struct_tree); |
| 18 | } |
| 19 | |
| 20 | IPDF_StructElement* ToStructTreeElement(FPDF_STRUCTELEMENT struct_element) { |
| 21 | return reinterpret_cast<IPDF_StructElement*>(struct_element); |
| 22 | } |
| 23 | |
Dan Sinclair | 29479f6 | 2017-04-04 10:48:19 -0400 | [diff] [blame^] | 24 | unsigned long WideStringToBuffer(const CFX_WideString& str, |
| 25 | void* buffer, |
| 26 | unsigned long buflen) { |
| 27 | if (str.IsEmpty()) |
| 28 | return 0; |
| 29 | |
| 30 | CFX_ByteString encodedStr = str.UTF16LE_Encode(); |
| 31 | const unsigned long len = encodedStr.GetLength(); |
| 32 | if (buffer && len <= buflen) |
| 33 | memcpy(buffer, encodedStr.c_str(), len); |
| 34 | return len; |
| 35 | } |
| 36 | |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 37 | } // namespace |
| 38 | |
| 39 | DLLEXPORT FPDF_STRUCTTREE STDCALL FPDF_StructTree_GetForPage(FPDF_PAGE page) { |
| 40 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 41 | if (!pPage) |
| 42 | return nullptr; |
tsepez | d5b81ce | 2016-12-16 14:45:46 -0800 | [diff] [blame] | 43 | return IPDF_StructTree::LoadPage(pPage->m_pDocument, pPage->m_pFormDict) |
| 44 | .release(); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | DLLEXPORT void STDCALL FPDF_StructTree_Close(FPDF_STRUCTTREE struct_tree) { |
Tom Sepez | f4a8c72 | 2017-01-26 16:37:20 -0800 | [diff] [blame] | 48 | std::unique_ptr<IPDF_StructTree>(ToStructTree(struct_tree)); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | DLLEXPORT int STDCALL |
| 52 | FPDF_StructTree_CountChildren(FPDF_STRUCTTREE struct_tree) { |
| 53 | IPDF_StructTree* tree = ToStructTree(struct_tree); |
| 54 | return tree ? tree->CountTopElements() : -1; |
| 55 | } |
| 56 | |
| 57 | DLLEXPORT FPDF_STRUCTELEMENT STDCALL |
| 58 | FPDF_StructTree_GetChildAtIndex(FPDF_STRUCTTREE struct_tree, int index) { |
| 59 | IPDF_StructTree* tree = ToStructTree(struct_tree); |
| 60 | if (!tree || index < 0 || index >= tree->CountTopElements()) |
| 61 | return nullptr; |
| 62 | return tree->GetTopElement(index); |
| 63 | } |
| 64 | |
| 65 | DLLEXPORT unsigned long STDCALL |
| 66 | FPDF_StructElement_GetAltText(FPDF_STRUCTELEMENT struct_element, |
| 67 | void* buffer, |
| 68 | unsigned long buflen) { |
| 69 | IPDF_StructElement* elem = ToStructTreeElement(struct_element); |
Dan Sinclair | 29479f6 | 2017-04-04 10:48:19 -0400 | [diff] [blame^] | 70 | return (elem && elem->GetDict()) |
| 71 | ? WideStringToBuffer(elem->GetDict()->GetUnicodeTextFor("Alt"), |
| 72 | buffer, buflen) |
| 73 | : 0; |
| 74 | } |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 75 | |
Dan Sinclair | 29479f6 | 2017-04-04 10:48:19 -0400 | [diff] [blame^] | 76 | DLLEXPORT unsigned long STDCALL |
| 77 | FPDF_StructElement_GetType(FPDF_STRUCTELEMENT struct_element, |
| 78 | void* buffer, |
| 79 | unsigned long buflen) { |
| 80 | IPDF_StructElement* elem = ToStructTreeElement(struct_element); |
| 81 | return elem ? WideStringToBuffer(elem->GetType().UTF8Decode(), buffer, buflen) |
| 82 | : 0; |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | DLLEXPORT int STDCALL |
| 86 | FPDF_StructElement_CountChildren(FPDF_STRUCTELEMENT struct_element) { |
| 87 | IPDF_StructElement* elem = ToStructTreeElement(struct_element); |
| 88 | return elem ? elem->CountKids() : -1; |
| 89 | } |
| 90 | |
| 91 | DLLEXPORT FPDF_STRUCTELEMENT STDCALL |
| 92 | FPDF_StructElement_GetChildAtIndex(FPDF_STRUCTELEMENT struct_element, |
| 93 | int index) { |
| 94 | IPDF_StructElement* elem = ToStructTreeElement(struct_element); |
| 95 | if (!elem || index < 0 || index >= elem->CountKids()) |
| 96 | return nullptr; |
| 97 | |
tsepez | 0370d6b | 2017-01-26 12:15:34 -0800 | [diff] [blame] | 98 | return elem->GetKidIfElement(index); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 99 | } |