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