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
23 #else /* !HAVE_COCOA */
24 # include <X11/Xlib.h>
25 # include <X11/Xutil.h>
26 # include <GL/gl.h> /* only for GLfloat */
27 #endif /* !HAVE_COCOA */
31 #endif /* HAVE_JWZGLES */
33 extern char *progname;
35 #include "font-ximage.h"
38 #define MAX(a,b) ((a)>(b)?(a):(b))
41 #define countof(x) (sizeof((x))/sizeof((*x)))
47 union { int i; char c[sizeof(int)]; } u;
53 /* return the next larger power of 2. */
57 static const unsigned int pow2[] = {
58 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
59 2048, 4096, 8192, 16384, 32768, 65536 };
61 for (j = 0; j < countof(pow2); j++)
62 if (pow2[j] >= i) return pow2[j];
63 abort(); /* too big! */
67 /* Returns an XImage structure containing the string rendered in the font.
68 This XImage will be 32 bits per pixel, 8 each per R, G, and B, with the
69 extra byte set to 0xFF.
71 Foregroune and background are GL-style color specifiers: 4 floats from
75 text_to_ximage (Screen *screen, Visual *visual,
77 const char *text_lines,
81 Display *dpy = DisplayOfScreen (screen);
86 f = XLoadQueryFont(dpy, font);
89 f = XLoadQueryFont(dpy, "fixed");
91 fprintf (stderr, "%s: unable to load font \"%s\"; using \"fixed\".\n",
95 fprintf (stderr, "%s: unable to load fonts \"%s\" or \"fixed\"!\n",
101 /* Parse the text, and render it to `bitmap'
104 char *text, *text2, *line, *token;
115 text = strdup (text_lines);
117 (text[strlen(text)-1] == '\r' ||
118 text[strlen(text)-1] == '\n'))
119 text[strlen(text)-1] = 0;
121 text2 = strdup (text);
123 memset(&overall, 0, sizeof(overall));
126 while ((line = strtok (token, "\r\n")))
129 int ascent, descent, direction;
131 XTextExtents (f, line, strlen(line),
132 &direction, &ascent, &descent, &o2);
133 overall.lbearing = MAX(overall.lbearing, o2.lbearing);
134 overall.rbearing = MAX(overall.rbearing, o2.rbearing);
138 width = overall.lbearing + overall.rbearing + margin + margin + 1;
139 height = ((f->ascent + f->descent) * lines) + margin + margin;
141 /* GL texture sizes must be powers of two. */
143 int w2 = to_pow2(width);
144 int h2 = to_pow2(height);
145 xoff = (w2 - width) / 2;
146 yoff = (h2 - height) / 2;
151 bitmap = XCreatePixmap(dpy, RootWindowOfScreen (screen), width, height, 1);
155 gc = XCreateGC (dpy, bitmap, (GCFont | GCForeground), &gcv);
156 XFillRectangle(dpy, bitmap, gc, 0, 0, width, height);
157 XSetForeground(dpy, gc, fg);
161 while ((line = strtok(token, "\r\n")))
164 int ascent, descent, direction;
167 XTextExtents(f, line, strlen(line),
168 &direction, &ascent, &descent, &o2);
169 XDrawString(dpy, bitmap, gc,
170 overall.lbearing + margin + xoff,
171 ((f->ascent * (lines + 1)) +
172 (f->descent * lines) +
180 XUnloadFont(dpy, f->fid);
185 /* Convert the server-side Pixmap to a client-side GL-ordered XImage.
188 XImage *ximage1, *ximage2;
189 unsigned long fg, bg;
192 ximage1 = XGetImage (dpy, bitmap, 0, 0, width, height, ~0L, ZPixmap);
193 XFreePixmap(dpy, bitmap);
194 ximage2 = XCreateImage (dpy, visual, 32, ZPixmap, 0, 0,
195 width, height, 32, 0);
197 ximage2->data = (char *) malloc (height * ximage2->bytes_per_line);
199 /* Translate the 1-bit image to a deep image:
200 first figure out what the colors are.
203 int rpos, gpos, bpos, apos; /* bitfield positions */
205 /* Note that unlike X, which is endianness-agnostic (since any XImage
206 can have its own specific bit ordering, with the server reversing
207 things as necessary) OpenGL pretends everything is client-side, so
208 we need to pack things in the right order for the client machine.
211 /* #### Cherub says that the little-endian case must be taken on MacOSX,
212 or else the colors/alpha are the wrong way around. How can
216 rpos = 24, gpos = 16, bpos = 8, apos = 0;
219 rpos = 0, gpos = 8, bpos = 16, apos = 24;
221 fg = (((unsigned long) (texture_fg[0] * 255.0) << rpos) |
222 ((unsigned long) (texture_fg[1] * 255.0) << gpos) |
223 ((unsigned long) (texture_fg[2] * 255.0) << bpos) |
224 ((unsigned long) (texture_fg[3] * 255.0) << apos));
225 bg = (((unsigned long) (texture_bg[0] * 255.0) << rpos) |
226 ((unsigned long) (texture_bg[1] * 255.0) << gpos) |
227 ((unsigned long) (texture_bg[2] * 255.0) << bpos) |
228 ((unsigned long) (texture_bg[3] * 255.0) << apos));
231 for (y = 0; y < height; y++)
233 int y2 = (height-1-y); /* Texture maps are upside down. */
234 for (x = 0; x < width; x++)
235 XPutPixel (ximage2, x, y,
236 XGetPixel (ximage1, x, y2) ? fg : bg);
239 XDestroyImage (ximage1);
242 for (y = 0; y < height; y++)
244 int y2 = (height-1-y); /* Texture maps are upside down. */
245 for (x = 0; x < width; x++)
246 fputc ((XGetPixel (ximage2, x, y2) == fg ? '#' : ' '), stdout);
247 fputc ('\n', stdout);
249 fputc ('\n', stdout);