dwm

Kris's build of dwm
git clone git clone https://git.krisyotam.com/krisyotam/dwm.git
Log | Files | Refs | README | LICENSE

drw.c (20644B)


      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 "patches.h"
      9 #include "drw.h"
     10 #include "util.h"
     11 
     12 #if BIDI_PATCH
     13 #include <fribidi.h>
     14 
     15 static char fribidi_text[BUFSIZ] = "";
     16 
     17 static void
     18 apply_fribidi(const char *str)
     19 {
     20 	FriBidiStrIndex len = strlen(str);
     21 	FriBidiChar logical[BUFSIZ];
     22 	FriBidiChar visual[BUFSIZ];
     23 	FriBidiParType base = FRIBIDI_PAR_ON;
     24 	FriBidiCharSet charset;
     25 
     26 	fribidi_text[0] = 0;
     27 	if (len > 0) {
     28 		charset = fribidi_parse_charset("UTF-8");
     29 		len = fribidi_charset_to_unicode(charset, str, len, logical);
     30 		fribidi_log2vis(logical, len, &base, visual, NULL, NULL, NULL);
     31 		len = fribidi_unicode_to_charset(charset, visual, len, fribidi_text);
     32 	}
     33 }
     34 #endif
     35 
     36 #if !BAR_PANGO_PATCH
     37 #define UTF_INVALID 0xFFFD
     38 #endif // BAR_PANGO_PATCH
     39 
     40 #if BAR_POWERLINE_TAGS_PATCH || BAR_POWERLINE_STATUS_PATCH
     41 Clr transcheme[3];
     42 #endif // BAR_POWERLINE_TAGS_PATCH | BAR_POWERLINE_STATUS_PATCH
     43 
     44 #if !BAR_PANGO_PATCH
     45 static int
     46 utf8decode(const char *s_in, long *u, int *err)
     47 {
     48 	static const unsigned char lens[] = {
     49 		/* 0XXXX */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     50 		/* 10XXX */ 0, 0, 0, 0, 0, 0, 0, 0,  /* invalid */
     51 		/* 110XX */ 2, 2, 2, 2,
     52 		/* 1110X */ 3, 3,
     53 		/* 11110 */ 4,
     54 		/* 11111 */ 0,  /* invalid */
     55 	};
     56 	static const unsigned char leading_mask[] = { 0x7F, 0x1F, 0x0F, 0x07 };
     57 	static const unsigned int overlong[] = { 0x0, 0x80, 0x0800, 0x10000 };
     58 
     59 	const unsigned char *s = (const unsigned char *)s_in;
     60 	int len = lens[*s >> 3];
     61 	*u = UTF_INVALID;
     62 	*err = 1;
     63 	if (len == 0)
     64 		return 1;
     65 
     66 	long cp = s[0] & leading_mask[len - 1];
     67 	for (int i = 1; i < len; ++i) {
     68 		if (s[i] == '\0' || (s[i] & 0xC0) != 0x80)
     69 			return i;
     70 		cp = (cp << 6) | (s[i] & 0x3F);
     71 	}
     72 	/* out of range, surrogate, overlong encoding */
     73 	if (cp > 0x10FFFF || (cp >> 11) == 0x1B || cp < overlong[len - 1])
     74 		return len;
     75 
     76 	*err = 0;
     77 	*u = cp;
     78 	return len;
     79 }
     80 #endif // BAR_PANGO_PATCH
     81 
     82 Drw *
     83 #if BAR_ALPHA_PATCH
     84 drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap)
     85 #else
     86 drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h)
     87 #endif // BAR_ALPHA_PATCH
     88 {
     89 	Drw *drw = ecalloc(1, sizeof(Drw));
     90 
     91 	drw->dpy = dpy;
     92 	drw->screen = screen;
     93 	drw->root = root;
     94 	drw->w = w;
     95 	drw->h = h;
     96 
     97 	#if BAR_ALPHA_PATCH
     98 	drw->visual = visual;
     99 	drw->depth = depth;
    100 	drw->cmap = cmap;
    101 	drw->drawable = XCreatePixmap(dpy, root, w, h, depth);
    102 	#if BAR_WINICON_PATCH
    103 	drw->picture = XRenderCreatePicture(dpy, drw->drawable, XRenderFindVisualFormat(dpy, visual), 0, NULL);
    104 	#endif // BAR_WINICON_PATCH
    105 	drw->gc = XCreateGC(dpy, drw->drawable, 0, NULL);
    106 	#else
    107 	drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
    108 	#if BAR_WINICON_PATCH
    109 	drw->picture = XRenderCreatePicture(dpy, drw->drawable, XRenderFindVisualFormat(dpy, DefaultVisual(dpy, screen)), 0, NULL);
    110 	#endif // BAR_WINICON_PATCH
    111 	drw->gc = XCreateGC(dpy, root, 0, NULL);
    112 	#endif // BAR_ALPHA_PATCH
    113 	XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
    114 
    115 	return drw;
    116 }
    117 
    118 void
    119 drw_resize(Drw *drw, unsigned int w, unsigned int h)
    120 {
    121 	if (!drw)
    122 		return;
    123 
    124 	drw->w = w;
    125 	drw->h = h;
    126 	#if BAR_WINICON_PATCH
    127 	if (drw->picture)
    128 		XRenderFreePicture(drw->dpy, drw->picture);
    129 	#endif // BAR_WINICON_PATCH
    130 	if (drw->drawable)
    131 		XFreePixmap(drw->dpy, drw->drawable);
    132 	#if BAR_ALPHA_PATCH
    133 	drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, drw->depth);
    134 	#if BAR_WINICON_PATCH
    135 	drw->picture = XRenderCreatePicture(drw->dpy, drw->drawable, XRenderFindVisualFormat(drw->dpy, drw->visual), 0, NULL);
    136 	#endif // BAR_WINICON_PATCH
    137 	#else // !BAR_ALPHA_PATCH
    138 	drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
    139 	#if BAR_WINICON_PATCH
    140 	drw->picture = XRenderCreatePicture(drw->dpy, drw->drawable, XRenderFindVisualFormat(drw->dpy, DefaultVisual(drw->dpy, drw->screen)), 0, NULL);
    141 	#endif // BAR_WINICON_PATCH
    142 	#endif // BAR_ALPHA_PATCH
    143 }
    144 
    145 void
    146 drw_free(Drw *drw)
    147 {
    148 	#if BAR_WINICON_PATCH
    149 	XRenderFreePicture(drw->dpy, drw->picture);
    150 	#endif // BAR_WINICON_PATCH
    151 	XFreePixmap(drw->dpy, drw->drawable);
    152 	XFreeGC(drw->dpy, drw->gc);
    153 	drw_fontset_free(drw->fonts);
    154 	free(drw);
    155 }
    156 
    157 #if BAR_PANGO_PATCH
    158 /* This function is an implementation detail. Library users should use
    159  * drw_font_create instead.
    160  */
    161 static Fnt *
    162 xfont_create(Drw *drw, const char *fontname)
    163 {
    164 	Fnt *font;
    165 	PangoFontMap *fontmap;
    166 	PangoContext *context;
    167 	PangoFontDescription *desc;
    168 	PangoFontMetrics *metrics;
    169 
    170 	if (!fontname) {
    171 		die("no font specified.");
    172 	}
    173 
    174 	font = ecalloc(1, sizeof(Fnt));
    175 	font->dpy = drw->dpy;
    176 
    177 	fontmap = pango_xft_get_font_map(drw->dpy, drw->screen);
    178 	context = pango_font_map_create_context(fontmap);
    179 	desc = pango_font_description_from_string(fontname);
    180 	font->layout = pango_layout_new(context);
    181 	pango_layout_set_font_description(font->layout, desc);
    182 
    183 	metrics = pango_context_get_metrics(context, desc, pango_language_from_string ("en-us"));
    184 	font->h = pango_font_metrics_get_height(metrics) / PANGO_SCALE;
    185 
    186 	pango_font_metrics_unref(metrics);
    187 	g_object_unref(context);
    188 
    189 	return font;
    190 }
    191 #else
    192 /* This function is an implementation detail. Library users should use
    193  * drw_fontset_create instead.
    194  */
    195 static Fnt *
    196 xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
    197 {
    198 	Fnt *font;
    199 	XftFont *xfont = NULL;
    200 	FcPattern *pattern = NULL;
    201 
    202 	if (fontname) {
    203 		/* Using the pattern found at font->xfont->pattern does not yield the
    204 		 * same substitution results as using the pattern returned by
    205 		 * FcNameParse; using the latter results in the desired fallback
    206 		 * behaviour whereas the former just results in missing-character
    207 		 * rectangles being drawn, at least with some fonts. */
    208 		if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
    209 			fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname);
    210 			return NULL;
    211 		}
    212 		if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
    213 			fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname);
    214 			XftFontClose(drw->dpy, xfont);
    215 			return NULL;
    216 		}
    217 	} else if (fontpattern) {
    218 		if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
    219 			fprintf(stderr, "error, cannot load font from pattern.\n");
    220 			return NULL;
    221 		}
    222 	} else {
    223 		die("no font specified.");
    224 	}
    225 
    226 	#if BAR_NO_COLOR_EMOJI_PATCH
    227 	/* Do not allow using color fonts. This is a workaround for a BadLength
    228 	 * error from Xft with color glyphs. Modelled on the Xterm workaround. See
    229 	 * https://bugzilla.redhat.com/show_bug.cgi?id=1498269
    230 	 * https://lists.suckless.org/dev/1701/30932.html
    231 	 * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=916349
    232 	 * and lots more all over the internet.
    233 	 */
    234 	FcBool iscol;
    235 	if (FcPatternGetBool(xfont->pattern, FC_COLOR, 0, &iscol) == FcResultMatch && iscol) {
    236 		XftFontClose(drw->dpy, xfont);
    237 		return NULL;
    238 	}
    239 	#endif // BAR_NO_COLOR_EMOJI_PATCH
    240 
    241 	font = ecalloc(1, sizeof(Fnt));
    242 	font->xfont = xfont;
    243 	font->pattern = pattern;
    244 	font->h = xfont->ascent + xfont->descent;
    245 	font->dpy = drw->dpy;
    246 
    247 	return font;
    248 }
    249 #endif // BAR_PANGO_PATCH
    250 
    251 static void
    252 xfont_free(Fnt *font)
    253 {
    254 	if (!font)
    255 		return;
    256 	#if BAR_PANGO_PATCH
    257 	if (font->layout)
    258 		g_object_unref(font->layout);
    259 	#else
    260 	if (font->pattern)
    261 		FcPatternDestroy(font->pattern);
    262 	XftFontClose(font->dpy, font->xfont);
    263 	#endif // BAR_PANGO_PATCH
    264 	free(font);
    265 }
    266 
    267 #if BAR_PANGO_PATCH
    268 Fnt*
    269 drw_font_create(Drw* drw, const char font[])
    270 {
    271 	Fnt *fnt = NULL;
    272 
    273 	if (!drw || !font)
    274 		return NULL;
    275 
    276 	fnt = xfont_create(drw, font);
    277 
    278 	return (drw->fonts = fnt);
    279 }
    280 #else
    281 Fnt*
    282 drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
    283 {
    284 	Fnt *cur, *ret = NULL;
    285 	size_t i;
    286 
    287 	if (!drw || !fonts)
    288 		return NULL;
    289 
    290 	for (i = 1; i <= fontcount; i++) {
    291 		if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) {
    292 			cur->next = ret;
    293 			ret = cur;
    294 		}
    295 	}
    296 	return (drw->fonts = ret);
    297 }
    298 #endif // BAR_PANGO_PATCH
    299 
    300 void
    301 drw_fontset_free(Fnt *font)
    302 {
    303 	if (font) {
    304 		#if !BAR_PANGO_PATCH
    305 		drw_fontset_free(font->next);
    306 		#endif // BAR_PANGO_PATCH
    307 		xfont_free(font);
    308 	}
    309 }
    310 
    311 void
    312 drw_clr_create(
    313 	Drw *drw,
    314 	Clr *dest,
    315 	const char *clrname
    316 	#if BAR_ALPHA_PATCH
    317 	, unsigned int alpha
    318 	#endif // BAR_ALPHA_PATCH
    319 ) {
    320 	if (!drw || !dest || !clrname)
    321 		return;
    322 
    323 	#if BAR_ALPHA_PATCH
    324 	if (!XftColorAllocName(drw->dpy, drw->visual, drw->cmap,
    325 	                       clrname, dest))
    326 		#if DO_NOT_DIE_ON_COLOR_ALLOCATION_FAILURE_PATCH
    327 		fprintf(stderr, "warning, cannot allocate color '%s'", clrname);
    328 		#else
    329 		die("error, cannot allocate color '%s'", clrname);
    330 		#endif // DO_NOT_DIE_ON_COLOR_ALLOCATION_FAILURE_PATCH
    331 
    332 	dest->pixel = (dest->pixel & 0x00ffffffU) | (alpha << 24);
    333 	#else
    334 	if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
    335 	                       DefaultColormap(drw->dpy, drw->screen),
    336 	                       clrname, dest))
    337 		#if DO_NOT_DIE_ON_COLOR_ALLOCATION_FAILURE_PATCH
    338 		fprintf(stderr, "warning, cannot allocate color '%s'", clrname);
    339 		#else
    340 		die("error, cannot allocate color '%s'", clrname);
    341 		#endif // DO_NOT_DIE_ON_COLOR_ALLOCATION_FAILURE_PATCH
    342 
    343 	#if NO_TRANSPARENT_BORDERS_PATCH
    344 	dest->pixel |= 0xff << 24;
    345 	#endif // NO_TRANSPARENT_BORDERS_PATCH
    346 	#endif // BAR_ALPHA_PATCH
    347 }
    348 
    349 /* Wrapper to create color schemes. The caller has to call free(3) on the
    350  * returned color scheme when done using it. */
    351 Clr *
    352 drw_scm_create(
    353 	Drw *drw,
    354 	char *clrnames[],
    355 	#if BAR_ALPHA_PATCH
    356 	const unsigned int alphas[],
    357 	#endif // BAR_ALPHA_PATCH
    358 	size_t clrcount
    359 ) {
    360 	size_t i;
    361 	Clr *ret;
    362 
    363 	/* need at least two colors for a scheme */
    364 	if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
    365 		return NULL;
    366 
    367 	for (i = 0; i < clrcount; i++)
    368 		#if BAR_ALPHA_PATCH
    369 		drw_clr_create(drw, &ret[i], clrnames[i], alphas[i]);
    370 		#else
    371 		drw_clr_create(drw, &ret[i], clrnames[i]);
    372 		#endif // BAR_ALPHA_PATCH
    373 	return ret;
    374 }
    375 
    376 #if !BAR_PANGO_PATCH
    377 void
    378 drw_setfontset(Drw *drw, Fnt *set)
    379 {
    380 	if (drw)
    381 		drw->fonts = set;
    382 }
    383 #endif // BAR_PANGO_PATCH
    384 
    385 void
    386 drw_setscheme(Drw *drw, Clr *scm)
    387 {
    388 	if (drw)
    389 		drw->scheme = scm;
    390 }
    391 
    392 #if BAR_POWERLINE_TAGS_PATCH || BAR_POWERLINE_STATUS_PATCH
    393 void
    394 drw_settrans(Drw *drw, Clr *psc, Clr *nsc)
    395 {
    396 	if (drw) {
    397 		transcheme[0] = psc[ColBg]; transcheme[1] = nsc[ColBg]; transcheme[2] = psc[ColBorder];
    398 		drw->scheme = transcheme;
    399 	}
    400 }
    401 #endif // BAR_POWERLINE_TAGS_PATCH | BAR_POWERLINE_STATUS_PATCH
    402 
    403 void
    404 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
    405 {
    406 	if (!drw || !drw->scheme)
    407 		return;
    408 	XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
    409 	if (filled)
    410 		XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
    411 	else
    412 		XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
    413 }
    414 
    415 #if BIDI_PATCH
    416 int
    417 _drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert, Bool markup)
    418 #else
    419 int
    420 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert, Bool markup)
    421 #endif // BIDI_PATCH
    422 {
    423 #if BAR_PANGO_PATCH
    424 	char buf[1024];
    425 	int i, ty, th;
    426 	unsigned int ew, eh;
    427 	XftDraw *d = NULL;
    428 	size_t len;
    429 	int render = x || y || w || h;
    430 
    431 	if (!drw || (render && !drw->scheme) || !text || !drw->fonts)
    432 		return 0;
    433 
    434 	if (!render) {
    435 		w = ~w;
    436 	} else {
    437 		XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
    438 		XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
    439 		if (w < lpad)
    440 			return x + w;
    441 		#if BAR_ALPHA_PATCH
    442 		d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap);
    443 		#else
    444 		d = XftDrawCreate(drw->dpy, drw->drawable,
    445 		                  DefaultVisual(drw->dpy, drw->screen),
    446 		                  DefaultColormap(drw->dpy, drw->screen));
    447 		#endif // BAR_ALPHA_PATCH
    448 		x += lpad;
    449 		w -= lpad;
    450 	}
    451 
    452 	len = strlen(text);
    453 
    454 	if (len) {
    455 		drw_font_getexts(drw->fonts, text, len, &ew, &eh, markup);
    456 		th = eh;
    457 		/* shorten text if necessary */
    458 		for (len = MIN(len, sizeof(buf) - 1); len && ew > w; len--) {
    459 			drw_font_getexts(drw->fonts, text, len, &ew, &eh, markup);
    460 			if (eh > th)
    461 				th = eh;
    462 		}
    463 
    464 		if (len) {
    465 			memcpy(buf, text, len);
    466 			buf[len] = '\0';
    467 			if (len < strlen(text))
    468 				for (i = len; i && i > len - 3; buf[--i] = '.')
    469 					; /* NOP */
    470 
    471 			if (render) {
    472 				ty = y + (h - th) / 2;
    473 				if (markup)
    474 					pango_layout_set_markup(drw->fonts->layout, buf, len);
    475 				else
    476 					pango_layout_set_text(drw->fonts->layout, buf, len);
    477 				pango_xft_render_layout(d, &drw->scheme[invert ? ColBg : ColFg],
    478 					drw->fonts->layout, x * PANGO_SCALE, ty * PANGO_SCALE);
    479 				if (markup) /* clear markup attributes */
    480 					pango_layout_set_attributes(drw->fonts->layout, NULL);
    481 			}
    482 			x += ew;
    483 			w -= ew;
    484 		}
    485 	}
    486 	if (d)
    487 		XftDrawDestroy(d);
    488 
    489 	return x + (render ? w : 0);
    490 #else
    491 	int ty, ellipsis_x = 0;
    492 	unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len, hash, h0, h1;
    493 	XftDraw *d = NULL;
    494 	Fnt *usedfont, *curfont, *nextfont;
    495 	int utf8strlen, utf8charlen, utf8err, render = x || y || w || h;
    496 	long utf8codepoint = 0;
    497 	const char *utf8str;
    498 	FcCharSet *fccharset;
    499 	FcPattern *fcpattern;
    500 	FcPattern *match;
    501 	XftResult result;
    502 	int charexists = 0, overflow = 0;
    503 	/* keep track of a couple codepoints for which we have no match. */
    504 	static unsigned int nomatches[128], ellipsis_width, invalid_width;
    505 	static const char invalid[] = "�";
    506 
    507 	if (!drw || (render && (!drw->scheme || !w)) || !text || !drw->fonts)
    508 		return 0;
    509 
    510 	if (!render) {
    511 		w = invert ? invert : ~invert;
    512 	} else {
    513 		XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
    514 		XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
    515 		if (w < lpad)
    516 			return x + w;
    517 		#if BAR_ALPHA_PATCH
    518 		d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap);
    519 		#else
    520 		d = XftDrawCreate(drw->dpy, drw->drawable,
    521 		                  DefaultVisual(drw->dpy, drw->screen),
    522 		                  DefaultColormap(drw->dpy, drw->screen));
    523 		#endif // BAR_ALPHA_PATCH
    524 		x += lpad;
    525 		w -= lpad;
    526 	}
    527 
    528 	usedfont = drw->fonts;
    529 	if (!ellipsis_width && render)
    530 		ellipsis_width = drw_fontset_getwidth(drw, "...", markup);
    531 	if (!invalid_width && render)
    532 		invalid_width = drw_fontset_getwidth(drw, invalid, markup);
    533 	while (1) {
    534 		ew = ellipsis_len = utf8err = utf8charlen = utf8strlen = 0;
    535 		utf8str = text;
    536 		nextfont = NULL;
    537 		while (*text) {
    538 			utf8charlen = utf8decode(text, &utf8codepoint, &utf8err);
    539 			for (curfont = drw->fonts; curfont; curfont = curfont->next) {
    540 				charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
    541 				if (charexists) {
    542 					drw_font_getexts(curfont, text, utf8charlen, &tmpw, NULL);
    543 					if (ew + ellipsis_width <= w) {
    544 						/* keep track where the ellipsis still fits */
    545 						ellipsis_x = x + ew;
    546 						ellipsis_w = w - ew;
    547 						ellipsis_len = utf8strlen;
    548 					}
    549 
    550 					if (ew + tmpw > w) {
    551 						overflow = 1;
    552 						/* called from drw_fontset_getwidth_clamp():
    553 						 * it wants the width AFTER the overflow
    554 						 */
    555 						if (!render)
    556 							x += tmpw;
    557 						else
    558 							utf8strlen = ellipsis_len;
    559 					} else if (curfont == usedfont) {
    560 						text += utf8charlen;
    561 						utf8strlen += utf8err ? 0 : utf8charlen;
    562 						ew += utf8err ? 0 : tmpw;
    563 					} else {
    564 						nextfont = curfont;
    565 					}
    566 					break;
    567 				}
    568 			}
    569 
    570 			if (overflow || !charexists || nextfont || utf8err)
    571 				break;
    572 			else
    573 				charexists = 0;
    574 		}
    575 
    576 		if (utf8strlen) {
    577 			if (render) {
    578 				ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
    579 				XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
    580 				                  usedfont->xfont, x, ty, (XftChar8 *)utf8str, utf8strlen);
    581 			}
    582 			x += ew;
    583 			w -= ew;
    584 		}
    585 		if (utf8err && (!render || invalid_width < w)) {
    586 			if (render)
    587 				drw_text(drw, x, y, w, h, 0, invalid, invert, markup);
    588 			x += invalid_width;
    589 			w -= invalid_width;
    590 		}
    591 		if (render && overflow)
    592 			drw_text(drw, ellipsis_x, y, ellipsis_w, h, 0, "...", invert, markup);
    593 
    594 		if (!*text || overflow) {
    595 			break;
    596 		} else if (nextfont) {
    597 			charexists = 0;
    598 			usedfont = nextfont;
    599 		} else {
    600 			/* Regardless of whether or not a fallback font is found, the
    601 			 * character must be drawn. */
    602 			charexists = 1;
    603 
    604 			hash = (unsigned int)utf8codepoint;
    605 			hash = ((hash >> 16) ^ hash) * 0x21F0AAAD;
    606 			hash = ((hash >> 15) ^ hash) * 0xD35A2D97;
    607 			h0 = ((hash >> 15) ^ hash) % LENGTH(nomatches);
    608 			h1 = (hash >> 17) % LENGTH(nomatches);
    609 			/* avoid expensive XftFontMatch call when we know we won't find a match */
    610 			if (nomatches[h0] == utf8codepoint || nomatches[h1] == utf8codepoint)
    611 				goto no_match;
    612 
    613 			fccharset = FcCharSetCreate();
    614 			FcCharSetAddChar(fccharset, utf8codepoint);
    615 
    616 			if (!drw->fonts->pattern) {
    617 				/* Refer to the comment in xfont_create for more information. */
    618 				die("the first font in the cache must be loaded from a font string.");
    619 			}
    620 
    621 			fcpattern = FcPatternDuplicate(drw->fonts->pattern);
    622 			FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
    623 			FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
    624 			#if BAR_NO_COLOR_EMOJI_PATCH
    625 			FcPatternAddBool(fcpattern, FC_COLOR, FcFalse);
    626 			#endif // BAR_NO_COLOR_EMOJI_PATCH
    627 
    628 			FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
    629 			FcDefaultSubstitute(fcpattern);
    630 			match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
    631 
    632 			FcCharSetDestroy(fccharset);
    633 			FcPatternDestroy(fcpattern);
    634 
    635 			if (match) {
    636 				usedfont = xfont_create(drw, NULL, match);
    637 				if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
    638 					for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
    639 						; /* NOP */
    640 					curfont->next = usedfont;
    641 				} else {
    642 					xfont_free(usedfont);
    643 					nomatches[nomatches[h0] ? h1 : h0] = utf8codepoint;
    644 no_match:
    645 					usedfont = drw->fonts;
    646 				}
    647 			}
    648 		}
    649 	}
    650 	if (d)
    651 		XftDrawDestroy(d);
    652 
    653 	return x + (render ? w : 0);
    654 #endif // BAR_PANGO_PATCH
    655 }
    656 
    657 #if BIDI_PATCH
    658 int
    659 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert, Bool markup)
    660 {
    661 	apply_fribidi(text);
    662 	return _drw_text(drw, x, y, w, h, lpad, fribidi_text, invert, markup);
    663 }
    664 #endif // BIDI_PATCH
    665 
    666 #if BAR_POWERLINE_TAGS_PATCH || BAR_POWERLINE_STATUS_PATCH
    667 void
    668 drw_arrow(Drw *drw, int x, int y, unsigned int w, unsigned int h, int direction, int slash)
    669 {
    670 	if (!drw || !drw->scheme)
    671 		return;
    672 
    673 	/* direction=1 draws right arrow */
    674 	x = direction ? x : x + w;
    675 	w = direction ? w : -w;
    676 	/* slash=1 draws slash instead of arrow */
    677 	unsigned int hh = slash ? (direction ? 0 : h) : h/2;
    678 
    679 	XPoint points[] = {
    680 		{x    , y      },
    681 		{x + w, y + hh },
    682 		{x    , y + h  },
    683 	};
    684 
    685 	XPoint bg[] = {
    686 		{x    , y    },
    687 		{x + w, y    },
    688 		{x + w, y + h},
    689 		{x    , y + h},
    690 	};
    691 
    692 	XSetForeground(drw->dpy, drw->gc, drw->scheme[ColBg].pixel);
    693 	XFillPolygon(drw->dpy, drw->drawable, drw->gc, bg, 4, Convex, CoordModeOrigin);
    694 	XSetForeground(drw->dpy, drw->gc, drw->scheme[ColFg].pixel);
    695 	XFillPolygon(drw->dpy, drw->drawable, drw->gc, points, 3, Nonconvex, CoordModeOrigin);
    696 }
    697 #endif // BAR_POWERLINE_TAGS_PATCH | BAR_POWERLINE_STATUS_PATCH
    698 
    699 void
    700 drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
    701 {
    702 	if (!drw)
    703 		return;
    704 
    705 	XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
    706 	XSync(drw->dpy, False);
    707 }
    708 
    709 unsigned int
    710 drw_fontset_getwidth(Drw *drw, const char *text, Bool markup)
    711 {
    712 	if (!drw || !drw->fonts || !text)
    713 		return 0;
    714 	return drw_text(drw, 0, 0, 0, 0, 0, text, 0, markup);
    715 }
    716 
    717 #if BAR_PANGO_PATCH
    718 void
    719 drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h, Bool markup)
    720 {
    721 	if (!font || !text)
    722 		return;
    723 
    724 	PangoRectangle r;
    725 	if (markup)
    726 		pango_layout_set_markup(font->layout, text, len);
    727 	else
    728 		pango_layout_set_text(font->layout, text, len);
    729 	pango_layout_get_extents(font->layout, 0, &r);
    730 	if (markup) /* clear markup attributes */
    731 		pango_layout_set_attributes(font->layout, NULL);
    732 	if (w)
    733 		*w = r.width / PANGO_SCALE;
    734 	if (h)
    735 		*h = r.height / PANGO_SCALE;
    736 }
    737 #else
    738 void
    739 drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
    740 {
    741 	XGlyphInfo ext;
    742 
    743 	if (!font || !text)
    744 		return;
    745 
    746 	XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
    747 	if (w)
    748 		*w = ext.xOff;
    749 	if (h)
    750 		*h = font->h;
    751 }
    752 #endif // BAR_PANGO_PATCH
    753 
    754 Cur *
    755 drw_cur_create(Drw *drw, int shape)
    756 {
    757 	Cur *cur;
    758 
    759 	if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
    760 		return NULL;
    761 
    762 	cur->cursor = XCreateFontCursor(drw->dpy, shape);
    763 
    764 	return cur;
    765 }
    766 
    767 void
    768 drw_cur_free(Drw *drw, Cur *cursor)
    769 {
    770 	if (!cursor)
    771 		return;
    772 
    773 	XFreeCursor(drw->dpy, cursor->cursor);
    774 	free(cursor);
    775 }