On Sun, Dec 26, 2021 at 07:29:27PM +0100, Hiltjo Posthuma wrote:
> > Thanks for the patch. It uses indeed less resources for very long lines and 
> > it
> > makes more sense to do it this way.
> > 
> > A small change for the test script, you probably want to add a newline 
> > (echo)
> > between it:
> > 
> >     for i in $(seq 20); do
> >             cat /dev/urandom | base64 | tr -d '\n' | head -c 1000000
> >     echo
> >     done | ./dmenu -l 10
> > 
> > I synced the code to dwm and the libsl repository too.
> > 
> > -- 
> > Kind regards,
> > Hiltjo
> > 
> 
> Hi,
> 
> This patch had issues so it was reverted for now. Anyone have time to work on
> this during the holidays maybe?
> 
> The drw.c drw_text() text truncation code using "..." is also incorrect
> currently for UTF-8 and when multiple fallback fonts are used.

Hi,

I assume the reason for this was that with loop condition `n < limit`
the loop would stop when `n >= limit`, meaning that n might
"over-shoot".

If that was the case, then just having another loop cleanup the
over-shoot should fix the issue. Testing with the test script in this
thread, I see proper truncation identical to mainline dmenu without this
patch.

However I still saw some problems with emojies/unicode truncation, the
test file is attached.

        ./dmenu < unicringe.txt

Changing the first loop condition from `len < MIN(..)` to `len <=
MIN(..)` fixes that as well for me.

NB: I'm not really sure weather this patch is *actually* correct or not.
But since both of my test-cases are passing, sending the patch.

- NRK
๐Ÿ˜€ ๐Ÿ˜ƒ ๐Ÿ˜„ ๐Ÿ˜ ๐Ÿ˜† ๐Ÿ˜… ๐Ÿคฃ ๐Ÿ˜‚ ๐Ÿ™‚ ๐Ÿ™ƒ ๐Ÿ˜‰ ๐Ÿ˜Š ๐Ÿ˜‡ ๐Ÿฅฐ ๐Ÿ˜ ๐Ÿคฉ ๐Ÿ˜˜ ๐Ÿ˜— โ˜บ๏ธ ๐Ÿ˜š ๐Ÿ˜™ ๐Ÿฅฒ ๐Ÿ˜‹ ๐Ÿ˜› ๐Ÿ˜œ ๐Ÿคช ๐Ÿ˜ ๐Ÿค‘ ๐Ÿค— ๐Ÿคญ ๐Ÿคซ ๐Ÿค” ๐Ÿค ๐Ÿคจ ๐Ÿ˜ ๐Ÿ˜‘ ๐Ÿ˜ถ ๐Ÿ˜ ๐Ÿ˜’ 
๐Ÿ™„ ๐Ÿ˜ฌ ๐Ÿคฅ ๐Ÿ˜Œ ๐Ÿ˜” ๐Ÿ˜ช ๐Ÿคค ๐Ÿ˜ด ๐Ÿ˜ท ๐Ÿค’ ๐Ÿค• ๐Ÿคข ๐Ÿคฎ ๐Ÿคง ๐Ÿฅต ๐Ÿฅถ ๐Ÿฅด ๐Ÿ˜ต ๐Ÿคฏ ๐Ÿค  ๐Ÿฅณ ๐Ÿฅธ ๐Ÿ˜Ž ๐Ÿค“ ๐Ÿง ๐Ÿ˜• ๐Ÿ˜Ÿ ๐Ÿ™ โ˜น๏ธ ๐Ÿ˜ฎ ๐Ÿ˜ฏ ๐Ÿ˜ฒ ๐Ÿ˜ณ ๐Ÿฅบ ๐Ÿ˜ฆ ๐Ÿ˜ง ๐Ÿ˜จ ๐Ÿ˜ฐ ๐Ÿ˜ฅ 
๐Ÿ˜ข ๐Ÿ˜ญ ๐Ÿ˜ฑ ๐Ÿ˜– ๐Ÿ˜ฃ ๐Ÿ˜ž ๐Ÿ˜“ ๐Ÿ˜ฉ ๐Ÿ˜ซ ๐Ÿฅฑ ๐Ÿ˜ค ๐Ÿ˜ก ๐Ÿ˜  ๐Ÿคฌ ๐Ÿ˜ˆ ๐Ÿ‘ฟ ๐Ÿ’€ โ˜ ๏ธ ๐Ÿ’ฉ ๐Ÿคก ๐Ÿ‘น ๐Ÿ‘บ
>From 0ce7d6adfe1cdd460eabf26a4de6477dc65012a3 Mon Sep 17 00:00:00 2001
From: NRK <[email protected]>
Date: Sun, 13 Mar 2022 15:48:41 +0600
Subject: [PATCH] optimize drw_text() for large strings

... but correctly (hopefully =])

based on Miles Alan's patch.
---
 drw.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drw.c b/drw.c
index 4cdbcbe..e66e137 100644
--- a/drw.c
+++ b/drw.c
@@ -310,8 +310,14 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
 		if (utf8strlen) {
 			drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
 			/* shorten text if necessary */
-			for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--)
-				drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
+			if (ew > w) {
+				for (ew = 0, len = 0; ew < w && len <= MIN(utf8strlen, sizeof(buf) - 1); len++)
+					drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
+				for (; ew > w && len; --len) /* mop-up incase of overshoot */
+					drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
+			} else {
+				len = MIN(utf8strlen, sizeof(buf) - 1);
+			}
 
 			if (len) {
 				memcpy(buf, utf8str, len);
-- 
2.34.1

Reply via email to