From cef8ac9fd97b6a0aaada58dcb145db3fb39e1ac2 Mon Sep 17 00:00:00 2001 From: Oleg Korshul Date: Thu, 2 Jan 2025 22:36:18 +0300 Subject: [PATCH] Remove depends to CSvgFile from HtmlFile2. Add initialize fontsfor svg. --- DesktopEditor/graphics/pro/Image.h | 2 ++ DesktopEditor/raster/Metafile/MetaFile.cpp | 31 ++++++++++++++++++++++ DesktopEditor/raster/Metafile/MetaFile.h | 3 +++ HtmlFile2/htmlfile2.cpp | 31 +++++++++------------- 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/DesktopEditor/graphics/pro/Image.h b/DesktopEditor/graphics/pro/Image.h index 9bb1f8d7764..313ee4f51ee 100644 --- a/DesktopEditor/graphics/pro/Image.h +++ b/DesktopEditor/graphics/pro/Image.h @@ -123,6 +123,7 @@ namespace MetaFile virtual bool LoadFromFile(const wchar_t* wsFilePath) = 0; virtual bool LoadFromBuffer(BYTE* pBuffer, unsigned int unSize) = 0; + virtual bool LoadFromString(const std::wstring& data) = 0; virtual bool DrawOnRenderer(IRenderer* pRenderer, double dX, double dY, double dWidth, double dHeight) = 0; virtual void Close() = 0; virtual void GetBounds(double* pdX, double* pdY, double* pdW, double* pdH) = 0; @@ -131,6 +132,7 @@ namespace MetaFile virtual NSFonts::IFontManager* get_FontManager() = 0; virtual std::wstring ConvertToSvg(unsigned int unWidth = 0, unsigned int unHeight = 0) = 0; + virtual void SetTempDirectory(const std::wstring& dir) = 0; //Для тестов #ifdef METAFILE_SUPPORT_WMF_EMF diff --git a/DesktopEditor/raster/Metafile/MetaFile.cpp b/DesktopEditor/raster/Metafile/MetaFile.cpp index 2ab5f6e9dfa..d154f4cb230 100644 --- a/DesktopEditor/raster/Metafile/MetaFile.cpp +++ b/DesktopEditor/raster/Metafile/MetaFile.cpp @@ -448,6 +448,37 @@ namespace MetaFile return false; } + bool CMetaFile::LoadFromString(const std::wstring& data) + { +#ifdef METAFILE_SUPPORT_SVG + RELEASEINTERFACE(m_pFontManager); + + if (m_pAppFonts) + { + m_pFontManager = m_pAppFonts->GenerateFontManager(); + NSFonts::IFontsCache* pMeasurerCache = NSFonts::NSFontCache::Create(); + pMeasurerCache->SetStreams(m_pAppFonts->GetStreams()); + m_pFontManager->SetOwnerCache(pMeasurerCache); + } + + m_oSvgFile.SetFontManager(m_pFontManager); + + if (m_oSvgFile.ReadFromWString(data) == true) + { + m_lType = c_lMetaSvg; + return true; + } +#endif + return false; + } + + void CMetaFile::SetTempDirectory(const std::wstring& dir) + { +#ifdef METAFILE_SUPPORT_SVG + m_oSvgFile.SetWorkingDirectory(dir); +#endif + } + bool CMetaFile::DrawOnRenderer(IRenderer* pRenderer, double dX, double dY, double dWidth, double dHeight) { if (NULL == pRenderer) diff --git a/DesktopEditor/raster/Metafile/MetaFile.h b/DesktopEditor/raster/Metafile/MetaFile.h index 74738c1251f..018e0d1f199 100644 --- a/DesktopEditor/raster/Metafile/MetaFile.h +++ b/DesktopEditor/raster/Metafile/MetaFile.h @@ -64,6 +64,7 @@ namespace MetaFile bool LoadFromFile(const wchar_t* wsFilePath); bool LoadFromBuffer(BYTE* pBuffer, unsigned int unSize); + bool LoadFromString(const std::wstring& data); bool DrawOnRenderer(IRenderer* pRenderer, double dX, double dY, double dWidth, double dHeight); void Close(); void GetBounds(double* pdX, double* pdY, double* pdW, double* pdH); @@ -75,6 +76,8 @@ namespace MetaFile //конвертация в Svg std::wstring ConvertToSvg(unsigned int unWidth = 0, unsigned int unHeight = 0); + void SetTempDirectory(const std::wstring& dir); + //Для тестов #ifdef METAFILE_SUPPORT_WMF_EMF void ConvertToXml(const wchar_t *wsFilePath); diff --git a/HtmlFile2/htmlfile2.cpp b/HtmlFile2/htmlfile2.cpp index 9f48c4a0c1f..1fd8b087a11 100644 --- a/HtmlFile2/htmlfile2.cpp +++ b/HtmlFile2/htmlfile2.cpp @@ -24,7 +24,6 @@ #include "../DesktopEditor/xml/include/xmlutils.h" #include "../DesktopEditor/raster/BgraFrame.h" #include "../DesktopEditor/graphics/pro/Graphics.h" -#include "../DesktopEditor/raster/Metafile/svg/CSvgFile.h" #include "htmlfile2.h" #include "src/Languages.h" @@ -4534,29 +4533,22 @@ class CHtmlFile2_Private if (wsSvg.empty()) return false; - CSvgFile oSvgReader; - NSFonts::IApplicationFonts* pFonts = NSFonts::NSApplication::Create(); - NSFonts::IFontManager* pFontManager = pFonts->GenerateFontManager(); - NSFonts::IFontsCache* pFontCache = NSFonts::NSFontCache::Create(); - - pFontCache->SetStreams(pFonts->GetStreams()); - pFontManager->SetOwnerCache(pFontCache); - - oSvgReader.SetFontManager(pFontManager); + pFonts->Initialize(); - if (!oSvgReader.ReadFromWString(wsSvg)) + MetaFile::IMetaFile* pSvgReader = MetaFile::Create(pFonts); + if (!pSvgReader->LoadFromString(wsSvg)) { - RELEASEINTERFACE(pFontManager); - pFonts->Release(); + RELEASEINTERFACE(pSvgReader); + RELEASEINTERFACE(pFonts); return false; } NSGraphics::IGraphicsRenderer* pGrRenderer = NSGraphics::Create(); - pGrRenderer->SetFontManager(pFontManager); + pGrRenderer->SetFontManager(pSvgReader->get_FontManager()); double dX, dY, dW, dH; - oSvgReader.GetBounds(dX, dY, dW, dH); + pSvgReader->GetBounds(&dX, &dY, &dW, &dH); if (dW < 0) dW = -dW; if (dH < 0) dH = -dH; @@ -4615,19 +4607,20 @@ class CHtmlFile2_Private pGrRenderer->put_Width(dWidth); pGrRenderer->put_Height(dHeight); - oSvgReader.SetWorkingDirectory(m_sSrc); - oSvgReader.Draw(pGrRenderer, 0, 0, dWidth, dHeight); + // TODO: src directory as tmp - it's not good idea + pSvgReader->SetTempDirectory(m_sSrc); + pSvgReader->DrawOnRenderer(pGrRenderer, 0, 0, dWidth, dHeight); oFrame.SaveFile(m_sDst + L"/word/media/i" + std::to_wstring(m_arrImages.size()) + L".png", 4); oFrame.put_Data(NULL); - RELEASEINTERFACE(pFontManager); RELEASEINTERFACE(pGrRenderer); if (pBgraData) free(pBgraData); - pFonts->Release(); + RELEASEINTERFACE(pSvgReader); + RELEASEINTERFACE(pFonts); return true; }