summaryrefslogtreecommitdiff
path: root/oox
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 /oox
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 'oox')
-rw-r--r--oox/source/drawingml/diagram/diagram.cxx11
-rw-r--r--oox/source/drawingml/diagram/diagramlayoutatoms.cxx24
-rw-r--r--oox/source/drawingml/drawingmltypes.cxx5
-rw-r--r--oox/source/drawingml/shape.cxx14
-rw-r--r--oox/source/vml/vmlformatting.cxx28
5 files changed, 47 insertions, 35 deletions
diff --git a/oox/source/drawingml/diagram/diagram.cxx b/oox/source/drawingml/diagram/diagram.cxx
index 9a8f727b9070..28c79e8cbd60 100644
--- a/oox/source/drawingml/diagram/diagram.cxx
+++ b/oox/source/drawingml/diagram/diagram.cxx
@@ -30,6 +30,7 @@
#include <editeng/unoprnms.hxx>
#include <drawingml/fillproperties.hxx>
#include <drawingml/customshapeproperties.hxx>
+#include <o3tl/unit_conversion.hxx>
#include <oox/token/namespaces.hxx>
#include <basegfx/matrix/b2dhommatrix.hxx>
#include <svx/svdpage.hxx>
@@ -439,15 +440,17 @@ void reloadDiagram(SdrObject* pObj, core::XmlFilterBase& rFilter)
ShapePtr pShape = std::make_shared<Shape>();
pShape->setDiagramType();
- pShape->setSize(awt::Size(xShape->getSize().Width * EMU_PER_HMM,
- xShape->getSize().Height * EMU_PER_HMM));
+ pShape->setSize(
+ awt::Size(o3tl::convert(xShape->getSize().Width, o3tl::Length::mm100, o3tl::Length::emu),
+ o3tl::convert(xShape->getSize().Height, o3tl::Length::mm100, o3tl::Length::emu)));
loadDiagram(pShape, pDiagramData, layoutDom, styleDom, colorDom, rFilter);
uno::Reference<drawing::XShapes> xShapes(xShape, uno::UNO_QUERY_THROW);
basegfx::B2DHomMatrix aTransformation;
- aTransformation.translate(xShape->getPosition().X * EMU_PER_HMM,
- xShape->getPosition().Y * EMU_PER_HMM);
+ aTransformation.translate(
+ o3tl::convert(xShape->getPosition().X, o3tl::Length::mm100, o3tl::Length::emu),
+ o3tl::convert(xShape->getPosition().Y, o3tl::Length::mm100, o3tl::Length::emu));
for (auto const& child : pShape->getChildren())
child->addShape(rFilter, rFilter.getCurrentTheme(), xShapes, aTransformation, pShape->getFillProperties());
}
diff --git a/oox/source/drawingml/diagram/diagramlayoutatoms.cxx b/oox/source/drawingml/diagram/diagramlayoutatoms.cxx
index 66df25d9ef4a..bc24b0322059 100644
--- a/oox/source/drawingml/diagram/diagramlayoutatoms.cxx
+++ b/oox/source/drawingml/diagram/diagramlayoutatoms.cxx
@@ -26,6 +26,7 @@
#include <basegfx/numeric/ftools.hxx>
#include <sal/log.hxx>
+#include <o3tl/unit_conversion.hxx>
#include <oox/helper/attributelist.hxx>
#include <oox/token/properties.hxx>
#include <drawingml/fillproperties.hxx>
@@ -538,14 +539,11 @@ void CompositeAlg::applyConstraintToLayout(const Constraint& rConstraint,
// Reference not found, assume a fixed value.
// Values are never in EMU, while oox::drawingml::Shape position and size are always in
// EMU.
- double fUnitFactor = 0;
- if (isFontUnit(rConstraint.mnRefType))
- // Points -> EMU.
- fUnitFactor = EMU_PER_PT;
- else
- // Millimeters -> EMU.
- fUnitFactor = EMU_PER_HMM * 100;
- rProperties[rConstraint.msForName][rConstraint.mnType] = rConstraint.mfValue * fUnitFactor;
+ const double fValue = o3tl::convert(rConstraint.mfValue,
+ isFontUnit(rConstraint.mnRefType) ? o3tl::Length::pt
+ : o3tl::Length::mm,
+ o3tl::Length::emu);
+ rProperties[rConstraint.msForName][rConstraint.mnType] = fValue;
}
}
@@ -1709,7 +1707,8 @@ void AlgAtom::layoutShape(const ShapePtr& rShape, const std::vector<Constraint>&
double fFactor = convertPointToMms(rConstr.mfFactor);
// DrawingML works in EMUs, UNO API works in MM100s.
- sal_Int32 nValue = rShape->getSize().Width * fFactor / EMU_PER_HMM;
+ sal_Int32 nValue = o3tl::convert(rShape->getSize().Width * fFactor,
+ o3tl::Length::emu, o3tl::Length::mm100);
rShape->getShapeProperties().setProperty(nProperty, nValue);
}
@@ -1816,12 +1815,15 @@ void AlgAtom::layoutShape(const ShapePtr& rShape, const std::vector<Constraint>&
{
if (!aParagraph->getProperties().getParaLeftMargin().has_value())
{
- sal_Int32 nLeftMargin = 285750 * (nLevel - nStartBulletsAtLevel + 1) / EMU_PER_HMM;
+ sal_Int32 nLeftMargin
+ = o3tl::convert(285750 * (nLevel - nStartBulletsAtLevel + 1),
+ o3tl::Length::emu, o3tl::Length::mm100);
aParagraph->getProperties().getParaLeftMargin() = nLeftMargin;
}
if (!aParagraph->getProperties().getFirstLineIndentation().has_value())
- aParagraph->getProperties().getFirstLineIndentation() = -285750 / EMU_PER_HMM;
+ aParagraph->getProperties().getFirstLineIndentation()
+ = o3tl::convert(-285750, o3tl::Length::emu, o3tl::Length::mm100);
// It is not possible to change the bullet style for text.
aParagraph->getProperties().getBulletList().setBulletChar(u"•");
diff --git a/oox/source/drawingml/drawingmltypes.cxx b/oox/source/drawingml/drawingmltypes.cxx
index 645fe9ec9221..3a94a449f317 100644
--- a/oox/source/drawingml/drawingmltypes.cxx
+++ b/oox/source/drawingml/drawingmltypes.cxx
@@ -40,8 +40,7 @@ namespace oox::drawingml {
/** converts EMUs into 1/100th mmm */
sal_Int32 GetCoordinate( sal_Int32 nValue )
{
- nValue = o3tl::saturating_add<sal_Int32>(nValue, 180);
- return nValue / 360;
+ return o3tl::convert(nValue, o3tl::Length::emu, o3tl::Length::mm100);
}
/** converts an emu string into 1/100th mmm */
@@ -56,7 +55,7 @@ sal_Int32 GetCoordinate( std::u16string_view sValue )
/** converts 1/100mm to EMU */
sal_Int32 GetPointFromCoordinate( sal_Int32 nValue )
{
- return nValue * 360;
+ return o3tl::convert(nValue, o3tl::Length::mm100, o3tl::Length::emu);
}
/** converts a ST_Percentage % string into 1/1000th of % */
diff --git a/oox/source/drawingml/shape.cxx b/oox/source/drawingml/shape.cxx
index b4ce27ea4df4..22cacec6eaa6 100644
--- a/oox/source/drawingml/shape.cxx
+++ b/oox/source/drawingml/shape.cxx
@@ -35,6 +35,7 @@
#include <drawingml/chart/chartspacefragment.hxx>
#include <drawingml/chart/chartspacemodel.hxx>
#include <o3tl/safeint.hxx>
+#include <o3tl/unit_conversion.hxx>
#include <oox/ppt/pptimport.hxx>
#include <oox/vml/vmldrawing.hxx>
#include <oox/vml/vmlshape.hxx>
@@ -699,7 +700,11 @@ Reference< XShape > const & Shape::createAndInsert(
}
}
- awt::Rectangle aShapeRectHmm( maPosition.X / EMU_PER_HMM, maPosition.Y / EMU_PER_HMM, maSize.Width / EMU_PER_HMM, maSize.Height / EMU_PER_HMM );
+ awt::Rectangle aShapeRectHmm(
+ o3tl::convert(maPosition.X, o3tl::Length::emu, o3tl::Length::mm100),
+ o3tl::convert(maPosition.Y, o3tl::Length::emu, o3tl::Length::mm100),
+ o3tl::convert(maSize.Width, o3tl::Length::emu, o3tl::Length::mm100),
+ o3tl::convert(maSize.Height, o3tl::Length::emu, o3tl::Length::mm100));
OUString aServiceName;
if (pMathXml)
@@ -804,14 +809,17 @@ Reference< XShape > const & Shape::createAndInsert(
{
// if global position is used, add it to transformation
if (mbWps && !bInGroup)
- aTransformation.translate( maPosition.X * EMU_PER_HMM, maPosition.Y * EMU_PER_HMM);
+ aTransformation.translate(
+ o3tl::convert(maPosition.X, o3tl::Length::mm100, o3tl::Length::emu),
+ o3tl::convert(maPosition.Y, o3tl::Length::mm100, o3tl::Length::emu));
else
aTransformation.translate( maPosition.X, maPosition.Y );
}
aTransformation = aParentTransformation*aTransformation;
aParentTransformation = aTransformation;
- aTransformation.scale(1/double(EMU_PER_HMM), 1/double(EMU_PER_HMM));
+ constexpr double fEmuToMm100 = o3tl::convert(1.0, o3tl::Length::emu, o3tl::Length::mm100);
+ aTransformation.scale(fEmuToMm100, fEmuToMm100);
if( bIsCustomShape && mbFlipH != mbFlipV )
{
diff --git a/oox/source/vml/vmlformatting.cxx b/oox/source/vml/vmlformatting.cxx
index 60ef7f900d15..96d26efe8ff8 100644
--- a/oox/source/vml/vmlformatting.cxx
+++ b/oox/source/vml/vmlformatting.cxx
@@ -29,6 +29,7 @@
#include <com/sun/star/drawing/EnhancedCustomShapeTextPathMode.hpp>
#include <com/sun/star/table/ShadowFormat.hpp>
#include <com/sun/star/text/XTextRange.hpp>
+#include <o3tl/unit_conversion.hxx>
#include <rtl/strbuf.hxx>
#include <sal/log.hxx>
#include <osl/diagnose.h>
@@ -174,21 +175,20 @@ sal_Int64 ConversionHelper::decodeMeasureToEmu( const GraphicHelper& rGraphicHel
{
sal_Unicode cChar1 = aUnit[ 0 ];
sal_Unicode cChar2 = aUnit[ 1 ];
- if( (cChar1 == 'i') && (cChar2 == 'n') ) // 1 inch = 914,400 EMU
- fValue *= 914400.0;
- else if( (cChar1 == 'c') && (cChar2 == 'm') ) // 1 cm = 360,000 EMU
- fValue *= 360000.0;
- else if( (cChar1 == 'm') && (cChar2 == 'm') ) // 1 mm = 36,000 EMU
- fValue *= 36000.0;
- else if( (cChar1 == 'p') && (cChar2 == 't') ) // 1 point = 1/72 inch = 12,700 EMU
- fValue *= 12700.0;
- else if( (cChar1 == 'p') && (cChar2 == 'c') ) // 1 pica = 1/6 inch = 152,400 EMU
- fValue *= 152400.0;
+ if ((cChar1 == 'i') && (cChar2 == 'n'))
+ fValue = o3tl::convert(fValue, o3tl::Length::in, o3tl::Length::emu);
+ else if ((cChar1 == 'c') && (cChar2 == 'm'))
+ fValue = o3tl::convert(fValue, o3tl::Length::cm, o3tl::Length::emu);
+ else if ((cChar1 == 'm') && (cChar2 == 'm'))
+ fValue = o3tl::convert(fValue, o3tl::Length::mm, o3tl::Length::emu);
+ else if ((cChar1 == 'p') && (cChar2 == 't'))
+ fValue = o3tl::convert(fValue, o3tl::Length::pt, o3tl::Length::emu);
+ else if ((cChar1 == 'p') && (cChar2 == 'c'))
+ fValue = o3tl::convert(fValue, o3tl::Length::pc, o3tl::Length::emu);
else if( (cChar1 == 'p') && (cChar2 == 'x') ) // 1 pixel, dependent on output device
- fValue = static_cast< double >( ::oox::drawingml::convertHmmToEmu(
- bPixelX ?
- rGraphicHelper.convertScreenPixelXToHmm( fValue ) :
- rGraphicHelper.convertScreenPixelYToHmm( fValue ) ) );
+ fValue = o3tl::convert(bPixelX ? rGraphicHelper.convertScreenPixelXToHmm(fValue)
+ : rGraphicHelper.convertScreenPixelYToHmm(fValue),
+ o3tl::Length::mm100, o3tl::Length::emu);
}
else if( (aUnit.getLength() == 1) && (aUnit[ 0 ] == '%') )
{