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)
66 Drw *drw = ecalloc(1, sizeof(Drw));
73 drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
74 drw->gc = XCreateGC(dpy, root, 0, NULL);
75 XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
81 drw_resize(Drw *drw, unsigned int w, unsigned int h)
89 XFreePixmap(drw->dpy, drw->drawable);
90 drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
96 XFreePixmap(drw->dpy, drw->drawable);
97 XFreeGC(drw->dpy, drw->gc);
98 drw_fontset_free(drw->fonts);
102 /* This function is an implementation detail. Library users should use
103 * drw_fontset_create instead.
106 xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
109 XftFont *xfont = NULL;
110 FcPattern *pattern = NULL;
113 /* Using the pattern found at font->xfont->pattern does not yield the
114 * same substitution results as using the pattern returned by
115 * FcNameParse; using the latter results in the desired fallback
116 * behaviour whereas the former just results in missing-character
117 * rectangles being drawn, at least with some fonts. */
118 if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
119 fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname);
122 if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
123 fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname);
124 XftFontClose(drw->dpy, xfont);
127 } else if (fontpattern) {
128 if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
129 fprintf(stderr, "error, cannot load font from pattern.\n");
133 die("no font specified.");
136 /* Do not allow using color fonts. This is a workaround for a BadLength
137 * error from Xft with color glyphs. Modelled on the Xterm workaround. See
138 * https://bugzilla.redhat.com/show_bug.cgi?id=1498269
139 * https://lists.suckless.org/dev/1701/30932.html
140 * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=916349
141 * and lots more all over the internet.
144 if(FcPatternGetBool(xfont->pattern, FC_COLOR, 0, &iscol) == FcResultMatch && iscol) {
145 XftFontClose(drw->dpy, xfont);
149 font = ecalloc(1, sizeof(Fnt));
151 font->pattern = pattern;
152 font->h = xfont->ascent + xfont->descent;
153 font->dpy = drw->dpy;
159 xfont_free(Fnt *font)
164 FcPatternDestroy(font->pattern);
165 XftFontClose(font->dpy, font->xfont);
170 drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
172 Fnt *cur, *ret = NULL;
178 for (i = 1; i <= fontcount; i++) {
179 if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) {
184 return (drw->fonts = ret);
188 drw_fontset_free(Fnt *font)
191 drw_fontset_free(font->next);
197 drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
199 if (!drw || !dest || !clrname)
202 if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
203 DefaultColormap(drw->dpy, drw->screen),
205 die("error, cannot allocate color '%s'", clrname);
208 /* Wrapper to create color schemes. The caller has to call free(3) on the
209 * returned color scheme when done using it. */
211 drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
216 /* need at least two colors for a scheme */
217 if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
220 for (i = 0; i < clrcount; i++)
221 drw_clr_create(drw, &ret[i], clrnames[i]);
226 drw_setfontset(Drw *drw, Fnt *set)
233 drw_setscheme(Drw *drw, Clr *scm)
240 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
242 if (!drw || !drw->scheme)
244 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
246 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
248 XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
252 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
254 int i, ty, ellipsis_x = 0;
255 unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len;
257 Fnt *usedfont, *curfont, *nextfont;
258 int utf8strlen, utf8charlen, render = x || y || w || h;
259 long utf8codepoint = 0;
261 FcCharSet *fccharset;
262 FcPattern *fcpattern;
265 int charexists = 0, overflow = 0;
266 /* keep track of a couple codepoints for which we have no match. */
267 enum { nomatches_len = 64 };
268 static struct { long codepoint[nomatches_len]; unsigned int idx; } nomatches;
269 static unsigned int ellipsis_width = 0;
271 if (!drw || (render && (!drw->scheme || !w)) || !text || !drw->fonts)
275 w = invert ? invert : ~invert;
277 XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
278 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
279 d = XftDrawCreate(drw->dpy, drw->drawable,
280 DefaultVisual(drw->dpy, drw->screen),
281 DefaultColormap(drw->dpy, drw->screen));
286 usedfont = drw->fonts;
287 if (!ellipsis_width && render)
288 ellipsis_width = drw_fontset_getwidth(drw, "...");
290 ew = ellipsis_len = utf8strlen = 0;
294 utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
295 for (curfont = drw->fonts; curfont; curfont = curfont->next) {
296 charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
298 drw_font_getexts(curfont, text, utf8charlen, &tmpw, NULL);
299 if (ew + ellipsis_width <= w) {
300 /* keep track where the ellipsis still fits */
303 ellipsis_len = utf8strlen;
308 /* called from drw_fontset_getwidth_clamp():
309 * it wants the width AFTER the overflow
314 utf8strlen = ellipsis_len;
315 } else if (curfont == usedfont) {
316 utf8strlen += utf8charlen;
326 if (overflow || !charexists || nextfont)
334 ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
335 XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
336 usedfont->xfont, x, ty, (XftChar8 *)utf8str, utf8strlen);
341 if (render && overflow)
342 drw_text(drw, ellipsis_x, y, ellipsis_w, h, 0, "...", invert);
344 if (!*text || overflow) {
346 } else if (nextfont) {
350 /* Regardless of whether or not a fallback font is found, the
351 * character must be drawn. */
354 for (i = 0; i < nomatches_len; ++i) {
355 /* avoid calling XftFontMatch if we know we won't find a match */
356 if (utf8codepoint == nomatches.codepoint[i])
360 fccharset = FcCharSetCreate();
361 FcCharSetAddChar(fccharset, utf8codepoint);
363 if (!drw->fonts->pattern) {
364 /* Refer to the comment in xfont_create for more information. */
365 die("the first font in the cache must be loaded from a font string.");
368 fcpattern = FcPatternDuplicate(drw->fonts->pattern);
369 FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
370 FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
371 FcPatternAddBool(fcpattern, FC_COLOR, FcFalse);
373 FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
374 FcDefaultSubstitute(fcpattern);
375 match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
377 FcCharSetDestroy(fccharset);
378 FcPatternDestroy(fcpattern);
381 usedfont = xfont_create(drw, NULL, match);
382 if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
383 for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
385 curfont->next = usedfont;
387 xfont_free(usedfont);
388 nomatches.codepoint[++nomatches.idx % nomatches_len] = utf8codepoint;
390 usedfont = drw->fonts;
398 return x + (render ? w : 0);
402 drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
407 XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
408 XSync(drw->dpy, False);
412 drw_fontset_getwidth(Drw *drw, const char *text)
414 if (!drw || !drw->fonts || !text)
416 return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
420 drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n)
422 unsigned int tmp = 0;
423 if (drw && drw->fonts && text && n)
424 tmp = drw_text(drw, 0, 0, 0, 0, 0, text, n);
429 drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
436 XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
444 drw_cur_create(Drw *drw, int shape)
448 if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
451 cur->cursor = XCreateFontCursor(drw->dpy, shape);
457 drw_cur_free(Drw *drw, Cur *cursor)
462 XFreeCursor(drw->dpy, cursor->cursor);