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