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;
58 * This is for GLUT binary compatibility, as suggested by Steve Baker
60 #if TARGET_HOST_UNIX_X11
61 void* glutStrokeRoman;
62 void* glutStrokeMonoRoman;
63 void* glutBitmap9By15;
64 void* glutBitmap8By13;
65 void* glutBitmapTimesRoman10;
66 void* glutBitmapTimesRoman24;
67 void* glutBitmapHelvetica10;
68 void* glutBitmapHelvetica12;
69 void* glutBitmapHelvetica18;
73 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
76 * Matches a font ID with a SFG_Font structure pointer.
77 * This was changed to match the GLUT header style.
79 static SFG_Font* fghFontByID( void* font )
82 * Try matching the font ID and the font data structure
84 if( font == GLUT_BITMAP_8_BY_13 ) return( &fgFontFixed8x13 );
85 if( font == GLUT_BITMAP_9_BY_15 ) return( &fgFontFixed9x15 );
86 if( font == GLUT_BITMAP_HELVETICA_10 ) return( &fgFontHelvetica10 );
87 if( font == GLUT_BITMAP_HELVETICA_12 ) return( &fgFontHelvetica12 );
88 if( font == GLUT_BITMAP_HELVETICA_18 ) return( &fgFontHelvetica18 );
89 if( font == GLUT_BITMAP_TIMES_ROMAN_10 ) return( &fgFontTimesRoman10 );
90 if( font == GLUT_BITMAP_TIMES_ROMAN_24 ) return( &fgFontTimesRoman24 );
93 * This probably is the library user's fault
95 g_error( "font 0x%08x not found", font );
99 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
102 * Draw a bitmap character
104 void FGAPIENTRY glutBitmapCharacter( void* fontID, int character )
109 * First of all we'll need a font to use
111 SFG_Font* font = fghFontByID( fontID );
114 * Make sure the character we want to output is valid
116 freeglut_return_if_fail( character > 0 && character < 256 );
119 * Then find the character we want to draw
121 face = font->Characters[ character - 1 ];
124 * Save the old pixel store settings
126 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
129 * Set up the pixel unpacking ways
131 glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE );
132 glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE );
133 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
134 glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
135 glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
136 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
139 * We'll use a glBitmap call to draw the font.
142 face[ 0 ], font->Height, /* The bitmap's width and height */
143 0, 0, /* The origin -- what the hell? */
144 face[ 0 ] + 1, 0, /* The raster advance -- inc. x */
145 (face + 1) /* The packed bitmap data... */
149 * Restore the old pixel store settings
155 * Returns the width in pixels of a font's character
157 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
160 * First of all, grab the font we need
162 SFG_Font* font = fghFontByID( fontID );
165 * Make sure the character we want to output is valid
167 freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
170 * Scan the font looking for the specified character
172 return( *(font->Characters[ character - 1 ]) + 1 );
176 * Draw a stroke character
178 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
181 * Stroke fonts are not supported yet, use a bitmap font instead
183 glutBitmapCharacter( GLUT_BITMAP_8_BY_13, character );
187 * Return the width in pixels of a stroke character
189 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
192 * Stroke fonts are not supported yet, use a bitmap font instead
194 return( glutBitmapWidth( GLUT_BITMAP_8_BY_13, character ) );
198 * Return the width of a string drawn using a bitmap font
200 int FGAPIENTRY glutBitmapLength( void* fontID, const char* string )
205 * Using glutBitmapWidth() function to calculate the result
207 for( i=0; i<(gint) strlen( string ); i++ )
208 length += glutBitmapWidth( fontID, string[ i ] );
211 * Return the result now
217 * Return the width of a string drawn using a stroke font
219 int FGAPIENTRY glutStrokeLength( void* fontID, const char* string )
224 * Using glutStrokeWidth() function to calculate the result
226 for( i=0; i<(gint) strlen( string ); i++ )
227 length += glutStrokeWidth( fontID, string[ i ] );
230 * Return the result now
236 * Returns the height of a bitmap font
238 int FGAPIENTRY glutBitmapHeight( void* fontID )
241 * See which font are we queried about
243 SFG_Font* font = fghFontByID( fontID );
246 * Return the character set's height
248 return( font->Height );
252 * Returns the height of a stroke font
254 int FGAPIENTRY glutStrokeHeight( void* font )
257 * Stroke fonts are currently not implemented.
258 * Using GLUT_BITMAP_8_BY_13 bitmap font instead
260 return( glutBitmapHeight( GLUT_BITMAP_8_BY_13 ) );
263 /*** END OF FILE ***/