3
* Copyright (C) 2003-2004, Alexander Zaprjagaev <frustum@frustum.org>
4
* Modified by Abdul Bezrati <abezrati@hotmail.com>
5
* Bug fixed by Frederic MARTIN <martin-frederic@users.sourceforge.net>
15
memset(spaces, 0, 256);
21
glDeleteLists(fontBaseRange, 256);
24
fontTexture.destroy();
27
bool CFont::load(const char* fontPath)
33
if(!image.load(fontPath))
34
return Logger::writeErrorLog("Can't load font file");
36
if(image.getComponentsCount() != 4)
37
return Logger::writeErrorLog("RGBA image required for font");
39
if(!fontTexture.load2DImage(image, GL_CLAMP, GL_CLAMP, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true))
40
return Logger::writeErrorLog("Can't create font texture");
42
unsigned char *data = (unsigned char*)image.getDataBuffer();
44
height = image.getHeight();
45
width = image.getWidth();
48
memset(spacey, 0, 256);
54
for(y = 0, i = 0; y < 16; y++)
56
for(x = 0; x < 16; x++, i++)
60
for(j = 0; j < step; j++)
62
ptr = data + (size * y * step + x * step + j) * 4;
63
for(k = 0; k < step; k++)
65
if(*(ptr + 3) != 0) break;
74
for(j = step - 1; j >= 0; j--)
76
ptr = data + (size * y * step + x * step + j) * 4;
77
for(k = 0; k < step; k++) {
78
if(*(ptr + 3) != 0) break;
84
spaces[i] = step - spacey[i] - spaces[i];
89
glDeleteLists(fontBaseRange, 256);
91
fontTexture.activate();
92
fontBaseRange = glGenLists(256);
94
const unsigned char spacePos=' ';
95
spaces[spacePos] = step/3;
97
for(y = 0, i = 0; y < 16; y++)
99
for(x = 0; x < 16; x++, i++)
101
float s = (float)x / 16.0f + (float)spacey[i] / (float)size;
102
float t = (float)y / 16.0f;
103
float ds = (float)spaces[i] / (float)size;
104
float dt = 1.0f/ 16.0;
106
glNewList(fontBaseRange + i,GL_COMPILE);
107
glBegin(GL_TRIANGLE_STRIP);
108
glTexCoord2f(s + ds,t + dt); glVertex2i(spaces[i],step);
109
glTexCoord2f(s + ds,t ); glVertex2i(spaces[i], 0);
110
glTexCoord2f(s ,t + dt); glVertex2i(0 ,step);
111
glTexCoord2f(s ,t ); glVertex2i(0 , 0);
113
glTranslatef(float(spaces[i]),0,0);
117
fontTexture.deactivate();
118
fontHeight = height/16;
125
Tuple2i CFont::getStringDimensions(const std::string &string)
127
Tuple2i dimensions(0, fontHeight);
129
for(size_t i = 0; i < string.size(); i++)
131
// VERY IMPORTANT : unsigned char and not anything else !!!
132
// else you will have some beautiful bugs with special characters
133
dimensions.x += spaces[(unsigned char)(string[i])];
138
int CFont::getMaxFittingLength(const std::string &string, int bounds)
143
if(!bounds || !string.size() || !fontTexture.getID())
146
for(size_t i = 0; i < string.size(); i++)
147
if(currentLength < bounds)
149
currentLength += spaces[int(string[i])];
155
return (currentLength < bounds) ? index : index - 1;
159
void CFont::printProportional(float widthRatio, float heightRatio,
160
float width, float height,
161
float r, float g, float b,
162
const std::string &string)
169
glGetIntegerv(GL_VIEWPORT, viewport);
171
Tuple2i dimensions = getStringDimensions(string);
173
float xPosition = (viewport.z - dimensions.x*width)*widthRatio,
174
yPosition = (viewport.w - dimensions.y*height)*heightRatio;
176
print(xPosition, yPosition, width, height, r, g, b, string);
179
void CFont::print(float xPosition, float yPosition,
180
float width, float height,
181
float r, float g, float b,
182
const std::string &string)
184
GLint stringLength = GLint(string.size());
185
if(!stringLength || !fontTexture.getID())
189
fontTexture.activate();
191
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
193
glMatrixMode(GL_MODELVIEW);
195
glTranslatef(xPosition, yPosition, 0);
197
glListBase(fontBaseRange);
198
glScalef(width, height,1.0f);
199
glCallLists(stringLength,GL_UNSIGNED_BYTE, string.c_str());
203
fontTexture.activate();
207
int CFont::getHeight()
212
void CFont::printSubString(float xPosition, float yPosition,
213
float width, float height,
214
float r, float g, float b,
216
const std::string &string)
221
int stringLength = int(string.size());
222
if(stringLength < end)
226
fontTexture.activate();
228
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
230
glMatrixMode(GL_MODELVIEW);
232
glTranslatef(xPosition, yPosition, 0);
234
glListBase(fontBaseRange);
235
glScalef(width, height,1.0f);
236
glCallLists(end - start,GL_UNSIGNED_BYTE, string.c_str() + start);
240
fontTexture.deactivate();
244
const int *CFont::getCharHorizontalGlyphs() const{ return spaces; }