This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository e16.

View the commit online.

commit 4f617c77bfed9221b8ccab527839ae3742746054
Author: Kim Woelders <k...@woelders.dk>
AuthorDate: Tue Sep 19 15:15:52 2023 +0200

    text: Converge faster in TextFit functions
    
    "Fitting" very long strings (replacing center piece of text with
    ellipsis) could be very slow.
    
    This should speed things up considerably by skipping a lot of
    iterations.
---
 src/text.c | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/src/text.c b/src/text.c
index b388e537..cb11b08b 100644
--- a/src/text.c
+++ b/src/text.c
@@ -239,7 +239,7 @@ TextstateTextFit1(TextState * ts, char **ptext, int *pw, int textwidth_limit)
    char               *text = *ptext;
    int                 width, hh, ascent;
    char               *new_line;
-   int                 nuke_count = 0, nc2;
+   int                 nuke_count, nc2, len_n, cw;
    int                 len;
 
    len = strlen(text);
@@ -251,11 +251,12 @@ TextstateTextFit1(TextState * ts, char **ptext, int *pw, int textwidth_limit)
       return;
 
    width = *pw;
+   nuke_count = ((width - textwidth_limit) * len) / width;
+   if (nuke_count < 2)
+      nuke_count = 2;
 
    for (;;)
      {
-	nuke_count++;
-
 	if (nuke_count >= len - 1)
 	  {
 	     new_line[0] = text[0];
@@ -268,13 +269,20 @@ TextstateTextFit1(TextState * ts, char **ptext, int *pw, int textwidth_limit)
 	memcpy(new_line, text, nc2);
 	memcpy(new_line + nc2, "...", 3);
 	strcpy(new_line + nc2 + 3, text + nc2 + nuke_count);
+	len_n = len - nuke_count + 3;
 
 	ts->ops->TextSize(ts, new_line, 0, pw, &hh, &ascent);
 
 	width = *pw;
 	nc2 = textwidth_limit - width;
-	if (nc2 >= 0)
+	cw = width / len_n;
+
+	if (nc2 >= 0 && nc2 < 3 * cw)
 	   break;
+	if (nc2 > 0)
+	   nuke_count -= (nc2 <= 2 * cw) ? 1 : (nc2 + cw / 2) / cw;
+	else
+	   nuke_count += (-nc2 <= 2 * cw) ? 1 : (-nc2 + cw / 2) / cw;
      }
 
    Efree(text);
@@ -285,12 +293,12 @@ static void
 TextstateTextFitMB(TextState * ts, char **ptext, int *pw, int textwidth_limit)
 {
    char               *text = *ptext;
-   int                 width, hh, ascent;
+   int                 width, hh, ascent, cw;
    char               *new_line;
-   int                 nuke_count = 0, nc2;
+   int                 nuke_count, nc2;
    int                 len, len_mb;
    wchar_t            *wc_line = NULL;
-   int                 wc_len;
+   int                 wc_len, len_n;
 
    if (EwcOpen(ts->need_utf8 || Mode.locale.utf8_int))
       return;
@@ -312,11 +320,12 @@ TextstateTextFitMB(TextState * ts, char **ptext, int *pw, int textwidth_limit)
       goto done;
 
    width = *pw;
+   nuke_count = ((width - textwidth_limit) * wc_len) / width;
+   if (nuke_count < 2)
+      nuke_count = 2;
 
    for (;;)
      {
-	nuke_count++;
-
 	if (nuke_count >= wc_len - 1)
 	  {
 	     len_mb = EwcWcsToStr(wc_line, 1, new_line, MB_CUR_MAX);
@@ -336,13 +345,20 @@ TextstateTextFitMB(TextState * ts, char **ptext, int *pw, int textwidth_limit)
 			      wc_len - nc2 - nuke_count,
 			      new_line + len_mb, len + 10 - len_mb);
 	new_line[len_mb] = '\0';
+	len_n = wc_len - nuke_count + 3;
 
 	ts->ops->TextSize(ts, new_line, 0, pw, &hh, &ascent);
 
 	width = *pw;
 	nc2 = textwidth_limit - width;
-	if (nc2 >= 0)
+	cw = width / len_n;
+
+	if (nc2 >= 0 && nc2 < 3 * cw)
 	   break;
+	if (nc2 > 0)
+	   nuke_count -= (nc2 <= 2 * cw) ? 1 : (nc2 + cw / 2) / cw;
+	else
+	   nuke_count += (-nc2 <= 2 * cw) ? 1 : (-nc2 + cw / 2) / cw;
      }
 
    Efree(text);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to