dwm

personal fork of dwm (rnpnr branch)
git clone anongit@rnpnr.xyz:dwm.git
Log | Files | Refs | Feed | README | LICENSE

drw.c (11222B)


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