Xinqi Bao's Git

patch: alpha
[dmenu.git] / drw.c
1 /* See LICENSE file for copyright and license details. */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <X11/Xlib.h>
6 #include <X11/Xft/Xft.h>
7
8 #include "drw.h"
9 #include "util.h"
10
11 #define UTF_INVALID 0xFFFD
12 #define UTF_SIZ 4
13
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};
18
19 static long
20 utf8decodebyte(const char c, size_t *i)
21 {
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];
25 return 0;
26 }
27
28 static size_t
29 utf8validate(long *u, size_t i)
30 {
31 if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
32 *u = UTF_INVALID;
33 for (i = 1; *u > utfmax[i]; ++i)
34 ;
35 return i;
36 }
37
38 static size_t
39 utf8decode(const char *c, long *u, size_t clen)
40 {
41 size_t i, j, len, type;
42 long udecoded;
43
44 *u = UTF_INVALID;
45 if (!clen)
46 return 0;
47 udecoded = utf8decodebyte(c[0], &len);
48 if (!BETWEEN(len, 1, UTF_SIZ))
49 return 1;
50 for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
51 udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
52 if (type)
53 return j;
54 }
55 if (j < len)
56 return 0;
57 *u = udecoded;
58 utf8validate(u, len);
59
60 return len;
61 }
62
63 Drw *
64 drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap)
65 {
66 Drw *drw = ecalloc(1, sizeof(Drw));
67
68 drw->dpy = dpy;
69 drw->screen = screen;
70 drw->root = root;
71 drw->w = w;
72 drw->h = h;
73 drw->visual = visual;
74 drw->depth = depth;
75 drw->cmap = cmap;
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);
79
80 return drw;
81 }
82
83 void
84 drw_resize(Drw *drw, unsigned int w, unsigned int h)
85 {
86 if (!drw)
87 return;
88
89 drw->w = w;
90 drw->h = h;
91 if (drw->drawable)
92 XFreePixmap(drw->dpy, drw->drawable);
93 drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, drw->depth);
94 }
95
96 void
97 drw_free(Drw *drw)
98 {
99 XFreePixmap(drw->dpy, drw->drawable);
100 XFreeGC(drw->dpy, drw->gc);
101 drw_fontset_free(drw->fonts);
102 free(drw);
103 }
104
105 /* This function is an implementation detail. Library users should use
106 * drw_fontset_create instead.
107 */
108 static Fnt *
109 xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
110 {
111 Fnt *font;
112 XftFont *xfont = NULL;
113 FcPattern *pattern = NULL;
114
115 if (fontname) {
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);
123 return NULL;
124 }
125 if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
126 fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname);
127 XftFontClose(drw->dpy, xfont);
128 return NULL;
129 }
130 } else if (fontpattern) {
131 if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
132 fprintf(stderr, "error, cannot load font from pattern.\n");
133 return NULL;
134 }
135 } else {
136 die("no font specified.");
137 }
138
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.
145 */
146 FcBool iscol;
147 if(FcPatternGetBool(xfont->pattern, FC_COLOR, 0, &iscol) == FcResultMatch && iscol) {
148 XftFontClose(drw->dpy, xfont);
149 return NULL;
150 }
151
152 font = ecalloc(1, sizeof(Fnt));
153 font->xfont = xfont;
154 font->pattern = pattern;
155 font->h = xfont->ascent + xfont->descent;
156 font->dpy = drw->dpy;
157
158 return font;
159 }
160
161 static void
162 xfont_free(Fnt *font)
163 {
164 if (!font)
165 return;
166 if (font->pattern)
167 FcPatternDestroy(font->pattern);
168 XftFontClose(font->dpy, font->xfont);
169 free(font);
170 }
171
172 Fnt*
173 drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
174 {
175 Fnt *cur, *ret = NULL;
176 size_t i;
177
178 if (!drw || !fonts)
179 return NULL;
180
181 for (i = 1; i <= fontcount; i++) {
182 if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) {
183 cur->next = ret;
184 ret = cur;
185 }
186 }
187 return (drw->fonts = ret);
188 }
189
190 void
191 drw_fontset_free(Fnt *font)
192 {
193 if (font) {
194 drw_fontset_free(font->next);
195 xfont_free(font);
196 }
197 }
198
199 void
200 drw_clr_create(Drw *drw, Clr *dest, const char *clrname, unsigned int alpha)
201 {
202 if (!drw || !dest || !clrname)
203 return;
204
205 if (!XftColorAllocName(drw->dpy, drw->visual, drw->cmap,
206 clrname, dest))
207 die("error, cannot allocate color '%s'", clrname);
208
209 dest->pixel = (dest->pixel & 0x00ffffffU) | (alpha << 24);
210 }
211
212 /* Wrapper to create color schemes. The caller has to call free(3) on the
213 * returned color scheme when done using it. */
214 Clr *
215 drw_scm_create(Drw *drw, const char *clrnames[], const unsigned int alphas[], size_t clrcount)
216 {
217 size_t i;
218 Clr *ret;
219
220 /* need at least two colors for a scheme */
221 if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
222 return NULL;
223
224 for (i = 0; i < clrcount; i++)
225 drw_clr_create(drw, &ret[i], clrnames[i], alphas[i]);
226 return ret;
227 }
228
229 void
230 drw_setfontset(Drw *drw, Fnt *set)
231 {
232 if (drw)
233 drw->fonts = set;
234 }
235
236 void
237 drw_setscheme(Drw *drw, Clr *scm)
238 {
239 if (drw)
240 drw->scheme = scm;
241 }
242
243 void
244 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
245 {
246 if (!drw || !drw->scheme)
247 return;
248 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
249 if (filled)
250 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
251 else
252 XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
253 }
254
255 int
256 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
257 {
258 int i, ty, ellipsis_x = 0;
259 unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len, ellipsis_width;
260 XftDraw *d = NULL;
261 Fnt *usedfont, *curfont, *nextfont;
262 int utf8strlen, utf8charlen, render = x || y || w || h;
263 long utf8codepoint = 0;
264 const char *utf8str;
265 FcCharSet *fccharset;
266 FcPattern *fcpattern;
267 FcPattern *match;
268 XftResult result;
269 int charexists = 0, overflow = 0;
270 /* keep track of a couple codepoints for which we have no match. */
271 enum { nomatches_len = 64 };
272 static struct { long codepoint[nomatches_len]; unsigned int idx; } nomatches;
273
274 if (!drw || (render && !drw->scheme) || !text || !drw->fonts)
275 return 0;
276
277 if (!render) {
278 w = invert ? invert : ~invert;
279 } else {
280 XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
281 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
282 d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap);
283 x += lpad;
284 w -= lpad;
285 }
286
287 usedfont = drw->fonts;
288 drw_font_getexts(usedfont, "...", 3, &ellipsis_width, NULL);
289 while (1) {
290 ew = ellipsis_len = utf8strlen = 0;
291 utf8str = text;
292 nextfont = NULL;
293 while (*text) {
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);
297 if (charexists) {
298 drw_font_getexts(curfont, text, utf8charlen, &tmpw, NULL);
299 if (ew + ellipsis_width <= w) {
300 /* keep track where the ellipsis still fits */
301 ellipsis_x = x + ew;
302 ellipsis_w = w - ew;
303 ellipsis_len = utf8strlen;
304 }
305
306 if (ew + tmpw > w) {
307 overflow = 1;
308 /* called from drw_fontset_getwidth_clamp():
309 * it wants the width AFTER the overflow
310 */
311 if (!render)
312 x += tmpw;
313 else
314 utf8strlen = ellipsis_len;
315 } else if (curfont == usedfont) {
316 utf8strlen += utf8charlen;
317 text += utf8charlen;
318 ew += tmpw;
319 } else {
320 nextfont = curfont;
321 }
322 break;
323 }
324 }
325
326 if (overflow || !charexists || nextfont)
327 break;
328 else
329 charexists = 0;
330 }
331
332 if (utf8strlen) {
333 if (render) {
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);
337 }
338 x += ew;
339 w -= ew;
340 }
341 if (render && overflow)
342 drw_text(drw, ellipsis_x, y, ellipsis_w, h, 0, "...", invert);
343
344 if (!*text || overflow) {
345 break;
346 } else if (nextfont) {
347 charexists = 0;
348 usedfont = nextfont;
349 } else {
350 /* Regardless of whether or not a fallback font is found, the
351 * character must be drawn. */
352 charexists = 1;
353
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])
357 goto no_match;
358 }
359
360 fccharset = FcCharSetCreate();
361 FcCharSetAddChar(fccharset, utf8codepoint);
362
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.");
366 }
367
368 fcpattern = FcPatternDuplicate(drw->fonts->pattern);
369 FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
370 FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
371 FcPatternAddBool(fcpattern, FC_COLOR, FcFalse);
372
373 FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
374 FcDefaultSubstitute(fcpattern);
375 match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
376
377 FcCharSetDestroy(fccharset);
378 FcPatternDestroy(fcpattern);
379
380 if (match) {
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)
384 ; /* NOP */
385 curfont->next = usedfont;
386 } else {
387 xfont_free(usedfont);
388 nomatches.codepoint[++nomatches.idx % nomatches_len] = utf8codepoint;
389 no_match:
390 usedfont = drw->fonts;
391 }
392 }
393 }
394 }
395 if (d)
396 XftDrawDestroy(d);
397
398 return x + (render ? w : 0);
399 }
400
401 void
402 drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
403 {
404 if (!drw)
405 return;
406
407 XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
408 XSync(drw->dpy, False);
409 }
410
411 unsigned int
412 drw_fontset_getwidth(Drw *drw, const char *text)
413 {
414 if (!drw || !drw->fonts || !text)
415 return 0;
416 return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
417 }
418
419 unsigned int
420 drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n)
421 {
422 unsigned int tmp = 0;
423 if (drw && drw->fonts && text && n)
424 tmp = drw_text(drw, 0, 0, 0, 0, 0, text, n);
425 return MIN(n, tmp);
426 }
427
428 void
429 drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
430 {
431 XGlyphInfo ext;
432
433 if (!font || !text)
434 return;
435
436 XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
437 if (w)
438 *w = ext.xOff;
439 if (h)
440 *h = font->h;
441 }
442
443 Cur *
444 drw_cur_create(Drw *drw, int shape)
445 {
446 Cur *cur;
447
448 if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
449 return NULL;
450
451 cur->cursor = XCreateFontCursor(drw->dpy, shape);
452
453 return cur;
454 }
455
456 void
457 drw_cur_free(Drw *drw, Cur *cursor)
458 {
459 if (!cursor)
460 return;
461
462 XFreeCursor(drw->dpy, cursor->cursor);
463 free(cursor);
464 }