2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 /* \file xuserotfont.c
28 * A function like glXUseXFont() but takes a 0, 90, 180 or 270 degree
29 * rotation angle for rotated text display.
31 * Based on Mesa's glXUseXFont implementation written by Thorsten Ohl.
38 #include "xuserotfont.h"
42 * Generate OpenGL-compatible bitmap by drawing an X character glyph
43 * to an off-screen pixmap, then getting the image and testing pixels.
44 * \param width bitmap width in pixels
45 * \param height bitmap height in pixels
48 fill_bitmap(Display
*dpy
, Pixmap pixmap
, GC gc
,
49 unsigned int bitmapWidth
, unsigned int bitmapHeight
,
50 unsigned int charWidth
, unsigned int charHeight
,
51 int xPos
, int yPos
, unsigned int c
, GLubyte
* bitmap
,
54 const int bytesPerRow
= (bitmapWidth
+ 7) / 8;
58 /* clear pixmap to 0 */
59 XSetForeground(dpy
, gc
, 0);
60 XFillRectangle(dpy
, pixmap
, gc
, 0, 0, charWidth
, charHeight
);
62 /* The glyph is drawn snug up against the left/top edges of the pixmap */
63 XSetForeground(dpy
, gc
, 1);
64 char2b
.byte1
= (c
>> 8) & 0xff;
65 char2b
.byte2
= (c
& 0xff);
66 XDrawString16(dpy
, pixmap
, gc
, xPos
, yPos
, &char2b
, 1);
68 /* initialize GL bitmap */
69 memset(bitmap
, 0, bytesPerRow
* bitmapHeight
);
71 image
= XGetImage(dpy
, pixmap
, 0, 0, charWidth
, charHeight
, 1, XYPixmap
);
73 /* Set appropriate bits in the GL bitmap.
74 * Note: X11 and OpenGL are upside down wrt each other).
78 for (y
= 0; y
< charHeight
; y
++) {
79 for (x
= 0; x
< charWidth
; x
++) {
80 if (XGetPixel(image
, x
, y
)) {
81 int y2
= bitmapHeight
- y
- 1;
82 bitmap
[bytesPerRow
* y2
+ x
/ 8] |= (1 << (7 - (x
% 8)));
87 else if (rotation
== 90) {
88 for (y
= 0; y
< charHeight
; y
++) {
89 for (x
= 0; x
< charWidth
; x
++) {
90 if (XGetPixel(image
, x
, y
)) {
93 bitmap
[bytesPerRow
* y2
+ x2
/ 8] |= (1 << (7 - (x2
% 8)));
98 else if (rotation
== 180) {
99 for (y
= 0; y
< charHeight
; y
++) {
100 for (x
= 0; x
< charWidth
; x
++) {
101 if (XGetPixel(image
, x
, y
)) {
102 int x2
= charWidth
- x
- 1;
103 bitmap
[bytesPerRow
* y
+ x2
/ 8] |= (1 << (7 - (x2
% 8)));
109 assert(rotation
== 270);
110 for (y
= 0; y
< charHeight
; y
++) {
111 for (x
= 0; x
< charWidth
; x
++) {
112 if (XGetPixel(image
, x
, y
)) {
113 int x2
= charHeight
- y
- 1;
114 int y2
= charWidth
- x
- 1;
115 bitmap
[bytesPerRow
* y2
+ x2
/ 8] |= (1 << (7 - (x2
% 8)));
120 XDestroyImage(image
);
126 * Determine if a given glyph is valid and return the
127 * corresponding XCharStruct.
129 static const XCharStruct
*
130 isvalid(const XFontStruct
* fs
, unsigned int which
)
132 unsigned int rows
, pages
;
133 unsigned int byte1
= 0, byte2
= 0;
136 rows
= fs
->max_byte1
- fs
->min_byte1
+ 1;
137 pages
= fs
->max_char_or_byte2
- fs
->min_char_or_byte2
+ 1;
141 if ((fs
->min_char_or_byte2
> which
) || (fs
->max_char_or_byte2
< which
))
146 byte2
= which
& 0xff;
148 if ((fs
->min_char_or_byte2
> byte2
) ||
149 (fs
->max_char_or_byte2
< byte2
) ||
150 (fs
->min_byte1
> byte1
) || (fs
->max_byte1
< byte1
))
158 return fs
->per_char
+ (which
- fs
->min_char_or_byte2
);
162 i
= ((byte1
- fs
->min_byte1
) * pages
) +
163 (byte2
- fs
->min_char_or_byte2
);
164 return fs
->per_char
+ i
;
168 return &fs
->min_bounds
;
176 glXUseRotatedXFontMESA(Font font
, int first
, int count
, int listbase
,
184 GLint swapbytes
, lsbfirst
, rowlength
;
185 GLint skiprows
, skippixels
, alignment
;
186 unsigned int maxCharWidth
, maxCharHeight
;
196 dpy
= glXGetCurrentDisplay();
198 return; /* I guess glXMakeCurrent wasn't called */
199 win
= RootWindow(dpy
, DefaultScreen(dpy
));
201 fs
= XQueryFont(dpy
, font
);
204 _mesa_error(NULL, GL_INVALID_VALUE,
205 "Couldn't get font structure information");
210 /* Allocate a GL bitmap that can fit any character */
211 maxCharWidth
= fs
->max_bounds
.rbearing
- fs
->min_bounds
.lbearing
;
212 maxCharHeight
= fs
->max_bounds
.ascent
+ fs
->max_bounds
.descent
;
213 /* use max, in case we're rotating */
214 if (rotation
== 90 || rotation
== 270) {
215 /* swap width/height */
216 bm
= (GLubyte
*) malloc((maxCharHeight
+ 7) / 8 * maxCharWidth
);
219 /* normal or upside down */
220 bm
= (GLubyte
*) malloc((maxCharWidth
+ 7) / 8 * maxCharHeight
);
223 XFreeFontInfo(NULL
, fs
, 1);
225 _mesa_error(NULL, GL_OUT_OF_MEMORY,
226 "Couldn't allocate bitmap in glXUseXFont()");
232 /* get the page info */
233 pages
= fs
->max_char_or_byte2
- fs
->min_char_or_byte2
+ 1;
234 firstchar
= (fs
->min_byte1
<< 8) + fs
->min_char_or_byte2
;
235 lastchar
= (fs
->max_byte1
<< 8) + fs
->max_char_or_byte2
;
236 rows
= fs
->max_byte1
- fs
->min_byte1
+ 1;
237 unsigned int first_char
, last_char
, pages
, rows
;
240 /* Save the current packing mode for bitmaps. */
241 glGetIntegerv(GL_UNPACK_SWAP_BYTES
, &swapbytes
);
242 glGetIntegerv(GL_UNPACK_LSB_FIRST
, &lsbfirst
);
243 glGetIntegerv(GL_UNPACK_ROW_LENGTH
, &rowlength
);
244 glGetIntegerv(GL_UNPACK_SKIP_ROWS
, &skiprows
);
245 glGetIntegerv(GL_UNPACK_SKIP_PIXELS
, &skippixels
);
246 glGetIntegerv(GL_UNPACK_ALIGNMENT
, &alignment
);
248 /* Enforce a standard packing mode which is compatible with
249 fill_bitmap() from above. This is actually the default mode,
250 except for the (non)alignment. */
251 glPixelStorei(GL_UNPACK_SWAP_BYTES
, GL_FALSE
);
252 glPixelStorei(GL_UNPACK_LSB_FIRST
, GL_FALSE
);
253 glPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
254 glPixelStorei(GL_UNPACK_SKIP_ROWS
, 0);
255 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, 0);
256 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
258 /* Create pixmap and GC */
259 pixmap
= XCreatePixmap(dpy
, win
, maxCharWidth
, maxCharHeight
, 1);
262 unsigned long valuemask
;
263 values
.foreground
= BlackPixel(dpy
, DefaultScreen(dpy
));
264 values
.background
= WhitePixel(dpy
, DefaultScreen(dpy
));
265 values
.font
= fs
->fid
;
266 valuemask
= GCForeground
| GCBackground
| GCFont
;
267 gc
= XCreateGC(dpy
, pixmap
, valuemask
, &values
);
272 dump_font_struct(fs
);
275 for (i
= 0; i
< count
; i
++) {
276 const unsigned int c
= first
+ i
;
277 const int list
= listbase
+ i
;
278 unsigned int charWidth
, charHeight
;
279 unsigned int bitmapWidth
= 0, bitmapHeight
= 0;
280 GLfloat xOrig
, yOrig
, xStep
, yStep
, dtemp
;
281 const XCharStruct
*ch
;
285 /* check on index validity and get the bounds */
288 ch
= &fs
->max_bounds
;
298 sprintf(s
, isprint(c
) ? "%c> " : "\\%03o> ", c
);
299 dump_char_struct(ch
, s
);
303 /* glBitmap()' parameters:
304 straight from the glXUseXFont(3) manpage. */
305 charWidth
= ch
->rbearing
- ch
->lbearing
;
306 charHeight
= ch
->ascent
+ ch
->descent
;
307 xOrig
= -ch
->lbearing
;
312 /* X11's starting point. */
313 xPos
= -ch
->lbearing
;
320 bitmapWidth
= charWidth
;
321 bitmapHeight
= charHeight
;
330 xOrig
= charHeight
- (charHeight
- yPos
);
332 bitmapWidth
= charHeight
;
333 bitmapHeight
= charWidth
;
340 xOrig
= charWidth
- xOrig
- 1;
341 yOrig
= charHeight
- yOrig
- 1;
342 bitmapWidth
= charWidth
;
343 bitmapHeight
= charHeight
;
352 yOrig
= charWidth
- xOrig
;
355 bitmapWidth
= charHeight
;
356 bitmapHeight
= charWidth
;
359 /* should never get here */
363 glNewList(list
, GL_COMPILE
);
364 if (valid
&& bitmapWidth
> 0 && bitmapHeight
> 0) {
366 fill_bitmap(dpy
, pixmap
, gc
, bitmapWidth
, bitmapHeight
,
367 charWidth
, charHeight
,
368 xPos
, yPos
, c
, bm
, rotation
);
370 glBitmap(bitmapWidth
, bitmapHeight
, xOrig
, yOrig
, xStep
, yStep
, bm
);
374 printf("width/height = %u/%u\n", bitmapWidth
, bitmapHeight
);
375 dump_bitmap(bitmapWidth
, bitmapHeight
, bm
);
380 glBitmap(0, 0, 0.0, 0.0, xStep
, yStep
, NULL
);
386 XFreeFontInfo(NULL
, fs
, 1);
387 XFreePixmap(dpy
, pixmap
);
390 /* Restore saved packing modes. */
391 glPixelStorei(GL_UNPACK_SWAP_BYTES
, swapbytes
);
392 glPixelStorei(GL_UNPACK_LSB_FIRST
, lsbfirst
);
393 glPixelStorei(GL_UNPACK_ROW_LENGTH
, rowlength
);
394 glPixelStorei(GL_UNPACK_SKIP_ROWS
, skiprows
);
395 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, skippixels
);
396 glPixelStorei(GL_UNPACK_ALIGNMENT
, alignment
);