[Libreoffice-commits] core.git: Branch 'distro/collabora/co-2021' - vcl/qa vcl/source

Miklos Vajna (via logerrit) logerrit at kemper.freedesktop.org
Wed May 19 07:17:18 UTC 2021


 vcl/qa/cppunit/pdfexport/data/pdf-image-rotate-180.pdf |binary
 vcl/qa/cppunit/pdfexport/pdfexport.cxx                 |   67 +++++++++++++++++
 vcl/source/gdi/pdfwriter_impl.cxx                      |   53 ++++++++-----
 3 files changed, 99 insertions(+), 21 deletions(-)

New commits:
commit 6c2d69c30303d2acac1e26fe8360b158c3b3f4cc
Author:     Miklos Vajna <vmiklos at collabora.com>
AuthorDate: Mon May 17 13:51:31 2021 +0200
Commit:     Miklos Vajna <vmiklos at collabora.com>
CommitDate: Wed May 19 09:16:43 2021 +0200

    vcl PDF export: fix re-exporting PDF images with arbitrary page-level rotation
    
    Building on top of commit bd520b177637d4b7d9d93733103cff17a3c91b0a (vcl
    PDF export: fix re-exporting PDF images with page-level rotation,
    2019-11-06), this was already working for 90 degrees, now generalize
    this to work with 180 degrees as well.
    
    (cherry picked from commit d7d43cf460d66354a40ffa3b34c0f9efcd42d0be)
    
    Conflicts:
            vcl/qa/cppunit/pdfexport/pdfexport.cxx
    
    Change-Id: I5a5d51662399814d5554d7c2cb86a6c9a2974012
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/115707
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice at gmail.com>
    Reviewed-by: Miklos Vajna <vmiklos at collabora.com>

diff --git a/vcl/qa/cppunit/pdfexport/data/pdf-image-rotate-180.pdf b/vcl/qa/cppunit/pdfexport/data/pdf-image-rotate-180.pdf
new file mode 100644
index 000000000000..981ca32061cd
Binary files /dev/null and b/vcl/qa/cppunit/pdfexport/data/pdf-image-rotate-180.pdf differ
diff --git a/vcl/qa/cppunit/pdfexport/pdfexport.cxx b/vcl/qa/cppunit/pdfexport/pdfexport.cxx
index 92d5bcb22bd0..3a5b11784f27 100644
--- a/vcl/qa/cppunit/pdfexport/pdfexport.cxx
+++ b/vcl/qa/cppunit/pdfexport/pdfexport.cxx
@@ -2485,6 +2485,73 @@ CPPUNIT_TEST_FIXTURE(PdfExportTest, testReexportPDF)
 
 #endif
 }
+
+CPPUNIT_TEST_FIXTURE(PdfExportTest, testPdfImageRotate180)
+{
+    // Create an empty document.
+    mxComponent = loadFromDesktop("private:factory/swriter");
+    uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
+    uno::Reference<text::XText> xText = xTextDocument->getText();
+    uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
+
+    // Insert the PDF image.
+    uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, uno::UNO_QUERY);
+    uno::Reference<beans::XPropertySet> xGraphicObject(
+        xFactory->createInstance("com.sun.star.text.TextGraphicObject"), uno::UNO_QUERY);
+    OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "pdf-image-rotate-180.pdf";
+    xGraphicObject->setPropertyValue("GraphicURL", uno::makeAny(aURL));
+    uno::Reference<drawing::XShape> xShape(xGraphicObject, uno::UNO_QUERY);
+    xShape->setSize(awt::Size(1000, 1000));
+    uno::Reference<text::XTextContent> xTextContent(xGraphicObject, uno::UNO_QUERY);
+    xText->insertTextContent(xCursor->getStart(), xTextContent, /*bAbsorb=*/false);
+
+    // Save as PDF.
+    uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
+    utl::MediaDescriptor aMediaDescriptor;
+    aMediaDescriptor["FilterName"] <<= OUString("writer_pdf_Export");
+    xStorable->storeToURL(maTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
+
+    // Parse the export result.
+    SvFileStream aFile(maTempFile.GetURL(), StreamMode::READ);
+    maMemory.WriteStream(aFile);
+    std::shared_ptr<vcl::pdf::PDFium> pPDFium = vcl::pdf::PDFiumLibrary::get();
+    std::unique_ptr<vcl::pdf::PDFiumDocument> pPdfDocument
+        = pPDFium->openDocument(maMemory.GetData(), maMemory.GetSize());
+    CPPUNIT_ASSERT(pPdfDocument);
+    CPPUNIT_ASSERT_EQUAL(1, pPdfDocument->getPageCount());
+
+    // Make sure that the page -> form -> form has a child image.
+    std::unique_ptr<vcl::pdf::PDFiumPage> pPdfPage = pPdfDocument->openPage(/*nIndex=*/0);
+    CPPUNIT_ASSERT(pPdfPage);
+    CPPUNIT_ASSERT_EQUAL(1, pPdfPage->getObjectCount());
+    std::unique_ptr<vcl::pdf::PDFiumPageObject> pPageObject = pPdfPage->getObject(0);
+    CPPUNIT_ASSERT_EQUAL(vcl::pdf::PDFPageObjectType::Form, pPageObject->getType());
+    // 2: white background and the actual object.
+    CPPUNIT_ASSERT_EQUAL(2, pPageObject->getFormObjectCount());
+    std::unique_ptr<vcl::pdf::PDFiumPageObject> pFormObject = pPageObject->getFormObject(1);
+    CPPUNIT_ASSERT_EQUAL(vcl::pdf::PDFPageObjectType::Form, pFormObject->getType());
+    CPPUNIT_ASSERT_EQUAL(1, pFormObject->getFormObjectCount());
+
+    // Check if the inner form object (original page object in the pdf image) has the correct
+    // rotation.
+    std::unique_ptr<vcl::pdf::PDFiumPageObject> pInnerFormObject = pFormObject->getFormObject(0);
+    CPPUNIT_ASSERT_EQUAL(vcl::pdf::PDFPageObjectType::Form, pInnerFormObject->getType());
+    CPPUNIT_ASSERT_EQUAL(1, pInnerFormObject->getFormObjectCount());
+    std::unique_ptr<vcl::pdf::PDFiumPageObject> pImage = pInnerFormObject->getFormObject(0);
+    CPPUNIT_ASSERT_EQUAL(vcl::pdf::PDFPageObjectType::Image, pImage->getType());
+    basegfx::B2DHomMatrix aMat = pInnerFormObject->getMatrix();
+    basegfx::B2DTuple aScale;
+    basegfx::B2DTuple aTranslate;
+    double fRotate = 0;
+    double fShearX = 0;
+    aMat.decompose(aScale, aTranslate, fRotate, fShearX);
+    // Without the accompanying fix in place, this test would have failed with:
+    // - Expected: -1
+    // - Actual  : 1
+    // i.e. the 180 degrees rotation didn't happen (via a combination of horizontal + vertical
+    // flip).
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.0, aScale.getX(), 0.01);
+}
 }
 
 CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/vcl/source/gdi/pdfwriter_impl.cxx b/vcl/source/gdi/pdfwriter_impl.cxx
index 6c6fc7c8923b..3f5f47961e05 100644
--- a/vcl/source/gdi/pdfwriter_impl.cxx
+++ b/vcl/source/gdi/pdfwriter_impl.cxx
@@ -8519,29 +8519,40 @@ void PDFWriterImpl::writeReferenceXObject(ReferenceXObjectEmit& rEmit)
 
         tools::Long nWidth = aSize.Width();
         tools::Long nHeight = aSize.Height();
+        basegfx::B2DRange aBBox(0, 0, aSize.Width(),  aSize.Height());
         if (auto pRotate = dynamic_cast<filter::PDFNumberElement*>(pPage->Lookup("Rotate")))
         {
             // The original page was rotated, then construct a transformation matrix which does the
             // same with our form object.
-            if (rtl::math::approxEqual(pRotate->GetValue(), 90))
-            {
-                std::swap(nWidth, nHeight);
-                basegfx::B2DHomMatrix aMat;
-                aMat.rotate(basegfx::deg2rad(pRotate->GetValue()));
-                // Rotate around the origo (bottom left corner) counter-clockwise, then translate
-                // horizontally to effectively keep the bottom left corner unchanged.
-                aLine.append(" /Matrix [ ");
-                aLine.append(aMat.get(0, 0));
-                aLine.append(" ");
-                aLine.append(aMat.get(0, 1));
-                aLine.append(" ");
-                aLine.append(aMat.get(1, 0));
-                aLine.append(" ");
-                aLine.append(aMat.get(1, 1));
-                aLine.append(" 0 ");
-                aLine.append(nWidth);
-                aLine.append(" ] ");
-            }
+            sal_Int32 nRotAngle = static_cast<sal_Int32>(pRotate->GetValue()) % 360;
+            // /Rotate is clockwise, matrix rotate is counter-clockwise.
+            sal_Int32 nAngle = -1 * nRotAngle;
+
+            // The bounding box just rotates.
+            basegfx::B2DHomMatrix aBBoxMat;
+            aBBoxMat.rotate(basegfx::deg2rad(pRotate->GetValue()));
+            aBBox.transform(aBBoxMat);
+
+            // Now transform the object: rotate around the center and make sure that the rotation
+            // doesn't affect the aspect ratio.
+            basegfx::B2DHomMatrix aMat;
+            aMat.translate(-0.5 * aBBox.getWidth(), -0.5 * aBBox.getHeight());
+            aMat.rotate(basegfx::deg2rad(nAngle));
+            aMat.translate(0.5 * nWidth, 0.5 * nHeight);
+
+            aLine.append(" /Matrix [ ");
+            aLine.append(aMat.a());
+            aLine.append(" ");
+            aLine.append(aMat.b());
+            aLine.append(" ");
+            aLine.append(aMat.c());
+            aLine.append(" ");
+            aLine.append(aMat.d());
+            aLine.append(" ");
+            aLine.append(aMat.e());
+            aLine.append(" ");
+            aLine.append(aMat.f());
+            aLine.append(" ] ");
         }
 
         PDFObjectCopier aCopier(*this);
@@ -8549,9 +8560,9 @@ void PDFWriterImpl::writeReferenceXObject(ReferenceXObjectEmit& rEmit)
         aCopier.copyPageResources(pPage, aLine, rResources);
 
         aLine.append(" /BBox [ 0 0 ");
-        aLine.append(nWidth);
+        aLine.append(aBBox.getWidth());
         aLine.append(" ");
-        aLine.append(nHeight);
+        aLine.append(aBBox.getHeight());
         aLine.append(" ]");
 
         if (!g_bDebugDisableCompression)


More information about the Libreoffice-commits mailing list