[Libreoffice-commits] core.git: sd/qa

Tomaž Vajngerl (via logerrit) logerrit at kemper.freedesktop.org
Sun Jun 21 21:10:08 UTC 2020


 sd/qa/unit/SdrPdfImportTest.cxx       |  132 ++++++++++++++++++++++++++++++++++
 sd/qa/unit/data/PdfWithAnnotation.pdf |binary
 2 files changed, 132 insertions(+)

New commits:
commit 037463d2617e5d7bbaea6b99f6b8ffd265cb38af
Author:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
AuthorDate: Fri Jun 19 12:59:59 2020 +0200
Commit:     Tomaž Vajngerl <quikee at gmail.com>
CommitDate: Sun Jun 21 23:09:30 2020 +0200

    sd: Test PDF Annotation roundtrip
    
    Change-Id: I2532ecb451362b3b48c8c08b79c010dcad67d5da
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/96760
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <quikee at gmail.com>

diff --git a/sd/qa/unit/SdrPdfImportTest.cxx b/sd/qa/unit/SdrPdfImportTest.cxx
index 9f30ff5000be..4bbb24de4035 100644
--- a/sd/qa/unit/SdrPdfImportTest.cxx
+++ b/sd/qa/unit/SdrPdfImportTest.cxx
@@ -16,7 +16,11 @@
 
 #include <comphelper/scopeguard.hxx>
 #include <comphelper/processfactory.hxx>
+#include <comphelper/propertysequence.hxx>
 
+#include <unotools/tempfile.hxx>
+#include <unotools/mediadescriptor.hxx>
+#include <tools/stream.hxx>
 #include <svx/svdograf.hxx>
 #include <editeng/outlobj.hxx>
 #include <editeng/editobj.hxx>
@@ -123,6 +127,134 @@ CPPUNIT_TEST_FIXTURE(SdrPdfImportTest, testImportSimpleText)
 #endif // HAVE_FEATURE_PDFIUM
 }
 
+CPPUNIT_TEST_FIXTURE(SdrPdfImportTest, testAnnotationsImportExport)
+{
+#if HAVE_FEATURE_PDFIUM && !defined(_WIN32)
+    // We need to enable PDFium import (and make sure to disable after the test)
+    bool bResetEnvVar = false;
+    if (getenv("LO_IMPORT_USE_PDFIUM") == nullptr)
+    {
+        bResetEnvVar = true;
+        setenv("LO_IMPORT_USE_PDFIUM", "1", false);
+    }
+    comphelper::ScopeGuard aPDFiumEnvVarGuard([&]() {
+        if (bResetEnvVar)
+            unsetenv("LO_IMPORT_USE_PDFIUM");
+    });
+
+    bool bPDFCompressorResetEnvVar = false;
+    if (getenv("VCL_DEBUG_DISABLE_PDFCOMPRESSION") == nullptr)
+    {
+        bPDFCompressorResetEnvVar = true;
+        setenv("VCL_DEBUG_DISABLE_PDFCOMPRESSION", "1", false);
+    }
+    comphelper::ScopeGuard aPDFCompressorEnvVarGuard([&]() {
+        if (bPDFCompressorResetEnvVar)
+            unsetenv("VCL_DEBUG_DISABLE_PDFCOMPRESSION");
+    });
+
+    auto pPdfiumLibrary = vcl::pdf::PDFiumLibrary::get();
+
+    mxComponent
+        = loadFromDesktop(m_directories.getURLFromSrc("sd/qa/unit/data/PdfWithAnnotation.pdf"));
+    auto pImpressDocument = dynamic_cast<SdXImpressDocument*>(mxComponent.get());
+    sd::ViewShell* pViewShell = pImpressDocument->GetDocShell()->GetViewShell();
+    CPPUNIT_ASSERT(pViewShell);
+
+    const void* pData = nullptr;
+    int nLength = 0;
+
+    {
+        // Get the first page - there should be only one.
+        SdPage* pPage = pViewShell->GetActualPage();
+        CPPUNIT_ASSERT(pPage);
+
+        // Check the number of annotations
+        CPPUNIT_ASSERT_EQUAL(size_t(1), pPage->getAnnotations().size());
+
+        // Get the first object - there should be only one.
+        SdrObject* pObject = pPage->GetObj(0);
+        CPPUNIT_ASSERT(pObject);
+
+        // Check the object is a graphic object
+        SdrGrafObj* pGraphicObject = dynamic_cast<SdrGrafObj*>(pObject);
+        CPPUNIT_ASSERT(pGraphicObject);
+
+        // Check the graphic is a vector graphic and that it is PDF
+        Graphic aGraphic = pGraphicObject->GetGraphic();
+        auto const& pVectorGraphicData = aGraphic.getVectorGraphicData();
+        CPPUNIT_ASSERT(pVectorGraphicData);
+        CPPUNIT_ASSERT_EQUAL(VectorGraphicDataType::Pdf,
+                             pVectorGraphicData->getVectorGraphicDataType());
+
+        // Write the PDF
+        pData = pVectorGraphicData->getVectorGraphicDataArray().getConstArray();
+        nLength = pVectorGraphicData->getVectorGraphicDataArrayLength();
+    }
+
+    { // check graphic PDF has annotations
+
+        auto pPDFDocument = pPdfiumLibrary->openDocument(pData, nLength);
+        auto pPDFPage = pPDFDocument->openPage(0);
+
+        CPPUNIT_ASSERT_EQUAL(2, pPDFPage->getAnnotationCount());
+
+        auto pPDFAnnotation1 = pPDFPage->getAnnotation(0);
+        CPPUNIT_ASSERT_EQUAL(1, pPDFAnnotation1->getSubType()); // Text annotation
+
+        auto pPDFAnnotation2 = pPDFPage->getAnnotation(1);
+        CPPUNIT_ASSERT_EQUAL(16, pPDFAnnotation2->getSubType()); // Pop-up annotation
+    }
+
+    { // save as PDF and check annotations
+        utl::TempFile aTempFile;
+        aTempFile.EnableKillingFile();
+
+        uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
+        utl::MediaDescriptor aMediaDescriptor;
+        aMediaDescriptor["FilterName"] <<= OUString("writer_pdf_Export");
+        uno::Sequence<beans::PropertyValue> aFilterData(
+            comphelper::InitPropertySequence({ { "ExportBookmarks", uno::Any(true) } }));
+        aMediaDescriptor["FilterData"] <<= aFilterData;
+        xStorable->storeToURL(aTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
+        mxComponent->dispose();
+
+        SvFileStream aFile(aTempFile.GetURL(), StreamMode::READ);
+        SvMemoryStream aMemory;
+        aMemory.WriteStream(aFile);
+
+        // Check PDF for annotations
+        auto pPDFDocument = pPdfiumLibrary->openDocument(aMemory.GetData(), aMemory.GetSize());
+        CPPUNIT_ASSERT(pPDFDocument);
+        CPPUNIT_ASSERT_EQUAL(1, pPDFDocument->getPageCount());
+
+        auto pPDFPage = pPDFDocument->openPage(0);
+        CPPUNIT_ASSERT(pPDFPage);
+
+        // TODO: Should be 2 really
+        CPPUNIT_ASSERT_EQUAL(1, pPDFPage->getAnnotationCount());
+
+        auto pPDFAnnotation1 = pPDFPage->getAnnotation(0);
+        CPPUNIT_ASSERT_EQUAL(1, pPDFAnnotation1->getSubType()); // Text annotation
+
+        //auto pPDFAnnotation2 = pPDFPage->getAnnotation(1);
+        //CPPUNIT_ASSERT_EQUAL(16, pPDFAnnotation2->getSubType()); // Pop-up annotation
+
+        // Load document again
+        mxComponent = loadFromDesktop(aTempFile.GetURL());
+        auto pNewImpressDocument = dynamic_cast<SdXImpressDocument*>(mxComponent.get());
+        sd::ViewShell* pNewViewShell = pNewImpressDocument->GetDocShell()->GetViewShell();
+        CPPUNIT_ASSERT(pNewViewShell);
+
+        SdPage* pPage = pNewViewShell->GetActualPage();
+        CPPUNIT_ASSERT(pPage);
+
+        //CPPUNIT_ASSERT_EQUAL(false, pPage->getAnnotations().empty());
+    }
+
+#endif // HAVE_FEATURE_PDFIUM
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sd/qa/unit/data/PdfWithAnnotation.pdf b/sd/qa/unit/data/PdfWithAnnotation.pdf
new file mode 100644
index 000000000000..09529ed17884
Binary files /dev/null and b/sd/qa/unit/data/PdfWithAnnotation.pdf differ


More information about the Libreoffice-commits mailing list