Xinqi Bao's Git

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