shithub: mongrel

Download patch

ref: 8ebb6feb1ca4d27d7a6155846dc7ea0c880c9316
parent: b34f062e8d33eb64593a959302f7377bd0abc11c
author: phil9 <telephil9@gmail.com>
date: Thu May 26 17:18:18 EDT 2022

wrap lines (more) properly

	Instead of wrapping in the middle of words, try to wrap lines at the
	last encountered space character.

--- a/text.c
+++ b/text.c
@@ -25,7 +25,7 @@
 	{ "^-[^\\-]+$",   0xcc241dFF, nil },
 	{ "^\\+.*$", 0x98971AFF, nil },
 	/* quote */
-	//{ "^>.*$", 0x928374FF, nil },
+	{ "^>.*$", 0x928374FF, nil },
 };
 
 enum
@@ -60,7 +60,7 @@
 void
 computelines(Text *t)
 {
-	int i, x, w, l, c, n, wrap;
+	int i, x, w, l, c, n, s, wrap;
 	Rune r;
 	char buf[4096] = {0};
 	Image *h;
@@ -69,6 +69,7 @@
 	t->nlines = 1;
 	w = Dx(t->textr);
 	x = 0;
+	s = 0;
 	wrap = 0;
 	h = nil;
 	for(i = 0; i < t->ndata; ){
@@ -95,8 +96,11 @@
 			}else{
 				l = runestringnwidth(t->font, &r, 1);
 				x += l;
+				if(r == ' ')
+					s = i + 1;
 			}
 			if(x > w){
+				i = s;
 				n = i - t->lines[t->nlines - 1];
 				strncpy(buf, t->data + t->lines[t->nlines - 1], n);
 				buf[n] = 0;