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