summaryrefslogtreecommitdiff
path: root/fpdfsdk
diff options
context:
space:
mode:
Diffstat (limited to 'fpdfsdk')
-rw-r--r--fpdfsdk/fpdf_structtree.cpp40
-rw-r--r--fpdfsdk/fpdf_structtree_embeddertest.cpp29
-rw-r--r--fpdfsdk/fpdfview_c_api_test.c1
3 files changed, 55 insertions, 15 deletions
diff --git a/fpdfsdk/fpdf_structtree.cpp b/fpdfsdk/fpdf_structtree.cpp
index 06713fba1d..8a93d2299d 100644
--- a/fpdfsdk/fpdf_structtree.cpp
+++ b/fpdfsdk/fpdf_structtree.cpp
@@ -21,6 +21,19 @@ IPDF_StructElement* ToStructTreeElement(FPDF_STRUCTELEMENT struct_element) {
return reinterpret_cast<IPDF_StructElement*>(struct_element);
}
+unsigned long WideStringToBuffer(const CFX_WideString& str,
+ void* buffer,
+ unsigned long buflen) {
+ if (str.IsEmpty())
+ return 0;
+
+ CFX_ByteString encodedStr = str.UTF16LE_Encode();
+ const unsigned long len = encodedStr.GetLength();
+ if (buffer && len <= buflen)
+ memcpy(buffer, encodedStr.c_str(), len);
+ return len;
+}
+
} // namespace
DLLEXPORT FPDF_STRUCTTREE STDCALL FPDF_StructTree_GetForPage(FPDF_PAGE page) {
@@ -54,22 +67,19 @@ 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;
+ return (elem && elem->GetDict())
+ ? WideStringToBuffer(elem->GetDict()->GetUnicodeTextFor("Alt"),
+ buffer, buflen)
+ : 0;
+}
- CFX_ByteString encodedStr = str.UTF16LE_Encode();
- const unsigned long len = encodedStr.GetLength();
- if (buffer && len <= buflen)
- memcpy(buffer, encodedStr.c_str(), len);
- return len;
+DLLEXPORT unsigned long STDCALL
+FPDF_StructElement_GetType(FPDF_STRUCTELEMENT struct_element,
+ void* buffer,
+ unsigned long buflen) {
+ IPDF_StructElement* elem = ToStructTreeElement(struct_element);
+ return elem ? WideStringToBuffer(elem->GetType().UTF8Decode(), buffer, buflen)
+ : 0;
}
DLLEXPORT int STDCALL
diff --git a/fpdfsdk/fpdf_structtree_embeddertest.cpp b/fpdfsdk/fpdf_structtree_embeddertest.cpp
index 8ddde5317c..3110988d82 100644
--- a/fpdfsdk/fpdf_structtree_embeddertest.cpp
+++ b/fpdfsdk/fpdf_structtree_embeddertest.cpp
@@ -68,3 +68,32 @@ TEST_F(FPDFStructTreeEmbeddertest, GetAltText) {
FPDF_StructTree_Close(struct_tree);
FPDF_ClosePage(page);
}
+
+TEST_F(FPDFStructTreeEmbeddertest, GetType) {
+ ASSERT_TRUE(OpenDocument("tagged_alt_text.pdf"));
+ FPDF_PAGE page = LoadPage(0);
+ ASSERT_TRUE(page);
+
+ FPDF_STRUCTTREE struct_tree = FPDF_StructTree_GetForPage(page);
+ ASSERT_TRUE(struct_tree);
+ ASSERT_EQ(1, FPDF_StructTree_CountChildren(struct_tree));
+
+ FPDF_STRUCTELEMENT element = FPDF_StructTree_GetChildAtIndex(struct_tree, 0);
+ ASSERT_NE(nullptr, element);
+
+ unsigned short buffer[12];
+ memset(buffer, 0, sizeof(buffer));
+ // Deliberately pass in a small buffer size to make sure |buffer| remains
+ // untouched.
+ ASSERT_EQ(18U, FPDF_StructElement_GetType(element, buffer, 1));
+ for (size_t i = 0; i < FX_ArraySize(buffer); ++i)
+ EXPECT_EQ(0U, buffer[i]);
+
+ ASSERT_EQ(18U, FPDF_StructElement_GetType(element, buffer, sizeof(buffer)));
+ const wchar_t kExpected[] = L"Document";
+ EXPECT_EQ(CFX_WideString(kExpected),
+ CFX_WideString::FromUTF16LE(buffer, FXSYS_len(kExpected)));
+
+ FPDF_StructTree_Close(struct_tree);
+ FPDF_ClosePage(page);
+}
diff --git a/fpdfsdk/fpdfview_c_api_test.c b/fpdfsdk/fpdfview_c_api_test.c
index fec91efeca..9afbdd479e 100644
--- a/fpdfsdk/fpdfview_c_api_test.c
+++ b/fpdfsdk/fpdfview_c_api_test.c
@@ -178,6 +178,7 @@ int CheckPDFiumCApi() {
CHK(FPDF_StructTree_CountChildren);
CHK(FPDF_StructTree_GetChildAtIndex);
CHK(FPDF_StructElement_GetAltText);
+ CHK(FPDF_StructElement_GetType);
CHK(FPDF_StructElement_CountChildren);
CHK(FPDF_StructElement_GetChildAtIndex);