From a9cb8de19723d484f6f858d49ffd19268f014a61 Mon Sep 17 00:00:00 2001 From: Oleg Korshul Date: Thu, 20 Jan 2022 20:31:00 +0300 Subject: [PATCH] Refacrtoring wasm module --- .../pro/js/wasm/js/drawingfile_base.js | 2 +- .../graphics/pro/js/wasm/src/drawingfile.h | 13 ++++++- .../graphics/pro/officedrawingfile.cpp | 37 +++++++++++++------ .../graphics/pro/officedrawingfile.h | 4 +- HtmlRenderer/include/HTMLRendererText.h | 2 +- HtmlRenderer/src/HTMLRendererText.cpp | 4 +- HtmlRenderer/src/Text.h | 16 ++++++-- PdfReader/Src/RendererOutputDev.cpp | 2 + 8 files changed, 56 insertions(+), 24 deletions(-) diff --git a/DesktopEditor/graphics/pro/js/wasm/js/drawingfile_base.js b/DesktopEditor/graphics/pro/js/wasm/js/drawingfile_base.js index 93f5fd89cc..b2e1910263 100644 --- a/DesktopEditor/graphics/pro/js/wasm/js/drawingfile_base.js +++ b/DesktopEditor/graphics/pro/js/wasm/js/drawingfile_base.js @@ -190,7 +190,7 @@ "H" : _buffer[_cur++], "Dpi" : _buffer[_cur++], fonts : [], - textCommands : null + text : null }); } diff --git a/DesktopEditor/graphics/pro/js/wasm/src/drawingfile.h b/DesktopEditor/graphics/pro/js/wasm/src/drawingfile.h index 800c3c7d92..642df09897 100644 --- a/DesktopEditor/graphics/pro/js/wasm/src/drawingfile.h +++ b/DesktopEditor/graphics/pro/js/wasm/src/drawingfile.h @@ -14,6 +14,7 @@ class CGraphicsFileDrawing private: IOfficeDrawingFile* pReader; NSFonts::IApplicationFonts* pApplicationFonts; + NSFonts::IFontManager* pFontManager; NSHtmlRenderer::CHTMLRendererText* pTextRenderer; int nType; public: @@ -23,12 +24,20 @@ public: pTextRenderer = NULL; pApplicationFonts = pFonts; pApplicationFonts->AddRef(); + + pFontManager = pApplicationFonts->GenerateFontManager(); + NSFonts::IFontsCache* pFontCache = NSFonts::NSFontCache::Create(); + pFontCache->SetStreams(pApplicationFonts->GetStreams()); + pFontCache->SetCacheSize(8); + pFontManager->SetOwnerCache(pFontCache); + nType = -1; } ~CGraphicsFileDrawing() { RELEASEOBJECT(pReader); RELEASEOBJECT(pTextRenderer); + RELEASEOBJECT(pFontManager); RELEASEINTERFACE(pApplicationFonts); nType = -1; } @@ -81,14 +90,14 @@ public: } BYTE* GetPage(int nPageIndex, int nRasterW, int nRasterH) { - return pReader->ConvertToPixels(nPageIndex, nRasterW, nRasterH, true); + return pReader->ConvertToPixels(nPageIndex, nRasterW, nRasterH, true, pFontManager); } BYTE* GetGlyphs(int nPageIndex) { if (NULL == pTextRenderer) pTextRenderer = new NSHtmlRenderer::CHTMLRendererText(); - pTextRenderer->Init(pReader); + pTextRenderer->Init(pReader, 8); pReader->DrawPageOnRenderer(pTextRenderer, nPageIndex, NULL); return pTextRenderer->GetBuffer(); diff --git a/DesktopEditor/graphics/pro/officedrawingfile.cpp b/DesktopEditor/graphics/pro/officedrawingfile.cpp index a61d50b856..e3458371c0 100644 --- a/DesktopEditor/graphics/pro/officedrawingfile.cpp +++ b/DesktopEditor/graphics/pro/officedrawingfile.cpp @@ -33,16 +33,25 @@ #include "./officedrawingfile.h" #include "./Graphics.h" -CBgraFrame* GetFrame(IOfficeDrawingFile* pFile, int nPageIndex, int nRasterW, int nRasterH, bool bIsFlip, bool bIsSwapRGB) +CBgraFrame* GetFrame(IOfficeDrawingFile* pFile, int nPageIndex, int nRasterW, int nRasterH, bool bIsFlip, bool bIsSwapRGB, NSFonts::IFontManager* pFonts = NULL) { - NSFonts::IApplicationFonts* pApplicationFonts = pFile->GetFonts(); - if (!pApplicationFonts) - return NULL; + NSFonts::IFontManager *pFontManager = pFonts; - NSFonts::IFontManager *pFontManager = pApplicationFonts->GenerateFontManager(); - NSFonts::IFontsCache* pFontCache = NSFonts::NSFontCache::Create(); - pFontCache->SetStreams(pApplicationFonts->GetStreams()); - pFontManager->SetOwnerCache(pFontCache); + if (!pFontManager) + { + NSFonts::IApplicationFonts* pApplicationFonts = pFile->GetFonts(); + if (!pApplicationFonts) + return NULL; + + NSFonts::IFontManager *pFontManager = pApplicationFonts->GenerateFontManager(); + NSFonts::IFontsCache* pFontCache = NSFonts::NSFontCache::Create(); + pFontCache->SetStreams(pApplicationFonts->GetStreams()); + pFontManager->SetOwnerCache(pFontCache); + } + else + { + pFontManager->AddRef(); + } NSGraphics::IGraphicsRenderer* pRenderer = NSGraphics::Create(); pRenderer->SetFontManager(pFontManager); @@ -56,7 +65,11 @@ CBgraFrame* GetFrame(IOfficeDrawingFile* pFile, int nPageIndex, int nRasterW, in BYTE* pBgraData = new BYTE[nWidth * nHeight * 4]; if (!pBgraData) + { + RELEASEINTERFACE(pFontManager); + RELEASEOBJECT(pRenderer); return NULL; + } memset(pBgraData, 0xff, nWidth * nHeight * 4); CBgraFrame* pFrame = new CBgraFrame(); @@ -86,9 +99,9 @@ CBgraFrame* GetFrame(IOfficeDrawingFile* pFile, int nPageIndex, int nRasterW, in return pFrame; } -unsigned char* IOfficeDrawingFile::ConvertToPixels(int nPageIndex, int nRasterW, int nRasterH, bool bIsFlip) +unsigned char* IOfficeDrawingFile::ConvertToPixels(int nPageIndex, int nRasterW, int nRasterH, bool bIsFlip, NSFonts::IFontManager* pFonts) { - CBgraFrame* pFrame = GetFrame(this, nPageIndex, nRasterW, nRasterH, bIsFlip, true); + CBgraFrame* pFrame = GetFrame(this, nPageIndex, nRasterW, nRasterH, bIsFlip, true, pFonts); if (!pFrame) return NULL; @@ -99,9 +112,9 @@ unsigned char* IOfficeDrawingFile::ConvertToPixels(int nPageIndex, int nRasterW, return pData; } -void IOfficeDrawingFile::ConvertToRaster(int nPageIndex, const std::wstring& path, int nImageType, const int nRasterW, const int nRasterH, bool bIsFlip) +void IOfficeDrawingFile::ConvertToRaster(int nPageIndex, const std::wstring& path, int nImageType, const int nRasterW, const int nRasterH, bool bIsFlip, NSFonts::IFontManager* pFonts) { - CBgraFrame* pFrame = GetFrame(this, nPageIndex, nRasterW, nRasterH, bIsFlip, false); + CBgraFrame* pFrame = GetFrame(this, nPageIndex, nRasterW, nRasterH, bIsFlip, false, pFonts); if (!pFrame) return; diff --git a/DesktopEditor/graphics/pro/officedrawingfile.h b/DesktopEditor/graphics/pro/officedrawingfile.h index 7ad061e927..165298617e 100644 --- a/DesktopEditor/graphics/pro/officedrawingfile.h +++ b/DesktopEditor/graphics/pro/officedrawingfile.h @@ -73,8 +73,8 @@ public: virtual void DrawPageOnRenderer(IRenderer* pRenderer, int nPageIndex, bool* pBreak) = 0; // Common methods/wrappers on GetPageInfo + DrawPageOnRenderer - virtual unsigned char* ConvertToPixels(int nPageIndex, int nRasterW, int nRasterH, bool bIsFlip = false); - virtual void ConvertToRaster(int nPageIndex, const std::wstring& path, int nImageType, const int nRasterW = -1, const int nRasterH = -1, bool bIsFlip = false); + virtual unsigned char* ConvertToPixels(int nPageIndex, int nRasterW, int nRasterH, bool bIsFlip = false, NSFonts::IFontManager* pFonts = NULL); + virtual void ConvertToRaster(int nPageIndex, const std::wstring& path, int nImageType, const int nRasterW = -1, const int nRasterH = -1, bool bIsFlip = false, NSFonts::IFontManager* pFonts = NULL); // Common methods for viewer #ifdef BUILDING_WASM_MODULE diff --git a/HtmlRenderer/include/HTMLRendererText.h b/HtmlRenderer/include/HTMLRendererText.h index e9c62a5d4a..ed253878e2 100644 --- a/HtmlRenderer/include/HTMLRendererText.h +++ b/HtmlRenderer/include/HTMLRendererText.h @@ -52,7 +52,7 @@ namespace NSHtmlRenderer virtual ~CHTMLRendererText(); public: - void Init(IOfficeDrawingFile* pFile); + void Init(IOfficeDrawingFile* pFile, int nCacheSize = 0); BYTE* GetBuffer(); public: diff --git a/HtmlRenderer/src/HTMLRendererText.cpp b/HtmlRenderer/src/HTMLRendererText.cpp index 1f5ec7722f..872b62f847 100644 --- a/HtmlRenderer/src/HTMLRendererText.cpp +++ b/HtmlRenderer/src/HTMLRendererText.cpp @@ -147,7 +147,7 @@ namespace NSHtmlRenderer RELEASEOBJECT(m_pInternal); } - void CHTMLRendererText::Init(IOfficeDrawingFile* pFile) + void CHTMLRendererText::Init(IOfficeDrawingFile* pFile, int nCacheSize) { m_pInternal->m_oBrush.SetDefaultParams(); m_pInternal->m_oLastBrush.SetDefaultParams(); @@ -207,7 +207,7 @@ namespace NSHtmlRenderer } m_pInternal->m_bIsFontsInit = true; - m_pInternal->m_oSmartText.Init(pFile->GetFonts()); + m_pInternal->m_oSmartText.Init(pFile->GetFonts(), nCacheSize); } m_pInternal->m_oPage.ClearNoAttack(); diff --git a/HtmlRenderer/src/Text.h b/HtmlRenderer/src/Text.h index 9024455483..680453f8f0 100644 --- a/HtmlRenderer/src/Text.h +++ b/HtmlRenderer/src/Text.h @@ -320,10 +320,18 @@ namespace NSHtmlRenderer { m_pManager = NULL; } - void Init(NSFonts::IApplicationFonts* pApplicationFonts) + void Init(NSFonts::IApplicationFonts* pApplicationFonts, int nCacheSize = 0) { RELEASEOBJECT(m_pManager); m_pManager = pApplicationFonts->GenerateFontManager(); + + if (0 != nCacheSize) + { + NSFonts::IFontsCache* pFontCache = NSFonts::NSFontCache::Create(); + pFontCache->SetStreams(pApplicationFonts->GetStreams()); + pFontCache->SetCacheSize(nCacheSize); + m_pManager->SetOwnerCache(pFontCache); + } } virtual ~CFontManagerWrapper() @@ -491,9 +499,9 @@ namespace NSHtmlRenderer m_lCountSymbols = 0; m_lCountSpaces = 0; } - void Init(NSFonts::IApplicationFonts* pApplicationFonts) + void Init(NSFonts::IApplicationFonts* pApplicationFonts, int nCacheSize = 0) { - m_oFontManager.Init(pApplicationFonts); + m_oFontManager.Init(pApplicationFonts, nCacheSize); } template @@ -735,7 +743,7 @@ namespace NSHtmlRenderer LONG lTextLen = nCount; bool bIsLoadFontAttack = true; - // говенные значения приходят из пдф + // плохие значения приходят из пдф /* if (1 == lTextLen && 0 <= width) bIsLoadFontAttack = false; diff --git a/PdfReader/Src/RendererOutputDev.cpp b/PdfReader/Src/RendererOutputDev.cpp index 411acf713c..93b21ecb80 100644 --- a/PdfReader/Src/RendererOutputDev.cpp +++ b/PdfReader/Src/RendererOutputDev.cpp @@ -579,6 +579,8 @@ namespace PdfReader if (c_nHtmlRendrerer2 == m_lRendererType) m_bDrawOnlyText = (S_OK == m_pRenderer->CommandLong(c_nCommandLongTypeOnlyText, 0)) ? true : false; + else if (c_nHtmlRendrererText == m_lRendererType) + m_bDrawOnlyText = true; else m_bDrawOnlyText = false; }