commit 8db8f0b44f5f1c727be32114a22863bf72355a76
parent 9c62c13ad2f58414b7de5dd1b928c9dd780df8cc
Author: lumidify <nobody@lumidify.org>
Date:   Sat, 23 May 2020 20:18:29 +0200
Only wrap lines on space characters
Diffstat:
2 files changed, 41 insertions(+), 20 deletions(-)
diff --git a/text_buffer.c b/text_buffer.c
@@ -113,6 +113,9 @@ ltk_text_line_wrap(struct ltk_text_line *tl, int max_width) {
 	int last_linebreak = par_is_rtl ? tl->w : 0;
 	int cur_start = 0;
 	/* FIXME: also calculate max height of each line */
+	/* Note: 0x20 is space */
+	/* Note: No, this doesn't do proper Unicode linebreaking */
+	/* Note: This is probably buggy */
 	while (cur) {
 		if (sl->w + cur->w <= max_width) {
 			sl->w += cur->w;
@@ -127,14 +130,23 @@ ltk_text_line_wrap(struct ltk_text_line *tl, int max_width) {
 				glyph = &cur->glyphs[i];
 				int cur_w = sl->w + cur_start - glyph->x_abs;
 				if (cur_w > max_width) {
+					int char_break = -1;
 					for (int j = i; j < cur->num_glyphs; j++) {
-						if (cur->glyphs[j].cluster != glyph->cluster ||
+						if (char_break == -1 && cur->glyphs[j].cluster != glyph->cluster)
+							char_break = j;
+						if ((j != i && tl->log_buf->buf[cur->glyphs[j].cluster] == 0x20) ||
 						    j == cur->num_glyphs - 1 || sl->len == 0) {
 							if (j == cur->num_glyphs - 1 &&
-							    cur->glyphs[j].cluster == glyph->cluster &&
-							    sl->len > 0) {
-								i = j;
-								last_linebreak = cur_start;
+							    tl->log_buf->buf[cur->glyphs[j].cluster] != 0x20) {
+								if (sl->len == 0) {
+									char_break = char_break == -1 ? j : char_break;
+									i = char_break - 1;
+									last_linebreak = cur->glyphs[char_break].x_abs;
+									sl->len += j - char_break + 1;
+								} else {
+									i = j;
+									last_linebreak = cur_start;
+								}
 							} else {
 								i = j - 1;
 								last_linebreak = cur->glyphs[j].x_abs;
@@ -175,14 +187,23 @@ ltk_text_line_wrap(struct ltk_text_line *tl, int max_width) {
 				   of the two? What if the glyph width is actually larger than x_advance? */
 				int cur_w = sl->w + glyph->x_abs + glyph->x_advance - cur_start;
 				if (cur_w > max_width) {
+					int char_break = -1;
 					for (int j = i; j >= 0; j--) {
-						if (cur->glyphs[j].cluster != glyph->cluster ||
+						if (char_break == -1 && cur->glyphs[j].cluster != glyph->cluster)
+							char_break = j;
+						if ((j != i && tl->log_buf->buf[cur->glyphs[j].cluster] == 0x20) ||
 						    j == 0 || sl->len == 0) {
 							if (j == 0 &&
-							    cur->glyphs[j].cluster == glyph->cluster &&
-							    sl->len > 0) {
-								i = j;
-								last_linebreak = cur_start;
+							    tl->log_buf->buf[cur->glyphs[j].cluster] != 0x20) {
+								if (sl->len == 0) {
+									char_break = char_break == -1 ? j : char_break;
+									i = char_break + 1;
+									last_linebreak = cur->glyphs[char_break + 1].x_abs;
+									sl->len += char_break - j + 1;
+								} else {
+									i = j;
+									last_linebreak = cur_start;
+								}
 							} else {
 								i = j + 1;
 								last_linebreak = cur->glyphs[j + 1].x_abs;
@@ -882,8 +903,6 @@ ltk_text_line_create(uint16_t font_size) {
 	line->len = 0;
 	line->w_wrapped = line->h_wrapped = 0;
 	line->font_size = font_size;
-	/* FIXME */
-	line->line_gap = 0;
 	return line;
 error:
 	(void)fprintf(stderr, "No memory left while creating text line\n");
@@ -937,7 +956,7 @@ ltk_text_buffer_insert_utf8_at_cursor(struct ltk_text_buffer *tb, char *text) {
 			new[i] = c;
 		}
 	}
-	ltk_text_line_insert_utf32(tb->cur_line, tb->cursor_pos, new, actual_len);
+	//ltk_text_line_insert_utf32(tb->cur_line, tb->cursor_pos, new, actual_len);
 	free(new);
 }
 
@@ -948,10 +967,8 @@ ltk_text_buffer_create(void) {
 		(void)fprintf(stderr, "No memory while creating text buffer\n");
 		exit(1);
 	}
-	buf->head = ltk_text_line_create(20);
-	buf->cur_line = buf->head;
+	buf->head = NULL;
 	buf->cursor_pos = 0;
-	buf->line_gap = 0;
 }
 
 void
diff --git a/text_buffer.h b/text_buffer.h
@@ -84,14 +84,18 @@ struct ltk_text_line {
 	int h;
 	int w_wrapped;
 	int h_wrapped;
-	unsigned int line_gap;
+};
+
+struct ltk_text_line_list {
+	struct ltk_text_line *tl;
+	XImage *img;
+	struct ltk_text_line_list *next;
+	struct ltk_text_line_list *last;
 };
 
 struct ltk_text_buffer {
-	struct ltk_text_line *head;
-	struct ltk_text_line *cur_line;
+	struct ltk_text_line_list *head;
 	size_t cursor_pos;
-	unsigned int line_gap;
 };
 
 void ltk_soft_line_destroy(struct ltk_soft_line *sl);