0139cddbecd0b8ac2288d19b17d2be30dd5bf893
[mesa.git] / src / glut / directfb / font.c
1 /* Copyright (c) Mark J. Kilgard, 1994, 1998.
2
3 This program is freely distributable without licensing fees
4 and is provided without guarantee or warrantee expressed or
5 implied. This program is -not- in the public domain. */
6
7 #include "internal.h"
8 #include "font.h"
9
10
11 #if defined(_WIN32) || defined (GLUT_IMPORT_LIB)
12
13 static inline void*
14 __glutFont( void *font )
15 {
16 switch((long)font) {
17 case (long)GLUT_STROKE_ROMAN:
18 return &glutStrokeRoman;
19 case (long)GLUT_STROKE_MONO_ROMAN:
20 return &glutStrokeMonoRoman;
21 case (long)GLUT_BITMAP_9_BY_15:
22 return &glutBitmap9By15;
23 case (long)GLUT_BITMAP_8_BY_13:
24 return &glutBitmap8By13;
25 case (long)GLUT_BITMAP_TIMES_ROMAN_10:
26 return &glutBitmapTimesRoman10;
27 case (long)GLUT_BITMAP_TIMES_ROMAN_24:
28 return &glutBitmapTimesRoman24;
29 case (long)GLUT_BITMAP_HELVETICA_10:
30 return &glutBitmapHelvetica10;
31 case (long)GLUT_BITMAP_HELVETICA_12:
32 return &glutBitmapHelvetica12;
33 case (long)GLUT_BITMAP_HELVETICA_18:
34 return &glutBitmapHelvetica18;
35 }
36
37 return NULL;
38 }
39
40 #else
41
42 static inline void*
43 __glutFont( void *font )
44 {
45 return font;
46 }
47
48 #endif
49
50
51 void GLUTAPIENTRY
52 glutBitmapCharacter( GLUTbitmapFont font, int c )
53 {
54 const BitmapCharRec *ch;
55 BitmapFontPtr fontinfo;
56 GLint swapbytes, lsbfirst, rowlength;
57 GLint skiprows, skippixels, alignment;
58
59 fontinfo = (BitmapFontPtr) __glutFont( font );
60
61 if (!fontinfo || c < fontinfo->first ||
62 c >= fontinfo->first + fontinfo->num_chars)
63 return;
64
65 ch = fontinfo->ch[c - fontinfo->first];
66 if (ch) {
67 /* Save current modes. */
68 glGetIntegerv(GL_UNPACK_SWAP_BYTES, &swapbytes);
69 glGetIntegerv(GL_UNPACK_LSB_FIRST, &lsbfirst);
70 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &rowlength);
71 glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skiprows);
72 glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skippixels);
73 glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
74 /* Little endian machines (DEC Alpha for example) could
75 benefit from setting GL_UNPACK_LSB_FIRST to GL_TRUE
76 instead of GL_FALSE, but this would require changing the
77 generated bitmaps too. */
78 glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
79 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
80 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
81 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
82 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
83 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
84 glBitmap(ch->width, ch->height, ch->xorig, ch->yorig,
85 ch->advance, 0, ch->bitmap);
86 /* Restore saved modes. */
87 glPixelStorei(GL_UNPACK_SWAP_BYTES, swapbytes);
88 glPixelStorei(GL_UNPACK_LSB_FIRST, lsbfirst);
89 glPixelStorei(GL_UNPACK_ROW_LENGTH, rowlength);
90 glPixelStorei(GL_UNPACK_SKIP_ROWS, skiprows);
91 glPixelStorei(GL_UNPACK_SKIP_PIXELS, skippixels);
92 glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
93 }
94 }
95
96
97 int GLUTAPIENTRY
98 glutBitmapWidth( GLUTbitmapFont font, int c )
99 {
100 BitmapFontPtr fontinfo;
101 const BitmapCharRec *ch;
102
103 fontinfo = (BitmapFontPtr) __glutFont( font );
104
105 if (!fontinfo || c < fontinfo->first ||
106 c >= fontinfo->first + fontinfo->num_chars)
107 return 0;
108
109 ch = fontinfo->ch[c - fontinfo->first];
110 if (ch)
111 return ch->advance;
112
113 return 0;
114 }
115
116
117 int GLUTAPIENTRY
118 glutBitmapLength( GLUTbitmapFont font, const unsigned char *string )
119 {
120 int c, length;
121 BitmapFontPtr fontinfo;
122 const BitmapCharRec *ch;
123
124 fontinfo = (BitmapFontPtr) __glutFont( font );
125 if (!fontinfo)
126 return 0;
127
128 for (length = 0; *string != '\0'; string++) {
129 c = *string;
130 if (c >= fontinfo->first &&
131 c < fontinfo->first + fontinfo->num_chars) {
132 ch = fontinfo->ch[c - fontinfo->first];
133 if (ch)
134 length += ch->advance;
135 }
136 }
137
138 return length;
139 }
140
141
142 void GLUTAPIENTRY
143 glutStrokeCharacter( GLUTstrokeFont font, int c )
144 {
145 const StrokeCharRec *ch;
146 const StrokeRec *stroke;
147 const CoordRec *coord;
148 StrokeFontPtr fontinfo;
149 int i, j;
150
151 fontinfo = (StrokeFontPtr) __glutFont( font );
152
153 if (!fontinfo || c < 0 || c >= fontinfo->num_chars)
154 return;
155
156 ch = &(fontinfo->ch[c]);
157 if (ch) {
158 for (i = ch->num_strokes, stroke = ch->stroke;
159 i > 0; i--, stroke++) {
160 glBegin(GL_LINE_STRIP);
161 for (j = stroke->num_coords, coord = stroke->coord;
162 j > 0; j--, coord++) {
163 glVertex2f(coord->x, coord->y);
164 }
165 glEnd();
166 }
167 glTranslatef(ch->right, 0.0, 0.0);
168 }
169 }
170
171
172 int GLUTAPIENTRY
173 glutStrokeWidth( GLUTstrokeFont font, int c )
174 {
175 StrokeFontPtr fontinfo;
176 const StrokeCharRec *ch;
177
178 fontinfo = (StrokeFontPtr) __glutFont( font );
179
180 if (!fontinfo || c < 0 || c >= fontinfo->num_chars)
181 return 0;
182
183 ch = &(fontinfo->ch[c]);
184 if (ch)
185 return ch->right;
186
187 return 0;
188 }
189
190
191 int GLUTAPIENTRY
192 glutStrokeLength( GLUTstrokeFont font, const unsigned char *string )
193 {
194 int c, length;
195 StrokeFontPtr fontinfo;
196 const StrokeCharRec *ch;
197
198 fontinfo = (StrokeFontPtr) __glutFont( font );
199 if (!fontinfo)
200 return 0;
201
202 for (length = 0; *string != '\0'; string++) {
203 c = *string;
204 if (c >= 0 && c < fontinfo->num_chars) {
205 ch = &(fontinfo->ch[c]);
206 if (ch)
207 length += ch->right;
208 }
209 }
210
211 return length;
212 }
213