2 * Mesa 3-D graphics library
4 * Copyright (C) 1995 Thorsten.Ohl @ Physik.TH-Darmstadt.de
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. 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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
29 * Fake implementation of glXUseXFont().
36 #include "main/errors.h"
39 /* Some debugging info. */
47 dump_char_struct(XCharStruct
* ch
, char *prefix
)
49 printf("%slbearing = %d, rbearing = %d, width = %d\n",
50 prefix
, ch
->lbearing
, ch
->rbearing
, ch
->width
);
51 printf("%sascent = %d, descent = %d, attributes = %u\n",
52 prefix
, ch
->ascent
, ch
->descent
, (unsigned int) ch
->attributes
);
56 dump_font_struct(XFontStruct
* font
)
58 printf("ascent = %d, descent = %d\n", font
->ascent
, font
->descent
);
59 printf("char_or_byte2 = (%u,%u)\n",
60 font
->min_char_or_byte2
, font
->max_char_or_byte2
);
61 printf("byte1 = (%u,%u)\n", font
->min_byte1
, font
->max_byte1
);
62 printf("all_chars_exist = %s\n", font
->all_chars_exist
? "True" : "False");
63 printf("default_char = %c (\\%03o)\n",
64 (char) (isprint(font
->default_char
) ? font
->default_char
: ' '),
66 dump_char_struct(&font
->min_bounds
, "min> ");
67 dump_char_struct(&font
->max_bounds
, "max> ");
69 for (c
= font
->min_char_or_byte2
; c
<= font
->max_char_or_byte2
; c
++) {
71 sprintf(prefix
, "%d> ", c
);
72 dump_char_struct(&font
->per_char
[c
], prefix
);
78 dump_bitmap(unsigned int width
, unsigned int height
, GLubyte
* bitmap
)
83 for (x
= 0; x
< 8 * width
; x
++)
84 printf("%o", 7 - (x
% 8));
86 for (y
= 0; y
< height
; y
++) {
88 for (x
= 0; x
< 8 * width
; x
++)
89 putchar((bitmap
[width
* (height
- y
- 1) + x
/ 8] & (1 << (7 - (x
%
93 for (x
= 0; x
< width
; x
++)
94 printf("0x%02x, ", bitmap
[width
* (height
- y
- 1) + x
]);
101 /* Implementation. */
103 /* Fill a BITMAP with a character C from thew current font
104 in the graphics context GC. WIDTH is the width in bytes
105 and HEIGHT is the height in bits.
107 Note that the generated bitmaps must be used with
109 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
110 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
111 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
112 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
113 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
114 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
116 Possible optimizations:
118 * use only one reusable pixmap with the maximum dimensions.
119 * draw the entire font into a single pixmap (careful with
120 proportional fonts!).
125 * Generate OpenGL-compatible bitmap.
128 fill_bitmap(Display
* dpy
, Window win
, GC gc
,
129 unsigned int width
, unsigned int height
,
130 int x0
, int y0
, unsigned int c
, GLubyte
* bitmap
)
137 pixmap
= XCreatePixmap(dpy
, win
, 8 * width
, height
, 1);
138 XSetForeground(dpy
, gc
, 0);
139 XFillRectangle(dpy
, pixmap
, gc
, 0, 0, 8 * width
, height
);
140 XSetForeground(dpy
, gc
, 1);
142 char2b
.byte1
= (c
>> 8) & 0xff;
143 char2b
.byte2
= (c
& 0xff);
145 XDrawString16(dpy
, pixmap
, gc
, x0
, y0
, &char2b
, 1);
147 image
= XGetImage(dpy
, pixmap
, 0, 0, 8 * width
, height
, 1, XYPixmap
);
149 /* Fill the bitmap (X11 and OpenGL are upside down wrt each other). */
150 for (y
= 0; y
< height
; y
++)
151 for (x
= 0; x
< 8 * width
; x
++)
152 if (XGetPixel(image
, x
, y
))
153 bitmap
[width
* (height
- y
- 1) + x
/ 8] |=
154 (1 << (7 - (x
% 8)));
155 XDestroyImage(image
);
158 XFreePixmap(dpy
, pixmap
);
162 * determine if a given glyph is valid and return the
163 * corresponding XCharStruct.
166 isvalid(XFontStruct
* fs
, unsigned int which
)
168 unsigned int rows
, pages
;
169 unsigned int byte1
= 0, byte2
= 0;
172 rows
= fs
->max_byte1
- fs
->min_byte1
+ 1;
173 pages
= fs
->max_char_or_byte2
- fs
->min_char_or_byte2
+ 1;
177 if ((fs
->min_char_or_byte2
> which
) || (fs
->max_char_or_byte2
< which
))
182 byte2
= which
& 0xff;
184 if ((fs
->min_char_or_byte2
> byte2
) ||
185 (fs
->max_char_or_byte2
< byte2
) ||
186 (fs
->min_byte1
> byte1
) || (fs
->max_byte1
< byte1
))
194 return (fs
->per_char
+ (which
- fs
->min_char_or_byte2
));
198 i
= ((byte1
- fs
->min_byte1
) * pages
) +
199 (byte2
- fs
->min_char_or_byte2
);
200 return (fs
->per_char
+ i
);
204 return (&fs
->min_bounds
);
212 glXUseXFont(Font font
, int first
, int count
, int listbase
)
219 unsigned long valuemask
;
221 GLint swapbytes
, lsbfirst
, rowlength
;
222 GLint skiprows
, skippixels
, alignment
;
223 unsigned int max_width
, max_height
, max_bm_width
, max_bm_height
;
227 dpy
= glXGetCurrentDisplay();
229 return; /* I guess glXMakeCurrent wasn't called */
230 i
= DefaultScreen(dpy
);
231 win
= RootWindow(dpy
, i
);
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
= 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
);