summaryrefslogtreecommitdiff
path: root/sw
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 /sw
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 'sw')
-rw-r--r--sw/source/core/view/viewsh.cxx6
-rw-r--r--sw/source/filter/html/css1atr.cxx81
-rw-r--r--sw/source/filter/ww8/docxattributeoutput.cxx9
-rw-r--r--sw/source/filter/ww8/wrtw8esh.cxx5
-rw-r--r--sw/source/filter/ww8/ww8par6.cxx7
5 files changed, 30 insertions, 78 deletions
diff --git a/sw/source/core/view/viewsh.cxx b/sw/source/core/view/viewsh.cxx
index 5365b3399698..2c4ad8f2e8f4 100644
--- a/sw/source/core/view/viewsh.cxx
+++ b/sw/source/core/view/viewsh.cxx
@@ -77,6 +77,7 @@
#include <svx/svdpagv.hxx>
#include <comphelper/lok.hxx>
#include <sfx2/lokhelper.hxx>
+#include <tools/UnitConversion.hxx>
#if !HAVE_FEATURE_DESKTOP
#include <vcl/sysdata.hxx>
@@ -1903,8 +1904,9 @@ void SwViewShell::PaintTile(VirtualDevice &rDevice, int contextWidth, int contex
// Scaling. Must convert from pixels to twips. We know
// that VirtualDevices use a DPI of 96.
- Fraction scaleX = Fraction(contextWidth, 96) * Fraction(1440) / Fraction(tileWidth);
- Fraction scaleY = Fraction(contextHeight, 96) * Fraction(1440) / Fraction(tileHeight);
+ const Fraction scale = conversionFract(o3tl::Length::px, o3tl::Length::twip);
+ Fraction scaleX = Fraction(contextWidth, tileWidth) * scale;
+ Fraction scaleY = Fraction(contextHeight, tileHeight) * scale;
aMapMode.SetScaleX(scaleX);
aMapMode.SetScaleY(scaleY);
rDevice.SetMapMode(aMapMode);
diff --git a/sw/source/filter/html/css1atr.cxx b/sw/source/filter/html/css1atr.cxx
index b2515833eba7..0b3721fe4caa 100644
--- a/sw/source/filter/html/css1atr.cxx
+++ b/sw/source/filter/html/css1atr.cxx
@@ -91,6 +91,7 @@
#include <IDocumentStylePoolAccess.hxx>
#include <numrule.hxx>
#include <o3tl/typed_flags_set.hxx>
+#include <o3tl/unit_conversion.hxx>
#include <rtl/strbuf.hxx>
@@ -376,10 +377,8 @@ static void AddUnitPropertyValue(OStringBuffer &rOut, tools::Long nVal,
rOut.append('-');
}
- // the recalculated unit results from (x * nMul)/(nDiv*nFac*10)
- tools::Long nMul = 1000;
- tools::Long nDiv = 1;
- tools::Long nFac = 100;
+ o3tl::Length eTo;
+ int nFac; // used to get specific number of decimals
const char *pUnit;
switch( eUnit )
{
@@ -387,9 +386,7 @@ static void AddUnitPropertyValue(OStringBuffer &rOut, tools::Long nVal,
OSL_ENSURE( FieldUnit::MM == eUnit, "Measuring unit not supported" );
[[fallthrough]];
case FieldUnit::MM:
- // 0.01mm = 0.57twip
- nMul = 25400; // 25.4 * 1000
- nDiv = 1440; // 72 * 20;
+ eTo = o3tl::Length::mm;
nFac = 100;
pUnit = sCSS1_UNIT_mm;
break;
@@ -399,9 +396,7 @@ static void AddUnitPropertyValue(OStringBuffer &rOut, tools::Long nVal,
OSL_ENSURE( FieldUnit::CM == eUnit, "Measuring unit not supported" );
[[fallthrough]];
case FieldUnit::CM:
- // 0.01cm = 5.7twip (not exact, but the UI is also not exact)
- nMul = 2540; // 2.54 * 1000
- nDiv = 1440; // 72 * 20;
+ eTo = o3tl::Length::cm;
nFac = 100;
pUnit = sCSS1_UNIT_cm;
break;
@@ -410,17 +405,13 @@ static void AddUnitPropertyValue(OStringBuffer &rOut, tools::Long nVal,
OSL_ENSURE( FieldUnit::POINT == eUnit, "Measuring unit not supported" );
[[fallthrough]];
case FieldUnit::POINT:
- // 0.1pt = 2.0twip (not exact, but the UI is also not exact)
- nMul = 100;
- nDiv = 20;
+ eTo = o3tl::Length::pt;
nFac = 10;
pUnit = sCSS1_UNIT_pt;
break;
case FieldUnit::PICA:
- // 0.01pc = 2.40twip (not exact, but the UI is also not exact)
- nMul = 1000;
- nDiv = 240; // 12 * 20;
+ eTo = o3tl::Length::pc;
nFac = 100;
pUnit = sCSS1_UNIT_pc;
break;
@@ -433,63 +424,21 @@ static void AddUnitPropertyValue(OStringBuffer &rOut, tools::Long nVal,
case FieldUnit::INCH:
default:
OSL_ENSURE( FieldUnit::INCH == eUnit, "Measuring unit not supported" );
- // 0.01in = 14.4twip (not exact, but the UI is also not exact)
- nMul = 1000;
- nDiv = 1440; // 72 * 20;
+ eTo = o3tl::Length::in;
nFac = 100;
pUnit = sCSS1_UNIT_inch;
break;
}
- tools::Long nLongVal = 0;
- bool bOutLongVal = true;
- if( nVal > LONG_MAX / nMul )
+ sal_Int64 nResult = o3tl::convert(nVal * nFac, o3tl::Length::twip, eTo);
+ rOut.append(OString::number(nResult/nFac));
+ if ((nResult % nFac) != 0)
{
- sal_Int64 nBigVal( nVal );
- nBigVal *= nMul;
- nBigVal /= nDiv;
- nBigVal += 5;
- nBigVal /= 10;
-
- if( nBigVal <= LONG_MAX )
- {
- // a long is sufficient
- nLongVal = static_cast<tools::Long>(nBigVal);
- }
- else
- {
- rOut.append(nBigVal / static_cast<sal_Int64>(nFac));
- if( (nBigVal % static_cast<sal_Int64>(nFac)) != 0 )
- {
- rOut.append('.');
- while( nFac > 1 && (nBigVal % static_cast<sal_Int64>(nFac)) != 0 )
- {
- nFac /= 10;
- rOut.append((nBigVal / static_cast<sal_Int64>(nFac)) % sal_Int64(10));
- }
- }
- bOutLongVal = false;
- }
- }
- else
- {
- nLongVal = nVal * nMul;
- nLongVal /= nDiv;
- nLongVal += 5;
- nLongVal /= 10;
- }
-
- if( bOutLongVal )
- {
- rOut.append(OString::number(nLongVal/nFac));
- if( (nLongVal % nFac) != 0 )
+ rOut.append('.');
+ while (nFac > 1 && (nResult % nFac) != 0)
{
- rOut.append('.');
- while( nFac > 1 && (nLongVal % nFac) != 0 )
- {
- nFac /= 10;
- rOut.append(OString::number((nLongVal / nFac) % 10));
- }
+ nFac /= 10;
+ rOut.append(OString::number((nResult / nFac) % 10));
}
}
diff --git a/sw/source/filter/ww8/docxattributeoutput.cxx b/sw/source/filter/ww8/docxattributeoutput.cxx
index c9f78fd2c446..ddc26f1a3be4 100644
--- a/sw/source/filter/ww8/docxattributeoutput.cxx
+++ b/sw/source/filter/ww8/docxattributeoutput.cxx
@@ -133,6 +133,7 @@
#include <txtatr.hxx>
#include <frameformats.hxx>
+#include <o3tl/unit_conversion.hxx>
#include <osl/file.hxx>
#include <utility>
#include <vcl/embeddedfontshelper.hxx>
@@ -9271,10 +9272,10 @@ void DocxAttributeOutput::FormatBox( const SvxBoxItem& rBox )
double fDistanceBottomTwips = double(rBox.GetDistance(SvxBoxItemLine::BOTTOM));
// Convert 'TWIPS' to 'INCH' (because in Word the default values are in Inches)
- double fDistanceLeftInch = fDistanceLeftTwips / 1440;
- double fDistanceTopInch = fDistanceTopTwips / 1440;
- double fDistanceRightInch = fDistanceRightTwips / 1440;
- double fDistanceBottomInch = fDistanceBottomTwips / 1440;
+ double fDistanceLeftInch = o3tl::convert(fDistanceLeftTwips, o3tl::Length::twip, o3tl::Length::in);
+ double fDistanceTopInch = o3tl::convert(fDistanceTopTwips, o3tl::Length::twip, o3tl::Length::in);
+ double fDistanceRightInch = o3tl::convert(fDistanceRightTwips, o3tl::Length::twip, o3tl::Length::in);
+ double fDistanceBottomInch = o3tl::convert(fDistanceBottomTwips, o3tl::Length::twip, o3tl::Length::in);
// This code will write ONLY the non-default values. The values are in 'left','top','right','bottom' order.
// so 'bottom' is checked if it is default and if it is non-default - all the values will be written
diff --git a/sw/source/filter/ww8/wrtw8esh.cxx b/sw/source/filter/ww8/wrtw8esh.cxx
index bee858964b4e..a964516780db 100644
--- a/sw/source/filter/ww8/wrtw8esh.cxx
+++ b/sw/source/filter/ww8/wrtw8esh.cxx
@@ -2145,11 +2145,8 @@ void SwBasicEscherEx::Init()
}
// MS-DFF-Properties mostly are in EMU (English Metric Units)
- // 1mm=36000emu, 1twip=635emu
- Fraction aFact(360, 1);
+ Fraction aFact = conversionFract(o3tl::Length::mm100, o3tl::Length::emu);
aFact /= GetMapFactor(MapUnit::Map100thMM, eMap).X();
- // create little values
- aFact = Fraction(aFact.GetNumerator(), aFact.GetDenominator());
mnEmuMul = aFact.GetNumerator();
mnEmuDiv = aFact.GetDenominator();
diff --git a/sw/source/filter/ww8/ww8par6.cxx b/sw/source/filter/ww8/ww8par6.cxx
index 44acfec4b3fb..9cffb20357fe 100644
--- a/sw/source/filter/ww8/ww8par6.cxx
+++ b/sw/source/filter/ww8/ww8par6.cxx
@@ -19,6 +19,7 @@
#include <stdlib.h>
#include <o3tl/safeint.hxx>
+#include <o3tl/unit_conversion.hxx>
#include <svl/itemiter.hxx>
#include <svl/grabbagitem.hxx>
#include <rtl/tencinfo.h>
@@ -106,8 +107,10 @@ using namespace nsHdFtFlags;
// various
-#define MM_250 1417 // WW default for horizontal borders: 2.5 cm
-#define MM_200 1134 // WW default for lower border: 2.0 cm
+// WW default for horizontal borders: 2.5 cm
+constexpr auto MM_250 = o3tl::convert(25, o3tl::Length::mm, o3tl::Length::twip); // 1417
+// WW default for lower border: 2.0 cm
+constexpr auto MM_200 = o3tl::convert(20, o3tl::Length::mm, o3tl::Length::twip); // 1134
static sal_uInt8 lcl_ReadBorders(bool bVer67, WW8_BRCVer9* brc, WW8PLCFx_Cp_FKP* pPap,