1 /* See LICENSE file for copyright and license details. */
6 #include <X11/Xft/Xft.h>
11 #define UTF_INVALID 0xFFFD
14 static const unsigned char utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
15 static const unsigned char utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
16 static const long utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
17 static const long utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
20 utf8decodebyte(const char c, size_t *i)
22 for (*i = 0; *i < (UTF_SIZ + 1); ++(*i))
23 if (((unsigned char)c & utfmask[*i]) == utfbyte[*i])
24 return (unsigned char)c & ~utfmask[*i];
29 utf8validate(long *u, size_t i)
31 if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
33 for (i = 1; *u > utfmax[i]; ++i)
39 utf8decode(const char *c, long *u, size_t clen)
41 size_t i, j, len, type;
47 udecoded = utf8decodebyte(c[0], &len);
48 if (!BETWEEN(len, 1, UTF_SIZ))
50 for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
51 udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
64 drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap)
66 Drw *drw = ecalloc(1, sizeof(Drw));
76 drw->drawable = XCreatePixmap(dpy, root, w, h, depth);
77 drw->gc = XCreateGC(dpy, drw->drawable, 0, NULL);
78 XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
84 drw_resize(Drw *drw, unsigned int w, unsigned int h)
92 XFreePixmap(drw->dpy, drw->drawable);
93 drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, drw->depth);
99 XFreePixmap(drw->dpy, drw->drawable);
100 XFreeGC(drw->dpy, drw->gc);
101 drw_fontset_free(drw->fonts);
105 /* This function is an implementation detail. Library users should use
106 * drw_fontset_create instead.
109 xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
112 XftFont *xfont = NULL;
113 FcPattern *pattern = NULL;
116 /* Using the pattern found at font->xfont->pattern does not yield the
117 * same substitution results as using the pattern returned by
118 * FcNameParse; using the latter results in the desired fallback
119 * behaviour whereas the former just results in missing-character
120 * rectangles being drawn, at least with some fonts. */
121 if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
122 fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname);
125 if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
126 fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname);
127 XftFontClose(drw->dpy, xfont);
130 } else if (fontpattern) {
131 if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
132 fprintf(stderr, "error, cannot load font from pattern.\n");
136 die("no font specified.");
139 /* Do not allow using color fonts. This is a workaround for a BadLength
140 * error from Xft with color glyphs. Modelled on the Xterm workaround. See
141 * https://bugzilla.redhat.com/show_bug.cgi?id=1498269
142 * https://lists.suckless.org/dev/1701/30932.html
143 * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=916349
144 * and lots more all over the internet.
147 if(FcPatternGetBool(xfont->pattern, FC_COLOR, 0, &iscol) == FcResultMatch && iscol) {
148 XftFontClose(drw->dpy, xfont);
152 font = ecalloc(1, sizeof(Fnt));
154 font->pattern = pattern;
155 font->h = xfont->ascent + xfont->descent;
156 font->dpy = drw->dpy;
162 xfont_free(Fnt *font)
167 FcPatternDestroy(font->pattern);
168 XftFontClose(font->dpy, font->xfont);
173 drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
175 Fnt *cur, *ret = NULL;
181 for (i = 1; i <= fontcount; i++) {
182 if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) {
187 return (drw->fonts = ret);
191 drw_fontset_free(Fnt *font)
194 drw_fontset_free(font->next);
200 drw_clr_create(Drw *drw, Clr *dest, const char *clrname, unsigned int alpha)
202 if (!drw || !dest || !clrname)
205 if (!XftColorAllocName(drw->dpy, drw->visual, drw->cmap,
207 die("error, cannot allocate color '%s'", clrname);
209 dest->pixel = (dest->pixel & 0x00ffffffU) | (alpha << 24);
212 /* Wrapper to create color schemes. The caller has to call free(3) on the
213 * returned color scheme when done using it. */
215 drw_scm_create(Drw *drw, const char *clrnames[], const unsigned int alphas[], size_t clrcount)
220 /* need at least two colors for a scheme */
221 if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
224 for (i = 0; i < clrcount; i++)
225 drw_clr_create(drw, &ret[i], clrnames[i], alphas[i]);
230 drw_setfontset(Drw *drw, Fnt *set)
237 drw_setscheme(Drw *drw, Clr *scm)
244 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
246 if (!drw || !drw->scheme)
248 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
250 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
252 XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
256 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
262 Fnt *usedfont, *curfont, *nextfont;
264 int utf8strlen, utf8charlen, render = x || y || w || h;
265 long utf8codepoint = 0;
267 FcCharSet *fccharset;
268 FcPattern *fcpattern;
273 if (!drw || (render && !drw->scheme) || !text || !drw->fonts)
279 XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
280 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
281 d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap);
286 usedfont = drw->fonts;
292 utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
293 for (curfont = drw->fonts; curfont; curfont = curfont->next) {
294 charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
296 if (curfont == usedfont) {
297 utf8strlen += utf8charlen;
306 if (!charexists || nextfont)
313 drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
314 /* shorten text if necessary */
315 for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--)
316 drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
319 memcpy(buf, utf8str, len);
321 if (len < utf8strlen)
322 for (i = len; i && i > len - 3; buf[--i] = '.')
326 ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
327 XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
328 usedfont->xfont, x, ty, (XftChar8 *)buf, len);
337 } else if (nextfont) {
341 /* Regardless of whether or not a fallback font is found, the
342 * character must be drawn. */
345 fccharset = FcCharSetCreate();
346 FcCharSetAddChar(fccharset, utf8codepoint);
348 if (!drw->fonts->pattern) {
349 /* Refer to the comment in xfont_create for more information. */
350 die("the first font in the cache must be loaded from a font string.");
353 fcpattern = FcPatternDuplicate(drw->fonts->pattern);
354 FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
355 FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
356 FcPatternAddBool(fcpattern, FC_COLOR, FcFalse);
358 FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
359 FcDefaultSubstitute(fcpattern);
360 match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
362 FcCharSetDestroy(fccharset);
363 FcPatternDestroy(fcpattern);
366 usedfont = xfont_create(drw, NULL, match);
367 if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
368 for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
370 curfont->next = usedfont;
372 xfont_free(usedfont);
373 usedfont = drw->fonts;
381 return x + (render ? w : 0);
385 drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
390 XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
391 XSync(drw->dpy, False);
395 drw_fontset_getwidth(Drw *drw, const char *text)
397 if (!drw || !drw->fonts || !text)
399 return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
403 drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
410 XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
418 drw_cur_create(Drw *drw, int shape)
422 if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
425 cur->cursor = XCreateFontCursor(drw->dpy, shape);
431 drw_cur_free(Drw *drw, Cur *cursor)
436 XFreeCursor(drw->dpy, cursor->cursor);