2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <base/system.h>
|
|
|
|
#include <base/math.h>
|
|
|
|
#include <engine/graphics.h>
|
|
|
|
#include <engine/textrender.h>
|
|
|
|
|
|
|
|
#ifdef CONF_FAMILY_WINDOWS
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ft2 texture
|
|
|
|
#include <ft2build.h>
|
|
|
|
#include FT_FREETYPE_H
|
|
|
|
|
|
|
|
// TODO: Refactor: clean this up
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
MAX_CHARACTERS = 64,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-04 11:30:40 +00:00
|
|
|
static int aFontSizes[] = {8,9,10,11,12,13,14,15,16,17,18,19,20,36,64};
|
2010-05-29 07:25:38 +00:00
|
|
|
#define NUM_FONT_SIZES (sizeof(aFontSizes)/sizeof(int))
|
|
|
|
|
|
|
|
struct CFontChar
|
|
|
|
{
|
2011-02-12 10:40:36 +00:00
|
|
|
int m_ID;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// these values are scaled to the pFont size
|
|
|
|
// width * font_size == real_size
|
|
|
|
float m_Width;
|
|
|
|
float m_Height;
|
|
|
|
float m_OffsetX;
|
|
|
|
float m_OffsetY;
|
|
|
|
float m_AdvanceX;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
float m_aUvs[4];
|
|
|
|
int64 m_TouchTime;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CFontSizeData
|
|
|
|
{
|
|
|
|
int m_FontSize;
|
|
|
|
FT_Face *m_pFace;
|
|
|
|
|
2011-12-31 08:40:11 +00:00
|
|
|
int m_aTextures[2];
|
2010-05-29 07:25:38 +00:00
|
|
|
int m_TextureWidth;
|
|
|
|
int m_TextureHeight;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int m_NumXChars;
|
|
|
|
int m_NumYChars;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int m_CharMaxWidth;
|
|
|
|
int m_CharMaxHeight;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CFontChar m_aCharacters[MAX_CHARACTERS*MAX_CHARACTERS];
|
2011-04-13 18:37:12 +00:00
|
|
|
|
|
|
|
int m_CurrentCharacter;
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
|
|
|
|
2010-07-05 18:57:07 +00:00
|
|
|
class CFont
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2010-07-05 18:57:07 +00:00
|
|
|
public:
|
2010-10-06 21:07:35 +00:00
|
|
|
char m_aFilename[512];
|
2010-05-29 07:25:38 +00:00
|
|
|
FT_Face m_FtFace;
|
|
|
|
CFontSizeData m_aSizes[NUM_FONT_SIZES];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class CTextRender : public IEngineTextRender
|
|
|
|
{
|
|
|
|
IGraphics *m_pGraphics;
|
|
|
|
IGraphics *Graphics() { return m_pGraphics; }
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int WordLength(const char *pText)
|
|
|
|
{
|
|
|
|
int s = 1;
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
if(*pText == 0)
|
|
|
|
return s-1;
|
|
|
|
if(*pText == '\n' || *pText == '\t' || *pText == ' ')
|
|
|
|
return s;
|
|
|
|
pText++;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float m_TextR;
|
|
|
|
float m_TextG;
|
|
|
|
float m_TextB;
|
|
|
|
float m_TextA;
|
2011-03-13 11:55:00 +00:00
|
|
|
|
|
|
|
float m_TextOutlineR;
|
|
|
|
float m_TextOutlineG;
|
|
|
|
float m_TextOutlineB;
|
|
|
|
float m_TextOutlineA;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-12-31 08:40:11 +00:00
|
|
|
//int m_FontTextureFormat;
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2010-07-05 18:57:07 +00:00
|
|
|
CFont *m_pDefaultFont;
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
FT_Library m_FTLibrary;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int GetFontSizeIndex(int Pixelsize)
|
|
|
|
{
|
|
|
|
for(unsigned i = 0; i < NUM_FONT_SIZES; i++)
|
|
|
|
{
|
|
|
|
if(aFontSizes[i] >= Pixelsize)
|
|
|
|
return i;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
return NUM_FONT_SIZES-1;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
void Grow(unsigned char *pIn, unsigned char *pOut, int w, int h)
|
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
for(int y = 0; y < h; y++)
|
|
|
|
for(int x = 0; x < w; x++)
|
|
|
|
{
|
|
|
|
int c = pIn[y*w+x];
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
for(int sy = -1; sy <= 1; sy++)
|
|
|
|
for(int sx = -1; sx <= 1; sx++)
|
|
|
|
{
|
|
|
|
int GetX = x+sx;
|
|
|
|
int GetY = y+sy;
|
|
|
|
if (GetX >= 0 && GetY >= 0 && GetX < w && GetY < h)
|
|
|
|
{
|
|
|
|
int Index = GetY*w+GetX;
|
|
|
|
if(pIn[Index] > c)
|
2011-04-13 18:37:12 +00:00
|
|
|
c = pIn[Index];
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pOut[y*w+x] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitTexture(CFontSizeData *pSizeData, int CharWidth, int CharHeight, int Xchars, int Ychars)
|
|
|
|
{
|
|
|
|
static int FontMemoryUsage = 0;
|
|
|
|
int Width = CharWidth*Xchars;
|
|
|
|
int Height = CharHeight*Ychars;
|
|
|
|
void *pMem = mem_alloc(Width*Height, 1);
|
|
|
|
mem_zero(pMem, Width*Height);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-12-31 08:40:11 +00:00
|
|
|
for(int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
if(pSizeData->m_aTextures[i] != 0)
|
|
|
|
{
|
|
|
|
Graphics()->UnloadTexture(pSizeData->m_aTextures[i]);
|
|
|
|
FontMemoryUsage -= pSizeData->m_TextureWidth*pSizeData->m_TextureHeight;
|
|
|
|
pSizeData->m_aTextures[i] = 0;
|
|
|
|
}
|
|
|
|
|
2011-12-31 10:18:55 +00:00
|
|
|
pSizeData->m_aTextures[i] = Graphics()->LoadTextureRaw(Width, Height, CImageInfo::FORMAT_ALPHA, pMem, CImageInfo::FORMAT_ALPHA, IGraphics::TEXLOAD_NOMIPMAPS);
|
2011-12-31 08:40:11 +00:00
|
|
|
FontMemoryUsage += Width*Height;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
pSizeData->m_NumXChars = Xchars;
|
|
|
|
pSizeData->m_NumYChars = Ychars;
|
|
|
|
pSizeData->m_TextureWidth = Width;
|
|
|
|
pSizeData->m_TextureHeight = Height;
|
|
|
|
pSizeData->m_CurrentCharacter = 0;
|
2015-07-09 00:08:14 +00:00
|
|
|
|
2014-09-09 20:23:56 +00:00
|
|
|
dbg_msg("text", "pFont memory usage: %d", FontMemoryUsage);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
mem_free(pMem);
|
|
|
|
}
|
|
|
|
|
2011-01-29 17:48:55 +00:00
|
|
|
int AdjustOutlineThicknessToFontSize(int OutlineThickness, int FontSize)
|
|
|
|
{
|
|
|
|
if(FontSize > 36)
|
|
|
|
OutlineThickness *= 4;
|
|
|
|
else if(FontSize >= 18)
|
|
|
|
OutlineThickness *= 2;
|
|
|
|
return OutlineThickness;
|
2011-01-04 11:30:40 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void IncreaseTextureSize(CFontSizeData *pSizeData)
|
|
|
|
{
|
|
|
|
if(pSizeData->m_TextureWidth < pSizeData->m_TextureHeight)
|
|
|
|
pSizeData->m_NumXChars <<= 1;
|
|
|
|
else
|
|
|
|
pSizeData->m_NumYChars <<= 1;
|
2011-04-13 18:37:12 +00:00
|
|
|
InitTexture(pSizeData, pSizeData->m_CharMaxWidth, pSizeData->m_CharMaxHeight, pSizeData->m_NumXChars, pSizeData->m_NumYChars);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// TODO: Refactor: move this into a pFont class
|
|
|
|
void InitIndex(CFont *pFont, int Index)
|
|
|
|
{
|
|
|
|
CFontSizeData *pSizeData = &pFont->m_aSizes[Index];
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
pSizeData->m_FontSize = aFontSizes[Index];
|
|
|
|
FT_Set_Pixel_Sizes(pFont->m_FtFace, 0, pSizeData->m_FontSize);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-01-04 11:30:40 +00:00
|
|
|
int OutlineThickness = AdjustOutlineThicknessToFontSize(1, pSizeData->m_FontSize);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
unsigned GlyphIndex;
|
|
|
|
int MaxH = 0;
|
|
|
|
int MaxW = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int Charcode = FT_Get_First_Char(pFont->m_FtFace, &GlyphIndex);
|
|
|
|
while(GlyphIndex != 0)
|
2011-04-13 18:37:12 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
// do stuff
|
|
|
|
FT_Load_Glyph(pFont->m_FtFace, GlyphIndex, FT_LOAD_DEFAULT);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(pFont->m_FtFace->glyph->metrics.width > MaxW) MaxW = pFont->m_FtFace->glyph->metrics.width; // ignore_convention
|
|
|
|
if(pFont->m_FtFace->glyph->metrics.height > MaxH) MaxH = pFont->m_FtFace->glyph->metrics.height; // ignore_convention
|
|
|
|
Charcode = FT_Get_Next_Char(pFont->m_FtFace, Charcode, &GlyphIndex);
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
MaxW = (MaxW>>6)+2+OutlineThickness*2;
|
|
|
|
MaxH = (MaxH>>6)+2+OutlineThickness*2;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
for(pSizeData->m_CharMaxWidth = 1; pSizeData->m_CharMaxWidth < MaxW; pSizeData->m_CharMaxWidth <<= 1);
|
|
|
|
for(pSizeData->m_CharMaxHeight = 1; pSizeData->m_CharMaxHeight < MaxH; pSizeData->m_CharMaxHeight <<= 1);
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
//dbg_msg("pFont", "init size %d, texture size %d %d", pFont->sizes[index].font_size, w, h);
|
|
|
|
//FT_New_Face(m_FTLibrary, "data/fonts/vera.ttf", 0, &pFont->ft_face);
|
|
|
|
InitTexture(pSizeData, pSizeData->m_CharMaxWidth, pSizeData->m_CharMaxHeight, 8, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFontSizeData *GetSize(CFont *pFont, int Pixelsize)
|
|
|
|
{
|
|
|
|
int Index = GetFontSizeIndex(Pixelsize);
|
|
|
|
if(pFont->m_aSizes[Index].m_FontSize != aFontSizes[Index])
|
|
|
|
InitIndex(pFont, Index);
|
|
|
|
return &pFont->m_aSizes[Index];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-12 10:40:36 +00:00
|
|
|
void UploadGlyph(CFontSizeData *pSizeData, int Texnum, int SlotID, int Chr, const void *pData)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2011-02-12 10:40:36 +00:00
|
|
|
int x = (SlotID%pSizeData->m_NumXChars) * (pSizeData->m_TextureWidth/pSizeData->m_NumXChars);
|
|
|
|
int y = (SlotID/pSizeData->m_NumXChars) * (pSizeData->m_TextureHeight/pSizeData->m_NumYChars);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-12-31 08:40:11 +00:00
|
|
|
Graphics()->LoadTextureRawSub(pSizeData->m_aTextures[Texnum], x, y,
|
|
|
|
pSizeData->m_TextureWidth/pSizeData->m_NumXChars,
|
|
|
|
pSizeData->m_TextureHeight/pSizeData->m_NumYChars,
|
|
|
|
CImageInfo::FORMAT_ALPHA, pData);
|
|
|
|
/*
|
2010-05-29 07:25:38 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, pSizeData->m_aTextures[Texnum]);
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y,
|
|
|
|
pSizeData->m_TextureWidth/pSizeData->m_NumXChars,
|
|
|
|
pSizeData->m_TextureHeight/pSizeData->m_NumYChars,
|
2011-12-31 08:40:11 +00:00
|
|
|
m_FontTextureFormat, GL_UNSIGNED_BYTE, pData);*/
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
2010-10-29 21:49:33 +00:00
|
|
|
// 32k of data used for rendering glyphs
|
|
|
|
unsigned char ms_aGlyphData[(1024/8) * (1024/8)];
|
|
|
|
unsigned char ms_aGlyphDataOutlined[(1024/8) * (1024/8)];
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
int GetSlot(CFontSizeData *pSizeData)
|
|
|
|
{
|
|
|
|
int CharCount = pSizeData->m_NumXChars*pSizeData->m_NumYChars;
|
|
|
|
if(pSizeData->m_CurrentCharacter < CharCount)
|
|
|
|
{
|
|
|
|
int i = pSizeData->m_CurrentCharacter;
|
|
|
|
pSizeData->m_CurrentCharacter++;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// kick out the oldest
|
|
|
|
// TODO: remove this linear search
|
|
|
|
{
|
|
|
|
int Oldest = 0;
|
|
|
|
for(int i = 1; i < CharCount; i++)
|
|
|
|
{
|
|
|
|
if(pSizeData->m_aCharacters[i].m_TouchTime < pSizeData->m_aCharacters[Oldest].m_TouchTime)
|
|
|
|
Oldest = i;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-12-29 12:03:06 +00:00
|
|
|
if(time_get()-pSizeData->m_aCharacters[Oldest].m_TouchTime < time_freq() &&
|
|
|
|
(pSizeData->m_NumXChars < MAX_CHARACTERS || pSizeData->m_NumYChars < MAX_CHARACTERS))
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
IncreaseTextureSize(pSizeData);
|
|
|
|
return GetSlot(pSizeData);
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
return Oldest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int RenderGlyph(CFont *pFont, CFontSizeData *pSizeData, int Chr)
|
|
|
|
{
|
|
|
|
FT_Bitmap *pBitmap;
|
2011-02-12 10:40:36 +00:00
|
|
|
int SlotID = 0;
|
2010-05-29 07:25:38 +00:00
|
|
|
int SlotW = pSizeData->m_TextureWidth / pSizeData->m_NumXChars;
|
|
|
|
int SlotH = pSizeData->m_TextureHeight / pSizeData->m_NumYChars;
|
|
|
|
int SlotSize = SlotW*SlotH;
|
|
|
|
int x = 1;
|
|
|
|
int y = 1;
|
2017-03-12 15:47:37 +00:00
|
|
|
unsigned int px, py;
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
FT_Set_Pixel_Sizes(pFont->m_FtFace, 0, pSizeData->m_FontSize);
|
|
|
|
|
|
|
|
if(FT_Load_Char(pFont->m_FtFace, Chr, FT_LOAD_RENDER|FT_LOAD_NO_BITMAP))
|
|
|
|
{
|
|
|
|
dbg_msg("pFont", "error loading glyph %d", Chr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pBitmap = &pFont->m_FtFace->glyph->bitmap; // ignore_convention
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// fetch slot
|
2011-02-12 10:40:36 +00:00
|
|
|
SlotID = GetSlot(pSizeData);
|
|
|
|
if(SlotID < 0)
|
2010-05-29 07:25:38 +00:00
|
|
|
return -1;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// adjust spacing
|
2011-01-04 11:30:40 +00:00
|
|
|
int OutlineThickness = AdjustOutlineThicknessToFontSize(1, pSizeData->m_FontSize);
|
2010-05-29 07:25:38 +00:00
|
|
|
x += OutlineThickness;
|
|
|
|
y += OutlineThickness;
|
|
|
|
|
|
|
|
// prepare glyph data
|
|
|
|
mem_zero(ms_aGlyphData, SlotSize);
|
|
|
|
|
|
|
|
if(pBitmap->pixel_mode == FT_PIXEL_MODE_GRAY) // ignore_convention
|
|
|
|
{
|
2017-03-18 09:27:11 +00:00
|
|
|
for(py = 0; py < (unsigned)pBitmap->rows; py++) // ignore_convention
|
|
|
|
for(px = 0; px < (unsigned)pBitmap->width; px++) // ignore_convention
|
2010-05-29 07:25:38 +00:00
|
|
|
ms_aGlyphData[(py+y)*SlotW+px+x] = pBitmap->buffer[py*pBitmap->pitch+px]; // ignore_convention
|
|
|
|
}
|
|
|
|
else if(pBitmap->pixel_mode == FT_PIXEL_MODE_MONO) // ignore_convention
|
|
|
|
{
|
2017-03-18 09:27:11 +00:00
|
|
|
for(py = 0; py < (unsigned)pBitmap->rows; py++) // ignore_convention
|
|
|
|
for(px = 0; px < (unsigned)pBitmap->width; px++) // ignore_convention
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
if(pBitmap->buffer[py*pBitmap->pitch+px/8]&(1<<(7-(px%8)))) // ignore_convention
|
|
|
|
ms_aGlyphData[(py+y)*SlotW+px+x] = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 12:04:31 +00:00
|
|
|
if(0) for(py = 0; (int)py < SlotW; py++)
|
|
|
|
for(px = 0; (int)px < SlotH; px++)
|
2010-05-29 07:25:38 +00:00
|
|
|
ms_aGlyphData[py*SlotW+px] = 255;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// upload the glyph
|
2011-02-12 10:40:36 +00:00
|
|
|
UploadGlyph(pSizeData, 0, SlotID, Chr, ms_aGlyphData);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(OutlineThickness == 1)
|
|
|
|
{
|
|
|
|
Grow(ms_aGlyphData, ms_aGlyphDataOutlined, SlotW, SlotH);
|
2011-02-12 10:40:36 +00:00
|
|
|
UploadGlyph(pSizeData, 1, SlotID, Chr, ms_aGlyphDataOutlined);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-05 11:12:23 +00:00
|
|
|
for(int i = OutlineThickness; i > 0; i-=2)
|
|
|
|
{
|
|
|
|
Grow(ms_aGlyphData, ms_aGlyphDataOutlined, SlotW, SlotH);
|
|
|
|
Grow(ms_aGlyphDataOutlined, ms_aGlyphData, SlotW, SlotH);
|
|
|
|
}
|
2011-02-12 10:40:36 +00:00
|
|
|
UploadGlyph(pSizeData, 1, SlotID, Chr, ms_aGlyphData);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// set char info
|
|
|
|
{
|
2011-02-12 10:40:36 +00:00
|
|
|
CFontChar *pFontchr = &pSizeData->m_aCharacters[SlotID];
|
2010-05-29 07:25:38 +00:00
|
|
|
float Scale = 1.0f/pSizeData->m_FontSize;
|
|
|
|
float Uscale = 1.0f/pSizeData->m_TextureWidth;
|
|
|
|
float Vscale = 1.0f/pSizeData->m_TextureHeight;
|
|
|
|
int Height = pBitmap->rows + OutlineThickness*2 + 2; // ignore_convention
|
|
|
|
int Width = pBitmap->width + OutlineThickness*2 + 2; // ignore_convention
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-02-12 10:40:36 +00:00
|
|
|
pFontchr->m_ID = Chr;
|
2010-05-29 07:25:38 +00:00
|
|
|
pFontchr->m_Height = Height * Scale;
|
|
|
|
pFontchr->m_Width = Width * Scale;
|
|
|
|
pFontchr->m_OffsetX = (pFont->m_FtFace->glyph->bitmap_left-1) * Scale; // ignore_convention
|
|
|
|
pFontchr->m_OffsetY = (pSizeData->m_FontSize - pFont->m_FtFace->glyph->bitmap_top) * Scale; // ignore_convention
|
|
|
|
pFontchr->m_AdvanceX = (pFont->m_FtFace->glyph->advance.x>>6) * Scale; // ignore_convention
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-02-12 10:40:36 +00:00
|
|
|
pFontchr->m_aUvs[0] = (SlotID%pSizeData->m_NumXChars) / (float)(pSizeData->m_NumXChars);
|
|
|
|
pFontchr->m_aUvs[1] = (SlotID/pSizeData->m_NumXChars) / (float)(pSizeData->m_NumYChars);
|
2010-05-29 07:25:38 +00:00
|
|
|
pFontchr->m_aUvs[2] = pFontchr->m_aUvs[0] + Width*Uscale;
|
|
|
|
pFontchr->m_aUvs[3] = pFontchr->m_aUvs[1] + Height*Vscale;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-02-12 10:40:36 +00:00
|
|
|
return SlotID;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CFontChar *GetChar(CFont *pFont, CFontSizeData *pSizeData, int Chr)
|
|
|
|
{
|
|
|
|
CFontChar *pFontchr = NULL;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// search for the character
|
|
|
|
// TODO: remove this linear search
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < pSizeData->m_CurrentCharacter; i++)
|
|
|
|
{
|
2011-02-12 10:40:36 +00:00
|
|
|
if(pSizeData->m_aCharacters[i].m_ID == Chr)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
pFontchr = &pSizeData->m_aCharacters[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// check if we need to render the character
|
|
|
|
if(!pFontchr)
|
|
|
|
{
|
|
|
|
int Index = RenderGlyph(pFont, pSizeData, Chr);
|
|
|
|
if(Index >= 0)
|
|
|
|
pFontchr = &pSizeData->m_aCharacters[Index];
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// touch the character
|
|
|
|
// TODO: don't call time_get here
|
|
|
|
if(pFontchr)
|
|
|
|
pFontchr->m_TouchTime = time_get();
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
return pFontchr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// must only be called from the rendering function as the pFont must be set to the correct size
|
|
|
|
void RenderSetup(CFont *pFont, int size)
|
|
|
|
{
|
|
|
|
FT_Set_Pixel_Sizes(pFont->m_FtFace, 0, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Kerning(CFont *pFont, int Left, int Right)
|
|
|
|
{
|
|
|
|
FT_Vector Kerning = {0,0};
|
|
|
|
FT_Get_Kerning(pFont->m_FtFace, Left, Right, FT_KERNING_DEFAULT, &Kerning);
|
|
|
|
return (Kerning.x>>6);
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
public:
|
|
|
|
CTextRender()
|
|
|
|
{
|
|
|
|
m_pGraphics = 0;
|
|
|
|
|
2011-03-13 11:55:00 +00:00
|
|
|
m_TextR = 1.0f;
|
|
|
|
m_TextG = 1.0f;
|
|
|
|
m_TextB = 1.0f;
|
|
|
|
m_TextA = 1.0f;
|
|
|
|
m_TextOutlineR = 0.0f;
|
|
|
|
m_TextOutlineG = 0.0f;
|
|
|
|
m_TextOutlineB = 0.0f;
|
|
|
|
m_TextOutlineA = 0.3f;
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
m_pDefaultFont = 0;
|
2017-07-21 18:45:23 +00:00
|
|
|
m_FTLibrary = 0;
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
// GL_LUMINANCE can be good for debugging
|
2011-12-31 08:40:11 +00:00
|
|
|
//m_FontTextureFormat = GL_ALPHA;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2017-07-21 17:10:50 +00:00
|
|
|
virtual ~CTextRender()
|
|
|
|
{
|
2017-07-28 18:44:03 +00:00
|
|
|
if(m_pDefaultFont != 0)
|
2017-07-21 17:10:50 +00:00
|
|
|
DestroyFont(m_pDefaultFont);
|
2017-07-21 18:45:23 +00:00
|
|
|
|
2017-07-28 18:44:03 +00:00
|
|
|
if(m_FTLibrary != 0)
|
2017-07-21 18:45:23 +00:00
|
|
|
FT_Done_FreeType(m_FTLibrary);
|
2017-07-21 17:10:50 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual void Init()
|
|
|
|
{
|
|
|
|
m_pGraphics = Kernel()->RequestInterface<IGraphics>();
|
|
|
|
FT_Init_FreeType(&m_FTLibrary);
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
virtual CFont *LoadFont(const char *pFilename)
|
|
|
|
{
|
|
|
|
CFont *pFont = (CFont *)mem_alloc(sizeof(CFont), 1);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2017-07-21 17:10:50 +00:00
|
|
|
mem_zero(pFont, sizeof(CFont));
|
2010-05-29 07:25:38 +00:00
|
|
|
str_copy(pFont->m_aFilename, pFilename, sizeof(pFont->m_aFilename));
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(FT_New_Face(m_FTLibrary, pFont->m_aFilename, 0, &pFont->m_FtFace))
|
|
|
|
{
|
|
|
|
mem_free(pFont);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(unsigned i = 0; i < NUM_FONT_SIZES; i++)
|
|
|
|
pFont->m_aSizes[i].m_FontSize = -1;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
dbg_msg("textrender", "loaded pFont from '%s'", pFilename);
|
|
|
|
return pFont;
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void DestroyFont(CFont *pFont)
|
|
|
|
{
|
2017-07-21 17:10:50 +00:00
|
|
|
FT_Done_Face(pFont->m_FtFace);
|
2010-05-29 07:25:38 +00:00
|
|
|
mem_free(pFont);
|
|
|
|
}
|
|
|
|
|
2010-07-05 18:57:07 +00:00
|
|
|
virtual void SetDefaultFont(CFont *pFont)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
dbg_msg("textrender", "default pFont set %p", pFont);
|
2017-07-28 18:44:03 +00:00
|
|
|
if(m_pDefaultFont != 0)
|
2017-07-21 17:10:50 +00:00
|
|
|
DestroyFont(m_pDefaultFont);
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pDefaultFont = pFont;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual void SetCursor(CTextCursor *pCursor, float x, float y, float FontSize, int Flags)
|
|
|
|
{
|
|
|
|
mem_zero(pCursor, sizeof(*pCursor));
|
|
|
|
pCursor->m_FontSize = FontSize;
|
|
|
|
pCursor->m_StartX = x;
|
|
|
|
pCursor->m_StartY = y;
|
|
|
|
pCursor->m_X = x;
|
|
|
|
pCursor->m_Y = y;
|
|
|
|
pCursor->m_LineCount = 1;
|
|
|
|
pCursor->m_LineWidth = -1;
|
|
|
|
pCursor->m_Flags = Flags;
|
|
|
|
pCursor->m_CharCount = 0;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual void Text(void *pFontSetV, float x, float y, float Size, const char *pText, int MaxWidth)
|
|
|
|
{
|
|
|
|
CTextCursor Cursor;
|
|
|
|
SetCursor(&Cursor, x, y, Size, TEXTFLAG_RENDER);
|
|
|
|
Cursor.m_LineWidth = MaxWidth;
|
|
|
|
TextEx(&Cursor, pText, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual float TextWidth(void *pFontSetV, float Size, const char *pText, int Length)
|
|
|
|
{
|
|
|
|
CTextCursor Cursor;
|
|
|
|
SetCursor(&Cursor, 0, 0, Size, 0);
|
|
|
|
TextEx(&Cursor, pText, Length);
|
|
|
|
return Cursor.m_X;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-08-16 00:21:18 +00:00
|
|
|
virtual int TextLineCount(void *pFontSetV, float Size, const char *pText, float LineWidth)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
CTextCursor Cursor;
|
|
|
|
SetCursor(&Cursor, 0, 0, Size, 0);
|
|
|
|
Cursor.m_LineWidth = LineWidth;
|
|
|
|
TextEx(&Cursor, pText, -1);
|
|
|
|
return Cursor.m_LineCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void TextColor(float r, float g, float b, float a)
|
|
|
|
{
|
|
|
|
m_TextR = r;
|
|
|
|
m_TextG = g;
|
|
|
|
m_TextB = b;
|
|
|
|
m_TextA = a;
|
|
|
|
}
|
2011-03-13 11:55:00 +00:00
|
|
|
|
|
|
|
virtual void TextOutlineColor(float r, float g, float b, float a)
|
|
|
|
{
|
|
|
|
m_TextOutlineR = r;
|
|
|
|
m_TextOutlineG = g;
|
|
|
|
m_TextOutlineB = b;
|
|
|
|
m_TextOutlineA = a;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual void TextEx(CTextCursor *pCursor, const char *pText, int Length)
|
|
|
|
{
|
|
|
|
CFont *pFont = pCursor->m_pFont;
|
|
|
|
CFontSizeData *pSizeData = NULL;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
//dbg_msg("textrender", "rendering text '%s'", text);
|
|
|
|
|
|
|
|
float ScreenX0, ScreenY0, ScreenX1, ScreenY1;
|
|
|
|
float FakeToScreenX, FakeToScreenY;
|
|
|
|
int ActualX, ActualY;
|
|
|
|
|
|
|
|
int ActualSize;
|
|
|
|
int i;
|
|
|
|
int GotNewLine = 0;
|
2010-11-17 17:36:19 +00:00
|
|
|
float DrawX = 0.0f, DrawY = 0.0f;
|
|
|
|
int LineCount = 0;
|
2010-05-29 07:25:38 +00:00
|
|
|
float CursorX, CursorY;
|
|
|
|
|
|
|
|
float Size = pCursor->m_FontSize;
|
|
|
|
|
|
|
|
// to correct coords, convert to screen coords, round, and convert back
|
|
|
|
Graphics()->GetScreen(&ScreenX0, &ScreenY0, &ScreenX1, &ScreenY1);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
FakeToScreenX = (Graphics()->ScreenWidth()/(ScreenX1-ScreenX0));
|
|
|
|
FakeToScreenY = (Graphics()->ScreenHeight()/(ScreenY1-ScreenY0));
|
2010-08-16 00:21:18 +00:00
|
|
|
ActualX = (int)(pCursor->m_X * FakeToScreenX);
|
|
|
|
ActualY = (int)(pCursor->m_Y * FakeToScreenY);
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
CursorX = ActualX / FakeToScreenX;
|
|
|
|
CursorY = ActualY / FakeToScreenY;
|
|
|
|
|
|
|
|
// same with size
|
2010-08-16 00:21:18 +00:00
|
|
|
ActualSize = (int)(Size * FakeToScreenY);
|
2010-05-29 07:25:38 +00:00
|
|
|
Size = ActualSize / FakeToScreenY;
|
|
|
|
|
|
|
|
// fetch pFont data
|
|
|
|
if(!pFont)
|
|
|
|
pFont = m_pDefaultFont;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!pFont)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pSizeData = GetSize(pFont, ActualSize);
|
|
|
|
RenderSetup(pFont, ActualSize);
|
2010-08-17 22:21:38 +00:00
|
|
|
|
|
|
|
float Scale = 1/pSizeData->m_FontSize;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// set length
|
|
|
|
if(Length < 0)
|
|
|
|
Length = str_length(pText);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// if we don't want to render, we can just skip the first outline pass
|
|
|
|
i = 1;
|
|
|
|
if(pCursor->m_Flags&TEXTFLAG_RENDER)
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
for(;i < 2; i++)
|
|
|
|
{
|
|
|
|
const char *pCurrent = (char *)pText;
|
|
|
|
const char *pEnd = pCurrent+Length;
|
|
|
|
DrawX = CursorX;
|
|
|
|
DrawY = CursorY;
|
2010-10-11 00:29:30 +00:00
|
|
|
LineCount = pCursor->m_LineCount;
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
if(pCursor->m_Flags&TEXTFLAG_RENDER)
|
|
|
|
{
|
|
|
|
// TODO: Make this better
|
|
|
|
if (i == 0)
|
2011-12-31 08:40:11 +00:00
|
|
|
Graphics()->TextureSet(pSizeData->m_aTextures[1]);
|
2010-05-29 07:25:38 +00:00
|
|
|
else
|
2011-12-31 08:40:11 +00:00
|
|
|
Graphics()->TextureSet(pSizeData->m_aTextures[0]);
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
Graphics()->QuadsBegin();
|
|
|
|
if (i == 0)
|
2011-03-13 11:55:00 +00:00
|
|
|
Graphics()->SetColor(m_TextOutlineR, m_TextOutlineG, m_TextOutlineB, m_TextOutlineA*m_TextA);
|
2010-05-29 07:25:38 +00:00
|
|
|
else
|
|
|
|
Graphics()->SetColor(m_TextR, m_TextG, m_TextB, m_TextA);
|
|
|
|
}
|
|
|
|
|
2010-10-11 00:29:30 +00:00
|
|
|
while(pCurrent < pEnd && (pCursor->m_MaxLines < 1 || LineCount <= pCursor->m_MaxLines))
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
int NewLine = 0;
|
|
|
|
const char *pBatchEnd = pEnd;
|
|
|
|
if(pCursor->m_LineWidth > 0 && !(pCursor->m_Flags&TEXTFLAG_STOP_AT_END))
|
|
|
|
{
|
|
|
|
int Wlen = min(WordLength((char *)pCurrent), (int)(pEnd-pCurrent));
|
|
|
|
CTextCursor Compare = *pCursor;
|
|
|
|
Compare.m_X = DrawX;
|
|
|
|
Compare.m_Y = DrawY;
|
|
|
|
Compare.m_Flags &= ~TEXTFLAG_RENDER;
|
|
|
|
Compare.m_LineWidth = -1;
|
2012-04-20 22:23:48 +00:00
|
|
|
TextEx(&Compare, pCurrent, Wlen);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(Compare.m_X-DrawX > pCursor->m_LineWidth)
|
|
|
|
{
|
|
|
|
// word can't be fitted in one line, cut it
|
|
|
|
CTextCursor Cutter = *pCursor;
|
|
|
|
Cutter.m_CharCount = 0;
|
|
|
|
Cutter.m_X = DrawX;
|
|
|
|
Cutter.m_Y = DrawY;
|
|
|
|
Cutter.m_Flags &= ~TEXTFLAG_RENDER;
|
|
|
|
Cutter.m_Flags |= TEXTFLAG_STOP_AT_END;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
TextEx(&Cutter, (const char *)pCurrent, Wlen);
|
|
|
|
Wlen = Cutter.m_CharCount;
|
|
|
|
NewLine = 1;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(Wlen <= 3) // if we can't place 3 chars of the word on this line, take the next
|
|
|
|
Wlen = 0;
|
|
|
|
}
|
|
|
|
else if(Compare.m_X-pCursor->m_StartX > pCursor->m_LineWidth)
|
|
|
|
{
|
|
|
|
NewLine = 1;
|
|
|
|
Wlen = 0;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
pBatchEnd = pCurrent + Wlen;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-02-18 10:08:38 +00:00
|
|
|
const char *pTmp = pCurrent;
|
|
|
|
int NextCharacter = str_utf8_decode(&pTmp);
|
2010-05-29 07:25:38 +00:00
|
|
|
while(pCurrent < pBatchEnd)
|
|
|
|
{
|
2011-02-18 10:08:38 +00:00
|
|
|
int Character = NextCharacter;
|
|
|
|
pCurrent = pTmp;
|
|
|
|
NextCharacter = str_utf8_decode(&pTmp);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(Character == '\n')
|
|
|
|
{
|
|
|
|
DrawX = pCursor->m_StartX;
|
|
|
|
DrawY += Size;
|
|
|
|
DrawX = (int)(DrawX * FakeToScreenX) / FakeToScreenX; // realign
|
|
|
|
DrawY = (int)(DrawY * FakeToScreenY) / FakeToScreenY;
|
2010-10-11 00:29:30 +00:00
|
|
|
++LineCount;
|
|
|
|
if(pCursor->m_MaxLines > 0 && LineCount > pCursor->m_MaxLines)
|
|
|
|
break;
|
2010-05-29 07:25:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-02-18 10:08:38 +00:00
|
|
|
CFontChar *pChr = GetChar(pFont, pSizeData, Character);
|
2010-05-29 07:25:38 +00:00
|
|
|
if(pChr)
|
|
|
|
{
|
2011-02-18 10:08:38 +00:00
|
|
|
float Advance = pChr->m_AdvanceX + Kerning(pFont, Character, NextCharacter)*Scale;
|
2011-01-09 22:25:07 +00:00
|
|
|
if(pCursor->m_Flags&TEXTFLAG_STOP_AT_END && DrawX+Advance*Size-pCursor->m_StartX > pCursor->m_LineWidth)
|
|
|
|
{
|
|
|
|
// we hit the end of the line, no more to render or count
|
|
|
|
pCurrent = pEnd;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(pCursor->m_Flags&TEXTFLAG_RENDER)
|
|
|
|
{
|
|
|
|
Graphics()->QuadsSetSubset(pChr->m_aUvs[0], pChr->m_aUvs[1], pChr->m_aUvs[2], pChr->m_aUvs[3]);
|
|
|
|
IGraphics::CQuadItem QuadItem(DrawX+pChr->m_OffsetX*Size, DrawY+pChr->m_OffsetY*Size, pChr->m_Width*Size, pChr->m_Height*Size);
|
|
|
|
Graphics()->QuadsDrawTL(&QuadItem, 1);
|
|
|
|
}
|
|
|
|
|
2011-02-18 10:08:38 +00:00
|
|
|
DrawX += Advance*Size;
|
|
|
|
pCursor->m_CharCount++;
|
|
|
|
}
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(NewLine)
|
|
|
|
{
|
|
|
|
DrawX = pCursor->m_StartX;
|
|
|
|
DrawY += Size;
|
|
|
|
GotNewLine = 1;
|
|
|
|
DrawX = (int)(DrawX * FakeToScreenX) / FakeToScreenX; // realign
|
2011-04-13 18:37:12 +00:00
|
|
|
DrawY = (int)(DrawY * FakeToScreenY) / FakeToScreenY;
|
2010-10-11 00:29:30 +00:00
|
|
|
++LineCount;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pCursor->m_Flags&TEXTFLAG_RENDER)
|
|
|
|
Graphics()->QuadsEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
pCursor->m_X = DrawX;
|
2010-10-11 00:29:30 +00:00
|
|
|
pCursor->m_LineCount = LineCount;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(GotNewLine)
|
|
|
|
pCursor->m_Y = DrawY;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2017-09-27 10:19:39 +00:00
|
|
|
virtual void UploadText(int TextureID, const char *pText, int Length, float x, float y, int Size, int MaxWidth, int MaxSize = -1, int MinSize = -1)
|
2017-09-12 18:10:27 +00:00
|
|
|
{
|
|
|
|
CFont *pFont = m_pDefaultFont;
|
|
|
|
FT_Bitmap *pBitmap;
|
|
|
|
|
|
|
|
if(!pFont)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// set length
|
|
|
|
if(Length < 0)
|
|
|
|
Length = str_length(pText);
|
|
|
|
|
|
|
|
const char *pCurrent = (char *)pText;
|
|
|
|
const char *pEnd = pCurrent+Length;
|
|
|
|
|
|
|
|
int WidthLastChars = 0;
|
2017-09-27 10:19:39 +00:00
|
|
|
|
|
|
|
int FontSize = Size;
|
|
|
|
|
|
|
|
//adjust font size by the full space
|
|
|
|
if(Size == -1)
|
2017-09-12 18:10:27 +00:00
|
|
|
{
|
2017-09-27 10:19:39 +00:00
|
|
|
bool FoundMaxFontSize = false;
|
|
|
|
if(MinSize == -1)
|
|
|
|
MinSize = 8;
|
|
|
|
FontSize = MinSize;
|
2017-09-12 18:10:27 +00:00
|
|
|
|
2017-09-27 12:52:06 +00:00
|
|
|
while(!FoundMaxFontSize)
|
|
|
|
{
|
2017-09-27 10:19:39 +00:00
|
|
|
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)
|
|
|
|
{
|
2017-09-12 18:10:27 +00:00
|
|
|
const char *pTmp = pCurrent;
|
|
|
|
int NextCharacter = str_utf8_decode(&pTmp);
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
if(NextCharacter)
|
|
|
|
{
|
2017-09-12 18:10:27 +00:00
|
|
|
unsigned int px, py;
|
|
|
|
|
2017-09-27 10:19:39 +00:00
|
|
|
FT_Set_Pixel_Sizes(pFont->m_FtFace, 0, FontSize-1);
|
2017-09-12 18:10:27 +00:00
|
|
|
|
|
|
|
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; // ignore_convention
|
2017-09-15 01:01:26 +00:00
|
|
|
|
|
|
|
int MaxSize = (MaxWidth - WidthLastChars);
|
|
|
|
if (MaxSize > 0)
|
2017-09-12 18:10:27 +00:00
|
|
|
{
|
2017-09-27 12:52:06 +00:00
|
|
|
int SlotW = ((unsigned int)MaxSize < pBitmap->width ? MaxSize : pBitmap->width);
|
2017-09-15 01:01:26 +00:00
|
|
|
int SlotH = pBitmap->rows;
|
|
|
|
int SlotSize = SlotW*SlotH;
|
|
|
|
|
|
|
|
// prepare glyph data
|
|
|
|
mem_zero(ms_aGlyphData, SlotSize);
|
|
|
|
|
|
|
|
if(pBitmap->pixel_mode == FT_PIXEL_MODE_GRAY) // ignore_convention
|
|
|
|
{
|
|
|
|
for (py = 0; py < (unsigned)SlotH; py++) // ignore_convention
|
|
|
|
for (px = 0; px < (unsigned)SlotW; px++)
|
|
|
|
{
|
|
|
|
ms_aGlyphData[(py)*SlotW + px] = pBitmap->buffer[py*pBitmap->width + px]; // ignore_convention
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*else if(pBitmap->pixel_mode == FT_PIXEL_MODE_MONO) // ignore_convention
|
|
|
|
{
|
|
|
|
for(py = 0; py < (unsigned)pBitmap->rows; py++) // ignore_convention
|
|
|
|
for(px = 0; px < (unsigned)pBitmap->width; px++) // ignore_convention
|
|
|
|
{
|
|
|
|
if(pBitmap->buffer[py*pBitmap->pitch+px/8]&(1<<(7-(px%8)))) // ignore_convention
|
|
|
|
ms_aGlyphData[(py)*SlotW+px] = 255;
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
|
|
|
Graphics()->LoadTextureRawSub(TextureID, x + WidthLastChars, y, SlotW, SlotH, CImageInfo::FORMAT_ALPHA, ms_aGlyphData);
|
|
|
|
WidthLastChars += (SlotW + 1);
|
2017-09-12 18:10:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pCurrent = pTmp;
|
|
|
|
}
|
|
|
|
}
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
IEngineTextRender *CreateEngineTextRender() { return new CTextRender; }
|