summaryrefslogtreecommitdiff
path: root/fpdfsdk/fpdf_structtree.cpp
diff options
context:
space:
mode:
authorthestig <thestig@chromium.org>2016-11-23 14:10:06 -0800
committerCommit bot <commit-bot@chromium.org>2016-11-23 14:10:06 -0800
commit9067fd683ebf8d6467f8cc5aa7daf5e1f950f846 (patch)
treec967cfe1c94ca9049896d3c3b54d81594c2e9a9f /fpdfsdk/fpdf_structtree.cpp
parent841a85b19cc10d719505ea2c7e0e49bae843db6e (diff)
downloadpdfium-9067fd683ebf8d6467f8cc5aa7daf5e1f950f846.tar.xz
Add APIs for limited use of document tagged code.
BUG=pdfium:568 Review-Url: https://codereview.chromium.org/2519343002
Diffstat (limited to 'fpdfsdk/fpdf_structtree.cpp')
-rw-r--r--fpdfsdk/fpdf_structtree.cpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/fpdfsdk/fpdf_structtree.cpp b/fpdfsdk/fpdf_structtree.cpp
new file mode 100644
index 0000000000..541c46b378
--- /dev/null
+++ b/fpdfsdk/fpdf_structtree.cpp
@@ -0,0 +1,88 @@
+// 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"
+
+namespace {
+
+IPDF_StructTree* ToStructTree(FPDF_STRUCTTREE struct_tree) {
+ return reinterpret_cast<IPDF_StructTree*>(struct_tree);
+}
+
+IPDF_StructElement* ToStructTreeElement(FPDF_STRUCTELEMENT struct_element) {
+ return reinterpret_cast<IPDF_StructElement*>(struct_element);
+}
+
+} // namespace
+
+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);
+}
+
+DLLEXPORT void STDCALL FPDF_StructTree_Close(FPDF_STRUCTTREE struct_tree) {
+ delete ToStructTree(struct_tree);
+}
+
+DLLEXPORT int STDCALL
+FPDF_StructTree_CountChildren(FPDF_STRUCTTREE struct_tree) {
+ IPDF_StructTree* tree = ToStructTree(struct_tree);
+ return tree ? tree->CountTopElements() : -1;
+}
+
+DLLEXPORT FPDF_STRUCTELEMENT STDCALL
+FPDF_StructTree_GetChildAtIndex(FPDF_STRUCTTREE struct_tree, int index) {
+ IPDF_StructTree* tree = ToStructTree(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 = ToStructTreeElement(struct_element);
+ if (!elem)
+ return 0;
+
+ CPDF_Dictionary* dict = elem->GetDict();
+ if (!dict)
+ return 0;
+
+ CFX_WideString str = elem->GetDict()->GetUnicodeTextFor("Alt");
+ if (str.IsEmpty())
+ return 0;
+
+ CFX_ByteString encodedStr = str.UTF16LE_Encode();
+ const unsigned long len = encodedStr.GetLength();
+ if (buffer && len <= buflen)
+ FXSYS_memcpy(buffer, encodedStr.c_str(), len);
+ return len;
+}
+
+DLLEXPORT int STDCALL
+FPDF_StructElement_CountChildren(FPDF_STRUCTELEMENT struct_element) {
+ IPDF_StructElement* elem = ToStructTreeElement(struct_element);
+ return elem ? elem->CountKids() : -1;
+}
+
+DLLEXPORT FPDF_STRUCTELEMENT STDCALL
+FPDF_StructElement_GetChildAtIndex(FPDF_STRUCTELEMENT struct_element,
+ int index) {
+ IPDF_StructElement* elem = ToStructTreeElement(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;
+}