~ubuntu-branches/ubuntu/edgy/pouetchess/edgy-updates

« back to all changes in this revision

Viewing changes to src/sxmlgui/Font.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc (mr_pouit)
  • Date: 2006-07-15 15:45:57 UTC
  • Revision ID: james.westby@ubuntu.com-20060715154557-3paxq02hx4od0wm4
Tags: upstream-0.2.0
ImportĀ upstreamĀ versionĀ 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* CFont
 
2
 *
 
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>
 
6
 */
 
7
 
 
8
#include "Font.h"
 
9
#include "GenUtils.h"
 
10
 
 
11
CFont::CFont()
 
12
{
 
13
  fontBaseRange = 0;
 
14
  fontHeight    = 0;
 
15
  memset(spaces, 0, 256);
 
16
}
 
17
 
 
18
CFont::~CFont()
 
19
{
 
20
  if(fontBaseRange)
 
21
    glDeleteLists(fontBaseRange, 256);
 
22
 
 
23
  fontBaseRange = 0;
 
24
  fontTexture.destroy();
 
25
}
 
26
 
 
27
bool CFont::load(const char* fontPath)
 
28
{
 
29
  Image  image;
 
30
  int    width  = 0,
 
31
         height = 0;
 
32
 
 
33
  if(!image.load(fontPath))
 
34
    return Logger::writeErrorLog("Can't load font file");
 
35
 
 
36
  if(image.getComponentsCount() != 4)
 
37
    return Logger::writeErrorLog("RGBA image required for font");
 
38
 
 
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");
 
41
 
 
42
  unsigned char *data = (unsigned char*)image.getDataBuffer();
 
43
 
 
44
  height = image.getHeight();
 
45
  width  = image.getWidth();
 
46
 
 
47
  int spacey[256];
 
48
  memset(spacey, 0, 256);
 
49
 
 
50
  int size   = width,
 
51
      step   = size / 16;
 
52
 
 
53
  int x,y,i,j,k;
 
54
  for(y = 0, i = 0; y < 16; y++)
 
55
  {
 
56
    for(x = 0; x < 16; x++, i++)
 
57
    {
 
58
      unsigned char *ptr;
 
59
      spacey[i] = 0;
 
60
      for(j = 0; j < step; j++)
 
61
      {
 
62
        ptr = data + (size * y * step + x * step + j) * 4;
 
63
        for(k = 0; k < step; k++)
 
64
        {
 
65
          if(*(ptr + 3) != 0) break;
 
66
          ptr += size * 4;
 
67
        }
 
68
        if(k != step) break;
 
69
        spacey[i]++;
 
70
      }
 
71
      spaces[i] = 0;
 
72
      if(spacey[i] == step)
 
73
        continue;
 
74
      for(j = step - 1; j >= 0; j--)
 
75
      {
 
76
        ptr = data + (size * y * step + x * step + j) * 4;
 
77
        for(k = 0; k < step; k++) {
 
78
          if(*(ptr + 3) != 0) break;
 
79
          ptr += size * 4;
 
80
        }
 
81
        if(k != step) break;
 
82
        spaces[i]++;
 
83
      }
 
84
      spaces[i] = step - spacey[i] - spaces[i];
 
85
    }
 
86
  }
 
87
 
 
88
  if(fontBaseRange)
 
89
    glDeleteLists(fontBaseRange, 256);
 
90
 
 
91
  fontTexture.activate();
 
92
  fontBaseRange = glGenLists(256);
 
93
 
 
94
  const unsigned char spacePos=' ';
 
95
  spaces[spacePos] = step/3;
 
96
 
 
97
  for(y = 0, i = 0; y < 16; y++)
 
98
  {
 
99
    for(x = 0; x < 16; x++, i++)
 
100
    {
 
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;
 
105
  
 
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);
 
112
      glEnd();
 
113
      glTranslatef(float(spaces[i]),0,0);
 
114
      glEndList();
 
115
    }
 
116
  }
 
117
  fontTexture.deactivate();
 
118
  fontHeight = height/16;
 
119
  
 
120
 
 
121
  
 
122
  return true;
 
123
}
 
124
 
 
125
Tuple2i CFont::getStringDimensions(const std::string &string)
 
126
{
 
127
  Tuple2i dimensions(0, fontHeight);
 
128
 
 
129
  for(size_t i = 0; i < string.size(); i++)
 
130
  {
 
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])];
 
134
  }
 
135
  return dimensions;
 
136
}
 
137
 
 
138
int  CFont::getMaxFittingLength(const std::string &string, int bounds)
 
139
{
 
140
  int index         = 0,
 
141
      currentLength = 0;
 
142
 
 
143
  if(!bounds || !string.size() || !fontTexture.getID())
 
144
    return 0;
 
145
 
 
146
  for(size_t i = 0; i < string.size(); i++)
 
147
    if(currentLength < bounds)
 
148
    {
 
149
      currentLength += spaces[int(string[i])];
 
150
      index++;
 
151
    }
 
152
    else 
 
153
     break;
 
154
 
 
155
  return (currentLength < bounds) ? index : index - 1;
 
156
}
 
157
 
 
158
 
 
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)
 
163
{
 
164
  Tuple4i viewport;
 
165
 
 
166
  if(!string.size())
 
167
    return;
 
168
 
 
169
  glGetIntegerv(GL_VIEWPORT, viewport);
 
170
 
 
171
  Tuple2i dimensions = getStringDimensions(string);
 
172
 
 
173
  float xPosition = (viewport.z - dimensions.x*width)*widthRatio,
 
174
        yPosition = (viewport.w - dimensions.y*height)*heightRatio;
 
175
 
 
176
  print(xPosition, yPosition, width, height, r, g, b, string);
 
177
}
 
178
 
 
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)
 
183
{
 
184
  GLint stringLength =  GLint(string.size());
 
185
  if(!stringLength || !fontTexture.getID())
 
186
    return;
 
187
 
 
188
  glColor3f(r,g,b);
 
189
  fontTexture.activate();
 
190
  glEnable(GL_BLEND);
 
191
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
192
 
 
193
  glMatrixMode(GL_MODELVIEW);
 
194
  glPushMatrix();
 
195
  glTranslatef(xPosition, yPosition, 0);
 
196
 
 
197
  glListBase(fontBaseRange);
 
198
  glScalef(width, height,1.0f);
 
199
  glCallLists(stringLength,GL_UNSIGNED_BYTE, string.c_str());
 
200
  glPopMatrix();
 
201
  glDisable(GL_BLEND);
 
202
 
 
203
  fontTexture.activate();
 
204
  glColor3f(1,1,1);
 
205
}
 
206
 
 
207
int  CFont::getHeight()
 
208
{
 
209
  return fontHeight;
 
210
}
 
211
 
 
212
void CFont::printSubString(float xPosition,  float yPosition,
 
213
                          float width,      float height,
 
214
                          float r, float g, float b,
 
215
                          int   start, int end,
 
216
                          const std::string &string)
 
217
{
 
218
  if(start >= end)
 
219
    return;
 
220
 
 
221
  int stringLength =  int(string.size());
 
222
  if(stringLength  < end)
 
223
    return;
 
224
 
 
225
  glColor3f(r,g,b);
 
226
  fontTexture.activate();
 
227
  glEnable(GL_BLEND);
 
228
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
229
 
 
230
  glMatrixMode(GL_MODELVIEW);
 
231
  glPushMatrix();
 
232
  glTranslatef(xPosition, yPosition, 0);
 
233
 
 
234
  glListBase(fontBaseRange);
 
235
  glScalef(width, height,1.0f);
 
236
  glCallLists(end - start,GL_UNSIGNED_BYTE, string.c_str() + start);
 
237
  glPopMatrix();
 
238
  glDisable(GL_BLEND);
 
239
 
 
240
  fontTexture.deactivate();
 
241
  glColor3f(1,1,1);
 
242
}
 
243
 
 
244
const   int *CFont::getCharHorizontalGlyphs() const{ return spaces; }
 
245