File: GLTTPixmapFont.C

package info (click to toggle)
gltt 2.5-1.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 964 kB
  • ctags: 510
  • sloc: sh: 7,190; cpp: 4,845; makefile: 165
file content (253 lines) | stat: -rw-r--r-- 6,527 bytes parent folder | download | duplicates (2)
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
/*
 * gltt graphics library
 * Copyright (C) 1998-1999 Stephane Rehel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "GLTTPixmapFont.h"

#include "FTPixmapFont.h"
#include "FTInstance.h"
#include "FTGlyph.h"
#include "FTGlyphPixmap.h"

#ifdef WIN32
#include <windows.h>
#endif

#ifdef LOCAL_GL_HEADER
  #include "local_gl.h"
#else
  #include <GL/gl.h>
#endif

/////////////////////////////////////////////////////////////////////////////

GLTTPixmapFont::GLTTPixmapFont( FTFace* _face )
{
  face= _face;

  instance= 0;

  pixmaps= 0;
  pixmaps= 0;
}

/////////////////////////////////////////////////////////////////////////////

GLTTPixmapFont::~GLTTPixmapFont()
{
  destroy();

  face= 0;
}

/////////////////////////////////////////////////////////////////////////////

void GLTTPixmapFont::destroy()
{
  delete pixmaps;
  pixmaps= 0;

  delete pixmaps;
  pixmaps= 0;

  delete instance;
  instance= 0;
}

/////////////////////////////////////////////////////////////////////////////

GLTTboolean GLTTPixmapFont::create( int point_size )
{
  destroy();

  if( point_size < 1 )
    point_size= 1;

  instance= new FTInstance(face);

  if( ! instance->create() )
    return GLTT_FALSE;

  if( ! instance->setResolutions(96,96) )
    return GLTT_FALSE;

  if( ! instance->setPointSize(point_size) )
    return GLTT_FALSE;

  pixmaps= new FTPixmapFont(instance);

  if( ! pixmaps->create() )
    return GLTT_FALSE;

  return GLTT_TRUE;
}

/////////////////////////////////////////////////////////////////////////////

void GLTTPixmapFont::output( int x, int y, const char* text )
{
  if( text == 0 || pixmaps == 0 )
    return;

  glRasterPos2i( x, y );

  GLboolean position_valid;
  glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &position_valid);
  if( !position_valid )
    {
    glRasterPos2i(0,0);

    glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &position_valid);
    if( !position_valid )
      return;

    glBitmap(0, 0, 0, 0, x, y, (const GLubyte *)0);
    }

  output(text);
}

/////////////////////////////////////////////////////////////////////////////

void GLTTPixmapFont::output( const char* text )
{
  if( text == 0 || pixmaps == 0 )
    return;

  GLfloat color[4];
  glGetFloatv( GL_CURRENT_COLOR, color );
  unsigned char r= (unsigned char)(color[0] * 255.);
  unsigned char g= (unsigned char)(color[1] * 255.);
  unsigned char b= (unsigned char)(color[2] * 255.);
  unsigned char a= (unsigned char)(color[3] * 255.);

  GLint swapbytes, lsbfirst, rowlength;
  GLint skiprows, skippixels, alignment;

  // Save the current packing mode for bitmaps.
  glGetIntegerv( GL_UNPACK_SWAP_BYTES, &swapbytes );
  glGetIntegerv( GL_UNPACK_LSB_FIRST, &lsbfirst );
  glGetIntegerv( GL_UNPACK_ROW_LENGTH, &rowlength );
  glGetIntegerv( GL_UNPACK_SKIP_ROWS, &skiprows );
  glGetIntegerv( GL_UNPACK_SKIP_PIXELS, &skippixels );
  glGetIntegerv( GL_UNPACK_ALIGNMENT, &alignment );

  // Enforce a standard packing mode
  glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE );
  glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE );
  glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
  glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );

  glPushAttrib(GL_ENABLE_BIT);
  glPushAttrib(GL_PIXEL_MODE_BIT);

  glPixelZoom(1.,1.);
  glPixelTransferf( GL_RED_SCALE,   1. );
  glPixelTransferf( GL_GREEN_SCALE, 1. );
  glPixelTransferf( GL_BLUE_SCALE,  1. );
  glPixelTransferf( GL_ALPHA_SCALE, 1. );
  glPixelTransferf( GL_RED_BIAS,    0. );
  glPixelTransferf( GL_GREEN_BIAS,  0. );
  glPixelTransferf( GL_BLUE_BIAS,   0. );
  glPixelTransferf( GL_ALPHA_BIAS,  0. );

//  glPushAttrib(GL_COLOR_BUFFER_BIT); // for glEnable(GL_ALPHA_TEST)
//  glEnable(GL_ALPHA_TEST);
//  glAlphaFunc( GL_GEQUAL, 0.1 );
  glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );

  for(;;)
    {
    int ch= (unsigned char)*text;
    if( ch == 0 )
      break;
    ++text;

    FTGlyphPixmap* gpixmap= pixmaps->getPixmap(ch);
    if( gpixmap == 0 )
      continue;

    unsigned char* data= gpixmap->getPixmap(r,g,b,a);
    glBitmap( 0, 0,
              0.0, 0.0,
              GLfloat(gpixmap->getDeltaX())/64.0,
              GLfloat(gpixmap->getDeltaY())/64.0,
              (const GLubyte *)0 );
    if( data != 0 )
      {
      glPixelStorei( GL_UNPACK_ROW_LENGTH,
                     gpixmap->getPixmapAllocatedWidth() );

      glDrawPixels( gpixmap->getPixmapWidth(),
                    gpixmap->getPixmapHeight(),
                    GL_RGBA,
                    GL_UNSIGNED_BYTE,
                    (const GLvoid*) data );
      }

    glBitmap( 0, 0,
              0.0, 0.0,
              GLfloat(gpixmap->getAdvance()-gpixmap->getDeltaX())/64.0,
              GLfloat(gpixmap->getDeltaY())/-64.0,
              (const GLubyte *)0 );
    }

//  glPopAttrib(); // GL_COLOR_BUFFER_BIT
  glPopAttrib(); // GL_PIXEL_MODE_BIT
  glPopAttrib(); // GL_ENABLE_BIT

  // Restore saved packing modes.
  glPixelStorei( GL_UNPACK_SWAP_BYTES, swapbytes );
  glPixelStorei( GL_UNPACK_LSB_FIRST, lsbfirst );
  glPixelStorei( GL_UNPACK_ROW_LENGTH, rowlength );
  glPixelStorei( GL_UNPACK_SKIP_ROWS, skiprows );
  glPixelStorei( GL_UNPACK_SKIP_PIXELS, skippixels );
  glPixelStorei( GL_UNPACK_ALIGNMENT, alignment );
}

/////////////////////////////////////////////////////////////////////////////

int GLTTPixmapFont::getWidth( const char* text )
{
  if( pixmaps == 0 )
    return 0;

  return pixmaps->getWidth(text);
}

/////////////////////////////////////////////////////////////////////////////

int GLTTPixmapFont::getHeight() const
{
  if( instance == 0 )
    return 0;

  return instance->getHeight();
}

/////////////////////////////////////////////////////////////////////////////

int GLTTPixmapFont::getDescender() const
{
  if( instance == 0 )
    return 0;

  return instance->getDescender();
}

/////////////////////////////////////////////////////////////////////////////