drw_text: improve both performance and correctness 41fdabbf
this patch makes some non-trivial changes, which significantly improves
the performance of drawing large strings as well as fixes any issues
regarding the printing of the ellipsis when string gets truncated.

* performance:

before there were two O(n) loops, one which finds how long we can go
without changing font, and the second loop would (incorrectly) truncate
the string if it's too big.

this patch merges the overflow calculation into the first loop and exits
out when overflow is detected. when dumping lots of emojies into dmenu,
i see some noticeable startup time improvement:

before -> after
460ms  -> 360ms

input latency when scrolling up/down is also noticeably better and can
be tested with the following:

	for _ in $(seq 20); do
		cat /dev/urandom | base64 | tr -d '\n' | head -c 1000000
	echo
	done | ./dmenu -l 10

* correctness:

the previous version would incorrectly assumed single byte chars and
would overwrite them with '.' , this caused a whole bunch of obvious
problems, including the ellipsis not getting rendered if then font
changed.

in addition to exiting out when we detect overflow, this patch also
keeps track of the last x-position where the ellipsis would fit. if we
detect overflow, we simply make a recursing call to drw_text() at the
ellipsis_x position and overwrite what was there.

so now the ellipsis will always be printed properly, regardless of
weather the font changes or if the string is single byte char or not.

the idea of rendering the ellipsis on top incase of overflow was
from Bakkeby <bakkeby@gmail.com>, thanks! however the original patch had
some issues incorrectly truncating the prompt (-p flag) and cutting off
emojies. those have been fixed in here.
NRK · 2022-03-24 00:37 1 file(s) · +28 −28
drw.c +28 −28
251 251
int
252 252
drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
253 253
{
254 -
	char buf[1024];
255 -
	int ty;
256 -
	unsigned int ew;
254 +
	int ty, ellipsis_x = 0;
255 +
	unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len, ellipsis_width;
257 256
	XftDraw *d = NULL;
258 257
	Fnt *usedfont, *curfont, *nextfont;
259 -
	size_t i, len;
260 258
	int utf8strlen, utf8charlen, render = x || y || w || h;
261 259
	long utf8codepoint = 0;
262 260
	const char *utf8str;
264 262
	FcPattern *fcpattern;
265 263
	FcPattern *match;
266 264
	XftResult result;
267 -
	int charexists = 0;
265 +
	int charexists = 0, overflow = 0;
268 266
269 267
	if (!drw || (render && !drw->scheme) || !text || !drw->fonts)
270 268
		return 0;
282 280
	}
283 281
284 282
	usedfont = drw->fonts;
283 +
	drw_font_getexts(usedfont, "...", 3, &ellipsis_width, NULL);
285 284
	while (1) {
286 -
		utf8strlen = 0;
285 +
		ew = ellipsis_len = utf8strlen = 0;
287 286
		utf8str = text;
288 287
		nextfont = NULL;
289 288
		while (*text) {
291 290
			for (curfont = drw->fonts; curfont; curfont = curfont->next) {
292 291
				charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
293 292
				if (charexists) {
294 -
					if (curfont == usedfont) {
293 +
					drw_font_getexts(curfont, text, utf8charlen, &tmpw, NULL);
294 +
					if (ew + ellipsis_width <= w) {
295 +
						/* keep track where the ellipsis still fits */
296 +
						ellipsis_x = x + ew;
297 +
						ellipsis_w = w - ew;
298 +
						ellipsis_len = utf8strlen;
299 +
					}
300 +
301 +
					if (ew + tmpw > w) {
302 +
						overflow = 1;
303 +
						utf8strlen = ellipsis_len;
304 +
					} else if (curfont == usedfont) {
295 305
						utf8strlen += utf8charlen;
296 306
						text += utf8charlen;
307 +
						ew += tmpw;
297 308
					} else {
298 309
						nextfont = curfont;
299 310
					}
301 312
				}
302 313
			}
303 314
304 -
			if (!charexists || nextfont)
315 +
			if (overflow || !charexists || nextfont)
305 316
				break;
306 317
			else
307 318
				charexists = 0;
308 319
		}
309 320
310 321
		if (utf8strlen) {
311 -
			drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
312 -
			/* shorten text if necessary */
313 -
			for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--)
314 -
				drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
315 -
316 -
			if (len) {
317 -
				memcpy(buf, utf8str, len);
318 -
				buf[len] = '\0';
319 -
				if (len < utf8strlen)
320 -
					for (i = len; i && i > len - 3; buf[--i] = '.')
321 -
						; /* NOP */
322 -
323 -
				if (render) {
324 -
					ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
325 -
					XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
326 -
					                  usedfont->xfont, x, ty, (XftChar8 *)buf, len);
327 -
				}
328 -
				x += ew;
329 -
				w -= ew;
322 +
			if (render) {
323 +
				ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
324 +
				XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
325 +
				                  usedfont->xfont, x, ty, (XftChar8 *)utf8str, utf8strlen);
330 326
			}
327 +
			x += ew;
328 +
			w -= ew;
331 329
		}
330 +
		if (render && overflow)
331 +
			drw_text(drw, ellipsis_x, y, ellipsis_w, h, 0, "...", invert);
332 332
333 -
		if (!*text) {
333 +
		if (!*text || overflow) {
334 334
			break;
335 335
		} else if (nextfont) {
336 336
			charexists = 0;