1 /* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
11 textnw(const char *text, unsigned int len) {
15 XmbTextExtents(dc.font.set, text, len, NULL, &r);
18 return XTextWidth(dc.font.xfont, text, len);
24 drawtext(const char *text, unsigned long col[ColLast]) {
27 unsigned int len, olen;
29 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
31 XSetForeground(dpy, dc.gc, col[ColBG]);
32 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
36 olen = len = strlen(text);
39 memcpy(buf, text, len);
41 h = dc.font.ascent + dc.font.descent;
42 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
44 /* shorten text if necessary */
45 while(len && (w = textnw(buf, len)) > dc.w - h)
56 return; /* too long */
57 gcv.foreground = col[ColFG];
59 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
60 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc,
64 gcv.font = dc.font.xfont->fid;
65 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
66 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
71 getcolor(const char *colstr) {
72 Colormap cmap = DefaultColormap(dpy, screen);
75 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
76 eprint("error, cannot allocate color '%s'\n", colstr);
81 setfont(const char *fontstr) {
87 XFreeFontSet(dpy, dc.font.set);
88 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
90 XFreeStringList(missing);
92 XFontSetExtents *font_extents;
95 dc.font.ascent = dc.font.descent = 0;
96 font_extents = XExtentsOfFontSet(dc.font.set);
97 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
98 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
99 if(dc.font.ascent < (*xfonts)->ascent)
100 dc.font.ascent = (*xfonts)->ascent;
101 if(dc.font.descent < (*xfonts)->descent)
102 dc.font.descent = (*xfonts)->descent;
108 XFreeFont(dpy, dc.font.xfont);
109 dc.font.xfont = NULL;
110 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
111 eprint("error, cannot load font: '%s'\n", fontstr);
112 dc.font.ascent = dc.font.xfont->ascent;
113 dc.font.descent = dc.font.xfont->descent;
115 dc.font.height = dc.font.ascent + dc.font.descent;
119 textw(const char *text) {
120 return textnw(text, strlen(text)) + dc.font.height;