Fix for the font binary compatibility problem.
[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 #define  G_LOG_DOMAIN  "freeglut-font"
33
34 #include "../include/GL/freeglut.h"
35 #include "freeglut_internal.h"
36
37 /*
38  * TODO BEFORE THE STABLE RELEASE:
39  *
40  *  Test things out ...
41  */
42
43 /* -- IMPORT DECLARATIONS -------------------------------------------------- */
44
45 /*
46  * These are the font faces defined in freeglut_font_data.c file:
47  */
48 extern SFG_Font fgFontFixed8x13;
49 extern SFG_Font fgFontFixed9x15;
50 extern SFG_Font fgFontHelvetica10;
51 extern SFG_Font fgFontHelvetica12;
52 extern SFG_Font fgFontHelvetica18;
53 extern SFG_Font fgFontTimesRoman10;
54 extern SFG_Font fgFontTimesRoman24;
55 extern SFG_StrokeFont fgStrokeRoman;
56 extern SFG_StrokeFont fgStrokeMonoRoman;
57
58 /*
59  * This is for GLUT binary compatibility, as suggested by Steve Baker
60  */
61 #if TARGET_HOST_UNIX_X11
62   struct _GLUTstrokeFont {
63     const char *name;
64     int num_chars;
65     void *ch;
66     float top;
67     float bottom;
68   };
69
70   struct _GLUTbitmapFont {
71     const char *name;
72     const int num_chars;
73     const int first;
74     const void *ch;
75   };
76
77   struct _GLUTstrokeFont glutStrokeRoman;
78   struct _GLUTstrokeFont glutStrokeMonoRoman;
79   struct _GLUTbitmapFont glutBitmap9By15;
80   struct _GLUTbitmapFont glutBitmap8By13;
81   struct _GLUTbitmapFont glutBitmapTimesRoman10;
82   struct _GLUTbitmapFont glutBitmapTimesRoman24;
83   struct _GLUTbitmapFont glutBitmapHelvetica10;
84   struct _GLUTbitmapFont glutBitmapHelvetica12;
85   struct _GLUTbitmapFont glutBitmapHelvetica18;
86 #endif
87
88
89 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
90
91 /*
92  * Matches a font ID with a SFG_Font structure pointer.
93  * This was changed to match the GLUT header style.
94  */
95 static SFG_Font* fghFontByID( void* font )
96 {
97   /*
98    * Try matching the font ID and the font data structure
99    */
100   if( font == GLUT_BITMAP_8_BY_13        ) return( &fgFontFixed8x13    );
101   if( font == GLUT_BITMAP_9_BY_15        ) return( &fgFontFixed9x15    );
102   if( font == GLUT_BITMAP_HELVETICA_10   ) return( &fgFontHelvetica10  );
103   if( font == GLUT_BITMAP_HELVETICA_12   ) return( &fgFontHelvetica12  );
104   if( font == GLUT_BITMAP_HELVETICA_18   ) return( &fgFontHelvetica18  );
105   if( font == GLUT_BITMAP_TIMES_ROMAN_10 ) return( &fgFontTimesRoman10 );
106   if( font == GLUT_BITMAP_TIMES_ROMAN_24 ) return( &fgFontTimesRoman24 );
107
108   /*
109    * This probably is the library user's fault
110    */
111   fgError( "font 0x%08x not found", font );
112
113   return 0;
114 }
115
116 /*
117  * Matches a font ID with a SFG_StrokeFont structure pointer.
118  * This was changed to match the GLUT header style.
119  */
120 static SFG_StrokeFont* fghStrokeByID( void* font )
121 {
122   /*
123    * Try matching the font ID and the font data structure
124    */
125   if( font == GLUT_STROKE_ROMAN      ) return( &fgStrokeRoman     );
126   if( font == GLUT_STROKE_MONO_ROMAN ) return( &fgStrokeMonoRoman );
127
128   /*
129    * This probably is the library user's fault
130    */
131   fgError( "stroke font 0x%08x not found", font );
132
133   return 0;
134 }
135
136
137 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
138
139 /*
140  * Draw a bitmap character
141  */
142 void FGAPIENTRY glutBitmapCharacter( void* fontID, int character )
143 {
144   const GLubyte* face;
145
146   /*
147    * First of all we'll need a font to use
148    */
149   SFG_Font* font = fghFontByID( fontID );
150
151   /*
152    * Make sure the character we want to output is valid
153    */
154   freeglut_return_if_fail( character >= 0 && character < 256 );
155
156   /*
157    * Then find the character we want to draw
158    */
159   face = font->Characters[ character - 1 ];
160
161   /*
162    * Save the old pixel store settings
163    */
164   glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
165
166   /*
167    * Set up the pixel unpacking ways
168    */
169   glPixelStorei( GL_UNPACK_SWAP_BYTES,  GL_FALSE );
170   glPixelStorei( GL_UNPACK_LSB_FIRST,   GL_FALSE );
171   glPixelStorei( GL_UNPACK_ROW_LENGTH,  0        );
172   glPixelStorei( GL_UNPACK_SKIP_ROWS,   0        );
173   glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0        );
174   glPixelStorei( GL_UNPACK_ALIGNMENT,   1        );
175
176   /*
177    * We'll use a glBitmap call to draw the font.
178    */
179   glBitmap(
180       face[ 0 ], font->Height,      /* The bitmap's width and height */
181       font->xorig, font->yorig,     /* The origin -- what on earth?  */
182       (float)(face[ 0 ]), 0.0,      /* The raster advance -- inc. x  */
183       (face + 1)                    /* The packed bitmap data...     */
184   );
185
186   /*
187    * Restore the old pixel store settings
188    */
189   glPopClientAttrib();
190 }
191
192 void FGAPIENTRY glutBitmapString( void* fontID, const char *string )
193 {
194   int c;
195   int numchar = strlen ( string ) ;
196
197   /*
198    * First of all we'll need a font to use
199    */
200   SFG_Font* font = fghFontByID( fontID );
201
202   float raster_position[4] ;
203   glGetFloatv ( GL_CURRENT_RASTER_POSITION, raster_position ) ;
204
205   /*
206    * Save the old pixel store settings
207    */
208   glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
209
210   /*
211    * Set up the pixel unpacking ways
212    */
213   glPixelStorei( GL_UNPACK_SWAP_BYTES,  GL_FALSE );
214   glPixelStorei( GL_UNPACK_LSB_FIRST,   GL_FALSE );
215   glPixelStorei( GL_UNPACK_ROW_LENGTH,  0        );
216   glPixelStorei( GL_UNPACK_SKIP_ROWS,   0        );
217   glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0        );
218   glPixelStorei( GL_UNPACK_ALIGNMENT,   1        );
219
220   /*
221    * Step through the string, drawing each character.
222    * A carriage return will simply translate the next character's insertion point back to the
223    * start of the line and down one line.
224    */
225   for( c = 0; c < numchar; c++ )
226   {
227     if ( string[c] == '\n' )
228     {
229       raster_position[1] -= (float)font->Height ;
230       glRasterPos4fv ( raster_position ) ;
231     }
232     else  /* Not a carriage return, draw the bitmap character */
233     {
234       const GLubyte* face = font->Characters[ string[ c ] - 1 ] ;
235
236       /*
237        * We'll use a glBitmap call to draw the font.
238        */
239       glBitmap(
240           face[ 0 ], font->Height,      /* The bitmap's width and height */
241           font->xorig, font->yorig,     /* The origin -- what on earth?  */
242           (float)(face[ 0 ]), 0.0,      /* The raster advance -- inc. x  */
243           (face + 1)                    /* The packed bitmap data...     */
244       ) ;
245     }
246   }
247
248   /*
249    * Restore the old pixel store settings
250    */
251   glPopClientAttrib();
252 }
253
254 /*
255  * Returns the width in pixels of a font's character
256  */
257 int FGAPIENTRY glutBitmapWidth( void* fontID, int character )
258 {
259   /*
260    * First of all, grab the font we need
261    */
262   SFG_Font* font = fghFontByID( fontID );
263
264   /*
265    * Make sure the character we want to output is valid
266    */
267   freeglut_return_val_if_fail( character > 0 && character < 256, 0 );
268
269   /*
270        * Scan the font looking for the specified character
271    */
272   return( *(font->Characters[ character - 1 ]) );
273 }
274
275 /*
276  * Return the width of a string drawn using a bitmap font
277  */
278 int FGAPIENTRY glutBitmapLength( void* fontID, const char* string )
279 {
280   int c, length = 0, this_line_length = 0;
281
282   /*
283    * First of all, grab the font we need
284    */
285   SFG_Font* font = fghFontByID( fontID );
286
287   /*
288    * Step through the characters in the string, adding up the width of each one
289    */
290   int numchar = strlen ( string ) ;
291   for( c = 0; c < numchar; c++ )
292   {
293     if ( string[ c ] == '\n' )  /* Carriage return, reset the length of this line */
294     {
295       if ( length < this_line_length ) length = this_line_length ;
296       this_line_length = 0 ;
297     }
298     else  /* Not a carriage return, increment the length of this line */
299       this_line_length += *(font->Characters[ string[ c ] - 1 ]) ;
300   }
301
302   if ( length < this_line_length ) length = this_line_length ;
303
304   /*
305    * Return the result now
306    */
307   return( length );
308 }
309
310 /*
311  * Returns the height of a bitmap font
312  */
313 int FGAPIENTRY glutBitmapHeight( void* fontID )
314 {
315   /*
316    * See which font are we queried about
317    */
318   SFG_Font* font = fghFontByID( fontID );
319
320   /*
321    * Return the character set's height
322    */
323   return( font->Height );
324 }
325
326 /*
327  * Draw a stroke character
328  */
329 void FGAPIENTRY glutStrokeCharacter( void* fontID, int character )
330 {
331   const SFG_StrokeChar *schar;
332   const SFG_StrokeStrip *strip;
333   int i, j;
334
335   /*
336    * First of all we'll need a font to use
337    */
338   SFG_StrokeFont* font = fghStrokeByID( fontID );
339
340   /*
341    * Make sure the character we want to output is valid
342    */
343   freeglut_return_if_fail( character >= 0 && character < font->Quantity );
344
345   schar = font->Characters[character];
346
347   freeglut_return_if_fail( schar );
348
349   strip = schar->Strips;
350
351   for (i = 0; i < schar->Number; i++, strip++)
352   {
353     glBegin(GL_LINE_STRIP);
354     for(j = 0; j < strip->Number; j++)
355     {
356       glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
357     }
358     glEnd();
359   }
360   glTranslatef(schar->Right, 0.0, 0.0);
361 }
362
363 void FGAPIENTRY glutStrokeString( void* fontID, const char *string )
364 {
365   int c, i, j;
366   int numchar = strlen ( string ) ;
367   float length = 0.0 ;
368
369   /*
370    * First of all we'll need a font to use
371    */
372   SFG_StrokeFont* font = fghStrokeByID( fontID );
373
374   /*
375    * Step through the string, drawing each character.
376    * A carriage return will simply translate the next character's insertion point back to the
377    * start of the line and down one line.
378    */
379   for( c = 0; c < numchar; c++ )
380   {
381     if ( ( string[ c ] >= 0 ) && ( string[ c ] < font->Quantity ) )
382     {
383       if ( string[c] == '\n' )
384       {
385         glTranslatef ( -length, -(float)(font->Height), 0.0 ) ;
386         length = 0.0 ;
387       }
388       else  /* Not a carriage return, draw the bitmap character */
389       {
390         const SFG_StrokeChar *schar = font->Characters[string[c]];
391         if ( schar != NULL )
392         {
393           const SFG_StrokeStrip *strip = schar->Strips;
394
395           for (i = 0; i < schar->Number; i++, strip++)
396           {
397             glBegin(GL_LINE_STRIP);
398             for(j = 0; j < strip->Number; j++)
399               glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
400
401             glEnd();
402           }
403
404           length += schar->Right ;
405           glTranslatef(schar->Right, 0.0, 0.0);
406         }
407       }
408     }
409   }
410 }
411
412 /*
413  * Return the width in pixels of a stroke character
414  */
415 int FGAPIENTRY glutStrokeWidth( void* fontID, int character )
416 {
417   const SFG_StrokeChar *schar;
418   /*
419    * First of all we'll need a font to use
420    */
421   SFG_StrokeFont* font = fghStrokeByID( fontID );
422
423   /*
424    * Make sure the character we want to output is valid
425    */
426   freeglut_return_val_if_fail( character >= 0 && character < font->Quantity, 0 );
427
428   schar = font->Characters[character];
429
430   freeglut_return_val_if_fail( schar, 0 );
431
432   return ((int)(schar->Right + 0.5));
433 }
434
435 /*
436  * Return the width of a string drawn using a stroke font
437  */
438 int FGAPIENTRY glutStrokeLength( void* fontID, const char* string )
439 {
440   int c;
441   float length = 0.0;
442   float this_line_length = 0.0 ;
443
444   /*
445    * First of all we'll need a font to use
446    */
447   SFG_StrokeFont* font = fghStrokeByID( fontID );
448
449   /*
450    * Step through the characters in the string, adding up the width of each one
451    */
452   int numchar = strlen ( string ) ;
453   for( c = 0; c < numchar; c++ )
454   {
455     if ( ( string[ c ] >= 0 ) && ( string[ c ] < font->Quantity ) )
456     {
457       if ( string[ c ] == '\n' )  /* Carriage return, reset the length of this line */
458       {
459         if ( length < this_line_length ) length = this_line_length ;
460         this_line_length = 0.0 ;
461       }
462       else  /* Not a carriage return, increment the length of this line */
463       {
464         const SFG_StrokeChar *schar = font->Characters[string[c]];
465         if ( schar != NULL )
466           this_line_length += schar->Right ;
467       }
468     }
469   }
470
471   if ( length < this_line_length ) length = this_line_length ;
472
473   /*
474    * Return the result now
475    */
476   return( (int)(length+0.5) );
477 }
478
479 /*
480  * Returns the height of a stroke font
481  */
482 GLfloat FGAPIENTRY glutStrokeHeight( void* fontID )
483 {
484     /*
485      * See which font are we queried about
486      */
487     SFG_StrokeFont* font = fghStrokeByID( fontID );
488
489     /*
490      * Return the character set's height
491      */
492     return( font->Height );
493 }
494
495 /*** END OF FILE ***/