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