Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/xfonts.c

raw
1 #include "sgcf.h"
2 #if defined(HAVE_X11)
3
4 /* $Id: xfonts.c,v 1.4 2000/12/31 16:32:58 mholst Exp $ */
5
6 /* xfonts.c -- glXUseXFont() for Mesa
7 Copyright (C) 1995 Thorsten.Ohl @ Physik.TH-Darmstadt.de
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING. If not, write to
21 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29 #include "GL/gl.h"
30 #include "GL/glx.h"
31 #include "GL/xmesa.h"
32 #include "macros.h"
33
34
35 /* Some debugging info. */
36
37 #ifdef DEBUG
38 #include <ctype.h>
39
40 int debug_xfonts = 0;
41
42 static void
43 dump_char_struct (XCharStruct *ch, char *prefix)
44 {
45 printf ("%slbearing = %d, rbearing = %d, width = %d\n",
46 prefix, ch->lbearing, ch->rbearing, ch->width);
47 printf ("%sascent = %d, descent = %d, attributes = %u\n",
48 prefix, ch->ascent, ch->descent, ch->attributes);
49 }
50
51 static void
52 dump_font_struct (XFontStruct *font)
53 {
54 printf ("ascent = %d, descent = %d\n", font->ascent, font->descent);
55 printf ("char_or_byte2 = (%d,%d)\n",
56 font->min_char_or_byte2, font->max_char_or_byte2);
57 printf ("byte1 = (%d,%d)\n", font->min_byte1, font->max_byte1);
58 printf ("all_chars_exist = %s\n", font->all_chars_exist ? "True" : "False");
59 printf ("default_char = %c (\\%03o)\n",
60 isprint (font->default_char) ? font->default_char : ' ',
61 font->default_char);
62 dump_char_struct (&font->min_bounds, "min> ");
63 dump_char_struct (&font->max_bounds, "max> ");
64 #if 0
65 for (c = font->min_char_or_byte2; c <= font->max_char_or_byte2; c++)
66 {
67 char prefix[8];
68 sprintf (prefix, "%d> ", c);
69 dump_char_struct (&font->per_char[c], prefix);
70 }
71 #endif
72 }
73
74 static void
75 dump_bitmap (unsigned int width, unsigned int height, GLubyte *bitmap)
76 {
77 int x, y;
78
79 printf (" ");
80 for (x = 0; x < 8*width; x++)
81 printf ("%o", 7 - (x % 8));
82 putchar ('\n');
83 for (y = 0; y < height; y++)
84 {
85 printf ("%3o:", y);
86 for (x = 0; x < 8*width; x++)
87 putchar ((bitmap[width*(height - y - 1) + x/8] & (1 << (7 - (x % 8))))
88 ? '*' : '.');
89 printf (" ");
90 for (x = 0; x < width; x++)
91 printf ("0x%02x, ", bitmap[width*(height - y - 1) + x]);
92 putchar ('\n');
93 }
94 }
95 #endif /* DEBUG */
96
97
98 /* Implementation. */
99
100 /* Fill a BITMAP with a character C from thew current font
101 in the graphics context GC. WIDTH is the width in bytes
102 and HEIGHT is the height in bits.
103
104 Note that the generated bitmaps must be used with
105
106 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
107 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
108 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
109 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
110 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
111 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
112
113 Possible optimizations:
114
115 * use only one reusable pixmap with the maximum dimensions.
116 * draw the entire font into a single pixmap (careful with
117 proportional fonts!).
118 */
119
120 static void
121 fill_bitmap (Display *dpy, Window win, GC gc,
122 unsigned int width, unsigned int height,
123 int x0, int y0, char c, GLubyte *bitmap)
124 {
125 XImage *image;
126 int x, y;
127 Pixmap pixmap;
128
129 pixmap = XCreatePixmap (dpy, win, 8*width, height, 1);
130 XSetForeground(dpy, gc, 0);
131 XFillRectangle (dpy, pixmap, gc, 0, 0, 8*width, height);
132 XSetForeground(dpy, gc, 1);
133 XDrawString (dpy, pixmap, gc, x0, y0, &c, 1);
134
135 image = XGetImage (dpy, pixmap, 0, 0, 8*width, height, 1, XYPixmap);
136
137 /* Fill the bitmap (X11 and OpenGL are upside down wrt each other). */
138 for (y = 0; y < height; y++)
139 for (x = 0; x < 8*width; x++)
140 if (XGetPixel (image, x, y))
141 bitmap[width*(height - y - 1) + x/8] |= (1 << (7 - (x % 8)));
142
143 XFreePixmap (dpy, pixmap);
144 XDestroyImage (image);
145 }
146
147 #include "xmesaP.h"
148 #include "context.h"
149
150 void
151 glXUseXFont (Font font, int first, int count, int listbase)
152 {
153 XMesaContext CC;
154 Display *dpy;
155 Window win;
156 Pixmap pixmap;
157 GC gc;
158 XGCValues values;
159 unsigned long valuemask;
160
161 XFontStruct *fs;
162
163 GLint swapbytes, lsbfirst, rowlength;
164 GLint skiprows, skippixels, alignment;
165
166 unsigned int max_width, max_height, max_bm_width, max_bm_height;
167 GLubyte *bm;
168
169 int i;
170
171 CC = XMesaGetCurrentContext();
172 dpy = CC->display;
173 win = CC->xm_buffer->frontbuffer;
174
175 fs = XQueryFont (dpy, font);
176 if (!fs)
177 {
178 gl_error (CC->gl_ctx, GL_INVALID_VALUE,
179 "Couldn't get font structure information");
180 return;
181 }
182
183 /* Allocate a bitmap that can fit all characters. */
184 max_width = fs->max_bounds.rbearing - fs->min_bounds.lbearing;
185 max_height = fs->max_bounds.ascent + fs->max_bounds.descent;
186 max_bm_width = (max_width + 7) / 8;
187 max_bm_height = max_height;
188
189 bm = (GLubyte *) malloc ((max_bm_width * max_bm_height) * sizeof (GLubyte));
190 if (!bm)
191 {
192 gl_error (CC->gl_ctx, GL_OUT_OF_MEMORY,
193 "Couldn't allocate bitmap in glXUseXFont()");
194 return;
195 }
196
197 /* Save the current packing mode for bitmaps. */
198 glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
199 glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
200 glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
201 glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
202 glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
203 glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
204
205 /* Enforce a standard packing mode which is compatible with
206 fill_bitmap() from above. This is actually the default mode,
207 except for the (non)alignment. */
208 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
209 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
210 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
211 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
212 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
213 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
214
215 pixmap = XCreatePixmap (dpy, win, 10, 10, 1);
216 values.foreground = BlackPixel (dpy, DefaultScreen (dpy));
217 values.background = WhitePixel (dpy, DefaultScreen (dpy));
218 values.font = fs->fid;
219 valuemask = GCForeground | GCBackground | GCFont;
220 gc = XCreateGC (dpy, pixmap, valuemask, &values);
221 XFreePixmap (dpy, pixmap);
222
223 #ifdef DEBUG
224 if (debug_xfonts)
225 dump_font_struct (fs);
226 #endif
227
228 for (i = 0; i < count; i++)
229 {
230 unsigned int width, height, bm_width, bm_height;
231 GLfloat x0, y0, dx, dy;
232 XCharStruct *ch;
233 int x, y;
234 int c = first + i;
235 int list = listbase + i;
236
237 if (fs->per_char
238 && (c >= fs->min_char_or_byte2) && (c <= fs->max_char_or_byte2))
239 ch = &fs->per_char[c-fs->min_char_or_byte2];
240 else
241 ch = &fs->max_bounds;
242
243 #ifdef DEBUG
244 if (debug_xfonts)
245 {
246 char s[7];
247 sprintf (s, isprint (c) ? "%c> " : "\\%03o> ", c);
248 dump_char_struct (ch, s);
249 }
250 #endif
251
252 /* glBitmap()' parameters:
253 straight from the glXUseXFont(3) manpage. */
254 width = ch->rbearing - ch->lbearing;
255 height = ch->ascent + ch->descent;
256 x0 = - ch->lbearing;
257 y0 = ch->descent - 1;
258 dx = ch->width;
259 dy = 0;
260
261 /* X11's starting point. */
262 x = - ch->lbearing;
263 y = ch->ascent;
264
265 /* Round the width to a multiple of eight. We will use this also
266 for the pixmap for capturing the X11 font. This is slightly
267 inefficient, but it makes the OpenGL part real easy. */
268 bm_width = (width + 7) / 8;
269 bm_height = height;
270
271 glNewList (list, GL_COMPILE);
272 if ((c >= fs->min_char_or_byte2) && (c <= fs->max_char_or_byte2)
273 && (bm_width > 0) && (bm_height > 0))
274 {
275 MEMSET (bm, '\0', bm_width * bm_height);
276 fill_bitmap (dpy, win, gc, bm_width, bm_height, x, y, c, bm);
277 glBitmap (width, height, x0, y0, dx, dy, bm);
278 #ifdef DEBUG
279 if (debug_xfonts)
280 {
281 printf ("width/height = %d/%d\n", width, height);
282 printf ("bm_width/bm_height = %d/%d\n", bm_width, bm_height);
283 dump_bitmap (bm_width, bm_height, bm);
284 }
285 #endif
286 }
287 else
288 glBitmap (0, 0, 0.0, 0.0, dx, dy, NULL);
289 glEndList ();
290 }
291
292 free (bm);
293 XFreeFontInfo( NULL, fs, 0 );
294 XFreeGC (dpy, gc);
295
296 /* Restore saved packing modes. */
297 glPixelStorei(GL_UNPACK_SWAP_BYTES, swapbytes);
298 glPixelStorei(GL_UNPACK_LSB_FIRST, lsbfirst);
299 glPixelStorei(GL_UNPACK_ROW_LENGTH, rowlength);
300 glPixelStorei(GL_UNPACK_SKIP_ROWS, skiprows);
301 glPixelStorei(GL_UNPACK_SKIP_PIXELS, skippixels);
302 glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
303 }
304
305 /* The End. */
306
307 #endif