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;
56 extern SFG_StrokeFont fgStrokeRoman;
57 extern SFG_StrokeFont fgStrokeMonoRoman;
60 * This is for GLUT binary compatibility, as suggested by Steve Baker
62 #if TARGET_HOST_UNIX_X11
63 void* glutStrokeRoman;
64 void* glutStrokeMonoRoman;
65 void* glutBitmap9By15;
66 void* glutBitmap8By13;
67 void* glutBitmapTimesRoman10;
68 void* glutBitmapTimesRoman24;
69 void* glutBitmapHelvetica10;
70 void* glutBitmapHelvetica12;
71 void* glutBitmapHelvetica18;
75 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
78 * Matches a font ID with a SFG_Font structure pointer.
79 * This was changed to match the GLUT header style.
81 static SFG_Font* fghFontByID( void* font )
84 * Try matching the font ID and the font data structure
86 if( font == GLUT_BITMAP_8_BY_13 ) return( &fgFontFixed8x13 );
87 if( font == GLUT_BITMAP_9_BY_15 ) return( &fgFontFixed9x15 );
88 if( font == GLUT_BITMAP_HELVETICA_10 ) return( &fgFontHelvetica10 );
89 if( font == GLUT_BITMAP_HELVETICA_12 ) return( &fgFontHelvetica12 );
90 if( font == GLUT_BITMAP_HELVETICA_18 ) return( &fgFontHelvetica18 );
91 if( font == GLUT_BITMAP_TIMES_ROMAN_10 ) return( &fgFontTimesRoman10 );
92 if( font == GLUT_BITMAP_TIMES_ROMAN_24 ) return( &fgFontTimesRoman24 );
95 * This probably is the library user's fault
97 fgError( "font 0x%08x not found", font );
103 * Matches a font ID with a SFG_StrokeFont structure pointer.
104 * This was changed to match the GLUT header style.
106 static SFG_StrokeFont* fghStrokeByID( void* font )
109 * Try matching the font ID and the font data structure
111 if( font == GLUT_STROKE_ROMAN ) return( &fgStrokeRoman );
112 if( font == GLUT_STROKE_MONO_ROMAN ) return( &fgStrokeMonoRoman );
115 * This probably is the library user's fault
117 fgError( "stroke font 0x%08x not found", font );
123 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
126 * Draw a bitmap character
128 void FGAPIENTRY glutBitmapCharacter( void* fontID, int character )
133 * First of all we'll need a font to use
135 SFG_Font* font = fghFontByID( fontID );
138 * Make sure the character we want to output is valid
140 freeglut_return_if_fail( character >= 0 && character < 256 );
143 * Then find the character we want to draw
145 face = font->Characters[ character - 1 ];
148 * Save the old pixel store settings
150 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
153 * Set up the pixel unpacking ways
155 glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE );
156 glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE );
157 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
158 glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
159 glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
160 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
163 * We'll use a glBitmap call to draw the font.
166 face[ 0 ], font->Height, /* The bitmap's width and height */
167 0, 0, /* The origin -- what the hell? */
168 face[ 0 ] + 1, 0, /* The raster advance -- inc. x */
169 (face + 1) /* The packed bitmap data... */
173 * Restore the old pixel store settings
178 void FGAPIENTRY glutBitmapString( void* fontID, const char *string )
182 for( i=0; i<strlen( string ); i++ )
183 glutBitmapCharacter( fontID, string[ i ] );
187 * Returns the width in pixels of a font's character
189 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
192 * First of all, grab the font we need
194 SFG_Font* font = fghFontByID( fontID );
197 * Make sure the character we want to output is valid
199 freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
202 * Scan the font looking for the specified character
204 return( *(font->Characters[ character - 1 ]) + 1 );
208 * Draw a stroke character
210 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
212 const SFG_StrokeChar *schar;
213 const SFG_StrokeStrip *strip;
217 * First of all we'll need a font to use
219 SFG_StrokeFont* font = fghStrokeByID( fontID );
222 * Make sure the character we want to output is valid
224 freeglut_return_if_fail( character >= 0 && character < font->Quantity );
226 schar = font->Characters[character];
228 freeglut_return_if_fail( schar );
230 strip = schar->Strips;
232 for (i = 0; i < schar->Number; i++, strip++)
234 glBegin(GL_LINE_STRIP);
235 for(j = 0; j < strip->Number; j++)
237 glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
241 glTranslatef(schar->Right, 0.0, 0.0);
245 * Return the width in pixels of a stroke character
247 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
249 const SFG_StrokeChar *schar;
251 * First of all we'll need a font to use
253 SFG_StrokeFont* font = fghStrokeByID( fontID );
256 * Make sure the character we want to output is valid
258 freeglut_return_if_fail( character >= 0 && character < font->Quantity );
260 schar = font->Characters[character];
262 freeglut_return_if_fail( schar );
264 return (schar->Right);
268 * Return the width of a string drawn using a bitmap font
270 int FGAPIENTRY glutBitmapLength( void* fontID, const char* string )
275 * Using glutBitmapWidth() function to calculate the result
277 for( i=0; i<strlen( string ); i++ )
278 length += glutBitmapWidth( fontID, string[ i ] );
281 * Return the result now
287 * Return the width of a string drawn using a stroke font
289 int FGAPIENTRY glutStrokeLength( void* fontID, const char* string )
294 * Using glutStrokeWidth() function to calculate the result
296 for( i=0; i<strlen( string ); i++ )
297 length += glutStrokeWidth( fontID, string[ i ] );
300 * Return the result now
306 * Returns the height of a bitmap font
308 int FGAPIENTRY glutBitmapHeight( void* fontID )
311 * See which font are we queried about
313 SFG_Font* font = fghFontByID( fontID );
316 * Return the character set's height
318 return( font->Height );
322 * Returns the height of a stroke font
324 GLfloat FGAPIENTRY glutStrokeHeight( void* fontID )
327 * See which font are we queried about
329 SFG_StrokeFont* font = fghStrokeByID( fontID );
332 * Return the character set's height
334 return( font->Height );
337 /*** END OF FILE ***/