summaryrefslogtreecommitdiff
path: root/sc
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2021-02-13 13:55:22 +0300
committerMike Kaganski <mike.kaganski@collabora.com>2021-02-14 12:50:01 +0100
commitcfff893b9c82843a90aac4ecdb3a3936721b74a0 (patch)
tree2859340e329ea6dbffe5ae9c7eba0f67a88c57af /sc
parent20305894243e24eb383ab9feefebf4a0e9f2644f (diff)
Move unit conversion code to o3tl, and unify on that in more places
This also allows to easily add more units, both of length and for other unit categories. The conversion for "Line" unit (312 twip) is questionable. Corresponding entries in aImplFactor in vcl/source/control/field.cxx were inconsistent (45/11 in; 10/13 pc; 156/10 pt). They were added without explanation in commit c85db626029fd8a5e0dfcb312937279df32339a0. I haven't found a spec of the unit (https://en.wikipedia.org/wiki/Line_(unit) is not specific). I used the definition based on "by pt", "by mm/100", "by char" (they all were consistent); "by pc" seems inverted; "by twip" was half as much. This accepted conversion makes unit test for tdf#79236 pass. Change-Id: Iae5a21d915fa8e934a1f47f8ba9f6df03b79a9fd Reviewed-on: https://gerrit.libreoffice.org/c/core/+/110839 Tested-by: Mike Kaganski <mike.kaganski@collabora.com> Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'sc')
-rw-r--r--sc/source/filter/oox/unitconverter.cxx23
-rw-r--r--sc/source/ui/vba/vbarange.cxx12
2 files changed, 15 insertions, 20 deletions
diff --git a/sc/source/filter/oox/unitconverter.cxx b/sc/source/filter/oox/unitconverter.cxx
index 088121286d9e..8ca79ce78d90 100644
--- a/sc/source/filter/oox/unitconverter.cxx
+++ b/sc/source/filter/oox/unitconverter.cxx
@@ -26,6 +26,7 @@
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/util/Date.hpp>
#include <com/sun/star/util/DateTime.hpp>
+#include <o3tl/unit_conversion.hxx>
#include <osl/diagnose.h>
#include <oox/core/filterbase.hxx>
#include <oox/helper/propertyset.hxx>
@@ -43,11 +44,6 @@ using namespace ::com::sun::star::uno;
namespace {
-const double MM100_PER_INCH = 2540.0;
-const double MM100_PER_POINT = MM100_PER_INCH / 72.0;
-const double MM100_PER_TWIP = MM100_PER_POINT / 20.0;
-const double MM100_PER_EMU = 1.0 / 360.0;
-
/** Returns true, if the passed year is a leap year. */
bool lclIsLeapYear( sal_Int32 nYear )
{
@@ -95,10 +91,10 @@ UnitConverter::UnitConverter( const WorkbookHelper& rHelper ) :
{
// initialize constant and default coefficients
const DeviceInfo& rDeviceInfo = getBaseFilter().getGraphicHelper().getDeviceInfo();
- maCoeffs[ Unit::Inch ] = MM100_PER_INCH;
- maCoeffs[ Unit::Point ] = MM100_PER_POINT;
- maCoeffs[ Unit::Twip ] = MM100_PER_TWIP;
- maCoeffs[ Unit::Emu ] = MM100_PER_EMU;
+ maCoeffs[Unit::Inch] = o3tl::convert(1.0, o3tl::Length::in, o3tl::Length::mm100);
+ maCoeffs[Unit::Point] = o3tl::convert(1.0, o3tl::Length::pt, o3tl::Length::mm100);
+ maCoeffs[Unit::Twip] = o3tl::convert(1.0, o3tl::Length::twip, o3tl::Length::mm100);
+ maCoeffs[Unit::Emu] = o3tl::convert(1.0, o3tl::Length::emu, o3tl::Length::mm100);
maCoeffs[ Unit::ScreenX ] = (rDeviceInfo.PixelPerMeterX > 0) ? (100000.0 / rDeviceInfo.PixelPerMeterX) : 50.0;
maCoeffs[ Unit::ScreenY ] = (rDeviceInfo.PixelPerMeterY > 0) ? (100000.0 / rDeviceInfo.PixelPerMeterY) : 50.0;
maCoeffs[ Unit::Digit ] = 200.0; // default: 1 digit = 2 mm
@@ -133,13 +129,16 @@ void UnitConverter::finalizeImport()
return;
// get maximum width of all digits
- sal_Int32 nDigitWidth = 0;
+ sal_Int64 nDigitWidth = 0;
for( sal_Unicode cChar = '0'; cChar <= '9'; ++cChar )
- nDigitWidth = ::std::max( nDigitWidth, scaleToMm100( xFont->getCharWidth( cChar ), Unit::Twip ) );
+ nDigitWidth
+ = ::std::max(nDigitWidth, o3tl::convert(xFont->getCharWidth(cChar), o3tl::Length::twip,
+ o3tl::Length::mm100));
if( nDigitWidth > 0 )
maCoeffs[ Unit::Digit ] = nDigitWidth;
// get width of space character
- sal_Int32 nSpaceWidth = scaleToMm100( xFont->getCharWidth( ' ' ), Unit::Twip );
+ sal_Int32 nSpaceWidth
+ = o3tl::convert(xFont->getCharWidth(' '), o3tl::Length::twip, o3tl::Length::mm100);
if( nSpaceWidth > 0 )
maCoeffs[ Unit::Space ] = nSpaceWidth;
}
diff --git a/sc/source/ui/vba/vbarange.cxx b/sc/source/ui/vba/vbarange.cxx
index 1dd212e61bb2..e78088facd52 100644
--- a/sc/source/ui/vba/vbarange.cxx
+++ b/sc/source/ui/vba/vbarange.cxx
@@ -22,6 +22,7 @@
#include <comphelper/types.hxx>
#include <cppuhelper/exc_hlp.hxx>
#include <o3tl/any.hxx>
+#include <o3tl/unit_conversion.hxx>
#include <tools/diagnose_ex.h>
#include <com/sun/star/script/ArrayWrapper.hpp>
@@ -163,11 +164,6 @@ using ::std::vector;
// difference between VBA and file format width, in character units
const double fExtraWidth = 182.0 / 256.0;
-// * 1 point = 1/72 inch = 20 twips
-// * 1 inch = 72 points = 1440 twips
-// * 1 cm = 567 twips
-static double lcl_hmmToPoints( double nVal ) { return nVal / 1000 * 567 / 20; }
-
const sal_Int16 supportedIndexTable[] = { excel::XlBordersIndex::xlEdgeLeft, excel::XlBordersIndex::xlEdgeTop, excel::XlBordersIndex::xlEdgeBottom, excel::XlBordersIndex::xlEdgeRight, excel::XlBordersIndex::xlDiagonalDown, excel::XlBordersIndex::xlDiagonalUp, excel::XlBordersIndex::xlInsideVertical, excel::XlBordersIndex::xlInsideHorizontal };
static sal_uInt16 lcl_pointsToTwips( double nVal )
@@ -3747,7 +3743,7 @@ static double getDefaultCharWidth( ScDocShell* pDocShell )
pAttr->GetFont( aDefFont, SC_AUTOCOL_BLACK, pRefDevice );
pRefDevice->SetFont( aDefFont );
tools::Long nCharWidth = pRefDevice->GetTextWidth( OUString( '0' ) ); // 1/100th mm
- return lcl_hmmToPoints( nCharWidth );
+ return o3tl::convert<double>(nCharWidth, o3tl::Length::mm100, o3tl::Length::pt);
}
uno::Any SAL_CALL
@@ -4084,7 +4080,7 @@ ScVbaRange::getLeft()
if ( m_Areas->getCount() > 1 )
return getArea( 0 )->getLeft();
awt::Point aPoint = getPosition();
- return uno::makeAny( lcl_hmmToPoints( aPoint.X ) );
+ return uno::makeAny(o3tl::convert<double>(aPoint.X, o3tl::Length::mm100, o3tl::Length::pt));
}
uno::Any SAL_CALL
@@ -4094,7 +4090,7 @@ ScVbaRange::getTop()
if ( m_Areas->getCount() > 1 )
return getArea( 0 )->getTop();
awt::Point aPoint= getPosition();
- return uno::makeAny( lcl_hmmToPoints( aPoint.Y ) );
+ return uno::makeAny(o3tl::convert<double>(aPoint.Y, o3tl::Length::mm100, o3tl::Length::pt));
}
static uno::Reference< sheet::XCellRangeReferrer > getNamedRange( const uno::Reference< uno::XInterface >& xIf, const uno::Reference< table::XCellRange >& thisRange )