From b78ad9e751021712c51972afd8501d7f58c3fbd7 Mon Sep 17 00:00:00 2001 From: Oleg Korshul Date: Tue, 9 Aug 2022 21:52:48 +0300 Subject: [PATCH] Fix bugs --- .../graphics/pro/js/qt/raster/main.cpp | 23 ++++++- .../graphics/pro/js/qt/raster/raster.cpp | 2 +- .../raster/Metafile/Common/MetaFile.h | 2 + .../EmfInterpretator/CEmfInterpretatorSvg.cpp | 5 +- .../WmfInterpretator/CWmfInterpretatorSvg.cpp | 13 ++-- DesktopEditor/raster/Metafile/Wmf/WmfTypes.h | 7 +- OfficeUtils/js/wasm/js/zlib.js | 69 +++++++++++++++++++ 7 files changed, 110 insertions(+), 11 deletions(-) diff --git a/DesktopEditor/graphics/pro/js/qt/raster/main.cpp b/DesktopEditor/graphics/pro/js/qt/raster/main.cpp index af469bef82..ff6848a10b 100644 --- a/DesktopEditor/graphics/pro/js/qt/raster/main.cpp +++ b/DesktopEditor/graphics/pro/js/qt/raster/main.cpp @@ -16,7 +16,7 @@ int main() } } - if (true) + if (false) { std::wstring sFilePath = L"D:/1.emf"; MetaFile::CMetaFile oFrame(NULL); @@ -28,5 +28,26 @@ int main() return 0; } } + + if (true) + { + BYTE* pFileBuffer = NULL; + DWORD nFileSize = 0; + NSFile::CFileBinary::ReadAllBytes(L"D:/image1.wmf", &pFileBuffer, nFileSize); + + void* pEncodedData = Raster_Encode(pFileBuffer, (int)nFileSize, 24); + int nEncodedSize = Raster_GetEncodedSize(pEncodedData); + BYTE* pEncodedBuffer = (BYTE*)Raster_GetEncodedBuffer(pEncodedData); + + NSFile::CFileBinary oFile; + oFile.CreateFileW(L"D:/123.svg"); + oFile.WriteFile(pEncodedBuffer, nEncodedSize); + oFile.CloseFile(); + + Raster_DestroyEncodedData(pEncodedData); + + RELEASEARRAYOBJECTS(pFileBuffer); + } + return 1; } diff --git a/DesktopEditor/graphics/pro/js/qt/raster/raster.cpp b/DesktopEditor/graphics/pro/js/qt/raster/raster.cpp index 7de8f944b1..c2579544b3 100644 --- a/DesktopEditor/graphics/pro/js/qt/raster/raster.cpp +++ b/DesktopEditor/graphics/pro/js/qt/raster/raster.cpp @@ -74,7 +74,7 @@ void* Raster_EncodeImageData(unsigned char* buffer, int w, int h, int stride, in void* Raster_Encode(unsigned char* buffer, int size, int format) { CImageFileFormatChecker oChecker; - bool bIsImageFile = oChecker.isImageFile(buffer, (DWORD)format); + bool bIsImageFile = oChecker.isImageFile(buffer, (DWORD)size); if (bIsImageFile) { diff --git a/DesktopEditor/raster/Metafile/Common/MetaFile.h b/DesktopEditor/raster/Metafile/Common/MetaFile.h index 403160ecfd..d1adcb1332 100644 --- a/DesktopEditor/raster/Metafile/Common/MetaFile.h +++ b/DesktopEditor/raster/Metafile/Common/MetaFile.h @@ -90,6 +90,8 @@ namespace MetaFile m_pBufferData = pBuffer; m_bIsExternalBuffer = bIsExternal; m_oStream.SetStream(pBuffer, unSize); + + return true; } bool OpenFromFile(const wchar_t* wsFilePath) { diff --git a/DesktopEditor/raster/Metafile/Emf/EmfInterpretator/CEmfInterpretatorSvg.cpp b/DesktopEditor/raster/Metafile/Emf/EmfInterpretator/CEmfInterpretatorSvg.cpp index 9254b44cb8..bb8e5eed49 100644 --- a/DesktopEditor/raster/Metafile/Emf/EmfInterpretator/CEmfInterpretatorSvg.cpp +++ b/DesktopEditor/raster/Metafile/Emf/EmfInterpretator/CEmfInterpretatorSvg.cpp @@ -890,8 +890,9 @@ namespace MetaFile if (!m_oViewport.Empty()) m_sOutputData.insert(5, L"viewBox=\"" + std::to_wstring(m_oViewport.dLeft) + L' ' + std::to_wstring(m_oViewport.dTop) + L' ' + std::to_wstring(m_oViewport.GetWidth()) + L' ' + std::to_wstring(m_oViewport.GetHeight()) + L"\" "); - if (0 != m_oSizeWindow.cx && 0 != m_oSizeWindow.cy) - m_sOutputData.insert(5, L"width=\"" + std::to_wstring(m_oSizeWindow.cx) + L"\" height=\"" + std::to_wstring(m_oSizeWindow.cy) + L"\" "); + int nSizeW = (m_oSizeWindow.cx == 0) ? ((int)m_oViewport.GetWidth()) : m_oSizeWindow.cx; + int nSizeH = (m_oSizeWindow.cy == 0) ? ((int)m_oViewport.GetHeight()) : m_oSizeWindow.cy; + m_sOutputData.insert(5, L"width=\"" + std::to_wstring(nSizeW) + L"\" height=\"" + std::to_wstring(nSizeH) + L"\" "); // m_oXmlWriter.SetXmlString(wsXml); diff --git a/DesktopEditor/raster/Metafile/Wmf/WmfInterpretator/CWmfInterpretatorSvg.cpp b/DesktopEditor/raster/Metafile/Wmf/WmfInterpretator/CWmfInterpretatorSvg.cpp index ff72959100..a6ca3704bb 100644 --- a/DesktopEditor/raster/Metafile/Wmf/WmfInterpretator/CWmfInterpretatorSvg.cpp +++ b/DesktopEditor/raster/Metafile/Wmf/WmfInterpretator/CWmfInterpretatorSvg.cpp @@ -292,9 +292,9 @@ namespace MetaFile TRectD oNewRect; oNewRect.dLeft = TranslateX(shL); - oNewRect.dTop = TranslateX(shT); + oNewRect.dTop = TranslateY(shT); oNewRect.dRight = TranslateX(shR); - oNewRect.dBottom = TranslateX(shB); + oNewRect.dBottom = TranslateY(shB); NodeAttributes arAttributes = {{L"x", std::to_wstring(oNewRect.dLeft)}, {L"y", std::to_wstring(oNewRect.dTop)}, @@ -314,9 +314,9 @@ namespace MetaFile TRectD oNewRect; oNewRect.dLeft = TranslateX(shL); - oNewRect.dTop = TranslateX(shT); + oNewRect.dTop = TranslateY(shT); oNewRect.dRight = TranslateX(shR); - oNewRect.dBottom = TranslateX(shB); + oNewRect.dBottom = TranslateY(shB); NodeAttributes arAttributes = {{L"x", std::to_wstring(oNewRect.dLeft)}, @@ -630,8 +630,9 @@ namespace MetaFile if (!m_oViewport.Empty()) m_sOutputData.insert(5, L"viewBox=\"" + std::to_wstring(m_oViewport.dLeft) + L' ' + std::to_wstring(m_oViewport.dTop) + L' ' + std::to_wstring(m_oViewport.GetWidth()) + L' ' + std::to_wstring(m_oViewport.GetHeight()) + L"\" "); - if (0 != m_oSizeWindow.cx && 0 != m_oSizeWindow.cy) - m_sOutputData.insert(5, L"width=\"" + std::to_wstring(m_oSizeWindow.cx) + L"\" height=\"" + std::to_wstring(m_oSizeWindow.cy) + L"\" "); + int nSizeW = (m_oSizeWindow.cx == 0) ? ((int)m_oViewport.GetWidth()) : m_oSizeWindow.cx; + int nSizeH = (m_oSizeWindow.cy == 0) ? ((int)m_oViewport.GetHeight()) : m_oSizeWindow.cy; + m_sOutputData.insert(5, L"width=\"" + std::to_wstring(nSizeW) + L"\" height=\"" + std::to_wstring(nSizeH) + L"\" "); // m_oXmlWriter.SetXmlString(wsXml); // m_oXmlWriter.SaveToFile((!m_wsSvgFilePath.empty()) ? m_wsSvgFilePath : L"temp.svg"); diff --git a/DesktopEditor/raster/Metafile/Wmf/WmfTypes.h b/DesktopEditor/raster/Metafile/Wmf/WmfTypes.h index 4e69561488..7f3b56dc97 100644 --- a/DesktopEditor/raster/Metafile/Wmf/WmfTypes.h +++ b/DesktopEditor/raster/Metafile/Wmf/WmfTypes.h @@ -184,9 +184,14 @@ namespace MetaFile short Right; short Bottom; + TWmfRect() + { + Left = Top = Right = Bottom = 0; + } + bool Empty() const { - return 0 == Left == Top == Right == Bottom; + return (0 == Left) && (0 == Top) && (0 == Right) && (0 == Bottom); } }; struct TWmfPlaceable diff --git a/OfficeUtils/js/wasm/js/zlib.js b/OfficeUtils/js/wasm/js/zlib.js index ea2f2f14f5..2978dd297a 100644 --- a/OfficeUtils/js/wasm/js/zlib.js +++ b/OfficeUtils/js/wasm/js/zlib.js @@ -313,6 +313,75 @@ this.engine = 0; }; + /** + * Get image type + * @returns {Number} + */ + ZLib.prototype.getImageType = function(path) + { + let fileData = this.getFile(path); + return Module["_Image_GetFormat"](this.files[path].p + 4, fileData.length); + }; + + /** + * Get image in needed format + * @returns {Uint8Array} + */ + ZLib.prototype.getImageAsFormat = function(path, format) + { + let fileData = this.getFile(path); + let encodedData = Module["_Raster_Encode"](this.files[path].p + 4, fileData.length, format); + let encodedSize = Module["_Raster_GetEncodedSize"](encodedData); + let encodedBuffer = Module["_Raster_GetEncodedBuffer"](encodedData); + + let copyData = new Uint8Array(encodedSize); + copyData.set(new Uint8Array(Module["HEAP8"].buffer, encodedBuffer, encodedSize)); + + Module["_Raster_DestroyEncodedData"](encodedData); + + return copyData; + }; + /** + * Get image as svg (for simple test) + * @returns {string} + */ + ZLib.prototype.getImageAsSvg = function(path) + { + let fileData = this.getFile(path); + let encodedData = Module["_Raster_Encode"](this.files[path].p + 4, fileData.length, 24); + let encodedSize = Module["_Raster_GetEncodedSize"](encodedData); + let encodedBuffer = Module["_Raster_GetEncodedBuffer"](encodedData); + + let string = String.prototype.fromUtf8(new Uint8Array(Module["HEAP8"].buffer, encodedBuffer, encodedSize)); + + Module["_Raster_DestroyEncodedData"](encodedData); + + return string; + }; + /** + * Get image blob for browser + * @returns {Blob} + */ + ZLib.prototype.getImageBlob = function(path) + { + let imageType = this.getImageType(path); + if (imageType != 10 && imageType != 21) + { + return new Blob([this.getFile(path)], {type:AscCommon.openXml.GetMimeType(AscCommon.GetFileExtension(path))}); + } + + let fileData = this.getFile(path); + let encodedData = Module["_Raster_Encode"](this.files[path].p + 4, fileData.length, 24); + let encodedSize = Module["_Raster_GetEncodedSize"](encodedData); + let encodedBuffer = Module["_Raster_GetEncodedBuffer"](encodedData); + + let blob = new Blob([new Uint8Array(Module["HEAP8"].buffer, encodedBuffer, encodedSize)], {type : AscCommon.openXml.GetMimeType("svg")}); + + Module["_Raster_DestroyEncodedData"](encodedData); + + return blob; + }; + window.AscCommon = window.AscCommon || {}; window.AscCommon.CZLibEngineJS = ZLib; window.onZlibEngineInit = function()