1 /* $Id: xfonts.c,v 1.8 2000/08/13 03:57:21 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
28 /* xfonts.c -- glXUseXFont() for Mesa written by
29 * Copyright (C) 1995 Thorsten.Ohl @ Physik.TH-Darmstadt.de
41 #include <X11/Xutil.h>
49 /* Some debugging info. */
60 dump_char_struct (XCharStruct
*ch
, char *prefix
)
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
);
69 dump_font_struct (XFontStruct
*font
)
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" :
77 printf ("default_char = %c (\\%03o)\n",
78 (char) (isprint (font
->default_char
) ? font
->default_char
: ' '),
80 dump_char_struct (&font
->min_bounds
, "min> ");
81 dump_char_struct (&font
->max_bounds
, "max> ");
83 for (c
= font
->min_char_or_byte2
; c
<= font
->max_char_or_byte2
; c
++)
86 sprintf (prefix
, "%d> ", c
);
87 dump_char_struct (&font
->per_char
[c
], prefix
);
93 dump_bitmap (unsigned int width
, unsigned int height
, GLubyte
*bitmap
)
98 for (x
= 0; x
< 8*width
; x
++)
99 printf ("%o", 7 - (x
% 8));
101 for (y
= 0; y
< height
; y
++)
104 for (x
= 0; x
< 8*width
; x
++)
105 putchar ((bitmap
[width
*(height
- y
- 1) + x
/8] & (1 << (7 - (x
%
109 for (x
= 0; x
< width
; x
++)
110 printf ("0x%02x, ", bitmap
[width
*(height
- y
- 1) + x
]);
117 /* Implementation. */
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.
123 Note that the generated bitmaps must be used with
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);
132 Possible optimizations:
134 * use only one reusable pixmap with the maximum dimensions.
135 * draw the entire font into a single pixmap (careful with
136 proportional fonts!).
141 * Generate OpenGL-compatible bitmap.
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
)
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);
158 char2b
.byte1
= (c
>> 8) & 0xff;
159 char2b
.byte2
= (c
& 0xff);
161 XDrawString16 (dpy
, pixmap
, gc
, x0
, y0
, &char2b
, 1);
163 image
= XGetImage (dpy
, pixmap
, 0, 0, 8*width
, height
, 1, XYPixmap
);
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
);
173 XFreePixmap (dpy
, pixmap
);
177 * determine if a given glyph is valid and return the
178 * corresponding XCharStruct.
180 static XCharStruct
*isvalid(XFontStruct
*fs
, int which
)
182 unsigned int rows
,pages
;
186 rows
= fs
->max_byte1
- fs
->min_byte1
+ 1;
187 pages
= fs
->max_char_or_byte2
- fs
->min_char_or_byte2
+ 1;
191 if ((fs
->min_char_or_byte2
> which
) ||
192 (fs
->max_char_or_byte2
< which
)) valid
= 0;
195 byte2
= which
& 0xff;
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;
207 return(fs
->per_char
+ (which
-fs
->min_char_or_byte2
) );
210 i
= ((byte1
- fs
->min_byte1
) * pages
) +
211 (byte2
- fs
->min_char_or_byte2
);
212 return(fs
->per_char
+ i
);
215 return(&fs
->min_bounds
);
222 void Fake_glXUseXFont( Font font
, int first
, int count
, int listbase
)
229 unsigned long valuemask
;
231 GLint swapbytes
, lsbfirst
, rowlength
;
232 GLint skiprows
, skippixels
, alignment
;
233 unsigned int max_width
, max_height
, max_bm_width
, max_bm_height
;
237 dpy
= glXGetCurrentDisplay();
239 return; /* I guess glXMakeCurrent wasn't called */
240 win
= RootWindow(dpy
, DefaultScreen(dpy
));
242 fs
= XQueryFont (dpy
, font
);
244 gl_error(NULL
, GL_INVALID_VALUE
,
245 "Couldn't get font structure information");
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
;
255 bm
= (GLubyte
*) MALLOC((max_bm_width
* max_bm_height
) * sizeof
258 XFreeFontInfo( NULL
, fs
, 0 );
259 gl_error(NULL
, GL_OUT_OF_MEMORY
,
260 "Couldn't allocate bitmap in glXUseXFont()");
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
;
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
);
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);
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
);
301 dump_font_struct (fs
);
304 for (i
= 0; i
< count
; i
++)
306 unsigned int width
, height
, bm_width
, bm_height
;
307 GLfloat x0
, y0
, dx
, dy
;
310 unsigned int c
= first
+ i
;
311 int list
= listbase
+ i
;
314 /* check on index validity and get the bounds */
317 ch
= &fs
->max_bounds
;
326 sprintf (s
, isprint (c
) ? "%c> " : "\\%03o> ", c
);
327 dump_char_struct (ch
, s
);
331 /* glBitmap()' parameters:
332 straight from the glXUseXFont(3) manpage. */
333 width
= ch
->rbearing
- ch
->lbearing
;
334 height
= ch
->ascent
+ ch
->descent
;
336 y0
= ch
->descent
- 0; /* XXX used to subtract 1 here */
337 /* but that caused a conformace failure */
341 /* X11's starting point. */
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;
351 glNewList (list
, GL_COMPILE
);
352 if (valid
&& (bm_width
> 0) && (bm_height
> 0)) {
354 MEMSET (bm
, '\0', bm_width
* bm_height
);
355 fill_bitmap (dpy
, win
, gc
, bm_width
, bm_height
, x
, y
, c
, bm
);
357 glBitmap (width
, height
, x0
, y0
, dx
, dy
, bm
);
360 printf ("width/height = %u/%u\n", width
, height
);
361 printf ("bm_width/bm_height = %u/%u\n", bm_width
,
363 dump_bitmap (bm_width
, bm_height
, bm
);
367 glBitmap (0, 0, 0.0, 0.0, dx
, dy
, NULL
);
373 XFreeFontInfo( NULL
, fs
, 0 );
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
);