1 /* $Id: xfonts.c,v 1.3 1999/11/11 01:29:28 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 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>
50 /* Some debugging info. */
61 dump_char_struct (XCharStruct
*ch
, char *prefix
)
63 printf ("%slbearing = %d, rbearing = %d, width = %d\n",
64 prefix
, ch
->lbearing
, ch
->rbearing
, ch
->width
);
65 printf ("%sascent = %d, descent = %d, attributes = %u\n",
66 prefix
, ch
->ascent
, ch
->descent
, (unsigned int) ch
->attributes
);
70 dump_font_struct (XFontStruct
*font
)
72 printf ("ascent = %d, descent = %d\n", font
->ascent
, font
->descent
);
73 printf ("char_or_byte2 = (%u,%u)\n",
74 font
->min_char_or_byte2
, font
->max_char_or_byte2
);
75 printf ("byte1 = (%u,%u)\n", font
->min_byte1
, font
->max_byte1
);
76 printf ("all_chars_exist = %s\n", font
->all_chars_exist
? "True" :
78 printf ("default_char = %c (\\%03o)\n",
79 (char) (isprint (font
->default_char
) ? font
->default_char
: ' '),
81 dump_char_struct (&font
->min_bounds
, "min> ");
82 dump_char_struct (&font
->max_bounds
, "max> ");
84 for (c
= font
->min_char_or_byte2
; c
<= font
->max_char_or_byte2
; c
++)
87 sprintf (prefix
, "%d> ", c
);
88 dump_char_struct (&font
->per_char
[c
], prefix
);
94 dump_bitmap (unsigned int width
, unsigned int height
, GLubyte
*bitmap
)
99 for (x
= 0; x
< 8*width
; x
++)
100 printf ("%o", 7 - (x
% 8));
102 for (y
= 0; y
< height
; y
++)
105 for (x
= 0; x
< 8*width
; x
++)
106 putchar ((bitmap
[width
*(height
- y
- 1) + x
/8] & (1 << (7 - (x
%
110 for (x
= 0; x
< width
; x
++)
111 printf ("0x%02x, ", bitmap
[width
*(height
- y
- 1) + x
]);
118 /* Implementation. */
120 /* Fill a BITMAP with a character C from thew current font
121 in the graphics context GC. WIDTH is the width in bytes
122 and HEIGHT is the height in bits.
124 Note that the generated bitmaps must be used with
126 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
127 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
128 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
129 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
130 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
131 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
133 Possible optimizations:
135 * use only one reusable pixmap with the maximum dimensions.
136 * draw the entire font into a single pixmap (careful with
137 proportional fonts!).
142 * Generate OpenGL-compatible bitmap.
145 fill_bitmap (Display
*dpy
, Window win
, GC gc
,
146 unsigned int width
, unsigned int height
,
147 int x0
, int y0
, unsigned int c
, GLubyte
*bitmap
)
154 pixmap
= XCreatePixmap (dpy
, win
, 8*width
, height
, 1);
155 XSetForeground(dpy
, gc
, 0);
156 XFillRectangle (dpy
, pixmap
, gc
, 0, 0, 8*width
, height
);
157 XSetForeground(dpy
, gc
, 1);
159 char2b
.byte1
= (c
>> 8) & 0xff;
160 char2b
.byte2
= (c
& 0xff);
162 XDrawString16 (dpy
, pixmap
, gc
, x0
, y0
, &char2b
, 1);
164 image
= XGetImage (dpy
, pixmap
, 0, 0, 8*width
, height
, 1, XYPixmap
);
166 /* Fill the bitmap (X11 and OpenGL are upside down wrt each other). */
167 for (y
= 0; y
< height
; y
++)
168 for (x
= 0; x
< 8*width
; x
++)
169 if (XGetPixel (image
, x
, y
))
170 bitmap
[width
*(height
- y
- 1) + x
/8] |= (1 << (7 - (x
% 8)));
171 XDestroyImage (image
);
174 XFreePixmap (dpy
, pixmap
);
178 * determine if a given glyph is valid and return the
179 * corresponding XCharStruct.
181 static XCharStruct
*isvalid(XFontStruct
*fs
, int which
)
183 unsigned int rows
,pages
;
187 rows
= fs
->max_byte1
- fs
->min_byte1
+ 1;
188 pages
= fs
->max_char_or_byte2
- fs
->min_char_or_byte2
+ 1;
192 if ((fs
->min_char_or_byte2
> which
) ||
193 (fs
->max_char_or_byte2
< which
)) valid
= 0;
196 byte2
= which
& 0xff;
198 if ((fs
->min_char_or_byte2
> byte2
) ||
199 (fs
->max_char_or_byte2
< byte2
) ||
200 (fs
->min_byte1
> byte1
) ||
201 (fs
->max_byte1
< byte1
)) valid
= 0;
208 return(fs
->per_char
+ (which
-fs
->min_char_or_byte2
) );
211 i
= ((byte1
- fs
->min_byte1
) * pages
) +
212 (byte2
- fs
->min_char_or_byte2
);
213 return(fs
->per_char
+ i
);
216 return(&fs
->min_bounds
);
223 void Fake_glXUseXFont( Font font
, int first
, int count
, int listbase
)
231 unsigned long valuemask
;
234 GLint swapbytes
, lsbfirst
, rowlength
;
235 GLint skiprows
, skippixels
, alignment
;
237 unsigned int max_width
, max_height
, max_bm_width
, max_bm_height
;
242 CC
= XMesaGetCurrentContext();
244 win
= CC
->xm_buffer
->frontbuffer
;
246 fs
= XQueryFont (dpy
, font
);
249 gl_error (CC
->gl_ctx
, GL_INVALID_VALUE
,
250 "Couldn't get font structure information");
254 /* Allocate a bitmap that can fit all characters. */
255 max_width
= fs
->max_bounds
.rbearing
- fs
->min_bounds
.lbearing
;
256 max_height
= fs
->max_bounds
.ascent
+ fs
->max_bounds
.descent
;
257 max_bm_width
= (max_width
+ 7) / 8;
258 max_bm_height
= max_height
;
260 bm
= (GLubyte
*) MALLOC((max_bm_width
* max_bm_height
) * sizeof
263 XFreeFontInfo( NULL
, fs
, 0 );
264 gl_error (CC
->gl_ctx
, GL_OUT_OF_MEMORY
,
265 "Couldn't allocate bitmap in glXUseXFont()");
270 /* get the page info */
271 pages
= fs
->max_char_or_byte2
- fs
->min_char_or_byte2
+ 1;
272 firstchar
= (fs
->min_byte1
<< 8) + fs
->min_char_or_byte2
;
273 lastchar
= (fs
->max_byte1
<< 8) + fs
->max_char_or_byte2
;
274 rows
= fs
->max_byte1
- fs
->min_byte1
+ 1;
275 unsigned int first_char
, last_char
, pages
, rows
;
278 /* Save the current packing mode for bitmaps. */
279 glGetIntegerv (GL_UNPACK_SWAP_BYTES
, &swapbytes
);
280 glGetIntegerv (GL_UNPACK_LSB_FIRST
, &lsbfirst
);
281 glGetIntegerv (GL_UNPACK_ROW_LENGTH
, &rowlength
);
282 glGetIntegerv (GL_UNPACK_SKIP_ROWS
, &skiprows
);
283 glGetIntegerv (GL_UNPACK_SKIP_PIXELS
, &skippixels
);
284 glGetIntegerv (GL_UNPACK_ALIGNMENT
, &alignment
);
286 /* Enforce a standard packing mode which is compatible with
287 fill_bitmap() from above. This is actually the default mode,
288 except for the (non)alignment. */
289 glPixelStorei (GL_UNPACK_SWAP_BYTES
, GL_FALSE
);
290 glPixelStorei (GL_UNPACK_LSB_FIRST
, GL_FALSE
);
291 glPixelStorei (GL_UNPACK_ROW_LENGTH
, 0);
292 glPixelStorei (GL_UNPACK_SKIP_ROWS
, 0);
293 glPixelStorei (GL_UNPACK_SKIP_PIXELS
, 0);
294 glPixelStorei (GL_UNPACK_ALIGNMENT
, 1);
296 pixmap
= XCreatePixmap (dpy
, win
, 10, 10, 1);
297 values
.foreground
= BlackPixel (dpy
, DefaultScreen (dpy
));
298 values
.background
= WhitePixel (dpy
, DefaultScreen (dpy
));
299 values
.font
= fs
->fid
;
300 valuemask
= GCForeground
| GCBackground
| GCFont
;
301 gc
= XCreateGC (dpy
, pixmap
, valuemask
, &values
);
302 XFreePixmap (dpy
, pixmap
);
306 dump_font_struct (fs
);
309 for (i
= 0; i
< count
; i
++)
311 unsigned int width
, height
, bm_width
, bm_height
;
312 GLfloat x0
, y0
, dx
, dy
;
315 unsigned int c
= first
+ i
;
316 int list
= listbase
+ i
;
319 /* check on index validity and get the bounds */
322 ch
= &fs
->max_bounds
;
331 sprintf (s
, isprint (c
) ? "%c> " : "\\%03o> ", c
);
332 dump_char_struct (ch
, s
);
336 /* glBitmap()' parameters:
337 straight from the glXUseXFont(3) manpage. */
338 width
= ch
->rbearing
- ch
->lbearing
;
339 height
= ch
->ascent
+ ch
->descent
;
341 y0
= ch
->descent
- 1;
345 /* X11's starting point. */
349 /* Round the width to a multiple of eight. We will use this also
350 for the pixmap for capturing the X11 font. This is slightly
351 inefficient, but it makes the OpenGL part real easy. */
352 bm_width
= (width
+ 7) / 8;
355 glNewList (list
, GL_COMPILE
);
356 if (valid
&& (bm_width
> 0) && (bm_height
> 0)) {
358 MEMSET (bm
, '\0', bm_width
* bm_height
);
359 fill_bitmap (dpy
, win
, gc
, bm_width
, bm_height
, x
, y
, c
, bm
);
361 glBitmap (width
, height
, x0
, y0
, dx
, dy
, bm
);
364 printf ("width/height = %u/%u\n", width
, height
);
365 printf ("bm_width/bm_height = %u/%u\n", bm_width
,
367 dump_bitmap (bm_width
, bm_height
, bm
);
371 glBitmap (0, 0, 0.0, 0.0, dx
, dy
, NULL
);
377 XFreeFontInfo( NULL
, fs
, 0 );
380 /* Restore saved packing modes. */
381 glPixelStorei(GL_UNPACK_SWAP_BYTES
, swapbytes
);
382 glPixelStorei(GL_UNPACK_LSB_FIRST
, lsbfirst
);
383 glPixelStorei(GL_UNPACK_ROW_LENGTH
, rowlength
);
384 glPixelStorei(GL_UNPACK_SKIP_ROWS
, skiprows
);
385 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, skippixels
);
386 glPixelStorei(GL_UNPACK_ALIGNMENT
, alignment
);
389 void xmesa_xfonts_dummy( void )
391 /* silence unused var warnings */