Removed glib dependancy
[freeglut] / freeglut-1.3 / freeglut_font.c
1 /*
2  * freeglut_font.c
3  *
4  * Bitmap and stroke fonts displaying.
5  *
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
9  *
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:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
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.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #define  G_LOG_DOMAIN  "freeglut-font"
33
34 #include "../include/GL/freeglut.h"
35 #include "../include/GL/freeglut_internal.h"
36
37 /*
38  * TODO BEFORE THE STABLE RELEASE:
39  *
40  *  glutStrokeCharacter()       -- stroke fonts not implemented, uses a bitmap font instead
41  *  glutStrokeWidth()           -- stroke fonts not implemented, uses a bitmap font instead
42  */
43
44 /* -- IMPORT DECLARATIONS -------------------------------------------------- */
45
46 /*
47  * These are the font faces defined in freeglut_font_data.c file:
48  */
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;
56
57 /*
58  * This is for GLUT binary compatibility, as suggested by Steve Baker
59  */
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;
70 #endif
71
72
73 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
74
75 /*
76  * Matches a font ID with a SFG_Font structure pointer.
77  * This was changed to match the GLUT header style.
78  */
79 static SFG_Font* fghFontByID( void* font )
80 {
81     /*
82      * Try matching the font ID and the font data structure
83      */
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 );
91
92     /*
93      * This probably is the library user's fault
94      */
95     fgError( "font 0x%08x not found", font );
96
97     return 0;
98 }
99
100
101 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
102
103 /*
104  * Draw a bitmap character
105  */
106 void FGAPIENTRY glutBitmapCharacter( void* fontID, int character )
107 {
108     const GLubyte* face;
109
110     /*
111      * First of all we'll need a font to use
112      */
113     SFG_Font* font = fghFontByID( fontID );
114
115     /*
116      * Make sure the character we want to output is valid
117      */
118     freeglut_return_if_fail( character > 0 && character < 256 );
119
120     /*
121      * Then find the character we want to draw
122      */
123     face = font->Characters[ character - 1 ];
124
125     /*
126      * Save the old pixel store settings
127      */
128     glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
129
130     /*
131      * Set up the pixel unpacking ways
132      */
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        );
139
140     /*
141      * We'll use a glBitmap call to draw the font.
142      */
143     glBitmap(
144         face[ 0 ], font->Height,    /* The bitmap's width and height */
145         0, 0,                       /* The origin -- what the hell?  */
146         face[ 0 ] + 1, 0,           /* The raster advance -- inc. x  */
147         (face + 1)                  /* The packed bitmap data...     */
148     );
149
150     /*
151      * Restore the old pixel store settings
152      */
153     glPopClientAttrib();
154 }
155
156 void FGAPIENTRY glutBitmapString( void* fontID, const char *string )
157 {
158     int i;
159
160     for( i=0; i<strlen( string ); i++ )
161         glutBitmapCharacter( fontID, string[ i ] );
162 }
163
164 /*
165  * Returns the width in pixels of a font's character
166  */
167 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
168 {
169     /*
170      * First of all, grab the font we need
171      */
172     SFG_Font* font = fghFontByID( fontID );
173
174     /*
175      * Make sure the character we want to output is valid
176      */
177     freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
178
179     /*
180          * Scan the font looking for the specified character
181      */
182     return( *(font->Characters[ character - 1 ]) + 1 );
183 }
184
185 /*
186  * Draw a stroke character
187  */
188 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
189 {
190     /*
191      * Stroke fonts are not supported yet, use a bitmap font instead
192      */
193     glutBitmapCharacter( GLUT_BITMAP_8_BY_13, character );
194 }
195
196 /*
197  * Return the width in pixels of a stroke character
198  */
199 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
200 {
201     /*
202      * Stroke fonts are not supported yet, use a bitmap font instead
203      */
204     return( glutBitmapWidth( GLUT_BITMAP_8_BY_13, character ) );
205 }
206
207 /*
208  * Return the width of a string drawn using a bitmap font
209  */
210 int FGAPIENTRY glutBitmapLength( void* fontID, const char* string )
211 {
212     int i, length = 0;
213
214     /*
215      * Using glutBitmapWidth() function to calculate the result
216      */
217     for( i=0; i<strlen( string ); i++ )
218         length += glutBitmapWidth( fontID, string[ i ] );
219
220     /*
221      * Return the result now
222      */
223     return( length );
224 }
225
226 /*
227  * Return the width of a string drawn using a stroke font
228  */
229 int FGAPIENTRY glutStrokeLength( void* fontID, const char* string )
230 {
231     int i, length = 0;
232
233     /*
234      * Using glutStrokeWidth() function to calculate the result
235      */
236     for( i=0; i<strlen( string ); i++ )
237         length += glutStrokeWidth( fontID, string[ i ] );
238
239     /*
240      * Return the result now
241      */
242     return( length );
243 }
244
245 /*
246  * Returns the height of a bitmap font
247  */
248 int FGAPIENTRY glutBitmapHeight( void* fontID )
249 {
250     /*
251      * See which font are we queried about
252      */
253     SFG_Font* font = fghFontByID( fontID );
254
255     /*
256      * Return the character set's height
257      */
258     return( font->Height );
259 }
260
261 /*
262  * Returns the height of a stroke font
263  */
264 int FGAPIENTRY glutStrokeHeight( void* font )
265 {
266     /*
267      * Stroke fonts are currently not implemented.
268      * Using GLUT_BITMAP_8_BY_13 bitmap font instead
269      */
270     return( glutBitmapHeight( GLUT_BITMAP_8_BY_13 ) );
271 }
272
273 /*** END OF FILE ***/