Hash :
cc4f28ba
Author :
Date :
2010-05-23T00:34:49
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
/*
* FTGL - OpenGL font library
*
* Copyright (c) 2001-2004 Henry Maddocks <ftgl@opengl.geek.nz>
* Copyright (c) 2008 Sam Hocevar <sam@hocevar.net>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <cassert>
#include <string> // For memset
#include "FTGL/ftgl.h"
#include "FTInternals.h"
#include "../FTGlyph/FTTextureGlyphImpl.h"
#include "./FTTextureFontImpl.h"
//
// FTTextureFont
//
FTTextureFont::FTTextureFont(char const *fontFilePath) :
FTFont(new FTTextureFontImpl(this, fontFilePath))
{}
FTTextureFont::FTTextureFont(const unsigned char *pBufferBytes,
size_t bufferSizeInBytes) :
FTFont(new FTTextureFontImpl(this, pBufferBytes, bufferSizeInBytes))
{}
FTTextureFont::~FTTextureFont()
{}
FTGlyph* FTTextureFont::MakeGlyph(FT_GlyphSlot ftGlyph)
{
FTTextureFontImpl *myimpl = dynamic_cast<FTTextureFontImpl *>(impl);
if(!myimpl)
{
return NULL;
}
return myimpl->MakeGlyphImpl(ftGlyph);
}
//
// FTTextureFontImpl
//
static inline GLuint ClampSize(GLuint in, GLuint maxTextureSize)
{
// Find next power of two
--in;
in |= in >> 16;
in |= in >> 8;
in |= in >> 4;
in |= in >> 2;
in |= in >> 1;
++in;
// Clamp to max texture size
return in < maxTextureSize ? in : maxTextureSize;
}
FTTextureFontImpl::FTTextureFontImpl(FTFont *ftFont, const char* fontFilePath)
: FTFontImpl(ftFont, fontFilePath),
maximumGLTextureSize(0),
textureWidth(0),
textureHeight(0),
glyphHeight(0),
glyphWidth(0),
padding(3),
xOffset(0),
yOffset(0)
{
load_flags = FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP;
remGlyphs = numGlyphs = face.GlyphCount();
}
FTTextureFontImpl::FTTextureFontImpl(FTFont *ftFont,
const unsigned char *pBufferBytes,
size_t bufferSizeInBytes)
: FTFontImpl(ftFont, pBufferBytes, bufferSizeInBytes),
maximumGLTextureSize(0),
textureWidth(0),
textureHeight(0),
glyphHeight(0),
glyphWidth(0),
padding(3),
xOffset(0),
yOffset(0)
{
load_flags = FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP;
remGlyphs = numGlyphs = face.GlyphCount();
}
FTTextureFontImpl::~FTTextureFontImpl()
{
if(textureIDList.size())
{
glDeleteTextures((GLsizei)textureIDList.size(),
(const GLuint*)&textureIDList[0]);
}
}
FTGlyph* FTTextureFontImpl::MakeGlyphImpl(FT_GlyphSlot ftGlyph)
{
glyphHeight = static_cast<int>(charSize.Height() + 0.5f);
glyphWidth = static_cast<int>(charSize.Width() + 0.5f);
if(glyphHeight < 1) glyphHeight = 1;
if(glyphWidth < 1) glyphWidth = 1;
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().Upper().X() - tempGlyph->BBox().Lower().X() + padding + 0.5);
--remGlyphs;
return tempGlyph;
}
void FTTextureFontImpl::CalculateTextureSize()
{
if(!maximumGLTextureSize)
{
maximumGLTextureSize = 1024;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint*)&maximumGLTextureSize);
assert(maximumGLTextureSize); // Indicates an invalid OpenGL context
}
// Texture width required for numGlyphs glyphs. Will probably not be
// large enough, but we try to fit as many glyphs in one line as possible
textureWidth = ClampSize(glyphWidth * numGlyphs + padding * 2,
maximumGLTextureSize);
// Number of lines required for that many glyphs in a line
int tmp = (textureWidth - (padding * 2)) / glyphWidth;
tmp = tmp > 0 ? tmp : 1;
tmp = (numGlyphs + (tmp - 1)) / tmp; // round division up
// Texture height required for tmp lines of glyphs
textureHeight = ClampSize(glyphHeight * tmp + padding * 2,
maximumGLTextureSize);
}
GLuint FTTextureFontImpl::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 FTTextureFontImpl::FaceSize(const unsigned int size, const unsigned int res)
{
if(!textureIDList.empty())
{
glDeleteTextures((GLsizei)textureIDList.size(), (const GLuint*)&textureIDList[0]);
textureIDList.clear();
remGlyphs = numGlyphs = face.GlyphCount();
}
return FTFontImpl::FaceSize(size, res);
}
template <typename T>
inline FTPoint FTTextureFontImpl::RenderI(const T* string, const int len,
FTPoint position, FTPoint spacing,
int renderMode)
{
// Protect GL_TEXTURE_2D
glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TEXTURE_ENV_MODE);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
FTTextureGlyphImpl::ResetActiveTexture();
FTPoint tmp = FTFontImpl::Render(string, len,
position, spacing, renderMode);
glPopAttrib();
return tmp;
}
FTPoint FTTextureFontImpl::Render(const char * string, const int len,
FTPoint position, FTPoint spacing,
int renderMode)
{
return RenderI(string, len, position, spacing, renderMode);
}
FTPoint FTTextureFontImpl::Render(const wchar_t * string, const int len,
FTPoint position, FTPoint spacing,
int renderMode)
{
return RenderI(string, len, position, spacing, renderMode);
}