Chromium Code Reviews| Index: fpdfsdk/fpdf_structtree.cpp |
| diff --git a/fpdfsdk/fpdf_structtree.cpp b/fpdfsdk/fpdf_structtree.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d28f7941bd5c144878bfd1854c14d7bd383ac091 |
| --- /dev/null |
| +++ b/fpdfsdk/fpdf_structtree.cpp |
| @@ -0,0 +1,79 @@ |
| +// Copyright 2016 PDFium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "public/fpdf_structtree.h" |
| + |
| +#include "core/fpdfapi/page/cpdf_page.h" |
| +#include "core/fpdfapi/parser/cpdf_dictionary.h" |
| +#include "core/fpdfdoc/fpdf_tagged.h" |
| +#include "fpdfsdk/fsdk_define.h" |
| + |
| +DLLEXPORT FPDF_STRUCTTREE STDCALL FPDF_StructTree_GetForPage(FPDF_PAGE page) { |
| + CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| + if (!pPage) |
| + return nullptr; |
| + return IPDF_StructTree::LoadPage(pPage->m_pDocument, pPage->m_pFormDict); |
|
dsinclair
2016/11/23 14:04:19
return page ? IPDF_StructTree::LoadPage(pPage->m_p
Lei Zhang
2016/11/23 19:49:55
It's kind of ugly:
return pPage
? I
|
| +} |
| + |
| +DLLEXPORT void STDCALL FPDF_StructTree_Close(FPDF_STRUCTTREE struct_tree) { |
| + delete reinterpret_cast<IPDF_StructTree*>(struct_tree); |
| +} |
|
dsinclair
2016/11/23 14:04:19
Add a anonymous ToStructTree helper and remove the
Lei Zhang
2016/11/23 19:49:55
Done.
|
| + |
| +DLLEXPORT int STDCALL |
| +FPDF_StructTree_CountChildren(FPDF_STRUCTTREE struct_tree) { |
| + IPDF_StructTree* tree = reinterpret_cast<IPDF_StructTree*>(struct_tree); |
| + return tree ? tree->CountTopElements() : -1; |
| +} |
| + |
| +DLLEXPORT FPDF_STRUCTELEMENT STDCALL |
| +FPDF_StructTree_GetChildAtIndex(FPDF_STRUCTTREE struct_tree, int index) { |
| + IPDF_StructTree* tree = reinterpret_cast<IPDF_StructTree*>(struct_tree); |
| + if (!tree || index < 0 || index >= tree->CountTopElements()) |
| + return nullptr; |
| + return tree->GetTopElement(index); |
| +} |
| + |
| +DLLEXPORT unsigned long STDCALL |
| +FPDF_StructElement_GetAltText(FPDF_STRUCTELEMENT struct_element, |
| + void* buffer, |
| + unsigned long buflen) { |
| + IPDF_StructElement* elem = |
| + reinterpret_cast<IPDF_StructElement*>(struct_element); |
| + if (!elem) |
| + return 0; |
| + |
| + CPDF_Dictionary* dict = elem->GetDict(); |
| + if (!dict) |
| + return 0; |
| + |
| + CFX_WideString str = elem->GetDict()->GetUnicodeTextFor("Alt"); |
|
Lei Zhang
2016/11/23 01:00:40
Made this code, the test, and the comments in the
|
| + if (str.IsEmpty()) |
| + return 0; |
| + |
| + CFX_ByteString encodedStr = str.UTF16LE_Encode(); |
| + const unsigned long len = encodedStr.GetLength(); |
|
Lei Zhang
2016/11/23 01:00:40
The length of the UTF16LE_Encoded string includes
|
| + if (buffer || buflen >= len) |
|
dsinclair
2016/11/23 14:04:19
This should be && yea?
Lei Zhang
2016/11/23 19:49:55
Ya....
Tom Sepez
2016/11/23 20:28:33
Also len < buflen may read better, in that < compa
Lei Zhang
2016/11/23 21:38:56
It's copy pasta, so I flipped all the obvious ones
|
| + FXSYS_memcpy(buffer, encodedStr.c_str(), len); |
| + return len; |
| +} |
| + |
| +DLLEXPORT int STDCALL |
| +FPDF_StructElement_CountChildren(FPDF_STRUCTELEMENT struct_element) { |
| + IPDF_StructElement* elem = |
| + reinterpret_cast<IPDF_StructElement*>(struct_element); |
|
dsinclair
2016/11/23 14:04:18
ToStructElement helper?
Lei Zhang
2016/11/23 19:49:55
Done.
|
| + return elem ? elem->CountKids() : -1; |
| +} |
| + |
| +DLLEXPORT FPDF_STRUCTELEMENT STDCALL |
| +FPDF_StructElement_GetChildAtIndex(FPDF_STRUCTELEMENT struct_element, |
| + int index) { |
| + IPDF_StructElement* elem = |
| + reinterpret_cast<IPDF_StructElement*>(struct_element); |
| + if (!elem || index < 0 || index >= elem->CountKids()) |
| + return nullptr; |
| + |
| + CPDF_StructKid kid = elem->GetKid(index); |
| + return kid.m_Type == CPDF_StructKid::Element ? kid.m_Element.m_pElement |
| + : nullptr; |
| +} |