chart2/source/controller/main/ChartWindow.cxx | 41 +++++++++++++++----------- include/o3tl/unit_conversion.hxx | 3 + sd/source/ui/unoidl/unomodel.cxx | 7 +--- sfx2/source/view/lokcharthelper.cxx | 20 +++++++----- sw/source/uibase/uno/unotxdoc.cxx | 13 +++----- 5 files changed, 49 insertions(+), 35 deletions(-)
New commits: commit 17dface79321c87b2d30a53a67aeda2e9f9dfff6 Author: Mike Kaganski <mike.kagan...@collabora.com> AuthorDate: Wed Nov 3 09:58:25 2021 +0300 Commit: Mike Kaganski <mike.kagan...@collabora.com> CommitDate: Wed Nov 3 09:14:25 2021 +0100 Drop TWIPS_PER_PIXEL and use o3tl::convert Change-Id: I8ee3fddaccf6809c95319db1da9a1d2897d00c25 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/124626 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com> diff --git a/chart2/source/controller/main/ChartWindow.cxx b/chart2/source/controller/main/ChartWindow.cxx index 811016de28c1..7c114d5fff72 100644 --- a/chart2/source/controller/main/ChartWindow.cxx +++ b/chart2/source/controller/main/ChartWindow.cxx @@ -30,8 +30,6 @@ #include <sfx2/lokhelper.hxx> #include <comphelper/lok.hxx> -#define TWIPS_PER_PIXEL 15 - using namespace ::com::sun::star; namespace @@ -307,20 +305,25 @@ void ChartWindow::LogicInvalidate(const tools::Rectangle* pRectangle) if (pEditWin) { MapMode aCWMapMode = GetMapMode(); - double fXScale( aCWMapMode.GetScaleX() ); - double fYScale( aCWMapMode.GetScaleY() ); + constexpr auto p = o3tl::getConversionMulDiv(o3tl::Length::px, o3tl::Length::twip); + const auto& scaleX = aCWMapMode.GetScaleX(); + const auto& scaleY = aCWMapMode.GetScaleY(); + const auto nXNum = p.first * scaleX.GetDenominator(); + const auto nXDen = p.second * scaleX.GetNumerator(); + const auto nYNum = p.first * scaleY.GetDenominator(); + const auto nYDen = p.second * scaleY.GetNumerator(); if (!IsMapModeEnabled()) { - aRectangle.SetLeft( aRectangle.Left() / fXScale ); - aRectangle.SetRight( aRectangle.Right() / fXScale ); - aRectangle.SetTop( aRectangle.Top() / fYScale ); - aRectangle.SetBottom( aRectangle.Bottom() / fYScale ); + aRectangle.SetLeft( o3tl::convert(aRectangle.Left(), scaleX.GetDenominator(), scaleX.GetNumerator()) ); + aRectangle.SetRight( o3tl::convert(aRectangle.Right(), scaleX.GetDenominator(), scaleX.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); - aOffset.setX( aOffset.X() * (TWIPS_PER_PIXEL / fXScale) ); - aOffset.setY( aOffset.Y() * (TWIPS_PER_PIXEL / fYScale) ); + aOffset.setX( o3tl::convert(aOffset.X(), nXNum, nXDen) ); + aOffset.setY( o3tl::convert(aOffset.Y(), nYNum, nYDen) ); aRectangle = tools::Rectangle(aRectangle.TopLeft() + aOffset, aRectangle.GetSize()); } @@ -374,14 +377,20 @@ tools::Rectangle ChartWindow::GetBoundingBox() // In all cases, the following code fragment // returns the chart bounding box in twips. MapMode aCWMapMode = GetMapMode(); - double fXScale( aCWMapMode.GetScaleX() ); - double fYScale( aCWMapMode.GetScaleY() ); + constexpr auto p = o3tl::getConversionMulDiv(o3tl::Length::px, o3tl::Length::twip); + const auto& scaleX = aCWMapMode.GetScaleX(); + const auto& scaleY = aCWMapMode.GetScaleY(); + const auto nXNum = p.first * scaleX.GetDenominator(); + const auto nXDen = p.second * scaleX.GetNumerator(); + const auto nYNum = p.first * scaleY.GetDenominator(); + const auto nYDen = p.second * scaleY.GetNumerator(); + Point aOffset = GetOffsetPixelFrom(*pRootWin); - aOffset.setX( aOffset.X() * (TWIPS_PER_PIXEL / fXScale) ); - aOffset.setY( aOffset.Y() * (TWIPS_PER_PIXEL / fYScale) ); + aOffset.setX( o3tl::convert(aOffset.X(), nXNum, nXDen) ); + aOffset.setY( o3tl::convert(aOffset.Y(), nYNum, nYDen) ); Size aSize = GetSizePixel(); - aSize.setWidth( aSize.Width() * (TWIPS_PER_PIXEL / fXScale) ); - aSize.setHeight( aSize.Height() * (TWIPS_PER_PIXEL / fYScale) ); + aSize.setWidth( o3tl::convert(aSize.Width(), nXNum, nXDen) ); + aSize.setHeight( o3tl::convert(aSize.Height(), nYNum, nYDen) ); aBBox = tools::Rectangle(aOffset, aSize); } return aBBox; diff --git a/include/o3tl/unit_conversion.hxx b/include/o3tl/unit_conversion.hxx index 7bee0ed397e5..27fb184d52a4 100644 --- a/include/o3tl/unit_conversion.hxx +++ b/include/o3tl/unit_conversion.hxx @@ -142,6 +142,9 @@ template <int N> constexpr auto prepareMDArray(const m_and_d (&mdBase)[N]) return a; } +// A generic template used for fundamental arithmetic types +template <typename U> constexpr sal_Int64 md(U i, U /*j*/) { return i; } + // Length units implementation // Array of conversion quotients for mm, used to build final conversion table. Entries diff --git a/sd/source/ui/unoidl/unomodel.cxx b/sd/source/ui/unoidl/unomodel.cxx index c629f2ca87d0..d4304815519f 100644 --- a/sd/source/ui/unoidl/unomodel.cxx +++ b/sd/source/ui/unoidl/unomodel.cxx @@ -52,6 +52,7 @@ #include <editeng/UnoForbiddenCharsTable.hxx> #include <svx/svdoutl.hxx> #include <o3tl/safeint.hxx> +#include <o3tl/unit_conversion.hxx> #include <svx/UnoNamespaceMap.hxx> #include <svx/svdlayer.hxx> #include <svx/svdsob.hxx> @@ -125,8 +126,6 @@ #include <tools/json_writer.hxx> #include <tools/UnitConversion.hxx> -#define TWIPS_PER_PIXEL 15 - using namespace ::cppu; using namespace ::com::sun::star; using namespace ::sd; @@ -2506,7 +2505,7 @@ void SdXImpressDocument::postMouseEvent(int nType, int nX, int nY, int nCount, i if (!pViewShell) return; - double fScale = 1.0/TWIPS_PER_PIXEL; + constexpr double fScale = o3tl::convert(1.0, o3tl::Length::twip, o3tl::Length::px); // check if user hit a chart which is being edited by him LokChartHelper aChartHelper(pViewShell->GetViewShell()); @@ -2578,7 +2577,7 @@ void SdXImpressDocument::setGraphicSelection(int nType, int nX, int nY) if (!pViewShell) return; - double fScale = 1.0/TWIPS_PER_PIXEL; + constexpr double fScale = o3tl::convert(1.0, o3tl::Length::twip, o3tl::Length::px); LokChartHelper aChartHelper(pViewShell->GetViewShell()); if (aChartHelper.setGraphicSelection(nType, nX, nY, fScale, fScale)) diff --git a/sfx2/source/view/lokcharthelper.cxx b/sfx2/source/view/lokcharthelper.cxx index 1097c0a3c39b..7220621a566c 100644 --- a/sfx2/source/view/lokcharthelper.cxx +++ b/sfx2/source/view/lokcharthelper.cxx @@ -24,8 +24,6 @@ #include <com/sun/star/frame/XDispatch.hpp> #include <com/sun/star/chart2/XChartDocument.hpp> -#define TWIPS_PER_PIXEL 15 - using namespace com::sun::star; namespace { @@ -135,14 +133,20 @@ tools::Rectangle LokChartHelper::GetChartBoundingBox() // In all cases, the following code fragment // returns the chart bounding box in twips. const MapMode& aCWMapMode = pWindow->GetMapMode(); - double fXScale( aCWMapMode.GetScaleX() ); - double fYScale( aCWMapMode.GetScaleY() ); + constexpr auto p = o3tl::getConversionMulDiv(o3tl::Length::px, o3tl::Length::twip); + const auto& scaleX = aCWMapMode.GetScaleX(); + const auto& scaleY = aCWMapMode.GetScaleY(); + const auto nXNum = p.first * scaleX.GetDenominator(); + const auto nXDen = p.second * scaleX.GetNumerator(); + const auto nYNum = p.first * scaleY.GetDenominator(); + const auto nYDen = p.second * scaleY.GetNumerator(); + Point aOffset = pWindow->GetOffsetPixelFrom(*pRootWin); - aOffset.setX( aOffset.X() * (TWIPS_PER_PIXEL / fXScale) ); - aOffset.setY( aOffset.Y() * (TWIPS_PER_PIXEL / fYScale) ); + aOffset.setX( o3tl::convert(aOffset.X(), nXNum, nXDen) ); + aOffset.setY( o3tl::convert(aOffset.Y(), nYNum, nYDen) ); Size aSize = pWindow->GetSizePixel(); - aSize.setWidth( aSize.Width() * (TWIPS_PER_PIXEL / fXScale) ); - aSize.setHeight( aSize.Height() * (TWIPS_PER_PIXEL / fYScale) ); + aSize.setWidth( o3tl::convert(aSize.Width(), nXNum, nXDen) ); + aSize.setHeight( o3tl::convert(aSize.Height(), nYNum, nYDen) ); aBBox = tools::Rectangle(aOffset, aSize); } } diff --git a/sw/source/uibase/uno/unotxdoc.cxx b/sw/source/uibase/uno/unotxdoc.cxx index 6a04c98cc931..c09a9a17c8f2 100644 --- a/sw/source/uibase/uno/unotxdoc.cxx +++ b/sw/source/uibase/uno/unotxdoc.cxx @@ -166,8 +166,6 @@ #include <IDocumentOutlineNodes.hxx> #include <SearchResultLocator.hxx> -#define TWIPS_PER_PIXEL 15 - using namespace ::com::sun::star; using namespace ::com::sun::star::text; using namespace ::com::sun::star::i18n; @@ -3178,11 +3176,12 @@ void SwXTextDocument::setClientZoom(int nTilePixelWidth_, int /*nTilePixelHeight return; SwViewShell* pWrtViewShell = m_pDocShell->GetWrtShell(); - double fScale = nTilePixelWidth_ * TWIPS_PER_PIXEL / (nTileTwipWidth_ * 1.0); + double fScale = 100.0 * nTilePixelWidth_ / nTileTwipWidth_ + * o3tl::convert(1.0, o3tl::Length::px, o3tl::Length::twip); SwViewOption aOption(*(pWrtViewShell->GetViewOptions())); - if (aOption.GetZoom() != fScale * 100) + if (aOption.GetZoom() != fScale) { - aOption.SetZoom(fScale * 100); + aOption.SetZoom(fScale); pWrtViewShell->ApplyViewOptions(aOption); // Changing the zoom value doesn't always trigger the updating of @@ -3489,7 +3488,7 @@ void SwXTextDocument::postMouseEvent(int nType, int nX, int nY, int nCount, int } SwViewOption aOption(*(pWrtViewShell->GetViewOptions())); - double fScale = aOption.GetZoom() / (TWIPS_PER_PIXEL * 100.0); + double fScale = aOption.GetZoom() / o3tl::convert(100.0, o3tl::Length::px, o3tl::Length::twip); // check if the user hit a chart which is being edited by this view SfxViewShell* pViewShell = m_pDocShell->GetView(); @@ -3580,7 +3579,7 @@ void SwXTextDocument::setGraphicSelection(int nType, int nX, int nY) SwViewShell* pWrtViewShell = m_pDocShell->GetWrtShell(); SwViewOption aOption(*(pWrtViewShell->GetViewOptions())); - double fScale = aOption.GetZoom() / (TWIPS_PER_PIXEL * 100.0); + double fScale = aOption.GetZoom() / o3tl::convert(100.0, o3tl::Length::px, o3tl::Length::twip); SfxViewShell* pViewShell = m_pDocShell->GetView(); LokChartHelper aChartHelper(pViewShell);