Hash :
ee107630
Author :
Date :
2008-03-03T16:41:35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
#include <cassert>
#include <string> // For memset
#include "FTGLTextureFont.h"
#include "FTTextureGlyph.h"
inline GLuint NextPowerOf2( GLuint in)
{
in -= 1;
in |= in >> 16;
in |= in >> 8;
in |= in >> 4;
in |= in >> 2;
in |= in >> 1;
return in + 1;
}
FTGLTextureFont::FTGLTextureFont( const char* fontFilePath)
: FTFont( fontFilePath),
maximumGLTextureSize(0),
textureWidth(0),
textureHeight(0),
glyphHeight(0),
glyphWidth(0),
padding(3),
xOffset(0),
yOffset(0)
{
remGlyphs = numGlyphs = face.GlyphCount();
}
FTGLTextureFont::FTGLTextureFont( const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
: FTFont( pBufferBytes, bufferSizeInBytes),
maximumGLTextureSize(0),
textureWidth(0),
textureHeight(0),
glyphHeight(0),
glyphWidth(0),
padding(3),
xOffset(0),
yOffset(0)
{
remGlyphs = numGlyphs = face.GlyphCount();
}
FTGLTextureFont::~FTGLTextureFont()
{
glDeleteTextures( textureIDList.size(), (const GLuint*)&textureIDList[0]);
}
FTGlyph* FTGLTextureFont::MakeGlyph( unsigned int glyphIndex)
{
FT_GlyphSlot ftGlyph = face.Glyph( glyphIndex, FT_LOAD_NO_HINTING);
if( ftGlyph)
{
glyphHeight = static_cast<int>( charSize.Height());
glyphWidth = static_cast<int>( charSize.Width());
if( textureIDList.empty())
{
textureIDList.push_back( CreateTexture());
xOffset = yOffset = padding;
}
if( xOffset > ( textureWidth - glyphWidth))
{
xOffset = padding;
yOffset += glyphHeight;
if( yOffset > ( textureHeight - glyphHeight))
{
textureIDList.push_back( CreateTexture());
yOffset = padding;
}
}
FTTextureGlyph* tempGlyph = new FTTextureGlyph( ftGlyph, textureIDList[textureIDList.size() - 1],
xOffset, yOffset, textureWidth, textureHeight);
xOffset += static_cast<int>( tempGlyph->BBox().upperX - tempGlyph->BBox().lowerX + padding);
--remGlyphs;
return tempGlyph;
}
err = face.Error();
return NULL;
}
void FTGLTextureFont::CalculateTextureSize()
{
if( !maximumGLTextureSize)
{
glGetIntegerv( GL_MAX_TEXTURE_SIZE, (GLint*)&maximumGLTextureSize);
assert(maximumGLTextureSize); // If you hit this then you have an invalid OpenGL context.
}
textureWidth = NextPowerOf2( (remGlyphs * glyphWidth) + ( padding * 2));
textureWidth = textureWidth > maximumGLTextureSize ? maximumGLTextureSize : textureWidth;
int h = static_cast<int>( (textureWidth - ( padding * 2)) / glyphWidth);
textureHeight = NextPowerOf2( (( numGlyphs / h) + 1) * glyphHeight);
textureHeight = textureHeight > maximumGLTextureSize ? maximumGLTextureSize : textureHeight;
}
GLuint FTGLTextureFont::CreateTexture()
{
CalculateTextureSize();
int totalMemory = textureWidth * textureHeight;
unsigned char* textureMemory = new unsigned char[totalMemory];
memset( textureMemory, 0, totalMemory);
GLuint textID;
glGenTextures( 1, (GLuint*)&textID);
glBindTexture( GL_TEXTURE_2D, textID);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D( GL_TEXTURE_2D, 0, GL_ALPHA, textureWidth, textureHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, textureMemory);
delete [] textureMemory;
return textID;
}
bool FTGLTextureFont::FaceSize( const unsigned int size, const unsigned int res)
{
if( !textureIDList.empty())
{
glDeleteTextures( textureIDList.size(), (const GLuint*)&textureIDList[0]);
textureIDList.clear();
remGlyphs = numGlyphs = face.GlyphCount();
}
return FTFont::FaceSize( size, res);
}
void FTGLTextureFont::Render( const char* string)
{
glPushAttrib( GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE
FTTextureGlyph::ResetActiveTexture();
FTFont::Render( string);
glPopAttrib();
}
void FTGLTextureFont::Render( const wchar_t* string)
{
glPushAttrib( GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE
FTTextureGlyph::ResetActiveTexture();
FTFont::Render( string);
glPopAttrib();
}