sync updated drw code from dmenu
[dwm.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)
65 {
66 Drw *drw;
67
68 drw = ecalloc(1, sizeof(Drw));
69 drw->dpy = dpy;
70 drw->screen = screen;
71 drw->root = root;
72 drw->w = w;
73 drw->h = h;
74 drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
75 drw->gc = XCreateGC(dpy, root, 0, NULL);
76 drw->fontcount = 0;
77 XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
78
79 return drw;
80 }
81
82 void
83 drw_resize(Drw *drw, unsigned int w, unsigned int h)
84 {
85 drw->w = w;
86 drw->h = h;
87 if (drw->drawable)
88 XFreePixmap(drw->dpy, drw->drawable);
89 drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
90 }
91
92 void
93 drw_free(Drw *drw)
94 {
95 size_t i;
96
97 for (i = 0; i < drw->fontcount; i++)
98 drw_font_free(drw->fonts[i]);
99 XFreePixmap(drw->dpy, drw->drawable);
100 XFreeGC(drw->dpy, drw->gc);
101 free(drw);
102 }
103
104 /* This function is an implementation detail. Library users should use
105 * drw_font_create instead.
106 */
107 static Fnt *
108 drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern)
109 {
110 Fnt *font;
111 XftFont *xfont = NULL;
112 FcPattern *pattern = NULL;
113
114 if (fontname) {
115 /* Using the pattern found at font->xfont->pattern does not yield same
116 * the same substitution results as using the pattern returned by
117 * FcNameParse; using the latter results in the desired fallback
118 * behaviour whereas the former just results in
119 * missing-character-rectangles being drawn, at least with some fonts.
120 */
121 if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
122 fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
123 return NULL;
124 }
125 if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
126 fprintf(stderr, "error, cannot load font: '%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 pattern.\n");
133 return NULL;
134 }
135 } else {
136 die("no font specified.\n");
137 }
138
139 font = ecalloc(1, sizeof(Fnt));
140 font->xfont = xfont;
141 font->pattern = pattern;
142 font->ascent = xfont->ascent;
143 font->descent = xfont->descent;
144 font->h = font->ascent + font->descent;
145 font->dpy = drw->dpy;
146
147 return font;
148 }
149
150 Fnt*
151 drw_font_create(Drw *drw, const char *fontname)
152 {
153 return drw_font_xcreate(drw, fontname, NULL);
154 }
155
156 void
157 drw_load_fonts(Drw* drw, const char *fonts[], size_t fontcount)
158 {
159 size_t i;
160 Fnt *font;
161
162 for (i = 0; i < fontcount; i++) {
163 if (drw->fontcount >= DRW_FONT_CACHE_SIZE) {
164 die("font cache exhausted.\n");
165 } else if ((font = drw_font_xcreate(drw, fonts[i], NULL))) {
166 drw->fonts[drw->fontcount++] = font;
167 }
168 }
169 }
170
171 void
172 drw_font_free(Fnt *font)
173 {
174 if (!font)
175 return;
176 if (font->pattern)
177 FcPatternDestroy(font->pattern);
178 XftFontClose(font->dpy, font->xfont);
179 free(font);
180 }
181
182 Clr *
183 drw_clr_create(Drw *drw, const char *clrname)
184 {
185 Clr *clr;
186
187 clr = ecalloc(1, sizeof(Clr));
188 if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
189 DefaultColormap(drw->dpy, drw->screen),
190 clrname, &clr->rgb))
191 die("error, cannot allocate color '%s'\n", clrname);
192 clr->pix = clr->rgb.pixel;
193
194 return clr;
195 }
196
197 void
198 drw_clr_free(Clr *clr)
199 {
200 free(clr);
201 }
202
203 void
204 drw_setscheme(Drw *drw, ClrScheme *scheme)
205 {
206 drw->scheme = scheme;
207 }
208
209 void
210 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert)
211 {
212 if (!drw->scheme)
213 return;
214 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->pix : drw->scheme->fg->pix);
215 if (filled)
216 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w + 1, h + 1);
217 else if (empty)
218 XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
219 }
220
221 int
222 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert)
223 {
224 char buf[1024];
225 int tx, ty, th;
226 Extnts tex;
227 XftDraw *d = NULL;
228 Fnt *curfont, *nextfont;
229 size_t i, len;
230 int utf8strlen, utf8charlen, render;
231 long utf8codepoint = 0;
232 const char *utf8str;
233 FcCharSet *fccharset;
234 FcPattern *fcpattern;
235 FcPattern *match;
236 XftResult result;
237 int charexists = 0;
238
239 if (!drw->scheme || !drw->fontcount)
240 return 0;
241
242 if (!(render = x || y || w || h)) {
243 w = ~w;
244 } else {
245 XSetForeground(drw->dpy, drw->gc, invert ?
246 drw->scheme->fg->pix : drw->scheme->bg->pix);
247 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
248 d = XftDrawCreate(drw->dpy, drw->drawable,
249 DefaultVisual(drw->dpy, drw->screen),
250 DefaultColormap(drw->dpy, drw->screen));
251 }
252
253 curfont = drw->fonts[0];
254 while (1) {
255 utf8strlen = 0;
256 utf8str = text;
257 nextfont = NULL;
258 while (*text) {
259 utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
260 for (i = 0; i < drw->fontcount; i++) {
261 charexists = charexists || XftCharExists(drw->dpy, drw->fonts[i]->xfont, utf8codepoint);
262 if (charexists) {
263 if (drw->fonts[i] == curfont) {
264 utf8strlen += utf8charlen;
265 text += utf8charlen;
266 } else {
267 nextfont = drw->fonts[i];
268 }
269 break;
270 }
271 }
272
273 if (!charexists || (nextfont && nextfont != curfont))
274 break;
275 else
276 charexists = 0;
277 }
278
279 if (utf8strlen) {
280 drw_font_getexts(curfont, utf8str, utf8strlen, &tex);
281 /* shorten text if necessary */
282 for (len = MIN(utf8strlen, (sizeof buf) - 1); len && (tex.w > w - drw->fonts[0]->h || w < drw->fonts[0]->h); len--)
283 drw_font_getexts(curfont, utf8str, len, &tex);
284
285 if (len) {
286 memcpy(buf, utf8str, len);
287 buf[len] = '\0';
288 if (len < utf8strlen)
289 for (i = len; i && i > len - 3; buf[--i] = '.');
290
291 if (render) {
292 th = curfont->ascent + curfont->descent;
293 ty = y + (h / 2) - (th / 2) + curfont->ascent;
294 tx = x + (h / 2);
295 XftDrawStringUtf8(d, invert ? &drw->scheme->bg->rgb : &drw->scheme->fg->rgb, curfont->xfont, tx, ty, (XftChar8 *)buf, len);
296 }
297 x += tex.w;
298 w -= tex.w;
299 }
300 }
301
302 if (!*text) {
303 break;
304 } else if (nextfont) {
305 charexists = 0;
306 curfont = nextfont;
307 } else {
308 /* Regardless of whether or not a fallback font is found, the
309 * character must be drawn.
310 */
311 charexists = 1;
312
313 if (drw->fontcount >= DRW_FONT_CACHE_SIZE)
314 continue;
315
316 fccharset = FcCharSetCreate();
317 FcCharSetAddChar(fccharset, utf8codepoint);
318
319 if (!drw->fonts[0]->pattern) {
320 /* Refer to the comment in drw_font_xcreate for more
321 * information. */
322 die("the first font in the cache must be loaded from a font string.\n");
323 }
324
325 fcpattern = FcPatternDuplicate(drw->fonts[0]->pattern);
326 FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
327 FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
328
329 FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
330 FcDefaultSubstitute(fcpattern);
331 match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
332
333 FcCharSetDestroy(fccharset);
334 FcPatternDestroy(fcpattern);
335
336 if (match) {
337 curfont = drw_font_xcreate(drw, NULL, match);
338 if (curfont && XftCharExists(drw->dpy, curfont->xfont, utf8codepoint)) {
339 drw->fonts[drw->fontcount++] = curfont;
340 } else {
341 drw_font_free(curfont);
342 curfont = drw->fonts[0];
343 }
344 }
345 }
346 }
347 if (d)
348 XftDrawDestroy(d);
349
350 return x;
351 }
352
353 void
354 drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
355 {
356 XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
357 XSync(drw->dpy, False);
358 }
359
360 void
361 drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex)
362 {
363 XGlyphInfo ext;
364
365 XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
366 tex->h = font->h;
367 tex->w = ext.xOff;
368 }
369
370 unsigned int
371 drw_font_getexts_width(Fnt *font, const char *text, unsigned int len)
372 {
373 Extnts tex;
374
375 drw_font_getexts(font, text, len, &tex);
376
377 return tex.w;
378 }
379
380 Cur *
381 drw_cur_create(Drw *drw, int shape)
382 {
383 Cur *cur;
384
385 cur = ecalloc(1, sizeof(Cur));
386 cur->cursor = XCreateFontCursor(drw->dpy, shape);
387
388 return cur;
389 }
390
391 void
392 drw_cur_free(Drw *drw, Cur *cursor)
393 {
394 if (!cursor)
395 return;
396 XFreeCursor(drw->dpy, cursor->cursor);
397 free(cursor);
398 }