auto adjust font size for entity overlay

This commit is contained in:
Jupeyy 2017-09-27 12:19:39 +02:00
parent 745d77ffeb
commit 9fd1290734
3 changed files with 50 additions and 12 deletions

View file

@ -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;
@ -760,14 +760,53 @@ 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 *pBatchEnd = 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 *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;
}
}

View file

@ -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;

View file

@ -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);
}
}
}