Added rendering of custom fonts in svg

This commit is contained in:
Kirill Polyakov
2024-06-28 20:58:19 +03:00
parent 6ae6c37ef1
commit efa4433195
4 changed files with 69 additions and 5 deletions

View File

@ -9,6 +9,8 @@ namespace SVG
if (!wsUnicode.empty())
m_wchUnicode = wsUnicode[0];
m_oHorizAdvX.SetValue(oNode.GetAttributeOrValue(L"horiz-adv-x"));
}
wchar_t CGlyph::GetUnicode() const
@ -29,6 +31,8 @@ namespace SVG
m_oArguments.m_wsFontVariant = oNode.GetAttribute(L"font-variant");
m_oArguments.m_wsFontStyle = oNode.GetAttribute(L"font-style");
m_oArguments.m_wsFontWidght = oNode.GetAttribute(L"font-weight");
m_oHorizAdvX.SetValue(oNode.GetAttributeOrValue(L"horiz-adv-x"));
}
CFont::~CFont()
@ -45,6 +49,55 @@ namespace SVG
bool CFont::Apply(IRenderer *pRenderer, const CSvgFile *pFile, const TBounds &oObjectBounds)
{
return false;
}
bool CFont::Draw(const std::wstring &wsText, double dX, double dY, IRenderer *pRenderer, const CSvgFile *pFile, CommandeMode oMode, const TSvgStyles *pStyles) const
{
if (NULL == pRenderer)
return false;
double dM11, dM12, dM21, dM22, dRx, dRy;
pRenderer->GetTransform(&dM11, &dM12, &dM21, &dM22, &dRx, &dRy);
Aggplus::CMatrix oMatrix(dM11, dM12, dM21, dM22, dRx, dRy);
oMatrix.Translate(dX, dY);
pRenderer->SetTransform(dM11, dM12, dM21, -dM22, oMatrix.tx(), oMatrix.ty());
MGlyphsMap::const_iterator itFound;
TBounds oGlyphBounds;
for (wchar_t wchGlyph : wsText)
{
itFound = m_mGlyphs.find(wchGlyph);
if (m_mGlyphs.cend() == itFound)
{
if (NULL == m_pMissingGlyph)
continue;
m_pMissingGlyph->Draw(pRenderer, pFile, oMode, pStyles);
oMatrix.Translate(m_oHorizAdvX.ToDouble(NSCSS::Pixel), 0);
}
else
{
itFound->second->Draw(pRenderer, pFile, oMode, pStyles);
if (!itFound->second->m_oHorizAdvX.Empty())
oMatrix.Translate(itFound->second->m_oHorizAdvX.ToDouble(NSCSS::Pixel), 0);
else
oMatrix.Translate(m_oHorizAdvX.ToDouble(NSCSS::Pixel), 0);
}
oMatrix.Translate(std::abs(oGlyphBounds.m_dRight - oGlyphBounds.m_dLeft), 0);
pRenderer->SetTransform(dM11, dM12, dM21, -dM22, oMatrix.tx(), oMatrix.ty());
}
pRenderer->SetTransform(dM11, dM12, dM21, dM22, dRx, dRy);
return true;
}

View File

@ -12,7 +12,10 @@ namespace SVG
wchar_t GetUnicode() const;
private:
wchar_t m_wchUnicode;
wchar_t m_wchUnicode;
SvgDigit m_oHorizAdvX;
friend class CFont;
};
class CFontFace
@ -40,13 +43,18 @@ namespace SVG
void SetData(const std::map<std::wstring, std::wstring> &mAttributes, unsigned short ushLevel, bool bHardMode) override;
bool Apply(IRenderer* pRenderer, const CSvgFile *pFile, const TBounds &oObjectBounds) override;
bool Draw(const std::wstring& wsText, double dX, double dY, IRenderer* pRenderer, const CSvgFile *pFile, CommandeMode oMode = CommandeModeDraw, const TSvgStyles* pStyles = NULL) const;
private:
void ParseGlyphs(XmlUtils::CXmlNode& oNode);
TFontArguments m_oArguments;
std::map<wchar_t, CGlyph*> m_mGlyphs;
typedef std::map<wchar_t, CGlyph*> MGlyphsMap;
MGlyphsMap m_mGlyphs;
CPath *m_pMissingGlyph;
SvgDigit m_oHorizAdvX;
};
}

View File

@ -4,6 +4,7 @@
#include "../SvgUtils.h"
#include "../CSvgFile.h"
#include "CContainer.h"
#include "CFont.h"
#include "CStyle.h"
#ifndef MININT8
@ -152,7 +153,7 @@ namespace SVG
double dX, dY;
CalculatePosition(dX, dY);
if (!UseExternalFont(pFile))
if (!UseExternalFont(pFile, dX, dY, pRenderer, pFile, oMode, pOtherStyles))
{
ApplyFont(pRenderer, dX, dY);
@ -296,7 +297,7 @@ namespace SVG
pRenderer->put_BrushAlpha1(255);
}
bool CTSpan::UseExternalFont(const CSvgFile *pFile) const
bool CTSpan::UseExternalFont(const CSvgFile *pFile, double dX, double dY, IRenderer *pRenderer, const CSvgFile *pFile, CommandeMode oMode, const TSvgStyles *pOtherStyles) const
{
std::wstring wsFontFamily = DefaultFontFamily;
@ -311,6 +312,8 @@ namespace SVG
if (NULL == pFont)
return false;
pFont->Draw(m_wsText, dX, dY, pRenderer, pFile, oMode, pOtherStyles);
return true;
}

View File

@ -31,7 +31,7 @@ namespace SVG
void ApplyStyle(IRenderer* pRenderer, const TSvgStyles* pStyles, const CSvgFile* pFile, int& nTypePath) const override;
void ApplyFont(IRenderer* pRenderer, double& dX, double& dY) const;
bool UseExternalFont(const CSvgFile* pFile) const;
bool UseExternalFont(const CSvgFile* pFile, double dX, double dY, IRenderer* pRenderer, const CSvgFile* pFile, CommandeMode oMode = CommandeModeDraw, const TSvgStyles* pOtherStyles = NULL) const;
TBounds GetBounds() const override;