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