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" |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame] | 11 | #include "core/fpdfdoc/cpdf_structelement.h" |
| 12 | #include "core/fpdfdoc/cpdf_structtree.h" |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 13 | #include "fpdfsdk/fsdk_define.h" |
| 14 | |
| 15 | namespace { |
| 16 | |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame] | 17 | CPDF_StructTree* ToStructTree(FPDF_STRUCTTREE struct_tree) { |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 18 | return static_cast<CPDF_StructTree*>(struct_tree); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 19 | } |
| 20 | |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame] | 21 | CPDF_StructElement* ToStructTreeElement(FPDF_STRUCTELEMENT struct_element) { |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 22 | return static_cast<CPDF_StructElement*>(struct_element); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 23 | } |
| 24 | |
Dan Sinclair | 29479f6 | 2017-04-04 10:48:19 -0400 | [diff] [blame] | 25 | unsigned 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 | |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 38 | } // namespace |
| 39 | |
| 40 | DLLEXPORT FPDF_STRUCTTREE STDCALL FPDF_StructTree_GetForPage(FPDF_PAGE page) { |
| 41 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 42 | if (!pPage) |
| 43 | return nullptr; |
Tom Sepez | 4cb82ee | 2017-05-22 15:15:30 -0700 | [diff] [blame] | 44 | return CPDF_StructTree::LoadPage(pPage->m_pDocument.Get(), |
| 45 | pPage->m_pFormDict.Get()) |
tsepez | d5b81ce | 2016-12-16 14:45:46 -0800 | [diff] [blame] | 46 | .release(); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | DLLEXPORT void STDCALL FPDF_StructTree_Close(FPDF_STRUCTTREE struct_tree) { |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame] | 50 | std::unique_ptr<CPDF_StructTree>(ToStructTree(struct_tree)); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | DLLEXPORT int STDCALL |
| 54 | FPDF_StructTree_CountChildren(FPDF_STRUCTTREE struct_tree) { |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame] | 55 | CPDF_StructTree* tree = ToStructTree(struct_tree); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 56 | return tree ? tree->CountTopElements() : -1; |
| 57 | } |
| 58 | |
| 59 | DLLEXPORT FPDF_STRUCTELEMENT STDCALL |
| 60 | FPDF_StructTree_GetChildAtIndex(FPDF_STRUCTTREE struct_tree, int index) { |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame] | 61 | CPDF_StructTree* tree = ToStructTree(struct_tree); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 62 | if (!tree || index < 0 || index >= tree->CountTopElements()) |
| 63 | return nullptr; |
| 64 | return tree->GetTopElement(index); |
| 65 | } |
| 66 | |
| 67 | DLLEXPORT unsigned long STDCALL |
| 68 | FPDF_StructElement_GetAltText(FPDF_STRUCTELEMENT struct_element, |
| 69 | void* buffer, |
| 70 | unsigned long buflen) { |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame] | 71 | CPDF_StructElement* elem = ToStructTreeElement(struct_element); |
Dan Sinclair | 29479f6 | 2017-04-04 10:48:19 -0400 | [diff] [blame] | 72 | return (elem && elem->GetDict()) |
| 73 | ? WideStringToBuffer(elem->GetDict()->GetUnicodeTextFor("Alt"), |
| 74 | buffer, buflen) |
| 75 | : 0; |
| 76 | } |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 77 | |
Dan Sinclair | 29479f6 | 2017-04-04 10:48:19 -0400 | [diff] [blame] | 78 | DLLEXPORT unsigned long STDCALL |
| 79 | FPDF_StructElement_GetType(FPDF_STRUCTELEMENT struct_element, |
| 80 | void* buffer, |
| 81 | unsigned long buflen) { |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame] | 82 | CPDF_StructElement* elem = ToStructTreeElement(struct_element); |
Dan Sinclair | 29479f6 | 2017-04-04 10:48:19 -0400 | [diff] [blame] | 83 | return elem ? WideStringToBuffer(elem->GetType().UTF8Decode(), buffer, buflen) |
| 84 | : 0; |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 85 | } |
| 86 | |
dan sinclair | d9dad3a | 2017-04-06 14:44:02 -0400 | [diff] [blame] | 87 | DLLEXPORT unsigned long STDCALL |
| 88 | FPDF_StructElement_GetTitle(FPDF_STRUCTELEMENT struct_element, |
| 89 | void* buffer, |
| 90 | unsigned long buflen) { |
| 91 | CPDF_StructElement* elem = ToStructTreeElement(struct_element); |
| 92 | return elem |
| 93 | ? WideStringToBuffer(elem->GetTitle().UTF8Decode(), buffer, buflen) |
| 94 | : 0; |
| 95 | } |
| 96 | |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 97 | DLLEXPORT int STDCALL |
| 98 | FPDF_StructElement_CountChildren(FPDF_STRUCTELEMENT struct_element) { |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame] | 99 | CPDF_StructElement* elem = ToStructTreeElement(struct_element); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 100 | return elem ? elem->CountKids() : -1; |
| 101 | } |
| 102 | |
| 103 | DLLEXPORT FPDF_STRUCTELEMENT STDCALL |
| 104 | FPDF_StructElement_GetChildAtIndex(FPDF_STRUCTELEMENT struct_element, |
| 105 | int index) { |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame] | 106 | CPDF_StructElement* elem = ToStructTreeElement(struct_element); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 107 | if (!elem || index < 0 || index >= elem->CountKids()) |
| 108 | return nullptr; |
| 109 | |
tsepez | 0370d6b | 2017-01-26 12:15:34 -0800 | [diff] [blame] | 110 | return elem->GetKidIfElement(index); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 111 | } |