2 * Mesa 3-D graphics library
5 * Copyright (C) 1995 Thorsten.Ohl @ Physik.TH-Darmstadt.de
6 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Copyright (C) 2009 VMware, Inc. 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.
29 * Fake implementation of glXUseXFont().
33 #include "main/context.h"
34 #include "main/imports.h"
36 #include "pipe/p_compiler.h"
39 /* Some debugging info. */
50 dump_char_struct(XCharStruct
* ch
, char *prefix
)
52 printf("%slbearing = %d, rbearing = %d, width = %d\n",
53 prefix
, ch
->lbearing
, ch
->rbearing
, ch
->width
);
54 printf("%sascent = %d, descent = %d, attributes = %u\n",
55 prefix
, ch
->ascent
, ch
->descent
, (unsigned int) ch
->attributes
);
59 dump_font_struct(XFontStruct
* font
)
61 printf("ascent = %d, descent = %d\n", font
->ascent
, font
->descent
);
62 printf("char_or_byte2 = (%u,%u)\n",
63 font
->min_char_or_byte2
, font
->max_char_or_byte2
);
64 printf("byte1 = (%u,%u)\n", font
->min_byte1
, font
->max_byte1
);
65 printf("all_chars_exist = %s\n", font
->all_chars_exist
? "True" : "False");
66 printf("default_char = %c (\\%03o)\n",
67 (char) (isprint(font
->default_char
) ? font
->default_char
: ' '),
69 dump_char_struct(&font
->min_bounds
, "min> ");
70 dump_char_struct(&font
->max_bounds
, "max> ");
72 for (c
= font
->min_char_or_byte2
; c
<= font
->max_char_or_byte2
; c
++) {
74 sprintf(prefix
, "%d> ", c
);
75 dump_char_struct(&font
->per_char
[c
], prefix
);
81 dump_bitmap(unsigned int width
, unsigned int height
, GLubyte
* bitmap
)
86 for (x
= 0; x
< 8 * width
; x
++)
87 printf("%o", 7 - (x
% 8));
89 for (y
= 0; y
< height
; y
++) {
91 for (x
= 0; x
< 8 * width
; x
++)
92 putchar((bitmap
[width
* (height
- y
- 1) + x
/ 8] & (1 << (7 - (x
%
96 for (x
= 0; x
< width
; x
++)
97 printf("0x%02x, ", bitmap
[width
* (height
- y
- 1) + x
]);
104 /* Implementation. */
106 /* Fill a BITMAP with a character C from thew current font
107 in the graphics context GC. WIDTH is the width in bytes
108 and HEIGHT is the height in bits.
110 Note that the generated bitmaps must be used with
112 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
113 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
114 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
115 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
116 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
117 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
119 Possible optimizations:
121 * use only one reusable pixmap with the maximum dimensions.
122 * draw the entire font into a single pixmap (careful with
123 proportional fonts!).
128 * Generate OpenGL-compatible bitmap.
131 fill_bitmap(Display
* dpy
, Window win
, GC gc
,
132 unsigned int width
, unsigned int height
,
133 int x0
, int y0
, unsigned int c
, GLubyte
* bitmap
)
140 pixmap
= XCreatePixmap(dpy
, win
, 8 * width
, height
, 1);
141 XSetForeground(dpy
, gc
, 0);
142 XFillRectangle(dpy
, pixmap
, gc
, 0, 0, 8 * width
, height
);
143 XSetForeground(dpy
, gc
, 1);
145 char2b
.byte1
= (c
>> 8) & 0xff;
146 char2b
.byte2
= (c
& 0xff);
148 XDrawString16(dpy
, pixmap
, gc
, x0
, y0
, &char2b
, 1);
150 image
= XGetImage(dpy
, pixmap
, 0, 0, 8 * width
, height
, 1, XYPixmap
);
152 /* Fill the bitmap (X11 and OpenGL are upside down wrt each other). */
153 for (y
= 0; y
< height
; y
++)
154 for (x
= 0; x
< 8 * width
; x
++)
155 if (XGetPixel(image
, x
, y
))
156 bitmap
[width
* (height
- y
- 1) + x
/ 8] |=
157 (1 << (7 - (x
% 8)));
158 XDestroyImage(image
);
161 XFreePixmap(dpy
, pixmap
);
165 * determine if a given glyph is valid and return the
166 * corresponding XCharStruct.
169 isvalid(XFontStruct
* fs
, unsigned int which
)
171 unsigned int rows
, pages
;
172 unsigned int byte1
= 0, byte2
= 0;
175 rows
= fs
->max_byte1
- fs
->min_byte1
+ 1;
176 pages
= fs
->max_char_or_byte2
- fs
->min_char_or_byte2
+ 1;
180 if ((fs
->min_char_or_byte2
> which
) || (fs
->max_char_or_byte2
< which
))
185 byte2
= which
& 0xff;
187 if ((fs
->min_char_or_byte2
> byte2
) ||
188 (fs
->max_char_or_byte2
< byte2
) ||
189 (fs
->min_byte1
> byte1
) || (fs
->max_byte1
< byte1
))
197 return (fs
->per_char
+ (which
- fs
->min_char_or_byte2
));
201 i
= ((byte1
- fs
->min_byte1
) * pages
) +
202 (byte2
- fs
->min_char_or_byte2
);
203 return (fs
->per_char
+ i
);
207 return (&fs
->min_bounds
);
215 glXUseXFont(Font font
, int first
, int count
, int listbase
)
222 unsigned long valuemask
;
224 GLint swapbytes
, lsbfirst
, rowlength
;
225 GLint skiprows
, skippixels
, alignment
;
226 unsigned int max_width
, max_height
, max_bm_width
, max_bm_height
;
230 dpy
= glXGetCurrentDisplay();
232 return; /* I guess glXMakeCurrent wasn't called */
233 i
= DefaultScreen(dpy
);
234 win
= RootWindow(dpy
, i
);
236 fs
= XQueryFont(dpy
, font
);
238 _mesa_error(NULL
, GL_INVALID_VALUE
,
239 "Couldn't get font structure information");
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
;
249 bm
= (GLubyte
*) MALLOC((max_bm_width
* max_bm_height
) * sizeof(GLubyte
));
251 XFreeFontInfo(NULL
, fs
, 1);
252 _mesa_error(NULL
, GL_OUT_OF_MEMORY
,
253 "Couldn't allocate bitmap in glXUseXFont()");
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
;
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
);
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);
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
);
294 dump_font_struct(fs
);
297 for (i
= 0; i
< count
; i
++) {
298 unsigned int width
, height
, bm_width
, bm_height
;
299 GLfloat x0
, y0
, dx
, dy
;
302 unsigned int c
= first
+ i
;
303 int list
= listbase
+ i
;
306 /* check on index validity and get the bounds */
309 ch
= &fs
->max_bounds
;
319 sprintf(s
, isprint(c
) ? "%c> " : "\\%03o> ", c
);
320 dump_char_struct(ch
, s
);
324 /* glBitmap()' parameters:
325 straight from the glXUseXFont(3) manpage. */
326 width
= ch
->rbearing
- ch
->lbearing
;
327 height
= ch
->ascent
+ ch
->descent
;
329 y0
= ch
->descent
- 0; /* XXX used to subtract 1 here */
330 /* but that caused a conformace failure */
334 /* X11's starting point. */
338 /* Round the width to a multiple of eight. We will use this also
339 for the pixmap for capturing the X11 font. This is slightly
340 inefficient, but it makes the OpenGL part real easy. */
341 bm_width
= (width
+ 7) / 8;
344 glNewList(list
, GL_COMPILE
);
345 if (valid
&& (bm_width
> 0) && (bm_height
> 0)) {
347 MEMSET(bm
, '\0', bm_width
* bm_height
);
348 fill_bitmap(dpy
, win
, gc
, bm_width
, bm_height
, x
, y
, c
, bm
);
350 glBitmap(width
, height
, x0
, y0
, dx
, dy
, bm
);
353 printf("width/height = %u/%u\n", width
, height
);
354 printf("bm_width/bm_height = %u/%u\n", bm_width
, bm_height
);
355 dump_bitmap(bm_width
, bm_height
, bm
);
360 glBitmap(0, 0, 0.0, 0.0, dx
, dy
, NULL
);
366 XFreeFontInfo(NULL
, fs
, 1);
369 /* Restore saved packing modes. */
370 glPixelStorei(GL_UNPACK_SWAP_BYTES
, swapbytes
);
371 glPixelStorei(GL_UNPACK_LSB_FIRST
, lsbfirst
);
372 glPixelStorei(GL_UNPACK_ROW_LENGTH
, rowlength
);
373 glPixelStorei(GL_UNPACK_SKIP_ROWS
, skiprows
);
374 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, skippixels
);
375 glPixelStorei(GL_UNPACK_ALIGNMENT
, alignment
);