sw/CppunitTest_sw_uwriter.mk | 1 sw/inc/swrect.hxx | 68 +++++++++++++++++++++++++++++++++++++ sw/qa/core/test_rect.cxx | 67 ++++++++++++++++++++++++++++++++++++ sw/source/core/bastyp/swrect.cxx | 44 ----------------------- sw/source/core/bastyp/swregion.cxx | 6 +-- 5 files changed, 138 insertions(+), 48 deletions(-)
New commits: commit 6702e8a93243c4807c80b53965a4397cdbfde8b5 Author: Luboš Luňák <l.lu...@collabora.com> AuthorDate: Fri Oct 1 10:59:44 2021 +0200 Commit: Luboš Luňák <l.lu...@collabora.com> CommitDate: Sat Oct 2 00:35:47 2021 +0200 add GetUnion/Intersection() to SwRect This is a more readable and efficient way of first constructing a copy SwRect and then using Union/Intersection() on it. Named Get* because that's how tools::Rectangle names them. Change-Id: Ib3033f1900c48792b23526771a941b4d4c1c07bd Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122913 Tested-by: Jenkins Reviewed-by: Luboš Luňák <l.lu...@collabora.com> diff --git a/sw/CppunitTest_sw_uwriter.mk b/sw/CppunitTest_sw_uwriter.mk index 7626039ca23e..aad626119569 100644 --- a/sw/CppunitTest_sw_uwriter.mk +++ b/sw/CppunitTest_sw_uwriter.mk @@ -20,6 +20,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,sw_uwriter, \ sw/qa/core/test_ToxLinkProcessor \ sw/qa/core/test_ToxTextGenerator \ sw/qa/core/test_ToxMiscTest \ + sw/qa/core/test_rect \ sw/qa/core/test_region \ )) diff --git a/sw/inc/swrect.hxx b/sw/inc/swrect.hxx index 039d2b414704..83416cf3969f 100644 --- a/sw/inc/swrect.hxx +++ b/sw/inc/swrect.hxx @@ -79,6 +79,9 @@ public: SwRect &Union( const SwRect& rRect ); SwRect &Intersection( const SwRect& rRect ); + SwRect GetUnion( const SwRect& rRect ) const; + SwRect GetIntersection( const SwRect& rRect ) const; + // Same as Intersection, only assume that Rects are overlapping! SwRect &Intersection_( const SwRect &rRect ); @@ -359,6 +362,26 @@ inline bool SwRect::Overlaps( const SwRect& rRect ) const (Bottom()>= rRect.Top()); } +inline SwRect SwRect::GetUnion( const SwRect& rRect ) const +{ + return SwRect( + Point( std::min( Left(), rRect.Left()), + std::min( Top(), rRect.Top())), + Point( std::max( Right(), rRect.Right()), + std::max( Bottom(), rRect.Bottom()))); +} + +inline SwRect SwRect::GetIntersection( const SwRect& rRect ) const +{ + return Overlaps( rRect ) + ? SwRect( + Point( std::max( Left(), rRect.Left()), + std::max( Top(), rRect.Top())), + Point( std::min( Right(), rRect.Right()), + std::min( Bottom(), rRect.Bottom()))) + : SwRect(); +} + template< typename charT, typename traits > inline std::basic_ostream<charT, traits> & operator <<( diff --git a/sw/qa/core/test_rect.cxx b/sw/qa/core/test_rect.cxx new file mode 100644 index 000000000000..3c6a47907915 --- /dev/null +++ b/sw/qa/core/test_rect.cxx @@ -0,0 +1,67 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#include <cppunit/TestAssert.h> +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> + +#include <swrect.hxx> + +class RectUnittest : public CppUnit::TestFixture +{ +public: + void testUnion(); + void testIntersection(); + + CPPUNIT_TEST_SUITE(RectUnittest); + CPPUNIT_TEST(testUnion); + CPPUNIT_TEST(testIntersection); + CPPUNIT_TEST_SUITE_END(); +}; + +void RectUnittest::testUnion() +{ + SwRect rect1(Point(10, 10), Size(10, 10)); + SwRect rect2(Point(15, 15), Size(10, 10)); + SwRect rect3(Point(30, 30), Size(10, 10)); + SwRect tmp; + + tmp = rect1; + tmp.Union(rect2); + CPPUNIT_ASSERT_EQUAL(SwRect(Point(10, 10), Size(15, 15)), tmp); + CPPUNIT_ASSERT_EQUAL(SwRect(Point(10, 10), Size(15, 15)), rect1.GetUnion(rect2)); + + tmp = rect1; + tmp.Union(rect3); + CPPUNIT_ASSERT_EQUAL(SwRect(Point(10, 10), Size(30, 30)), tmp); + CPPUNIT_ASSERT_EQUAL(SwRect(Point(10, 10), Size(30, 30)), rect1.GetUnion(rect3)); +} + +void RectUnittest::testIntersection() +{ + SwRect rect1(Point(10, 10), Size(10, 10)); + SwRect rect2(Point(15, 15), Size(10, 10)); + SwRect rect3(Point(30, 30), Size(10, 10)); + SwRect tmp; + + tmp = rect1; + tmp.Intersection(rect2); + CPPUNIT_ASSERT_EQUAL(SwRect(Point(15, 15), Size(5, 5)), tmp); + CPPUNIT_ASSERT_EQUAL(SwRect(Point(15, 15), Size(5, 5)), rect1.GetIntersection(rect2)); + + tmp = rect1; + tmp.Intersection(rect3); + CPPUNIT_ASSERT(tmp.IsEmpty()); + CPPUNIT_ASSERT(rect1.GetIntersection(rect3).IsEmpty()); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(RectUnittest); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/bastyp/swregion.cxx b/sw/source/core/bastyp/swregion.cxx index 5b928a1ab9b0..b790bf14764f 100644 --- a/sw/source/core/bastyp/swregion.cxx +++ b/sw/source/core/bastyp/swregion.cxx @@ -202,10 +202,8 @@ void SwRegionRects::Compress( CompressType type ) // paints), the area of the union can be a little bit larger: // ( 9622 * 141.5 = 1361513 ~= a quarter (1/4) centimeter wider // than the width of an A4 page - SwRect aUnion( (*this)[i] ); - aUnion.Union( (*this)[j] ); - SwRect aInter( (*this)[i] ); - aInter.Intersection( (*this)[j] ); + SwRect aUnion = (*this)[i].GetUnion( (*this)[j] ); + SwRect aInter = (*this)[i].GetIntersection( (*this)[j] ); if ( CalcArea( (*this)[i] ) + CalcArea( (*this)[j] ) - CalcArea( aInter ) + nFuzzy >= CalcArea( aUnion ) ) { commit 48895bef52bd59e30751ece0c8a8d57ef7864936 Author: Luboš Luňák <l.lu...@collabora.com> AuthorDate: Fri Oct 1 10:25:00 2021 +0200 Commit: Luboš Luňák <l.lu...@collabora.com> CommitDate: Sat Oct 2 00:35:35 2021 +0200 make some simple SwRect functions inline They are not trivial, but having them inline should still give the compiler more chances to optimize these. Change-Id: Ia296282b64f64c801a057332047c12576d1d9604 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122912 Tested-by: Jenkins Reviewed-by: Luboš Luňák <l.lu...@collabora.com> diff --git a/sw/inc/swrect.hxx b/sw/inc/swrect.hxx index 3ba1f67d0882..039d2b414704 100644 --- a/sw/inc/swrect.hxx +++ b/sw/inc/swrect.hxx @@ -315,6 +315,51 @@ inline SwRect::SwRect( tools::Long X, tools::Long Y, tools::Long W, tools::Long { } +inline Point SwRect::Center() const +{ + return Point( Left() + Width() / 2, + Top() + Height() / 2 ); +} + +inline bool SwRect::Contains( const SwRect& rRect ) const +{ + const tools::Long nRight = Right(); + const tools::Long nBottom = Bottom(); + const tools::Long nrRight = rRect.Right(); + const tools::Long nrBottom= rRect.Bottom(); + return (Left() <= rRect.Left()) && (rRect.Left()<= nRight) && + (Left() <= nrRight) && (nrRight <= nRight) && + (Top() <= rRect.Top()) && (rRect.Top() <= nBottom) && + (Top() <= nrBottom) && (nrBottom <= nBottom); +} + +inline bool SwRect::Contains( const Point& rPoint ) const +{ + return (Left() <= rPoint.X()) && + (Top() <= rPoint.Y()) && + (Right() >= rPoint.X()) && + (Bottom()>= rPoint.Y()); +} + +// mouse moving of table borders +inline bool SwRect::IsNear( const Point& rPoint, tools::Long nTolerance ) const +{ + bool bIsNearby = (((Left() - nTolerance) <= rPoint.X()) && + ((Top() - nTolerance) <= rPoint.Y()) && + ((Right() + nTolerance) >= rPoint.X()) && + ((Bottom() + nTolerance) >= rPoint.Y())); + return Contains(rPoint) || bIsNearby; +} + +inline bool SwRect::Overlaps( const SwRect& rRect ) const +{ + return (Top() <= rRect.Bottom()) && + (Left() <= rRect.Right()) && + (Right() >= rRect.Left()) && + (Bottom()>= rRect.Top()); +} + + template< typename charT, typename traits > inline std::basic_ostream<charT, traits> & operator <<( std::basic_ostream<charT, traits> & stream, const SwRect& rectangle ) diff --git a/sw/source/core/bastyp/swrect.cxx b/sw/source/core/bastyp/swrect.cxx index 8f94cef23727..0825696f45c6 100644 --- a/sw/source/core/bastyp/swrect.cxx +++ b/sw/source/core/bastyp/swrect.cxx @@ -32,12 +32,6 @@ SwRect::SwRect( const tools::Rectangle &rRect ) : m_Size.setHeight(rRect.IsHeightEmpty() ? 0 : rRect.Bottom() - rRect.Top() + 1); } -Point SwRect::Center() const -{ - return Point( Left() + Width() / 2, - Top() + Height() / 2 ); -} - SwRect& SwRect::Union( const SwRect& rRect ) { if ( Top() > rRect.Top() ) @@ -90,44 +84,6 @@ SwRect& SwRect::Intersection_( const SwRect& rOther ) return *this; } -bool SwRect::Contains( const SwRect& rRect ) const -{ - const tools::Long nRight = Right(); - const tools::Long nBottom = Bottom(); - const tools::Long nrRight = rRect.Right(); - const tools::Long nrBottom= rRect.Bottom(); - return (Left() <= rRect.Left()) && (rRect.Left()<= nRight) && - (Left() <= nrRight) && (nrRight <= nRight) && - (Top() <= rRect.Top()) && (rRect.Top() <= nBottom) && - (Top() <= nrBottom) && (nrBottom <= nBottom); -} - -bool SwRect::Contains( const Point& rPoint ) const -{ - return (Left() <= rPoint.X()) && - (Top() <= rPoint.Y()) && - (Right() >= rPoint.X()) && - (Bottom()>= rPoint.Y()); -} - -// mouse moving of table borders -bool SwRect::IsNear( const Point& rPoint, tools::Long nTolerance ) const -{ - bool bIsNearby = (((Left() - nTolerance) <= rPoint.X()) && - ((Top() - nTolerance) <= rPoint.Y()) && - ((Right() + nTolerance) >= rPoint.X()) && - ((Bottom() + nTolerance) >= rPoint.Y())); - return Contains(rPoint) || bIsNearby; -} - -bool SwRect::Overlaps( const SwRect& rRect ) const -{ - return (Top() <= rRect.Bottom()) && - (Left() <= rRect.Right()) && - (Right() >= rRect.Left()) && - (Bottom()>= rRect.Top()); -} - void SwRect::Justify() { if ( m_Size.getHeight() < 0 )