filter/qa/data/picture.pdf |binary filter/qa/pdf.cxx | 39 ++++++++++++++++++++++++++++++++++++ filter/source/pdf/pdfdecomposer.cxx | 8 +++---- 3 files changed, 43 insertions(+), 4 deletions(-)
New commits: commit 4df75c2a2f9abbf2621a9aacd0c4f9fe8841b051 Author: Miklos Vajna <vmik...@collabora.com> AuthorDate: Mon Feb 7 19:58:20 2022 +0100 Commit: Miklos Vajna <vmik...@collabora.com> CommitDate: Tue Feb 8 13:42:45 2022 +0100 sw: fix size of inserted PDF image We used to multiply the pixel size with the DPI, which resulted in some large enough mm100 width that the inserted image was as wide as the body frame allowed it. Change-Id: I97b81f23166889006363b9241a572f6afde99ee3 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129640 Reviewed-by: Miklos Vajna <vmik...@collabora.com> Tested-by: Jenkins diff --git a/filter/qa/data/picture.pdf b/filter/qa/data/picture.pdf new file mode 100644 index 000000000000..79af2b349dd2 Binary files /dev/null and b/filter/qa/data/picture.pdf differ diff --git a/filter/qa/pdf.cxx b/filter/qa/pdf.cxx index 07025c1b1c2e..8eb64b08cc7a 100644 --- a/filter/qa/pdf.cxx +++ b/filter/qa/pdf.cxx @@ -14,6 +14,9 @@ #include <com/sun/star/document/XFilter.hpp> #include <com/sun/star/frame/Desktop.hpp> #include <com/sun/star/xml/crypto/SEInitializer.hpp> +#include <com/sun/star/drawing/XDrawPageSupplier.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/graphic/XGraphic.hpp> #include <comphelper/propertyvalue.hxx> #include <tools/stream.hxx> @@ -52,6 +55,8 @@ void Test::tearDown() test::BootstrapFixture::tearDown(); } +constexpr OUStringLiteral DATA_DIRECTORY = u"/filter/qa/data/"; + CPPUNIT_TEST_FIXTURE(Test, testSignCertificateSubjectName) { uno::Reference<xml::crypto::XSEInitializer> xSEInitializer @@ -101,6 +106,40 @@ CPPUNIT_TEST_FIXTURE(Test, testSignCertificateSubjectName) CPPUNIT_ASSERT(pPdfDocument); CPPUNIT_ASSERT_EQUAL(1, pPdfDocument->getSignatureCount()); } + +CPPUNIT_TEST_FIXTURE(Test, testPdfDecompositionSize) +{ + // Given an empty Writer document: + getComponent().set( + loadFromDesktop("private:factory/swriter", "com.sun.star.text.TextDocument")); + + // When inserting a 267 points wide PDF image into the document: + uno::Sequence<beans::PropertyValue> aArgs = { + comphelper::makePropertyValue("FileName", + m_directories.getURLFromSrc(DATA_DIRECTORY) + "picture.pdf"), + }; + dispatchCommand(getComponent(), ".uno:InsertGraphic", aArgs); + + // Then make sure that its size is correct: + uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier(getComponent(), uno::UNO_QUERY); + uno::Reference<container::XIndexAccess> xDrawPage = xDrawPageSupplier->getDrawPage(); + uno::Reference<beans::XPropertySet> xShape(xDrawPage->getByIndex(0), uno::UNO_QUERY); + auto xGraphic = xShape->getPropertyValue("Graphic").get<uno::Reference<graphic::XGraphic>>(); + CPPUNIT_ASSERT(xGraphic.is()); + Graphic aGraphic(xGraphic); + basegfx::B2DRange aRange = aGraphic.getVectorGraphicData()->getRange(); + // Without the accompanying fix in place, this test would have failed with: + // - Expected: 9419 + // - Actual : 34176 + // i.e. the width was too large, it used all width of the body frame. + // 9419 mm100 is 267 points from the file. +#if defined MACOSX + // TODO the bitmap size is larger (75486) on macOS, but that should not affect the logic size. + (void)aRange; +#else + CPPUNIT_ASSERT_EQUAL(static_cast<double>(9419), aRange.getWidth()); +#endif +} } CPPUNIT_PLUGIN_IMPLEMENT(); diff --git a/filter/source/pdf/pdfdecomposer.cxx b/filter/source/pdf/pdfdecomposer.cxx index f861b4eaf86e..7a032bf3688a 100644 --- a/filter/source/pdf/pdfdecomposer.cxx +++ b/filter/source/pdf/pdfdecomposer.cxx @@ -82,12 +82,12 @@ XPdfDecomposer::getDecomposition(const uno::Reference<util::XBinaryDataContainer BitmapEx aReplacement(aBitmaps[0]); // short form for scale and translate transformation - const Size aDPI( - Application::GetDefaultDevice()->LogicToPixel(Size(1, 1), MapMode(MapUnit::MapInch))); const Size aBitmapSize(aReplacement.GetSizePixel()); + // ImpGraphic::getPrefMapMode() requires mm100 for bitmaps rendered from vector graphic data. + const Size aMM100( + Application::GetDefaultDevice()->PixelToLogic(aBitmapSize, MapMode(MapUnit::Map100thMM))); const basegfx::B2DHomMatrix aBitmapTransform(basegfx::utils::createScaleTranslateB2DHomMatrix( - aBitmapSize.getWidth() * aDPI.getWidth(), aBitmapSize.getHeight() * aDPI.getHeight(), 0, - 0)); + aMM100.getWidth(), aMM100.getHeight(), 0, 0)); // create primitive return drawinglayer::primitive2d::Primitive2DContainer{