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