From fbcc5d9afb4506eea1003d024a5fac5d601cc196 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 5 Jun 2018 13:57:29 +0000 Subject: Add test for FPDF_GetPageSizeByIndex() Ensure that FPDF_GetPageSizeByIndex() doesn't do a full page parse. Issue was noticed on CL https://pdfium-review.googlesource.com/32830 Change-Id: I51966e0b91e1a002d33ee51f00c0428fa1cda04d Reviewed-on: https://pdfium-review.googlesource.com/33792 Commit-Queue: dsinclair Reviewed-by: dsinclair --- core/fpdfapi/page/cpdf_pageobjectholder.cpp | 3 +++ core/fpdfapi/parser/cpdf_document.h | 4 ++++ fpdfsdk/fpdf_doc_embeddertest.cpp | 5 ++++ fpdfsdk/fpdf_view_embeddertest.cpp | 36 +++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/core/fpdfapi/page/cpdf_pageobjectholder.cpp b/core/fpdfapi/page/cpdf_pageobjectholder.cpp index 87a1aebaab..4d4fc56ac2 100644 --- a/core/fpdfapi/page/cpdf_pageobjectholder.cpp +++ b/core/fpdfapi/page/cpdf_pageobjectholder.cpp @@ -14,6 +14,7 @@ #include "core/fpdfapi/page/cpdf_contentparser.h" #include "core/fpdfapi/page/cpdf_pageobject.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" +#include "core/fpdfapi/parser/cpdf_document.h" CPDF_PageObjectHolder::CPDF_PageObjectHolder(CPDF_Document* pDoc, CPDF_Dictionary* pFormDict) @@ -38,6 +39,8 @@ void CPDF_PageObjectHolder::ContinueParse(PauseIndicatorIface* pPause) { return; m_ParseState = CONTENT_PARSED; + m_pDocument->IncrementParsedPageCount(); + if (m_pParser->GetCurStates()) m_LastCTM = m_pParser->GetCurStates()->m_CTM; diff --git a/core/fpdfapi/parser/cpdf_document.h b/core/fpdfapi/parser/cpdf_document.h index fe3f170114..a34b25d001 100644 --- a/core/fpdfapi/parser/cpdf_document.h +++ b/core/fpdfapi/parser/cpdf_document.h @@ -106,6 +106,9 @@ class CPDF_Document : public CPDF_IndirectObjectHolder { void CreateNewDoc(); CPDF_Dictionary* CreateNewPage(int iPage); + void IncrementParsedPageCount() { ++m_ParsedPageCount; } + uint32_t GetParsedPageCountForTesting() { return m_ParsedPageCount; } + CPDF_Font* AddStandardFont(const char* font, CPDF_FontEncoding* pEncoding); CPDF_Font* AddFont(CFX_Font* pFont, int charset, bool bVert); #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ @@ -165,6 +168,7 @@ class CPDF_Document : public CPDF_IndirectObjectHolder { bool m_bLinearized; int m_iFirstPageNo; uint32_t m_dwFirstPageObjNum; + uint32_t m_ParsedPageCount = 0; std::unique_ptr m_pDocPage; std::unique_ptr m_pDocRender; std::unique_ptr m_pCodecContext; diff --git a/fpdfsdk/fpdf_doc_embeddertest.cpp b/fpdfsdk/fpdf_doc_embeddertest.cpp index c1f5e02ed6..a9eb4b8cb7 100644 --- a/fpdfsdk/fpdf_doc_embeddertest.cpp +++ b/fpdfsdk/fpdf_doc_embeddertest.cpp @@ -7,7 +7,9 @@ #include #include +#include "core/fpdfapi/parser/cpdf_document.h" #include "core/fxcrt/fx_string.h" +#include "fpdfsdk/cpdfsdk_helpers.h" #include "public/cpp/fpdf_scopers.h" #include "public/fpdf_doc.h" #include "public/fpdf_edit.h" @@ -20,6 +22,7 @@ class FPDFDocEmbeddertest : public EmbedderTest {}; TEST_F(FPDFDocEmbeddertest, MultipleSamePage) { EXPECT_TRUE(OpenDocument("hello_world.pdf")); + CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document()); std::set unique_pages; std::vector owned_pages(4); @@ -29,8 +32,10 @@ TEST_F(FPDFDocEmbeddertest, MultipleSamePage) { } #ifdef PDF_ENABLE_XFA EXPECT_EQ(1u, unique_pages.size()); + EXPECT_EQ(1u, pDoc->GetParsedPageCountForTesting()); #else // PDF_ENABLE_XFA EXPECT_EQ(4u, unique_pages.size()); + EXPECT_EQ(4u, pDoc->GetParsedPageCountForTesting()); #endif // PDF_ENABLE_XFA } diff --git a/fpdfsdk/fpdf_view_embeddertest.cpp b/fpdfsdk/fpdf_view_embeddertest.cpp index 7cc5478808..cef5e9a0d4 100644 --- a/fpdfsdk/fpdf_view_embeddertest.cpp +++ b/fpdfsdk/fpdf_view_embeddertest.cpp @@ -7,6 +7,8 @@ #include #include +#include "core/fpdfapi/parser/cpdf_document.h" +#include "fpdfsdk/cpdfsdk_helpers.h" #include "fpdfsdk/fpdf_view_c_api_test.h" #include "public/cpp/fpdf_scopers.h" #include "public/fpdfview.h" @@ -589,6 +591,40 @@ TEST_F(FPDFViewEmbeddertest, FPDF_RenderPageBitmapWithMatrix) { UnloadPage(page); } +TEST_F(FPDFViewEmbeddertest, FPDF_GetPageSizeByIndex) { + EXPECT_TRUE(OpenDocument("rectangles.pdf")); + CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document()); + + double width = 0; + double height = 0; + + // Page -1 doesn't exist. + EXPECT_FALSE(FPDF_GetPageSizeByIndex(document(), -1, &width, &height)); + + // Page 1 doesn't exist. + EXPECT_FALSE(FPDF_GetPageSizeByIndex(document(), 1, &width, &height)); + + // Page 0 exists. + EXPECT_TRUE(FPDF_GetPageSizeByIndex(document(), 0, &width, &height)); + EXPECT_EQ(200.0, width); + EXPECT_EQ(300.0, height); + +#ifdef PDF_ENABLE_XFA + // TODO(tsepez): XFA must obtain this size without parsing. + EXPECT_EQ(1u, pDoc->GetParsedPageCountForTesting()); +#else // PDF_ENABLE_XFA + EXPECT_EQ(0u, pDoc->GetParsedPageCountForTesting()); +#endif // PDF_ENABLE_XFA + + // Double-check against values from when page is actually parsed. + FPDF_PAGE page = LoadPage(0); + ASSERT_TRUE(page); + EXPECT_EQ(width, FPDF_GetPageWidth(page)); + EXPECT_EQ(height, FPDF_GetPageHeight(page)); + EXPECT_EQ(1u, pDoc->GetParsedPageCountForTesting()); + UnloadPage(page); +} + class UnSupRecordDelegate : public EmbedderTest::Delegate { public: UnSupRecordDelegate() : type_(-1) {} -- cgit v1.2.3