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 void* glutStrokeRoman;
63 void* glutStrokeMonoRoman;
64 void* glutBitmap9By15;
65 void* glutBitmap8By13;
66 void* glutBitmapTimesRoman10;
67 void* glutBitmapTimesRoman24;
68 void* glutBitmapHelvetica10;
69 void* glutBitmapHelvetica12;
70 void* glutBitmapHelvetica18;
74 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
77 * Matches a font ID with a SFG_Font structure pointer.
78 * This was changed to match the GLUT header style.
80 static SFG_Font* fghFontByID( void* font )
83 * Try matching the font ID and the font data structure
85 if( font == GLUT_BITMAP_8_BY_13 ) return( &fgFontFixed8x13 );
86 if( font == GLUT_BITMAP_9_BY_15 ) return( &fgFontFixed9x15 );
87 if( font == GLUT_BITMAP_HELVETICA_10 ) return( &fgFontHelvetica10 );
88 if( font == GLUT_BITMAP_HELVETICA_12 ) return( &fgFontHelvetica12 );
89 if( font == GLUT_BITMAP_HELVETICA_18 ) return( &fgFontHelvetica18 );
90 if( font == GLUT_BITMAP_TIMES_ROMAN_10 ) return( &fgFontTimesRoman10 );
91 if( font == GLUT_BITMAP_TIMES_ROMAN_24 ) return( &fgFontTimesRoman24 );
94 * This probably is the library user's fault
96 fgError( "font 0x%08x not found", font );
102 * Matches a font ID with a SFG_StrokeFont structure pointer.
103 * This was changed to match the GLUT header style.
105 static SFG_StrokeFont* fghStrokeByID( void* font )
108 * Try matching the font ID and the font data structure
110 if( font == GLUT_STROKE_ROMAN ) return( &fgStrokeRoman );
111 if( font == GLUT_STROKE_MONO_ROMAN ) return( &fgStrokeMonoRoman );
114 * This probably is the library user's fault
116 fgError( "stroke font 0x%08x not found", font );
122 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
125 * Draw a bitmap character
127 void FGAPIENTRY glutBitmapCharacter( void* fontID, int character )
132 * First of all we'll need a font to use
134 SFG_Font* font = fghFontByID( fontID );
137 * Make sure the character we want to output is valid
139 freeglut_return_if_fail( character >= 0 && character < 256 );
142 * Then find the character we want to draw
144 face = font->Characters[ character - 1 ];
147 * Save the old pixel store settings
149 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
152 * Set up the pixel unpacking ways
154 glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE );
155 glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE );
156 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
157 glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
158 glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
159 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
162 * We'll use a glBitmap call to draw the font.
165 face[ 0 ], font->Height, /* The bitmap's width and height */
166 font->xorig, font->yorig, /* The origin -- what on earth? */
167 (float)(face[ 0 ]), 0.0, /* The raster advance -- inc. x */
168 (face + 1) /* The packed bitmap data... */
172 * Restore the old pixel store settings
177 void FGAPIENTRY glutBitmapString( void* fontID, const char *string )
180 int numchar = strlen ( string ) ;
183 * First of all we'll need a font to use
185 SFG_Font* font = fghFontByID( fontID );
187 float raster_position[4] ;
188 glGetFloatv ( GL_CURRENT_RASTER_POSITION, raster_position ) ;
191 * Save the old pixel store settings
193 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
196 * Set up the pixel unpacking ways
198 glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE );
199 glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE );
200 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
201 glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
202 glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
203 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
206 * Step through the string, drawing each character.
207 * A carriage return will simply translate the next character's insertion point back to the
208 * start of the line and down one line.
210 for( c = 0; c < numchar; c++ )
212 if ( string[c] == '\n' )
214 raster_position[1] -= (float)font->Height ;
215 glRasterPos4fv ( raster_position ) ;
217 else /* Not a carriage return, draw the bitmap character */
219 const GLubyte* face = font->Characters[ string[ c ] - 1 ] ;
222 * We'll use a glBitmap call to draw the font.
225 face[ 0 ], font->Height, /* The bitmap's width and height */
226 font->xorig, font->yorig, /* The origin -- what on earth? */
227 (float)(face[ 0 ]), 0.0, /* The raster advance -- inc. x */
228 (face + 1) /* The packed bitmap data... */
234 * Restore the old pixel store settings
240 * Returns the width in pixels of a font's character
242 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
245 * First of all, grab the font we need
247 SFG_Font* font = fghFontByID( fontID );
250 * Make sure the character we want to output is valid
252 freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
255 * Scan the font looking for the specified character
257 return( *(font->Characters[ character - 1 ]) );
261 * Return the width of a string drawn using a bitmap font
263 int FGAPIENTRY glutBitmapLength( void* fontID, const char* string )
265 int c, length = 0, this_line_length = 0;
268 * First of all, grab the font we need
270 SFG_Font* font = fghFontByID( fontID );
273 * Step through the characters in the string, adding up the width of each one
275 int numchar = strlen ( string ) ;
276 for( c = 0; c < numchar; c++ )
278 if ( string[ c ] == '\n' ) /* Carriage return, reset the length of this line */
280 if ( length < this_line_length ) length = this_line_length ;
281 this_line_length = 0 ;
283 else /* Not a carriage return, increment the length of this line */
284 this_line_length += *(font->Characters[ string[ c ] - 1 ]) ;
287 if ( length < this_line_length ) length = this_line_length ;
290 * Return the result now
296 * Returns the height of a bitmap font
298 int FGAPIENTRY glutBitmapHeight( void* fontID )
301 * See which font are we queried about
303 SFG_Font* font = fghFontByID( fontID );
306 * Return the character set's height
308 return( font->Height );
312 * Draw a stroke character
314 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
316 const SFG_StrokeChar *schar;
317 const SFG_StrokeStrip *strip;
321 * First of all we'll need a font to use
323 SFG_StrokeFont* font = fghStrokeByID( fontID );
326 * Make sure the character we want to output is valid
328 freeglut_return_if_fail( character >= 0 && character < font->Quantity );
330 schar = font->Characters[character];
332 freeglut_return_if_fail( schar );
334 strip = schar->Strips;
336 for (i = 0; i < schar->Number; i++, strip++)
338 glBegin(GL_LINE_STRIP);
339 for(j = 0; j < strip->Number; j++)
341 glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
345 glTranslatef(schar->Right, 0.0, 0.0);
348 void FGAPIENTRY glutStrokeString( void* fontID, const char *string )
351 int numchar = strlen ( string ) ;
355 * First of all we'll need a font to use
357 SFG_StrokeFont* font = fghStrokeByID( fontID );
360 * Step through the string, drawing each character.
361 * A carriage return will simply translate the next character's insertion point back to the
362 * start of the line and down one line.
364 for( c = 0; c < numchar; c++ )
366 if ( ( string[ c ] >= 0 ) && ( string[ c ] < font->Quantity ) )
368 if ( string[c] == '\n' )
370 glTranslatef ( -length, -(float)(font->Height), 0.0 ) ;
373 else /* Not a carriage return, draw the bitmap character */
375 const SFG_StrokeChar *schar = font->Characters[string[c]];
378 const SFG_StrokeStrip *strip = schar->Strips;
380 for (i = 0; i < schar->Number; i++, strip++)
382 glBegin(GL_LINE_STRIP);
383 for(j = 0; j < strip->Number; j++)
384 glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
389 length += schar->Right ;
390 glTranslatef(schar->Right, 0.0, 0.0);
398 * Return the width in pixels of a stroke character
400 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
402 const SFG_StrokeChar *schar;
404 * First of all we'll need a font to use
406 SFG_StrokeFont* font = fghStrokeByID( fontID );
409 * Make sure the character we want to output is valid
411 freeglut_return_val_if_fail( character >= 0 && character < font->Quantity, 0 );
413 schar = font->Characters[character];
415 freeglut_return_val_if_fail( schar, 0 );
417 return ((int)(schar->Right + 0.5));
421 * Return the width of a string drawn using a stroke font
423 int FGAPIENTRY glutStrokeLength( void* fontID, const char* string )
427 float this_line_length = 0.0 ;
430 * First of all we'll need a font to use
432 SFG_StrokeFont* font = fghStrokeByID( fontID );
435 * Step through the characters in the string, adding up the width of each one
437 int numchar = strlen ( string ) ;
438 for( c = 0; c < numchar; c++ )
440 if ( ( string[ c ] >= 0 ) && ( string[ c ] < font->Quantity ) )
442 if ( string[ c ] == '\n' ) /* Carriage return, reset the length of this line */
444 if ( length < this_line_length ) length = this_line_length ;
445 this_line_length = 0.0 ;
447 else /* Not a carriage return, increment the length of this line */
449 const SFG_StrokeChar *schar = font->Characters[string[c]];
451 this_line_length += schar->Right ;
456 if ( length < this_line_length ) length = this_line_length ;
459 * Return the result now
461 return( (int)(length+0.5) );
465 * Returns the height of a stroke font
467 GLfloat FGAPIENTRY glutStrokeHeight( void* fontID )
470 * See which font are we queried about
472 SFG_StrokeFont* font = fghStrokeByID( fontID );
475 * Return the character set's height
477 return( font->Height );
480 /*** END OF FILE ***/