[hackers] [PATCH 8/8] drw: fixup drw_text

From: Markus Teich <markus.teich_AT_stusta.mhn.de>
Date: Sun, 22 May 2016 22:29:25 +0200

- add optional left padding parameter which is ignored on non-render calls where
  just the width of the text is calculated.
- add /* NOP */ comments to empty loop bodies.
---
 drw.c | 30 +++++++++++++++---------------
 drw.h |  2 +-
 2 files changed, 16 insertions(+), 16 deletions(-)
diff --git a/drw.c b/drw.c
index 80383c9..987e53b 100644
--- a/drw.c
+++ b/drw.c
_AT_@ -235,10 +235,10 @@ drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int
 }
 
 int
-drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert)
+drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
 {
 	char buf[1024];
-	int tx, ty, th;
+	int ty;
 	unsigned int ew;
 	XftDraw *d = NULL;
 	Fnt *usedfont, *curfont, *nextfont;
_AT_@ -258,12 +258,13 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
 	if (!render) {
 		w = ~w;
 	} else {
-		XSetForeground(drw->dpy, drw->gc, invert ?
-		               drw->scheme[ColFg].pixel : drw->scheme[ColBg].pixel);
+		XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
 		XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
 		d = XftDrawCreate(drw->dpy, drw->drawable,
 		                  DefaultVisual(drw->dpy, drw->screen),
 		                  DefaultColormap(drw->dpy, drw->screen));
+		x += lpad;
+		w -= lpad;
 	}
 
 	usedfont = drw->fonts;
_AT_@ -295,20 +296,20 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
 		if (utf8strlen) {
 			drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
 			/* shorten text if necessary */
-			for (len = MIN(utf8strlen, (sizeof buf) - 1); len && (ew > w - usedfont->h || w < usedfont->h); len--)
+			for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--)
 				drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
 
 			if (len) {
 				memcpy(buf, utf8str, len);
 				buf[len] = '\0';
 				if (len < utf8strlen)
-					for (i = len; i && i > len - 3; buf[--i] = '.');
+					for (i = len; i && i > len - 3; buf[--i] = '.')
+						; /* NOP */
 
 				if (render) {
-					th = usedfont->h;
-					ty = y + (h - th) / 2 + usedfont->xfont->ascent;
-					tx = x + (h / 2);
-					XftDrawStringUtf8(d, invert ? &drw->scheme[ColBg] : &drw->scheme[ColFg], usedfont->xfont, tx, ty, (XftChar8 *)buf, len);
+					ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
+					XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
+					                  usedfont->xfont, x, ty, (XftChar8 *)buf, len);
 				}
 				x += ew;
 				w -= ew;
_AT_@ -322,8 +323,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
 			usedfont = nextfont;
 		} else {
 			/* Regardless of whether or not a fallback font is found, the
-			 * character must be drawn.
-			 */
+			 * character must be drawn. */
 			charexists = 1;
 
 			fccharset = FcCharSetCreate();
_AT_@ -349,7 +349,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
 				usedfont = xfont_create(drw, NULL, match);
 				if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
 					for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
-						; /* just find the end of the linked list */
+						; /* NOP */
 					curfont->next = usedfont;
 				} else {
 					xfont_free(usedfont);
_AT_@ -361,7 +361,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
 	if (d)
 		XftDrawDestroy(d);
 
-	return x;
+	return x + (render ? w : 0);
 }
 
 void
_AT_@ -379,7 +379,7 @@ drw_fontset_getwidth(Drw *drw, const char *text)
 {
 	if (!drw || !drw->fonts || !text)
 		return 0;
-	return drw_text(drw, 0, 0, 0, 0, text, 0);
+	return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
 }
 
 void
diff --git a/drw.h b/drw.h
index c221780..ff4355b 100644
--- a/drw.h
+++ b/drw.h
_AT_@ -51,7 +51,7 @@ void drw_setscheme(Drw *drw, Scm scm);
 
 /* Drawing functions */
 void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert);
-int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert);
+int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert);
 
 /* Map functions */
 void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h);
-- 
2.7.3
Received on Sun May 22 2016 - 22:29:25 CEST

This archive was generated by hypermail 2.3.0 : Sun May 22 2016 - 22:36:35 CEST