1 //
2 // "$Id: glut_font.cxx 7903 2010-11-28 21:06:39Z matt $"
3 //
4 // GLUT font routines for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 // USA.
22 //
23 // Please report all bugs and problems on the following page:
24 //
25 //     http://www.fltk.org/str.php
26 //
27 // The stroked text code was copied from FreeGLUT 2.4.0 which carries
28 // the following notice:
29 //
30 //     Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
31 //
32 
33 // (sort of) emulation of Glut's bitmap drawing functions, using FL's
34 // font stuff.  Not all the fonts match!
35 
36 #include <config.h>
37 #if HAVE_GL
38 #  include <FL/glut.H>
39 
40 Fl_Glut_Bitmap_Font glutBitmap9By15 = {FL_SCREEN, 15};
41 Fl_Glut_Bitmap_Font glutBitmap8By13 = {FL_SCREEN, 13};
42 Fl_Glut_Bitmap_Font glutBitmapTimesRoman10 = {FL_TIMES, 10};
43 Fl_Glut_Bitmap_Font glutBitmapTimesRoman24 = {FL_TIMES, 24};
44 Fl_Glut_Bitmap_Font glutBitmapHelvetica10 = {FL_HELVETICA, 10};
45 Fl_Glut_Bitmap_Font glutBitmapHelvetica12 = {FL_HELVETICA, 12};
46 Fl_Glut_Bitmap_Font glutBitmapHelvetica18 = {FL_HELVETICA, 18};
47 
glutBitmapCharacter(void * font,int character)48 void glutBitmapCharacter(void* font, int character) {
49   gl_font(((Fl_Glut_Bitmap_Font *)font)->font,((Fl_Glut_Bitmap_Font *)font)->size);
50   char a[1]; a[0] = character;
51   gl_draw(a,1);
52 }
53 
glutBitmapHeight(void * font)54 int glutBitmapHeight(void* font) {
55   gl_font(((Fl_Glut_Bitmap_Font *)font)->font,((Fl_Glut_Bitmap_Font *)font)->size);
56   return gl_height();
57 }
58 
glutBitmapLength(void * font,const unsigned char * string)59 int glutBitmapLength(void *font, const unsigned char *string) {
60   gl_font(((Fl_Glut_Bitmap_Font *)font)->font,((Fl_Glut_Bitmap_Font *)font)->size);
61   const char *s = (const char*)string;
62   return int(gl_width(s)+.5);
63 }
64 
glutBitmapString(void * font,const unsigned char * string)65 void glutBitmapString(void *font, const unsigned char *string) {
66   gl_font(((Fl_Glut_Bitmap_Font *)font)->font,((Fl_Glut_Bitmap_Font *)font)->size);
67   const char *s = (const char*)string;
68   gl_draw(s);
69 }
70 
glutBitmapWidth(void * font,int character)71 int glutBitmapWidth(void* font, int character) {
72   gl_font(((Fl_Glut_Bitmap_Font *)font)->font,((Fl_Glut_Bitmap_Font *)font)->size);
73   return int(gl_width(character)+.5);
74 }
75 
76 
77 /*
78  * Draw a stroke character
79  */
glutStrokeCharacter(void * fontID,int character)80 void glutStrokeCharacter(void* fontID, int character) {
81   const Fl_Glut_StrokeChar *schar;
82   const Fl_Glut_StrokeStrip *strip;
83   int i, j;
84   Fl_Glut_StrokeFont* font = (Fl_Glut_StrokeFont *)fontID;
85 
86   if (character < 0 || character >= font->Quantity) return;
87 
88   schar = font->Characters[character];
89   if (!schar) return;
90 
91   strip = schar->Strips;
92 
93   for (i = 0; i < schar->Number; i++, strip++)
94   {
95     glBegin(GL_LINE_STRIP);
96     for (j = 0; j < strip->Number; j++)
97       glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
98     glEnd();
99   }
100 
101   glTranslatef(schar->Right, 0.0, 0.0);
102 }
103 
glutStrokeString(void * fontID,const unsigned char * string)104 void glutStrokeString(void* fontID, const unsigned char *string) {
105   unsigned char c;
106   int i, j;
107   float length = 0.0;
108   Fl_Glut_StrokeFont* font = (Fl_Glut_StrokeFont *)fontID;
109 
110   if (!string || ! *string) return;
111 
112  /*
113   * Step through the string, drawing each character.
114   * A newline will simply translate the next character's insertion
115   * point back to the start of the line and down one line.
116   */
117   while ((c = *string++) != 0) {
118     if (c < font->Quantity) {
119       if (c == '\n') {
120 	glTranslatef(-length, -(float)(font->Height), 0.0);
121 	length = 0.0;
122       } else  {
123         /* Not an EOL, draw the bitmap character */
124 	const Fl_Glut_StrokeChar *schar = font->Characters[c];
125 	if (schar) {
126 	  const Fl_Glut_StrokeStrip *strip = schar->Strips;
127 
128 	  for (i = 0; i < schar->Number; i++, strip++) {
129 	    glBegin(GL_LINE_STRIP);
130 	    for (j = 0; j < strip->Number; j++)
131 	      glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
132 	    glEnd();
133 	  }
134 
135 	  length += schar->Right;
136 	  glTranslatef(schar->Right, 0.0, 0.0);
137 	}
138       }
139     }
140   }
141 }
142 
143 /*
144  * Return the width in pixels of a stroke character
145  */
glutStrokeWidth(void * fontID,int character)146 int glutStrokeWidth( void* fontID, int character )
147 {
148   const Fl_Glut_StrokeChar *schar;
149   Fl_Glut_StrokeFont* font = (Fl_Glut_StrokeFont *)fontID;
150   if (character < 0 || character >= font->Quantity) return 0;
151   schar = font->Characters[ character ];
152 
153   return schar ? (int)(schar->Right + 0.5) : 0;
154 }
155 
156 /*
157  * Return the width of a string drawn using a stroke font
158  */
glutStrokeLength(void * fontID,const unsigned char * string)159 int glutStrokeLength(void* fontID, const unsigned char* string) {
160   unsigned char c;
161   float length = 0.0;
162   float this_line_length = 0.0;
163   Fl_Glut_StrokeFont* font = (Fl_Glut_StrokeFont *)fontID;
164 
165   if (!string || ! *string) return 0;
166 
167   while ((c = *string++) != 0) {
168     if (c < font->Quantity) {
169       if (c == '\n') {
170         /* EOL; reset the length of this line */
171 	if (length < this_line_length) length = this_line_length;
172 	this_line_length = 0.0;
173       } else {
174         /* Not an EOL, increment the length of this line */
175 	const Fl_Glut_StrokeChar *schar = font->Characters[c];
176 	if (schar) this_line_length += schar->Right;
177       }
178     }
179   }
180 
181   if (length < this_line_length) length = this_line_length;
182 
183   return (int)(length + 0.5);
184 }
185 
186 /*
187  * Returns the height of a stroke font
188  */
glutStrokeHeight(void * fontID)189 GLfloat glutStrokeHeight(void* fontID) {
190   Fl_Glut_StrokeFont* font = (Fl_Glut_StrokeFont *)fontID;
191   return font->Height;
192 }
193 
194 #endif // HAVE_GL
195 
196 //
197 // End of "$Id: glut_font.cxx 7903 2010-11-28 21:06:39Z matt $".
198 //
199