summaryrefslogtreecommitdiff
path: root/svx/source/svdraw/svdtrans.cxx
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/source/svdraw/svdtrans.cxx
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/source/svdraw/svdtrans.cxx')
-rw-r--r--svx/source/svdraw/svdtrans.cxx165
1 files changed, 62 insertions, 103 deletions
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;
}