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 fg_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 );
282 if (fgState.StrokeFontDrawJoinDots)
284 glBegin( GL_POINTS );
285 for( j = 0; j < strip->Number; j++ )
286 glVertex2f( strip->Vertices[ j ].X, strip->Vertices[ j ].Y );
290 glTranslatef( schar->Right, 0.0, 0.0 );
293 void FGAPIENTRY glutStrokeString( void* fontID, const unsigned char *string )
298 SFG_StrokeFont* font;
299 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeString" );
300 font = fghStrokeByID( fontID );
303 fgWarning("glutStrokeString: stroke font 0x%08x not found. Make sure you're not passing a bitmap font.\n",fontID);
306 if ( !string || ! *string )
310 * Step through the string, drawing each character.
311 * A newline will simply translate the next character's insertion
312 * point back to the start of the line and down one line.
314 while( ( c = *string++) )
315 if( c < font->Quantity )
319 glTranslatef ( -length, -( float )( font->Height ), 0.0 );
322 else /* Not an EOL, draw the bitmap character */
324 const SFG_StrokeChar *schar = font->Characters[ c ];
327 const SFG_StrokeStrip *strip = schar->Strips;
329 for( i = 0; i < schar->Number; i++, strip++ )
331 glBegin( GL_LINE_STRIP );
332 for( j = 0; j < strip->Number; j++ )
333 glVertex2f( strip->Vertices[ j ].X,
334 strip->Vertices[ j ].Y);
339 length += schar->Right;
340 glTranslatef( schar->Right, 0.0, 0.0 );
347 * Return the width in pixels of a stroke character
349 GLfloat FGAPIENTRY glutStrokeWidthf( void* fontID, int character )
351 const SFG_StrokeChar *schar;
352 SFG_StrokeFont* font;
353 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeWidth" );
354 font = fghStrokeByID( fontID );
357 fgWarning("glutStrokeWidth: stroke font 0x%08x not found. Make sure you're not passing a bitmap font.\n",fontID);
360 freeglut_return_val_if_fail( ( character >= 0 ) &&
361 ( character < font->Quantity ),
364 schar = font->Characters[ character ];
365 freeglut_return_val_if_fail( schar, 0 );
369 int FGAPIENTRY glutStrokeWidth(void* fontID, int character)
371 return ( int )( glutStrokeWidthf(fontID,character) + 0.5f );
375 * Return the width of a string drawn using a stroke font
377 GLfloat FGAPIENTRY glutStrokeLengthf( void* fontID, const unsigned char* string )
380 GLfloat length = 0.0;
381 GLfloat this_line_length = 0.0;
382 SFG_StrokeFont* font;
383 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeLength" );
384 font = fghStrokeByID( fontID );
387 fgWarning("glutStrokeLength: stroke font 0x%08x not found. Make sure you're not passing a bitmap font.\n",fontID);
390 if ( !string || ! *string )
393 while( ( c = *string++) )
394 if( c < font->Quantity )
396 if( c == '\n' ) /* EOL; reset the length of this line */
398 if( length < this_line_length )
399 length = this_line_length;
400 this_line_length = 0.0;
402 else /* Not an EOL, increment the length of this line */
404 const SFG_StrokeChar *schar = font->Characters[ c ];
406 this_line_length += schar->Right;
409 if( length < this_line_length )
410 length = this_line_length;
413 int FGAPIENTRY glutStrokeLength( void* fontID, const unsigned char* string )
415 return( int )( glutStrokeLengthf(fontID,string) + 0.5f );
419 * Returns the height of a stroke font
421 GLfloat FGAPIENTRY glutStrokeHeight( void* fontID )
423 SFG_StrokeFont* font;
424 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeHeight" );
425 font = fghStrokeByID( fontID );
428 fgWarning("glutStrokeHeight: stroke font 0x%08x not found. Make sure you're not passing a bitmap font.\n",fontID);
434 /*** END OF FILE ***/