Introduce 'scale' to Point/Size/Rectangle, to simplify anisotropic scaling

Change-Id: I12c25838e8eec8d05e43b593790847c626edde31
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/124638
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
This commit is contained in:
Mike Kaganski
2021-11-03 13:04:24 +03:00
parent 9c6aa26092
commit 7e23fec4b7
3 changed files with 49 additions and 28 deletions

View File

@@ -315,15 +315,11 @@ void ChartWindow::LogicInvalidate(const tools::Rectangle* pRectangle)
if (!IsMapModeEnabled()) if (!IsMapModeEnabled())
{ {
aRectangle.SetLeft( o3tl::convert(aRectangle.Left(), scaleX.GetDenominator(), scaleX.GetNumerator()) ); aRectangle = aRectangle.scale(scaleX.GetDenominator(), scaleX.GetNumerator(),
aRectangle.SetRight( o3tl::convert(aRectangle.Right(), scaleX.GetDenominator(), scaleX.GetNumerator()) ); scaleY.GetDenominator(), scaleY.GetNumerator());
aRectangle.SetTop( o3tl::convert(aRectangle.Top(), scaleY.GetDenominator(), scaleY.GetNumerator()) );
aRectangle.SetBottom( o3tl::convert(aRectangle.Bottom(), scaleY.GetDenominator(), scaleY.GetNumerator()) );
} }
Point aOffset = this->GetOffsetPixelFrom(*pEditWin); Point aOffset = this->GetOffsetPixelFrom(*pEditWin).scale(nXNum, nXDen, nYNum, nYDen);
aOffset.setX( o3tl::convert(aOffset.X(), nXNum, nXDen) );
aOffset.setY( o3tl::convert(aOffset.Y(), nYNum, nYDen) );
aRectangle = tools::Rectangle(aRectangle.TopLeft() + aOffset, aRectangle.GetSize()); aRectangle = tools::Rectangle(aRectangle.TopLeft() + aOffset, aRectangle.GetSize());
} }

View File

@@ -106,6 +106,10 @@ public:
Pair const & toPair() const { return *this; } Pair const & toPair() const { return *this; }
Pair & toPair() { return *this; } Pair & toPair() { return *this; }
// Scales relative to 0,0
constexpr inline Point scale(sal_Int64 nMulX, sal_Int64 nDivX,
sal_Int64 nMulY, sal_Int64 nDivY) const;
using Pair::toString; using Pair::toString;
}; };
@@ -173,14 +177,19 @@ inline bool operator !=(Point const & p1, Point const & p2)
return !(p1 == p2); return !(p1 == p2);
} }
constexpr inline Point Point::scale(sal_Int64 nMulX, sal_Int64 nDivX, sal_Int64 nMulY, sal_Int64 nDivY) const
{
return Point(o3tl::convert(getX(), nMulX, nDivX),
o3tl::convert(getY(), nMulY, nDivY));
}
namespace o3tl namespace o3tl
{ {
constexpr Point convert(const Point& rPoint, o3tl::Length eFrom, o3tl::Length eTo) constexpr Point convert(const Point& rPoint, o3tl::Length eFrom, o3tl::Length eTo)
{ {
return Point( const auto [num, den] = o3tl::getConversionMulDiv(eFrom, eTo);
o3tl::convert(rPoint.getX(), eFrom, eTo), return rPoint.scale(num, den, num, den);
o3tl::convert(rPoint.getY(), eFrom, eTo));
} }
} // end o3tl } // end o3tl
@@ -234,6 +243,9 @@ public:
friend inline Size operator*( const Size &rVal1, const tools::Long nVal2 ); friend inline Size operator*( const Size &rVal1, const tools::Long nVal2 );
friend inline Size operator/( const Size &rVal1, const tools::Long nVal2 ); friend inline Size operator/( const Size &rVal1, const tools::Long nVal2 );
constexpr inline Size scale(sal_Int64 nMulX, sal_Int64 nDivX,
sal_Int64 nMulY, sal_Int64 nDivY) const;
}; };
inline bool operator ==(Size const & s1, Size const & s2) inline bool operator ==(Size const & s1, Size const & s2)
@@ -294,14 +306,20 @@ inline Size operator/( const Size &rVal1, const tools::Long nVal2 )
return Size( rVal1.nA/nVal2, rVal1.nB/nVal2 ); return Size( rVal1.nA/nVal2, rVal1.nB/nVal2 );
} }
constexpr inline Size Size::scale(sal_Int64 nMulX, sal_Int64 nDivX,
sal_Int64 nMulY, sal_Int64 nDivY) const
{
return Size(o3tl::convert(Width(), nMulX, nDivX),
o3tl::convert(Height(), nMulY, nDivY));
}
namespace o3tl namespace o3tl
{ {
constexpr Size convert(const Size& rSize, o3tl::Length eFrom, o3tl::Length eTo) constexpr Size convert(const Size& rSize, o3tl::Length eFrom, o3tl::Length eTo)
{ {
return Size( const auto [num, den] = o3tl::getConversionMulDiv(eFrom, eTo);
o3tl::convert(rSize.Width(), eFrom, eTo), return rSize.scale(num, den, num, den);
o3tl::convert(rSize.Height(), eFrom, eTo));
} }
} // end o3tl } // end o3tl
@@ -595,6 +613,10 @@ public:
void SaturatingSetPosX(tools::Long x); void SaturatingSetPosX(tools::Long x);
void SaturatingSetPosY(tools::Long y); void SaturatingSetPosY(tools::Long y);
// Scales relative to 0,0
constexpr inline tools::Rectangle scale(sal_Int64 nMulX, sal_Int64 nDivX,
sal_Int64 nMulY, sal_Int64 nDivY) const;
private: private:
tools::Long nLeft = 0; tools::Long nLeft = 0;
tools::Long nTop = 0; tools::Long nTop = 0;
@@ -718,20 +740,27 @@ inline Rectangle operator - ( const Rectangle& rRect, const Point& rPt )
} }
constexpr inline tools::Rectangle tools::Rectangle::scale(sal_Int64 nMulX, sal_Int64 nDivX,
sal_Int64 nMulY, sal_Int64 nDivY) const
{
// 1. Create an empty rectangle with correct left and top
tools::Rectangle aRect(o3tl::convert(Left(), nMulX, nDivX),
o3tl::convert(Top(), nMulY, nDivY));
// 2. If source has width/height, set respective right and bottom
if (!IsWidthEmpty())
aRect.SetRight(o3tl::convert(Right(), nMulX, nDivX));
if (!IsHeightEmpty())
aRect.SetBottom(o3tl::convert(Bottom(), nMulY, nDivY));
return aRect;
}
namespace o3tl namespace o3tl
{ {
constexpr tools::Rectangle convert(const tools::Rectangle& rRectangle, o3tl::Length eFrom, o3tl::Length eTo) constexpr tools::Rectangle convert(const tools::Rectangle& rRectangle, o3tl::Length eFrom, o3tl::Length eTo)
{ {
// 1. Create an empty rectangle with correct left and top const auto [num, den] = o3tl::getConversionMulDiv(eFrom, eTo);
tools::Rectangle aRect(o3tl::convert(rRectangle.Left(), eFrom, eTo), return rRectangle.scale(num, den, num, den);
o3tl::convert(rRectangle.Top(), eFrom, eTo));
// 2. If source has width/height, set respective right and bottom
if (!rRectangle.IsWidthEmpty())
aRect.SetRight(o3tl::convert(rRectangle.Right(), eFrom, eTo));
if (!rRectangle.IsHeightEmpty())
aRect.SetBottom(o3tl::convert(rRectangle.Bottom(), eFrom, eTo));
return aRect;
} }
} // end o3tl } // end o3tl

View File

@@ -141,12 +141,8 @@ tools::Rectangle LokChartHelper::GetChartBoundingBox()
const auto nYNum = p.first * scaleY.GetDenominator(); const auto nYNum = p.first * scaleY.GetDenominator();
const auto nYDen = p.second * scaleY.GetNumerator(); const auto nYDen = p.second * scaleY.GetNumerator();
Point aOffset = pWindow->GetOffsetPixelFrom(*pRootWin); Point aOffset = pWindow->GetOffsetPixelFrom(*pRootWin).scale(nXNum, nXDen, nYNum, nYDen);
aOffset.setX( o3tl::convert(aOffset.X(), nXNum, nXDen) ); Size aSize = pWindow->GetSizePixel().scale(nXNum, nXDen, nYNum, nYDen);
aOffset.setY( o3tl::convert(aOffset.Y(), nYNum, nYDen) );
Size aSize = pWindow->GetSizePixel();
aSize.setWidth( o3tl::convert(aSize.Width(), nXNum, nXDen) );
aSize.setHeight( o3tl::convert(aSize.Height(), nYNum, nYDen) );
aBBox = tools::Rectangle(aOffset, aSize); aBBox = tools::Rectangle(aOffset, aSize);
} }
} }