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 | |
| 24 | } // namespace |
| 25 | |
| 26 | DLLEXPORT FPDF_STRUCTTREE STDCALL FPDF_StructTree_GetForPage(FPDF_PAGE page) { |
| 27 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 28 | if (!pPage) |
| 29 | return nullptr; |
tsepez | d5b81ce | 2016-12-16 14:45:46 -0800 | [diff] [blame] | 30 | return IPDF_StructTree::LoadPage(pPage->m_pDocument, pPage->m_pFormDict) |
| 31 | .release(); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | DLLEXPORT void STDCALL FPDF_StructTree_Close(FPDF_STRUCTTREE struct_tree) { |
Tom Sepez | f4a8c72 | 2017-01-26 16:37:20 -0800 | [diff] [blame] | 35 | std::unique_ptr<IPDF_StructTree>(ToStructTree(struct_tree)); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | DLLEXPORT int STDCALL |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 | |
tsepez | 0370d6b | 2017-01-26 12:15:34 -0800 | [diff] [blame] | 88 | return elem->GetKidIfElement(index); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 89 | } |