vcl/qa/cppunit/pdfexport/pdfexport.cxx |    6 +++++-
 vcl/source/gdi/pdfwriter_impl.cxx      |   14 ++++++++++----
 2 files changed, 15 insertions(+), 5 deletions(-)

New commits:
commit 8dc932a517818b5b721653b372aa0124aff2375f
Author:     Patrick Luby <guibmac...@gmail.com>
AuthorDate: Mon Apr 22 19:36:14 2024 -0400
Commit:     Patrick Luby <guibomac...@gmail.com>
CommitDate: Tue Apr 30 15:12:28 2024 +0200

    tdf#160714 use crop box for bounds of embedded PDF object
    
    If there is no crop box, fallback to the media box just to be safe.
    
    Change-Id: I29f99a43e550cf09a1534c0aa01ffd6a55536fb7
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166544
    Tested-by: Jenkins
    Reviewed-by: Patrick Luby <guibomac...@gmail.com>
    (cherry picked from commit 4b31f87e918c38a7eb30ceb85563a5c98b426da5)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166673
    Reviewed-by: Michael Stahl <michael.st...@allotropia.de>
    Reviewed-by: Adolfo Jayme Barrientos <fit...@ubuntu.com>
    Tested-by: Patrick Luby <guibomac...@gmail.com>

diff --git a/vcl/qa/cppunit/pdfexport/pdfexport.cxx 
b/vcl/qa/cppunit/pdfexport/pdfexport.cxx
index d6ae5f527ab8..483910dcf07f 100644
--- a/vcl/qa/cppunit/pdfexport/pdfexport.cxx
+++ b/vcl/qa/cppunit/pdfexport/pdfexport.cxx
@@ -45,6 +45,7 @@
 #include <o3tl/string_view.hxx>
 
 #include <vcl/filter/PDFiumLibrary.hxx>
+#include <vcl/pdfread.hxx>
 #include <comphelper/propertyvalue.hxx>
 
 using namespace ::com::sun::star;
@@ -7800,7 +7801,10 @@ CPPUNIT_TEST_FIXTURE(PdfExportTest, 
testRexportMediaBoxOrigin)
         = { // Rotation by $   heta$ $cos(     heta), sin(     heta), -sin(    
heta), cos(     heta)$
             0, -1, 1, 0,
             // Translate x,y
-            -aOrigin[1] - aSize[1] / 2 + aSize[0] / 2, aOrigin[0] + aSize[0] / 
2 + aSize[1] / 2
+            -aOrigin[1] - aSize[1] / vcl::PDF_INSERT_MAGIC_SCALE_FACTOR / 2
+                + aSize[0] / vcl::PDF_INSERT_MAGIC_SCALE_FACTOR / 2,
+            aOrigin[0] + aSize[0] / vcl::PDF_INSERT_MAGIC_SCALE_FACTOR / 2
+                + aSize[1] / vcl::PDF_INSERT_MAGIC_SCALE_FACTOR / 2
           };
 
     for (sal_Int32 nIdx = 0; nIdx < 6; ++nIdx)
diff --git a/vcl/source/gdi/pdfwriter_impl.cxx 
b/vcl/source/gdi/pdfwriter_impl.cxx
index d3cb95ea7d0a..d104ca9ea1df 100644
--- a/vcl/source/gdi/pdfwriter_impl.cxx
+++ b/vcl/source/gdi/pdfwriter_impl.cxx
@@ -9106,9 +9106,15 @@ void PDFWriterImpl::writeReferenceXObject(const 
ReferenceXObjectEmit& rEmit)
         }
 
         double aOrigin[2] = { 0.0, 0.0 };
-        if (auto* pArray = 
dynamic_cast<filter::PDFArrayElement*>(pPage->Lookup("MediaBox")))
+
+        // tdf#160714 use crop box for bounds of embedded PDF object
+        // If there is no crop box, fallback to the media box just to be safe.
+        auto* pBoundsArray = 
dynamic_cast<filter::PDFArrayElement*>(pPage->Lookup("CropBox"));
+        if (!pBoundsArray)
+            pBoundsArray = 
dynamic_cast<filter::PDFArrayElement*>(pPage->Lookup("MediaBox"));
+        if (pBoundsArray)
         {
-            const auto& rElements = pArray->GetElements();
+            const auto& rElements = pBoundsArray->GetElements();
             if (rElements.size() >= 4)
             {
                 // get x1, y1 of the rectangle.
@@ -9221,9 +9227,9 @@ void PDFWriterImpl::writeReferenceXObject(const 
ReferenceXObjectEmit& rEmit)
             // 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() - aOrigin[0], -0.5 * 
aBBox.getHeight() - aOrigin[1]);
+            aMat.translate((-0.5 * aBBox.getWidth() / fMagicScaleFactor) - 
aOrigin[0], (-0.5 * aBBox.getHeight() / fMagicScaleFactor) - aOrigin[1]);
             aMat.rotate(basegfx::deg2rad(nAngle));
-            aMat.translate(0.5 * nWidth, 0.5 * nHeight);
+            aMat.translate(0.5 * nWidth / fMagicScaleFactor, 0.5 * nHeight / 
fMagicScaleFactor);
 
             aLine.append(" /Matrix [ ");
             aLine.append(aMat.a());

Reply via email to