1
#include <string> // For memset
3
#include "FTGLTextureFont.h"
4
#include "FTTextureGlyph.h"
7
inline GLuint NextPowerOf2( GLuint in)
21
FTGLTextureFont::FTGLTextureFont( const char* fontname)
32
remGlyphs = numGlyphs = face.GlyphCount();
36
FTGLTextureFont::FTGLTextureFont( const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
37
: FTFont( pBufferBytes, bufferSizeInBytes),
47
remGlyphs = numGlyphs = face.GlyphCount();
51
FTGLTextureFont::~FTGLTextureFont()
53
glDeleteTextures( textureIDList.size(), (const GLuint*)&textureIDList[0]);
57
FTGlyph* FTGLTextureFont::MakeGlyph( unsigned int glyphIndex)
59
FT_GlyphSlot ftGlyph = face.Glyph( glyphIndex, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP);
63
glyphHeight = static_cast<int>( charSize.Height());
64
glyphWidth = static_cast<int>( charSize.Width());
66
if( textureIDList.empty())
68
textureIDList.push_back( CreateTexture());
69
xOffset = yOffset = padding;
72
if( xOffset > ( textureWidth - glyphWidth))
75
yOffset += glyphHeight;
77
if( yOffset > ( textureHeight - glyphHeight))
79
textureIDList.push_back( CreateTexture());
84
FTTextureGlyph* tempGlyph = new FTTextureGlyph( ftGlyph, textureIDList[textureIDList.size() - 1],
85
xOffset, yOffset, textureWidth, textureHeight);
86
xOffset += static_cast<int>( tempGlyph->BBox().upperX - tempGlyph->BBox().lowerX + padding);
97
void FTGLTextureFont::CalculateTextureSize()
101
glGetIntegerv( GL_MAX_TEXTURE_SIZE, (GLint*)&maxTextSize);
104
textureWidth = NextPowerOf2( (remGlyphs * glyphWidth) + ( padding * 2));
105
if( textureWidth > maxTextSize)
107
textureWidth = maxTextSize;
110
int h = static_cast<int>( (textureWidth - ( padding * 2)) / glyphWidth);
112
textureHeight = NextPowerOf2( (( numGlyphs / h) + 1) * glyphHeight);
113
textureHeight = textureHeight > maxTextSize ? maxTextSize : textureHeight;
117
GLuint FTGLTextureFont::CreateTexture()
119
CalculateTextureSize();
121
int totalMemory = textureWidth * textureHeight;
122
unsigned char* textureMemory = new unsigned char[totalMemory];
123
memset( textureMemory, 0, totalMemory);
126
glGenTextures( 1, (GLuint*)&textID);
128
glBindTexture( GL_TEXTURE_2D, textID);
129
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
130
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
131
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
132
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
134
glTexImage2D( GL_TEXTURE_2D, 0, GL_ALPHA, textureWidth, textureHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, textureMemory);
136
delete [] textureMemory;
142
bool FTGLTextureFont::FaceSize( const unsigned int size, const unsigned int res)
144
if( !textureIDList.empty())
146
glDeleteTextures( textureIDList.size(), (const GLuint*)&textureIDList[0]);
147
remGlyphs = numGlyphs = face.GlyphCount();
150
return FTFont::FaceSize( size, res);
154
void FTGLTextureFont::Render( const char* string)
156
glPushAttrib( GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
159
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE
161
FTFont::Render( string);
167
void FTGLTextureFont::Render( const wchar_t* string)
169
glPushAttrib( GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
172
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE
174
FTFont::Render( string);