From 0661c9b55233979b6f617bf0dfd9f3deed558dfe Mon Sep 17 00:00:00 2001 From: Kirill Poljakov Date: Tue, 7 Feb 2023 18:42:34 +0300 Subject: [PATCH] Changed working with svg records data and refactoring --- .../3dParty/html/css/src/CCompiledStyle.cpp | 16 +- Common/3dParty/html/css/src/CCssCalculator.h | 4 +- .../html/css/src/CCssCalculator_Private.cpp | 14 +- .../html/css/src/CCssCalculator_Private.h | 84 ++--- .../html/css/src/CUnitMeasureConverter.cpp | 302 ++++++++++++++++++ .../html/css/src/CUnitMeasureConverter.h | 37 +++ Common/3dParty/html/css/src/ConstValues.h | 78 ++--- Common/3dParty/html/css/src/StaticFunctions.h | 8 + .../3dParty/html/css/src/StyleProperties.cpp | 125 +++++++- Common/3dParty/html/css/src/StyleProperties.h | 20 ++ DesktopEditor/graphics/pro/metafile.pri | 3 +- .../raster/Metafile/svg/CSvgFile.cpp | 20 +- DesktopEditor/raster/Metafile/svg/CSvgFile.h | 12 +- .../raster/Metafile/svg/CSvgParser.cpp | 66 ++-- .../raster/Metafile/svg/CSvgParser.h | 4 +- .../Metafile/svg/SvgObjects/CCircle.cpp | 53 ++- .../raster/Metafile/svg/SvgObjects/CCircle.h | 10 +- .../Metafile/svg/SvgObjects/CContainer.cpp | 56 ++-- .../Metafile/svg/SvgObjects/CContainer.h | 16 +- .../Metafile/svg/SvgObjects/CEllipse.cpp | 58 +++- .../raster/Metafile/svg/SvgObjects/CEllipse.h | 12 +- .../Metafile/svg/SvgObjects/CHeader.cpp | 41 --- .../raster/Metafile/svg/SvgObjects/CHeader.h | 22 -- .../raster/Metafile/svg/SvgObjects/CLine.cpp | 55 +++- .../raster/Metafile/svg/SvgObjects/CLine.h | 13 +- .../Metafile/svg/SvgObjects/CObjectBase.cpp | 156 +++++++++ .../Metafile/svg/SvgObjects/CObjectBase.h | 159 ++------- .../raster/Metafile/svg/SvgObjects/CPath.cpp | 9 +- .../raster/Metafile/svg/SvgObjects/CPath.h | 4 +- .../Metafile/svg/SvgObjects/CPolyline.cpp | 13 +- .../Metafile/svg/SvgObjects/CPolyline.h | 4 +- .../raster/Metafile/svg/SvgObjects/CRect.cpp | 134 ++++---- .../raster/Metafile/svg/SvgObjects/CRect.h | 12 +- .../raster/Metafile/svg/SvgObjects/CStyle.cpp | 215 ++++++++++--- .../raster/Metafile/svg/SvgObjects/CStyle.h | 32 +- .../raster/Metafile/svg/SvgObjects/CText.cpp | 117 ++++++- .../raster/Metafile/svg/SvgObjects/CText.h | 16 +- DesktopEditor/raster/Metafile/svg/SvgTypes.h | 17 + DesktopEditor/raster/Metafile/svg/SvgUtils.h | 1 - 39 files changed, 1416 insertions(+), 602 deletions(-) create mode 100644 Common/3dParty/html/css/src/CUnitMeasureConverter.cpp create mode 100644 Common/3dParty/html/css/src/CUnitMeasureConverter.h delete mode 100644 DesktopEditor/raster/Metafile/svg/SvgObjects/CHeader.cpp delete mode 100644 DesktopEditor/raster/Metafile/svg/SvgObjects/CHeader.h create mode 100644 DesktopEditor/raster/Metafile/svg/SvgObjects/CObjectBase.cpp diff --git a/Common/3dParty/html/css/src/CCompiledStyle.cpp b/Common/3dParty/html/css/src/CCompiledStyle.cpp index 8fcf446b06..4aa8bf0e04 100644 --- a/Common/3dParty/html/css/src/CCompiledStyle.cpp +++ b/Common/3dParty/html/css/src/CCompiledStyle.cpp @@ -16,7 +16,7 @@ namespace NSCSS { typedef std::map::const_iterator styles_iterator; - CCompiledStyle::CCompiledStyle() : m_nDpi(96), m_UnitMeasure(Default){} + CCompiledStyle::CCompiledStyle() : m_nDpi(96), m_UnitMeasure(Point){} CCompiledStyle::CCompiledStyle(const CCompiledStyle& oStyle) : m_arParentsStyles(oStyle.m_arParentsStyles), m_sId(oStyle.m_sId), @@ -663,12 +663,12 @@ namespace NSCSS else if (ScalingDirectionX == enScalingDirection && 0 != m_oSourceWindow.m_ushWidth && 0 != m_oDeviceWindow.m_ushWidth && m_oSourceWindow.m_ushWidth != m_oDeviceWindow.m_ushWidth) { - nValue = static_cast((double)nValue / m_oSourceWindow.m_ushWidth * m_oDeviceWindow.m_ushWidth + 0.5f); + nValue = static_cast((double)nValue / m_oSourceWindow.m_ushWidth * m_oDeviceWindow.m_ushWidth + 0.5f); } else if (ScalingDirectionY == enScalingDirection && 0 != m_oSourceWindow.m_ushHeight && 0 != m_oDeviceWindow.m_ushHeight && m_oSourceWindow.m_ushHeight != m_oDeviceWindow.m_ushHeight) { - nValue = static_cast((double)nValue / m_oSourceWindow.m_ushHeight * m_oDeviceWindow.m_ushHeight + 0.5f); + nValue = static_cast((double)nValue / m_oSourceWindow.m_ushHeight * m_oDeviceWindow.m_ushHeight + 0.5f); } } @@ -684,7 +684,6 @@ namespace NSCSS { case Pixel: return static_cast(dValue); - case Default: case Point: return ConvertPxToPt(dValue); case Cantimeter: @@ -735,7 +734,6 @@ namespace NSCSS switch (m_UnitMeasure) { - case Default: case Point: return ConvertCmToPt(dValue); case Pixel: @@ -790,7 +788,6 @@ namespace NSCSS { case Pixel: return ConvertMmToPx(dValue); - case Default: case Point: return ConvertMmToPt(dValue); case Cantimeter: @@ -817,12 +814,12 @@ namespace NSCSS inline int CCompiledStyle::ConvertMmToPc(const float& dValue) const { - return static_cast(2.8346f * dValue + 0.5f); + return static_cast(0.236f * dValue + 0.5f); } inline int CCompiledStyle::ConvertMmToPt(const float& dValue) const { - return static_cast(0.23262f * dValue + 0.5f); + return static_cast(2.835f * dValue + 0.5f); } inline int CCompiledStyle::ConvertMmToPx(const float& dValue) const @@ -842,7 +839,6 @@ namespace NSCSS { case Pixel: return ConvertInToPx(dValue); - case Default: case Point: return ConvertInToPt(dValue); case Cantimeter: @@ -894,7 +890,6 @@ namespace NSCSS { case Pixel: return ConvertPtToPx(dValue); - case Default: case Point: return static_cast(dValue + 0.5f); case Cantimeter: @@ -947,7 +942,6 @@ namespace NSCSS { case Pixel: return ConvertPcToPx(dValue); - case Default: case Point: return ConvertPcToPt(dValue); case Cantimeter: diff --git a/Common/3dParty/html/css/src/CCssCalculator.h b/Common/3dParty/html/css/src/CCssCalculator.h index 43160d50d9..9667f5dab9 100644 --- a/Common/3dParty/html/css/src/CCssCalculator.h +++ b/Common/3dParty/html/css/src/CCssCalculator.h @@ -19,8 +19,8 @@ namespace NSCSS CCssCalculator(); ~CCssCalculator(); - CCompiledStyle GetCompiledStyle(const std::vector &arSelectors, const bool& bIsSettings = false, const UnitMeasure& unitMeasure = Default) const; - bool GetCompiledStyle(CCompiledStyle& oStyle, const std::vector &arSelectors, const bool& bIsSettings = false, const UnitMeasure& unitMeasure = Default) const; + CCompiledStyle GetCompiledStyle(const std::vector &arSelectors, const bool& bIsSettings = false, const UnitMeasure& unitMeasure = Point) const; + bool GetCompiledStyle(CCompiledStyle& oStyle, const std::vector &arSelectors, const bool& bIsSettings = false, const UnitMeasure& unitMeasure = Point) const; // void AddStyle(const std::vector& sSelectors, const std::string& sStyle); void AddStyles (const std::string& sStyle); diff --git a/Common/3dParty/html/css/src/CCssCalculator_Private.cpp b/Common/3dParty/html/css/src/CCssCalculator_Private.cpp index cd30080993..782fe9832a 100644 --- a/Common/3dParty/html/css/src/CCssCalculator_Private.cpp +++ b/Common/3dParty/html/css/src/CCssCalculator_Private.cpp @@ -40,7 +40,7 @@ bool operator<(const std::vector &arLeftSelectors, const std::vect namespace NSCSS { - CCssCalculator_Private::CCssCalculator_Private() : m_nDpi(96), m_nCountNodes(0), m_UnitMeasure(Default), m_mStatictics(NULL), m_sEncoding(L"UTF-8"){} + CCssCalculator_Private::CCssCalculator_Private() : m_nDpi(96), m_nCountNodes(0), m_UnitMeasure(Point), m_mStatictics(NULL), m_sEncoding(L"UTF-8"){} CCssCalculator_Private::~CCssCalculator_Private() { @@ -309,8 +309,7 @@ namespace NSCSS if (arSelectors.empty()) return CCompiledStyle(); - if (unitMeasure != Default) - SetUnitMeasure(unitMeasure); + SetUnitMeasure(unitMeasure); if (!bIsSettings) { @@ -542,8 +541,7 @@ namespace NSCSS if (arSelectors.empty()) return false; - if (unitMeasure != Default) - SetUnitMeasure(unitMeasure); + SetUnitMeasure(unitMeasure); if (!bIsSettings) { @@ -843,6 +841,10 @@ namespace NSCSS return m_nDpi; } + const std::map *CCssCalculator_Private::GetData() const + { + return &m_mData; + } UnitMeasure CCssCalculator_Private::GetUnitMeasure() const { @@ -858,7 +860,7 @@ namespace NSCSS { m_sEncoding = L"UTF-8"; m_nDpi = 96; - m_UnitMeasure = Default; + m_UnitMeasure = Point; m_mData.clear(); m_arFiles.clear(); diff --git a/Common/3dParty/html/css/src/CCssCalculator_Private.h b/Common/3dParty/html/css/src/CCssCalculator_Private.h index 284eb9c1d1..02db846a2f 100644 --- a/Common/3dParty/html/css/src/CCssCalculator_Private.h +++ b/Common/3dParty/html/css/src/CCssCalculator_Private.h @@ -11,67 +11,69 @@ namespace NSCSS { - class CCssCalculator_Private - { - unsigned short int m_nDpi; - unsigned short int m_nCountNodes; - UnitMeasure m_UnitMeasure; + class CCssCalculator_Private + { + unsigned short int m_nDpi; + unsigned short int m_nCountNodes; + UnitMeasure m_UnitMeasure; - std::list m_arFiles; + std::list m_arFiles; - std::map m_mData; + std::map m_mData; - std::map *m_mStatictics; // Количество повторений свойств id и style у селекторов + std::map *m_mStatictics; // Количество повторений свойств id и style у селекторов - std::map, CCompiledStyle*> m_mUsedStyles; + std::map, CCompiledStyle*> m_mUsedStyles; - std::wstring m_sEncoding; + std::wstring m_sEncoding; - CSizeWindow m_oSourceWindow; - CSizeWindow m_oDeviceWindow; + CSizeWindow m_oSourceWindow; + CSizeWindow m_oDeviceWindow; - void GetStylesheet(const KatanaStylesheet* oStylesheet); - void GetRule(const KatanaRule* oRule); + void GetStylesheet(const KatanaStylesheet* oStylesheet); + void GetRule(const KatanaRule* oRule); - void GetStyleRule(const KatanaStyleRule* oRule); + void GetStyleRule(const KatanaStyleRule* oRule); - std::wstring GetValueList(const KatanaArray* oValues); + std::wstring GetValueList(const KatanaArray* oValues); - std::vector GetSelectorList(const KatanaArray* oSelectors) const; - std::wstring GetSelector(const KatanaSelector* oSelector) const; + std::vector GetSelectorList(const KatanaArray* oSelectors) const; + std::wstring GetSelector(const KatanaSelector* oSelector) const; - std::map GetDeclarationList(const KatanaArray* oDeclarations) const; - std::pair GetDeclaration(const KatanaDeclaration* oDecl) const; + std::map GetDeclarationList(const KatanaArray* oDeclarations) const; + std::pair GetDeclaration(const KatanaDeclaration* oDecl) const; - void GetOutputData(KatanaOutput* oOutput); + void GetOutputData(KatanaOutput* oOutput); - public: - CCssCalculator_Private(); - ~CCssCalculator_Private(); + public: + CCssCalculator_Private(); + ~CCssCalculator_Private(); - CCompiledStyle GetCompiledStyle(const std::vector &arSelectors, const bool& bIsSettings = false, const UnitMeasure& unitMeasure = Default); - bool GetCompiledStyle(CCompiledStyle& oStyle, const std::vector &arSelectors, const bool& bIsSettings = false, const UnitMeasure& unitMeasure = Default); + CCompiledStyle GetCompiledStyle(const std::vector &arSelectors, const bool& bIsSettings = false, const UnitMeasure& unitMeasure = Point); + bool GetCompiledStyle(CCompiledStyle& oStyle, const std::vector &arSelectors, const bool& bIsSettings = false, const UnitMeasure& unitMeasure = Point); - void AddStyles(const std::string& sStyle); - void AddStyles(const std::wstring& sStyle); - void AddStylesFromFile(const std::wstring& sFileName); + void AddStyles(const std::string& sStyle); + void AddStyles(const std::wstring& sStyle); + void AddStylesFromFile(const std::wstring& sFileName); - void SetUnitMeasure(const UnitMeasure& nType); - void SetDpi(unsigned short int nValue); - void SetBodyTree(const CTree &oTree); + void SetUnitMeasure(const UnitMeasure& nType); + void SetDpi(unsigned short int nValue); + void SetBodyTree(const CTree &oTree); - void SetSizeSourceWindow(const CSizeWindow& oSizeWindow); - void SetSizeDeviceWindow(const CSizeWindow& oSizeWindow); + void SetSizeSourceWindow(const CSizeWindow& oSizeWindow); + void SetSizeDeviceWindow(const CSizeWindow& oSizeWindow); - CSizeWindow GetSizeSourceWindow() const; - CSizeWindow GetSizeDeviceWindow() const; + CSizeWindow GetSizeSourceWindow() const; + CSizeWindow GetSizeDeviceWindow() const; - UnitMeasure GetUnitMeasure() const; - std::wstring GetEncoding() const; - unsigned short int GetDpi() const; + UnitMeasure GetUnitMeasure() const; + std::wstring GetEncoding() const; + unsigned short int GetDpi() const; - void Clear(); + const std::map* GetData() const; - }; + void Clear(); + + }; } #endif // CCSSCALCULATOR_PRIVATE_H diff --git a/Common/3dParty/html/css/src/CUnitMeasureConverter.cpp b/Common/3dParty/html/css/src/CUnitMeasureConverter.cpp new file mode 100644 index 0000000000..007c0bf7b9 --- /dev/null +++ b/Common/3dParty/html/css/src/CUnitMeasureConverter.cpp @@ -0,0 +1,302 @@ +#include "CUnitMeasureConverter.h" + +#include +#include +#include + +#include "StaticFunctions.h" + +namespace NSCSS +{ + CUnitMeasureConverter::CUnitMeasureConverter() + {} + + double CUnitMeasureConverter::ConvertPx(double dValue, UnitMeasure enUnitMeasure, unsigned short ushDPI) + { + switch (enUnitMeasure) + { + case NSCSS::Pixel: + return dValue; + case NSCSS::Point: + return 72. / (double)ushDPI * dValue; + case NSCSS::Cantimeter: + return dValue / (double)ushDPI * 2.54; + case NSCSS::Millimeter: + return dValue / (double)ushDPI * 25.4; + case NSCSS::Inch: + return 1. / (double)ushDPI * dValue; + case NSCSS::Peak: + return 0.16667 / (double)ushDPI * dValue; + } + + return 0.; + } + + double CUnitMeasureConverter::ConvertCm(double dValue, UnitMeasure enUnitMeasure, unsigned short ushDPI) + { + switch (enUnitMeasure) + { + case NSCSS::Point: + return 28.35 * dValue; + case NSCSS::Pixel: + return (double)ushDPI / 2.54 * dValue; + case NSCSS::Cantimeter: + return dValue; + case NSCSS::Millimeter: + return dValue * 10.; + case NSCSS::Inch: + return dValue / 2.54f; + case NSCSS::Peak: + return 2.36 * dValue; + } + + return 0.; + } + + double CUnitMeasureConverter::ConvertMm(double dValue, NSCSS::UnitMeasure enUnitMeasure, unsigned short ushDPI) + { + switch (enUnitMeasure) + { + case NSCSS::Point: + return 2.835 * dValue; + case NSCSS::Pixel: + return (double)ushDPI / 25.4 * dValue; + case NSCSS::Cantimeter: + return dValue / 10.; + case NSCSS::Millimeter: + return dValue; + case NSCSS::Inch: + return dValue / 25.4; + case NSCSS::Peak: + return 0.236 * dValue; + } + + return 0.; + } + + double CUnitMeasureConverter::ConvertIn(double dValue, NSCSS::UnitMeasure enUnitMeasure, unsigned short ushDPI) + { + switch (enUnitMeasure) + { + case NSCSS::Point: + return dValue / 6.; + case NSCSS::Pixel: + return dValue * (double)ushDPI; + case NSCSS::Cantimeter: + return dValue * 2.54; + case NSCSS::Millimeter: + return dValue * 25.4; + case NSCSS::Inch: + return dValue; + case NSCSS::Peak: + return dValue / 72.; + } + + return 0.; + } + + double CUnitMeasureConverter::ConvertPt(double dValue, NSCSS::UnitMeasure enUnitMeasure, unsigned short ushDPI) + { + switch (enUnitMeasure) + { + case NSCSS::Point: + return dValue; + case NSCSS::Pixel: + return (double)ushDPI / 72. * dValue; + case NSCSS::Cantimeter: + return dValue * 0.03528; + case NSCSS::Millimeter: + return dValue * 0.3528; + case NSCSS::Inch: + return dValue / 72.; + case NSCSS::Peak: + return dValue / 12.; + } + + return 0.; + } + + double CUnitMeasureConverter::ConvertPc(double dValue, NSCSS::UnitMeasure enUnitMeasure, unsigned short ushDPI) + { + switch (enUnitMeasure) + { + case NSCSS::Point: + return dValue * 12.; + case NSCSS::Pixel: + return (double)ushDPI / 6. * dValue; + case NSCSS::Cantimeter: + return dValue * 0.423; + case NSCSS::Millimeter: + return dValue * 4.23; + case NSCSS::Inch: + return dValue / 6.; + case NSCSS::Peak: + return dValue; + } + + return 0.; + } + + std::wstring CUnitMeasureConverter::ConvertUnitMeasure(const std::wstring &wsValue, double dPreviousValue, UnitMeasure enUnitMeasure, unsigned short ushDPI) + { + if (wsValue.empty()) + return std::wstring(); + + std::wstring wsNewValue; + + std::vector arValues = NSCSS::NS_STATIC_FUNCTIONS::GetWordsWithSigns(wsValue); + + for (std::wstring& wsValueTemp : arValues) + { + std::transform(wsValueTemp.begin(), wsValueTemp.end(), wsValueTemp.begin(), tolower); + + if (wsValueTemp == L"important") + { + wsNewValue += L"!important"; + continue; + } + + size_t nPosGrid = wsValueTemp.find(L'#'); + + if (nPosGrid != std::wstring::npos || !NSCSS::NS_STATIC_FUNCTIONS::NumberInWString(wsValueTemp)) + { + if (!NSCSS::NS_STATIC_FUNCTIONS::ConvertAbsoluteValue(wsValueTemp, dPreviousValue)) + { + wsNewValue += wsValueTemp; + continue; + } + } + + double dValue = NSCSS::NS_STATIC_FUNCTIONS::ReadDouble(wsValueTemp); + + const size_t posPercent = wsValueTemp.find(L'%'); + + if (posPercent != std::wstring::npos) + { + wsNewValue += std::to_wstring(dValue * dPreviousValue / 100.); + + if (wsValueTemp.find(L';') != std::wstring::npos) + wsNewValue += L';'; + else if (arValues.size() > 1 && wsValueTemp.find(L':') == std::wstring::npos) + wsNewValue += L' '; + } + else if (wsValueTemp.find(L"px") != std::wstring::npos) + { + wsNewValue += std::to_wstring(ConvertPx(dValue, enUnitMeasure, ushDPI)); + + if (wsValueTemp.find(L';') != std::wstring::npos) + wsNewValue += L';'; + else if (arValues.size() > 1 && wsValueTemp.find(L':') == std::wstring::npos) + wsNewValue += L' '; + } + else if (wsValueTemp.find(L"cm") != std::wstring::npos) + { + wsNewValue += std::to_wstring(ConvertCm(dValue, enUnitMeasure, ushDPI)); + + if (wsValueTemp.find(L';') != std::wstring::npos) + wsNewValue += L';'; + else if (arValues.size() > 1 && wsValueTemp.find(L':') == std::wstring::npos) + wsNewValue += L' '; + } + else if (wsValueTemp.find(L"mm") != std::wstring::npos) + { + wsNewValue += std::to_wstring(ConvertMm(dValue, enUnitMeasure, ushDPI)); + + if (wsValueTemp.find(L';') != std::wstring::npos) + wsNewValue += L';'; + else if (arValues.size() > 1 && wsValueTemp.find(L':') == std::wstring::npos) + wsNewValue += L' '; + } + else if (wsValueTemp.find(L"in") != std::wstring::npos) + { + wsNewValue += std::to_wstring(ConvertIn(dValue, enUnitMeasure, ushDPI)); + + if (wsValueTemp.find(L';') != std::wstring::npos) + wsNewValue += L';'; + else if (arValues.size() > 1 && wsValueTemp.find(L':') == std::wstring::npos) + wsNewValue += L' '; + } + else if (wsValueTemp.find(L"pt") != std::wstring::npos) + { + wsNewValue += std::to_wstring(ConvertPt(dValue, enUnitMeasure, ushDPI)); + + if (wsValueTemp.find(L';') != std::wstring::npos) + wsNewValue += L';'; + else if (arValues.size() > 1 && wsValueTemp.find(L':') == std::wstring::npos) + wsNewValue += L' '; + } + else if (wsValueTemp.find(L"pc") != std::wstring::npos) + { + wsNewValue += std::to_wstring(ConvertPc(dValue, enUnitMeasure, ushDPI)); + + if (wsValueTemp.find(L';') != std::wstring::npos) + wsNewValue += L';'; + else if (arValues.size() > 1 && wsValueTemp.find(L':') == std::wstring::npos) + wsNewValue += L' '; + } + else if (wsValueTemp.find(L"em") != std::wstring::npos) + { + wsNewValue += std::to_wstring(dValue * dPreviousValue); + + if (wsValueTemp.find(L';') != std::wstring::npos) + wsNewValue += L';'; + else if (arValues.size() > 1 && wsValueTemp.find(L':') == std::wstring::npos) + wsNewValue += L' '; + } + else + { + wsNewValue += wsValueTemp; + + if (wsValueTemp.find(L";") != std::wstring::npos) + wsNewValue += L';'; + + continue; + } + + if (wsValueTemp.back() != L';' && wsValueTemp.back() != L':' && wsValueTemp.back() != L' ') + wsValueTemp += L' '; + } + + return wsNewValue; + } + + bool CUnitMeasureConverter::GetValue(const std::wstring &wsValue, double &dValue, UnitMeasure &enUnitMeasure) + { + std::wstring::const_iterator oFoundDigit = std::find_if(wsValue.begin(), wsValue.end(), std::iswdigit); + + if (wsValue.end() == oFoundDigit) + return false; + + std::wistringstream(wsValue) >> dValue; + + std::wstring::const_iterator oFoundUM = std::find_if(oFoundDigit, wsValue.end(), [](wchar_t wcSymbol){ return std::iswalpha(wcSymbol) || L'%' == wcSymbol; }); + + if (wsValue.end() != oFoundUM) + { + if (L'%' == *oFoundUM) + { + enUnitMeasure = Percent; + return true; + } + + std::wstring wsUnitMeasure(oFoundUM, oFoundUM + 2); + + if (L"px" == wsUnitMeasure) + enUnitMeasure = Pixel; + else if (L"pt" == wsUnitMeasure) + enUnitMeasure = Point; + else if (L"cm" == wsUnitMeasure) + enUnitMeasure = Cantimeter; + else if (L"mm" == wsUnitMeasure) + enUnitMeasure = Millimeter; + else if (L"in" == wsUnitMeasure) + enUnitMeasure = Inch; + else if (L"pc" == wsUnitMeasure) + enUnitMeasure = Peak; + } + else + enUnitMeasure = None; + + return true; + } +} diff --git a/Common/3dParty/html/css/src/CUnitMeasureConverter.h b/Common/3dParty/html/css/src/CUnitMeasureConverter.h new file mode 100644 index 0000000000..9c32466cf0 --- /dev/null +++ b/Common/3dParty/html/css/src/CUnitMeasureConverter.h @@ -0,0 +1,37 @@ +#ifndef CUNITMEASURECONVERTER_H +#define CUNITMEASURECONVERTER_H + +#include + +namespace NSCSS +{ + typedef enum + { + None, + Percent, + Pixel, + Point, + Cantimeter, + Millimeter, + Inch, + Peak + } UnitMeasure; + + class CUnitMeasureConverter + { + CUnitMeasureConverter(); + public: + static double ConvertPx(double dValue, UnitMeasure enUnitMeasure, unsigned short ushDPI); + static double ConvertCm(double dValue, UnitMeasure enUnitMeasure, unsigned short ushDPI); + static double ConvertMm(double dValue, UnitMeasure enUnitMeasure, unsigned short ushDPI); + static double ConvertIn(double dValue, UnitMeasure enUnitMeasure, unsigned short ushDPI); + static double ConvertPt(double dValue, UnitMeasure enUnitMeasure, unsigned short ushDPI); + static double ConvertPc(double dValue, UnitMeasure enUnitMeasure, unsigned short ushDPI); + + static std::wstring ConvertUnitMeasure(const std::wstring& wsValue, double dPreviousValue, UnitMeasure enUnitMeasure, unsigned short ushDPI = 96); + + static bool GetValue(const std::wstring& wsValue, double& dValue, UnitMeasure& enUnitMeasure); + }; +} + +#endif // CUNITMEASURECONVERTER_H diff --git a/Common/3dParty/html/css/src/ConstValues.h b/Common/3dParty/html/css/src/ConstValues.h index 54975d05e1..f05fadcb85 100644 --- a/Common/3dParty/html/css/src/ConstValues.h +++ b/Common/3dParty/html/css/src/ConstValues.h @@ -16,16 +16,6 @@ namespace NSCSS #define RGB_TO_INT(r, g, b) ((unsigned int)( ( (unsigned char)(r) )| ( ( (unsigned char)(g) ) << 8 ) | ( ( (unsigned char)(b) ) << 16 ) ) ) #endif - typedef enum { - Default = 0, - Pixel, - Point, - Cantimeter, - Millimeter, - Inch, - Peak - } UnitMeasure; - typedef enum { ScalingDirectionNone = 0, @@ -3358,40 +3348,40 @@ namespace NSCSS }; } - const std::vector arPseudoClasses { - L"invalid", - L"read-only", - L"-moz-placeholder", - L"-webkit-input-placeholder", - L"active", - L"checked", - L"default", - L"disabled", - L"empty", - L"enabled", - L"first-child", - L"first-of-type", - L"focus", - L"hover", - L"indeterminate", - L"lang", - L"last-child", - L"last-of-type", - L"link", - L"not", - L"nth-child", - L"nth-last-child", - L"nth-last-of-type", - L"nth-of-type", - L"only-child", - L"only-of-type", - L"optional", - L"read-write", - L"required", - L"root", - L"target", - L"valid", - L"visited"}; + const std::wstring arPseudoClasses[33] { + L"invalid", + L"read-only", + L"-moz-placeholder", + L"-webkit-input-placeholder", + L"active", + L"checked", + L"default", + L"disabled", + L"empty", + L"enabled", + L"first-child", + L"first-of-type", + L"focus", + L"hover", + L"indeterminate", + L"lang", + L"last-child", + L"last-of-type", + L"link", + L"not", + L"nth-child", + L"nth-last-child", + L"nth-last-of-type", + L"nth-of-type", + L"only-child", + L"only-of-type", + L"optional", + L"read-write", + L"required", + L"root", + L"target", + L"valid", + L"visited"}; } } diff --git a/Common/3dParty/html/css/src/StaticFunctions.h b/Common/3dParty/html/css/src/StaticFunctions.h index 328273633d..b097e14f94 100644 --- a/Common/3dParty/html/css/src/StaticFunctions.h +++ b/Common/3dParty/html/css/src/StaticFunctions.h @@ -5,6 +5,7 @@ #include "../../../../../DesktopEditor/common/File.h" #include "CNode.h" #include +#include #include #include #include @@ -144,6 +145,13 @@ namespace NSCSS return arWords; } + inline double ReadDouble(const std::wstring& wsValue) + { + double dValue; + std::wistringstream(wsValue) >> dValue; + return dValue; + } + inline std::vector ReadDoubleValues(const std::wstring& wsValue) { std::vector arValues; diff --git a/Common/3dParty/html/css/src/StyleProperties.cpp b/Common/3dParty/html/css/src/StyleProperties.cpp index ee3744fe01..fdc08d6d37 100644 --- a/Common/3dParty/html/css/src/StyleProperties.cpp +++ b/Common/3dParty/html/css/src/StyleProperties.cpp @@ -1,7 +1,5 @@ #include "StyleProperties.h" -#include - #include "StaticFunctions.h" #include "ConstValues.h" @@ -223,10 +221,29 @@ namespace NSCSS return *this; } + double CDigit::ConvertValue(double dPrevValue, UnitMeasure enUnitMeasure) const + { + switch(m_enUnitMeasure) + { + case Percent: return dPrevValue * m_oValue; + case Pixel: return CUnitMeasureConverter::ConvertPx(m_oValue, enUnitMeasure, 96); + case Point: return CUnitMeasureConverter::ConvertPt(m_oValue, enUnitMeasure, 96); + case Cantimeter: return CUnitMeasureConverter::ConvertCm(m_oValue, enUnitMeasure, 96); + case Millimeter: return CUnitMeasureConverter::ConvertMm(m_oValue, enUnitMeasure, 96); + case Inch: return CUnitMeasureConverter::ConvertIn(m_oValue, enUnitMeasure, 96); + case Peak: return CUnitMeasureConverter::ConvertPc(m_oValue, enUnitMeasure, 96); + case None: return m_oValue; + } + } + CDigit::CDigit() : CValue(DBL_MIN, 0, false) {} + CDigit::CDigit(double dValue) + : CValue(dValue, 0, false) + {} + CDigit::CDigit(double dValue, unsigned int unLevel, bool bImportant) : CValue(dValue, unLevel, bImportant) {} @@ -265,6 +282,35 @@ namespace NSCSS return std::to_wstring(m_oValue); } + int CDigit::ToInt(UnitMeasure enUnitMeasure, double dPrevValue) const + { + if (DBL_MIN == m_oValue) + return 0; + + return static_cast(ConvertValue(dPrevValue, enUnitMeasure) + 0.5); + } + + double CDigit::ToDouble(UnitMeasure enUnitMeasure, double dPrevValue) const + { + if (DBL_MIN == m_oValue) + return 0; + + return ConvertValue(dPrevValue, enUnitMeasure); + } + + std::wstring CDigit::ToWString(UnitMeasure enUnitMeasure, double dPrevValue) const + { + if (DBL_MIN == m_oValue) + return 0; + + return std::to_wstring(ConvertValue(dPrevValue, enUnitMeasure)); + } + + UnitMeasure CDigit::GetUnitMeasure() const + { + return m_enUnitMeasure; + } + CDigit CDigit::operator+(const CDigit &oDigit) const { CDigit oTemp; @@ -276,6 +322,48 @@ namespace NSCSS return oTemp; } + CDigit CDigit::operator-(const CDigit &oDigit) const + { + CDigit oTemp; + + oTemp.m_oValue = m_oValue - oDigit.m_oValue; + oTemp.m_unLevel = std::max(m_unLevel, oDigit.m_unLevel); + oTemp.m_bImportant = std::max(m_bImportant, oDigit.m_bImportant); + + return oTemp; + } + + CDigit CDigit::operator*(const CDigit &oDigit) const + { + CDigit oTemp; + + oTemp.m_oValue = m_oValue * oDigit.m_oValue; + oTemp.m_unLevel = std::max(m_unLevel, oDigit.m_unLevel); + oTemp.m_bImportant = std::max(m_bImportant, oDigit.m_bImportant); + + return oTemp; + } + + CDigit CDigit::operator/(const CDigit &oDigit) const + { + CDigit oTemp; + + oTemp.m_oValue = m_oValue / oDigit.m_oValue; + oTemp.m_unLevel = std::max(m_unLevel, oDigit.m_unLevel); + oTemp.m_bImportant = std::max(m_bImportant, oDigit.m_bImportant); + + return oTemp; + } + + CDigit CDigit::operator*(double dValue) const + { + CDigit oTemp(*this); + + oTemp.m_oValue *= dValue; + + return oTemp; + } + CDigit &CDigit::operator+=(const CDigit &oDigit) { if (m_unLevel > oDigit.m_unLevel || (m_bImportant && !oDigit.m_bImportant) || DBL_MIN == oDigit.m_oValue) @@ -294,12 +382,30 @@ namespace NSCSS return *this; } + CDigit &CDigit::operator-=(double dValue) + { + m_oValue -= dValue; + return *this; + } + CDigit &CDigit::operator*=(double dValue) { m_oValue *= dValue; return *this; } + CDigit &CDigit::operator/=(double dValue) + { + m_oValue /= dValue; + return *this; + } + + CDigit &CDigit::operator =(double dValue) + { + m_oValue = dValue; + return *this; + } + bool CDigit::SetValue(const std::wstring &wsValue, unsigned int unLevel, bool bHardMode) { if (wsValue.empty() || (m_bImportant && !bHardMode)) @@ -307,16 +413,13 @@ namespace NSCSS std::wstring wsNewValue = wsValue; - bool bImportant = CutImportant(wsNewValue); - + bool bImportant = CutImportant(wsNewValue); //TODO:: иногда мы знаем, что "!important" точно не встретится + // возможно стоит добавить ещё метод if (m_bImportant && !bImportant) return false; - double dValue = 0; + CUnitMeasureConverter::GetValue(wsValue, m_oValue, m_enUnitMeasure); - std::wistringstream(wsValue) >> dValue; - - m_oValue = dValue; m_unLevel = unLevel; m_bImportant = bImportant; @@ -1572,9 +1675,9 @@ namespace NSCSS { std::wstring wsNewFamily(wsValue); - if (wsNewFamily.end() == wsNewFamily.erase(std::remove(wsNewFamily.begin(), wsNewFamily.end(), L'\''), wsNewFamily.end()) && - wsNewFamily.end() == wsNewFamily.erase(std::remove(wsNewFamily.begin(), wsNewFamily.end(), L'"'), wsNewFamily.end())) - return false; +// if (wsNewFamily.end() == wsNewFamily.erase(std::remove(wsNewFamily.begin(), wsNewFamily.end(), L'\''), wsNewFamily.end()) && +// wsNewFamily.end() == wsNewFamily.erase(std::remove(wsNewFamily.begin(), wsNewFamily.end(), L'"'), wsNewFamily.end())) +// return false; std::vector arWords = NS_STATIC_FUNCTIONS::GetWordsW(wsNewFamily, L","); diff --git a/Common/3dParty/html/css/src/StyleProperties.h b/Common/3dParty/html/css/src/StyleProperties.h index ebf369ba0e..8bbecd16c7 100644 --- a/Common/3dParty/html/css/src/StyleProperties.h +++ b/Common/3dParty/html/css/src/StyleProperties.h @@ -7,6 +7,7 @@ #include #include "../../../../DesktopEditor/graphics/Matrix.h" +#include "CUnitMeasureConverter.h" namespace NSCSS { @@ -71,8 +72,12 @@ namespace NSCSS class CDigit : public CValue { + UnitMeasure m_enUnitMeasure; + + double ConvertValue(double dPrevValue, UnitMeasure enUnitMeasure) const; public: CDigit(); + CDigit(double dValue); CDigit(double dValue, unsigned int unLevel, bool bImportant = false); bool SetValue(const std::wstring& wsValue, unsigned int unLevel, bool bHardMode) override; @@ -84,11 +89,26 @@ namespace NSCSS double ToDouble() const override; std::wstring ToWString() const override; + int ToInt(UnitMeasure enUnitMeasure, double dPrevValue = 0.) const; + double ToDouble(UnitMeasure enUnitMeasure, double dPrevValue = 0.) const; + std::wstring ToWString(UnitMeasure enUnitMeasure, double dPrevValue = 0.) const; + + UnitMeasure GetUnitMeasure() const; + CDigit operator+(const CDigit& oDigit) const; + CDigit operator-(const CDigit& oDigit) const; + CDigit operator*(const CDigit& oDigit) const; + CDigit operator/(const CDigit& oDigit) const; + + CDigit operator*(double dValue) const; CDigit& operator+=(const CDigit& oDigit); + CDigit& operator-=(const CDigit& oDigit); CDigit& operator+=(double dValue); + CDigit& operator-=(double dValue); CDigit& operator*=(double dValue); + CDigit& operator/=(double dValue); + CDigit& operator =(double dValue); }; struct TRGB diff --git a/DesktopEditor/graphics/pro/metafile.pri b/DesktopEditor/graphics/pro/metafile.pri index 1e94576c58..d72af989f2 100644 --- a/DesktopEditor/graphics/pro/metafile.pri +++ b/DesktopEditor/graphics/pro/metafile.pri @@ -74,7 +74,6 @@ METAFILE_PATH = $$PWD/../../raster/Metafile $$METAFILE_PATH/svg/CSvgFile.h \ $$METAFILE_PATH/svg/CSvgParser.h \ $$METAFILE_PATH/svg/SvgObjects/CContainer.h \ - $$METAFILE_PATH/svg/SvgObjects/CHeader.h \ $$METAFILE_PATH/svg/SvgObjects/CLine.h \ $$METAFILE_PATH/svg/SvgObjects/CRect.h \ $$METAFILE_PATH/svg/SvgObjects/CCircle.h \ @@ -92,7 +91,6 @@ METAFILE_PATH = $$PWD/../../raster/Metafile $$METAFILE_PATH/svg/CSvgFile.cpp \ $$METAFILE_PATH/svg/CSvgParser.cpp \ $$METAFILE_PATH/svg/SvgObjects/CContainer.cpp \ - $$METAFILE_PATH/svg/SvgObjects/CHeader.cpp \ $$METAFILE_PATH/svg/SvgObjects/CLine.cpp \ $$METAFILE_PATH/svg/SvgObjects/CRect.cpp \ $$METAFILE_PATH/svg/SvgObjects/CCircle.cpp \ @@ -100,6 +98,7 @@ METAFILE_PATH = $$PWD/../../raster/Metafile $$METAFILE_PATH/svg/SvgObjects/CPath.cpp \ $$METAFILE_PATH/svg/SvgObjects/CText.cpp \ $$METAFILE_PATH/svg/SvgObjects/CPolyline.cpp \ + $$METAFILE_PATH/svg/SvgObjects/CObjectBase.cpp \ $$METAFILE_PATH/svg/SvgObjects/CStyle.cpp include($$METAFILE_PATH/../../../Common/3dParty/html/css/CssCalculator.pri) diff --git a/DesktopEditor/raster/Metafile/svg/CSvgFile.cpp b/DesktopEditor/raster/Metafile/svg/CSvgFile.cpp index aed90cd66b..f5ab0d300f 100644 --- a/DesktopEditor/raster/Metafile/svg/CSvgFile.cpp +++ b/DesktopEditor/raster/Metafile/svg/CSvgFile.cpp @@ -1,7 +1,6 @@ #include "CSvgFile.h" #include "SvgObjects/CContainer.h" -#include "SvgObjects/CHeader.h" CSvgFile::CSvgFile() : m_pParser(NULL), m_pContainer(NULL) @@ -41,21 +40,20 @@ void CSvgFile::Close() bool CSvgFile::GetBounds(double &dX, double &dY, double &dWidth, double &dHeight) const { - if (NULL == m_pContainer || m_pContainer->Empty()) + if (NULL == m_pContainer || m_pContainer->Empty() || m_pContainer->GetWidth().Empty() || m_pContainer->GetHeight().Empty()) return false; - SVG::CHeader *pHeader = m_pContainer->GetHeader(); + dX = m_pContainer->GetX().ToDouble(NSCSS::Pixel); + dY = m_pContainer->GetY().ToDouble(NSCSS::Pixel);; + dWidth = m_pContainer->GetWidth().ToDouble(NSCSS::Pixel);; + dHeight = m_pContainer->GetHeight().ToDouble(NSCSS::Pixel);; - if (NULL == pHeader) - return false; - - pHeader->GetBounds(dX, dY, dWidth, dHeight); return true; } -SVG::CGeneralStyle *CSvgFile::GetBaseStyle() +const SVG::CSvgCalculator *CSvgFile::GetSvgCalculator() const { - return &m_oStyle; + return &m_oSvgCalculator; } void CSvgFile::SetFontManager(NSFonts::IFontManager *pFontManager) @@ -64,9 +62,9 @@ void CSvgFile::SetFontManager(NSFonts::IFontManager *pFontManager) m_pParser->SetFontManager(pFontManager); } -void CSvgFile::AddStyle(const std::wstring &wsStyle) +void CSvgFile::AddStyles(const std::wstring &wsStyles) { - m_oStyle.AddStyle(wsStyle); + m_oSvgCalculator.AddStyles(wsStyles); } bool CSvgFile::Draw(IRenderer *pRenderer, double dX, double dY, double dWidth, double dHeight) diff --git a/DesktopEditor/raster/Metafile/svg/CSvgFile.h b/DesktopEditor/raster/Metafile/svg/CSvgFile.h index 2640de9c4c..6e7bc24733 100644 --- a/DesktopEditor/raster/Metafile/svg/CSvgFile.h +++ b/DesktopEditor/raster/Metafile/svg/CSvgFile.h @@ -6,6 +6,7 @@ #include "../../../graphics/pro/Fonts.h" #include "CSvgParser.h" +#include "SvgObjects/CStyle.h" class GRAPHICS_DECL CSvgFile { @@ -20,20 +21,19 @@ class GRAPHICS_DECL CSvgFile void Close(); bool GetBounds(double& dX, double& dY, double& dWidth, double& dHeight) const; - - SVG::CGeneralStyle* GetBaseStyle(); + const SVG::CSvgCalculator* GetSvgCalculator() const; void SetFontManager(NSFonts::IFontManager* pFontManager); - void AddStyle(const std::wstring& wsStyle); + void AddStyles(const std::wstring& wsStyles); bool Draw(IRenderer* pRenderer, double dX, double dY, double dWidth, double dHeight); private: void Init(); - SVG::CSvgParser *m_pParser; - SVG::CContainer *m_pContainer; - SVG::CGeneralStyle m_oStyle; + SVG::CSvgParser *m_pParser; + SVG::CContainer *m_pContainer; + SVG::CSvgCalculator m_oSvgCalculator; }; #endif // CSVGFILE_H diff --git a/DesktopEditor/raster/Metafile/svg/CSvgParser.cpp b/DesktopEditor/raster/Metafile/svg/CSvgParser.cpp index bc69462edc..cfac98fea3 100644 --- a/DesktopEditor/raster/Metafile/svg/CSvgParser.cpp +++ b/DesktopEditor/raster/Metafile/svg/CSvgParser.cpp @@ -9,7 +9,6 @@ #include "SvgObjects/CContainer.h" #include "SvgObjects/CPolyline.h" #include "SvgObjects/CEllipse.h" -#include "SvgObjects/CHeader.h" #include "SvgObjects/CCircle.h" #include "SvgObjects/CStyle.h" #include "SvgObjects/CRect.h" @@ -62,12 +61,19 @@ namespace SVG bool CSvgParser::LoadFromXmlNode(XmlUtils::CXmlNode &oElement, CContainer* pContainer, CSvgFile* pFile) const { - if (NULL != pContainer && NULL != pFile && pContainer->ReadHeader(oElement, *pFile->GetBaseStyle())) - return ReadChildrens(oElement, pContainer, pFile, pContainer->GetHeader()); + if (NULL != pFile && NULL != pContainer && NULL != pFile && pContainer->ReadFromXmlNode(oElement)) + { + const CSvgCalculator *pSvgCalculator = pFile->GetSvgCalculator(); + + if (NULL != pSvgCalculator) + pSvgCalculator->SetData(*pContainer); + + return ReadChildrens(oElement, pContainer, pFile); + } return false; } - bool CSvgParser::ReadElement(XmlUtils::CXmlNode &oElement, CContainer* pContainer, CSvgFile* pFile, CObjectBase *pParent) const + bool CSvgParser::ReadElement(XmlUtils::CXmlNode &oElement, CContainer *pContainer, CSvgFile *pFile) const { if (NULL == pContainer || NULL == pFile) return false; @@ -78,55 +84,61 @@ namespace SVG if (L"svg" == wsElementName || L"g" == wsElementName) { - CContainer *pNewContainer = new CContainer(pParent); +// CContainer *pNewContainer = new CContainer(pContainer); - if (NULL == pNewContainer) - return false; +// if (NULL == pNewContainer) +// return false; - if (pNewContainer->ReadFromXmlNode(oElement, *this, pFile)) - { - pContainer->AddObject(pNewContainer); - return true; - } - else - return false; +// if (pNewContainer->ReadFromXmlNode(oElement, *this, pFile)) +// { +// pContainer->AddObject(pNewContainer); +// return true; +// } +// else +// return false; } else if (L"style" == wsElementName) { if (NULL != pFile) - pFile->AddStyle(oElement.GetText()); + pFile->AddStyles(oElement.GetText()); } else if (L"line" == wsElementName) - pObject = new CLine(pParent); + pObject = new CLine(pContainer); else if (L"rect" == wsElementName) - pObject = new CRect(pParent); + pObject = new CRect(pContainer); else if (L"circle" == wsElementName) - pObject = new CCircle(pParent); + pObject = new CCircle(pContainer); else if (L"ellipse" == wsElementName) - pObject = new CEllipse(pParent); + pObject = new CEllipse(pContainer); else if (L"path" == wsElementName) - pObject = new CPath(pParent); + pObject = new CPath(pContainer); else if (L"text" == wsElementName) - pObject = new CText(pParent, m_pFontManager); + pObject = new CText(pContainer, m_pFontManager); else if (L"polyline" == wsElementName) - pObject = new CPolyline(pParent); + pObject = new CPolyline(pContainer); else if (L"polygon" == wsElementName) - pObject = new CPolygon(pParent); + pObject = new CPolygon(pContainer); if (NULL != pObject) { - if (pObject->ReadFromXmlNode(oElement, *pFile->GetBaseStyle())) + if (pObject->ReadFromXmlNode(oElement)) + { pContainer->AddObject(pObject); + const CSvgCalculator *pSvgCalculator = pFile->GetSvgCalculator(); + + if (NULL != pSvgCalculator) + pSvgCalculator->SetData(*pObject); + } else RELEASEOBJECT(pObject); } else return false; - return ReadChildrens(oElement, pContainer, pFile, pObject); + return true; } - bool CSvgParser::ReadChildrens(XmlUtils::CXmlNode &oElement, CContainer* pContainer, CSvgFile* pFile, CObjectBase *pParent) const + bool CSvgParser::ReadChildrens(XmlUtils::CXmlNode &oElement, CContainer* pContainer, CSvgFile* pFile) const { XmlUtils::CXmlNodes arChilds; @@ -139,7 +151,7 @@ namespace SVG if (!arChilds.GetAt(unChildrenIndex, oChild)) break; - ReadElement(oChild, pContainer, pFile, pParent); + ReadElement(oChild, pContainer, pFile); oChild.Clear(); } diff --git a/DesktopEditor/raster/Metafile/svg/CSvgParser.h b/DesktopEditor/raster/Metafile/svg/CSvgParser.h index 2e8a6674d0..9bf2b18ca3 100644 --- a/DesktopEditor/raster/Metafile/svg/CSvgParser.h +++ b/DesktopEditor/raster/Metafile/svg/CSvgParser.h @@ -25,8 +25,8 @@ namespace SVG void AddStyle(const std::wstring& wsStyle); private: - bool ReadElement(XmlUtils::CXmlNode& oElement, CContainer* pContainer, CSvgFile* pFile, CObjectBase *pParent = NULL) const; - bool ReadChildrens(XmlUtils::CXmlNode& oElement, CContainer* pContainer, CSvgFile* pFile, CObjectBase *pParent = NULL) const; + bool ReadElement(XmlUtils::CXmlNode& oElement, CContainer* pContainer, CSvgFile* pFile) const; + bool ReadChildrens(XmlUtils::CXmlNode& oElement, CContainer* pContainer, CSvgFile* pFile) const; NSFonts::IFontManager *m_pFontManager; }; diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CCircle.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CCircle.cpp index fe9781dd9d..014061329c 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CCircle.cpp +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CCircle.cpp @@ -1,5 +1,8 @@ #include "CCircle.h" +#include "CContainer.h" +#include "CStyle.h" + namespace SVG { CCircle::CCircle(CObjectBase *pParent) : CObjectBase(pParent) @@ -12,16 +15,27 @@ namespace SVG } - bool CCircle::ReadFromXmlNode(XmlUtils::CXmlNode &oNode, const CGeneralStyle& oBaseStyle) + void CCircle::SetData(const std::map &mAttributes, unsigned short ushLevel, bool bHardMode) + { + SetStroke(mAttributes, ushLevel, bHardMode); + SetFill(mAttributes, ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"cx")) + m_oCx.SetValue(mAttributes.at(L"cx"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"cy")) + m_oCy.SetValue(mAttributes.at(L"cy"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"r")) + m_oR.SetValue(mAttributes.at(L"r"), ushLevel, bHardMode); + } + + bool CCircle::ReadFromXmlNode(XmlUtils::CXmlNode &oNode) { if (!oNode.IsValid()) return false; -// m_dCx = oNode.GetAttributeDouble(L"cx"); -// m_dCy = oNode.GetAttributeDouble(L"cy"); -// m_dR = oNode.GetAttributeDouble(L"r"); - - SaveNodeData(oNode, oBaseStyle); + SaveNodeData(oNode); return true; } @@ -31,22 +45,35 @@ namespace SVG if (NULL == pRenderer) return false; + double dParentWidth = 0, dParentHeight = 0; + CContainer *pContainer = dynamic_cast(m_pParent); + + if (NULL != pContainer) + { + dParentWidth = pContainer->GetWidth().ToDouble(NSCSS::Pixel); + dParentHeight = pContainer->GetHeight().ToDouble(NSCSS::Pixel); + } + + double dX = m_oCx.ToDouble(NSCSS::Pixel, dParentWidth); + double dY = m_oCy.ToDouble(NSCSS::Pixel, dParentHeight); + double dR = m_oR .ToDouble(NSCSS::Pixel); + int nPathType = 0; ApplyStyle(pRenderer, nPathType); pRenderer->PathCommandStart(); - pRenderer->BeginCommand (c_nPathType); + pRenderer->BeginCommand(c_nPathType); - pRenderer->PathCommandStart (); + pRenderer->PathCommandStart(); - pRenderer->PathCommandMoveTo(m_dCx + m_dR, m_dCy); - pRenderer->PathCommandArcTo(m_dCx - m_dR, m_dCy - m_dR, m_dR * 2.0, m_dR * 2.0, 0, 360); + pRenderer->PathCommandMoveTo(dX + dR, dY); + pRenderer->PathCommandArcTo(dX - dR, dY - dR, dR * 2.0, dR * 2.0, 0, 360); - pRenderer->DrawPath (nPathType); - pRenderer->EndCommand (c_nPathType); + pRenderer->DrawPath(nPathType); + pRenderer->EndCommand(c_nPathType); - pRenderer->PathCommandEnd (); + pRenderer->PathCommandEnd(); return true; } diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CCircle.h b/DesktopEditor/raster/Metafile/svg/SvgObjects/CCircle.h index d5fb07a581..b2c5bac3fb 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CCircle.h +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CCircle.h @@ -11,14 +11,16 @@ namespace SVG CCircle(CObjectBase* pParent = NULL); virtual ~CCircle(); - bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode, const CGeneralStyle& oBaseStyle) override; + void SetData(const std::map& mAttributes, unsigned short ushLevel, bool bHardMode = false) override; + + bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode) override; bool Draw(IRenderer* pRenderer) const override; private: void ApplyStyle(IRenderer* pRenderer, int& nTypePath) const override; - double m_dCx; - double m_dCy; - double m_dR; + SvgDigit m_oCx; + SvgDigit m_oCy; + SvgDigit m_oR; }; } diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CContainer.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CContainer.cpp index 5584060705..5ee345909a 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CContainer.cpp +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CContainer.cpp @@ -1,14 +1,12 @@ #include "CContainer.h" -#include "CHeader.h" - #include "../CSvgFile.h" #include "../CSvgParser.h" namespace SVG { CContainer::CContainer(CObjectBase *pParent) - : CObjectBase(pParent), m_pHeader(NULL) + : CObjectBase(pParent) {} CContainer::~CContainer() @@ -16,9 +14,24 @@ namespace SVG Clear(); } - bool CContainer::ReadHeader(XmlUtils::CXmlNode &oElement, const CGeneralStyle& oBaseStyle) + void CContainer::SetData(const std::map &mAttributes, unsigned short ushLevel, bool bHardMode) { - std::wstring wsNodeName = oElement.GetName(); + if (mAttributes.end() != mAttributes.find(L"x")) + m_oX.SetValue(mAttributes.at(L"x"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"y")) + m_oY.SetValue(mAttributes.at(L"y"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"width")) + m_oWidth.SetValue(mAttributes.at(L"width"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"height")) + m_oHeight.SetValue(mAttributes.at(L"height"), ushLevel, bHardMode); + } + + bool CContainer::ReadFromXmlNode(XmlUtils::CXmlNode &oNode) + { + std::wstring wsNodeName = oNode.GetName(); if (L"svg" != wsNodeName && L"g" != wsNodeName && @@ -27,18 +40,9 @@ namespace SVG Clear(); - m_pHeader = new CHeader(m_pParent); - return m_pHeader->ReadFromXmlNode(oElement, oBaseStyle); - } + SaveNodeData(oNode); - bool CContainer::ReadFromXmlNode(XmlUtils::CXmlNode &oNode, const CGeneralStyle& oBaseStyle) - { - return false; - } - - bool CContainer::ReadFromXmlNode(XmlUtils::CXmlNode &oNode, const CSvgParser &oParser, CSvgFile* pFile) - { - return oParser.LoadFromXmlNode(oNode, this, pFile); + return true; } bool CContainer::Draw(IRenderer *pRenderer) const @@ -54,7 +58,6 @@ namespace SVG void CContainer::Clear() { - RELEASEOBJECT(m_pHeader); m_arObjects.clear(); } @@ -63,9 +66,24 @@ namespace SVG return m_arObjects.empty(); } - CHeader *CContainer::GetHeader() const + SvgDigit CContainer::GetX() const { - return m_pHeader; + return m_oX; + } + + NSCSS::NSProperties::CDigit CContainer::GetY() const + { + return m_oY; + } + + SvgDigit CContainer::GetWidth() const + { + return m_oWidth; + } + + NSCSS::NSProperties::CDigit CContainer::GetHeight() const + { + return m_oHeight; } void CContainer::AddObject(CObjectBase *pObject) diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CContainer.h b/DesktopEditor/raster/Metafile/svg/SvgObjects/CContainer.h index 49f838037e..6c45bf51bd 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CContainer.h +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CContainer.h @@ -17,24 +17,30 @@ namespace SVG CContainer(CObjectBase* pParent = NULL); virtual ~CContainer(); - bool ReadHeader(XmlUtils::CXmlNode &oElement, const CGeneralStyle& oBaseStyle); + void SetData(const std::map& mAttributes, unsigned short ushLevel, bool bHardMode = false) override; - bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode, const CGeneralStyle& oBaseStyle) override; - bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode, const CSvgParser& oParser, CSvgFile* pFile); + bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode) override; bool Draw(IRenderer* pRenderer) const override; void Clear(); bool Empty() const; - CHeader* GetHeader() const; + SvgDigit GetX() const; + SvgDigit GetY() const; + SvgDigit GetWidth() const; + SvgDigit GetHeight() const; void AddObject(CObjectBase* pObject); private: void ApplyStyle(IRenderer* pRenderer, int& nTypePath) const override; - CHeader *m_pHeader; std::vector m_arObjects; + + SvgDigit m_oX; + SvgDigit m_oY; + SvgDigit m_oWidth; + SvgDigit m_oHeight; }; } diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CEllipse.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CEllipse.cpp index 24db44f09a..d85aee5d79 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CEllipse.cpp +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CEllipse.cpp @@ -1,21 +1,37 @@ #include "CEllipse.h" +#include "CStyle.h" +#include "CContainer.h" + namespace SVG { CEllipse::CEllipse(CObjectBase *pParent) : CObjectBase(pParent) {} - bool CEllipse::ReadFromXmlNode(XmlUtils::CXmlNode &oNode, const CGeneralStyle& oBaseStyle) + void CEllipse::SetData(const std::map &mAttributes, unsigned short ushLevel, bool bHardMode) + { + SetStroke(mAttributes, ushLevel, bHardMode); + SetFill(mAttributes, ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"cx")) + m_oCx.SetValue(mAttributes.at(L"cx"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"cy")) + m_oCy.SetValue(mAttributes.at(L"cy"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"rx")) + m_oRx.SetValue(mAttributes.at(L"rx"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"ry")) + m_oRy.SetValue(mAttributes.at(L"ry"), ushLevel, bHardMode); + } + + bool CEllipse::ReadFromXmlNode(XmlUtils::CXmlNode &oNode) { if (!oNode.IsValid()) return false; - m_dCx = oNode.GetAttributeDouble(L"cx"); - m_dCy = oNode.GetAttributeDouble(L"cy"); - m_dRx = oNode.GetAttributeDouble(L"rx"); - m_dRy = oNode.GetAttributeDouble(L"ry"); - - SaveNodeData(oNode, oBaseStyle); + SaveNodeData(oNode); return true; } @@ -25,22 +41,36 @@ namespace SVG if (NULL == pRenderer) return false; + double dParentWidth = 0, dParentHeight = 0; + CContainer *pContainer = dynamic_cast(m_pParent); + + if (NULL != pContainer) + { + dParentWidth = pContainer->GetWidth().ToDouble(NSCSS::Pixel); + dParentHeight = pContainer->GetHeight().ToDouble(NSCSS::Pixel); + } + + double dX = m_oCx.ToDouble(NSCSS::Pixel, dParentWidth); + double dY = m_oCy.ToDouble(NSCSS::Pixel, dParentHeight); + double dRx = m_oRx.ToDouble(NSCSS::Pixel, dParentWidth); + double dRy = m_oRy.ToDouble(NSCSS::Pixel, dParentHeight); + int nPathType = 0; ApplyStyle(pRenderer, nPathType); pRenderer->PathCommandStart(); - pRenderer->BeginCommand (c_nPathType); + pRenderer->BeginCommand(c_nPathType); - pRenderer->PathCommandStart (); + pRenderer->PathCommandStart(); - pRenderer->PathCommandMoveTo(m_dCx + m_dRx, m_dCy); - pRenderer->PathCommandArcTo(m_dCx - m_dRx, m_dCy - m_dRy, m_dRx * 2.0, m_dRy * 2.0, 0, 360); + pRenderer->PathCommandMoveTo(dX + dRx, dY); + pRenderer->PathCommandArcTo(dX - dRx, dY - dRy, dRx * 2.0, dRy * 2.0, 0, 360); - pRenderer->DrawPath (nPathType); - pRenderer->EndCommand (c_nPathType); + pRenderer->DrawPath(nPathType); + pRenderer->EndCommand(c_nPathType); - pRenderer->PathCommandEnd (); + pRenderer->PathCommandEnd(); return true; } diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CEllipse.h b/DesktopEditor/raster/Metafile/svg/SvgObjects/CEllipse.h index 1e50854442..7380de4f92 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CEllipse.h +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CEllipse.h @@ -11,15 +11,17 @@ namespace SVG public: CEllipse(CObjectBase* pParent = NULL); - bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode, const CGeneralStyle& oBaseStyle) override; + void SetData(const std::map& mAttributes, unsigned short ushLevel, bool bHardMode = false) override; + + bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode) override; bool Draw(IRenderer* pRenderer) const override; private: void ApplyStyle(IRenderer* pRenderer, int& nTypePath) const override; - double m_dCx; - double m_dCy; - double m_dRx; - double m_dRy; + SvgDigit m_oCx; + SvgDigit m_oCy; + SvgDigit m_oRx; + SvgDigit m_oRy; }; } diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CHeader.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CHeader.cpp deleted file mode 100644 index e2bf1f83f4..0000000000 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CHeader.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "CHeader.h" - -namespace SVG -{ - CHeader::CHeader(CObjectBase *pParent) : CObjectBase(pParent) - { - - } - - CHeader::~CHeader() - { - } - - bool CHeader::ReadFromXmlNode(XmlUtils::CXmlNode &oNode, const CGeneralStyle& oBaseStyle) - { - SaveNodeData(oNode, oBaseStyle); - - return true; - } - - bool CHeader::Draw(IRenderer *pRenderer) const - { - if (NULL == pRenderer) - return false; - - return true; - } - - void CHeader::GetBounds(double &dX, double &dY, double &dWidth, double &dHeight) - { - dX = m_oStyle.m_oDisplay.GetX() .ToDouble(); - dY = m_oStyle.m_oDisplay.GetY() .ToDouble(); - dWidth = m_oStyle.m_oDisplay.GetWidth() .ToDouble(); - dHeight = m_oStyle.m_oDisplay.GetHeight().ToDouble(); - } - - void CHeader::ApplyStyle(IRenderer *pRenderer, int& nTypePath) const - { - - } -} diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CHeader.h b/DesktopEditor/raster/Metafile/svg/SvgObjects/CHeader.h deleted file mode 100644 index dc33508a2d..0000000000 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CHeader.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef CHEADER_H -#define CHEADER_H - -#include "CObjectBase.h" - -namespace SVG -{ - class CHeader : public CObjectBase - { - public: - CHeader(CObjectBase* pParent = NULL); - virtual ~CHeader(); - - bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode, const CGeneralStyle& oBaseStyle) override; - bool Draw(IRenderer* pRenderer) const override; - - void GetBounds(double &dX, double &dY, double &dWidth, double &dHeight); - private: - void ApplyStyle(IRenderer* pRenderer, int& nTypePath) const override; - }; -} -#endif // CHEADER_H diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CLine.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CLine.cpp index ed3ff8ae5f..6a8473f373 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CLine.cpp +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CLine.cpp @@ -1,5 +1,8 @@ #include "CLine.h" +#include "CStyle.h" +#include "CContainer.h" + namespace SVG { CLine::CLine(CObjectBase *pParent) : CObjectBase(pParent) @@ -7,21 +10,29 @@ namespace SVG } - CLine::~CLine() + void CLine::SetData(const std::map &mAttributes, unsigned short ushLevel, bool bHardMode) { + SetStroke(mAttributes, ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"x1")) + m_oX1.SetValue(mAttributes.at(L"x1"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"y1")) + m_oY1.SetValue(mAttributes.at(L"y1"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"x2")) + m_oX2.SetValue(mAttributes.at(L"x2"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"y2")) + m_oY2.SetValue(mAttributes.at(L"y2"), ushLevel, bHardMode); } - bool CLine::ReadFromXmlNode(XmlUtils::CXmlNode &oNode, const CGeneralStyle& oBaseStyle) + bool CLine::ReadFromXmlNode(XmlUtils::CXmlNode &oNode) { if (!oNode.IsValid()) return false; - m_dX1 = oNode.GetAttributeDouble(L"x1"); - m_dY1 = oNode.GetAttributeDouble(L"y1"); - m_dX2 = oNode.GetAttributeDouble(L"x2"); - m_dY2 = oNode.GetAttributeDouble(L"y2"); - - SaveNodeData(oNode, oBaseStyle); + SaveNodeData(oNode); return true; } @@ -31,20 +42,34 @@ namespace SVG if (NULL == pRenderer) return false; + double dParentWidth = 0, dParentHeight = 0; + CContainer *pContainer = dynamic_cast(m_pParent); + + if (NULL != pContainer) + { + dParentWidth = pContainer->GetWidth().ToDouble(NSCSS::Pixel); + dParentHeight = pContainer->GetHeight().ToDouble(NSCSS::Pixel); + } + + double dX1 = m_oX1.ToDouble(NSCSS::Pixel, dParentWidth); + double dY1 = m_oY1.ToDouble(NSCSS::Pixel, dParentHeight); + double dX2 = m_oX2.ToDouble(NSCSS::Pixel, dParentWidth); + double dY2 = m_oY2.ToDouble(NSCSS::Pixel, dParentHeight); + int nPathType = 0; ApplyStyle(pRenderer, nPathType); pRenderer->PathCommandStart(); - pRenderer->BeginCommand (c_nPathType); + pRenderer->BeginCommand(c_nPathType); - pRenderer->PathCommandStart (); + pRenderer->PathCommandStart(); - pRenderer->PathCommandMoveTo (m_dX1, m_dY1); - pRenderer->PathCommandLineTo (m_dX2, m_dY2); + pRenderer->PathCommandMoveTo(dX1, dY1); + pRenderer->PathCommandLineTo(dX2, dY2); - pRenderer->DrawPath (nPathType); - pRenderer->EndCommand (c_nPathType); - pRenderer->PathCommandEnd (); + pRenderer->DrawPath(nPathType); + pRenderer->EndCommand(c_nPathType); + pRenderer->PathCommandEnd(); return true; } diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CLine.h b/DesktopEditor/raster/Metafile/svg/SvgObjects/CLine.h index acc18911e6..36dd59d8d9 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CLine.h +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CLine.h @@ -9,17 +9,18 @@ namespace SVG { public: CLine(CObjectBase* pParent = NULL); - virtual ~CLine(); - bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode, const CGeneralStyle& oBaseStyle) override; + void SetData(const std::map& mAttributes, unsigned short ushLevel, bool bHardMode = false) override; + + bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode) override; bool Draw(IRenderer* pRenderer) const override; private: void ApplyStyle(IRenderer* pRenderer, int& nTypePath) const override; - double m_dX1; - double m_dY1; - double m_dX2; - double m_dY2; + SvgDigit m_oX1; + SvgDigit m_oY1; + SvgDigit m_oX2; + SvgDigit m_oY2; }; } diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CObjectBase.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CObjectBase.cpp new file mode 100644 index 0000000000..969a3e2d76 --- /dev/null +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CObjectBase.cpp @@ -0,0 +1,156 @@ +#include "CObjectBase.h" + +namespace SVG +{ + CObjectBase::CObjectBase(CObjectBase *pParent) + : m_pParent(pParent) + {} + + CObjectBase::~CObjectBase() + {} + + std::vector CObjectBase::GetFullPath() const + { + if (NULL == m_pParent) + return {m_oXmlNode}; + + std::vector arObjects = m_pParent->GetFullPath(); + arObjects.push_back({m_oXmlNode}); + return arObjects; + } + + void CObjectBase::SetData(const std::wstring wsStyles, unsigned short ushLevel, bool bHardMode) + { + if (wsStyles.empty()) + return; + + const std::vector arWords = NSCSS::NS_STATIC_FUNCTIONS::GetWordsWithSigns(wsStyles, L":;"); + + if (arWords.empty()) + return; + + std::wstring wsProperty, wsValue; + + std::map mAttributes; + + for (std::vector::const_iterator iWord = arWords.begin(); iWord != arWords.end(); ++iWord) + { + if ((*iWord).back() == L':') + { + wsProperty = *iWord; + wsProperty.pop_back(); + } + else + { + wsValue += *iWord; + + if (L' ' == wsValue.front()) + wsValue.erase(0, 1); + + if (!wsValue.empty() && ((*iWord).back() == L';' || iWord == (arWords.end() - 1))) + { + if (wsValue.back() == L';') + wsValue.pop_back(); + + std::transform(wsProperty.begin(), wsProperty.end(), wsProperty.begin(), tolower); + std::transform(wsValue.begin(), wsValue.end(), wsValue.begin(), tolower); + mAttributes[wsProperty] = wsValue; + wsProperty.clear(); + wsValue.clear(); + } + } + } + + if (!mAttributes.empty()) + SetData(mAttributes, ushLevel, bHardMode); + } + + void CObjectBase::SaveNodeData(XmlUtils::CXmlNode &oNode) + { + std::vector arProperties, arValues; + + oNode.GetAllAttributes(arProperties, arValues); + + NSCSS::CNode oXmlNode; + m_oXmlNode.m_sName = oNode.GetName(); + + for (unsigned int unIndex = 0; unIndex < arProperties.size(); ++unIndex) + { + if (L"class" == arProperties[unIndex]) + m_oXmlNode.m_sClass = arValues[unIndex]; + else if (L"id" == arProperties[unIndex]) + m_oXmlNode.m_sId = arValues[unIndex]; + else if (L"style" == arProperties[unIndex]) + m_oXmlNode.m_sStyle = arValues[unIndex]; + else + m_oXmlNode.m_mAttrs.insert({arProperties[unIndex], arValues[unIndex]}); + } + } + + void CObjectBase::SetStroke(const std::map &mAttributes, unsigned short ushLevel, bool bHardMode) + { + if (mAttributes.end() != mAttributes.find(L"stroke")) + m_oStroke.m_oColor.SetValue(mAttributes.at(L"stroke"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"stroke-width")) + m_oStroke.m_oWidth.SetValue(mAttributes.at(L"stroke-width"), ushLevel, bHardMode); + } + + void CObjectBase::SetFill(const std::map &mAttributes, unsigned short ushLevel, bool bHardMode) + { + if (mAttributes.end() != mAttributes.find(L"fill")) + m_oFill.SetValue(mAttributes.at(L"fill"), ushLevel, bHardMode); + } + + void CObjectBase::ApplyDefaultStroke(IRenderer *pRenderer, int &nTypePath) const + { + nTypePath += c_nStroke; + pRenderer->put_PenSize(1); + pRenderer->put_PenColor(0); + } + + void CObjectBase::ApplyStroke(IRenderer *pRenderer, int &nTypePath, bool bUseDedault) const + { + if (!m_oStroke.m_oColor.Empty()) + { + nTypePath += c_nStroke; + pRenderer->put_PenColor(m_oStroke.m_oColor.ToInt()); + + double dStrokeWidth = m_oStroke.m_oWidth.ToDouble(NSCSS::Pixel); + + if (0 != dStrokeWidth) + pRenderer->put_PenSize(dStrokeWidth); + } + else if (bUseDedault) + ApplyDefaultStroke(pRenderer, nTypePath); + } + + void CObjectBase::ApplyDefaultFill(IRenderer *pRenderer, int &nTypePath) const + { + nTypePath += c_nWindingFillMode; + pRenderer->put_BrushColor1(0); + } + + void CObjectBase::ApplyFill(IRenderer *pRenderer, int &nTypePath, bool bUseDedault) const + { + if (NSCSS::NSProperties::ColorType::ColorNone == m_oFill.GetType()) + return; + else if (NSCSS::NSProperties::ColorType::ColorHEX == m_oFill.GetType() || + NSCSS::NSProperties::ColorType::ColorRGB == m_oFill.GetType()) + { + nTypePath += c_nWindingFillMode; + pRenderer->put_BrushColor1(m_oFill.ToInt()); + } + else if (bUseDedault) + ApplyDefaultFill(pRenderer, nTypePath); + } + + void CObjectBase::ApplyTransform(IRenderer *pRenderer) const + { + pRenderer->ResetTransform(); + +// Aggplus::CMatrix oMatrix = m_oStyle.m_oTransform.GetMatrix().GetValue(); + + pRenderer->SetTransform(25.4 / 96., 0, 0, 25.4 / 96., 0, 0); + } +} diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CObjectBase.h b/DesktopEditor/raster/Metafile/svg/SvgObjects/CObjectBase.h index 8f26494693..32fbe6ca09 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CObjectBase.h +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CObjectBase.h @@ -1,160 +1,46 @@ #ifndef COBJECTBASE_H #define COBJECTBASE_H -#include "../../../../Common/3dParty/html/css/src/StyleProperties.h" +#include "../../../../Common/3dParty/html/css/src/CNode.h" +#include "../../../../Common/3dParty/html/css/src/StaticFunctions.h" #include "../../../xml/include/xmlutils.h" #include "../../IRenderer.h" -#include "CStyle.h" - -#define MapCI std::map::const_iterator - -#define SVGDigit NSCSS::NSProperties::CDigit -#define SVGString NSCSS::NSProperties::CString -#define SVGColor NSCSS::NSProperties::CColor -#define SVGMatrix NSCSS::NSProperties::CMatrix +#include "../SvgTypes.h" namespace SVG { class CObjectBase { public: - CObjectBase(CObjectBase* pParent = NULL) : m_pParent(pParent){}; - virtual ~CObjectBase(){}; + CObjectBase(CObjectBase* pParent = NULL); + virtual ~CObjectBase(); - virtual bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode, const CGeneralStyle& oBaseStyle) = 0; + virtual bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode) = 0; virtual bool Draw(IRenderer* pRenderer) const = 0; + + std::vector GetFullPath() const; + + void SetData(const std::wstring wsStyles, unsigned short ushLevel, bool bHardMode = false); + + virtual void SetData(const std::map& mAttributes, unsigned short ushLevel, bool bHardMode = false) = 0; private: - void SaveNodeData(XmlUtils::CXmlNode& oNode, const CGeneralStyle& oBaseStyle) - { - std::vector arProperties, arValues; + void SaveNodeData(XmlUtils::CXmlNode& oNode); - oNode.GetAllAttributes(arProperties, arValues); - - NSCSS::CNode oXmlNode; - m_oXmlNode.m_sName = oNode.GetName(); - - for (unsigned int unIndex = 0; unIndex < arProperties.size(); ++unIndex) - { - if (L"class" == arProperties[unIndex]) - m_oXmlNode.m_sClass = arValues[unIndex]; - else if (L"id" == arProperties[unIndex]) - m_oXmlNode.m_sId = arValues[unIndex]; - else if (L"style" == arProperties[unIndex]) - m_oXmlNode.m_sStyle = arValues[unIndex]; - else - m_oXmlNode.m_mAttrs.insert({arProperties[unIndex], arValues[unIndex]}); - } - - m_oStyle = (oBaseStyle.GetStyle(GetFullPath())); - }; - - std::vector GetFullPath() const - { - if (NULL == m_pParent) - return {m_oXmlNode}; - - std::vector arObjects = m_pParent->GetFullPath(); - arObjects.push_back({m_oXmlNode}); - return arObjects; - } + void SetStroke(const std::map& mAttributes, unsigned short ushLevel, bool bHardMode = false); + void SetFill(const std::map& mAttributes, unsigned short ushLevel, bool bHardMode = false); virtual void ApplyStyle(IRenderer* pRenderer, int& nTypePath) const = 0; - void ApplyDefaultStroke(IRenderer* pRenderer, int& nTypePath) const - { - nTypePath += c_nStroke; - pRenderer->put_PenColor(0); - } - - void ApplyStroke(IRenderer* pRenderer, int& nTypePath, bool bUseDedault = false) const - { - if (!m_oStyle.m_oStroke.GetColor().Empty()) - { - nTypePath += c_nStroke; - pRenderer->put_PenColor(m_oStyle.m_oStroke.GetColor().ToInt()); - - double dStrokeWidth = m_oStyle.m_oStroke.GetWidth().ToDouble(); - - if (0 != dStrokeWidth) - pRenderer->put_PenSize(dStrokeWidth); - } - else if (bUseDedault) - ApplyDefaultStroke(pRenderer, nTypePath); - - } - - void ApplyDefaultFill(IRenderer* pRenderer, int& nTypePath) const - { - nTypePath += c_nWindingFillMode; - pRenderer->put_BrushColor1(0); - } - - void ApplyFill(IRenderer* pRenderer, int& nTypePath, bool bUseDedault = false) const - { - if (NSCSS::NSProperties::ColorType::ColorNone == m_oStyle.m_oBackground.GetColor().GetType()) - return; - else if (NSCSS::NSProperties::ColorType::ColorHEX == m_oStyle.m_oBackground.GetColor().GetType() || - NSCSS::NSProperties::ColorType::ColorRGB == m_oStyle.m_oBackground.GetColor().GetType()) - { - nTypePath += c_nWindingFillMode; - pRenderer->put_BrushColor1(m_oStyle.m_oBackground.GetColor().ToInt()); - } - else if (bUseDedault) - { - ApplyDefaultFill(pRenderer, nTypePath); - } - } - - void ApplyTransform(IRenderer* pRenderer) const - { - pRenderer->ResetTransform(); - - Aggplus::CMatrix oMatrix = m_oStyle.m_oTransform.GetMatrix().GetValue(); - - pRenderer->SetTransform(oMatrix.sx() * 25.4 / 96., oMatrix.shy(), oMatrix.shx(), oMatrix.sy() * 25.4 / 96., oMatrix.tx(), oMatrix.ty()); - } - - void ApplyFont(IRenderer* pRenderer, double dScaleX) const - { - std::wstring wsName = m_oStyle.m_oFont.GetFamily().ToWString(); - - if (!wsName.empty()) - pRenderer->put_FontName(wsName.substr(1, wsName.length() - 2)); - - double dSize = m_oStyle.m_oFont.GetSize().ToDouble() /** dScaleX*/; - - if (0 != dSize) - pRenderer->put_FontSize(dSize / 25.4 * 72); - - int lStyle = 0; - if (m_oStyle.m_oFont.GetWeight() == L"bold") - lStyle |= 0x01; - if (m_oStyle.m_oFont.GetStyle() == L"italic") - lStyle |= 0x02; -// if (pFont->IsUnderline()) -// lStyle |= (1 << 2); -// if (pFont->IsStrikeOut()) -// lStyle |= (1 << 7); - - pRenderer->put_FontStyle(lStyle); - - pRenderer->put_BrushType(c_BrushTypeSolid); - - int nColor = m_oStyle.m_oBackground.GetColor().ToInt(); - - if (-1 == nColor) - pRenderer->put_BrushColor1(0); - else - pRenderer->put_BrushColor1(nColor); - - pRenderer->put_BrushAlpha1(255); - } + void ApplyDefaultStroke(IRenderer* pRenderer, int& nTypePath) const; + void ApplyStroke(IRenderer* pRenderer, int& nTypePath, bool bUseDedault = false) const; + void ApplyDefaultFill(IRenderer* pRenderer, int& nTypePath) const; + void ApplyFill(IRenderer* pRenderer, int& nTypePath, bool bUseDedault = false) const; + void ApplyTransform(IRenderer* pRenderer) const; friend class CLine; friend class CRect; friend class CCircle; friend class CEllipse; - friend class CHeader; friend class CPath; friend class CText; friend class CTspan; @@ -164,7 +50,10 @@ namespace SVG CObjectBase *m_pParent; NSCSS::CNode m_oXmlNode; - CStyle m_oStyle; + + //Styles + SvgColor m_oFill; + CStroke m_oStroke; }; } diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CPath.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CPath.cpp index 7963a79f2c..f3eb90f184 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CPath.cpp +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CPath.cpp @@ -15,7 +15,12 @@ namespace SVG delete pPathElement; } - bool CPath::ReadFromXmlNode(XmlUtils::CXmlNode &oNode, const CGeneralStyle& oBaseStyle) + void CPath::SetData(const std::map &mAttributes, unsigned short ushLevel, bool bHardMode) + { + + } + + bool CPath::ReadFromXmlNode(XmlUtils::CXmlNode &oNode) { std::wstring wsPoints = oNode.GetAttribute(L"d"); @@ -24,7 +29,7 @@ namespace SVG ReadFromString(wsPoints); - SaveNodeData(oNode, oBaseStyle); + SaveNodeData(oNode); return true; } diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CPath.h b/DesktopEditor/raster/Metafile/svg/SvgObjects/CPath.h index 3502dd87c8..26a03f4f82 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CPath.h +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CPath.h @@ -1073,7 +1073,9 @@ namespace SVG CPath(CObjectBase* pParent = NULL); virtual ~CPath(); - bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode, const CGeneralStyle& oBaseStyle) override; + void SetData(const std::map& mAttributes, unsigned short ushLevel, bool bHardMode = false) override; + + bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode) override; bool Draw(IRenderer* pRenderer) const override; private: void ApplyStyle(IRenderer* pRenderer, int& nTypePath) const override; diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CPolyline.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CPolyline.cpp index 43dfe0693c..f3f5ad7e63 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CPolyline.cpp +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CPolyline.cpp @@ -8,7 +8,12 @@ namespace SVG CPolyline::~CPolyline() {} - bool CPolyline::ReadFromXmlNode(XmlUtils::CXmlNode &oNode, const CGeneralStyle& oBaseStyle) + void CPolyline::SetData(const std::map &mAttributes, unsigned short ushLevel, bool bHardMode) + { + + } + + bool CPolyline::ReadFromXmlNode(XmlUtils::CXmlNode &oNode) { if (!oNode.IsValid()) return false; @@ -18,9 +23,9 @@ namespace SVG if (wsValue.empty()) return false; - m_arValues = StrUtils::ReadDoubleValues(wsValue); + m_arValues = NSCSS::NS_STATIC_FUNCTIONS::ReadDoubleValues(wsValue); - SaveNodeData(oNode, oBaseStyle); + SaveNodeData(oNode); return m_arValues.size() >= 4; } @@ -72,7 +77,7 @@ namespace SVG pRenderer->PathCommandEnd(); } - CPolygon::CPolygon(CObjectBase *pParent) : CPolyline(pParent) + CPolygon::CPolygon(CObjectBase *pParent) : CPolyline(pParent) {} CPolygon::~CPolygon() diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CPolyline.h b/DesktopEditor/raster/Metafile/svg/SvgObjects/CPolyline.h index a392e39ea6..2a1db47cc2 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CPolyline.h +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CPolyline.h @@ -11,7 +11,9 @@ namespace SVG CPolyline(CObjectBase* pParent = NULL); virtual ~CPolyline(); - bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode, const CGeneralStyle& oBaseStyle) override; + void SetData(const std::map& mAttributes, unsigned short ushLevel, bool bHardMode = false) override; + + bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode) override; bool Draw(IRenderer* pRenderer) const override; private: void ApplyStyle(IRenderer* pRenderer, int& nTypePath) const override; diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CRect.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CRect.cpp index f614f55aff..edaec057df 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CRect.cpp +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CRect.cpp @@ -1,96 +1,118 @@ #include "CRect.h" #include "CStyle.h" +#include "CContainer.h" #include "../SvgTypes.h" namespace SVG { CRect::CRect(CObjectBase *pParent) : CObjectBase(pParent) - { - - } + {} CRect::~CRect() - { + {} + void CRect::SetData(const std::map &mAttributes, unsigned short ushLevel, bool bHardMode) + { + SetStroke(mAttributes, ushLevel, bHardMode); + SetFill(mAttributes, ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"x")) + m_oX.SetValue(mAttributes.at(L"x"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"y")) + m_oY.SetValue(mAttributes.at(L"y"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"width")) + m_oWidth.SetValue(mAttributes.at(L"width"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"height")) + m_oHeight.SetValue(mAttributes.at(L"height"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"rx")) + m_oRx.SetValue(mAttributes.at(L"rx"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"ry")) + m_oRy.SetValue(mAttributes.at(L"ry"), ushLevel, bHardMode); } - bool CRect::ReadFromXmlNode(XmlUtils::CXmlNode &oNode, const CGeneralStyle& oBaseStyle) + bool CRect::ReadFromXmlNode(XmlUtils::CXmlNode &oNode) { if (!oNode.IsValid()) return false; - SaveNodeData(oNode, oBaseStyle); + SaveNodeData(oNode); return true; } bool CRect::Draw(IRenderer *pRenderer) const { + if (NULL == pRenderer) + return false; + + double dParentWidth = 0, dParentHeight = 0; + CContainer *pContainer = dynamic_cast(m_pParent); + + if (NULL != pContainer) + { + dParentWidth = pContainer->GetWidth().ToDouble(NSCSS::Pixel); + dParentHeight = pContainer->GetHeight().ToDouble(NSCSS::Pixel); + } + + double dX = m_oX.ToDouble(NSCSS::Pixel, dParentWidth); + double dY = m_oY.ToDouble(NSCSS::Pixel, dParentHeight); + double dWidth = m_oWidth.ToDouble(NSCSS::Pixel, dParentWidth); + double dHeight = m_oHeight.ToDouble(NSCSS::Pixel, dParentHeight); + int nPathType = 0; ApplyStyle(pRenderer, nPathType); - double dX = m_oStyle.m_oDisplay.GetX().ToDouble(); - double dY = m_oStyle.m_oDisplay.GetY().ToDouble(); - double dWidth = m_oStyle.m_oDisplay.GetWidth().ToDouble(); - double dHeight= m_oStyle.m_oDisplay.GetHeight().ToDouble(); + if (m_oRx.Empty() && m_oRy.Empty()) + { + pRenderer->PathCommandStart(); + pRenderer->BeginCommand(c_nPathType); - pRenderer->PathCommandStart(); - pRenderer->BeginCommand(c_nPathType); + pRenderer->PathCommandStart(); - pRenderer->PathCommandMoveTo(dX, dY); - pRenderer->PathCommandLineTo(dX + dWidth, dY); - pRenderer->PathCommandLineTo(dX + dWidth, dY + dHeight); - pRenderer->PathCommandLineTo(dX, dY + dHeight); - pRenderer->PathCommandClose(); + pRenderer->PathCommandMoveTo(dX, dY); + pRenderer->PathCommandLineTo(dX + dWidth, dY); + pRenderer->PathCommandLineTo(dX + dWidth, dY + dHeight); + pRenderer->PathCommandLineTo(dX, dY + dHeight); + pRenderer->PathCommandClose(); - pRenderer->DrawPath(nPathType); - pRenderer->EndCommand(c_nPathType); - pRenderer->PathCommandEnd(); + pRenderer->DrawPath(nPathType); + pRenderer->EndCommand(c_nPathType); + pRenderer->PathCommandEnd(); + } + else + { + double dRx = m_oRx.ToDouble(NSCSS::Pixel); + double dRy = m_oRy.ToDouble(NSCSS::Pixel); + pRenderer->PathCommandStart(); + pRenderer->BeginCommand(c_nPathType); -// if ((fabs(m_dRx) < 0.000001) && (fabs(m_dRy) < 0.000001)) -// { -// pRenderer->PathCommandStart(); -// pRenderer->BeginCommand(c_nPathType); + pRenderer->PathCommandStart(); -// pRenderer->PathCommandStart(); + pRenderer->PathCommandMoveTo(dX + dRx, dY); -// pRenderer->PathCommandMoveTo(m_dX, m_dY); -// pRenderer->PathCommandLineTo(m_dX + dWidth, m_dY); -// pRenderer->PathCommandLineTo(m_dX + dWidth, m_dY + m_dHeight); -// pRenderer->PathCommandLineTo(m_dX, m_dY + m_dHeight); -// pRenderer->PathCommandClose(); + pRenderer->PathCommandLineTo(dX - dRx, dY); + pRenderer->PathCommandArcTo(dX - dRx * 2.0, dY, dRx * 2.0, dRy * 2.0, 270.0, 90.0); -// pRenderer->DrawPath(nPathType); -// pRenderer->EndCommand(c_nPathType); -// pRenderer->PathCommandEnd(); -// } -// else -// { -// pRenderer->PathCommandStart(); -// pRenderer->BeginCommand(c_nPathType); + pRenderer->PathCommandLineTo(dX, dY + dHeight - dRy); + pRenderer->PathCommandArcTo(dX - dRx * 2.0, dY + dHeight - dRy * 2.0, dRx * 2.0, dRy * 2.0, 0.0, 90.0); -// pRenderer->PathCommandStart(); + pRenderer->PathCommandLineTo(dX + dWidth + dRx, dY + dHeight); + pRenderer->PathCommandArcTo(dX + dWidth, dY + dHeight - dRy * 2.0, dRx * 2.0, dRy * 2.0, 90.0, 90.0); -// pRenderer->PathCommandMoveTo(m_dX + m_dRx, m_dY); + pRenderer->PathCommandLineTo(dX + dWidth, dY + dRy); + pRenderer->PathCommandArcTo(dX + dWidth, dY, dRx * 2.0, dRy * 2.0, 180.0, 90.0); -// pRenderer->PathCommandLineTo(m_dX - m_dRx, m_dY); -// pRenderer->PathCommandArcTo(m_dX - m_dRx * 2.0, m_dY, m_dRx * 2.0, m_dRy * 2.0, 270.0, 90.0); - -// pRenderer->PathCommandLineTo(m_dX, m_dY + m_dHeight - m_dRy); -// pRenderer->PathCommandArcTo(m_dX - m_dRx * 2.0, m_dY + m_dHeight - m_dRy * 2.0, m_dRx * 2.0, m_dRy * 2.0, 0.0, 90.0); - -// pRenderer->PathCommandLineTo(m_dX + dWidth + m_dRx, m_dY + m_dHeight); -// pRenderer->PathCommandArcTo(m_dX + dWidth, m_dY + m_dHeight - m_dRy * 2.0, m_dRx * 2.0, m_dRy * 2.0, 90.0, 90.0); - -// pRenderer->PathCommandLineTo(m_dX + dWidth, m_dY + m_dRy); -// pRenderer->PathCommandArcTo(m_dX + dWidth, m_dY, m_dRx * 2.0, m_dRy * 2.0, 180.0, 90.0); - -// pRenderer->DrawPath(nPathType); -// pRenderer->EndCommand(c_nPathType); -// pRenderer->PathCommandEnd (); -// } + pRenderer->DrawPath(nPathType); + pRenderer->EndCommand(c_nPathType); + pRenderer->PathCommandEnd (); + } return true; } diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CRect.h b/DesktopEditor/raster/Metafile/svg/SvgObjects/CRect.h index 07d607bb9c..69d2c74933 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CRect.h +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CRect.h @@ -11,10 +11,20 @@ namespace SVG CRect(CObjectBase* pParent = NULL); virtual ~CRect(); - bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode, const CGeneralStyle& oBaseStyle) override; + void SetData(const std::map& mAttributes, unsigned short ushLevel, bool bHardMode = false) override; + + bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode) override; bool Draw(IRenderer* pRenderer) const override; private: void ApplyStyle(IRenderer* pRenderer, int& nTypePath) const override; + + SvgDigit m_oX; + SvgDigit m_oY; + SvgDigit m_oWidth; + SvgDigit m_oHeight; + + SvgDigit m_oRx; + SvgDigit m_oRy; }; } diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CStyle.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CStyle.cpp index 451aebde40..106e618576 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CStyle.cpp +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CStyle.cpp @@ -1,80 +1,189 @@ #include "CStyle.h" +#include + #include "CObjectBase.h" #include "../SvgUtils.h" +#include "../../../../../Common/3dParty/html/css/src/CElement.h" + namespace SVG { - CGeneralStyle::CGeneralStyle() + CSvgCalculator::CSvgCalculator() + : m_pInternal(new NSCSS::CCssCalculator_Private) { - m_oCssCalculator.SetUnitMeasure(NSCSS::UnitMeasure::Pixel); + m_pInternal->SetDpi(96); + m_pInternal->SetUnitMeasure(NSCSS::UnitMeasure::Pixel); } - CGeneralStyle::~CGeneralStyle() + CSvgCalculator::~CSvgCalculator() { - + if (NULL != m_pInternal) + delete m_pInternal; } - void CGeneralStyle::AddStyle(const std::wstring &wsStyle) + void CSvgCalculator::AddStyles(const std::wstring &wsStyles) { - m_oCssCalculator.AddStyles(wsStyle); + m_pInternal->AddStyles(wsStyles); } - CStyle CGeneralStyle::GetStyle(const std::vector& arNodes) const + void CSvgCalculator::SetData(CObjectBase &oSvgObject) const { - CStyle oStyle; + const std::map *pData = m_pInternal->GetData(); + const std::vector arSelectors = oSvgObject.GetFullPath(); - m_oCssCalculator.GetCompiledStyle(oStyle, arNodes); + if ((NULL == pData || pData->empty()) && arSelectors.empty()) + return; - return oStyle; - } + std::vector arWords; + arWords.reserve(arSelectors.size() * 2); - void CGeneralStyle::SetStyle(CObjectBase &oObject) - { + std::vector arNextNodes; + arNextNodes.reserve(arSelectors.size() * 2); - } - - CStyle::CStyle() - {} - - CStyle::~CStyle() - { - - } - - void CStyle::AddOtherStyle(const std::pair &oStyle, const unsigned int unLevel, const bool &bHardMode) - { - if (L"fill" == oStyle.first) + for (std::vector::const_reverse_iterator oNode = arSelectors.rbegin(); oNode != arSelectors.rend(); ++oNode) { - m_oBackground.SetColor(oStyle.second, unLevel, bHardMode); + arWords.push_back(oNode->m_sName); + + if (!oNode->m_sClass.empty()) + { + if (oNode->m_sClass.find(L' ') != std::wstring::npos) + { + std::vector arClasses = NSCSS::NS_STATIC_FUNCTIONS::GetWordsW(oNode->m_sClass, L" "); + + if (arClasses.size() > 1) + arClasses.resize(unique(arClasses.begin(),arClasses.end()) - arClasses.begin()); + switch (arClasses.size()) + { + case 1: + { + arWords.push_back(L'.' + arClasses[0]); + break; + } + case 2: + { + arWords.push_back(L'.' + arClasses[0] + L" ." + arClasses[1]); + break; + } + case 3: + { + arWords.push_back(L'.' + arClasses[0] + L" ." + arClasses[1] + L" ." + arClasses[2]); + break; + } + default: + { + arWords.push_back(std::accumulate(arClasses.begin(), arClasses.end(), std::wstring(), + [](std::wstring sRes, const std::wstring& sClass) + {return sRes += L'.' + sClass + L' ';})); + break; + } + } + } + else + arWords.push_back(L'.' + oNode->m_sClass); + } + if (!oNode->m_sId.empty()) + arWords.push_back(L'#' + oNode->m_sId); } - else if (L"stroke" == oStyle.first) + + std::vector arElements; + + for (size_t i = 0; i < arSelectors.size(); ++i) { - m_oStroke.SetColor(oStyle.second, unLevel, bHardMode); - } - else if (L"stroke-width" == oStyle.first) - { - m_oStroke.SetWidth(ConvertUnitMeasure(oStyle.second, 0.0f), unLevel, bHardMode); - } - else if (L"transform" == oStyle.first) - { - m_oTransform.SetMatrix(oStyle.second, unLevel, bHardMode); - } - else if (L"x" == oStyle.first || L"cx" == oStyle.first) - { - m_oDisplay.SetX(ConvertUnitMeasure(oStyle.second, m_oDisplay.GetWidth().ToDouble(), NSCSS::ScalingDirectionX), unLevel, bHardMode); - } - else if (L"y" == oStyle.first || L"cy" == oStyle.first) - { - m_oDisplay.SetY(ConvertUnitMeasure(oStyle.second, m_oDisplay.GetWidth().ToDouble(), NSCSS::ScalingDirectionY), unLevel, bHardMode); - } - else if (L"dx" == oStyle.first) - { - m_oDisplay.AddX(ConvertUnitMeasure(oStyle.second, m_oDisplay.GetWidth().ToDouble(), NSCSS::ScalingDirectionX), unLevel, bHardMode); - } - else if (L"dy" == oStyle.first) - { - m_oDisplay.AddY(ConvertUnitMeasure(oStyle.second, m_oDisplay.GetWidth().ToDouble(), NSCSS::ScalingDirectionY), unLevel, bHardMode); + std::wstring sName, sId; + std::vector arClasses; + + if (arWords.back()[0] == L'#') + { + sId = arWords.back(); + arWords.pop_back(); + arNextNodes.push_back(sId); + } + + if (arWords.back()[0] == L'.') + { + arClasses = NSCSS::NS_STATIC_FUNCTIONS::GetWordsW(arWords.back(), L" "); + arNextNodes.push_back(arWords.back()); + arWords.pop_back(); + } + + sName = arWords.back(); + arWords.pop_back(); + arNextNodes.push_back(sName); + + const std::map::const_iterator oFindName = pData->find(sName); + std::map::const_iterator oFindId; + std::vector arFindElements; + + if (!sId.empty()) + { + oFindId = pData->find(sId); + + if (oFindId != std::end(*pData)) + { + if (!oFindId->second->Empty()) + arFindElements.push_back(oFindId->second); + + const std::vector arTempPrev = oFindId->second->GetPrevElements(arNextNodes.rbegin() + ((arClasses.empty()) ? 1 : 2), arNextNodes.rend()); + + if (!arTempPrev.empty()) + arFindElements.insert(arFindElements.end(), arTempPrev.begin(), arTempPrev.end()); + } + } + + if (!arClasses.empty()) + { + for (std::vector::const_reverse_iterator iClass = arClasses.rbegin(); iClass != arClasses.rend(); ++iClass) + { + const std::map::const_iterator oFindClass = pData->find(*iClass); + if (oFindClass != std::end(*pData)) + { + if (!oFindClass->second->Empty()) + arFindElements.push_back(oFindClass->second); + + const std::vector arTempPrev = oFindClass->second->GetPrevElements(arNextNodes.rbegin() + 2, arNextNodes.rend()); + const std::vector arTempKins = oFindClass->second->GetNextOfKin(sName); + + if (!arTempPrev.empty()) + arFindElements.insert(arFindElements.end(), arTempPrev.begin(), arTempPrev.end()); + + if (!arTempKins.empty()) + arFindElements.insert(arFindElements.end(), arTempKins.begin(), arTempKins.end()); + } + } + } + + if (oFindName != std::end(*pData)) + { + if (!oFindName->second->Empty()) + arFindElements.push_back(oFindName->second); + + const std::vector arTempPrev = oFindName->second->GetPrevElements(arNextNodes.rbegin() + 1, arNextNodes.rend()); + const std::vector arTempKins = oFindName->second->GetNextOfKin(sName, arClasses); + + if (!arTempPrev.empty()) + arFindElements.insert(arFindElements.end(), arTempPrev.begin(), arTempPrev.end()); + + if (!arTempKins.empty()) + arFindElements.insert(arFindElements.end(), arTempKins.begin(), arTempKins.end()); + } + + + if (arFindElements.size() > 1) + { + std::sort(arFindElements.rbegin(), arFindElements.rend(), + [](NSCSS::CElement* oFirstElement, NSCSS::CElement* oSecondElement) + { + return oFirstElement->GetWeight() > oSecondElement->GetWeight(); + }); + } + + oSvgObject.SetData(arSelectors[i].m_mAttrs, i + 1); + + for (const NSCSS::CElement* oElement : arFindElements) + oSvgObject.SetData(oElement->GetStyle(), i + 1); + + oSvgObject.SetData(arSelectors[i].m_sStyle, i + 1, true); } } } diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CStyle.h b/DesktopEditor/raster/Metafile/svg/SvgObjects/CStyle.h index 3399634fd3..498bc20bba 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CStyle.h +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CStyle.h @@ -3,37 +3,21 @@ #include #include "../SvgUtils.h" -#include "../../../../../Common/3dParty/html/css/src/CCssCalculator.h" +#include "../../../../../Common/3dParty/html/css/src/CCssCalculator_Private.h" +#include "CObjectBase.h" namespace SVG { - class CObjectBase; - - class CStyle : public NSCSS::CCompiledStyle + class CSvgCalculator { + NSCSS::CCssCalculator_Private *m_pInternal; public: - CStyle(); - ~CStyle(); + CSvgCalculator(); + ~CSvgCalculator(); - virtual void AddOtherStyle(const std::pair& oStyle, const unsigned int unLevel, const bool& bHardMode = true); + void AddStyles(const std::wstring& wsStyles); - NSCSS::NSProperties::CStroke m_oStroke; - NSCSS::NSProperties::CTransform m_oTransform; - }; - - class CGeneralStyle - { - public: - CGeneralStyle(); - ~CGeneralStyle(); - - void AddStyle(const std::wstring& wsStyle); - CStyle GetStyle(const std::vector& arNodes) const; - - void SetStyle(CObjectBase& oObject); - - private: - NSCSS::CCssCalculator m_oCssCalculator; + void SetData(CObjectBase& oSvgObject) const; }; } diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CText.cpp b/DesktopEditor/raster/Metafile/svg/SvgObjects/CText.cpp index a883e98e1c..ff4b97c119 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CText.cpp +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CText.cpp @@ -1,6 +1,8 @@ #include "CText.h" #include "../SvgUtils.h" +#include "CContainer.h" +#include "CStyle.h" #ifndef MININT8 #define MAXUINT8 ((unsigned char)~((unsigned char)0)) @@ -10,6 +12,8 @@ namespace SVG { +#define DefaultFontFamily L"Times New Roman" + CText::CText(CObjectBase *pParent, NSFonts::IFontManager* pFontManager) : CObjectBase(pParent), m_pFontManager(pFontManager)/*, m_oCoord({0, 0})*/ {} @@ -20,14 +24,54 @@ namespace SVG delete pTspan; } - bool CText::ReadFromXmlNode(XmlUtils::CXmlNode &oNode, const CGeneralStyle& oBaseStyle) + void CText::SetData(const std::map &mAttributes, unsigned short ushLevel, bool bHardMode) { - if (!oNode.IsValid()) + SetStroke(mAttributes, ushLevel, bHardMode); + SetFill(mAttributes, ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"x")) + m_oX.SetValue(mAttributes.at(L"x"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"y")) + m_oY.SetValue(mAttributes.at(L"y"), ushLevel, bHardMode); + + //FONT + if (mAttributes.end() != mAttributes.find(L"font")) + m_oFont.SetValue(mAttributes.at(L"font"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"font-size")) + m_oFont.SetSize(mAttributes.at(L"font-size"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"font-size-adjust")) + m_oFont.SetSize(mAttributes.at(L"font-size-adjust"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"font-stretch")) + m_oFont.SetStretch(mAttributes.at(L"font-stretch"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"font-style")) + m_oFont.SetStyle(mAttributes.at(L"font-style"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"font-variant")) + m_oFont.SetVariant(mAttributes.at(L"font-variant"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"font-weight")) + m_oFont.SetWeight(mAttributes.at(L"font-weight"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"font-family")) + m_oFont.SetFamily(mAttributes.at(L"font-family"), ushLevel, bHardMode); + + if (mAttributes.end() != mAttributes.find(L"line-height")) + m_oFont.SetLineHeight(mAttributes.at(L"line-height"), ushLevel, bHardMode); + } + + bool CText::ReadFromXmlNode(XmlUtils::CXmlNode &oNode) + { + if (!oNode.IsValid() || NULL != dynamic_cast(m_pParent)) return false; m_wsText = StrUtils::TrimExtraEnding(oNode.GetText()); - SaveNodeData(oNode, oBaseStyle); + SaveNodeData(oNode); XmlUtils::CXmlNodes arChilds; @@ -42,7 +86,7 @@ namespace SVG { CTspan *pTSpan = new CTspan(this); - if (pTSpan->ReadFromXmlNode(oChild, oBaseStyle)) + if (pTSpan->ReadFromXmlNode(oChild)) m_arChildrens.push_back(pTSpan); } } @@ -55,17 +99,28 @@ namespace SVG if (NULL == pRenderer || m_wsText.empty()) return false; - pRenderer->ResetTransform(); + double dM11, dM12, dM21, dM22, dRx, dRy; + pRenderer->GetTransform(&dM11, &dM12, &dM21, &dM22, &dRx, &dRy); -// Aggplus::CMatrix oMatrix = m_oStyle.GetTransform(); -// oMatrix.TransformPoint(dX, dY); + double dParentWidth = 0, dParentHeight = 0; + CContainer *pContainer = dynamic_cast(m_pParent); -// ApplyFont(pRenderer, oStyle, oMatrix.sx()); + if (NULL != pContainer) + { + dParentWidth = pContainer->GetWidth().ToDouble(NSCSS::Pixel); + dParentHeight = pContainer->GetHeight().ToDouble(NSCSS::Pixel); + } -// for (double dNewY = 40; dNewY < 50; dNewY += 10) -// { -// pRenderer->CommandDrawText(m_wsText, dX, dNewY, 0, 0); -// } + double dX = m_oX.ToDouble(NSCSS::Pixel, dParentWidth); + double dY = m_oY.ToDouble(NSCSS::Pixel, dParentHeight); + + GetWidth(); + + int nPathType = 0; + + ApplyStyle(pRenderer, nPathType); + + pRenderer->CommandDrawText(m_wsText, dX, dY, 0, 0); // for (CTspan* pTspan : m_arChildrens) // pTspan->Draw(pRenderer, pBaseStyle); @@ -75,11 +130,38 @@ namespace SVG void CText::ApplyStyle(IRenderer *pRenderer, int& nTypePath) const { + ApplyFont(pRenderer); + ApplyTransform(pRenderer); + } + + void CText::ApplyFont(IRenderer* pRenderer) const + { + pRenderer->put_FontName((!m_oFont.GetFamily().Empty()) ? m_oFont.GetFamily().ToWString() : DefaultFontFamily); + pRenderer->put_FontSize((!m_oFont.GetSize().Empty()) ? m_oFont.GetSize().ToDouble(NSCSS::Pixel) * 72. / 25.4 : 18.); + + int nStyle = 0; + + if (m_oFont.GetWeight().ToWString() == L"bold") + nStyle |= 0x01; + if (m_oFont.GetStyle() .ToWString() == L"italic") + nStyle |= 0x02; +// if (pFont->IsUnderline()) +// nStyle |= (1 << 2); +// if (pFont->IsStrikeOut()) +// nStyle |= (1 << 7); + + pRenderer->put_FontStyle(nStyle); + pRenderer->put_BrushType(c_BrushTypeSolid); + pRenderer->put_BrushColor1(m_oFill.ToInt()); + pRenderer->put_BrushAlpha1(255); } double CText::GetWidth() const { - m_pFontManager->LoadFontByName(L"Arial", 1, 0, 72, 72); + std::wstring wsName = (!m_oFont.GetFamily().Empty()) ? m_oFont.GetFamily().ToWString() : DefaultFontFamily; + double dSize = (!m_oFont.GetSize().Empty()) ? m_oFont.GetSize().ToDouble(NSCSS::Pixel) * 72. / 25.4 : 18.; + + m_pFontManager->LoadFontByName(wsName, dSize, 0, 72, 72); m_pFontManager->LoadString1(m_wsText, 0, 0); TBBox oBox = m_pFontManager->MeasureString2(); @@ -106,11 +188,18 @@ namespace SVG } - bool CTspan::ReadFromXmlNode(XmlUtils::CXmlNode &oNode, const CGeneralStyle& oBaseStyle) + void CTspan::SetData(const std::map &mAttributes, unsigned short ushLevel, bool bHardMode) + { + + } + + bool CTspan::ReadFromXmlNode(XmlUtils::CXmlNode &oNode) { double dX = oNode.GetAttributeDouble(L"x", MININT8); double dY = oNode.GetAttributeDouble(L"y", MININT8); + SaveNodeData(oNode); + if (dX != MININT8) m_oCoord.dX = dX; else if (NULL != m_pParent) diff --git a/DesktopEditor/raster/Metafile/svg/SvgObjects/CText.h b/DesktopEditor/raster/Metafile/svg/SvgObjects/CText.h index 6effff32c2..aa9888a450 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgObjects/CText.h +++ b/DesktopEditor/raster/Metafile/svg/SvgObjects/CText.h @@ -16,20 +16,28 @@ namespace SVG CText(CObjectBase* pParent = NULL, NSFonts::IFontManager* pFontManager = NULL); virtual ~CText(); - bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode, const CGeneralStyle& oBaseStyle) override; + void SetData(const std::map& mAttributes, unsigned short ushLevel, bool bHardMode = false) override; + + bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode) override; bool Draw(IRenderer* pRenderer) const override; private: void ApplyStyle(IRenderer* pRenderer, int& nTypePath) const override; + void ApplyFont(IRenderer* pRenderer) const; + double GetWidth() const; NSFonts::IFontManager* m_pFontManager; -// Point m_oCoord; + SvgDigit m_oX; + SvgDigit m_oY; + std::wstring m_wsText; std::vector m_arChildrens; + SvgFont m_oFont; + friend class CTspan; }; @@ -39,9 +47,11 @@ namespace SVG CTspan(CObjectBase* pParent); virtual ~CTspan(); + void SetData(const std::map& mAttributes, unsigned short ushLevel, bool bHardMode = false) override; + double GetWidth() const; - bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode, const CGeneralStyle& oBaseStyle) override; + bool ReadFromXmlNode(XmlUtils::CXmlNode& oNode) override; bool Draw(IRenderer* pRenderer) const override; private: void ApplyStyle(IRenderer* pRenderer, int& nTypePath) const override; diff --git a/DesktopEditor/raster/Metafile/svg/SvgTypes.h b/DesktopEditor/raster/Metafile/svg/SvgTypes.h index bc2730d4f8..0749106f25 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgTypes.h +++ b/DesktopEditor/raster/Metafile/svg/SvgTypes.h @@ -4,8 +4,25 @@ #include #include +#include "../../../Common/3dParty/html/css/src/StyleProperties.h" + namespace SVG { + #define MapCI std::map::const_iterator + + #define SvgDigit NSCSS::NSProperties::CDigit + #define SvgString NSCSS::NSProperties::CString + #define SvgColor NSCSS::NSProperties::CColor + #define SvgMatrix NSCSS::NSProperties::CMatrix + + #define SvgFont NSCSS::NSProperties::CFont + + struct CStroke + { + SvgColor m_oColor; + SvgDigit m_oWidth; + }; + struct Point { double dX; diff --git a/DesktopEditor/raster/Metafile/svg/SvgUtils.h b/DesktopEditor/raster/Metafile/svg/SvgUtils.h index 9286565135..4d22f74a8f 100644 --- a/DesktopEditor/raster/Metafile/svg/SvgUtils.h +++ b/DesktopEditor/raster/Metafile/svg/SvgUtils.h @@ -49,7 +49,6 @@ namespace SVG } return arValues; - } static inline long LongValue(const std::wstring& value)