tdf#157680 scale down size and adjust size and scale factor for /BBox

The size of an embedded PDF files is multiplied by
PDF_INSERT_MAGIC_SCALE_FACTOR for platforms like macOS so undo that
by adjusting the size and scale factor.

For some unknown reason, when exporting the following PDF, the
estimated size of embedded PDF charts are 20x larger than expected.
This only occurs on macOS so possibly there is some special conversion
from MapUnit::MapPoint to MapUnit::MapTwip elsewhere in the code:

  https://bugs.documentfoundation.org/attachment.cgi?id=190109

Change-Id: Id0563466fea3d7a3a0419787ec9da45f0c1d2e0a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/158155
Tested-by: Jenkins
Reviewed-by: Patrick Luby <plubius@neooffice.org>
diff --git a/vcl/source/gdi/impgraph.cxx b/vcl/source/gdi/impgraph.cxx
index 35a2145..c4a5a1c 100644
--- a/vcl/source/gdi/impgraph.cxx
+++ b/vcl/source/gdi/impgraph.cxx
@@ -788,6 +788,17 @@ Size ImpGraphic::getPrefSize() const
                        // svg not yet buffered in maBitmapEx, return size derived from range
                        const basegfx::B2DRange& rRange = maVectorGraphicData->getRange();

#ifdef MACOSX
                        // tdf#157680 scale down estimated size of embedded PDF
                        // For some unknown reason, the embedded PDF sizes
                        // are 20x larger than expected. This only occurs on
                        // macOS so possibly there is some special conversion
                        // from MapUnit::MapPoint to MapUnit::MapTwip elsewhere
                        // in the code.
                        if (maVectorGraphicData->getType() == VectorGraphicDataType::Pdf)
                           aSize = Size(basegfx::fround(rRange.getWidth() / 20.0f), basegfx::fround(rRange.getHeight() / 20.0f));
                        else
#endif
                        aSize = Size(basegfx::fround(rRange.getWidth()), basegfx::fround(rRange.getHeight()));
                    }
                    else
diff --git a/vcl/source/gdi/pdfwriter_impl.cxx b/vcl/source/gdi/pdfwriter_impl.cxx
index 8176f3f..15eeb5c 100644
--- a/vcl/source/gdi/pdfwriter_impl.cxx
+++ b/vcl/source/gdi/pdfwriter_impl.cxx
@@ -71,6 +71,7 @@
#include <vcl/lineinfo.hxx>
#include <vcl/metric.hxx>
#include <vcl/mnemonic.hxx>
#include <vcl/pdfread.hxx>
#include <vcl/settings.hxx>
#include <strhelper.hxx>
#include <vcl/svapp.hxx>
@@ -9058,6 +9059,7 @@ void PDFWriterImpl::writeReferenceXObject(const ReferenceXObjectEmit& rEmit)
    // vcl::ImportPDF() uses getDefaultPdfResolutionDpi to set the desired
    // rendering DPI so we have to take into account that here too.
    static const double fResolutionDPI = vcl::pdf::getDefaultPdfResolutionDpi();
    static const double fMagicScaleFactor = PDF_INSERT_MAGIC_SCALE_FACTOR;

    sal_Int32 nOldDPIX = GetDPIX();
    sal_Int32 nOldDPIY = GetDPIY();
@@ -9072,15 +9074,13 @@ void PDFWriterImpl::writeReferenceXObject(const ReferenceXObjectEmit& rEmit)
    sal_Int32 nWrappedFormObject = 0;
    if (!m_aContext.UseReferenceXObject)
    {
#ifdef MACOSX
        // tdf#156842 increase scale for external PDF data
        // For some unknown reason, the scale is 8 times larger than for
        // non-external PDF XObjects.
        // Multiply PDF_INSERT_MAGIC_SCALE_FACTOR for platforms like macOS
        // that scale all images by this number.
        // This fix also allows the CppunitTest_vcl_pdfexport to run
        // successfully on macOS.
        fScaleX = 8.0 / aSize.Width();
        fScaleY = 8.0 / aSize.Height();
#endif
        fScaleX = fMagicScaleFactor / aSize.Width();
        fScaleY = fMagicScaleFactor / aSize.Height();

        // Parse the PDF data, we need that to write the PDF dictionary of our
        // object.
@@ -9322,9 +9322,11 @@ void PDFWriterImpl::writeReferenceXObject(const ReferenceXObjectEmit& rEmit)
    appendDouble(fScaleY, aLine);
    aLine.append(" 0 0 ]");
    aLine.append(" /BBox [ 0 0 ");
    aLine.append(aSize.Width());
    // tdf#157680 reduce size by magic scale factor in /BBox
    aLine.append(aSize.Width() / fMagicScaleFactor);
    aLine.append(" ");
    aLine.append(aSize.Height());
    // tdf#157680 reduce size by magic scale factor in /BBox
    aLine.append(aSize.Height() / fMagicScaleFactor);
    aLine.append(" ]\n");

    if (m_aContext.UseReferenceXObject && rEmit.m_nEmbeddedObject > 0)