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) { |
| 18 | return reinterpret_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) { |
| 22 | return reinterpret_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; |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame^] | 44 | return CPDF_StructTree::LoadPage(pPage->m_pDocument, pPage->m_pFormDict) |
tsepez | d5b81ce | 2016-12-16 14:45:46 -0800 | [diff] [blame] | 45 | .release(); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | DLLEXPORT void STDCALL FPDF_StructTree_Close(FPDF_STRUCTTREE struct_tree) { |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame^] | 49 | std::unique_ptr<CPDF_StructTree>(ToStructTree(struct_tree)); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | DLLEXPORT int STDCALL |
| 53 | FPDF_StructTree_CountChildren(FPDF_STRUCTTREE struct_tree) { |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame^] | 54 | CPDF_StructTree* tree = ToStructTree(struct_tree); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 55 | return tree ? tree->CountTopElements() : -1; |
| 56 | } |
| 57 | |
| 58 | DLLEXPORT FPDF_STRUCTELEMENT STDCALL |
| 59 | FPDF_StructTree_GetChildAtIndex(FPDF_STRUCTTREE struct_tree, int index) { |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame^] | 60 | CPDF_StructTree* tree = ToStructTree(struct_tree); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 61 | if (!tree || index < 0 || index >= tree->CountTopElements()) |
| 62 | return nullptr; |
| 63 | return tree->GetTopElement(index); |
| 64 | } |
| 65 | |
| 66 | DLLEXPORT unsigned long STDCALL |
| 67 | FPDF_StructElement_GetAltText(FPDF_STRUCTELEMENT struct_element, |
| 68 | void* buffer, |
| 69 | unsigned long buflen) { |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame^] | 70 | CPDF_StructElement* elem = ToStructTreeElement(struct_element); |
Dan Sinclair | 29479f6 | 2017-04-04 10:48:19 -0400 | [diff] [blame] | 71 | return (elem && elem->GetDict()) |
| 72 | ? WideStringToBuffer(elem->GetDict()->GetUnicodeTextFor("Alt"), |
| 73 | buffer, buflen) |
| 74 | : 0; |
| 75 | } |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 76 | |
Dan Sinclair | 29479f6 | 2017-04-04 10:48:19 -0400 | [diff] [blame] | 77 | DLLEXPORT unsigned long STDCALL |
| 78 | FPDF_StructElement_GetType(FPDF_STRUCTELEMENT struct_element, |
| 79 | void* buffer, |
| 80 | unsigned long buflen) { |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame^] | 81 | CPDF_StructElement* elem = ToStructTreeElement(struct_element); |
Dan Sinclair | 29479f6 | 2017-04-04 10:48:19 -0400 | [diff] [blame] | 82 | return elem ? WideStringToBuffer(elem->GetType().UTF8Decode(), buffer, buflen) |
| 83 | : 0; |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | DLLEXPORT int STDCALL |
| 87 | FPDF_StructElement_CountChildren(FPDF_STRUCTELEMENT struct_element) { |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame^] | 88 | CPDF_StructElement* elem = ToStructTreeElement(struct_element); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 89 | return elem ? elem->CountKids() : -1; |
| 90 | } |
| 91 | |
| 92 | DLLEXPORT FPDF_STRUCTELEMENT STDCALL |
| 93 | FPDF_StructElement_GetChildAtIndex(FPDF_STRUCTELEMENT struct_element, |
| 94 | int index) { |
dan sinclair | 7f38961 | 2017-04-06 13:38:54 -0400 | [diff] [blame^] | 95 | CPDF_StructElement* elem = ToStructTreeElement(struct_element); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 96 | if (!elem || index < 0 || index >= elem->CountKids()) |
| 97 | return nullptr; |
| 98 | |
tsepez | 0370d6b | 2017-01-26 12:15:34 -0800 | [diff] [blame] | 99 | return elem->GetKidIfElement(index); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 100 | } |