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 | |
| 7 | #include "core/fpdfapi/page/cpdf_page.h" |
| 8 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
| 9 | #include "core/fpdfdoc/fpdf_tagged.h" |
| 10 | #include "fpdfsdk/fsdk_define.h" |
| 11 | |
| 12 | namespace { |
| 13 | |
| 14 | IPDF_StructTree* ToStructTree(FPDF_STRUCTTREE struct_tree) { |
| 15 | return reinterpret_cast<IPDF_StructTree*>(struct_tree); |
| 16 | } |
| 17 | |
| 18 | IPDF_StructElement* ToStructTreeElement(FPDF_STRUCTELEMENT struct_element) { |
| 19 | return reinterpret_cast<IPDF_StructElement*>(struct_element); |
| 20 | } |
| 21 | |
| 22 | } // namespace |
| 23 | |
| 24 | DLLEXPORT FPDF_STRUCTTREE STDCALL FPDF_StructTree_GetForPage(FPDF_PAGE page) { |
| 25 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 26 | if (!pPage) |
| 27 | return nullptr; |
tsepez | d5b81ce | 2016-12-16 14:45:46 -0800 | [diff] [blame] | 28 | return IPDF_StructTree::LoadPage(pPage->m_pDocument, pPage->m_pFormDict) |
| 29 | .release(); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | DLLEXPORT void STDCALL FPDF_StructTree_Close(FPDF_STRUCTTREE struct_tree) { |
| 33 | delete ToStructTree(struct_tree); |
| 34 | } |
| 35 | |
| 36 | DLLEXPORT int STDCALL |
| 37 | FPDF_StructTree_CountChildren(FPDF_STRUCTTREE struct_tree) { |
| 38 | IPDF_StructTree* tree = ToStructTree(struct_tree); |
| 39 | return tree ? tree->CountTopElements() : -1; |
| 40 | } |
| 41 | |
| 42 | DLLEXPORT FPDF_STRUCTELEMENT STDCALL |
| 43 | FPDF_StructTree_GetChildAtIndex(FPDF_STRUCTTREE struct_tree, int index) { |
| 44 | IPDF_StructTree* tree = ToStructTree(struct_tree); |
| 45 | if (!tree || index < 0 || index >= tree->CountTopElements()) |
| 46 | return nullptr; |
| 47 | return tree->GetTopElement(index); |
| 48 | } |
| 49 | |
| 50 | DLLEXPORT unsigned long STDCALL |
| 51 | FPDF_StructElement_GetAltText(FPDF_STRUCTELEMENT struct_element, |
| 52 | void* buffer, |
| 53 | unsigned long buflen) { |
| 54 | IPDF_StructElement* elem = ToStructTreeElement(struct_element); |
| 55 | if (!elem) |
| 56 | return 0; |
| 57 | |
| 58 | CPDF_Dictionary* dict = elem->GetDict(); |
| 59 | if (!dict) |
| 60 | return 0; |
| 61 | |
| 62 | CFX_WideString str = elem->GetDict()->GetUnicodeTextFor("Alt"); |
| 63 | if (str.IsEmpty()) |
| 64 | return 0; |
| 65 | |
| 66 | CFX_ByteString encodedStr = str.UTF16LE_Encode(); |
| 67 | const unsigned long len = encodedStr.GetLength(); |
| 68 | if (buffer && len <= buflen) |
| 69 | FXSYS_memcpy(buffer, encodedStr.c_str(), len); |
| 70 | return len; |
| 71 | } |
| 72 | |
| 73 | DLLEXPORT int STDCALL |
| 74 | FPDF_StructElement_CountChildren(FPDF_STRUCTELEMENT struct_element) { |
| 75 | IPDF_StructElement* elem = ToStructTreeElement(struct_element); |
| 76 | return elem ? elem->CountKids() : -1; |
| 77 | } |
| 78 | |
| 79 | DLLEXPORT FPDF_STRUCTELEMENT STDCALL |
| 80 | FPDF_StructElement_GetChildAtIndex(FPDF_STRUCTELEMENT struct_element, |
| 81 | int index) { |
| 82 | IPDF_StructElement* elem = ToStructTreeElement(struct_element); |
| 83 | if (!elem || index < 0 || index >= elem->CountKids()) |
| 84 | return nullptr; |
| 85 | |
tsepez | 0370d6b | 2017-01-26 12:15:34 -0800 | [diff] [blame] | 86 | return elem->GetKidIfElement(index); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 87 | } |