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