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 #include <GL/freeglut.h>
33 #include "freeglut_internal.h"
36 * TODO BEFORE THE STABLE RELEASE:
41 /* -- IMPORT DECLARATIONS -------------------------------------------------- */
44 * These are the font faces defined in freeglut_font_data.c file:
46 extern SFG_Font fgFontFixed8x13;
47 extern SFG_Font fgFontFixed9x15;
48 extern SFG_Font fgFontHelvetica10;
49 extern SFG_Font fgFontHelvetica12;
50 extern SFG_Font fgFontHelvetica18;
51 extern SFG_Font fgFontTimesRoman10;
52 extern SFG_Font fgFontTimesRoman24;
53 extern SFG_StrokeFont fgStrokeRoman;
54 extern SFG_StrokeFont fgStrokeMonoRoman;
57 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
60 * Matches a font ID with a SFG_Font structure pointer.
61 * This was changed to match the GLUT header style.
63 static SFG_Font* fghFontByID( void* font )
65 if( font == GLUT_BITMAP_8_BY_13 )
66 return &fgFontFixed8x13;
67 if( font == GLUT_BITMAP_9_BY_15 )
68 return &fgFontFixed9x15;
69 if( font == GLUT_BITMAP_HELVETICA_10 )
70 return &fgFontHelvetica10;
71 if( font == GLUT_BITMAP_HELVETICA_12 )
72 return &fgFontHelvetica12;
73 if( font == GLUT_BITMAP_HELVETICA_18 )
74 return &fgFontHelvetica18;
75 if( font == GLUT_BITMAP_TIMES_ROMAN_10 )
76 return &fgFontTimesRoman10;
77 if( font == GLUT_BITMAP_TIMES_ROMAN_24 )
78 return &fgFontTimesRoman24;
80 fgError( "font 0x%08x not found", font );
81 return 0; /*** NOT REACHED ***/
85 * Matches a font ID with a SFG_StrokeFont structure pointer.
86 * This was changed to match the GLUT header style.
88 static SFG_StrokeFont* fghStrokeByID( void* font )
90 if( font == GLUT_STROKE_ROMAN )
91 return &fgStrokeRoman;
92 if( font == GLUT_STROKE_MONO_ROMAN )
93 return &fgStrokeMonoRoman;
95 fgError( "stroke font 0x%08x not found", font );
96 return 0; /*** NOT REACHED ***/
100 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
103 * Draw a bitmap character
105 void FGAPIENTRY glutBitmapCharacter( void* fontID, int character )
108 SFG_Font* font = fghFontByID( fontID );
110 freeglut_return_if_fail( ( character >= 1 )&&( character < 256 ) );
113 * Find the character we want to draw (???)
115 face = font->Characters[ character ];
117 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
118 glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE );
119 glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE );
120 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
121 glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
122 glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
123 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
125 face[ 0 ], font->Height, /* The bitmap's width and height */
126 font->xorig, font->yorig, /* The origin in the font glyph */
127 ( float )( face[ 0 ] ), 0.0, /* The raster advance -- inc. x,y */
128 ( face + 1 ) /* The packed bitmap data... */
130 glPopClientAttrib( );
133 void FGAPIENTRY glutBitmapString( void* fontID, const unsigned char *string )
136 int numchar = strlen( (char *) string );
137 SFG_Font* font = fghFontByID( fontID );
138 float raster_position[ 4 ];
140 glPushAttrib( GL_TRANSFORM_BIT );
141 glMatrixMode( GL_MODELVIEW );
144 glMatrixMode( GL_PROJECTION );
148 0, glutGet( GLUT_WINDOW_WIDTH ),
149 0, glutGet( GLUT_WINDOW_HEIGHT ),
153 glGetFloatv ( GL_CURRENT_RASTER_POSITION, raster_position );
154 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
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 * Step through the string, drawing each character.
164 * A newline will simply translate the next character's insertion
165 * point back to the start of the line and down one line.
167 for( c = 0; c < numchar; c++ )
168 if( string[c] == '\n' )
170 raster_position[ 1 ] -= ( float )font->Height;
171 glRasterPos4fv( raster_position );
173 else /* Not an EOL, draw the bitmap character */
175 const GLubyte* face = font->Characters[ string[ c ] ];
178 face[ 0 ], font->Height, /* Bitmap's width and height */
179 font->xorig, font->yorig, /* The origin in the font glyph */
180 ( float )( face[ 0 ] ), 0.0, /* The raster advance; inc. x,y */
181 ( face + 1 ) /* The packed bitmap data... */
185 glPopClientAttrib( );
187 glMatrixMode( GL_MODELVIEW );
193 * Returns the width in pixels of a font's character
195 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
197 SFG_Font* font = fghFontByID( fontID );
199 freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
200 return *( font->Characters[ character ] );
204 * Return the width of a string drawn using a bitmap font
206 int FGAPIENTRY glutBitmapLength( void* fontID, const unsigned char* string )
208 int c, length = 0, this_line_length = 0;
209 SFG_Font* font = fghFontByID( fontID );
210 int numchar = strlen( (char *) string );
212 for( c = 0; c < numchar; c++ )
214 if( string[ c ] != '\n' )/* Not an EOL, increment length of line */
215 this_line_length += *( font->Characters[ string[ c ] ]);
216 else /* EOL; reset the length of this line */
218 if( length < this_line_length )
219 length = this_line_length;
220 this_line_length = 0;
223 if ( length < this_line_length )
224 length = this_line_length;
230 * Returns the height of a bitmap font
232 int FGAPIENTRY glutBitmapHeight( void* fontID )
234 SFG_Font* font = fghFontByID( fontID );
239 * Draw a stroke character
241 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
243 const SFG_StrokeChar *schar;
244 const SFG_StrokeStrip *strip;
246 SFG_StrokeFont* font = fghStrokeByID( fontID );
248 freeglut_return_if_fail( character >= 0 );
249 freeglut_return_if_fail( character < font->Quantity );
251 schar = font->Characters[ character ];
252 freeglut_return_if_fail( schar );
253 strip = schar->Strips;
255 for( i = 0; i < schar->Number; i++, strip++ )
257 glBegin( GL_LINE_STRIP );
258 for( j = 0; j < strip->Number; j++ )
259 glVertex2f( strip->Vertices[ j ].X, strip->Vertices[ j ].Y );
262 glTranslatef( schar->Right, 0.0, 0.0 );
265 void FGAPIENTRY glutStrokeString( void* fontID, const unsigned char *string )
268 int numchar = strlen( (char *) string );
270 SFG_StrokeFont* font = fghStrokeByID( fontID );
273 * Step through the string, drawing each character.
274 * A newline will simply translate the next character's insertion
275 * point back to the start of the line and down one line.
277 for( c = 0; c < numchar; c++ )
278 if( string[ c ] < font->Quantity )
280 if( string[ c ] == '\n' )
282 glTranslatef ( -length, -( float )( font->Height ), 0.0 );
285 else /* Not an EOL, draw the bitmap character */
287 const SFG_StrokeChar *schar = font->Characters[ string[ c ] ];
290 const SFG_StrokeStrip *strip = schar->Strips;
292 for( i = 0; i < schar->Number; i++, strip++ )
294 glBegin( GL_LINE_STRIP );
295 for( j = 0; j < strip->Number; j++ )
296 glVertex2f( strip->Vertices[ j ].X,
297 strip->Vertices[ j ].Y);
302 length += schar->Right;
303 glTranslatef( schar->Right, 0.0, 0.0 );
310 * Return the width in pixels of a stroke character
312 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
314 const SFG_StrokeChar *schar;
315 SFG_StrokeFont* font = fghStrokeByID( fontID );
317 freeglut_return_val_if_fail( ( character >= 0 ) &&
318 ( character < font->Quantity ),
321 schar = font->Characters[ character ];
322 freeglut_return_val_if_fail( schar, 0 );
324 return ( int )( schar->Right + 0.5 );
328 * Return the width of a string drawn using a stroke font
330 int FGAPIENTRY glutStrokeLength( void* fontID, const unsigned char* string )
334 float this_line_length = 0.0;
335 SFG_StrokeFont* font = fghStrokeByID( fontID );
336 int numchar = strlen( (char *) string );
338 for( c = 0; c < numchar; c++ )
339 if( string[ c ] < font->Quantity )
341 if( string[ c ] == '\n' ) /* EOL; reset the length of this line */
343 if( length < this_line_length )
344 length = this_line_length;
345 this_line_length = 0.0;
347 else /* Not an EOL, increment the length of this line */
349 const SFG_StrokeChar *schar = font->Characters[ string[ c ] ];
351 this_line_length += schar->Right;
354 if( length < this_line_length )
355 length = this_line_length;
356 return( int )( length + 0.5 );
360 * Returns the height of a stroke font
362 GLfloat FGAPIENTRY glutStrokeHeight( void* fontID )
364 SFG_StrokeFont* font = fghStrokeByID( fontID );
368 /*** END OF FILE ***/