X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Ffreeglut_font.c;h=b163f6ed5cb32b84f33ced27f7e73265c96c0464;hb=9807c5fdb2f20f098c7046786908a7f3ce1dc2d4;hp=7581365d780ccfd8a13bf7ed3ad7eb5b1367b0fc;hpb=13bc069810e3f513a9aa6cd44e8a37d02c7baabf;p=freeglut diff --git a/src/freeglut_font.c b/src/freeglut_font.c index 7581365..b163f6e 100644 --- a/src/freeglut_font.c +++ b/src/freeglut_font.c @@ -25,11 +25,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "../include/GL/freeglut.h" +#include #include "freeglut_internal.h" /* @@ -62,20 +58,23 @@ extern SFG_StrokeFont fgStrokeMonoRoman; */ static SFG_Font* fghFontByID( void* font ) { - /* - * XXX Use a macro, a table of some kind, or else split these - * XXX statements properly. Jamming "return" on the end of an - * XXX "if" is just bad style, IMHO. - */ - if( font == GLUT_BITMAP_8_BY_13 ) return( &fgFontFixed8x13 ); - if( font == GLUT_BITMAP_9_BY_15 ) return( &fgFontFixed9x15 ); - if( font == GLUT_BITMAP_HELVETICA_10 ) return( &fgFontHelvetica10 ); - if( font == GLUT_BITMAP_HELVETICA_12 ) return( &fgFontHelvetica12 ); - if( font == GLUT_BITMAP_HELVETICA_18 ) return( &fgFontHelvetica18 ); - if( font == GLUT_BITMAP_TIMES_ROMAN_10 ) return( &fgFontTimesRoman10 ); - if( font == GLUT_BITMAP_TIMES_ROMAN_24 ) return( &fgFontTimesRoman24 ); - fgError( "font 0x%08x not found", font ); - return 0; /*** NOT REACHED ***/ + if( font == GLUT_BITMAP_8_BY_13 ) + return &fgFontFixed8x13; + if( font == GLUT_BITMAP_9_BY_15 ) + return &fgFontFixed9x15; + if( font == GLUT_BITMAP_HELVETICA_10 ) + return &fgFontHelvetica10; + if( font == GLUT_BITMAP_HELVETICA_12 ) + return &fgFontHelvetica12; + if( font == GLUT_BITMAP_HELVETICA_18 ) + return &fgFontHelvetica18; + if( font == GLUT_BITMAP_TIMES_ROMAN_10 ) + return &fgFontTimesRoman10; + if( font == GLUT_BITMAP_TIMES_ROMAN_24 ) + return &fgFontTimesRoman24; + + fgWarning( "font 0x%08x not found", font ); + return 0; } /* @@ -84,14 +83,13 @@ static SFG_Font* fghFontByID( void* font ) */ static SFG_StrokeFont* fghStrokeByID( void* font ) { - /* - * XXX Same comment as above about jamming "return" in after an - * XXX "if". - */ - if( font == GLUT_STROKE_ROMAN ) return( &fgStrokeRoman ); - if( font == GLUT_STROKE_MONO_ROMAN ) return( &fgStrokeMonoRoman ); - fgError( "stroke font 0x%08x not found", font ); - return 0; /*** NOT REACHED ***/ + if( font == GLUT_STROKE_ROMAN ) + return &fgStrokeRoman; + if( font == GLUT_STROKE_MONO_ROMAN ) + return &fgStrokeMonoRoman; + + fgWarning( "stroke font 0x%08x not found", font ); + return 0; } @@ -103,14 +101,16 @@ static SFG_StrokeFont* fghStrokeByID( void* font ) void FGAPIENTRY glutBitmapCharacter( void* fontID, int character ) { const GLubyte* face; - SFG_Font* font = fghFontByID( fontID ); - + SFG_Font* font; + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapCharacter" ); + font = fghFontByID( fontID ); freeglut_return_if_fail( ( character >= 1 )&&( character < 256 ) ); + freeglut_return_if_fail( font ); /* * Find the character we want to draw (???) */ - face = font->Characters[ character - 1 ]; + face = font->Characters[ character ]; glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT ); glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE ); @@ -130,12 +130,15 @@ void FGAPIENTRY glutBitmapCharacter( void* fontID, int character ) void FGAPIENTRY glutBitmapString( void* fontID, const unsigned char *string ) { - int c; - int numchar = strlen ( string ) ; - SFG_Font* font = fghFontByID( fontID ); - float raster_position[4]; + unsigned char c; + float x = 0.0f ; + SFG_Font* font; + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapString" ); + font = fghFontByID( fontID ); + freeglut_return_if_fail( font ); + if ( !string || ! *string ) + return; - glGetFloatv ( GL_CURRENT_RASTER_POSITION, raster_position ) ; glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT ); glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE ); glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE ); @@ -149,15 +152,15 @@ void FGAPIENTRY glutBitmapString( void* fontID, const unsigned char *string ) * A newline will simply translate the next character's insertion * point back to the start of the line and down one line. */ - for( c = 0; c < numchar; c++ ) - if ( string[c] == '\n' ) + while( ( c = *string++) ) + if( c == '\n' ) { - raster_position[1] -= (float)font->Height ; - glRasterPos4fv ( raster_position ) ; + glBitmap ( 0, 0, 0, 0, -x, (float) -font->Height, NULL ); + x = 0.0f; } else /* Not an EOL, draw the bitmap character */ { - const GLubyte* face = font->Characters[ string[ c ] - 1 ]; + const GLubyte* face = font->Characters[ c ]; glBitmap( face[ 0 ], font->Height, /* Bitmap's width and height */ @@ -165,7 +168,10 @@ void FGAPIENTRY glutBitmapString( void* fontID, const unsigned char *string ) ( float )( face[ 0 ] ), 0.0, /* The raster advance; inc. x,y */ ( face + 1 ) /* The packed bitmap data... */ ); + + x += ( float )( face[ 0 ] ); } + glPopClientAttrib( ); } @@ -174,10 +180,12 @@ void FGAPIENTRY glutBitmapString( void* fontID, const unsigned char *string ) */ int FGAPIENTRY glutBitmapWidth( void* fontID, int character ) { - SFG_Font* font = fghFontByID( fontID ); - + SFG_Font* font; + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapWidth" ); + font = fghFontByID( fontID ); freeglut_return_val_if_fail( character > 0 && character < 256, 0 ); - return *( font->Characters[ character - 1 ] ); + freeglut_return_val_if_fail( font, 0 ); + return *( font->Characters[ character ] ); } /* @@ -185,14 +193,19 @@ int FGAPIENTRY glutBitmapWidth( void* fontID, int character ) */ int FGAPIENTRY glutBitmapLength( void* fontID, const unsigned char* string ) { - int c, length = 0, this_line_length = 0; - SFG_Font* font = fghFontByID( fontID ); - int numchar = strlen ( string ) ; - - for( c = 0; c < numchar; c++ ) + unsigned char c; + int length = 0, this_line_length = 0; + SFG_Font* font; + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapLength" ); + font = fghFontByID( fontID ); + freeglut_return_val_if_fail( font, 0 ); + if ( !string || ! *string ) + return 0; + + while( ( c = *string++) ) { - if ( string[ c ] != '\n' )/* Not an EOL, increment length of line */ - this_line_length += *( font->Characters[ string[ c ] - 1 ]); + if( c != '\n' )/* Not an EOL, increment length of line */ + this_line_length += *( font->Characters[ c ]); else /* EOL; reset the length of this line */ { if( length < this_line_length ) @@ -211,7 +224,10 @@ int FGAPIENTRY glutBitmapLength( void* fontID, const unsigned char* string ) */ int FGAPIENTRY glutBitmapHeight( void* fontID ) { - SFG_Font* font = fghFontByID( fontID ); + SFG_Font* font; + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapHeight" ); + font = fghFontByID( fontID ); + freeglut_return_val_if_fail( font, 0 ); return font->Height; } @@ -223,11 +239,14 @@ void FGAPIENTRY glutStrokeCharacter( void* fontID, int character ) const SFG_StrokeChar *schar; const SFG_StrokeStrip *strip; int i, j; - SFG_StrokeFont* font = fghStrokeByID( fontID ); + SFG_StrokeFont* font; + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeCharacter" ); + font = fghStrokeByID( fontID ); + freeglut_return_if_fail( character >= 0 ); + freeglut_return_if_fail( character < font->Quantity ); + freeglut_return_if_fail( font ); - freeglut_return_if_fail( character >= 0 && character < font->Quantity ); - - schar = font->Characters[character]; + schar = font->Characters[ character ]; freeglut_return_if_fail( schar ); strip = schar->Strips; @@ -243,32 +262,37 @@ void FGAPIENTRY glutStrokeCharacter( void* fontID, int character ) void FGAPIENTRY glutStrokeString( void* fontID, const unsigned char *string ) { - int c, i, j; - int numchar = strlen ( string ); + unsigned char c; + int i, j; float length = 0.0; - SFG_StrokeFont* font = fghStrokeByID( fontID ); + SFG_StrokeFont* font; + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeString" ); + font = fghStrokeByID( fontID ); + freeglut_return_if_fail( font ); + if ( !string || ! *string ) + return; /* * Step through the string, drawing each character. * A newline will simply translate the next character's insertion * point back to the start of the line and down one line. */ - for( c = 0; c < numchar; c++ ) - if ( string[ c ] < font->Quantity ) + while( ( c = *string++) ) + if( c < font->Quantity ) { - if( string[ c ] == '\n' ) + if( c == '\n' ) { - glTranslatef ( -length, -(float)(font->Height), 0.0 ); + glTranslatef ( -length, -( float )( font->Height ), 0.0 ); length = 0.0; } else /* Not an EOL, draw the bitmap character */ { - const SFG_StrokeChar *schar = font->Characters[ string[ c ] ]; + const SFG_StrokeChar *schar = font->Characters[ c ]; if( schar ) { const SFG_StrokeStrip *strip = schar->Strips; - for(i = 0; i < schar->Number; i++, strip++) + for( i = 0; i < schar->Number; i++, strip++ ) { glBegin( GL_LINE_STRIP ); for( j = 0; j < strip->Number; j++ ) @@ -277,7 +301,7 @@ void FGAPIENTRY glutStrokeString( void* fontID, const unsigned char *string ) glEnd( ); } - + length += schar->Right; glTranslatef( schar->Right, 0.0, 0.0 ); } @@ -291,14 +315,17 @@ void FGAPIENTRY glutStrokeString( void* fontID, const unsigned char *string ) int FGAPIENTRY glutStrokeWidth( void* fontID, int character ) { const SFG_StrokeChar *schar; - SFG_StrokeFont* font = fghStrokeByID( fontID ); - + SFG_StrokeFont* font; + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeWidth" ); + font = fghStrokeByID( fontID ); freeglut_return_val_if_fail( ( character >= 0 ) && ( character < font->Quantity ), - 0 ); + 0 + ); + freeglut_return_val_if_fail( font, 0 ); schar = font->Characters[ character ]; freeglut_return_val_if_fail( schar, 0 ); - + return ( int )( schar->Right + 0.5 ); } @@ -307,16 +334,20 @@ int FGAPIENTRY glutStrokeWidth( void* fontID, int character ) */ int FGAPIENTRY glutStrokeLength( void* fontID, const unsigned char* string ) { - int c; + unsigned char c; float length = 0.0; float this_line_length = 0.0; - SFG_StrokeFont* font = fghStrokeByID( fontID ); - int numchar = strlen( string ); - - for( c = 0; c < numchar; c++ ) - if ( string[ c ] < font->Quantity ) + SFG_StrokeFont* font; + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeLength" ); + font = fghStrokeByID( fontID ); + freeglut_return_val_if_fail( font, 0 ); + if ( !string || ! *string ) + return 0; + + while( ( c = *string++) ) + if( c < font->Quantity ) { - if( string[ c ] == '\n' ) /* EOL; reset the length of this line */ + if( c == '\n' ) /* EOL; reset the length of this line */ { if( length < this_line_length ) length = this_line_length; @@ -324,14 +355,14 @@ int FGAPIENTRY glutStrokeLength( void* fontID, const unsigned char* string ) } else /* Not an EOL, increment the length of this line */ { - const SFG_StrokeChar *schar = font->Characters[ string[ c ] ]; - if ( schar ) + const SFG_StrokeChar *schar = font->Characters[ c ]; + if( schar ) this_line_length += schar->Right; } } if( length < this_line_length ) length = this_line_length; - return ( int )( length + 0.5 ); + return( int )( length + 0.5 ); } /* @@ -339,8 +370,11 @@ int FGAPIENTRY glutStrokeLength( void* fontID, const unsigned char* string ) */ GLfloat FGAPIENTRY glutStrokeHeight( void* fontID ) { - SFG_StrokeFont* font = fghStrokeByID( fontID ); - return( font->Height ); + SFG_StrokeFont* font; + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeHeight" ); + font = fghStrokeByID( fontID ); + freeglut_return_val_if_fail( font, 0.0 ); + return font->Height; } /*** END OF FILE ***/