From 077f6439954cdbb5061a78b59be95ebf2277ac26 Mon Sep 17 00:00:00 2001 From: Henrique Nakashima Date: Mon, 16 Oct 2017 13:32:01 -0400 Subject: 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 Commit-Queue: Henrique Nakashima --- BUILD.gn | 3 ++ fpdfsdk/fpdfcatalog.cpp | 22 ++++++++++ fpdfsdk/fpdfcatalog_unittest.cpp | 95 ++++++++++++++++++++++++++++++++++++++++ fpdfsdk/fpdfview_c_api_test.c | 4 ++ public/fpdf_catalog.h | 34 ++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 fpdfsdk/fpdfcatalog.cpp create mode 100644 fpdfsdk/fpdfcatalog_unittest.cpp create mode 100644 public/fpdf_catalog.h diff --git a/BUILD.gn b/BUILD.gn index 7355dca772..3de2d32566 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -138,6 +138,7 @@ static_library("pdfium") { "fpdfsdk/fpdf_transformpage.cpp", "fpdfsdk/fpdfannot.cpp", "fpdfsdk/fpdfattachment.cpp", + "fpdfsdk/fpdfcatalog.cpp", "fpdfsdk/fpdfdoc.cpp", "fpdfsdk/fpdfeditimg.cpp", "fpdfsdk/fpdfeditpage.cpp", @@ -159,6 +160,7 @@ static_library("pdfium") { "public/cpp/fpdf_deleters.h", "public/fpdf_annot.h", "public/fpdf_attachment.h", + "public/fpdf_catalog.h", "public/fpdf_dataavail.h", "public/fpdf_doc.h", "public/fpdf_edit.h", @@ -1927,6 +1929,7 @@ test("pdfium_unittests") { "core/fxcrt/weak_ptr_unittest.cpp", "core/fxcrt/widestring_unittest.cpp", "core/fxge/dib/cstretchengine_unittest.cpp", + "fpdfsdk/fpdfcatalog_unittest.cpp", "fpdfsdk/fpdfdoc_unittest.cpp", "fpdfsdk/fpdfeditimg_unittest.cpp", "testing/unit_test_main.cpp", 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()) {} + + void SetRoot(CPDF_Dictionary* root) { + reinterpret_cast(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(); + + // Setup the root directory. + m_pRootObj = pdfium::MakeUnique(); + } + + void TearDown() override { + m_pDoc.reset(); + CPDF_ModuleMgr::Destroy(); + } + + protected: + std::unique_ptr m_pDoc; + std::unique_ptr 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("OTHER_KEY", "other value", false); + EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get())); + + // Root with empty MarkInfo + CPDF_Dictionary* markInfoDict = + m_pRootObj->SetNewFor("MarkInfo"); + EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get())); + + // MarkInfo present but Marked is 0 + markInfoDict->SetNewFor("Marked", 0); + EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get())); + + // MarkInfo present and Marked is 1, PDF is considered tagged. + markInfoDict->SetNewFor("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); diff --git a/public/fpdf_catalog.h b/public/fpdf_catalog.h new file mode 100644 index 0000000000..48d27756f3 --- /dev/null +++ b/public/fpdf_catalog.h @@ -0,0 +1,34 @@ +// 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. + +#ifndef PUBLIC_FPDF_CATALOG_H_ +#define PUBLIC_FPDF_CATALOG_H_ + +// NOLINTNEXTLINE(build/include) +#include "fpdfview.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +/** + * Experimental API. + * + * Determine if |document| represents a tagged PDF. + * + * For the definition of tagged PDF, See (see 10.7 "Tagged PDF" in PDF + * Reference 1.7). + * + * document - handle to a document. + * + * Returns |true| iff |document| is a tagged PDF. + */ +FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV +FPDFCatalog_IsTagged(FPDF_DOCUMENT document); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus + +#endif // PUBLIC_FPDF_CATALOG_H_ -- cgit v1.2.3