26f7d6303cf356562481984b881a4e7496c59ae5
[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     g_error( "font 0x%08x not found", font );
96 }
97
98
99 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
100
101 /*
102  * Draw a bitmap character
103  */
104 void FGAPIENTRY glutBitmapCharacter( void* fontID, int character )
105 {
106     const guchar* face;
107
108     /*
109      * First of all we'll need a font to use
110      */
111     SFG_Font* font = fghFontByID( fontID );
112
113     /*
114      * Make sure the character we want to output is valid
115      */
116     freeglut_return_if_fail( character > 0 && character < 256 );
117
118     /*
119      * Then find the character we want to draw
120      */
121     face = font->Characters[ character - 1 ];
122
123     /*
124      * Save the old pixel store settings
125      */
126     glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
127
128     /*
129      * Set up the pixel unpacking ways
130      */
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        );
137
138     /*
139      * We'll use a glBitmap call to draw the font.
140      */
141     glBitmap(
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...     */
146     );
147
148     /*
149      * Restore the old pixel store settings
150      */
151     glPopClientAttrib();
152 }
153
154 /*
155  * Returns the width in pixels of a font's character
156  */
157 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
158 {
159     /*
160      * First of all, grab the font we need
161      */
162     SFG_Font* font = fghFontByID( fontID );
163
164     /*
165      * Make sure the character we want to output is valid
166      */
167     freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
168
169     /*
170          * Scan the font looking for the specified character
171      */
172     return( *(font->Characters[ character - 1 ]) + 1 );
173 }
174
175 /*
176  * Draw a stroke character
177  */
178 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
179 {
180     /*
181      * Stroke fonts are not supported yet, use a bitmap font instead
182      */
183     glutBitmapCharacter( GLUT_BITMAP_8_BY_13, character );
184 }
185
186 /*
187  * Return the width in pixels of a stroke character
188  */
189 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
190 {
191     /*
192      * Stroke fonts are not supported yet, use a bitmap font instead
193      */
194     return( glutBitmapWidth( GLUT_BITMAP_8_BY_13, character ) );
195 }
196
197 /*
198  * Return the width of a string drawn using a bitmap font
199  */
200 int FGAPIENTRY glutBitmapLength( void* fontID, const char* string )
201 {
202     gint i, length = 0;
203
204     /*
205      * Using glutBitmapWidth() function to calculate the result
206      */
207     for( i=0; i<(gint) strlen( string ); i++ )
208         length += glutBitmapWidth( fontID, string[ i ] );
209
210     /*
211      * Return the result now
212      */
213     return( length );
214 }
215
216 /*
217  * Return the width of a string drawn using a stroke font
218  */
219 int FGAPIENTRY glutStrokeLength( void* fontID, const char* string )
220 {
221     gint i, length = 0;
222
223     /*
224      * Using glutStrokeWidth() function to calculate the result
225      */
226     for( i=0; i<(gint) strlen( string ); i++ )
227         length += glutStrokeWidth( fontID, string[ i ] );
228
229     /*
230      * Return the result now
231      */
232     return( length );
233 }
234
235 /*
236  * Returns the height of a bitmap font
237  */
238 int FGAPIENTRY glutBitmapHeight( void* fontID )
239 {
240     /*
241      * See which font are we queried about
242      */
243     SFG_Font* font = fghFontByID( fontID );
244
245     /*
246      * Return the character set's height
247      */
248     return( font->Height );
249 }
250
251 /*
252  * Returns the height of a stroke font
253  */
254 int FGAPIENTRY glutStrokeHeight( void* font )
255 {
256     /*
257      * Stroke fonts are currently not implemented.
258      * Using GLUT_BITMAP_8_BY_13 bitmap font instead
259      */
260     return( glutBitmapHeight( GLUT_BITMAP_8_BY_13 ) );
261 }
262
263 /*** END OF FILE ***/