summaryrefslogtreecommitdiff
path: root/fpdfsdk
diff options
context:
space:
mode:
authorHenrique Nakashima <hnakashima@chromium.org>2017-10-16 13:32:01 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-10-16 17:46:49 +0000
commit077f6439954cdbb5061a78b59be95ebf2277ac26 (patch)
tree1f444c5fd6ced1da8452becf90c972676ce0dd92 /fpdfsdk
parente5a637cfb23da32443b345b07f090b65771fa1c9 (diff)
downloadpdfium-077f6439954cdbb5061a78b59be95ebf2277ac26.tar.xz
Add FPDFCatalog_IsTagged to public API.
The new fpdf_catalog.h will contains functions to access entries in the catalog (root) dict in a PDF. Bug: chromium:768986 Change-Id: I6e1d4a479d6f8742981e89f07bab98ee96dc3763 Reviewed-on: https://pdfium-review.googlesource.com/15970 Reviewed-by: dsinclair <dsinclair@chromium.org> Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Diffstat (limited to 'fpdfsdk')
-rw-r--r--fpdfsdk/fpdfcatalog.cpp22
-rw-r--r--fpdfsdk/fpdfcatalog_unittest.cpp95
-rw-r--r--fpdfsdk/fpdfview_c_api_test.c4
3 files changed, 121 insertions, 0 deletions
diff --git a/fpdfsdk/fpdfcatalog.cpp b/fpdfsdk/fpdfcatalog.cpp
new file mode 100644
index 0000000000..493e5aea43
--- /dev/null
+++ b/fpdfsdk/fpdfcatalog.cpp
@@ -0,0 +1,22 @@
+// Copyright 2017 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_catalog.h"
+
+#include "core/fpdfapi/parser/cpdf_document.h"
+#include "fpdfsdk/fsdk_define.h"
+
+FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
+FPDFCatalog_IsTagged(FPDF_DOCUMENT document) {
+ CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
+ if (!pDoc)
+ return false;
+
+ const CPDF_Dictionary* pCatalog = pDoc->GetRoot();
+ if (!pCatalog)
+ return false;
+
+ const CPDF_Dictionary* pMarkInfo = pCatalog->GetDictFor("MarkInfo");
+ return pMarkInfo && pMarkInfo->GetIntegerFor("Marked") != 0;
+}
diff --git a/fpdfsdk/fpdfcatalog_unittest.cpp b/fpdfsdk/fpdfcatalog_unittest.cpp
new file mode 100644
index 0000000000..13c063453b
--- /dev/null
+++ b/fpdfsdk/fpdfcatalog_unittest.cpp
@@ -0,0 +1,95 @@
+// Copyright 2017 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_catalog.h"
+
+#include "core/fpdfapi/cpdf_modulemgr.h"
+#include "core/fpdfapi/parser/cpdf_document.h"
+#include "core/fpdfapi/parser/cpdf_number.h"
+#include "core/fpdfapi/parser/cpdf_parser.h"
+#include "core/fpdfapi/parser/cpdf_string.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/test_support.h"
+
+#ifdef PDF_ENABLE_XFA
+#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
+#endif // PDF_ENABLE_XFA
+
+class CPDF_TestDocument : public CPDF_Document {
+ public:
+ CPDF_TestDocument() : CPDF_Document(nullptr) {}
+
+ void SetRoot(CPDF_Dictionary* root) {
+ m_pRootDict = root;
+ GetRoot();
+ }
+};
+
+#ifdef PDF_ENABLE_XFA
+class CPDF_TestXFAContext : public CPDFXFA_Context {
+ public:
+ CPDF_TestXFAContext()
+ : CPDFXFA_Context(pdfium::MakeUnique<CPDF_TestDocument>()) {}
+
+ void SetRoot(CPDF_Dictionary* root) {
+ reinterpret_cast<CPDF_TestDocument*>(GetPDFDoc())->SetRoot(root);
+ }
+
+ CPDF_IndirectObjectHolder* GetHolder() { return GetPDFDoc(); }
+};
+using CPDF_TestPdfDocument = CPDF_TestXFAContext;
+#else // PDF_ENABLE_XFA
+using CPDF_TestPdfDocument = CPDF_TestDocument;
+#endif // PDF_ENABLE_XFA
+
+class PDFCatalogTest : public testing::Test {
+ public:
+ void SetUp() override {
+ CPDF_ModuleMgr::Get()->Init();
+
+ m_pDoc = pdfium::MakeUnique<CPDF_TestPdfDocument>();
+
+ // Setup the root directory.
+ m_pRootObj = pdfium::MakeUnique<CPDF_Dictionary>();
+ }
+
+ void TearDown() override {
+ m_pDoc.reset();
+ CPDF_ModuleMgr::Destroy();
+ }
+
+ protected:
+ std::unique_ptr<CPDF_TestPdfDocument> m_pDoc;
+ std::unique_ptr<CPDF_Dictionary> m_pRootObj;
+};
+
+TEST_F(PDFCatalogTest, IsTagged) {
+ // Null doc
+ EXPECT_FALSE(FPDFCatalog_IsTagged(nullptr));
+
+ // No root
+ m_pDoc->SetRoot(nullptr);
+ EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
+
+ // Empty root
+ m_pDoc->SetRoot(m_pRootObj.get());
+ EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
+
+ // Root with other key
+ m_pRootObj->SetNewFor<CPDF_String>("OTHER_KEY", "other value", false);
+ EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
+
+ // Root with empty MarkInfo
+ CPDF_Dictionary* markInfoDict =
+ m_pRootObj->SetNewFor<CPDF_Dictionary>("MarkInfo");
+ EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
+
+ // MarkInfo present but Marked is 0
+ markInfoDict->SetNewFor<CPDF_Number>("Marked", 0);
+ EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
+
+ // MarkInfo present and Marked is 1, PDF is considered tagged.
+ markInfoDict->SetNewFor<CPDF_Number>("Marked", 1);
+ EXPECT_TRUE(FPDFCatalog_IsTagged(m_pDoc.get()));
+}
diff --git a/fpdfsdk/fpdfview_c_api_test.c b/fpdfsdk/fpdfview_c_api_test.c
index 211d7e0f6b..bf1a0c4877 100644
--- a/fpdfsdk/fpdfview_c_api_test.c
+++ b/fpdfsdk/fpdfview_c_api_test.c
@@ -11,6 +11,7 @@
#include "public/fpdf_annot.h"
#include "public/fpdf_attachment.h"
+#include "public/fpdf_catalog.h"
#include "public/fpdf_dataavail.h"
#include "public/fpdf_doc.h"
#include "public/fpdf_edit.h"
@@ -78,6 +79,9 @@ int CheckPDFiumCApi() {
CHK(FPDFAttachment_SetFile);
CHK(FPDFAttachment_GetFile);
+ // fpdf_catalog.h
+ CHK(FPDFCatalog_IsTagged);
+
// fpdf_dataavail.h
CHK(FPDFAvail_Create);
CHK(FPDFAvail_Destroy);