herbe icon indicating copy to clipboard operation
herbe copied to clipboard

patch: Centered text

Open TheAndreLuiz opened this issue 4 years ago • 2 comments

Makes the text centered, instead of the default right padding. Works with any font and font size.

Diff

TheAndreLuiz avatar May 20 '21 18:05 TheAndreLuiz

Doesn't cope well with multi-byte chars it seems. Try the following to reproduce:

$ ./herbe "  some text   "

image

N-R-K avatar Mar 15 '22 13:03 N-R-K

The following fixes the issue for me:

patch
From 440fe73fa5446039827e9b90548d74510fcfa1e3 Mon Sep 17 00:00:00 2001
From: NRK <[email protected]>
Date: Wed, 16 Mar 2022 17:36:00 +0600
Subject: [PATCH] patch: center text

---
 herbe.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/herbe.c b/herbe.c
index 51d3990..90a04de 100644
--- a/herbe.c
+++ b/herbe.c
@@ -189,8 +189,15 @@ int main(int argc, char *argv[])
 		{
 			XClearWindow(display, window);
 			for (int i = 0; i < num_of_lines; i++)
-				XftDrawStringUtf8(draw, &color, font, padding, line_spacing * i + text_height * (i + 1) + padding,
-								  (FcChar8 *)lines[i], strlen(lines[i]));
+			{
+				XGlyphInfo info;
+				int len = strlen(lines[i]);
+
+				XftTextExtentsUtf8(display, font, (XftChar8 *)lines[i], len, &info);
+				XftDrawStringUtf8(draw, &color, font, (width - info.width) / 2,
+				                  line_spacing * i + text_height * (i + 1) + padding,
+				                  (FcChar8 *)lines[i], len);
+			}
 		}
 		else if (event.type == ButtonPress)
 		{
@@ -217,4 +224,4 @@ int main(int argc, char *argv[])
 	XCloseDisplay(display);
 
 	return exit_code;
-}
\ No newline at end of file
+}
-- 
2.34.1

N-R-K avatar Mar 16 '22 11:03 N-R-K