diff --git a/src/SexyAppFramework/SysFont.cpp b/src/SexyAppFramework/SysFont.cpp index 79794e2..99be1b2 100644 --- a/src/SexyAppFramework/SysFont.cpp +++ b/src/SexyAppFramework/SysFont.cpp @@ -18,7 +18,6 @@ #include "SDL3Renderer/SDL3Renderer.h" #endif - using namespace Sexy; static std::string ResolveFontFile(const std::string &theFace) @@ -56,7 +55,7 @@ static std::string ResolveFontFile(const std::string &theFace) continue; std::string aStemLower = aStem; std::transform(aStemLower.begin(), aStemLower.end(), aStemLower.begin(), - [](unsigned char c) { return std::tolower(c); }); + [](unsigned char c) { return std::tolower(c); }); if (aStemLower == aLower) return anEntry.path().string(); } @@ -72,29 +71,28 @@ static std::string ResolveFontFile(const std::string &theFace) return ""; } - SysFont::SysFont(const std::string &theFace, int thePointSize, bool bold, bool italics, bool underline) { Init(gSexyAppBase, theFace, thePointSize, bold, italics, underline, false); } SysFont::SysFont(SexyAppBase *theApp, - const std::string &theFace, - int thePointSize, - bool bold, - bool italics, - bool underline) + const std::string &theFace, + int thePointSize, + bool bold, + bool italics, + bool underline) { Init(theApp, theFace, thePointSize, bold, italics, underline, true); } void SysFont::Init(SexyAppBase *theApp, - const std::string &theFace, - int thePointSize, - bool bold, - bool italics, - bool underline, - bool useDevCaps) + const std::string &theFace, + int thePointSize, + bool bold, + bool italics, + bool underline, + bool useDevCaps) { mApp = theApp; mApp->mRenderer->mSysFonts.insert(this); @@ -127,11 +125,11 @@ void SysFont::Init(SexyAppBase *theApp, FT_Select_Charmap(aFontFace, FT_ENCODING_UNICODE); if (mItalic) { - FT_Matrix matrix = {1 << 16, (FT_Fixed)(0.3 * (1 << 16)), 0, 1 << 16}; + FT_Matrix matrix = { 1 << 16, (FT_Fixed)(0.3 * (1 << 16)), 0, 1 << 16 }; FT_Set_Transform(aFontFace, &matrix, nullptr); } - mFontData = new TrueTypeData(this, aFontFace, thePointSize); + mFontData = std::make_shared(mApp, aFontFace, thePointSize, mBold); if (aFontFace->size) { @@ -170,9 +168,7 @@ void SysFont::Reinit() aFontFace->style_flags = aPrevFlags; int aOldSize = mFontData->mSize; - delete mFontData; - mFontData = nullptr; - mFontData = new TrueTypeData(this, aFontFace, aOldSize); + mFontData = std::make_shared(mApp, aFontFace, aOldSize, mBold); if (aFontFace->size) { @@ -187,7 +183,6 @@ SysFont::SysFont(const SysFont &theSysFont) mHeight = theSysFont.mHeight; mAscent = theSysFont.mAscent; mFontData = theSysFont.mFontData; - mFontData->mFont = this; mBold = theSysFont.mBold; mItalic = theSysFont.mItalic; @@ -196,20 +191,18 @@ SysFont::SysFont(const SysFont &theSysFont) SysFont::~SysFont() { - if (mFontData) - delete mFontData; mApp->mRenderer->mSysFonts.erase(this); } ImageFont *SysFont::CreateImageFont() -{ +{ //todo: uuhhh implement? return nullptr; } int SysFont::StringWidth(const SexyString &theString) -{ - int aWidth = 0; +{ + int aWidth = 0; auto it = theString.begin(); auto end = theString.end(); while (it != end) @@ -222,14 +215,14 @@ int SysFont::StringWidth(const SexyString &theString) } void SysFont::DrawString( - Graphics *g, int theX, int theY, const SexyString &theString, const Color &theColor, const Rect &theClipRect) -{ + Graphics *g, int theX, int theY, const SexyString &theString, const Color &theColor, const Rect &theClipRect) +{ if (mFontData == nullptr) return; int posX = theX; int posY = theY; int underlineY = posY - ((mFontData->mFace->underline_position * mFontData->mFace->size->metrics.y_scale) >> 16 >> 6); - + auto it = theString.begin(); auto end = theString.end(); while (it != end) @@ -245,26 +238,25 @@ void SysFont::DrawString( { Color aShadowColor = Color(0, 0, 0, 0); g->mDestImage->BltRawTexture( - mFontData->mAtlas.mAtlas, - aGlyph.mWidth, - aGlyph.mHeight, - Rect(aDrawX + g->mTransX + 1, aDrawY - mAscent + 1 + g->mTransY, aGlyph.mWidth, aGlyph.mHeight), - Rect(aGlyph.mX, aGlyph.mY, aGlyph.mWidth, aGlyph.mHeight), - theClipRect, - aShadowColor, - 0); + mFontData->mAtlas.mAtlas, + aGlyph.mWidth, + aGlyph.mHeight, + Rect(aDrawX + g->mTransX + 1, aDrawY - mAscent + 1 + g->mTransY, aGlyph.mWidth, aGlyph.mHeight), + Rect(aGlyph.mX, aGlyph.mY, aGlyph.mWidth, aGlyph.mHeight), + theClipRect, + aShadowColor, + 0); } g->mDestImage->BltRawTexture( - mFontData->mAtlas.mAtlas, - mFontData->mAtlas.mWidth, - mFontData->mAtlas.mHeight, - Rect(aDrawX, aDrawY, aGlyph.mWidth, aGlyph.mHeight), - Rect(aGlyph.mX, aGlyph.mY, aGlyph.mWidth, aGlyph.mHeight), - theClipRect, - theColor, - 0); - + mFontData->mAtlas.mAtlas, + mFontData->mAtlas.mWidth, + mFontData->mAtlas.mHeight, + Rect(aDrawX, aDrawY, aGlyph.mWidth, aGlyph.mHeight), + Rect(aGlyph.mX, aGlyph.mY, aGlyph.mWidth, aGlyph.mHeight), + theClipRect, + theColor, + 0); } else { @@ -272,26 +264,25 @@ void SysFont::DrawString( { Color aShadowColor = Color(0, 0, 0, 0); mApp->mRenderer->BltRawTexture( - mFontData->mAtlas.mAtlas, - mFontData->mAtlas.mWidth, - mFontData->mAtlas.mHeight, - Rect(aDrawX + g->mTransX + 1, aDrawY - mAscent + 1 + g->mTransY, aGlyph.mWidth, aGlyph.mHeight), - Rect(aGlyph.mX, aGlyph.mY, aGlyph.mWidth, aGlyph.mHeight), - theClipRect, - aShadowColor, - 0); - + mFontData->mAtlas.mAtlas, + mFontData->mAtlas.mWidth, + mFontData->mAtlas.mHeight, + Rect(aDrawX + g->mTransX + 1, aDrawY - mAscent + 1 + g->mTransY, aGlyph.mWidth, aGlyph.mHeight), + Rect(aGlyph.mX, aGlyph.mY, aGlyph.mWidth, aGlyph.mHeight), + theClipRect, + aShadowColor, + 0); } mApp->mRenderer->BltRawTexture( - mFontData->mAtlas.mAtlas, - mFontData->mAtlas.mWidth, - mFontData->mAtlas.mHeight, - Rect(aDrawX + g->mTransX + 1, aDrawY - mAscent + 1 + g->mTransY, aGlyph.mWidth, aGlyph.mHeight), - Rect(aGlyph.mX, aGlyph.mY, aGlyph.mWidth, aGlyph.mHeight), - theClipRect, - theColor, - 0); + mFontData->mAtlas.mAtlas, + mFontData->mAtlas.mWidth, + mFontData->mAtlas.mHeight, + Rect(aDrawX + g->mTransX + 1, aDrawY - mAscent + 1 + g->mTransY, aGlyph.mWidth, aGlyph.mHeight), + Rect(aGlyph.mX, aGlyph.mY, aGlyph.mWidth, aGlyph.mHeight), + theClipRect, + theColor, + 0); } posX += aGlyph.mAdvance; @@ -305,8 +296,7 @@ void SysFont::DrawString( { if (mUnderlined) mApp->mRenderer->DrawLine( - theX + g->mTransX + 1, underlineY, StringWidth(theString), underlineY, theColor, 0); - + theX + g->mTransX + 1, underlineY, StringWidth(theString), underlineY, theColor, 0); } } @@ -324,7 +314,7 @@ void TrueTypeData::Init() FT_Set_Pixel_Sizes(mFace, 0, mSize); if (mAtlas.mAtlas != nullptr) { - mFont->mApp->mRenderer->DeleteTexture(mAtlas.mAtlas); + mApp->mRenderer->DeleteTexture(mAtlas.mAtlas); } mAtlas.mGlyphs.clear(); @@ -342,8 +332,8 @@ void TrueTypeData::Init() { GlpyhAtlasEntry aGlyph; FT_Load_Char(mFace, aSetupChar, FT_LOAD_RENDER); - - if (mFont->mBold) + + if (mBold) { FT_GlyphSlot_Embolden(mFace->glyph); } @@ -388,7 +378,7 @@ void TrueTypeData::Init() mAtlas.mGlyphs[aSetupChar] = aGlyph; } - mAtlas.mAtlas = mFont->mApp->mRenderer->CreateTexture(anAtlasPixels, mAtlas.mWidth, mAtlas.mHeight, RawPixelFormat::RAW_FORMAT_RGBA, 1); + mAtlas.mAtlas = mApp->mRenderer->CreateTexture(anAtlasPixels, mAtlas.mWidth, mAtlas.mHeight, RawPixelFormat::RAW_FORMAT_RGBA, 1); delete[] anAtlasPixels; } @@ -397,7 +387,7 @@ TrueTypeData::~TrueTypeData() { if (mAtlas.mAtlas != nullptr) { - mFont->mApp->mRenderer->DeleteTexture(mAtlas.mAtlas); + mApp->mRenderer->DeleteTexture(mAtlas.mAtlas); } mAtlas.mGlyphs.clear(); FT_Done_Face(mFace); diff --git a/src/SexyAppFramework/SysFont.h b/src/SexyAppFramework/SysFont.h index d4d50f0..9b1eeb0 100644 --- a/src/SexyAppFramework/SysFont.h +++ b/src/SexyAppFramework/SysFont.h @@ -4,6 +4,7 @@ #include "Font.h" #include #include +#include namespace Sexy { @@ -30,13 +31,30 @@ struct GlyphAtlas std::unordered_map mGlyphs; }; +class TrueTypeData +{ + public: + GlyphAtlas mAtlas; + SexyAppBase *mApp; + FT_Face mFace; + int mSize; + bool mBold; + bool mIsDirty; -class TrueTypeData; + TrueTypeData(SexyAppBase *theApp, FT_Face theFace, int theSize, bool theBold) : mApp(theApp), mFace(theFace), mSize(theSize), mBold(theBold) + { + Init(); + } + + ~TrueTypeData(); + + void Init(); +}; class SysFont : public Font { public: - TrueTypeData* mFontData; + std::shared_ptr mFontData; SexyAppBase *mApp; std::string mFontName; bool mDrawShadow; @@ -44,26 +62,25 @@ class SysFont : public Font bool mItalic; bool mUnderlined; - void Init(SexyAppBase *theApp, - const std::string &theFace, - int thePointSize, - bool bold, - bool italics, - bool underline, - bool useDevCaps); + const std::string &theFace, + int thePointSize, + bool bold, + bool italics, + bool underline, + bool useDevCaps); void Reinit(); public: SysFont( - const std::string &theFace, int thePointSize, bool bold = false, bool italics = false, bool underline = false); + const std::string &theFace, int thePointSize, bool bold = false, bool italics = false, bool underline = false); SysFont(SexyAppBase *theApp, - const std::string &theFace, - int thePointSize, - bool bold = false, - bool italics = false, - bool underline = false); + const std::string &theFace, + int thePointSize, + bool bold = false, + bool italics = false, + bool underline = false); SysFont(const SysFont &theSysFont); virtual ~SysFont(); @@ -71,30 +88,11 @@ class SysFont : public Font ImageFont *CreateImageFont(); virtual int StringWidth(const SexyString &theString); virtual void DrawString( - Graphics *g, int theX, int theY, const SexyString &theString, const Color &theColor, const Rect &theClipRect); + Graphics *g, int theX, int theY, const SexyString &theString, const Color &theColor, const Rect &theClipRect); virtual Font *Duplicate(); - -}; - -struct TrueTypeData -{ - GlyphAtlas mAtlas; - SysFont *mFont; - FT_Face mFace; - int mSize; - bool mIsDirty; - - TrueTypeData(SysFont *theFontPtr, FT_Face &theFace, int theSize) : mFont(theFontPtr), mFace(theFace), mSize(theSize) - { - Init(); - } - - ~TrueTypeData(); - - void Init(); }; } // namespace Sexy -#endif //__SYSFONT_H__ \ No newline at end of file +#endif //__SYSFONT_H__