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, Bool border)
31 unsigned int len, olen;
34 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
36 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
37 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
43 points[1].x = dc.w - 1;
46 points[2].y = dc.h - 1;
47 points[3].x = -(dc.w - 1);
50 points[4].y = -(dc.h - 1);
51 XSetForeground(dpy, dc.gc, dc.border);
52 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
58 olen = len = strlen(text);
59 if(len >= sizeof(buf))
60 len = sizeof(buf) - 1;
61 memcpy(buf, text, len);
64 h = dc.font.ascent + dc.font.descent;
65 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
68 /* shorten text if necessary */
69 while(len && (w = textnw(buf, len)) > dc.w - h)
81 return; /* too long */
83 gcv.foreground = invert ? dc.bg : dc.fg;
84 gcv.background = invert ? dc.fg : dc.bg;
86 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
87 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
91 gcv.font = dc.font.xfont->fid;
92 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
93 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
98 getcolor(const char *colstr)
100 Colormap cmap = DefaultColormap(dpy, screen);
103 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
108 setfont(const char *fontstr)
110 char **missing, *def;
114 setlocale(LC_ALL, "");
116 XFreeFontSet(dpy, dc.font.set);
117 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
119 XFreeStringList(missing);
121 XFreeFontSet(dpy, dc.font.set);
126 XFontSetExtents *font_extents;
127 XFontStruct **xfonts;
130 dc.font.ascent = dc.font.descent = 0;
131 font_extents = XExtentsOfFontSet(dc.font.set);
132 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
133 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
134 if(dc.font.ascent < (*xfonts)->ascent)
135 dc.font.ascent = (*xfonts)->ascent;
136 if(dc.font.descent < (*xfonts)->descent)
137 dc.font.descent = (*xfonts)->descent;
143 XFreeFont(dpy, dc.font.xfont);
144 dc.font.xfont = NULL;
145 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
147 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
149 eprint("error, cannot init 'fixed' font\n");
150 dc.font.ascent = dc.font.xfont->ascent;
151 dc.font.descent = dc.font.xfont->descent;
153 dc.font.height = dc.font.ascent + dc.font.descent;
157 textw(const char *text)
159 return textnw(text, strlen(text)) + dc.font.height;