Debian Codegraph @

3rdparty/fonts/fnt.h

raw symbols SCIP
1 /*
2 PLIB - A Suite of Portable Game Libraries
3 Copyright (C) 1998,2002 Steve Baker
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 For further information visit http://plib.sourceforge.net
20
21 $Id: fnt.h 1923 2004-04-06 12:53:17Z sjbaker $
22 */
23
24
25 #ifndef _FNT_H_
26 #define _FNT_H_ 1
27
28 #include <stdio.h>
29
30 #include <plib/sg.h>
31 #include <osg/GL>
32
33 #define FNTMAX_CHAR 256
34 #define FNT_TRUE 1
35 #define FNT_FALSE 0
36
37 class SGPath;
38
39 class fntFont
40 {
41 public:
42 fntFont () ;
43
44 virtual ~fntFont () ;
45
46 virtual void getBBox ( const char *s, float pointsize, float italic,
47 float *left, float *right,
48 float *bot , float *top ) = 0 ;
49 virtual void putch ( sgVec3 curpos, float pointsize, float italic, char c ) = 0 ;
50 virtual void puts ( sgVec3 curpos, float pointsize, float italic, const char *s ) = 0 ;
51 virtual void begin () = 0 ;
52 virtual void end () = 0 ;
53
54 virtual int load ( const SGPath& fname, GLenum mag = GL_NEAREST,
55 GLenum min = GL_LINEAR_MIPMAP_LINEAR ) = 0 ;
56
57 virtual void setFixedPitch ( int fix ) = 0 ;
58 virtual int isFixedPitch () const = 0 ;
59
60 virtual void setWidth ( float w ) = 0 ;
61 virtual void setGap ( float g ) = 0 ;
62
63 virtual float getWidth () const = 0 ;
64 virtual float getGap () const = 0 ;
65
66 virtual int hasGlyph ( char c ) const = 0 ;
67 } ;
68
69
70 class fntTexFont : public fntFont
71 {
72 private:
73 GLuint texture ;
74 int bound ;
75 int fixed_pitch ;
76
77 float width ; /* The width of a character in fixed-width mode */
78 float gap ; /* Set the gap between characters */
79
80 int exists [ FNTMAX_CHAR ] ; /* TRUE if character exists in tex-map */
81
82 /*
83 The quadrilaterals that describe the characters
84 in the font are drawn with the following texture
85 and spatial coordinates. The texture coordinates
86 are in (S,T) space, with (0,0) at the bottom left
87 of the image and (1,1) at the top-right.
88
89 The spatial coordinates are relative to the current
90 'cursor' position. They should be scaled such that
91 1.0 represent the height of a capital letter. Hence,
92 characters like 'y' which have a descender will be
93 given a negative v_bot. Most capitals will have
94 v_bot==0.0 and v_top==1.0.
95 */
96
97 /* Nominal baseline widths */
98
99 float widths [ FNTMAX_CHAR ] ;
100
101 /* Texture coordinates */
102
103 float t_top [ FNTMAX_CHAR ] ; /* Top edge of each character [0..1] */
104 float t_bot [ FNTMAX_CHAR ] ; /* Bottom edge of each character [0..1] */
105 float t_left [ FNTMAX_CHAR ] ; /* Left edge of each character [0..1] */
106 float t_right [ FNTMAX_CHAR ] ; /* Right edge of each character [0..1] */
107
108 /* Vertex coordinates. */
109
110 float v_top [ FNTMAX_CHAR ] ;
111 float v_bot [ FNTMAX_CHAR ] ;
112 float v_left [ FNTMAX_CHAR ] ;
113 float v_right [ FNTMAX_CHAR ] ;
114
115 void bind_texture ()
116 {
117 glEnable ( GL_TEXTURE_2D ) ;
118 #ifdef GL_VERSION_1_1
119 glBindTexture ( GL_TEXTURE_2D, texture ) ;
120 #else
121 /* For ancient SGI machines */
122 glBindTextureEXT ( GL_TEXTURE_2D, texture ) ;
123 #endif
124 }
125
126 float low_putch ( sgVec3 curpos, float pointsize, float italic, char c ) ;
127
128 int loadTXF ( const SGPath& path, GLenum mag = GL_NEAREST,
129 GLenum min = GL_LINEAR_MIPMAP_LINEAR ) ;
130 public:
131
132 fntTexFont ()
133 {
134 bound = FNT_FALSE ;
135 fixed_pitch = FNT_TRUE ;
136 texture = 0 ;
137 width = 1.0f ;
138 gap = 0.1f ;
139
140 memset ( exists, FNT_FALSE, FNTMAX_CHAR * sizeof(int) ) ;
141 }
142
143 fntTexFont ( const SGPath& path, GLenum mag = GL_NEAREST,
144 GLenum min = GL_LINEAR_MIPMAP_LINEAR ) : fntFont ()
145 {
146 bound = FNT_FALSE ;
147 fixed_pitch = FNT_TRUE ;
148 texture = 0 ;
149 width = 1.0f ;
150 gap = 0.1f ;
151
152 memset ( exists, FNT_FALSE, FNTMAX_CHAR * sizeof(int) ) ;
153 load ( path, mag, min ) ;
154 }
155
156 ~fntTexFont ()
157 {
158 if ( texture != 0 )
159 {
160 #ifdef GL_VERSION_1_1
161 glDeleteTextures ( 1, &texture ) ;
162 #else
163 /* For ancient SGI machines */
164 glDeleteTexturesEXT ( 1, &texture ) ;
165 #endif
166 }
167 }
168
169 int load ( const SGPath& path, GLenum mag = GL_NEAREST,
170 GLenum min = GL_LINEAR_MIPMAP_LINEAR ) override ;
171
172 void setFixedPitch ( int fix ) { fixed_pitch = fix ; }
173 int isFixedPitch () const { return fixed_pitch ; }
174
175 void setWidth ( float w ) { width = w ; }
176 void setGap ( float g ) { gap = g ; }
177
178 float getWidth () const { return width ; }
179 float getGap () const { return gap ; }
180
181
182 void setGlyph ( char c, float wid,
183 float tex_left, float tex_right,
184 float tex_bot , float tex_top ,
185 float vtx_left, float vtx_right,
186 float vtx_bot , float vtx_top ) ;
187 void setGlyph ( char c,
188 float tex_left, float tex_right,
189 float tex_bot , float tex_top ,
190 float vtx_left, float vtx_right,
191 float vtx_bot , float vtx_top ) /* deprecated */
192 {
193 setGlyph ( c, vtx_right,
194 tex_left, tex_right, tex_bot, tex_top,
195 vtx_left, vtx_right, vtx_bot, vtx_top ) ;
196 }
197
198 int getGlyph ( char c, float* wid,
199 float *tex_left = NULL, float *tex_right = NULL,
200 float *tex_bot = NULL, float *tex_top = NULL,
201 float *vtx_left = NULL, float *vtx_right = NULL,
202 float *vtx_bot = NULL, float *vtx_top = NULL) ;
203 int getGlyph ( char c,
204 float *tex_left = NULL, float *tex_right = NULL,
205 float *tex_bot = NULL, float *tex_top = NULL,
206 float *vtx_left = NULL, float *vtx_right = NULL,
207 float *vtx_bot = NULL, float *vtx_top = NULL) /* deprecated */
208 {
209 return getGlyph ( c, NULL,
210 tex_left, tex_right, tex_bot, tex_top,
211 vtx_left, vtx_right, vtx_bot, vtx_top ) ;
212 }
213
214 int hasGlyph ( char c ) const { return exists[ (GLubyte) c ] ; }
215
216 void getBBox ( const char *s, float pointsize, float italic,
217 float *left, float *right,
218 float *bot , float *top ) ;
219
220 void begin ()
221 {
222 bind_texture () ;
223 bound = FNT_TRUE ;
224 }
225
226 void end ()
227 {
228 bound = FNT_FALSE ;
229 }
230
231 void puts ( sgVec3 curpos, float pointsize, float italic, const char *s ) ;
232
233 void putch ( sgVec3 curpos, float pointsize, float italic, char c )
234 {
235 if ( ! bound )
236 bind_texture () ;
237
238 low_putch ( curpos, pointsize, italic, c ) ;
239 }
240
241 } ;
242
243
244
245 class fntBitmapFont : public fntFont
246 {
247 protected:
248
249 const GLubyte **data;
250 int first;
251 int count;
252 int height;
253 float xorig, yorig;
254 int fix;
255 float wid, gap;
256
257 public:
258
259 // data is a null-terminated list of glyph images:
260 // data[i][0] - image width
261 // data[i][1..n] - packed bitmap
262
263 fntBitmapFont ( const GLubyte **data, int first, int height,
264 float xorig, float yorig ) ;
265
266 virtual ~fntBitmapFont () ;
267
268 virtual void getBBox ( const char *s, float pointsize, float italic,
269 float *left, float *right,
270 float *bot , float *top ) ;
271
272 virtual void putch ( sgVec3 curpos, float pointsize, float italic, char c ) ;
273 virtual void puts ( sgVec3 curpos, float pointsize, float italic, const char *s ) ;
274
275 virtual void begin () ;
276 virtual void end () ;
277
278 virtual int load ( const SGPath& fname, GLenum mag, GLenum min ) override { return -1; }
279
280 virtual void setFixedPitch ( int f ) { fix = f; }
281 virtual int isFixedPitch () const { return fix; }
282
283 virtual void setWidth ( float w ) { wid = w; }
284 virtual void setGap ( float g ) { gap = g; }
285
286 virtual float getWidth () const { return wid; }
287 virtual float getGap () const { return gap; }
288
289 virtual int hasGlyph ( char c ) const ;
290 };
291
292
293 /* Builtin Bitmap Fonts */
294
295 #define FNT_BITMAP_8_BY_13 0
296 #define FNT_BITMAP_9_BY_15 1
297 #define FNT_BITMAP_HELVETICA_10 2
298 #define FNT_BITMAP_HELVETICA_12 3
299 #define FNT_BITMAP_HELVETICA_18 4
300 #define FNT_BITMAP_TIMES_ROMAN_10 5
301 #define FNT_BITMAP_TIMES_ROMAN_24 6
302
303 fntBitmapFont *fntGetBitmapFont(int id);
304
305
306
307 class fntRenderer
308 {
309 fntFont *font ;
310
311 sgVec3 curpos ;
312
313 float pointsize ;
314 float italic ;
315
316 public:
317 fntRenderer ()
318 {
319 start2f ( 0.0f, 0.0f ) ;
320 font = NULL ;
321 pointsize = 10 ;
322 italic = 0 ;
323 }
324
325 void start3fv ( sgVec3 pos ) { sgCopyVec3 ( curpos, pos ) ; }
326 void start2fv ( sgVec2 pos ) { sgCopyVec2 ( curpos, pos ) ; curpos[2]=0.0f ; }
327 void start2f ( float x, float y ) { sgSetVec3 ( curpos, x, y, 0.0f ) ; }
328 void start3f ( float x, float y, float z ) { sgSetVec3 ( curpos, x, y, z ) ; }
329
330 void getCursor ( float *x, float *y, float *z ) const
331 {
332 if ( x != NULL ) *x = curpos [ 0 ] ;
333 if ( y != NULL ) *y = curpos [ 1 ] ;
334 if ( z != NULL ) *z = curpos [ 2 ] ;
335 }
336
337 void setFont ( fntFont *f ) { font = f ; }
338 fntFont *getFont () const { return font ; }
339
340 void setSlant ( float i ) { italic = i ; }
341 void setPointSize ( float p ) { pointsize = p ; }
342
343 float getSlant () const { return italic ; }
344 float getPointSize () const { return pointsize ; }
345
346 void begin () { font->begin () ; }
347 void end () { font->end () ; }
348
349 void putch ( char c ) { font->putch ( curpos, pointsize, italic, c ) ; }
350 void puts ( const char *s ) { font->puts ( curpos, pointsize, italic, s ) ; }
351 } ;
352
353
354 void fntInit () ;
355
356 #endif
357