diff options
author | NRK <nrk@disroot.org> | 2022-03-24 02:00:00 +0600 |
---|---|---|
committer | Hiltjo Posthuma <hiltjo@codemadness.org> | 2022-03-25 22:49:07 +0100 |
commit | 6be057f060543bb0f3ed9423904263617cdffffe (patch) | |
tree | 1c38c588659ed513040068e3cafd6561b4917f55 | |
parent | 41fdabbf7c517f8d524b70cbd78238cc319ccef3 (diff) |
introduce drw_fontset_getwidth_clamp()
getting the width of a string is an O(n) operation, and in many cases
users only care about getting the width upto a certain number.
instead of calling drw_fontset_getwidth() and *then* clamping the
result, this patch introduces drw_fontset_getwidth_clamp() function,
similar to strnlen(), which will stop once we reach n.
the `invert` parameter was overloaded internally to preserve the API,
however library users should be calling drw_fontset_getwidth_clamp() and
not depend upon internal behavior of drw_text().
-rw-r--r-- | drw.c | 19 | ||||
-rw-r--r-- | drw.h | 1 |
2 files changed, 18 insertions, 2 deletions
@@ -268,7 +268,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp | |||
268 | return 0; | 268 | return 0; |
269 | 269 | ||
270 | if (!render) { | 270 | if (!render) { |
271 | w = ~w; | 271 | w = invert ? invert : ~invert; |
272 | } else { | 272 | } else { |
273 | XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel); | 273 | XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel); |
274 | XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); | 274 | XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); |
@@ -300,7 +300,13 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp | |||
300 | 300 | ||
301 | if (ew + tmpw > w) { | 301 | if (ew + tmpw > w) { |
302 | overflow = 1; | 302 | overflow = 1; |
303 | utf8strlen = ellipsis_len; | 303 | /* called from drw_fontset_getwidth_clamp(): |
304 | * it wants the width AFTER the overflow | ||
305 | */ | ||
306 | if (!render) | ||
307 | x += tmpw; | ||
308 | else | ||
309 | utf8strlen = ellipsis_len; | ||
304 | } else if (curfont == usedfont) { | 310 | } else if (curfont == usedfont) { |
305 | utf8strlen += utf8charlen; | 311 | utf8strlen += utf8charlen; |
306 | text += utf8charlen; | 312 | text += utf8charlen; |
@@ -397,6 +403,15 @@ drw_fontset_getwidth(Drw *drw, const char *text) | |||
397 | return drw_text(drw, 0, 0, 0, 0, 0, text, 0); | 403 | return drw_text(drw, 0, 0, 0, 0, 0, text, 0); |
398 | } | 404 | } |
399 | 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 | |||
400 | void | 415 | void |
401 | drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h) | 416 | drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h) |
402 | { | 417 | { |
@@ -35,6 +35,7 @@ void drw_free(Drw *drw); | |||
35 | Fnt *drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount); | 35 | Fnt *drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount); |
36 | void drw_fontset_free(Fnt* set); | 36 | void drw_fontset_free(Fnt* set); |
37 | unsigned int drw_fontset_getwidth(Drw *drw, const char *text); | 37 | unsigned int drw_fontset_getwidth(Drw *drw, const char *text); |
38 | unsigned int drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n); | ||
38 | void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h); | 39 | void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h); |
39 | 40 | ||
40 | /* Colorscheme abstraction */ | 41 | /* Colorscheme abstraction */ |