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.
28 #include <GL/freeglut.h>
29 #include "fg_internal.h"
32 * TODO BEFORE THE STABLE RELEASE:
37 /* -- IMPORT DECLARATIONS -------------------------------------------------- */
40 * These are the font faces defined in freeglut_font_data.c file:
42 extern SFG_Font fgFontFixed8x13;
43 extern SFG_Font fgFontFixed9x15;
44 extern SFG_Font fgFontHelvetica10;
45 extern SFG_Font fgFontHelvetica12;
46 extern SFG_Font fgFontHelvetica18;
47 extern SFG_Font fgFontTimesRoman10;
48 extern SFG_Font fgFontTimesRoman24;
49 extern SFG_StrokeFont fgStrokeRoman;
50 extern SFG_StrokeFont fgStrokeMonoRoman;
53 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
56 * Matches a font ID with a SFG_Font structure pointer.
57 * This was changed to match the GLUT header style.
59 SFG_Font* fghFontByID( void* font )
61 if( font == GLUT_BITMAP_8_BY_13 )
62 return &fgFontFixed8x13;
63 if( font == GLUT_BITMAP_9_BY_15 )
64 return &fgFontFixed9x15;
65 if( font == GLUT_BITMAP_HELVETICA_10 )
66 return &fgFontHelvetica10;
67 if( font == GLUT_BITMAP_HELVETICA_12 )
68 return &fgFontHelvetica12;
69 if( font == GLUT_BITMAP_HELVETICA_18 )
70 return &fgFontHelvetica18;
71 if( font == GLUT_BITMAP_TIMES_ROMAN_10 )
72 return &fgFontTimesRoman10;
73 if( font == GLUT_BITMAP_TIMES_ROMAN_24 )
74 return &fgFontTimesRoman24;
80 * Matches a font ID with a SFG_StrokeFont structure pointer.
81 * This was changed to match the GLUT header style.
83 static SFG_StrokeFont* fghStrokeByID( void* font )
85 if( font == GLUT_STROKE_ROMAN )
86 return &fgStrokeRoman;
87 if( font == GLUT_STROKE_MONO_ROMAN )
88 return &fgStrokeMonoRoman;
94 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
97 * Draw a bitmap character
99 void FGAPIENTRY glutBitmapCharacter( void* fontID, int character )
103 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapCharacter" );
104 font = fghFontByID( fontID );
107 fgWarning("glutBitmapCharacter: bitmap font 0x%08x not found. Make sure you're not passing a stroke font.\n",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 )
138 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapString" );
139 font = fghFontByID( fontID );
142 fgWarning("glutBitmapString: bitmap font 0x%08x not found. Make sure you're not passing a stroke font.\n",fontID);
145 if ( !string || ! *string )
148 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
149 glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE );
150 glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE );
151 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
152 glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
153 glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
154 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
157 * Step through the string, drawing each character.
158 * A newline will simply translate the next character's insertion
159 * point back to the start of the line and down one line.
161 while( ( c = *string++) )
164 glBitmap ( 0, 0, 0, 0, -x, (float) -font->Height, NULL );
167 else /* Not an EOL, draw the bitmap character */
169 const GLubyte* face = font->Characters[ c ];
172 face[ 0 ], font->Height, /* Bitmap's width and height */
173 font->xorig, font->yorig, /* The origin in the font glyph */
174 ( float )( face[ 0 ] ), 0.0, /* The raster advance; inc. x,y */
175 ( face + 1 ) /* The packed bitmap data... */
178 x += ( float )( face[ 0 ] );
181 glPopClientAttrib( );
185 * Returns the width in pixels of a font's character
187 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
190 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapWidth" );
191 freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
192 font = fghFontByID( fontID );
195 fgWarning("glutBitmapWidth: bitmap font 0x%08x not found. Make sure you're not passing a stroke font.\n",fontID);
198 return *( font->Characters[ character ] );
202 * Return the width of a string drawn using a bitmap font
204 int FGAPIENTRY glutBitmapLength( void* fontID, const unsigned char* string )
207 int length = 0, this_line_length = 0;
209 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapLength" );
210 font = fghFontByID( fontID );
213 fgWarning("glutBitmapLength: bitmap font 0x%08x not found. Make sure you're not passing a stroke font.\n",fontID);
216 if ( !string || ! *string )
219 while( ( c = *string++) )
221 if( c != '\n' )/* Not an EOL, increment length of line */
222 this_line_length += *( font->Characters[ c ]);
223 else /* EOL; reset the length of this line */
225 if( length < this_line_length )
226 length = this_line_length;
227 this_line_length = 0;
230 if ( length < this_line_length )
231 length = this_line_length;
237 * Returns the height of a bitmap font
239 int FGAPIENTRY glutBitmapHeight( void* fontID )
242 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapHeight" );
243 font = fghFontByID( fontID );
246 fgWarning("glutBitmapHeight: bitmap font 0x%08x not found. Make sure you're not passing a stroke font.\n",fontID);
253 * Draw a stroke character
255 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
257 const SFG_StrokeChar *schar;
258 const SFG_StrokeStrip *strip;
260 SFG_StrokeFont* font;
261 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeCharacter" );
262 font = fghStrokeByID( fontID );
265 fgWarning("glutStrokeCharacter: stroke font 0x%08x not found. Make sure you're not passing a bitmap font.\n",fontID);
268 freeglut_return_if_fail( character >= 0 );
269 freeglut_return_if_fail( character < font->Quantity );
271 schar = font->Characters[ character ];
272 freeglut_return_if_fail( schar );
273 strip = schar->Strips;
275 for( i = 0; i < schar->Number; i++, strip++ )
277 glBegin( GL_LINE_STRIP );
278 for( j = 0; j < strip->Number; j++ )
279 glVertex2f( strip->Vertices[ j ].X, strip->Vertices[ j ].Y );
281 glBegin( GL_POINTS );
282 for( j = 0; j < strip->Number; j++ )
283 glVertex2f( strip->Vertices[ j ].X, strip->Vertices[ j ].Y );
286 glTranslatef( schar->Right, 0.0, 0.0 );
289 void FGAPIENTRY glutStrokeString( void* fontID, const unsigned char *string )
294 SFG_StrokeFont* font;
295 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeString" );
296 font = fghStrokeByID( fontID );
299 fgWarning("glutStrokeString: stroke font 0x%08x not found. Make sure you're not passing a bitmap font.\n",fontID);
302 if ( !string || ! *string )
306 * Step through the string, drawing each character.
307 * A newline will simply translate the next character's insertion
308 * point back to the start of the line and down one line.
310 while( ( c = *string++) )
311 if( c < font->Quantity )
315 glTranslatef ( -length, -( float )( font->Height ), 0.0 );
318 else /* Not an EOL, draw the bitmap character */
320 const SFG_StrokeChar *schar = font->Characters[ c ];
323 const SFG_StrokeStrip *strip = schar->Strips;
325 for( i = 0; i < schar->Number; i++, strip++ )
327 glBegin( GL_LINE_STRIP );
328 for( j = 0; j < strip->Number; j++ )
329 glVertex2f( strip->Vertices[ j ].X,
330 strip->Vertices[ j ].Y);
335 length += schar->Right;
336 glTranslatef( schar->Right, 0.0, 0.0 );
343 * Return the width in pixels of a stroke character
345 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
347 const SFG_StrokeChar *schar;
348 SFG_StrokeFont* font;
349 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeWidth" );
350 font = fghStrokeByID( fontID );
353 fgWarning("glutStrokeWidth: stroke font 0x%08x not found. Make sure you're not passing a bitmap font.\n",fontID);
356 freeglut_return_val_if_fail( ( character >= 0 ) &&
357 ( character < font->Quantity ),
360 schar = font->Characters[ character ];
361 freeglut_return_val_if_fail( schar, 0 );
363 return ( int )( schar->Right + 0.5 );
367 * Return the width of a string drawn using a stroke font
369 int FGAPIENTRY glutStrokeLength( void* fontID, const unsigned char* string )
373 float this_line_length = 0.0;
374 SFG_StrokeFont* font;
375 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeLength" );
376 font = fghStrokeByID( fontID );
379 fgWarning("glutStrokeLength: stroke font 0x%08x not found. Make sure you're not passing a bitmap font.\n",fontID);
382 if ( !string || ! *string )
385 while( ( c = *string++) )
386 if( c < font->Quantity )
388 if( c == '\n' ) /* EOL; reset the length of this line */
390 if( length < this_line_length )
391 length = this_line_length;
392 this_line_length = 0.0;
394 else /* Not an EOL, increment the length of this line */
396 const SFG_StrokeChar *schar = font->Characters[ c ];
398 this_line_length += schar->Right;
401 if( length < this_line_length )
402 length = this_line_length;
403 return( int )( length + 0.5 );
407 * Returns the height of a stroke font
409 GLfloat FGAPIENTRY glutStrokeHeight( void* fontID )
411 SFG_StrokeFont* font;
412 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeHeight" );
413 font = fghStrokeByID( fontID );
416 fgWarning("glutStrokeHeight: stroke font 0x%08x not found. Make sure you're not passing a bitmap font.\n",fontID);
422 /*** END OF FILE ***/