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:
40 * glutStrokeCharacter() -- stroke fonts not implemented, uses a bitmap font instead
41 * glutStrokeWidth() -- stroke fonts not implemented, uses a bitmap font instead
44 /* -- IMPORT DECLARATIONS -------------------------------------------------- */
47 * These are the font faces defined in freeglut_font_data.c file:
49 extern SFG_Font fgFontFixed8x13;
50 extern SFG_Font fgFontFixed9x15;
51 extern SFG_Font fgFontHelvetica10;
52 extern SFG_Font fgFontHelvetica12;
53 extern SFG_Font fgFontHelvetica18;
54 extern SFG_Font fgFontTimesRoman10;
55 extern SFG_Font fgFontTimesRoman24;
58 * This is for GLUT binary compatibility, as suggested by Steve Baker
60 #if TARGET_HOST_UNIX_X11
61 void* glutStrokeRoman;
62 void* glutStrokeMonoRoman;
63 void* glutBitmap9By15;
64 void* glutBitmap8By13;
65 void* glutBitmapTimesRoman10;
66 void* glutBitmapTimesRoman24;
67 void* glutBitmapHelvetica10;
68 void* glutBitmapHelvetica12;
69 void* glutBitmapHelvetica18;
73 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
76 * Matches a font ID with a SFG_Font structure pointer.
77 * This was changed to match the GLUT header style.
79 static SFG_Font* fghFontByID( void* font )
82 * Try matching the font ID and the font data structure
84 if( font == GLUT_BITMAP_8_BY_13 ) return( &fgFontFixed8x13 );
85 if( font == GLUT_BITMAP_9_BY_15 ) return( &fgFontFixed9x15 );
86 if( font == GLUT_BITMAP_HELVETICA_10 ) return( &fgFontHelvetica10 );
87 if( font == GLUT_BITMAP_HELVETICA_12 ) return( &fgFontHelvetica12 );
88 if( font == GLUT_BITMAP_HELVETICA_18 ) return( &fgFontHelvetica18 );
89 if( font == GLUT_BITMAP_TIMES_ROMAN_10 ) return( &fgFontTimesRoman10 );
90 if( font == GLUT_BITMAP_TIMES_ROMAN_24 ) return( &fgFontTimesRoman24 );
93 * This probably is the library user's fault
95 fgError( "font 0x%08x not found", font );
101 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
104 * Draw a bitmap character
106 void FGAPIENTRY glutBitmapCharacter( void* fontID, int character )
111 * First of all we'll need a font to use
113 SFG_Font* font = fghFontByID( fontID );
116 * Make sure the character we want to output is valid
118 freeglut_return_if_fail( character > 0 && character < 256 );
121 * Then find the character we want to draw
123 face = font->Characters[ character - 1 ];
126 * Save the old pixel store settings
128 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
131 * Set up the pixel unpacking ways
133 glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE );
134 glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE );
135 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
136 glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
137 glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
138 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
141 * We'll use a glBitmap call to draw the font.
144 face[ 0 ], font->Height, /* The bitmap's width and height */
145 0, 0, /* The origin -- what the hell? */
146 face[ 0 ] + 1, 0, /* The raster advance -- inc. x */
147 (face + 1) /* The packed bitmap data... */
151 * Restore the old pixel store settings
156 void FGAPIENTRY glutBitmapString( void* fontID, const char *string )
160 for( i=0; i<strlen( string ); i++ )
161 glutBitmapCharacter( fontID, string[ i ] );
165 * Returns the width in pixels of a font's character
167 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
170 * First of all, grab the font we need
172 SFG_Font* font = fghFontByID( fontID );
175 * Make sure the character we want to output is valid
177 freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
180 * Scan the font looking for the specified character
182 return( *(font->Characters[ character - 1 ]) + 1 );
186 * Draw a stroke character
188 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
191 * Stroke fonts are not supported yet, use a bitmap font instead
193 glutBitmapCharacter( GLUT_BITMAP_8_BY_13, character );
197 * Return the width in pixels of a stroke character
199 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
202 * Stroke fonts are not supported yet, use a bitmap font instead
204 return( glutBitmapWidth( GLUT_BITMAP_8_BY_13, character ) );
208 * Return the width of a string drawn using a bitmap font
210 int FGAPIENTRY glutBitmapLength( void* fontID, const char* string )
215 * Using glutBitmapWidth() function to calculate the result
217 for( i=0; i<strlen( string ); i++ )
218 length += glutBitmapWidth( fontID, string[ i ] );
221 * Return the result now
227 * Return the width of a string drawn using a stroke font
229 int FGAPIENTRY glutStrokeLength( void* fontID, const char* string )
234 * Using glutStrokeWidth() function to calculate the result
236 for( i=0; i<strlen( string ); i++ )
237 length += glutStrokeWidth( fontID, string[ i ] );
240 * Return the result now
246 * Returns the height of a bitmap font
248 int FGAPIENTRY glutBitmapHeight( void* fontID )
251 * See which font are we queried about
253 SFG_Font* font = fghFontByID( fontID );
256 * Return the character set's height
258 return( font->Height );
262 * Returns the height of a stroke font
264 int FGAPIENTRY glutStrokeHeight( void* font )
267 * Stroke fonts are currently not implemented.
268 * Using GLUT_BITMAP_8_BY_13 bitmap font instead
270 return( glutBitmapHeight( GLUT_BITMAP_8_BY_13 ) );
273 /*** END OF FILE ***/