basegfx/test/LengthUnitTest.cxx | 77 ++++++++++++++++--- include/basegfx/units/Length.hxx | 111 +++++++++++++++------------ include/basegfx/utils/RectangleWrapper.hxx | 77 +++++++++++++++++++ include/svx/svdotext.hxx | 28 +----- svx/source/svdraw/svdocirc.cxx | 2 svx/source/svdraw/svdotext.cxx | 31 ++++++- svx/source/svdraw/svdotxtr.cxx | 7 - tools/qa/cppunit/test_rectangle.cxx | 117 +++++++++++++++++++---------- 8 files changed, 319 insertions(+), 131 deletions(-)
New commits: commit e08d574336b1e53073d53f0b2e24466c409cd0c1 Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Tue Nov 22 13:33:30 2022 +0900 Commit: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> CommitDate: Tue Nov 22 13:44:44 2022 +0900 svx: use RectangleWrapper for maRectangle on SdrTextObj This is needed so we can now transition to use gfx::Length and gfx::Range2DL to define the object position and size. Change-Id: Ie683a869ba061f53d437bd1dfbe72fe454011730 diff --git a/include/basegfx/utils/RectangleWrapper.hxx b/include/basegfx/utils/RectangleWrapper.hxx new file mode 100644 index 000000000000..00586d6eae71 --- /dev/null +++ b/include/basegfx/utils/RectangleWrapper.hxx @@ -0,0 +1,77 @@ +/* -*- 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/. + */ + +#pragma once + +#include <basegfx/units/Length.hxx> +#include <tools/gen.hxx> + +namespace gfx +{ +/** + * Wrapps tools::Rectangle and Range2DL, to make it easier to incrementally + * transition to use Range2DL. + */ +class RectangleWrapper +{ +private: + gfx::Range2DL m_aRange; + mutable tools::Rectangle m_aRectangle; + +public: + RectangleWrapper() = default; + + RectangleWrapper(gfx::Length x1, gfx::Length y1, gfx::Length x2, gfx::Length y2) + : m_aRange(x1, y1, x2, y2) + { + } + + gfx::Range2DL const& getRange() const { return m_aRange; } + + void setRange(gfx::Range2DL const& rRange) { m_aRange = rRange; } + + tools::Rectangle const& getRectangle() const + { + m_aRectangle = gfx::length::toRectangleHmm(m_aRange); + return m_aRectangle; + } + + void setRectangle(tools::Rectangle const& rRectangle) + { + m_aRange = gfx::length::fromRectangleHmm(rRectangle); + } + + void setSize(sal_Int32 nWidth, sal_Int32 nHeight) + { + auto width = gfx::Length::hmm(nWidth - 1); + auto height = gfx::Length::hmm(nHeight - 1); + + m_aRange.setSize(width, height); + } + + void move(sal_Int32 nXDelta, sal_Int32 nYDelta) + { + auto deltaX = gfx::Length::hmm(nXDelta); + auto deltaY = gfx::Length::hmm(nYDelta); + + m_aRange.shift(deltaX, deltaY); + } + + void setPosition(sal_Int32 nX, sal_Int32 nY) + { + auto x = gfx::Length::hmm(nX); + auto y = gfx::Length::hmm(nY); + + m_aRange.setPosition(x, y); + } +}; + +} // end namespace gfx + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/svx/svdotext.hxx b/include/svx/svdotext.hxx index a1cccb0804a4..5ac836e03b6d 100644 --- a/include/svx/svdotext.hxx +++ b/include/svx/svdotext.hxx @@ -32,6 +32,7 @@ #include <drawinglayer/primitive2d/Primitive2DContainer.hxx> #include <memory> #include <vector> +#include <basegfx/utils/RectangleWrapper.hxx> #include <com/sun/star/drawing/TextFitToSizeType.hpp> @@ -165,32 +166,17 @@ protected: // The "aRect" is also the rect of RectObj and CircObj. // When mbTextFrame=true the text will be formatted into this rect // When mbTextFrame=false the text will be centered around its middle - tools::Rectangle maRectangle; + gfx::RectangleWrapper maRectangle; - tools::Rectangle const& getRectangle() const - { - return maRectangle; - } + tools::Rectangle const& getRectangle() const; - void setRectangle(tools::Rectangle const& rRectangle) - { - maRectangle = rRectangle; - } + void setRectangle(tools::Rectangle const& rRectangle); - void setRectangleSize(sal_Int32 nWidth, sal_Int32 nHeight) - { - maRectangle.SetSize(Size(nWidth, nHeight)); - } + void setRectangleSize(sal_Int32 nWidth, sal_Int32 nHeight); - void moveRectangle(sal_Int32 nXDelta, sal_Int32 nYDelta) - { - maRectangle.Move(nXDelta, nYDelta); - } + void moveRectangle(sal_Int32 nXDelta, sal_Int32 nYDelta); - void moveRectanglePosition(sal_Int32 nX, sal_Int32 nY) - { - maRectangle.SetPos(Point(nX, nY)); - } + void moveRectanglePosition(sal_Int32 nX, sal_Int32 nY); // The GeoStat contains the rotation and shear angles GeoStat maGeo; diff --git a/svx/source/svdraw/svdocirc.cxx b/svx/source/svdraw/svdocirc.cxx index ab8bc58ddf85..d8b47be4fbba 100644 --- a/svx/source/svdraw/svdocirc.cxx +++ b/svx/source/svdraw/svdocirc.cxx @@ -707,7 +707,6 @@ bool SdrCircObj::MovCreate(SdrDragStat& rStat) ImpCircUser* pU=static_cast<ImpCircUser*>(rStat.GetUser()); rStat.SetActionRect(pU->aR); setRectangle(pU->aR); // for ObjName - ImpJustifyRect(maRectangle); nStartAngle=pU->nStart; nEndAngle=pU->nEnd; SetBoundRectDirty(); @@ -1049,7 +1048,6 @@ void SdrCircObj::NbcSetSnapRect(const tools::Rectangle& rRect) NbcMove(Size(rRect.Left()-aSR0.Left(),rRect.Top()-aSR0.Top())); } else { setRectangle(rRect); - ImpJustifyRect(maRectangle); } SetBoundAndSnapRectsDirty(); SetXPolyDirty(); diff --git a/svx/source/svdraw/svdotext.cxx b/svx/source/svdraw/svdotext.cxx index 411269da2c07..e8f1698326d5 100644 --- a/svx/source/svdraw/svdotext.cxx +++ b/svx/source/svdraw/svdotext.cxx @@ -70,6 +70,7 @@ std::unique_ptr<sdr::contact::ViewContact> SdrTextObj::CreateObjectSpecificViewC SdrTextObj::SdrTextObj(SdrModel& rSdrModel) : SdrAttrObj(rSdrModel) + , maRectangle(0_hmm, 0_hmm, 0_hmm, 0_hmm) , mpEditingOutliner(nullptr) , meTextKind(SdrObjKind::Text) , maTextEditOffset(Point(0, 0)) @@ -87,6 +88,7 @@ SdrTextObj::SdrTextObj(SdrModel& rSdrModel) SdrTextObj::SdrTextObj(SdrModel& rSdrModel, SdrTextObj const & rSource) : SdrAttrObj(rSdrModel, rSource) + , maRectangle(rSource.maRectangle) , mpEditingOutliner(nullptr) , meTextKind(rSource.meTextKind) , maTextEditOffset(Point(0, 0)) @@ -101,7 +103,6 @@ SdrTextObj::SdrTextObj(SdrModel& rSdrModel, SdrTextObj const & rSource) // #i25616# mbSupportTextIndentingOnLineWidthChange = true; - maRectangle = rSource.maRectangle; maGeo = rSource.maGeo; maTextSize = rSource.maTextSize; @@ -156,6 +157,7 @@ SdrTextObj::SdrTextObj(SdrModel& rSdrModel, const tools::Rectangle& rNewRect) SdrTextObj::SdrTextObj(SdrModel& rSdrModel, SdrObjKind eNewTextKind) : SdrAttrObj(rSdrModel) + , maRectangle(0_hmm, 0_hmm, 0_hmm, 0_hmm) , mpEditingOutliner(nullptr) , meTextKind(eNewTextKind) , maTextEditOffset(Point(0, 0)) @@ -201,8 +203,6 @@ SdrTextObj::~SdrTextObj() void SdrTextObj::FitFrameToTextSize() { - ImpJustifyRect(maRectangle); - SdrText* pText = getActiveText(); if(pText==nullptr || !pText->GetOutlinerParaObject()) return; @@ -312,6 +312,31 @@ bool SdrTextObj::IsAutoGrowHeight() const return bRet; } +tools::Rectangle const& SdrTextObj::getRectangle() const +{ + maRectangle.getRectangle(); +} + +void SdrTextObj::setRectangle(tools::Rectangle const& rRectangle) +{ + maRectangle.setRectangle(rRectangle); +} + +void SdrTextObj::setRectangleSize(sal_Int32 nWidth, sal_Int32 nHeight) +{ + maRectangle.setSize(nWidth, nHeight); +} + +void SdrTextObj::moveRectangle(sal_Int32 nXDelta, sal_Int32 nYDelta) +{ + maRectangle.move(nXDelta, nYDelta); +} + +void SdrTextObj::moveRectanglePosition(sal_Int32 nX, sal_Int32 nY) +{ + maRectangle.setPosition(nX, nY); +} + bool SdrTextObj::IsAutoGrowWidth() const { if (!mbTextFrame) diff --git a/svx/source/svdraw/svdotxtr.cxx b/svx/source/svdraw/svdotxtr.cxx index 523a820a4165..e2a050c99dba 100644 --- a/svx/source/svdraw/svdotxtr.cxx +++ b/svx/source/svdraw/svdotxtr.cxx @@ -57,7 +57,6 @@ void SdrTextObj::NbcSetSnapRect(const tools::Rectangle& rRect) // No rotation or shear. setRectangle(rRect); - ImpJustifyRect(maRectangle); AdaptTextMinSize(); @@ -74,7 +73,6 @@ const tools::Rectangle& SdrTextObj::GetLogicRect() const void SdrTextObj::NbcSetLogicRect(const tools::Rectangle& rRect) { setRectangle(rRect); - ImpJustifyRect(maRectangle); AdaptTextMinSize(); @@ -125,7 +123,6 @@ void SdrTextObj::NbcResize(const Point& rRef, const Fraction& xFact, const Fract setRectangle(aRectangle); if (bYMirr) { - maRectangle.Normalize(); moveRectangle(aRectangle.Right() - aRectangle.Left(), aRectangle.Bottom() - aRectangle.Top()); maGeo.nRotationAngle=18000_deg100; maGeo.RecalcSinCos(); @@ -174,8 +171,6 @@ void SdrTextObj::NbcResize(const Point& rRef, const Fraction& xFact, const Fract } } - ImpJustifyRect(maRectangle); - AdaptTextMinSize(); if(mbTextFrame && !getSdrModelFromSdrObject().IsPasteResize()) @@ -225,7 +220,6 @@ void SdrTextObj::NbcShear(const Point& rRef, Degree100 /*nAngle*/, double tn, bo auto aRectangle = getRectangle(); Poly2Rect(aPol, aRectangle, maGeo); setRectangle(aRectangle); - ImpJustifyRect(maRectangle); if (mbTextFrame) { NbcAdjustTextFrameWidthAndHeight(); @@ -281,7 +275,6 @@ void SdrTextObj::NbcMirror(const Point& rRef1, const Point& rRef2) maGeo.RecalcTan(); } - ImpJustifyRect(maRectangle); if (mbTextFrame) { NbcAdjustTextFrameWidthAndHeight(); } commit cd449dd42c138540d5f116b2ea9d17a5d10003a0 Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Tue Nov 22 13:19:51 2022 +0900 Commit: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> CommitDate: Tue Nov 22 13:19:51 2022 +0900 gfx: cover more Range2D methods with a unit test Change-Id: I894781ad490bf179698b6c6c40dfb05f403eb654 diff --git a/basegfx/test/LengthUnitTest.cxx b/basegfx/test/LengthUnitTest.cxx index 8537280486af..b7b072493573 100644 --- a/basegfx/test/LengthUnitTest.cxx +++ b/basegfx/test/LengthUnitTest.cxx @@ -120,6 +120,18 @@ public: CPPUNIT_ASSERT_EQUAL(3_cm, aRange.getMinY()); CPPUNIT_ASSERT_EQUAL(3_cm, aRange.getMaxX()); CPPUNIT_ASSERT_EQUAL(40_mm, aRange.getMaxY()); + + aRange.setSize(5_cm, 2_cm); + CPPUNIT_ASSERT_EQUAL(2_cm, aRange.getMinX()); + CPPUNIT_ASSERT_EQUAL(3_cm, aRange.getMinY()); + CPPUNIT_ASSERT_EQUAL(7_cm, aRange.getMaxX()); + CPPUNIT_ASSERT_EQUAL(5_cm, aRange.getMaxY()); + + aRange.setPosition(0_cm, 0_cm); + CPPUNIT_ASSERT_EQUAL(0_cm, aRange.getMinX()); + CPPUNIT_ASSERT_EQUAL(0_cm, aRange.getMinY()); + CPPUNIT_ASSERT_EQUAL(5_cm, aRange.getMaxX()); + CPPUNIT_ASSERT_EQUAL(2_cm, aRange.getMaxY()); } void testInTuple() @@ -162,17 +174,60 @@ public: CPPUNIT_ASSERT_EQUAL(true, aSize == aSize + gfx::Size2DL(0_emu, 0_emu)); } - void testConversionToRectanle() + void testConversionToRectangle() { - tools::Rectangle aRectangle(10, 20, 110, 120); - gfx::Range2DL aRange = gfx::length::fromRectangleHmm(aRectangle); - CPPUNIT_ASSERT_EQUAL(10_hmm, aRange.getMinX()); - CPPUNIT_ASSERT_EQUAL(20_hmm, aRange.getMinY()); - CPPUNIT_ASSERT_EQUAL(110_hmm, aRange.getMaxX()); - CPPUNIT_ASSERT_EQUAL(120_hmm, aRange.getMaxY()); - - tools::Rectangle aRectangleConverted = gfx::length::toRectangleHmm(aRange); - CPPUNIT_ASSERT_EQUAL(aRectangle, aRectangleConverted); + { + tools::Rectangle aEmpty; + gfx::Range2DL aEmptyRange = gfx::length::fromRectangleHmm(aEmpty); + CPPUNIT_ASSERT_EQUAL(true, aEmptyRange.isEmpty()); + + tools::Rectangle aRectangle(10, 20, 110, 120); + gfx::Range2DL aRange = gfx::length::fromRectangleHmm(aRectangle); + CPPUNIT_ASSERT_EQUAL(10_hmm, aRange.getMinX()); + CPPUNIT_ASSERT_EQUAL(20_hmm, aRange.getMinY()); + CPPUNIT_ASSERT_EQUAL(110_hmm, aRange.getMaxX()); + CPPUNIT_ASSERT_EQUAL(120_hmm, aRange.getMaxY()); + + tools::Rectangle aRectangleConverted = gfx::length::toRectangleHmm(aRange); + CPPUNIT_ASSERT_EQUAL(aRectangle, aRectangleConverted); + } + { + tools::Rectangle aRectangle(10, 20, 110, 120); + gfx::Range2DL aRange = gfx::length::fromRectangleHmm(aRectangle); + + aRectangle.Move(1000, 1000); + aRange.shift(1000_hmm, 1000_hmm); + CPPUNIT_ASSERT_EQUAL(aRectangle, gfx::length::toRectangleHmm(aRange)); + } + { + tools::Rectangle aRectangle(10, 20, 110, 120); + gfx::Range2DL aRange = gfx::length::fromRectangleHmm(aRectangle); + + aRectangle.SetSize(Size(201, 201)); + aRange.setSize(200_hmm, 200_hmm); + CPPUNIT_ASSERT_EQUAL(aRectangle, gfx::length::toRectangleHmm(aRange)); + } + { + tools::Rectangle aRectangle(10, 20, 110, 120); + gfx::Range2DL aRange = gfx::length::fromRectangleHmm(aRectangle); + + aRectangle.SetPos(Point(500, 500)); + aRange.setPosition(500_hmm, 500_hmm); + CPPUNIT_ASSERT_EQUAL(aRectangle, gfx::length::toRectangleHmm(aRange)); + } + { + tools::Rectangle aRectangle(Point(0, 0), Size(0, 31)); + CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRectangle.Left()); + CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRectangle.Top()); + CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRectangle.GetWidth()); + CPPUNIT_ASSERT_EQUAL(tools::Long(31), aRectangle.GetHeight()); + + gfx::Range2DL aRange = gfx::length::fromRectangleHmm(aRectangle); + CPPUNIT_ASSERT_EQUAL(0_hmm, aRange.getMinX()); + CPPUNIT_ASSERT_EQUAL(0_hmm, aRange.getMinY()); + CPPUNIT_ASSERT_EQUAL(0_hmm, aRange.getMaxX()); + CPPUNIT_ASSERT_EQUAL(30_hmm, aRange.getMaxY()); + } } CPPUNIT_TEST_SUITE(LengthTest); @@ -180,7 +235,7 @@ public: CPPUNIT_TEST(testDivision); CPPUNIT_TEST(testInRange); CPPUNIT_TEST(testInTuple); - CPPUNIT_TEST(testConversionToRectanle); + CPPUNIT_TEST(testConversionToRectangle); CPPUNIT_TEST_SUITE_END(); }; commit a58516f129cede550056989717f5f8a2bae96696 Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Tue Nov 22 13:16:44 2022 +0900 Commit: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> CommitDate: Tue Nov 22 13:16:44 2022 +0900 gfx: move conversion functions to the end of file, handle isEmpty We need to check for isEmpty or the behaviour doesn't match Change-Id: I616ff67cc4894d39888e575682ccbf4d397efa6e diff --git a/include/basegfx/units/Length.hxx b/include/basegfx/units/Length.hxx index 76b9b1bbe9fc..33361ba39e39 100644 --- a/include/basegfx/units/Length.hxx +++ b/include/basegfx/units/Length.hxx @@ -29,55 +29,6 @@ struct LengthTraits typedef Length DifferenceType; }; -typedef basegfx::Range2D<gfx::Length, gfx::LengthTraits> Range2DL; -typedef basegfx::Tuple2D<gfx::Length> Tuple2DL; -typedef basegfx::Size2D<gfx::Length> Size2DL; - -namespace length -{ -static inline Size2DL fromSizeHmm(Size const& rSize) -{ - auto width = Length::hmm(rSize.getWidth()); - auto height = Length::hmm(rSize.getHeight()); - return Size2DL(width, height); -} - -static inline Size toSizeHmm(Size2DL const& rTuple) -{ - auto width = rTuple.getWidth().as_hmm(); - auto height = rTuple.getHeight().as_hmm(); - return Size(width, height); -} - -static inline Range2DL fromRectangleHmm(tools::Rectangle const& rRectangle) -{ - auto left = Length::hmm(rRectangle.Left()); - auto top = Length::hmm(rRectangle.Top()); - auto right = Length::hmm(rRectangle.Right()); - auto bottom = Length::hmm(rRectangle.Bottom()); - return Range2DL(left, top, right, bottom); -} - -static inline basegfx::B2DRange toB2DRange2DHmm(Range2DL const& rRange2D) -{ - auto left = rRange2D.getMinX().as_hmm(); - auto top = rRange2D.getMinY().as_hmm(); - auto right = rRange2D.getMaxX().as_hmm(); - auto bottom = rRange2D.getMaxY().as_hmm(); - return basegfx::B2DRange(left, top, right, bottom); -} - -static inline tools::Rectangle toRectangleHmm(Range2DL const& rRange2D) -{ - auto left = rRange2D.getMinX().as_hmm(); - auto top = rRange2D.getMinY().as_hmm(); - auto right = rRange2D.getMaxX().as_hmm(); - auto bottom = rRange2D.getMaxY().as_hmm(); - return tools::Rectangle(left, top, right, bottom); -} - -} // end namespace length - } // end namespace gfx constexpr gfx::Length operator"" _emu(unsigned long long value) { return gfx::Length::emu(value); } @@ -134,3 +85,65 @@ inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, t return stream << rLength.raw() << " (twip=" << rLength.as_twip() << ", hmm=" << rLength.as_hmm() << ")"; } + +namespace gfx +{ +typedef basegfx::Range2D<gfx::Length, gfx::LengthTraits> Range2DL; +typedef basegfx::Tuple2D<gfx::Length> Tuple2DL; +typedef basegfx::Size2D<gfx::Length> Size2DL; + +namespace length +{ +static inline Size2DL fromSizeHmm(Size const& rSize) +{ + if (rSize.IsEmpty()) + return Size2DL(0_mm, 0_mm); + auto width = Length::hmm(rSize.getWidth()); + auto height = Length::hmm(rSize.getHeight()); + return Size2DL(width, height); +} + +static inline Size toSizeHmm(Size2DL const& rSize) +{ + auto width = rSize.getWidth().as_hmm(); + auto height = rSize.getHeight().as_hmm(); + return Size(width, height); +} + +static inline Range2DL fromRectangleHmm(tools::Rectangle const& rRectangle) +{ + if (rRectangle.IsWidthEmpty() && rRectangle.IsHeightEmpty()) + return Range2DL(); + + auto left = Length::hmm(rRectangle.Left()); + auto top = Length::hmm(rRectangle.Top()); + auto right = Length::hmm(rRectangle.Right()); + auto bottom = Length::hmm(rRectangle.Bottom()); + + return Range2DL(left, top, right, bottom); +} + +static inline basegfx::B2DRange toB2DRange2DHmm(Range2DL const& rRange2D) +{ + if (rRange2D.isEmpty()) + return basegfx::B2DRange(); + auto left = rRange2D.getMinX().as_hmm(); + auto top = rRange2D.getMinY().as_hmm(); + auto right = rRange2D.getMaxX().as_hmm(); + auto bottom = rRange2D.getMaxY().as_hmm(); + return basegfx::B2DRange(left, top, right, bottom); +} + +static inline tools::Rectangle toRectangleHmm(Range2DL const& rRange2D) +{ + if (rRange2D.isEmpty()) + return tools::Rectangle(); + auto left = rRange2D.getMinX().as_hmm(); + auto top = rRange2D.getMinY().as_hmm(); + auto right = rRange2D.getMaxX().as_hmm(); + auto bottom = rRange2D.getMaxY().as_hmm(); + return tools::Rectangle(left, top, right, bottom); +} + +} // end namespace gfx +} // end namespace length commit c162d6e5c54965839c8333323a67e6933484193d Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Tue Nov 22 13:13:53 2022 +0900 Commit: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> CommitDate: Tue Nov 22 13:13:53 2022 +0900 tools: rearrange Rectangle test, add construction test case Change-Id: I735600181665100e8540b6f5f14ffebfe6f33371 diff --git a/tools/qa/cppunit/test_rectangle.cxx b/tools/qa/cppunit/test_rectangle.cxx index 02b355ad0576..12e46910bc2f 100644 --- a/tools/qa/cppunit/test_rectangle.cxx +++ b/tools/qa/cppunit/test_rectangle.cxx @@ -14,10 +14,13 @@ namespace { -class Test : public CppUnit::TestFixture +class RectangleTest : public CppUnit::TestFixture { public: - void test_rectangle(); + void testConstruction(); + void testOpenClosedSize(); + void testUnitConvesion(); + void testSetOperators(); void test_rectnormalize_alreadynormal(); void test_rectnormalize_zerorect(); void test_rectnormalize_reverse_topleft_bottomright(); @@ -26,8 +29,11 @@ public: void test_rectnormalize_zerowidth_top_bottom_reversed(); void test_rectnormalize_zeroheight_left_right_reversed(); - CPPUNIT_TEST_SUITE(Test); - CPPUNIT_TEST(test_rectangle); + CPPUNIT_TEST_SUITE(RectangleTest); + CPPUNIT_TEST(testConstruction); + CPPUNIT_TEST(testOpenClosedSize); + CPPUNIT_TEST(testUnitConvesion); + CPPUNIT_TEST(testSetOperators); CPPUNIT_TEST(test_rectnormalize_zerorect); CPPUNIT_TEST(test_rectnormalize_alreadynormal); CPPUNIT_TEST(test_rectnormalize_reverse_topleft_bottomright); @@ -38,7 +44,49 @@ public: CPPUNIT_TEST_SUITE_END(); }; -void Test::test_rectangle() +void RectangleTest::testConstruction() +{ + { + tools::Rectangle aRect1(Point(), Size(0, 20)); + CPPUNIT_ASSERT_EQUAL(true, aRect1.IsEmpty()); + CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRect1.getOpenWidth()); + + tools::Rectangle aRect2{ Point(), Point(0, 20) }; + CPPUNIT_ASSERT_EQUAL(false, aRect2.IsEmpty()); + CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRect2.getOpenWidth()); + + tools::Rectangle aRect3(0, 0, 0, 20); + CPPUNIT_ASSERT_EQUAL(false, aRect3.IsEmpty()); + CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRect3.getOpenWidth()); + } + { + constexpr tools::Rectangle aRect(Point(), Size(-1, -2)); + static_assert(!aRect.IsEmpty()); + static_assert(aRect.Right() == 0); + static_assert(aRect.Bottom() == -1); + + tools::Rectangle aRect2; + aRect2.SetSize(Size(-1, -2)); + CPPUNIT_ASSERT_EQUAL(aRect, aRect2); + + constexpr tools::Rectangle aRect3(Point(), Size(0, 0)); + static_assert(aRect3.IsEmpty()); + static_assert(aRect3.Right() == 0); + static_assert(aRect3.Bottom() == 0); + + constexpr tools::Rectangle aRect4(Point(), Size(1, 1)); + static_assert(!aRect4.IsEmpty()); + static_assert(aRect4.Right() == 0); + static_assert(aRect4.Bottom() == 0); + + constexpr tools::Rectangle aRect5(Point(), Size(-1, -1)); + static_assert(!aRect5.IsEmpty()); + static_assert(aRect5.Right() == 0); + static_assert(aRect5.Bottom() == 0); + } +} + +void RectangleTest::testOpenClosedSize() { { tools::Rectangle aRect(1, 1, 1, 1); @@ -71,18 +119,10 @@ void Test::test_rectangle() aRect.SetPosY(12); CPPUNIT_ASSERT_EQUAL(tools::Long(1), aRect.GetWidth()); } +} - { - constexpr tools::Rectangle aRect(Point(), Size(-1, -2)); - static_assert(!aRect.IsEmpty()); - static_assert(aRect.Right() == 0); - static_assert(aRect.Bottom() == -1); - - tools::Rectangle aRect2; - aRect2.SetSize(Size(-1, -2)); - CPPUNIT_ASSERT_EQUAL(aRect, aRect2); - } - +void RectangleTest::testUnitConvesion() +{ { constexpr tools::Rectangle aRectTwip(100, 100, 100, 100); constexpr tools::Rectangle aRectMm100( @@ -106,24 +146,25 @@ void Test::test_rectangle() static_assert(aRectMm100.GetWidth() == 0); static_assert(aRectMm100.GetHeight() == 0); } +} - { - constexpr tools::Rectangle rect(Point(0, 0), Size(20, 20)); - constexpr tools::Rectangle inside(Point(10, 10), Size(10, 10)); - constexpr tools::Rectangle overlap(Point(10, 10), Size(20, 20)); - constexpr tools::Rectangle outside(Point(20, 20), Size(10, 10)); - CPPUNIT_ASSERT(rect.Contains(inside)); - CPPUNIT_ASSERT(rect.Contains(rect)); - CPPUNIT_ASSERT(!rect.Contains(overlap)); - CPPUNIT_ASSERT(!rect.Contains(outside)); - CPPUNIT_ASSERT(rect.Overlaps(inside)); - CPPUNIT_ASSERT(rect.Overlaps(rect)); - CPPUNIT_ASSERT(rect.Overlaps(overlap)); - CPPUNIT_ASSERT(!rect.Overlaps(outside)); - } +void RectangleTest::testSetOperators() +{ + constexpr tools::Rectangle rect(Point(0, 0), Size(20, 20)); + constexpr tools::Rectangle inside(Point(10, 10), Size(10, 10)); + constexpr tools::Rectangle overlap(Point(10, 10), Size(20, 20)); + constexpr tools::Rectangle outside(Point(20, 20), Size(10, 10)); + CPPUNIT_ASSERT(rect.Contains(inside)); + CPPUNIT_ASSERT(rect.Contains(rect)); + CPPUNIT_ASSERT(!rect.Contains(overlap)); + CPPUNIT_ASSERT(!rect.Contains(outside)); + CPPUNIT_ASSERT(rect.Overlaps(inside)); + CPPUNIT_ASSERT(rect.Overlaps(rect)); + CPPUNIT_ASSERT(rect.Overlaps(overlap)); + CPPUNIT_ASSERT(!rect.Overlaps(outside)); } -void Test::test_rectnormalize_alreadynormal() +void RectangleTest::test_rectnormalize_alreadynormal() { Point aTopLeft(0, 0); Point aBottomRight(1, 1); @@ -135,7 +176,7 @@ void Test::test_rectnormalize_alreadynormal() CPPUNIT_ASSERT_EQUAL(aRect.BottomRight(), aBottomRight); } -void Test::test_rectnormalize_zerorect() +void RectangleTest::test_rectnormalize_zerorect() { Point aTopLeft(53, 53); Point aBottomRight(53, 53); @@ -147,7 +188,7 @@ void Test::test_rectnormalize_zerorect() CPPUNIT_ASSERT_EQUAL(aRect.BottomRight(), aBottomRight); } -void Test::test_rectnormalize_reverse_topleft_bottomright() +void RectangleTest::test_rectnormalize_reverse_topleft_bottomright() { Point aPoint1(1, 1); Point aPoint2(0, 0); @@ -159,7 +200,7 @@ void Test::test_rectnormalize_reverse_topleft_bottomright() CPPUNIT_ASSERT_EQUAL_MESSAGE("BottomRight() is wrong", Point(1, 1), aRect.BottomRight()); } -void Test::test_rectnormalize_topright_bottomleft() +void RectangleTest::test_rectnormalize_topright_bottomleft() { Point aPoint1(1, 0); Point aPoint2(0, 1); @@ -171,7 +212,7 @@ void Test::test_rectnormalize_topright_bottomleft() CPPUNIT_ASSERT_EQUAL_MESSAGE("BottomRight() is wrong", Point(1, 1), aRect.BottomRight()); } -void Test::test_rectnormalize_bottomleft_topright() +void RectangleTest::test_rectnormalize_bottomleft_topright() { Point aPoint1(0, 1); Point aPoint2(1, 0); @@ -183,7 +224,7 @@ void Test::test_rectnormalize_bottomleft_topright() CPPUNIT_ASSERT_EQUAL_MESSAGE("BottomRight() is wrong", Point(1, 1), aRect.BottomRight()); } -void Test::test_rectnormalize_zerowidth_top_bottom_reversed() +void RectangleTest::test_rectnormalize_zerowidth_top_bottom_reversed() { Point aPoint1(0, 1); Point aPoint2(0, 0); @@ -195,7 +236,7 @@ void Test::test_rectnormalize_zerowidth_top_bottom_reversed() CPPUNIT_ASSERT_EQUAL_MESSAGE("BottomRight() is wrong", Point(0, 1), aRect.BottomRight()); } -void Test::test_rectnormalize_zeroheight_left_right_reversed() +void RectangleTest::test_rectnormalize_zeroheight_left_right_reversed() { Point aPoint1(1, 0); Point aPoint2(0, 0); @@ -207,7 +248,7 @@ void Test::test_rectnormalize_zeroheight_left_right_reversed() CPPUNIT_ASSERT_EQUAL_MESSAGE("BottomRight() is wrong", Point(1, 0), aRect.BottomRight()); } -CPPUNIT_TEST_SUITE_REGISTRATION(Test); +CPPUNIT_TEST_SUITE_REGISTRATION(RectangleTest); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */