From 82c469a0a66d775299ceaaa98443584affb33859 Mon Sep 17 00:00:00 2001 From: Kirill Polyakov Date: Thu, 26 Oct 2023 19:02:23 +0300 Subject: [PATCH] Changed the comparison of double values --- Common/3dParty/html/css/src/StyleProperties.cpp | 12 +++++++++++- Common/3dParty/html/css/src/StyleProperties.h | 5 ++++- DesktopEditor/raster/Metafile/svg/CSvgFile.cpp | 6 +++--- .../raster/Metafile/svg/SvgObjects/CMarker.cpp | 2 +- .../raster/Metafile/svg/SvgObjects/CObjectBase.cpp | 2 +- .../raster/Metafile/svg/SvgObjects/CPath.cpp | 4 ++-- .../raster/Metafile/svg/SvgObjects/CText.cpp | 2 +- DesktopEditor/raster/Metafile/svg/SvgTypes.h | 3 ++- DesktopEditor/raster/Metafile/svg/SvgUtils.h | 7 +++++-- 9 files changed, 30 insertions(+), 13 deletions(-) diff --git a/Common/3dParty/html/css/src/StyleProperties.cpp b/Common/3dParty/html/css/src/StyleProperties.cpp index 5df38069b2..c055a8f171 100644 --- a/Common/3dParty/html/css/src/StyleProperties.cpp +++ b/Common/3dParty/html/css/src/StyleProperties.cpp @@ -174,7 +174,7 @@ namespace NSCSS bool CDigit::Zero() const { - return 0. == m_oValue; + return (std::abs(m_oValue) <= DBL_EPSILON); } void CDigit::Clear() @@ -241,6 +241,16 @@ namespace NSCSS return m_enUnitMeasure; } + bool CDigit::operator==(const double &oValue) const + { + return (std::abs(oValue - m_oValue) <= DBL_EPSILON); + } + + bool CDigit::operator==(const CDigit &oDigit) const + { + return (std::abs(oDigit.m_oValue - m_oValue) <= DBL_EPSILON); + } + CDigit CDigit::operator+(const CDigit &oDigit) const { CDigit oTemp; diff --git a/Common/3dParty/html/css/src/StyleProperties.h b/Common/3dParty/html/css/src/StyleProperties.h index 0482dc4993..42eb94ff0f 100644 --- a/Common/3dParty/html/css/src/StyleProperties.h +++ b/Common/3dParty/html/css/src/StyleProperties.h @@ -87,7 +87,7 @@ namespace NSCSS return *this; } - bool operator==(const CValue& oValue) const + bool operator==(const CValue& oValue) const { return m_oValue == oValue.m_oValue; } @@ -141,6 +141,9 @@ namespace NSCSS UnitMeasure GetUnitMeasure() const; + bool operator==(const double& oValue) const; + bool operator==(const CDigit& oDigit) const; + CDigit operator+(const CDigit& oDigit) const; CDigit operator-(const CDigit& oDigit) const; CDigit operator*(const CDigit& oDigit) const; diff --git a/DesktopEditor/raster/Metafile/svg/CSvgFile.cpp b/DesktopEditor/raster/Metafile/svg/CSvgFile.cpp index 19066a6a97..d6b0478750 100644 --- a/DesktopEditor/raster/Metafile/svg/CSvgFile.cpp +++ b/DesktopEditor/raster/Metafile/svg/CSvgFile.cpp @@ -43,9 +43,9 @@ bool CSvgFile::GetBounds(double &dX, double &dY, double &dWidth, double &dHeight dWidth = oWindow.m_oWidth .ToDouble(NSCSS::Pixel); dHeight = oWindow.m_oHeight.ToDouble(NSCSS::Pixel); - if (0. == dWidth) + if (SVG::Equals(0., dWidth)) dWidth = (!m_oContainer.GetViewBox().m_oWidth.Empty()) ? m_oContainer.GetViewBox().m_oWidth.ToDouble(NSCSS::Pixel) : 300; - if (0. == dHeight) + if (SVG::Equals(0., dHeight)) dHeight = (!m_oContainer.GetViewBox().m_oHeight.Empty()) ? m_oContainer.GetViewBox().m_oHeight.ToDouble(NSCSS::Pixel) : 150; return true; @@ -132,7 +132,7 @@ bool CSvgFile::Draw(IRenderer *pRenderer, double dX, double dY, double dWidth, d double dWindowWidth = oWindow.m_oWidth.ToDouble(NSCSS::Pixel, dViewBoxWidth); double dWindowHeight = oWindow.m_oHeight.ToDouble(NSCSS::Pixel, dViewBoxHeight); - if (0. == dWindowWidth || 0. == dWindowHeight) + if (SVG::Equals(0., dWindowWidth) || SVG::Equals(0., dWindowHeight)) return false; double oldTransform[6]; diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CMarker.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CMarker.cpp index 36e865447c..97433acbde 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CMarker.cpp +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CMarker.cpp @@ -122,7 +122,7 @@ namespace SVG void CMarker::Draw(IRenderer *pRenderer, const std::vector &arPoints, double dStrokeWidth) const { - if (NULL == m_pImage || arPoints.empty() || 0. == dStrokeWidth) + if (NULL == m_pImage || arPoints.empty() || Equals(0., dStrokeWidth)) return; double dWidth = m_oWindow.m_oWidth.ToDouble(NSCSS::Pixel) * ((Marker_StrokeWidth == m_enUnits) ? dStrokeWidth : 1.); diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CObjectBase.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CObjectBase.cpp index b90069867c..50f33ab399 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CObjectBase.cpp +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CObjectBase.cpp @@ -344,7 +344,7 @@ namespace SVG double dStrokeWidth = pStroke->m_oWidth.ToDouble(NSCSS::Pixel); - if (0. == dStrokeWidth) + if (Equals(0., dStrokeWidth)) dStrokeWidth = 1.; int nColor = (pStroke->m_oColor.Empty() || NSCSS::NSProperties::ColorType::ColorNone == pStroke->m_oColor.GetType()) ? 0 : pStroke->m_oColor.ToInt(); diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CPath.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CPath.cpp index cc6d00dae7..87ed1fbc30 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CPath.cpp +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CPath.cpp @@ -269,7 +269,7 @@ namespace SVG std::vector CCBezierElement::CreateFromArc(std::vector &arValues, bool bRelativeCoordinate, IPathElement *pPrevElement) { - if (arValues.size() < 7 || 0. == arValues[0] || 0. == arValues[1]) + if (arValues.size() < 7 || Equals(0., arValues[0]) || Equals(0., arValues[1])) return std::vector(); Point oTranslatePoint{0., 0.}; @@ -292,7 +292,7 @@ namespace SVG Point oCenter{0, 0}; double dAngle = 0, dSweep = 0; - CalculateArcData(oSrartPoint, oSecondPoint, oRadius, oCenter, arValues[2], (1 == arValues[3]) ? true : false, (1 == arValues[4]) ? true : false, dAngle, dSweep); + CalculateArcData(oSrartPoint, oSecondPoint, oRadius, oCenter, arValues[2], Equals(1., arValues[3]) ? true : false, Equals(1., arValues[4]) ? true : false, dAngle, dSweep); double dStartAngle = dAngle; double dEndAngle; diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CText.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CText.cpp index e71a2e01d7..9447a8a9c2 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CText.cpp +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CText.cpp @@ -384,7 +384,7 @@ namespace SVG if (!ISZERO(dModuleM22) && (dModuleM22 < 0.05 || dModuleM22 > 100)) dYScale /= dModuleM22; - if (1. == dXScale && 1. == dYScale) + if (Equals(1., dXScale) && Equals(1., dYScale)) return; dX /= dXScale; diff --git a/DesktopEditor/raster/Metafile/svg/SvgTypes.h b/DesktopEditor/raster/Metafile/svg/SvgTypes.h index 00ca149fbf..38ea443a6d 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgTypes.h +++ b/DesktopEditor/raster/Metafile/svg/SvgTypes.h @@ -5,6 +5,7 @@ #include #include "../../../Common/3dParty/html/css/src/StyleProperties.h" +#include "SvgUtils.h" namespace SVG { @@ -50,7 +51,7 @@ namespace SVG bool operator==(const Point& oPoint) { - return dX == oPoint.dX && dY == oPoint.dY; + return Equals(dX, oPoint.dX) && Equals(dY, oPoint.dY); } Point& operator+=(const Point& oPoint) diff --git a/DesktopEditor/raster/Metafile/svg/SvgUtils.h b/DesktopEditor/raster/Metafile/svg/SvgUtils.h index 1abd79baf0..931766a6a1 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgUtils.h +++ b/DesktopEditor/raster/Metafile/svg/SvgUtils.h @@ -9,8 +9,6 @@ namespace SVG { - #define ADD_COLOR( COLOR, R, G, B ) m_Table.insert(std::pair( L##COLOR, ( R << 0 ) | ( G << 8 ) | ( B << 16 ) )) - enum Metrics { EM, @@ -27,6 +25,11 @@ namespace SVG UNDEFINED }; + inline bool Equals(double dFirst, double dSecond, double dEpsilon = DBL_EPSILON) + { + return std::abs(dFirst - dSecond) <= dEpsilon; + } + namespace StrUtils { inline std::vector ReadDoubleValues(std::wstring::const_iterator oBegin, std::wstring::const_iterator oEnd)