From 9fd12907349623ec32ada11febbd0e4e92e76078 Mon Sep 17 00:00:00 2001 From: Jupeyy Date: Wed, 27 Sep 2017 12:19:39 +0200 Subject: [PATCH] auto adjust font size for entity overlay --- src/engine/client/text.cpp | 54 ++++++++++++++++++++---- src/engine/textrender.h | 2 +- src/game/client/components/mapimages.cpp | 6 +-- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/engine/client/text.cpp b/src/engine/client/text.cpp index 51414ac9b..697e91044 100644 --- a/src/engine/client/text.cpp +++ b/src/engine/client/text.cpp @@ -745,7 +745,7 @@ public: pCursor->m_Y = DrawY; } - virtual void UploadText(int TextureID, const char *pText, int Length, float x, float y, int Size, int MaxWidth) + virtual void UploadText(int TextureID, const char *pText, int Length, float x, float y, int Size, int MaxWidth, int MaxSize = -1, int MinSize = -1) { CFont *pFont = m_pDefaultFont; FT_Bitmap *pBitmap; @@ -759,15 +759,54 @@ public: const char *pCurrent = (char *)pText; const char *pEnd = pCurrent+Length; - - int ChrCount = 0; int WidthLastChars = 0; + + int FontSize = Size; + + //adjust font size by the full space + if(Size == -1) + { + bool FoundMaxFontSize = false; + if(MinSize == -1) + MinSize = 8; + FontSize = MinSize; + + while(!FoundMaxFontSize){ + int WidthOfText = 0; + + while(pCurrent < pEnd) + { + const char *pTmp = pCurrent; + int NextCharacter = str_utf8_decode(&pTmp); + + if(NextCharacter) + { + FT_Set_Pixel_Sizes(pFont->m_FtFace, 0, FontSize); + if(FT_Load_Char(pFont->m_FtFace, NextCharacter, FT_LOAD_RENDER|FT_LOAD_NO_BITMAP)) + { + dbg_msg("pFont", "error loading glyph %d", NextCharacter); + pCurrent = pTmp; + continue; + } + pBitmap = &pFont->m_FtFace->glyph->bitmap; + + WidthOfText += pBitmap->width + 1; + } + pCurrent = pTmp; + } + if(WidthOfText > MaxWidth || (MaxSize != -1 && FontSize > MaxSize)) + FoundMaxFontSize = true; + + pCurrent = (char *)pText; + pEnd = pCurrent+Length; + ++FontSize; + } + } + while(pCurrent < pEnd) - { - const char *pBatchEnd = pEnd; - + { const char *pTmp = pCurrent; int NextCharacter = str_utf8_decode(&pTmp); @@ -775,7 +814,7 @@ public: { unsigned int px, py; - FT_Set_Pixel_Sizes(pFont->m_FtFace, 0, Size); + FT_Set_Pixel_Sizes(pFont->m_FtFace, 0, FontSize-1); if(FT_Load_Char(pFont->m_FtFace, NextCharacter, FT_LOAD_RENDER|FT_LOAD_NO_BITMAP)) { @@ -818,7 +857,6 @@ public: WidthLastChars += (SlotW + 1); } } - ++ChrCount; pCurrent = pTmp; } } diff --git a/src/engine/textrender.h b/src/engine/textrender.h index 5a6ef24fa..dacf0d128 100644 --- a/src/engine/textrender.h +++ b/src/engine/textrender.h @@ -43,7 +43,7 @@ public: // virtual void TextEx(CTextCursor *pCursor, const char *pText, int Length) = 0; - virtual void UploadText(int TextureID, const char *pText, int Length, float x, float y, int Size, int MaxWidth) = 0; + virtual void UploadText(int TextureID, const char *pText, int Length, float x, float y, int Size, int MaxWidth, int MaxSize = -1, int MinSize = -1) = 0; // old foolish interface virtual void TextColor(float r, float g, float b, float a) = 0; diff --git a/src/game/client/components/mapimages.cpp b/src/game/client/components/mapimages.cpp index d400e8474..f16c09b37 100644 --- a/src/game/client/components/mapimages.cpp +++ b/src/game/client/components/mapimages.cpp @@ -74,11 +74,11 @@ void CMapImages::OnInit() float x = (i%16) * 64; float y = (int)(i/16)* 64; - int Size = (len == 3 ? 20 : 64); + int MinSize = (len == 3 ? 20 : 50); int OffY = (len == 3 ? 10 : 5); - int OffX = (len == 3 ? 10 : 1); + int OffX = (len == 3 ? 5 : 1); - TextRender()->UploadText(m_OverlayCenterTexture, buff, -1, x + OffX, y + OffY, Size, 64-OffX); + TextRender()->UploadText(m_OverlayCenterTexture, buff, -1, x + OffX, y + OffY, -1, 64-(OffX*2), 64, MinSize); } } }