1 /* font-ximage.c --- renders text to an XImage for use with OpenGL.
2 * xscreensaver, Copyright (c) 2001, 2003 Jamie Zawinski <jwz@jwz.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
21 #include <X11/Xutil.h>
22 #include <GL/gl.h> /* only for GLfloat */
24 extern char *progname;
26 #include <X11/Xutil.h>
29 #define MAX(a,b) ((a)>(b)?(a):(b))
32 #define countof(x) (sizeof((x))/sizeof((*x)))
38 union { int i; char c[sizeof(int)]; } u;
44 /* return the next larger power of 2. */
48 static unsigned int pow2[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
49 2048, 4096, 8192, 16384, 32768, 65536 };
51 for (j = 0; j < countof(pow2); j++)
52 if (pow2[j] >= i) return pow2[j];
53 abort(); /* too big! */
57 /* Returns an XImage structure containing the string rendered in the font.
58 This XImage will be 32 bits per pixel, 8 each per R, G, and B, with the
59 extra byte set to 0xFF.
61 Foregroune and background are GL-style color specifiers: 4 floats from
65 text_to_ximage (Screen *screen, Visual *visual,
67 const char *text_lines,
71 Display *dpy = DisplayOfScreen (screen);
76 f = XLoadQueryFont(dpy, font);
79 f = XLoadQueryFont(dpy, "fixed");
81 fprintf (stderr, "%s: unable to load font \"%s\"; using \"fixed\".\n",
85 fprintf (stderr, "%s: unable to load fonts \"%s\" or \"fixed\"!\n",
91 /* Parse the text, and render it to `bitmap'
94 char *text, *text2, *line, *token;
105 text = strdup (text_lines);
107 (text[strlen(text)-1] == '\r' ||
108 text[strlen(text)-1] == '\n'))
109 text[strlen(text)-1] = 0;
111 text2 = strdup (text);
113 memset(&overall, 0, sizeof(overall));
116 while ((line = strtok (token, "\r\n")))
119 int ascent, descent, direction;
121 XTextExtents (f, line, strlen(line),
122 &direction, &ascent, &descent, &o2);
123 overall.lbearing = MAX(overall.lbearing, o2.lbearing);
124 overall.rbearing = MAX(overall.rbearing, o2.rbearing);
128 width = overall.lbearing + overall.rbearing + margin + margin + 1;
129 height = ((f->ascent + f->descent) * lines) + margin + margin;
131 /* GL texture sizes must be powers of two. */
133 int w2 = to_pow2(width);
134 int h2 = to_pow2(height);
135 xoff = (w2 - width) / 2;
136 yoff = (h2 - height) / 2;
141 bitmap = XCreatePixmap(dpy, RootWindowOfScreen (screen), width, height, 1);
145 gc = XCreateGC (dpy, bitmap, (GCFont | GCForeground), &gcv);
146 XFillRectangle(dpy, bitmap, gc, 0, 0, width, height);
147 XSetForeground(dpy, gc, fg);
151 while ((line = strtok(token, "\r\n")))
154 int ascent, descent, direction, xoff2;
157 XTextExtents(f, line, strlen(line),
158 &direction, &ascent, &descent, &o2);
160 ((overall.lbearing + overall.rbearing) -
161 (o2.lbearing + o2.rbearing)) / 2);
163 XDrawString(dpy, bitmap, gc,
164 overall.lbearing + margin + xoff,
165 ((f->ascent * (lines + 1)) +
166 (f->descent * lines) +
174 XUnloadFont(dpy, f->fid);
179 /* Convert the server-side Pixmap to a client-side GL-ordered XImage.
182 XImage *ximage1, *ximage2;
183 unsigned long fg, bg;
186 ximage1 = XGetImage (dpy, bitmap, 0, 0, width, height, ~0L, ZPixmap);
187 XFreePixmap(dpy, bitmap);
188 ximage2 = XCreateImage (dpy, visual, 32, ZPixmap, 0, 0,
189 width, height, 32, 0);
191 ximage2->data = (char *) malloc (height * ximage2->bytes_per_line);
193 /* Translate the 1-bit image to a deep image:
194 first figure out what the colors are.
197 int rpos, gpos, bpos, apos; /* bitfield positions */
199 /* Note that unlike X, which is endianness-agnostic (since any XImage
200 can have its own specific bit ordering, with the server reversing
201 things as necessary) OpenGL pretends everything is client-side, so
202 we need to pack things in the right order for the client machine.
205 /* #### Cherub says that the little-endian case must be taken on MacOSX,
206 or else the colors/alpha are the wrong way around. How can
210 rpos = 24, gpos = 16, bpos = 8, apos = 0;
213 rpos = 0, gpos = 8, bpos = 16, apos = 24;
215 fg = (((unsigned long) (texture_fg[0] * 255.0) << rpos) |
216 ((unsigned long) (texture_fg[1] * 255.0) << gpos) |
217 ((unsigned long) (texture_fg[2] * 255.0) << bpos) |
218 ((unsigned long) (texture_fg[3] * 255.0) << apos));
219 bg = (((unsigned long) (texture_bg[0] * 255.0) << rpos) |
220 ((unsigned long) (texture_bg[1] * 255.0) << gpos) |
221 ((unsigned long) (texture_bg[2] * 255.0) << bpos) |
222 ((unsigned long) (texture_bg[3] * 255.0) << apos));
225 for (y = 0; y < height; y++)
227 int y2 = (height-1-y); /* Texture maps are upside down. */
228 for (x = 0; x < width; x++)
229 XPutPixel (ximage2, x, y,
230 XGetPixel (ximage1, x, y2) ? fg : bg);
233 free (ximage1->data);
235 XDestroyImage (ximage1);
238 for (y = 0; y < height; y++)
240 int y2 = (height-1-y); /* Texture maps are upside down. */
241 for (x = 0; x < width; x++)
242 fputc ((XGetPixel (ximage2, x, y2) == fg ? '#' : ' '), stdout);
243 fputc ('\n', stdout);
245 fputc ('\n', stdout);