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