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 "../include/GL/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 ] + 1), 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 ] >= 0 ) && ( string[ c ] < 256 ) )
214 if ( string[c] == '\n' )
216 raster_position[1] -= (float)font->Height ;
217 glRasterPos4fv ( raster_position ) ;
219 else /* Not a carriage return, draw the bitmap character */
221 const GLubyte* face = font->Characters[ string[ c ] - 1 ] ;
224 * We'll use a glBitmap call to draw the font.
227 face[ 0 ], font->Height, /* The bitmap's width and height */
228 font->xorig, font->yorig, /* The origin -- what on earth? */
229 (float)(face[ 0 ] + 1), 0.0, /* The raster advance -- inc. x */
230 (face + 1) /* The packed bitmap data... */
237 * Restore the old pixel store settings
243 * Returns the width in pixels of a font's character
245 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
248 * First of all, grab the font we need
250 SFG_Font* font = fghFontByID( fontID );
253 * Make sure the character we want to output is valid
255 freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
258 * Scan the font looking for the specified character
260 return( *(font->Characters[ character - 1 ]) + 1 );
264 * Return the width of a string drawn using a bitmap font
266 int FGAPIENTRY glutBitmapLength( void* fontID, const char* string )
268 int c, length = 0, this_line_length = 0;
271 * First of all, grab the font we need
273 SFG_Font* font = fghFontByID( fontID );
276 * Step through the characters in the string, adding up the width of each one
278 int numchar = strlen ( string ) ;
279 for( c = 0; c < numchar; c++ )
281 if ( ( string[ c ] >= 0 ) && ( string[ c ] < 256 ) )
283 if ( string[ c ] == '\n' ) /* Carriage return, reset the length of this line */
285 if ( length < this_line_length ) length = this_line_length ;
286 this_line_length = 0 ;
288 else /* Not a carriage return, increment the length of this line */
289 this_line_length += *(font->Characters[ string[ c ] - 1 ]) + 1 ;
293 if ( length < this_line_length ) length = this_line_length ;
296 * Return the result now
302 * Returns the height of a bitmap font
304 int FGAPIENTRY glutBitmapHeight( void* fontID )
307 * See which font are we queried about
309 SFG_Font* font = fghFontByID( fontID );
312 * Return the character set's height
314 return( font->Height );
318 * Draw a stroke character
320 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
322 const SFG_StrokeChar *schar;
323 const SFG_StrokeStrip *strip;
327 * First of all we'll need a font to use
329 SFG_StrokeFont* font = fghStrokeByID( fontID );
332 * Make sure the character we want to output is valid
334 freeglut_return_if_fail( character >= 0 && character < font->Quantity );
336 schar = font->Characters[character];
338 freeglut_return_if_fail( schar );
340 strip = schar->Strips;
342 for (i = 0; i < schar->Number; i++, strip++)
344 glBegin(GL_LINE_STRIP);
345 for(j = 0; j < strip->Number; j++)
347 glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
351 glTranslatef(schar->Right, 0.0, 0.0);
354 void FGAPIENTRY glutStrokeString( void* fontID, const char *string )
357 int numchar = strlen ( string ) ;
361 * First of all we'll need a font to use
363 SFG_StrokeFont* font = fghStrokeByID( fontID );
366 * Step through the string, drawing each character.
367 * A carriage return will simply translate the next character's insertion point back to the
368 * start of the line and down one line.
370 for( c = 0; c < numchar; c++ )
372 if ( ( string[ c ] >= 0 ) && ( string[ c ] < font->Quantity ) )
374 if ( string[c] == '\n' )
376 glTranslatef ( -length, -(float)(font->Height), 0.0 ) ;
379 else /* Not a carriage return, draw the bitmap character */
381 const SFG_StrokeChar *schar = font->Characters[string[c]];
384 const SFG_StrokeStrip *strip = schar->Strips;
386 for (i = 0; i < schar->Number; i++, strip++)
388 glBegin(GL_LINE_STRIP);
389 for(j = 0; j < strip->Number; j++)
390 glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
395 length += schar->Right ;
396 glTranslatef(schar->Right, 0.0, 0.0);
404 * Return the width in pixels of a stroke character
406 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
408 const SFG_StrokeChar *schar;
410 * First of all we'll need a font to use
412 SFG_StrokeFont* font = fghStrokeByID( fontID );
415 * Make sure the character we want to output is valid
417 freeglut_return_val_if_fail( character >= 0 && character < font->Quantity, 0 );
419 schar = font->Characters[character];
421 freeglut_return_val_if_fail( schar, 0 );
423 return ((int)(schar->Right + 0.5));
427 * Return the width of a string drawn using a stroke font
429 int FGAPIENTRY glutStrokeLength( void* fontID, const char* string )
433 float this_line_length = 0.0 ;
436 * First of all we'll need a font to use
438 SFG_StrokeFont* font = fghStrokeByID( fontID );
441 * Step through the characters in the string, adding up the width of each one
443 int numchar = strlen ( string ) ;
444 for( c = 0; c < numchar; c++ )
446 if ( ( string[ c ] >= 0 ) && ( string[ c ] < font->Quantity ) )
448 if ( string[ c ] == '\n' ) /* Carriage return, reset the length of this line */
450 if ( length < this_line_length ) length = this_line_length ;
451 this_line_length = 0.0 ;
453 else /* Not a carriage return, increment the length of this line */
455 const SFG_StrokeChar *schar = font->Characters[string[c]];
457 this_line_length += schar->Right ;
462 if ( length < this_line_length ) length = this_line_length ;
465 * Return the result now
467 return( (int)(length+0.5) );
471 * Returns the height of a stroke font
473 GLfloat FGAPIENTRY glutStrokeHeight( void* fontID )
476 * See which font are we queried about
478 SFG_StrokeFont* font = fghStrokeByID( fontID );
481 * Return the character set's height
483 return( font->Height );
486 /*** END OF FILE ***/