Adding a Frequently Asked Questions file
[freeglut] / src / 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 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
30
31 /*
32  * TODO BEFORE THE STABLE RELEASE:
33  *
34  *  Test things out ...
35  */
36
37 /* -- IMPORT DECLARATIONS -------------------------------------------------- */
38
39 /*
40  * These are the font faces defined in freeglut_font_data.c file:
41  */
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;
51
52
53 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
54
55 /*
56  * Matches a font ID with a SFG_Font structure pointer.
57  * This was changed to match the GLUT header style.
58  */
59 static SFG_Font* fghFontByID( void* font )
60 {
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;
75
76     fgWarning( "font 0x%08x not found", font );
77     return 0;
78 }
79
80 /*
81  * Matches a font ID with a SFG_StrokeFont structure pointer.
82  * This was changed to match the GLUT header style.
83  */
84 static SFG_StrokeFont* fghStrokeByID( void* font )
85 {
86     if( font == GLUT_STROKE_ROMAN      )
87         return &fgStrokeRoman;
88     if( font == GLUT_STROKE_MONO_ROMAN )
89         return &fgStrokeMonoRoman;
90
91     fgWarning( "stroke font 0x%08x not found", font );
92     return 0;
93 }
94
95
96 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
97
98 /*
99  * Draw a bitmap character
100  */
101 void FGAPIENTRY glutBitmapCharacter( void* fontID, int character )
102 {
103     const GLubyte* face;
104     SFG_Font* font;
105     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapCharacter" );
106     font = fghFontByID( fontID );
107     freeglut_return_if_fail( ( character >= 1 )&&( character < 256 ) );
108     freeglut_return_if_fail( font );
109
110     /*
111      * Find the character we want to draw (???)
112      */
113     face = font->Characters[ character ];
114
115     glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
116     glPixelStorei( GL_UNPACK_SWAP_BYTES,  GL_FALSE );
117     glPixelStorei( GL_UNPACK_LSB_FIRST,   GL_FALSE );
118     glPixelStorei( GL_UNPACK_ROW_LENGTH,  0        );
119     glPixelStorei( GL_UNPACK_SKIP_ROWS,   0        );
120     glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0        );
121     glPixelStorei( GL_UNPACK_ALIGNMENT,   1        );
122     glBitmap(
123         face[ 0 ], font->Height,      /* The bitmap's width and height  */
124         font->xorig, font->yorig,     /* The origin in the font glyph   */
125         ( float )( face[ 0 ] ), 0.0,  /* The raster advance -- inc. x,y */
126         ( face + 1 )                  /* The packed bitmap data...      */
127     );
128     glPopClientAttrib( );
129 }
130
131 void FGAPIENTRY glutBitmapString( void* fontID, const unsigned char *string )
132 {
133     unsigned char c;
134     float x = 0.0f ;
135     SFG_Font* font;
136     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapString" );
137     font = fghFontByID( fontID );
138     freeglut_return_if_fail( font );
139     if ( !string || ! *string )
140         return;
141
142     glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
143     glPixelStorei( GL_UNPACK_SWAP_BYTES,  GL_FALSE );
144     glPixelStorei( GL_UNPACK_LSB_FIRST,   GL_FALSE );
145     glPixelStorei( GL_UNPACK_ROW_LENGTH,  0        );
146     glPixelStorei( GL_UNPACK_SKIP_ROWS,   0        );
147     glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0        );
148     glPixelStorei( GL_UNPACK_ALIGNMENT,   1        );
149
150     /*
151      * Step through the string, drawing each character.
152      * A newline will simply translate the next character's insertion
153      * point back to the start of the line and down one line.
154      */
155     while( ( c = *string++) )
156         if( c == '\n' )
157         {
158             glBitmap ( 0, 0, 0, 0, -x, (float) -font->Height, NULL );
159             x = 0.0f;
160         }
161         else  /* Not an EOL, draw the bitmap character */
162         {
163             const GLubyte* face = font->Characters[ c ];
164
165             glBitmap(
166                 face[ 0 ], font->Height,     /* Bitmap's width and height    */
167                 font->xorig, font->yorig,    /* The origin in the font glyph */
168                 ( float )( face[ 0 ] ), 0.0, /* The raster advance; inc. x,y */
169                 ( face + 1 )                 /* The packed bitmap data...    */
170             );
171
172             x += ( float )( face[ 0 ] );
173         }
174
175     glPopClientAttrib( );
176 }
177
178 /*
179  * Returns the width in pixels of a font's character
180  */
181 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
182 {
183     SFG_Font* font;
184     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapWidth" );
185     font = fghFontByID( fontID );
186     freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
187     freeglut_return_val_if_fail( font, 0 );
188     return *( font->Characters[ character ] );
189 }
190
191 /*
192  * Return the width of a string drawn using a bitmap font
193  */
194 int FGAPIENTRY glutBitmapLength( void* fontID, const unsigned char* string )
195 {
196     unsigned char c;
197     int length = 0, this_line_length = 0;
198     SFG_Font* font;
199     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapLength" );
200     font = fghFontByID( fontID );
201     freeglut_return_val_if_fail( font, 0 );
202     if ( !string || ! *string )
203         return 0;
204
205     while( ( c = *string++) )
206     {
207         if( c != '\n' )/* Not an EOL, increment length of line */
208             this_line_length += *( font->Characters[ c ]);
209         else  /* EOL; reset the length of this line */
210         {
211             if( length < this_line_length )
212                 length = this_line_length;
213             this_line_length = 0;
214         }
215     }
216     if ( length < this_line_length )
217         length = this_line_length;
218
219     return length;
220 }
221
222 /*
223  * Returns the height of a bitmap font
224  */
225 int FGAPIENTRY glutBitmapHeight( void* fontID )
226 {
227     SFG_Font* font;
228     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutBitmapHeight" );
229     font = fghFontByID( fontID );
230     freeglut_return_val_if_fail( font, 0 );
231     return font->Height;
232 }
233
234 /*
235  * Draw a stroke character
236  */
237 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
238 {
239     const SFG_StrokeChar *schar;
240     const SFG_StrokeStrip *strip;
241     int i, j;
242     SFG_StrokeFont* font;
243     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeCharacter" );
244     font = fghStrokeByID( fontID );
245     freeglut_return_if_fail( character >= 0 );
246     freeglut_return_if_fail( character < font->Quantity );
247     freeglut_return_if_fail( font );
248
249     schar = font->Characters[ character ];
250     freeglut_return_if_fail( schar );
251     strip = schar->Strips;
252
253     for( i = 0; i < schar->Number; i++, strip++ )
254     {
255         glBegin( GL_LINE_STRIP );
256         for( j = 0; j < strip->Number; j++ )
257             glVertex2f( strip->Vertices[ j ].X, strip->Vertices[ j ].Y );
258         glEnd( );
259     }
260     glTranslatef( schar->Right, 0.0, 0.0 );
261 }
262
263 void FGAPIENTRY glutStrokeString( void* fontID, const unsigned char *string )
264 {
265     unsigned char c;
266     int i, j;
267     float length = 0.0;
268     SFG_StrokeFont* font;
269     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeString" );
270     font = fghStrokeByID( fontID );
271     freeglut_return_if_fail( font );
272     if ( !string || ! *string )
273         return;
274
275     /*
276      * Step through the string, drawing each character.
277      * A newline will simply translate the next character's insertion
278      * point back to the start of the line and down one line.
279      */
280     while( ( c = *string++) )
281         if( c < font->Quantity )
282         {
283             if( c == '\n' )
284             {
285                 glTranslatef ( -length, -( float )( font->Height ), 0.0 );
286                 length = 0.0;
287             }
288             else  /* Not an EOL, draw the bitmap character */
289             {
290                 const SFG_StrokeChar *schar = font->Characters[ c ];
291                 if( schar )
292                 {
293                     const SFG_StrokeStrip *strip = schar->Strips;
294
295                     for( i = 0; i < schar->Number; i++, strip++ )
296                     {
297                         glBegin( GL_LINE_STRIP );
298                         for( j = 0; j < strip->Number; j++ )
299                             glVertex2f( strip->Vertices[ j ].X,
300                                         strip->Vertices[ j ].Y);
301
302                         glEnd( );
303                     }
304
305                     length += schar->Right;
306                     glTranslatef( schar->Right, 0.0, 0.0 );
307                 }
308             }
309         }
310 }
311
312 /*
313  * Return the width in pixels of a stroke character
314  */
315 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
316 {
317     const SFG_StrokeChar *schar;
318     SFG_StrokeFont* font;
319     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeWidth" );
320     font = fghStrokeByID( fontID );
321     freeglut_return_val_if_fail( ( character >= 0 ) &&
322                                  ( character < font->Quantity ),
323                                  0
324     );
325     freeglut_return_val_if_fail( font, 0 );
326     schar = font->Characters[ character ];
327     freeglut_return_val_if_fail( schar, 0 );
328
329     return ( int )( schar->Right + 0.5 );
330 }
331
332 /*
333  * Return the width of a string drawn using a stroke font
334  */
335 int FGAPIENTRY glutStrokeLength( void* fontID, const unsigned char* string )
336 {
337     unsigned char c;
338     float length = 0.0;
339     float this_line_length = 0.0;
340     SFG_StrokeFont* font;
341     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeLength" );
342     font = fghStrokeByID( fontID );
343     freeglut_return_val_if_fail( font, 0 );
344     if ( !string || ! *string )
345         return 0;
346
347     while( ( c = *string++) )
348         if( c < font->Quantity )
349         {
350             if( c == '\n' ) /* EOL; reset the length of this line */
351             {
352                 if( length < this_line_length )
353                     length = this_line_length;
354                 this_line_length = 0.0;
355             }
356             else  /* Not an EOL, increment the length of this line */
357             {
358                 const SFG_StrokeChar *schar = font->Characters[ c ];
359                 if( schar )
360                     this_line_length += schar->Right;
361             }
362         }
363     if( length < this_line_length )
364         length = this_line_length;
365     return( int )( length + 0.5 );
366 }
367
368 /*
369  * Returns the height of a stroke font
370  */
371 GLfloat FGAPIENTRY glutStrokeHeight( void* fontID )
372 {
373     SFG_StrokeFont* font;
374     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeHeight" );
375     font = fghStrokeByID( fontID );
376     freeglut_return_val_if_fail( font, 0.0 );
377     return font->Height;
378 }
379
380 /*** END OF FILE ***/