OLD | NEW |
(Empty) | |
| 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; |
| 28 return IPDF_StructTree::LoadPage(pPage->m_pDocument, pPage->m_pFormDict); |
| 29 } |
| 30 |
| 31 DLLEXPORT void STDCALL FPDF_StructTree_Close(FPDF_STRUCTTREE struct_tree) { |
| 32 delete ToStructTree(struct_tree); |
| 33 } |
| 34 |
| 35 DLLEXPORT int STDCALL |
| 36 FPDF_StructTree_CountChildren(FPDF_STRUCTTREE struct_tree) { |
| 37 IPDF_StructTree* tree = ToStructTree(struct_tree); |
| 38 return tree ? tree->CountTopElements() : -1; |
| 39 } |
| 40 |
| 41 DLLEXPORT FPDF_STRUCTELEMENT STDCALL |
| 42 FPDF_StructTree_GetChildAtIndex(FPDF_STRUCTTREE struct_tree, int index) { |
| 43 IPDF_StructTree* tree = ToStructTree(struct_tree); |
| 44 if (!tree || index < 0 || index >= tree->CountTopElements()) |
| 45 return nullptr; |
| 46 return tree->GetTopElement(index); |
| 47 } |
| 48 |
| 49 DLLEXPORT unsigned long STDCALL |
| 50 FPDF_StructElement_GetAltText(FPDF_STRUCTELEMENT struct_element, |
| 51 void* buffer, |
| 52 unsigned long buflen) { |
| 53 IPDF_StructElement* elem = ToStructTreeElement(struct_element); |
| 54 if (!elem) |
| 55 return 0; |
| 56 |
| 57 CPDF_Dictionary* dict = elem->GetDict(); |
| 58 if (!dict) |
| 59 return 0; |
| 60 |
| 61 CFX_WideString str = elem->GetDict()->GetUnicodeTextFor("Alt"); |
| 62 if (str.IsEmpty()) |
| 63 return 0; |
| 64 |
| 65 CFX_ByteString encodedStr = str.UTF16LE_Encode(); |
| 66 const unsigned long len = encodedStr.GetLength(); |
| 67 if (buffer && len <= buflen) |
| 68 FXSYS_memcpy(buffer, encodedStr.c_str(), len); |
| 69 return len; |
| 70 } |
| 71 |
| 72 DLLEXPORT int STDCALL |
| 73 FPDF_StructElement_CountChildren(FPDF_STRUCTELEMENT struct_element) { |
| 74 IPDF_StructElement* elem = ToStructTreeElement(struct_element); |
| 75 return elem ? elem->CountKids() : -1; |
| 76 } |
| 77 |
| 78 DLLEXPORT FPDF_STRUCTELEMENT STDCALL |
| 79 FPDF_StructElement_GetChildAtIndex(FPDF_STRUCTELEMENT struct_element, |
| 80 int index) { |
| 81 IPDF_StructElement* elem = ToStructTreeElement(struct_element); |
| 82 if (!elem || index < 0 || index >= elem->CountKids()) |
| 83 return nullptr; |
| 84 |
| 85 CPDF_StructKid kid = elem->GetKid(index); |
| 86 return kid.m_Type == CPDF_StructKid::Element ? kid.m_Element.m_pElement |
| 87 : nullptr; |
| 88 } |
OLD | NEW |