summaryrefslogtreecommitdiff
path: root/svx
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 /svx
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 'svx')
-rw-r--r--svx/source/customshapes/EnhancedCustomShape3d.cxx4
-rw-r--r--svx/source/svdraw/svdmodel.cxx98
-rw-r--r--svx/source/svdraw/svdtrans.cxx165
3 files changed, 83 insertions, 184 deletions
diff --git a/svx/source/customshapes/EnhancedCustomShape3d.cxx b/svx/source/customshapes/EnhancedCustomShape3d.cxx
index 651d176f6076..9c933ef9313e 100644
--- a/svx/source/customshapes/EnhancedCustomShape3d.cxx
+++ b/svx/source/customshapes/EnhancedCustomShape3d.cxx
@@ -18,6 +18,7 @@
*/
#include "EnhancedCustomShape3d.hxx"
+#include <o3tl/unit_conversion.hxx>
#include <svx/deflt3d.hxx>
#include <svx/svdmodel.hxx>
#include <tools/poly.hxx>
@@ -262,7 +263,8 @@ SdrObject* EnhancedCustomShape3d::Create3DObject(
if ( rSdrObjCustomShape.getSdrModelFromSdrObject().GetScaleUnit() != MapUnit::Map100thMM )
{
DBG_ASSERT( rSdrObjCustomShape.getSdrModelFromSdrObject().GetScaleUnit() == MapUnit::MapTwip, "EnhancedCustomShape3d::Current MapMode is Unsupported" );
- fMap *= 1440.0 / 2540.0;
+ // But we could use MapToO3tlUnit from <tools/UnitConversion> ... ?
+ fMap *= o3tl::convert(1.0, o3tl::Length::mm100, o3tl::Length::twip);
pMap = &fMap;
}
diff --git a/svx/source/svdraw/svdmodel.cxx b/svx/source/svdraw/svdmodel.cxx
index f6f2f8797d72..d5add2eb33b4 100644
--- a/svx/source/svdraw/svdmodel.cxx
+++ b/svx/source/svdraw/svdmodel.cxx
@@ -71,6 +71,7 @@
#include <sfx2/viewsh.hxx>
#include <o3tl/enumrange.hxx>
#include <tools/diagnose_ex.h>
+#include <tools/UnitConversion.hxx>
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
@@ -866,94 +867,31 @@ void SdrModel::ImpSetUIUnit()
m_aUIScale = Fraction(1,1);
}
- // set start values
m_nUIUnitDecimalMark = 0;
- sal_Int64 nMul(1);
- sal_Int64 nDiv(1);
-
- // normalize on meters resp. inch
- switch (m_eObjUnit)
- {
- case MapUnit::Map100thMM : m_nUIUnitDecimalMark+=5; break;
- case MapUnit::Map10thMM : m_nUIUnitDecimalMark+=4; break;
- case MapUnit::MapMM : m_nUIUnitDecimalMark+=3; break;
- case MapUnit::MapCM : m_nUIUnitDecimalMark+=2; break;
- case MapUnit::Map1000thInch: m_nUIUnitDecimalMark+=3; break;
- case MapUnit::Map100thInch : m_nUIUnitDecimalMark+=2; break;
- case MapUnit::Map10thInch : m_nUIUnitDecimalMark+=1; break;
- case MapUnit::MapInch : m_nUIUnitDecimalMark+=0; break;
- case MapUnit::MapPoint : nDiv=72; break; // 1Pt = 1/72"
- case MapUnit::MapTwip : nDiv=144; m_nUIUnitDecimalMark++; break; // 1Twip = 1/1440"
- case MapUnit::MapPixel : break;
- case MapUnit::MapSysFont : break;
- case MapUnit::MapAppFont : break;
- case MapUnit::MapRelative : break;
- default: break;
- } // switch
- // 1 mile = 8 furlong = 63.360" = 1.609.344,0mm
- // 1 furlong = 10 chains = 7.920" = 201.168,0mm
- // 1 chain = 4 poles = 792" = 20.116,8mm
- // 1 pole = 5 1/2 yd = 198" = 5.029,2mm
- // 1 yd = 3 ft = 36" = 914,4mm
- // 1 ft = 12 " = 1" = 304,8mm
+ o3tl::Length eFrom = MapToO3tlLength(m_eObjUnit, o3tl::Length::invalid);
+ o3tl::Length eTo;
+
switch (m_eUIUnit)
{
- case FieldUnit::NONE : break;
- // metric
- case FieldUnit::MM_100TH: m_nUIUnitDecimalMark-=5; break;
- case FieldUnit::MM : m_nUIUnitDecimalMark-=3; break;
- case FieldUnit::CM : m_nUIUnitDecimalMark-=2; break;
- case FieldUnit::M : m_nUIUnitDecimalMark+=0; break;
- case FieldUnit::KM : m_nUIUnitDecimalMark+=3; break;
- // Inch
- case FieldUnit::TWIP : nMul=144; m_nUIUnitDecimalMark--; break; // 1Twip = 1/1440"
- case FieldUnit::POINT : nMul=72; break; // 1Pt = 1/72"
- case FieldUnit::PICA : nMul=6; break; // 1Pica = 1/6"
- case FieldUnit::INCH : break; // 1" = 1"
- case FieldUnit::FOOT : nDiv*=12; break; // 1Ft = 12"
- case FieldUnit::MILE : nDiv*=6336; m_nUIUnitDecimalMark++; break; // 1mile = 63360"
- // other
- case FieldUnit::CUSTOM : break;
- case FieldUnit::PERCENT: m_nUIUnitDecimalMark+=2; break;
- // TODO: Add code to handle the following if needed (added to remove warning)
- case FieldUnit::CHAR : break;
- case FieldUnit::LINE : break;
- case FieldUnit::PIXEL : break;
- case FieldUnit::DEGREE : break;
- case FieldUnit::SECOND : break;
- case FieldUnit::MILLISECOND : break;
+ case FieldUnit::CHAR:
+ case FieldUnit::LINE:
+ eTo = o3tl::Length::invalid;
+ break;
+ case FieldUnit::PERCENT:
+ m_nUIUnitDecimalMark += 2;
+ [[fallthrough]];
+ default:
+ eTo = FieldToO3tlLength(m_eUIUnit, o3tl::Length::invalid);
} // switch
- // check if mapping is from metric to inch and adapt
- const bool bMapInch(IsInch(m_eObjUnit));
- const bool bUIMetr(IsMetric(m_eUIUnit));
-
- if (bMapInch && bUIMetr)
+ sal_Int32 nMul = 1, nDiv = 1;
+ if (eFrom != o3tl::Length::invalid && eTo != o3tl::Length::invalid)
{
- m_nUIUnitDecimalMark += 4;
- nMul *= 254;
+ const auto& [mul, div] = o3tl::getConversionMulDiv(eFrom, eTo);
+ nMul = mul;
+ nDiv = div;
}
-
- // check if mapping is from inch to metric and adapt
- const bool bMapMetr(IsMetric(m_eObjUnit));
- const bool bUIInch(IsInch(m_eUIUnit));
-
- if (bMapMetr && bUIInch)
- {
- m_nUIUnitDecimalMark -= 4;
- nDiv *= 254;
- }
-
- // use temporary fraction for reduction (fallback to 32bit here),
- // may need to be changed in the future, too
- if(1 != nMul || 1 != nDiv)
- {
- const Fraction aTemp(static_cast< tools::Long >(nMul), static_cast< tools::Long >(nDiv));
- nMul = aTemp.GetNumerator();
- nDiv = aTemp.GetDenominator();
- }
-
// #i89872# take Unit of Measurement into account
if(1 != m_aUIScale.GetDenominator() || 1 != m_aUIScale.GetNumerator())
{
diff --git a/svx/source/svdraw/svdtrans.cxx b/svx/source/svdraw/svdtrans.cxx
index 5b02f994cc12..6af3a49ea873 100644
--- a/svx/source/svdraw/svdtrans.cxx
+++ b/svx/source/svdraw/svdtrans.cxx
@@ -25,6 +25,7 @@
#include <vcl/virdev.hxx>
#include <tools/bigint.hxx>
+#include <tools/UnitConversion.hxx>
#include <unotools/syslocale.hxx>
#include <unotools/localedatawrapper.hxx>
#include <sal/log.hxx>
@@ -564,22 +565,28 @@ tools::Long BigMulDiv(tools::Long nVal, tools::Long nMul, tools::Long nDiv)
return BigInt::Scale(nVal, nMul, nDiv);
}
+static FrPair toPair(o3tl::Length eFrom, o3tl::Length eTo)
+{
+ const auto& [nNum, nDen] = o3tl::getConversionMulDiv(eFrom, eTo);
+ return FrPair(nNum, nDen);
+}
+
// How many eU units fit into a mm, respectively an inch?
// Or: How many mm, respectively inches, are there in an eU (and then give me the inverse)
static FrPair GetInchOrMM(MapUnit eU)
{
switch (eU) {
- case MapUnit::Map1000thInch: return FrPair(1000,1);
- case MapUnit::Map100thInch : return FrPair( 100,1);
- case MapUnit::Map10thInch : return FrPair( 10,1);
- case MapUnit::MapInch : return FrPair( 1,1);
- case MapUnit::MapPoint : return FrPair( 72,1);
- case MapUnit::MapTwip : return FrPair(1440,1);
- case MapUnit::Map100thMM : return FrPair( 100,1);
- case MapUnit::Map10thMM : return FrPair( 10,1);
- case MapUnit::MapMM : return FrPair( 1,1);
- case MapUnit::MapCM : return FrPair( 1,10);
+ case MapUnit::Map1000thInch: return toPair(o3tl::Length::in, o3tl::Length::in1000);
+ case MapUnit::Map100thInch : return toPair(o3tl::Length::in, o3tl::Length::in100);
+ case MapUnit::Map10thInch : return toPair(o3tl::Length::in, o3tl::Length::in10);
+ case MapUnit::MapInch : return toPair(o3tl::Length::in, o3tl::Length::in);
+ case MapUnit::MapPoint : return toPair(o3tl::Length::in, o3tl::Length::pt);
+ case MapUnit::MapTwip : return toPair(o3tl::Length::in, o3tl::Length::twip);
+ case MapUnit::Map100thMM : return toPair(o3tl::Length::mm, o3tl::Length::mm100);
+ case MapUnit::Map10thMM : return toPair(o3tl::Length::mm, o3tl::Length::mm10);
+ case MapUnit::MapMM : return toPair(o3tl::Length::mm, o3tl::Length::mm);
+ case MapUnit::MapCM : return toPair(o3tl::Length::mm, o3tl::Length::cm);
case MapUnit::MapPixel : {
ScopedVclPtrInstance< VirtualDevice > pVD;
pVD->SetMapMode(MapMode(MapUnit::Map100thMM));
@@ -599,31 +606,16 @@ static FrPair GetInchOrMM(MapUnit eU)
return Fraction(1,1);
}
-static FrPair GetInchOrMM(FieldUnit eU)
-{
- switch (eU) {
- case FieldUnit::INCH : return FrPair( 1,1);
- case FieldUnit::POINT : return FrPair( 72,1);
- case FieldUnit::TWIP : return FrPair(1440,1);
- case FieldUnit::MM_100TH : return FrPair( 100,1);
- case FieldUnit::MM : return FrPair( 1,1);
- case FieldUnit::CM : return FrPair( 1,10);
- case FieldUnit::M : return FrPair( 1,1000);
- case FieldUnit::KM : return FrPair( 1,1000000);
- case FieldUnit::PICA : return FrPair( 6,1);
- case FieldUnit::FOOT : return FrPair( 1,12);
- case FieldUnit::MILE : return FrPair( 1,63360);
- default: break;
- }
- return Fraction(1,1);
-}
-
// Calculate the factor that we need to convert units from eS to eD.
// e. g. GetMapFactor(UNIT_MM,UNIT_100TH_MM) => 100.
FrPair GetMapFactor(MapUnit eS, MapUnit eD)
{
if (eS==eD) return FrPair(1,1,1,1);
+ const auto eFrom = MapToO3tlLength(eS, o3tl::Length::invalid);
+ const auto eTo = MapToO3tlLength(eD, o3tl::Length::invalid);
+ if (eFrom != o3tl::Length::invalid && eTo != o3tl::Length::invalid)
+ return toPair(eFrom, eTo);
FrPair aS(GetInchOrMM(eS));
FrPair aD(GetInchOrMM(eD));
bool bSInch=IsInch(eS);
@@ -637,84 +629,51 @@ FrPair GetMapFactor(MapUnit eS, MapUnit eD)
FrPair GetMapFactor(FieldUnit eS, FieldUnit eD)
{
if (eS==eD) return FrPair(1,1,1,1);
- FrPair aS(GetInchOrMM(eS));
- FrPair aD(GetInchOrMM(eD));
- bool bSInch=IsInch(eS);
- bool bDInch=IsInch(eD);
- FrPair aRet(aD.X()/aS.X(),aD.Y()/aS.Y());
- if (bSInch && !bDInch) { aRet.X()*=Fraction(127,5); aRet.Y()*=Fraction(127,5); }
- if (!bSInch && bDInch) { aRet.X()*=Fraction(5,127); aRet.Y()*=Fraction(5,127); }
- return aRet;
+ auto eFrom = FieldToO3tlLength(eS), eTo = FieldToO3tlLength(eD);
+ if (eFrom == o3tl::Length::invalid)
+ {
+ if (eTo == o3tl::Length::invalid)
+ return FrPair(1,1,1,1);
+ eFrom = IsInch(eD) ? o3tl::Length::in : o3tl::Length::mm;
+ }
+ else if (eTo == o3tl::Length::invalid)
+ eTo = IsInch(eS) ? o3tl::Length::in : o3tl::Length::mm;
+ return toPair(eFrom, eTo);
};
-
- // 1 mile = 8 furlong = 63.360" = 1.609.344,0mm
- // 1 furlong = 10 chains = 7.920" = 201.168,0mm
- // 1 chain = 4 poles = 792" = 20.116,8mm
- // 1 pole = 5 1/2 yd = 198" = 5.029,2mm
- // 1 yd = 3 ft = 36" = 914,4mm
- // 1 ft = 12 " = 1" = 304,8mm
-
-static void GetMeterOrInch(MapUnit eMU, short& rnComma, tools::Long& rnMul, tools::Long& rnDiv, bool& rbMetr, bool& rbInch)
-{
- rnMul=1; rnDiv=1;
- short nComma=0;
- bool bMetr = false, bInch = false;
- switch (eMU) {
- // Metrically
- case MapUnit::Map100thMM : bMetr = true; nComma=5; break;
- case MapUnit::Map10thMM : bMetr = true; nComma=4; break;
- case MapUnit::MapMM : bMetr = true; nComma=3; break;
- case MapUnit::MapCM : bMetr = true; nComma=2; break;
- // Inch
- case MapUnit::Map1000thInch: bInch = true; nComma=3; break;
- case MapUnit::Map100thInch : bInch = true; nComma=2; break;
- case MapUnit::Map10thInch : bInch = true; nComma=1; break;
- case MapUnit::MapInch : bInch = true; nComma=0; break;
- case MapUnit::MapPoint : bInch = true; rnDiv=72; break; // 1Pt = 1/72"
- case MapUnit::MapTwip : bInch = true; rnDiv=144; nComma=1; break; // 1Twip = 1/1440"
- // Others
- case MapUnit::MapPixel : break;
- case MapUnit::MapSysFont : break;
- case MapUnit::MapAppFont : break;
- case MapUnit::MapRelative : break;
- default: break;
- } // switch
- rnComma=nComma;
- rbMetr=bMetr;
- rbInch=bInch;
-}
-
-
void SdrFormatter::Undirty()
{
- bool bSrcMetr,bSrcInch,bDstMetr,bDstInch;
- tools::Long nMul1,nDiv1,nMul2,nDiv2;
- short nComma1,nComma2;
- // first: normalize to m or in
- GetMeterOrInch(eSrcMU,nComma1,nMul1,nDiv1,bSrcMetr,bSrcInch);
- GetMeterOrInch(eDstMU,nComma2,nMul2,nDiv2,bDstMetr,bDstInch);
- nMul1*=nDiv2;
- nDiv1*=nMul2;
- nComma1=nComma1-nComma2;
-
- if (bSrcInch && bDstMetr) {
- nComma1+=4;
- nMul1*=254;
- }
- if (bSrcMetr && bDstInch) {
- nComma1-=4;
- nDiv1*=254;
- }
-
- // temporary fraction for canceling
- Fraction aTempFract(nMul1,nDiv1);
- nMul1=aTempFract.GetNumerator();
- nDiv1=aTempFract.GetDenominator();
-
- nMul_=nMul1;
- nDiv_=nDiv1;
- nComma_=nComma1;
+ const o3tl::Length eFrom = MapToO3tlLength(eSrcMU, o3tl::Length::invalid);
+ const o3tl::Length eTo = MapToO3tlLength(eDstMU, o3tl::Length::invalid);
+ if (eFrom != o3tl::Length::invalid && eTo != o3tl::Length::invalid)
+ {
+ const auto& [mul, div] = o3tl::getConversionMulDiv(eFrom, eTo);
+ sal_Int64 nMul = mul;
+ sal_Int64 nDiv = div;
+ short nComma = 0;
+
+ // shorten trailing zeros for dividend
+ while (0 == (nMul % 10))
+ {
+ nComma--;
+ nMul /= 10;
+ }
+
+ // shorten trailing zeros for divisor
+ while (0 == (nDiv % 10))
+ {
+ nComma++;
+ nDiv /= 10;
+ }
+ nMul_ = nMul;
+ nDiv_ = nDiv;
+ nComma_ = nComma;
+ }
+ else
+ {
+ nMul_ = nDiv_ = 1;
+ nComma_ = 0;
+ }
bDirty=false;
}