52a80c1d57fc764707de63bb6d96aadd8981a161
[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     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     fgError( "font 0x%08x not found", font );
81     return 0; /*** NOT REACHED ***/
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     fgError( "stroke font 0x%08x not found", font );
96     return 0; /*** NOT REACHED ***/
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
112     /*
113      * Find the character we want to draw (???)
114      */
115     face = font->Characters[ character - 1 ];
116
117     glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
118     glPixelStorei( GL_UNPACK_SWAP_BYTES,  GL_FALSE );
119     glPixelStorei( GL_UNPACK_LSB_FIRST,   GL_FALSE );
120     glPixelStorei( GL_UNPACK_ROW_LENGTH,  0        );
121     glPixelStorei( GL_UNPACK_SKIP_ROWS,   0        );
122     glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0        );
123     glPixelStorei( GL_UNPACK_ALIGNMENT,   1        );
124     glBitmap(
125         face[ 0 ], font->Height,      /* The bitmap's width and height  */
126         font->xorig, font->yorig,     /* The origin in the font glyph   */
127         ( float )( face[ 0 ] ), 0.0,  /* The raster advance -- inc. x,y */
128         ( face + 1 )                  /* The packed bitmap data...      */
129     );
130     glPopClientAttrib( );
131 }
132
133 void FGAPIENTRY glutBitmapString( void* fontID, const unsigned char *string )
134 {
135     int c;
136     int numchar = strlen( string );
137     SFG_Font* font = fghFontByID( fontID );
138     float raster_position[ 4 ];
139
140     glGetFloatv ( GL_CURRENT_RASTER_POSITION, raster_position );
141     glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
142     glPixelStorei( GL_UNPACK_SWAP_BYTES,  GL_FALSE );
143     glPixelStorei( GL_UNPACK_LSB_FIRST,   GL_FALSE );
144     glPixelStorei( GL_UNPACK_ROW_LENGTH,  0        );
145     glPixelStorei( GL_UNPACK_SKIP_ROWS,   0        );
146     glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0        );
147     glPixelStorei( GL_UNPACK_ALIGNMENT,   1        );
148
149     /*
150      * Step through the string, drawing each character.
151      * A newline will simply translate the next character's insertion
152      * point back to the start of the line and down one line.
153      */
154     for( c = 0; c < numchar; c++ )
155         if( string[c] == '\n' )
156         {
157             raster_position[ 1 ] -= ( float )font->Height;
158             glRasterPos4fv( raster_position );
159         }
160         else  /* Not an EOL, draw the bitmap character */
161         {
162             const GLubyte* face = font->Characters[ string[ c ] - 1 ];
163
164             glBitmap(
165                 face[ 0 ], font->Height,     /* Bitmap's width and height    */
166                 font->xorig, font->yorig,    /* The origin in the font glyph */
167                 ( float )( face[ 0 ] ), 0.0, /* The raster advance; inc. x,y */
168                 ( face + 1 )                 /* The packed bitmap data...    */
169             );
170         }
171     glPopClientAttrib( );
172 }
173
174 /*
175  * Returns the width in pixels of a font's character
176  */
177 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
178 {
179     SFG_Font* font = fghFontByID( fontID );
180
181     freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
182     return *( font->Characters[ character - 1 ] );
183 }
184
185 /*
186  * Return the width of a string drawn using a bitmap font
187  */
188 int FGAPIENTRY glutBitmapLength( void* fontID, const unsigned char* string )
189 {
190     int c, length = 0, this_line_length = 0;
191     SFG_Font* font = fghFontByID( fontID );
192     int numchar = strlen( string );
193
194     for( c = 0; c < numchar; c++ )
195     {
196         if( string[ c ] != '\n' )/* Not an EOL, increment length of line */
197             this_line_length += *( font->Characters[ string[ c ] - 1 ]);
198         else  /* EOL; reset the length of this line */
199         {
200             if( length < this_line_length )
201                 length = this_line_length;
202             this_line_length = 0;
203         }
204     }
205     if ( length < this_line_length )
206         length = this_line_length;
207
208     return length;
209 }
210
211 /*
212  * Returns the height of a bitmap font
213  */
214 int FGAPIENTRY glutBitmapHeight( void* fontID )
215 {
216     SFG_Font* font = fghFontByID( fontID );
217     return font->Height;
218 }
219
220 /*
221  * Draw a stroke character
222  */
223 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
224 {
225     const SFG_StrokeChar *schar;
226     const SFG_StrokeStrip *strip;
227     int i, j;
228     SFG_StrokeFont* font = fghStrokeByID( fontID );
229
230     freeglut_return_if_fail( character >= 0 );
231     freeglut_return_if_fail( character < font->Quantity );
232
233     schar = font->Characters[ character ];
234     freeglut_return_if_fail( schar );
235     strip = schar->Strips;
236
237     for( i = 0; i < schar->Number; i++, strip++ )
238     {
239         glBegin( GL_LINE_STRIP );
240         for( j = 0; j < strip->Number; j++ )
241             glVertex2f( strip->Vertices[ j ].X, strip->Vertices[ j ].Y );
242         glEnd( );
243     }
244     glTranslatef( schar->Right, 0.0, 0.0 );
245 }
246
247 void FGAPIENTRY glutStrokeString( void* fontID, const unsigned char *string )
248 {
249     int c, i, j;
250     int numchar = strlen( string );
251     float length = 0.0;
252     SFG_StrokeFont* font = fghStrokeByID( fontID );
253
254     /*
255      * Step through the string, drawing each character.
256      * A newline will simply translate the next character's insertion
257      * point back to the start of the line and down one line.
258      */
259     for( c = 0; c < numchar; c++ )
260         if( string[ c ] < font->Quantity )
261         {
262             if( string[ c ] == '\n' )
263             {
264                 glTranslatef ( -length, -( float )( font->Height ), 0.0 );
265                 length = 0.0;
266             }
267             else  /* Not an EOL, draw the bitmap character */
268             {
269                 const SFG_StrokeChar *schar = font->Characters[ string[ c ] ];
270                 if( schar )
271                 {
272                     const SFG_StrokeStrip *strip = schar->Strips;
273
274                     for( i = 0; i < schar->Number; i++, strip++ )
275                     {
276                         glBegin( GL_LINE_STRIP );
277                         for( j = 0; j < strip->Number; j++ )
278                             glVertex2f( strip->Vertices[ j ].X,
279                                         strip->Vertices[ j ].Y);
280
281                         glEnd( );
282                     }
283                     
284                     length += schar->Right;
285                     glTranslatef( schar->Right, 0.0, 0.0 );
286                 }
287             }
288         }
289 }
290
291 /*
292  * Return the width in pixels of a stroke character
293  */
294 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
295 {
296     const SFG_StrokeChar *schar;
297     SFG_StrokeFont* font = fghStrokeByID( fontID );
298
299     freeglut_return_val_if_fail( ( character >= 0 ) &&
300                                  ( character < font->Quantity ),
301                                  0
302     );
303     schar = font->Characters[ character ];
304     freeglut_return_val_if_fail( schar, 0 );
305     
306     return ( int )( schar->Right + 0.5 );
307 }
308
309 /*
310  * Return the width of a string drawn using a stroke font
311  */
312 int FGAPIENTRY glutStrokeLength( void* fontID, const unsigned char* string )
313 {
314     int c;
315     float length = 0.0;
316     float this_line_length = 0.0;
317     SFG_StrokeFont* font = fghStrokeByID( fontID );
318     int numchar = strlen( string );
319
320     for( c = 0; c < numchar; c++ )
321         if( string[ c ] < font->Quantity )
322         {
323             if( string[ c ] == '\n' ) /* EOL; reset the length of this line */
324             {
325                 if( length < this_line_length )
326                     length = this_line_length;
327                 this_line_length = 0.0;
328             }
329             else  /* Not an EOL, increment the length of this line */
330             {
331                 const SFG_StrokeChar *schar = font->Characters[ string[ c ] ];
332                 if( schar )
333                     this_line_length += schar->Right;
334             }
335         }
336     if( length < this_line_length )
337         length = this_line_length;
338     return( int )( length + 0.5 );
339 }
340
341 /*
342  * Returns the height of a stroke font
343  */
344 GLfloat FGAPIENTRY glutStrokeHeight( void* fontID )
345 {
346     SFG_StrokeFont* font = fghStrokeByID( fontID );
347     return font->Height;
348 }
349
350 /*** END OF FILE ***/