4 * Bitmap and stroke fonts displaying.
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8 * Creation date: Thu Dec 16 1999
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 #define G_LOG_DOMAIN "freeglut-font"
34 #include "../include/GL/freeglut.h"
35 #include "freeglut_internal.h"
38 * TODO BEFORE THE STABLE RELEASE:
43 /* -- IMPORT DECLARATIONS -------------------------------------------------- */
46 * These are the font faces defined in freeglut_font_data.c file:
48 extern SFG_Font fgFontFixed8x13;
49 extern SFG_Font fgFontFixed9x15;
50 extern SFG_Font fgFontHelvetica10;
51 extern SFG_Font fgFontHelvetica12;
52 extern SFG_Font fgFontHelvetica18;
53 extern SFG_Font fgFontTimesRoman10;
54 extern SFG_Font fgFontTimesRoman24;
55 extern SFG_StrokeFont fgStrokeRoman;
56 extern SFG_StrokeFont fgStrokeMonoRoman;
59 * This is for GLUT binary compatibility, as suggested by Steve Baker
61 #if TARGET_HOST_UNIX_X11
62 struct _GLUTstrokeFont {
70 struct _GLUTbitmapFont {
77 struct _GLUTstrokeFont glutStrokeRoman;
78 struct _GLUTstrokeFont glutStrokeMonoRoman;
79 struct _GLUTbitmapFont glutBitmap9By15;
80 struct _GLUTbitmapFont glutBitmap8By13;
81 struct _GLUTbitmapFont glutBitmapTimesRoman10;
82 struct _GLUTbitmapFont glutBitmapTimesRoman24;
83 struct _GLUTbitmapFont glutBitmapHelvetica10;
84 struct _GLUTbitmapFont glutBitmapHelvetica12;
85 struct _GLUTbitmapFont glutBitmapHelvetica18;
89 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
92 * Matches a font ID with a SFG_Font structure pointer.
93 * This was changed to match the GLUT header style.
95 static SFG_Font* fghFontByID( void* font )
98 * Try matching the font ID and the font data structure
100 if( font == GLUT_BITMAP_8_BY_13 ) return( &fgFontFixed8x13 );
101 if( font == GLUT_BITMAP_9_BY_15 ) return( &fgFontFixed9x15 );
102 if( font == GLUT_BITMAP_HELVETICA_10 ) return( &fgFontHelvetica10 );
103 if( font == GLUT_BITMAP_HELVETICA_12 ) return( &fgFontHelvetica12 );
104 if( font == GLUT_BITMAP_HELVETICA_18 ) return( &fgFontHelvetica18 );
105 if( font == GLUT_BITMAP_TIMES_ROMAN_10 ) return( &fgFontTimesRoman10 );
106 if( font == GLUT_BITMAP_TIMES_ROMAN_24 ) return( &fgFontTimesRoman24 );
109 * This probably is the library user's fault
111 fgError( "font 0x%08x not found", font );
117 * Matches a font ID with a SFG_StrokeFont structure pointer.
118 * This was changed to match the GLUT header style.
120 static SFG_StrokeFont* fghStrokeByID( void* font )
123 * Try matching the font ID and the font data structure
125 if( font == GLUT_STROKE_ROMAN ) return( &fgStrokeRoman );
126 if( font == GLUT_STROKE_MONO_ROMAN ) return( &fgStrokeMonoRoman );
129 * This probably is the library user's fault
131 fgError( "stroke font 0x%08x not found", font );
137 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
140 * Draw a bitmap character
142 void FGAPIENTRY glutBitmapCharacter( void* fontID, int character )
147 * First of all we'll need a font to use
149 SFG_Font* font = fghFontByID( fontID );
152 * Make sure the character we want to output is valid
154 freeglut_return_if_fail( character >= 0 && character < 256 );
157 * Then find the character we want to draw
159 face = font->Characters[ character - 1 ];
162 * Save the old pixel store settings
164 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
167 * Set up the pixel unpacking ways
169 glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE );
170 glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE );
171 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
172 glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
173 glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
174 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
177 * We'll use a glBitmap call to draw the font.
180 face[ 0 ], font->Height, /* The bitmap's width and height */
181 font->xorig, font->yorig, /* The origin -- what on earth? */
182 (float)(face[ 0 ]), 0.0, /* The raster advance -- inc. x */
183 (face + 1) /* The packed bitmap data... */
187 * Restore the old pixel store settings
192 void FGAPIENTRY glutBitmapString( void* fontID, const char *string )
195 int numchar = strlen ( string ) ;
198 * First of all we'll need a font to use
200 SFG_Font* font = fghFontByID( fontID );
202 float raster_position[4] ;
203 glGetFloatv ( GL_CURRENT_RASTER_POSITION, raster_position ) ;
206 * Save the old pixel store settings
208 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
211 * Set up the pixel unpacking ways
213 glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE );
214 glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE );
215 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
216 glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
217 glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
218 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
221 * Step through the string, drawing each character.
222 * A carriage return will simply translate the next character's insertion point back to the
223 * start of the line and down one line.
225 for( c = 0; c < numchar; c++ )
227 if ( string[c] == '\n' )
229 raster_position[1] -= (float)font->Height ;
230 glRasterPos4fv ( raster_position ) ;
232 else /* Not a carriage return, draw the bitmap character */
234 const GLubyte* face = font->Characters[ string[ c ] - 1 ] ;
237 * We'll use a glBitmap call to draw the font.
240 face[ 0 ], font->Height, /* The bitmap's width and height */
241 font->xorig, font->yorig, /* The origin -- what on earth? */
242 (float)(face[ 0 ]), 0.0, /* The raster advance -- inc. x */
243 (face + 1) /* The packed bitmap data... */
249 * Restore the old pixel store settings
255 * Returns the width in pixels of a font's character
257 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
260 * First of all, grab the font we need
262 SFG_Font* font = fghFontByID( fontID );
265 * Make sure the character we want to output is valid
267 freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
270 * Scan the font looking for the specified character
272 return( *(font->Characters[ character - 1 ]) );
276 * Return the width of a string drawn using a bitmap font
278 int FGAPIENTRY glutBitmapLength( void* fontID, const char* string )
280 int c, length = 0, this_line_length = 0;
283 * First of all, grab the font we need
285 SFG_Font* font = fghFontByID( fontID );
288 * Step through the characters in the string, adding up the width of each one
290 int numchar = strlen ( string ) ;
291 for( c = 0; c < numchar; c++ )
293 if ( string[ c ] == '\n' ) /* Carriage return, reset the length of this line */
295 if ( length < this_line_length ) length = this_line_length ;
296 this_line_length = 0 ;
298 else /* Not a carriage return, increment the length of this line */
299 this_line_length += *(font->Characters[ string[ c ] - 1 ]) ;
302 if ( length < this_line_length ) length = this_line_length ;
305 * Return the result now
311 * Returns the height of a bitmap font
313 int FGAPIENTRY glutBitmapHeight( void* fontID )
316 * See which font are we queried about
318 SFG_Font* font = fghFontByID( fontID );
321 * Return the character set's height
323 return( font->Height );
327 * Draw a stroke character
329 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
331 const SFG_StrokeChar *schar;
332 const SFG_StrokeStrip *strip;
336 * First of all we'll need a font to use
338 SFG_StrokeFont* font = fghStrokeByID( fontID );
341 * Make sure the character we want to output is valid
343 freeglut_return_if_fail( character >= 0 && character < font->Quantity );
345 schar = font->Characters[character];
347 freeglut_return_if_fail( schar );
349 strip = schar->Strips;
351 for (i = 0; i < schar->Number; i++, strip++)
353 glBegin(GL_LINE_STRIP);
354 for(j = 0; j < strip->Number; j++)
356 glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
360 glTranslatef(schar->Right, 0.0, 0.0);
363 void FGAPIENTRY glutStrokeString( void* fontID, const char *string )
366 int numchar = strlen ( string ) ;
370 * First of all we'll need a font to use
372 SFG_StrokeFont* font = fghStrokeByID( fontID );
375 * Step through the string, drawing each character.
376 * A carriage return will simply translate the next character's insertion point back to the
377 * start of the line and down one line.
379 for( c = 0; c < numchar; c++ )
381 if ( ( string[ c ] >= 0 ) && ( string[ c ] < font->Quantity ) )
383 if ( string[c] == '\n' )
385 glTranslatef ( -length, -(float)(font->Height), 0.0 ) ;
388 else /* Not a carriage return, draw the bitmap character */
390 const SFG_StrokeChar *schar = font->Characters[string[c]];
393 const SFG_StrokeStrip *strip = schar->Strips;
395 for (i = 0; i < schar->Number; i++, strip++)
397 glBegin(GL_LINE_STRIP);
398 for(j = 0; j < strip->Number; j++)
399 glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
404 length += schar->Right ;
405 glTranslatef(schar->Right, 0.0, 0.0);
413 * Return the width in pixels of a stroke character
415 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
417 const SFG_StrokeChar *schar;
419 * First of all we'll need a font to use
421 SFG_StrokeFont* font = fghStrokeByID( fontID );
424 * Make sure the character we want to output is valid
426 freeglut_return_val_if_fail( character >= 0 && character < font->Quantity, 0 );
428 schar = font->Characters[character];
430 freeglut_return_val_if_fail( schar, 0 );
432 return ((int)(schar->Right + 0.5));
436 * Return the width of a string drawn using a stroke font
438 int FGAPIENTRY glutStrokeLength( void* fontID, const char* string )
442 float this_line_length = 0.0 ;
445 * First of all we'll need a font to use
447 SFG_StrokeFont* font = fghStrokeByID( fontID );
450 * Step through the characters in the string, adding up the width of each one
452 int numchar = strlen ( string ) ;
453 for( c = 0; c < numchar; c++ )
455 if ( ( string[ c ] >= 0 ) && ( string[ c ] < font->Quantity ) )
457 if ( string[ c ] == '\n' ) /* Carriage return, reset the length of this line */
459 if ( length < this_line_length ) length = this_line_length ;
460 this_line_length = 0.0 ;
462 else /* Not a carriage return, increment the length of this line */
464 const SFG_StrokeChar *schar = font->Characters[string[c]];
466 this_line_length += schar->Right ;
471 if ( length < this_line_length ) length = this_line_length ;
474 * Return the result now
476 return( (int)(length+0.5) );
480 * Returns the height of a stroke font
482 GLfloat FGAPIENTRY glutStrokeHeight( void* fontID )
485 * See which font are we queried about
487 SFG_StrokeFont* font = fghStrokeByID( fontID );
490 * Return the character set's height
492 return( font->Height );
495 /*** END OF FILE ***/