[Libreoffice-commits] core.git: vcl/CppunitTest_vcl_pdfexport.mk vcl/qa

Miklos Vajna vmiklos at collabora.co.uk
Thu May 4 08:50:06 UTC 2017


 vcl/CppunitTest_vcl_pdfexport.mk       |    5 +-
 vcl/qa/cppunit/pdfexport/pdfexport.cxx |   78 ++++++++++++++++++++++-----------
 2 files changed, 58 insertions(+), 25 deletions(-)

New commits:
commit e9c01db5268a16d657d20f07a083583c2d2b011c
Author: Miklos Vajna <vmiklos at collabora.co.uk>
Date:   Thu May 4 09:04:04 2017 +0200

    CppunitTest_vcl_pdfexport: replace std::search() with pdfium calls
    
    The old code would also pass if the page has e.g. a literal "re f*"
    string (since the tokenizer doesn't parse streams), the new one uses a
    proper parser.
    
    Change-Id: Iabb242b420c14f619f716f5ba47a1165a942f88a
    Reviewed-on: https://gerrit.libreoffice.org/37223
    Reviewed-by: Miklos Vajna <vmiklos at collabora.co.uk>
    Tested-by: Jenkins <ci at libreoffice.org>

diff --git a/vcl/CppunitTest_vcl_pdfexport.mk b/vcl/CppunitTest_vcl_pdfexport.mk
index fbc95d868d57..7f87335fc049 100644
--- a/vcl/CppunitTest_vcl_pdfexport.mk
+++ b/vcl/CppunitTest_vcl_pdfexport.mk
@@ -28,7 +28,10 @@ $(eval $(call gb_CppunitTest_use_libraries,vcl_pdfexport, \
 	xmlsecurity \
 ))
 
-$(eval $(call gb_CppunitTest_use_external,vcl_pdfexport,boost_headers))
+$(eval $(call gb_CppunitTest_use_externals,vcl_pdfexport, \
+	boost_headers \
+	$(if $(filter PDFIUM,$(BUILD_TYPE)),pdfium) \
+))
 
 $(eval $(call gb_CppunitTest_use_sdk_api,vcl_pdfexport))
 
diff --git a/vcl/qa/cppunit/pdfexport/pdfexport.cxx b/vcl/qa/cppunit/pdfexport/pdfexport.cxx
index 36b5134bdaf3..2e2d0f5b4972 100644
--- a/vcl/qa/cppunit/pdfexport/pdfexport.cxx
+++ b/vcl/qa/cppunit/pdfexport/pdfexport.cxx
@@ -21,6 +21,10 @@
 #include <unotools/tempfile.hxx>
 #include <vcl/filter/pdfdocument.hxx>
 #include <tools/zcodec.hxx>
+#if HAVE_FEATURE_PDFIUM
+#include <fpdf_edit.h>
+#include <fpdfview.h>
+#endif
 
 using namespace ::com::sun::star;
 
@@ -193,34 +197,60 @@ void PdfExportTest::testTdf106693()
 
 void PdfExportTest::testTdf105461()
 {
-    vcl::filter::PDFDocument aDocument;
-    load("tdf105461.odp", aDocument);
+    // Setup.
+    FPDF_LIBRARY_CONFIG config;
+    config.version = 2;
+    config.m_pUserFontPaths = nullptr;
+    config.m_pIsolate = nullptr;
+    config.m_v8EmbedderSlot = 0;
+    FPDF_InitLibraryWithConfig(&config);
 
-    // The document has one page.
-    std::vector<vcl::filter::PDFObjectElement*> aPages = aDocument.GetPages();
-    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aPages.size());
+    // Import the bugdoc and export as PDF.
+    OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "tdf105461.odp";
+    mxComponent = loadFromDesktop(aURL);
+    CPPUNIT_ASSERT(mxComponent.is());
 
-    // The page has a stream.
-    vcl::filter::PDFObjectElement* pContents = aPages[0]->LookupObject("Contents");
-    CPPUNIT_ASSERT(pContents);
-    vcl::filter::PDFStreamElement* pStream = pContents->GetStream();
-    CPPUNIT_ASSERT(pStream);
-    SvMemoryStream& rObjectStream = pStream->GetMemory();
-    // Uncompress it.
-    SvMemoryStream aUncompressed;
-    ZCodec aZCodec;
-    aZCodec.BeginCompression();
-    rObjectStream.Seek(0);
-    aZCodec.Decompress(rObjectStream, aUncompressed);
-    CPPUNIT_ASSERT(aZCodec.EndCompression());
+    uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
+    utl::TempFile aTempFile;
+    aTempFile.EnableKillingFile();
+    utl::MediaDescriptor aMediaDescriptor;
+    aMediaDescriptor["FilterName"] <<= OUString("writer_pdf_Export");
+    xStorable->storeToURL(aTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
+
+    // Parse the export result with pdfium.
+    SvFileStream aFile(aTempFile.GetURL(), StreamMode::READ);
+    SvMemoryStream aMemory;
+    aMemory.WriteStream(aFile);
+    FPDF_DOCUMENT pPdfDocument = FPDF_LoadMemDocument(aMemory.GetData(), aMemory.GetSize(), /*password=*/nullptr);
+    CPPUNIT_ASSERT(pPdfDocument);
+
+    // The document has one page.
+    CPPUNIT_ASSERT_EQUAL(1, FPDF_GetPageCount(pPdfDocument));
+    FPDF_PAGE pPdfPage = FPDF_LoadPage(pPdfDocument, /*page_index=*/0);
+    CPPUNIT_ASSERT(pPdfPage);
 
     // Make sure there is a filled rectangle inside.
-    OString aFilledRectangle("re f*");
-    auto pStart = static_cast<const char*>(aUncompressed.GetData());
-    const char* pEnd = pStart + aUncompressed.GetSize();
-    auto it = std::search(pStart, pEnd, aFilledRectangle.getStr(), aFilledRectangle.getStr() + aFilledRectangle.getLength());
-    // This failed, stream contained no filled rectangle.
-    CPPUNIT_ASSERT(it != pEnd);
+    int nPageObjectCount = FPDFPage_CountObject(pPdfPage);
+    int nYellowPathCount = 0;
+    for (int i = 0; i < nPageObjectCount; ++i)
+    {
+        FPDF_PAGEOBJECT pPdfPageObject = FPDFPage_GetObject(pPdfPage, i);
+        if (FPDFPageObj_GetType(pPdfPageObject) != FPDF_PAGEOBJ_PATH)
+            continue;
+
+        unsigned int nRed = 0, nGreen = 0, nBlue = 0, nAlpha = 0;
+        FPDFPath_GetFillColor(pPdfPageObject, &nRed, &nGreen, &nBlue, &nAlpha);
+        if (RGB_COLORDATA(nRed, nGreen, nBlue) == COL_YELLOW)
+            ++nYellowPathCount;
+    }
+
+    // This was 0, the page contained no yellow paths.
+    CPPUNIT_ASSERT_EQUAL(1, nYellowPathCount);
+
+    // Cleanup.
+    FPDF_ClosePage(pPdfPage);
+    FPDF_CloseDocument(pPdfDocument);
+    FPDF_DestroyLibrary();
 }
 
 void PdfExportTest::testTdf105093()


More information about the Libreoffice-commits mailing list