2 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
8 #include <X11/Xlocale.h>
13 textnw(const char *text, unsigned int len)
18 XmbTextExtents(dc.font.set, text, len, NULL, &r);
21 return XTextWidth(dc.font.xfont, text, len);
27 drawtext(const char *text, Bool invert)
33 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
35 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
36 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
43 if(len >= sizeof(buf))
44 len = sizeof(buf) - 1;
45 memcpy(buf, text, len);
48 h = dc.font.ascent + dc.font.descent;
49 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
52 /* shorten text if necessary */
53 while(len && (w = textnw(buf, len)) > dc.w - h)
57 return; /* too long */
59 gcv.foreground = invert ? dc.bg : dc.fg;
60 gcv.background = invert ? dc.fg : dc.bg;
62 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
63 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
67 gcv.font = dc.font.xfont->fid;
68 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
69 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
74 getcolor(const char *colstr)
76 Colormap cmap = DefaultColormap(dpy, screen);
79 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
84 setfont(const char *fontstr)
90 setlocale(LC_ALL, "");
92 XFreeFontSet(dpy, dc.font.set);
93 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
95 XFreeStringList(missing);
97 XFreeFontSet(dpy, dc.font.set);
102 XFontSetExtents *font_extents;
103 XFontStruct **xfonts;
106 dc.font.ascent = dc.font.descent = 0;
107 font_extents = XExtentsOfFontSet(dc.font.set);
108 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
109 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
110 if(dc.font.ascent < (*xfonts)->ascent)
111 dc.font.ascent = (*xfonts)->ascent;
112 if(dc.font.descent < (*xfonts)->descent)
113 dc.font.descent = (*xfonts)->descent;
119 XFreeFont(dpy, dc.font.xfont);
120 dc.font.xfont = NULL;
121 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
123 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
125 eprint("error, cannot init 'fixed' font\n");
126 dc.font.ascent = dc.font.xfont->ascent;
127 dc.font.descent = dc.font.xfont->descent;
129 dc.font.height = dc.font.ascent + dc.font.descent;
133 textw(const char *text)
135 return textnw(text, strlen(text)) + dc.font.height;