3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
27 /* xfonts.c -- glXUseXFont() for Mesa written by
28 * Copyright (C) 1995 Thorsten.Ohl @ Physik.TH-Darmstadt.de
37 /* Some debugging info. */
48 dump_char_struct(XCharStruct
* ch
, char *prefix
)
50 printf("%slbearing = %d, rbearing = %d, width = %d\n",
51 prefix
, ch
->lbearing
, ch
->rbearing
, ch
->width
);
52 printf("%sascent = %d, descent = %d, attributes = %u\n",
53 prefix
, ch
->ascent
, ch
->descent
, (unsigned int) ch
->attributes
);
57 dump_font_struct(XFontStruct
* font
)
59 printf("ascent = %d, descent = %d\n", font
->ascent
, font
->descent
);
60 printf("char_or_byte2 = (%u,%u)\n",
61 font
->min_char_or_byte2
, font
->max_char_or_byte2
);
62 printf("byte1 = (%u,%u)\n", font
->min_byte1
, font
->max_byte1
);
63 printf("all_chars_exist = %s\n", font
->all_chars_exist
? "True" : "False");
64 printf("default_char = %c (\\%03o)\n",
65 (char) (isprint(font
->default_char
) ? font
->default_char
: ' '),
67 dump_char_struct(&font
->min_bounds
, "min> ");
68 dump_char_struct(&font
->max_bounds
, "max> ");
70 for (c
= font
->min_char_or_byte2
; c
<= font
->max_char_or_byte2
; c
++) {
72 sprintf(prefix
, "%d> ", c
);
73 dump_char_struct(&font
->per_char
[c
], prefix
);
79 dump_bitmap(unsigned int width
, unsigned int height
, GLubyte
* bitmap
)
84 for (x
= 0; x
< 8 * width
; x
++)
85 printf("%o", 7 - (x
% 8));
87 for (y
= 0; y
< height
; y
++) {
89 for (x
= 0; x
< 8 * width
; x
++)
90 putchar((bitmap
[width
* (height
- y
- 1) + x
/ 8] & (1 << (7 - (x
%
94 for (x
= 0; x
< width
; x
++)
95 printf("0x%02x, ", bitmap
[width
* (height
- y
- 1) + x
]);
102 /* Implementation. */
104 /* Fill a BITMAP with a character C from thew current font
105 in the graphics context GC. WIDTH is the width in bytes
106 and HEIGHT is the height in bits.
108 Note that the generated bitmaps must be used with
110 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
111 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
112 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
113 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
114 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
115 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
117 Possible optimizations:
119 * use only one reusable pixmap with the maximum dimensions.
120 * draw the entire font into a single pixmap (careful with
121 proportional fonts!).
126 * Generate OpenGL-compatible bitmap.
129 fill_bitmap(Display
* dpy
, Window win
, GC gc
,
130 unsigned int width
, unsigned int height
,
131 int x0
, int y0
, unsigned int c
, GLubyte
* bitmap
)
138 pixmap
= XCreatePixmap(dpy
, win
, 8 * width
, height
, 1);
139 XSetForeground(dpy
, gc
, 0);
140 XFillRectangle(dpy
, pixmap
, gc
, 0, 0, 8 * width
, height
);
141 XSetForeground(dpy
, gc
, 1);
143 char2b
.byte1
= (c
>> 8) & 0xff;
144 char2b
.byte2
= (c
& 0xff);
146 XDrawString16(dpy
, pixmap
, gc
, x0
, y0
, &char2b
, 1);
148 image
= XGetImage(dpy
, pixmap
, 0, 0, 8 * width
, height
, 1, XYPixmap
);
150 /* Fill the bitmap (X11 and OpenGL are upside down wrt each other). */
151 for (y
= 0; y
< height
; y
++)
152 for (x
= 0; x
< 8 * width
; x
++)
153 if (XGetPixel(image
, x
, y
))
154 bitmap
[width
* (height
- y
- 1) + x
/ 8] |=
155 (1 << (7 - (x
% 8)));
156 XDestroyImage(image
);
159 XFreePixmap(dpy
, pixmap
);
163 * determine if a given glyph is valid and return the
164 * corresponding XCharStruct.
167 isvalid(XFontStruct
* fs
, unsigned int which
)
169 unsigned int rows
, pages
;
170 unsigned int byte1
= 0, byte2
= 0;
173 rows
= fs
->max_byte1
- fs
->min_byte1
+ 1;
174 pages
= fs
->max_char_or_byte2
- fs
->min_char_or_byte2
+ 1;
178 if ((fs
->min_char_or_byte2
> which
) || (fs
->max_char_or_byte2
< which
))
183 byte2
= which
& 0xff;
185 if ((fs
->min_char_or_byte2
> byte2
) ||
186 (fs
->max_char_or_byte2
< byte2
) ||
187 (fs
->min_byte1
> byte1
) || (fs
->max_byte1
< byte1
))
195 return (fs
->per_char
+ (which
- fs
->min_char_or_byte2
));
199 i
= ((byte1
- fs
->min_byte1
) * pages
) +
200 (byte2
- fs
->min_char_or_byte2
);
201 return (fs
->per_char
+ i
);
205 return (&fs
->min_bounds
);
213 Fake_glXUseXFont(Font font
, int first
, int count
, int listbase
)
220 unsigned long valuemask
;
222 GLint swapbytes
, lsbfirst
, rowlength
;
223 GLint skiprows
, skippixels
, alignment
;
224 unsigned int max_width
, max_height
, max_bm_width
, max_bm_height
;
228 dpy
= glXGetCurrentDisplay();
230 return; /* I guess glXMakeCurrent wasn't called */
231 win
= RootWindow(dpy
, DefaultScreen(dpy
));
233 fs
= XQueryFont(dpy
, font
);
235 _mesa_error(NULL
, GL_INVALID_VALUE
,
236 "Couldn't get font structure information");
240 /* Allocate a bitmap that can fit all characters. */
241 max_width
= fs
->max_bounds
.rbearing
- fs
->min_bounds
.lbearing
;
242 max_height
= fs
->max_bounds
.ascent
+ fs
->max_bounds
.descent
;
243 max_bm_width
= (max_width
+ 7) / 8;
244 max_bm_height
= max_height
;
246 bm
= (GLubyte
*) MALLOC((max_bm_width
* max_bm_height
) * sizeof(GLubyte
));
248 XFreeFontInfo(NULL
, fs
, 1);
249 _mesa_error(NULL
, GL_OUT_OF_MEMORY
,
250 "Couldn't allocate bitmap in glXUseXFont()");
255 /* get the page info */
256 pages
= fs
->max_char_or_byte2
- fs
->min_char_or_byte2
+ 1;
257 firstchar
= (fs
->min_byte1
<< 8) + fs
->min_char_or_byte2
;
258 lastchar
= (fs
->max_byte1
<< 8) + fs
->max_char_or_byte2
;
259 rows
= fs
->max_byte1
- fs
->min_byte1
+ 1;
260 unsigned int first_char
, last_char
, pages
, rows
;
263 /* Save the current packing mode for bitmaps. */
264 glGetIntegerv(GL_UNPACK_SWAP_BYTES
, &swapbytes
);
265 glGetIntegerv(GL_UNPACK_LSB_FIRST
, &lsbfirst
);
266 glGetIntegerv(GL_UNPACK_ROW_LENGTH
, &rowlength
);
267 glGetIntegerv(GL_UNPACK_SKIP_ROWS
, &skiprows
);
268 glGetIntegerv(GL_UNPACK_SKIP_PIXELS
, &skippixels
);
269 glGetIntegerv(GL_UNPACK_ALIGNMENT
, &alignment
);
271 /* Enforce a standard packing mode which is compatible with
272 fill_bitmap() from above. This is actually the default mode,
273 except for the (non)alignment. */
274 glPixelStorei(GL_UNPACK_SWAP_BYTES
, GL_FALSE
);
275 glPixelStorei(GL_UNPACK_LSB_FIRST
, GL_FALSE
);
276 glPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
277 glPixelStorei(GL_UNPACK_SKIP_ROWS
, 0);
278 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, 0);
279 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
281 pixmap
= XCreatePixmap(dpy
, win
, 10, 10, 1);
282 values
.foreground
= BlackPixel(dpy
, DefaultScreen(dpy
));
283 values
.background
= WhitePixel(dpy
, DefaultScreen(dpy
));
284 values
.font
= fs
->fid
;
285 valuemask
= GCForeground
| GCBackground
| GCFont
;
286 gc
= XCreateGC(dpy
, pixmap
, valuemask
, &values
);
287 XFreePixmap(dpy
, pixmap
);
291 dump_font_struct(fs
);
294 for (i
= 0; i
< count
; i
++) {
295 unsigned int width
, height
, bm_width
, bm_height
;
296 GLfloat x0
, y0
, dx
, dy
;
299 unsigned int c
= first
+ i
;
300 int list
= listbase
+ i
;
303 /* check on index validity and get the bounds */
306 ch
= &fs
->max_bounds
;
316 sprintf(s
, isprint(c
) ? "%c> " : "\\%03o> ", c
);
317 dump_char_struct(ch
, s
);
321 /* glBitmap()' parameters:
322 straight from the glXUseXFont(3) manpage. */
323 width
= ch
->rbearing
- ch
->lbearing
;
324 height
= ch
->ascent
+ ch
->descent
;
326 y0
= ch
->descent
- 0; /* XXX used to subtract 1 here */
327 /* but that caused a conformace failure */
331 /* X11's starting point. */
335 /* Round the width to a multiple of eight. We will use this also
336 for the pixmap for capturing the X11 font. This is slightly
337 inefficient, but it makes the OpenGL part real easy. */
338 bm_width
= (width
+ 7) / 8;
341 glNewList(list
, GL_COMPILE
);
342 if (valid
&& (bm_width
> 0) && (bm_height
> 0)) {
344 MEMSET(bm
, '\0', bm_width
* bm_height
);
345 fill_bitmap(dpy
, win
, gc
, bm_width
, bm_height
, x
, y
, c
, bm
);
347 glBitmap(width
, height
, x0
, y0
, dx
, dy
, bm
);
350 printf("width/height = %u/%u\n", width
, height
);
351 printf("bm_width/bm_height = %u/%u\n", bm_width
, bm_height
);
352 dump_bitmap(bm_width
, bm_height
, bm
);
357 glBitmap(0, 0, 0.0, 0.0, dx
, dy
, NULL
);
363 XFreeFontInfo(NULL
, fs
, 1);
366 /* Restore saved packing modes. */
367 glPixelStorei(GL_UNPACK_SWAP_BYTES
, swapbytes
);
368 glPixelStorei(GL_UNPACK_LSB_FIRST
, lsbfirst
);
369 glPixelStorei(GL_UNPACK_ROW_LENGTH
, rowlength
);
370 glPixelStorei(GL_UNPACK_SKIP_ROWS
, skiprows
);
371 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, skippixels
);
372 glPixelStorei(GL_UNPACK_ALIGNMENT
, alignment
);