1 /* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
7 #include <X11/Xlocale.h>
12 textnw(const char *text, unsigned int len) {
16 XmbTextExtents(dc.font.set, text, len, NULL, &r);
19 return XTextWidth(dc.font.xfont, text, len);
25 drawtext(const char *text, unsigned long col[ColLast]) {
28 unsigned int len, olen;
30 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
32 XSetForeground(dpy, dc.gc, col[ColBG]);
33 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
37 olen = len = strlen(text);
40 memcpy(buf, text, len);
42 h = dc.font.ascent + dc.font.descent;
43 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
45 /* shorten text if necessary */
46 while(len && (w = textnw(buf, len)) > dc.w - h)
57 return; /* too long */
58 gcv.foreground = col[ColFG];
60 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
61 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc,
65 gcv.font = dc.font.xfont->fid;
66 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
67 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
72 getcolor(const char *colstr) {
73 Colormap cmap = DefaultColormap(dpy, screen);
76 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
77 eprint("error, cannot allocate color '%s'\n", colstr);
82 setfont(const char *fontstr) {
87 setlocale(LC_ALL, "");
89 XFreeFontSet(dpy, dc.font.set);
90 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
92 XFreeStringList(missing);
94 XFontSetExtents *font_extents;
97 dc.font.ascent = dc.font.descent = 0;
98 font_extents = XExtentsOfFontSet(dc.font.set);
99 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
100 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
101 if(dc.font.ascent < (*xfonts)->ascent)
102 dc.font.ascent = (*xfonts)->ascent;
103 if(dc.font.descent < (*xfonts)->descent)
104 dc.font.descent = (*xfonts)->descent;
110 XFreeFont(dpy, dc.font.xfont);
111 dc.font.xfont = NULL;
112 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
114 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
116 eprint("error, cannot init 'fixed' font\n");
117 dc.font.ascent = dc.font.xfont->ascent;
118 dc.font.descent = dc.font.xfont->descent;
120 dc.font.height = dc.font.ascent + dc.font.descent;
124 textw(const char *text) {
125 return textnw(text, strlen(text)) + dc.font.height;