improve lrpad - libsl - draw back-ends for dwm, dmenu, etc
 (HTM) git clone git://git.codemadness.org/libsl
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit f33024dd2dbb6471cb2c5c41f901fc19e4c133a8
 (DIR) parent b450cbeceb639c6372733d14d1a3af95550109d2
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Sun,  5 Apr 2020 17:26:59 +0200
       
       improve lrpad
       
       Diffstat:
         M x11/drw.c                           |      27 +++++++++++++++++----------
       
       1 file changed, 17 insertions(+), 10 deletions(-)
       ---
 (DIR) diff --git a/x11/drw.c b/x11/drw.c
       @@ -162,21 +162,25 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
                const char *text, int invert)
        {
                char buf[256];
       -        int i, tx, ty, th, len, olen;
       +        int i, tx, ty, th, len, olen, render = x || y || w || h;
                unsigned int texw, texh;
        
       -        /* TODO: lpad */
       -
       -        XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
       -        XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
       +        if (!render) {
       +                w = ~w;
       +        } else {
       +                XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
       +                XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
       +                x += lpad;
       +                w -= lpad;
       +        }
        
                olen = strlen(text);
                drw_font_getexts(drw->fonts, text, olen, &texw, &texh);
                th = drw->fonts->ascent + drw->fonts->descent;
                ty = y + (h / 2) - (th / 2) + drw->fonts->ascent;
       -        tx = x + (h / 2);
       +        tx = x;
                /* shorten text if necessary */
       -        for (len = MIN(olen, sizeof buf); len && (texw > w - texh || w < texh); len--)
       +        for (len = MIN(olen, sizeof buf); len && texw > w; len--)
                        drw_font_getexts(drw->fonts, text, len, &texw, &texh);
                if (!len)
                        return x;
       @@ -190,7 +194,10 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
                else
                        XDrawString(drw->dpy, drw->drawable, drw->gc, tx, ty, buf, len);
        
       -        return x + w;
       +        x += texw;
       +        w -= texw;
       +
       +        return x + (render ? w : 0);
        }
        
        void
       @@ -228,10 +235,10 @@ drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w,
                        if (h)
                                *h = r.height;
                } else {
       -                if (h)
       -                        *h = font->ascent + font->descent;
                        if (w)
                                *w = XTextWidth(font->xfont, text, len);
       +                if (h)
       +                        *h = font->ascent + font->descent;
                }
        }