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