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 "freeglut_internal.h"
38 * TODO BEFORE THE STABLE RELEASE:
43 /* -- IMPORT DECLARATIONS -------------------------------------------------- */
46 * These are the font faces defined in freeglut_font_data.c file:
48 extern SFG_Font fgFontFixed8x13;
49 extern SFG_Font fgFontFixed9x15;
50 extern SFG_Font fgFontHelvetica10;
51 extern SFG_Font fgFontHelvetica12;
52 extern SFG_Font fgFontHelvetica18;
53 extern SFG_Font fgFontTimesRoman10;
54 extern SFG_Font fgFontTimesRoman24;
55 extern SFG_StrokeFont fgStrokeRoman;
56 extern SFG_StrokeFont fgStrokeMonoRoman;
59 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
62 * Matches a font ID with a SFG_Font structure pointer.
63 * This was changed to match the GLUT header style.
65 static SFG_Font* fghFontByID( void* font )
67 if( font == GLUT_BITMAP_8_BY_13 ) return( &fgFontFixed8x13 );
68 if( font == GLUT_BITMAP_9_BY_15 ) return( &fgFontFixed9x15 );
69 if( font == GLUT_BITMAP_HELVETICA_10 ) return( &fgFontHelvetica10 );
70 if( font == GLUT_BITMAP_HELVETICA_12 ) return( &fgFontHelvetica12 );
71 if( font == GLUT_BITMAP_HELVETICA_18 ) return( &fgFontHelvetica18 );
72 if( font == GLUT_BITMAP_TIMES_ROMAN_10 ) return( &fgFontTimesRoman10 );
73 if( font == GLUT_BITMAP_TIMES_ROMAN_24 ) return( &fgFontTimesRoman24 );
74 fgError( "font 0x%08x not found", font );
75 return 0; /*** XXX NOT REACHED ***/
79 * Matches a font ID with a SFG_StrokeFont structure pointer.
80 * This was changed to match the GLUT header style.
82 static SFG_StrokeFont* fghStrokeByID( void* font )
84 if( font == GLUT_STROKE_ROMAN ) return( &fgStrokeRoman );
85 if( font == GLUT_STROKE_MONO_ROMAN ) return( &fgStrokeMonoRoman );
86 fgError( "stroke font 0x%08x not found", font );
87 return 0; /*** XXX NOT REACHED ***/
91 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
94 * Draw a bitmap character
96 void FGAPIENTRY glutBitmapCharacter( void* fontID, int character )
99 SFG_Font* font = fghFontByID( fontID );
101 freeglut_return_if_fail( character >= 1 && character < 256 );
104 * Find the character we want to draw (???)
106 face = font->Characters[ character - 1 ];
108 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
109 glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE );
110 glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE );
111 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
112 glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
113 glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
114 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
116 face[ 0 ], font->Height, /* The bitmap's width and height */
117 font->xorig, font->yorig, /* The origin in the font glyph */
118 (float)(face[ 0 ]), 0.0, /* The raster advance -- inc. x,y */
119 (face + 1) /* The packed bitmap data... */
124 void FGAPIENTRY glutBitmapString( void* fontID, const unsigned char *string )
127 int numchar = strlen ( string ) ;
128 SFG_Font* font = fghFontByID( fontID );
129 float raster_position[4] ;
131 glGetFloatv ( GL_CURRENT_RASTER_POSITION, raster_position ) ;
132 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
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 * Step through the string, drawing each character.
142 * A newline will simply translate the next character's insertion
143 * point back to the start of the line and down one line.
145 for( c = 0; c < numchar; c++ )
147 if ( string[c] == '\n' )
149 raster_position[1] -= (float)font->Height ;
150 glRasterPos4fv ( raster_position ) ;
152 else /* Not an EOL, draw the bitmap character */
154 const GLubyte* face = font->Characters[ string[ c ] - 1 ] ;
157 face[ 0 ], font->Height, /* The bitmap's width and height */
158 font->xorig, font->yorig, /* The origin in the font glyph */
159 (float)(face[ 0 ]), 0.0, /* The raster advance -- inc. x,y */
160 (face + 1) /* The packed bitmap data... */
168 * Returns the width in pixels of a font's character
170 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
172 SFG_Font* font = fghFontByID( fontID );
174 freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
175 return( *(font->Characters[ character - 1 ]) );
179 * Return the width of a string drawn using a bitmap font
181 int FGAPIENTRY glutBitmapLength( void* fontID, const unsigned char* string )
183 int c, length = 0, this_line_length = 0;
184 SFG_Font* font = fghFontByID( fontID );
185 int numchar = strlen ( string ) ;
187 for( c = 0; c < numchar; c++ )
189 if ( string[ c ] == '\n' ) /* EOL; reset the length of this line */
191 if ( length < this_line_length ) length = this_line_length ;
192 this_line_length = 0 ;
194 else /* Not an EOL, increment the length of this line */
195 this_line_length += *(font->Characters[ string[ c ] - 1 ]) ;
197 if ( length < this_line_length )
198 length = this_line_length ;
204 * Returns the height of a bitmap font
206 int FGAPIENTRY glutBitmapHeight( void* fontID )
208 SFG_Font* font = fghFontByID( fontID );
209 return( font->Height );
213 * Draw a stroke character
215 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
217 const SFG_StrokeChar *schar;
218 const SFG_StrokeStrip *strip;
220 SFG_StrokeFont* font = fghStrokeByID( fontID );
222 freeglut_return_if_fail( character >= 0 && character < font->Quantity );
224 schar = font->Characters[character];
225 freeglut_return_if_fail( schar );
226 strip = schar->Strips;
228 for (i = 0; i < schar->Number; i++, strip++)
230 glBegin(GL_LINE_STRIP);
231 for(j = 0; j < strip->Number; j++)
233 glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
237 glTranslatef(schar->Right, 0.0, 0.0);
240 void FGAPIENTRY glutStrokeString( void* fontID, const unsigned char *string )
243 int numchar = strlen ( string ) ;
245 SFG_StrokeFont* font = fghStrokeByID( fontID );
248 * Step through the string, drawing each character.
249 * A newline will simply translate the next character's insertion
250 * point back to the start of the line and down one line.
252 for( c = 0; c < numchar; c++ )
254 if ( string[ c ] < font->Quantity )
256 if ( string[c] == '\n' )
258 glTranslatef ( -length, -(float)(font->Height), 0.0 ) ;
261 else /* Not an EOL, draw the bitmap character */
263 const SFG_StrokeChar *schar = font->Characters[string[c]];
266 const SFG_StrokeStrip *strip = schar->Strips;
268 for (i = 0; i < schar->Number; i++, strip++)
270 glBegin(GL_LINE_STRIP);
271 for(j = 0; j < strip->Number; j++)
272 glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
277 length += schar->Right ;
278 glTranslatef(schar->Right, 0.0, 0.0);
286 * Return the width in pixels of a stroke character
288 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
290 const SFG_StrokeChar *schar;
291 SFG_StrokeFont* font = fghStrokeByID( fontID );
293 freeglut_return_val_if_fail( character >= 0 && character < font->Quantity, 0 );
294 schar = font->Characters[character];
295 freeglut_return_val_if_fail( schar, 0 );
297 return ((int)(schar->Right + 0.5));
301 * Return the width of a string drawn using a stroke font
303 int FGAPIENTRY glutStrokeLength( void* fontID, const unsigned char* string )
307 float this_line_length = 0.0 ;
308 SFG_StrokeFont* font = fghStrokeByID( fontID );
309 int numchar = strlen ( string ) ;
311 for( c = 0; c < numchar; c++ )
313 if ( string[ c ] < font->Quantity )
315 if ( string[ c ] == '\n' ) /* EOL; reset the length of this line */
317 if ( length < this_line_length ) length = this_line_length ;
318 this_line_length = 0.0 ;
320 else /* Not an EOL, increment the length of this line */
322 const SFG_StrokeChar *schar = font->Characters[string[c]];
324 this_line_length += schar->Right ;
328 if ( length < this_line_length )
329 length = this_line_length ;
330 return( (int)(length+0.5) );
334 * Returns the height of a stroke font
336 GLfloat FGAPIENTRY glutStrokeHeight( void* fontID )
338 SFG_StrokeFont* font = fghStrokeByID( fontID );
339 return( font->Height );
342 /*** END OF FILE ***/