/** * @file XTPSvgImage.h * * @copyright * (c) 1998-2025 Codejock Software, All Rights Reserved. * * This source file is the property of Codejock Software and must not be * redistributed by any means without the explicit written permission of * Codejock Software. * * The use of this source code is governed by the terms and conditions specified * in the Toolkit Pro license agreement. Codejock Software grants you, as a * single software developer, the limited right to use this software on one * computer only. * * Contact Information: * support@codejock.com * http://www.codejock.com * */ /** @cond */ #if !defined(__XTPSVGIMAGE_H__) # define __XTPSVGIMAGE_H__ /** @endcond */ # if _MSC_VER > 1000 # pragma once # endif // _MSC_VER > 1000 # include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" /** @cond */ struct NSVGimage; struct NSVGrasterizer; /** @endcond */ /** * @brief * Implements basic SVG image format support. */ class _XTP_EXT_CLASS CXTPSvgImage { public: /** * @brief * Constructs an uninitialized SVG image. */ CXTPSvgImage(); /** * @brief * Handles object desctruction. */ ~CXTPSvgImage(); public: /** * Defines image units */ enum Units { unitPX, /**< Pixel units */ unitPT, /**< Point units */ unitPC, /**< Picas units */ unitMM, /**< Millimeter units */ unitCM, /**< Centimeter units */ unitIN, /**< Inch units */ unitPercent, /**< Persent units */ unitEM, /**< Relative to the font size of the current element */ unitEX /**< Relative to the x-height of Helvetica font */ }; /** * @brief * Loads SVG data from the specified memory location. * @param pData Pointer to SVG memory buffer start. * @param cbSize Size of SVG memory buffer. * @param nUnits Image units to be used by default. * @return * TRUE if successful, otherwise FALSE. */ BOOL Load(const void* pData, SIZE_T cbSize, Units nUnits = unitPX); /** * @brief * Loads SVG data from the specified stream. * @param pStream Pointer to a stream object to load SVG data from. The data will be loaded from * the current stream read position. nUnits Image units to be used by default. * @param nUnits Image units to be used by default. * @return * TRUE if successful, otherwise FALSE. */ BOOL Load(IStream* pStream, Units nUnits = unitPX); /** * @brief * Loads SVG data from the specified file. * @param pFile Pointer to a file object to load SVG data from. The data will be loaded from * the current file read position. nUnits Image units to be used by default. * @param nUnits Image units to be used by default. * @return * TRUE if successful, otherwise FALSE. */ BOOL Load(CFile* pFile, Units nUnits = unitPX); /** * @brief * Loads SVG data from the specified file. * @param lpFilePath File path string. * @param nUnits Image units to be used by default. * @return * TRUE if successful, otherwise FALSE. */ BOOL Load(LPCTSTR lpFilePath, Units nUnits = unitPX); /** * @brief * Loads SVG data from the specified resource. * @param hModule Source module handle. * @param hRes Source resource handle. * @param nUnits Image units to be used by default. * @return * TRUE if successful, otherwise FALSE. */ BOOL Load(HMODULE hModule, HRSRC hRes, Units nUnits = unitPX); /** * @brief * Determines if SVG data has been successfuly loaded. * @return * TRUE if SVG data has been loaded and available, FALSE otherwise. */ BOOL IsLoaded() const; /** * @brief * Determines if the loaded .SVG file has alpha layer. * @return * TRUE if the .SVG file has alpha layer. */ BOOL IsAlpha() const; /** * @brief * Obtains bitmap object that contains a rasterized SVG image. * @return * A bitmap object pointer or NULL if an error occurrs. * @details * The first call after changing SVG size or loading a new SVG image * results in an expensive rasterization procedure. */ const CBitmap* GetBitmap() const; /** * @brief * Gets rasterized SVG image size. * @return * The current rasterized image size. * @details * Changing rasterized image size resets the cached rasterized bitmap * bitmap which we'll cause an expesive image rasterization procedure * the next time when image data is required. */ CSize GetSize() const; /** * @brief * Sets rasterized SVG image size. * @param size New rasterized image size. * @details * Changing rasterized image size resets the cached rasterized bitmap * bitmap which we'll cause an expesive image rasterization procedure * the next time when image data is required. */ void SetSize(CSize size); /** * @brief * Draws a loaded SVG image to the target device context. * @param hDC Target device context handle. * @param pt Target position of the drawn SVG image of its current size. * @param bPreserveAlpha If the loaded SVG image contains alpha channel then * setting bPreserveAlpha tp FALSE will force to draw it opaque. * @details * The first call after changing SVG size or loading a new SVG image * results in an expensive rasterization procedure. */ void Draw(HDC hDC, CPoint pt, BOOL bPreserveAlpha = TRUE); /** * @brief * Draws a loaded SVG image to the target device context. * @param pDC Target device context object pointer. * @param pt Target position of the drawn SVG image of its current size. * from the current SVG size then the current SVG size will be updated * and image re-rasterized. * @param bPreserveAlpha If the loaded SVG image contains alpha channel then * setting bPreserveAlpha tp FALSE will force to draw it opaque. * @details * The first call after changing SVG size or loading a new SVG image * results in an expensive rasterization procedure. */ void Draw(CDC* pDC, CPoint pt, BOOL bPreserveAlpha = TRUE); /** * @brief * Draws a loaded SVG image to the target device context. * @param hDC Target device context handle. * @param rc Target rectangle of the drawn SVG image. If rectangle size is different * from the current SVG size then the current SVG size will be updated * and image re-rasterized. * @param bPreserveAlpha If the loaded SVG image contains alpha channel then * setting bPreserveAlpha tp FALSE will force to draw it opaque. * @details * The first call after changing SVG size or loading a new SVG image * results in an expensive rasterization procedure. */ void Draw(HDC hDC, CRect rc, BOOL bPreserveAlpha = TRUE); /** * @brief * Draws a loaded SVG image to the target device context. * @param pDC Target device context object pointer. * @param rc Target rectangle of the drawn SVG image. If rectangle size is different * from the current SVG size then the current SVG size will be updated * and image re-rasterized. * @param bPreserveAlpha If the loaded SVG image contains alpha channel then * setting bPreserveAlpha tp FALSE will force to draw it opaque. * @details * The first call after changing SVG size or loading a new SVG image * results in an expensive rasterization procedure. */ void Draw(CDC* pDC, CRect rc, BOOL bPreserveAlpha = TRUE); /** * @brief * Draws rasterized image bits directly into the specified memoby buffer. * @param pBits Memory buffer pointer that is at least of szIcon.cx * szIcon.cy * 4 bytes * length. * @param cbSize Size of memory buffer. * @param rc Size of area to draw. * @return * The number of bytes written to the specified memory buffer or 0 if no image data has * been drawn. * @details * Image data consists of Width * Height DWORD color entries where each DWORD color entry * has the following color format: AlphaMask = 0xff000000, RedMask = 0x00ff0000, GreenMask * = 0x0000ff00, BlueMask = 0x000000ff. * @see * IsDirectDrawSupported */ SIZE_T DirectDraw(PVOID pBits, SIZE_T cbSize, CRect rc); private: BOOL LoadXml(const char* pData, Units nUnits = unitPX); BOOL Rasterize(); void Unrasterize(); void Reset(); CBitmap* AccessBitmap(); private: BOOL m_bLoaded; BOOL m_bAlpha; BOOL m_bRasterized; NSVGimage* m_pSvg; NSVGrasterizer* m_pRast; CSize m_size; CBitmap m_bmp; PVOID m_pBits; }; /** @cond */ AFX_INLINE BOOL CXTPSvgImage::IsLoaded() const { return m_bLoaded; } AFX_INLINE CSize CXTPSvgImage::GetSize() const { return m_size; } AFX_INLINE void CXTPSvgImage::Draw(CDC* pDC, CPoint pt, BOOL bPreserveAlpha /*= TRUE*/) { ASSERT_VALID(pDC); Draw(pDC->m_hDC, pt, bPreserveAlpha); } AFX_INLINE void CXTPSvgImage::Draw(CDC* pDC, CRect rc, BOOL bPreserveAlpha /*= TRUE*/) { ASSERT_VALID(pDC); Draw(pDC->m_hDC, rc, bPreserveAlpha); } /** @endcond */ # include "Common/Base/Diagnostic/XTPEnableNoisyWarnings.h" /** @cond */ #endif // #if !defined(__XTPSVGIMAGE_H__) /** @endcond */