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