diff --git a/sw/qa/core/layout/data/fly-rel-width-rounding.odt b/sw/qa/core/layout/data/fly-rel-width-rounding.odt new file mode 100644 index 000000000000..b70b9e51a917 Binary files /dev/null and b/sw/qa/core/layout/data/fly-rel-width-rounding.odt differ diff --git a/sw/qa/core/layout/fly.cxx b/sw/qa/core/layout/fly.cxx index 47cfb40fa6e7..81df20af3b35 100644 --- a/sw/qa/core/layout/fly.cxx +++ b/sw/qa/core/layout/fly.cxx @@ -86,6 +86,31 @@ CPPUNIT_TEST_FIXTURE(Test, testSplitFlyNegativeHeight) } } } + +CPPUNIT_TEST_FIXTURE(Test, testFlyRelWithRounding) +{ + // Given a document where page width is 21.001cm (11906 twips), and the image width is 48% of + // the page width: + createSwDoc("fly-rel-width-rounding.odt"); + + // When laying out that document: + SwDoc* pDoc = getSwDoc(); + SwRootFrame* pLayout = pDoc->getIDocumentLayoutAccess().GetCurrentLayout(); + + // Then make sure that we calculate the width of the fly correctly: + auto pPage = pLayout->GetLower()->DynCastPageFrame(); + CPPUNIT_ASSERT(pPage->GetSortedObjs()); + SwSortedObjs& rPageObjs = *pPage->GetSortedObjs(); + CPPUNIT_ASSERT_EQUAL(static_cast(1), rPageObjs.size()); + auto pFly = rPageObjs[0]->DynCastFlyFrame()->DynCastFlyAtContentFrame(); + CPPUNIT_ASSERT(pFly); + tools::Long nFlyWidth = pFly->getFrameArea().Width(); + // Without the accompanying fix in place, this test would have failed with: + // - Expected: 5715 + // - Actual : 5714 + // i.e. 5714.88 was truncated, not rounded. + CPPUNIT_ASSERT_EQUAL(static_cast(5715), nFlyWidth); +} } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/layout/fly.cxx b/sw/source/core/layout/fly.cxx index 549681ca7227..a3c9ac84e7c2 100644 --- a/sw/source/core/layout/fly.cxx +++ b/sw/source/core/layout/fly.cxx @@ -70,6 +70,7 @@ #include #include #include +#include #include #include @@ -2845,9 +2846,9 @@ Size SwFlyFrame::CalcRel( const SwFormatFrameSize &rSz ) const } if ( rSz.GetWidthPercent() && rSz.GetWidthPercent() != SwFormatFrameSize::SYNCED ) - aRet.setWidth( nRelWidth * rSz.GetWidthPercent() / 100 ); + aRet.setWidth(rtl::math::round(double(nRelWidth) * rSz.GetWidthPercent() / 100)); if ( rSz.GetHeightPercent() && rSz.GetHeightPercent() != SwFormatFrameSize::SYNCED ) - aRet.setHeight( nRelHeight * rSz.GetHeightPercent() / 100 ); + aRet.setHeight(rtl::math::round(double(nRelHeight) * rSz.GetHeightPercent() / 100)); if ( rSz.GetHeight() && rSz.GetWidthPercent() == SwFormatFrameSize::SYNCED ) { diff --git a/xmloff/qa/unit/text.cxx b/xmloff/qa/unit/text.cxx index 1679af04bc1f..f95e07e42578 100644 --- a/xmloff/qa/unit/text.cxx +++ b/xmloff/qa/unit/text.cxx @@ -508,11 +508,11 @@ CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testRelativeWidth) xmlDocUniquePtr pXmlDoc = parseExport(u"content.xml"_ustr); // Without the accompanying fix in place, this failed with: - // - Expected: 3.1492in (8cm) + // - Expected: 3.15in (8cm) // - Actual : 0.0161in (0.04 cm) // i.e. the fallback width value wasn't the expected half of the body frame width, but a smaller // value. - assertXPath(pXmlDoc, "//draw:frame"_ostr, "width"_ostr, u"3.1492in"_ustr); + assertXPath(pXmlDoc, "//draw:frame"_ostr, "width"_ostr, u"3.15in"_ustr); } CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testScaleWidthAndHeight)