Normalized the style of freeglut_font.c No substantial alterations.
[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 "../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     /*
66      * XXX Use a macro, a table of some kind, or else split these
67      * XXX statements properly.  Jamming "return" on the end of an
68      * XXX "if" is just bad style, IMHO.
69      */
70     if( font == GLUT_BITMAP_8_BY_13        ) return( &fgFontFixed8x13    );
71     if( font == GLUT_BITMAP_9_BY_15        ) return( &fgFontFixed9x15    );
72     if( font == GLUT_BITMAP_HELVETICA_10   ) return( &fgFontHelvetica10  );
73     if( font == GLUT_BITMAP_HELVETICA_12   ) return( &fgFontHelvetica12  );
74     if( font == GLUT_BITMAP_HELVETICA_18   ) return( &fgFontHelvetica18  );
75     if( font == GLUT_BITMAP_TIMES_ROMAN_10 ) return( &fgFontTimesRoman10 );
76     if( font == GLUT_BITMAP_TIMES_ROMAN_24 ) return( &fgFontTimesRoman24 );
77     fgError( "font 0x%08x not found", font );
78     return 0; /*** NOT REACHED ***/
79 }
80
81 /*
82  * Matches a font ID with a SFG_StrokeFont structure pointer.
83  * This was changed to match the GLUT header style.
84  */
85 static SFG_StrokeFont* fghStrokeByID( void* font )
86 {
87     /*
88      * XXX Same comment as above about jamming "return" in after an
89      * XXX "if".
90      */
91     if( font == GLUT_STROKE_ROMAN      ) return( &fgStrokeRoman     );
92     if( font == GLUT_STROKE_MONO_ROMAN ) return( &fgStrokeMonoRoman );
93     fgError( "stroke font 0x%08x not found", font );
94     return 0; /*** NOT REACHED ***/
95 }
96
97
98 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
99
100 /*
101  * Draw a bitmap character
102  */
103 void FGAPIENTRY glutBitmapCharacter( void* fontID, int character )
104 {
105     const GLubyte* face;
106     SFG_Font* font = fghFontByID( fontID );
107
108     freeglut_return_if_fail( ( character >= 1 )&&( character < 256 ) );
109
110     /*
111      * Find the character we want to draw (???)
112      */
113     face = font->Characters[ character - 1 ];
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     int c;
134     int numchar = strlen ( string ) ;
135     SFG_Font* font = fghFontByID( fontID );
136     float raster_position[4];
137
138     glGetFloatv ( GL_CURRENT_RASTER_POSITION, raster_position ) ;
139     glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
140     glPixelStorei( GL_UNPACK_SWAP_BYTES,  GL_FALSE );
141     glPixelStorei( GL_UNPACK_LSB_FIRST,   GL_FALSE );
142     glPixelStorei( GL_UNPACK_ROW_LENGTH,  0        );
143     glPixelStorei( GL_UNPACK_SKIP_ROWS,   0        );
144     glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0        );
145     glPixelStorei( GL_UNPACK_ALIGNMENT,   1        );
146
147     /*
148      * Step through the string, drawing each character.
149      * A newline will simply translate the next character's insertion
150      * point back to the start of the line and down one line.
151      */
152     for( c = 0; c < numchar; c++ )
153         if ( string[c] == '\n' )
154         {
155             raster_position[1] -= (float)font->Height ;
156             glRasterPos4fv ( raster_position ) ;
157         }
158         else  /* Not an EOL, draw the bitmap character */
159         {
160             const GLubyte* face = font->Characters[ string[ c ] - 1 ];
161
162             glBitmap(
163                 face[ 0 ], font->Height,     /* Bitmap's width and height    */
164                 font->xorig, font->yorig,    /* The origin in the font glyph */
165                 ( float )( face[ 0 ] ), 0.0, /* The raster advance; inc. x,y */
166                 ( face + 1 )                 /* The packed bitmap data...    */
167             );
168         }
169     glPopClientAttrib( );
170 }
171
172 /*
173  * Returns the width in pixels of a font's character
174  */
175 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
176 {
177     SFG_Font* font = fghFontByID( fontID );
178
179     freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
180     return *( font->Characters[ character - 1 ] );
181 }
182
183 /*
184  * Return the width of a string drawn using a bitmap font
185  */
186 int FGAPIENTRY glutBitmapLength( void* fontID, const unsigned char* string )
187 {
188     int c, length = 0, this_line_length = 0;
189     SFG_Font* font = fghFontByID( fontID );
190     int numchar = strlen ( string ) ;
191
192     for( c = 0; c < numchar; c++ )
193     {
194         if ( string[ c ] != '\n' )/* Not an EOL, increment length of line */
195             this_line_length += *( font->Characters[ string[ c ] - 1 ]);
196         else  /* EOL; reset the length of this line */
197         {
198             if( length < this_line_length )
199                 length = this_line_length;
200             this_line_length = 0;
201         }
202     }
203     if ( length < this_line_length )
204         length = this_line_length;
205
206     return length;
207 }
208
209 /*
210  * Returns the height of a bitmap font
211  */
212 int FGAPIENTRY glutBitmapHeight( void* fontID )
213 {
214     SFG_Font* font = fghFontByID( fontID );
215     return font->Height;
216 }
217
218 /*
219  * Draw a stroke character
220  */
221 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
222 {
223     const SFG_StrokeChar *schar;
224     const SFG_StrokeStrip *strip;
225     int i, j;
226     SFG_StrokeFont* font = fghStrokeByID( fontID );
227
228     freeglut_return_if_fail( character >= 0 && character < font->Quantity );
229
230     schar = font->Characters[character];
231     freeglut_return_if_fail( schar );
232     strip = schar->Strips;
233
234     for( i = 0; i < schar->Number; i++, strip++ )
235     {
236         glBegin( GL_LINE_STRIP );
237         for( j = 0; j < strip->Number; j++ )
238             glVertex2f( strip->Vertices[ j ].X, strip->Vertices[ j ].Y );
239         glEnd( );
240     }
241     glTranslatef( schar->Right, 0.0, 0.0 );
242 }
243
244 void FGAPIENTRY glutStrokeString( void* fontID, const unsigned char *string )
245 {
246     int c, i, j;
247     int numchar = strlen ( string );
248     float length = 0.0;
249     SFG_StrokeFont* font = fghStrokeByID( fontID );
250
251     /*
252      * Step through the string, drawing each character.
253      * A newline will simply translate the next character's insertion
254      * point back to the start of the line and down one line.
255      */
256     for( c = 0; c < numchar; c++ )
257         if ( string[ c ] < font->Quantity )
258         {
259             if( string[ c ] == '\n' )
260             {
261                 glTranslatef ( -length, -(float)(font->Height), 0.0 );
262                 length = 0.0;
263             }
264             else  /* Not an EOL, draw the bitmap character */
265             {
266                 const SFG_StrokeChar *schar = font->Characters[ string[ c ] ];
267                 if( schar )
268                 {
269                     const SFG_StrokeStrip *strip = schar->Strips;
270
271                     for(i = 0; i < schar->Number; i++, strip++)
272                     {
273                         glBegin( GL_LINE_STRIP );
274                         for( j = 0; j < strip->Number; j++ )
275                             glVertex2f( strip->Vertices[ j ].X,
276                                         strip->Vertices[ j ].Y);
277
278                         glEnd( );
279                     }
280                     
281                     length += schar->Right;
282                     glTranslatef( schar->Right, 0.0, 0.0 );
283                 }
284             }
285         }
286 }
287
288 /*
289  * Return the width in pixels of a stroke character
290  */
291 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
292 {
293     const SFG_StrokeChar *schar;
294     SFG_StrokeFont* font = fghStrokeByID( fontID );
295
296     freeglut_return_val_if_fail( ( character >= 0 ) &&
297                                  ( character < font->Quantity ),
298                                  0  );
299     schar = font->Characters[ character ];
300     freeglut_return_val_if_fail( schar, 0 );
301     
302     return ( int )( schar->Right + 0.5 );
303 }
304
305 /*
306  * Return the width of a string drawn using a stroke font
307  */
308 int FGAPIENTRY glutStrokeLength( void* fontID, const unsigned char* string )
309 {
310     int c;
311     float length = 0.0;
312     float this_line_length = 0.0;
313     SFG_StrokeFont* font = fghStrokeByID( fontID );
314     int numchar = strlen( string );
315
316     for( c = 0; c < numchar; c++ )
317         if ( string[ c ] < font->Quantity )
318         {
319             if( string[ c ] == '\n' ) /* EOL; reset the length of this line */
320             {
321                 if( length < this_line_length )
322                     length = this_line_length;
323                 this_line_length = 0.0;
324             }
325             else  /* Not an EOL, increment the length of this line */
326             {
327                 const SFG_StrokeChar *schar = font->Characters[ string[ c ] ];
328                 if ( schar )
329                     this_line_length += schar->Right;
330             }
331         }
332     if( length < this_line_length )
333         length = this_line_length;
334     return ( int )( length + 0.5 );
335 }
336
337 /*
338  * Returns the height of a stroke font
339  */
340 GLfloat FGAPIENTRY glutStrokeHeight( void* fontID )
341 {
342     SFG_StrokeFont* font = fghStrokeByID( fontID );
343     return( font->Height );
344 }
345
346 /*** END OF FILE ***/