Remove CVS keywords.
[mesa.git] / src / glx / x11 / xfont.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 3.1
4 *
5 * Copyright (C) 1999 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /* xfonts.c -- glXUseXFont() for Mesa written by
27 * Copyright (C) 1995 Thorsten.Ohl @ Physik.TH-Darmstadt.de
28 */
29
30 /*
31 This was take from Mesa and modified to work in the real GLX structure.
32 It provides a fully client side implementation of glXUseXFont and is
33 called by that routine when direct rendering is enabled.
34 */
35
36
37 #include "glxclient.h"
38
39 /* Some debugging info. */
40
41 #ifdef DEBUG
42 #undef _R
43 #undef _G
44 #undef _B
45 #include <ctype.h>
46
47 int debug_xfonts = 0;
48
49 static void
50 dump_char_struct (XCharStruct *ch, char *prefix)
51 {
52 printf ("%slbearing = %d, rbearing = %d, width = %d\n",
53 prefix, ch->lbearing, ch->rbearing, ch->width);
54 printf ("%sascent = %d, descent = %d, attributes = %u\n",
55 prefix, ch->ascent, ch->descent, (unsigned int) ch->attributes);
56 }
57
58 static void
59 dump_font_struct (XFontStruct *font)
60 {
61 printf ("ascent = %d, descent = %d\n", font->ascent, font->descent);
62 printf ("char_or_byte2 = (%u,%u)\n",
63 font->min_char_or_byte2, font->max_char_or_byte2);
64 printf ("byte1 = (%u,%u)\n", font->min_byte1, font->max_byte1);
65 printf ("all_chars_exist = %s\n", font->all_chars_exist ? "True" :
66 "False");
67 printf ("default_char = %c (\\%03o)\n",
68 (char) (isprint (font->default_char) ? font->default_char : ' '),
69 font->default_char);
70 dump_char_struct (&font->min_bounds, "min> ");
71 dump_char_struct (&font->max_bounds, "max> ");
72 #if 0
73 for (c = font->min_char_or_byte2; c <= font->max_char_or_byte2; c++)
74 {
75 char prefix[8];
76 sprintf (prefix, "%d> ", c);
77 dump_char_struct (&font->per_char[c], prefix);
78 }
79 #endif
80 }
81
82 static void
83 dump_bitmap (unsigned int width, unsigned int height, GLubyte *bitmap)
84 {
85 unsigned int x, y;
86
87 printf (" ");
88 for (x = 0; x < 8*width; x++)
89 printf ("%o", 7 - (x % 8));
90 putchar ('\n');
91 for (y = 0; y < height; y++)
92 {
93 printf ("%3o:", y);
94 for (x = 0; x < 8*width; x++)
95 putchar ((bitmap[width*(height - y - 1) + x/8] & (1 << (7 - (x %
96 8))))
97 ? '*' : '.');
98 printf (" ");
99 for (x = 0; x < width; x++)
100 printf ("0x%02x, ", bitmap[width*(height - y - 1) + x]);
101 putchar ('\n');
102 }
103 }
104 #endif /* DEBUG */
105
106
107 /* Implementation. */
108
109 /* Fill a BITMAP with a character C from thew current font
110 in the graphics context GC. WIDTH is the width in bytes
111 and HEIGHT is the height in bits.
112
113 Note that the generated bitmaps must be used with
114
115 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
116 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
117 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
118 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
119 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
120 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
121
122 Possible optimizations:
123
124 * use only one reusable pixmap with the maximum dimensions.
125 * draw the entire font into a single pixmap (careful with
126 proportional fonts!).
127 */
128
129
130 /*
131 * Generate OpenGL-compatible bitmap.
132 */
133 static void
134 fill_bitmap (Display *dpy, Window win, GC gc,
135 unsigned int width, unsigned int height,
136 int x0, int y0, unsigned int c, GLubyte *bitmap)
137 {
138 XImage *image;
139 unsigned int x, y;
140 Pixmap pixmap;
141 XChar2b char2b;
142
143 pixmap = XCreatePixmap (dpy, win, 8*width, height, 1);
144 XSetForeground(dpy, gc, 0);
145 XFillRectangle (dpy, pixmap, gc, 0, 0, 8*width, height);
146 XSetForeground(dpy, gc, 1);
147
148 char2b.byte1 = (c >> 8) & 0xff;
149 char2b.byte2 = (c & 0xff);
150
151 XDrawString16 (dpy, pixmap, gc, x0, y0, &char2b, 1);
152
153 image = XGetImage (dpy, pixmap, 0, 0, 8*width, height, 1, XYPixmap);
154 if (image) {
155 /* Fill the bitmap (X11 and OpenGL are upside down wrt each other). */
156 for (y = 0; y < height; y++)
157 for (x = 0; x < 8*width; x++)
158 if (XGetPixel (image, x, y))
159 bitmap[width*(height - y - 1) + x/8] |= (1 << (7 - (x % 8)));
160 XDestroyImage (image);
161 }
162
163 XFreePixmap (dpy, pixmap);
164 }
165
166 /*
167 * determine if a given glyph is valid and return the
168 * corresponding XCharStruct.
169 */
170 static XCharStruct *isvalid(XFontStruct *fs, int which)
171 {
172 unsigned int rows,pages;
173 int byte1 = 0, byte2 = 0;
174 int i,valid = 1;
175
176 rows = fs->max_byte1 - fs->min_byte1 + 1;
177 pages = fs->max_char_or_byte2 - fs->min_char_or_byte2 + 1;
178
179 if (rows == 1) {
180 /* "linear" fonts */
181 if ((fs->min_char_or_byte2 > which) ||
182 (fs->max_char_or_byte2 < which)) valid = 0;
183 } else {
184 /* "matrix" fonts */
185 byte2 = which & 0xff;
186 byte1 = which >> 8;
187 if ((fs->min_char_or_byte2 > byte2) ||
188 (fs->max_char_or_byte2 < byte2) ||
189 (fs->min_byte1 > byte1) ||
190 (fs->max_byte1 < byte1)) valid = 0;
191 }
192
193 if (valid) {
194 if (fs->per_char) {
195 if (rows == 1) {
196 /* "linear" fonts */
197 return(fs->per_char + (which-fs->min_char_or_byte2) );
198 } else {
199 /* "matrix" fonts */
200 i = ((byte1 - fs->min_byte1) * pages) +
201 (byte2 - fs->min_char_or_byte2);
202 return(fs->per_char + i);
203 }
204 } else {
205 return(&fs->min_bounds);
206 }
207 }
208 return(NULL);
209 }
210
211
212 void DRI_glXUseXFont( Font font, int first, int count, int listbase )
213 {
214 GLXContext CC;
215 Display *dpy;
216 Window win;
217 Pixmap pixmap;
218 GC gc;
219 XGCValues values;
220 unsigned long valuemask;
221 XFontStruct *fs;
222
223 GLint swapbytes, lsbfirst, rowlength;
224 GLint skiprows, skippixels, alignment;
225
226 unsigned int max_width, max_height, max_bm_width, max_bm_height;
227 GLubyte *bm;
228
229 int i;
230
231 CC = __glXGetCurrentContext();
232 dpy = CC->currentDpy;
233 win = CC->currentDrawable;
234
235 fs = XQueryFont (dpy, font);
236 if (!fs)
237 {
238 __glXSetError(CC, GL_INVALID_VALUE);
239 return;
240 }
241
242 /* Allocate a bitmap that can fit all characters. */
243 max_width = fs->max_bounds.rbearing - fs->min_bounds.lbearing;
244 max_height = fs->max_bounds.ascent + fs->max_bounds.descent;
245 max_bm_width = (max_width + 7) / 8;
246 max_bm_height = max_height;
247
248 bm = (GLubyte *) Xmalloc((max_bm_width * max_bm_height) * sizeof
249 (GLubyte));
250 if (!bm) {
251 XFreeFontInfo( NULL, fs, 1 );
252 __glXSetError(CC, GL_OUT_OF_MEMORY);
253 return;
254 }
255
256 #if 0
257 /* get the page info */
258 pages = fs->max_char_or_byte2 - fs->min_char_or_byte2 + 1;
259 firstchar = (fs->min_byte1 << 8) + fs->min_char_or_byte2;
260 lastchar = (fs->max_byte1 << 8) + fs->max_char_or_byte2;
261 rows = fs->max_byte1 - fs->min_byte1 + 1;
262 unsigned int first_char, last_char, pages, rows;
263 #endif
264
265 /* Save the current packing mode for bitmaps. */
266 glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
267 glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
268 glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
269 glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
270 glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
271 glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
272
273 /* Enforce a standard packing mode which is compatible with
274 fill_bitmap() from above. This is actually the default mode,
275 except for the (non)alignment. */
276 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
277 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
278 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
279 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
280 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
281 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
282
283 pixmap = XCreatePixmap (dpy, win, 10, 10, 1);
284 values.foreground = BlackPixel (dpy, DefaultScreen (dpy));
285 values.background = WhitePixel (dpy, DefaultScreen (dpy));
286 values.font = fs->fid;
287 valuemask = GCForeground | GCBackground | GCFont;
288 gc = XCreateGC (dpy, pixmap, valuemask, &values);
289 XFreePixmap (dpy, pixmap);
290
291 #ifdef DEBUG
292 if (debug_xfonts)
293 dump_font_struct (fs);
294 #endif
295
296 for (i = 0; i < count; i++)
297 {
298 unsigned int width, height, bm_width, bm_height;
299 GLfloat x0, y0, dx, dy;
300 XCharStruct *ch;
301 int x, y;
302 unsigned int c = first + i;
303 int list = listbase + i;
304 int valid;
305
306 /* check on index validity and get the bounds */
307 ch = isvalid(fs, c);
308 if (!ch) {
309 ch = &fs->max_bounds;
310 valid = 0;
311 } else {
312 valid = 1;
313 }
314
315 #ifdef DEBUG
316 if (debug_xfonts) {
317 char s[7];
318 sprintf (s, isprint (c) ? "%c> " : "\\%03o> ", c);
319 dump_char_struct (ch, s);
320 }
321 #endif
322
323 /* glBitmap()' parameters:
324 straight from the glXUseXFont(3) manpage. */
325 width = ch->rbearing - ch->lbearing;
326 height = ch->ascent + ch->descent;
327 x0 = - ch->lbearing;
328 y0 = ch->descent - 1;
329 dx = ch->width;
330 dy = 0;
331
332 /* X11's starting point. */
333 x = - ch->lbearing;
334 y = ch->ascent;
335
336 /* Round the width to a multiple of eight. We will use this also
337 for the pixmap for capturing the X11 font. This is slightly
338 inefficient, but it makes the OpenGL part real easy. */
339 bm_width = (width + 7) / 8;
340 bm_height = height;
341
342 glNewList (list, GL_COMPILE);
343 if (valid && (bm_width > 0) && (bm_height > 0)) {
344
345 memset (bm, '\0', bm_width * bm_height);
346 fill_bitmap (dpy, win, gc, bm_width, bm_height, x, y, c, bm);
347
348 glBitmap (width, height, x0, y0, dx, dy, bm);
349 #ifdef DEBUG
350 if (debug_xfonts) {
351 printf ("width/height = %u/%u\n", width, height);
352 printf ("bm_width/bm_height = %u/%u\n", bm_width,
353 bm_height);
354 dump_bitmap (bm_width, bm_height, bm);
355 }
356 #endif
357 } else {
358 glBitmap (0, 0, 0.0, 0.0, dx, dy, NULL);
359 }
360 glEndList ();
361 }
362
363 Xfree(bm);
364 XFreeFontInfo( NULL, fs, 1 );
365 XFreeGC (dpy, gc);
366
367 /* Restore saved packing modes. */
368 glPixelStorei(GL_UNPACK_SWAP_BYTES, swapbytes);
369 glPixelStorei(GL_UNPACK_LSB_FIRST, lsbfirst);
370 glPixelStorei(GL_UNPACK_ROW_LENGTH, rowlength);
371 glPixelStorei(GL_UNPACK_SKIP_ROWS, skiprows);
372 glPixelStorei(GL_UNPACK_SKIP_PIXELS, skippixels);
373 glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
374 }
375
376 /* The End. */