commit 86a8baae96e3e352e7486bb8f61584be42339e15
parent a6128237108e74682b8ca427a01dd6762cb95891
Author: lumidify <nobody@lumidify.org>
Date:   Sun, 17 May 2020 20:02:34 +0200
Draw glyphs in correct order
This will be needed for efficiently determining which glyph corresponds
to the location a user clicked.
Diffstat:
| M | text_buffer.c |  |  | 60 | +++++++++++++++++++++++++++++------------------------------- | 
1 file changed, 29 insertions(+), 31 deletions(-)
diff --git a/text_buffer.c b/text_buffer.c
@@ -200,6 +200,7 @@ ltk_create_ximage(Display *dpy, int w, int h, int depth, XColor bg) {
 /* based on http://codemadness.org/git/dwm-font/file/drw.c.html#l315 */
 void
 ltk_draw_glyph(LtkGlyph *glyph, XImage *img, int x, int y, XColor fg) {
+	printf("%d,%d\n", x, y);
 	double a;
 	int b;
 	for (int i = 0; i < glyph->info->h; i++) {
@@ -244,55 +245,52 @@ ltk_render_text_line_new(
 		int cur_border = par_is_rtl ? sl->w : 0;
 		while (cur && cur_len < sl->len) {
 			int start_index;
-			/* FIXME: the borders here aren't correct since they are
-			   x_abs + glyph->info->w, which may not be the same thing
-			   as the actual x_abs of the bordering glyph */
 			if (cur->dir == HB_DIRECTION_RTL) {
 				start_index = cur == sl->run ? sl->glyph_index : cur->num_glyphs - 1;
 				int local_border = cur->glyphs[start_index].x_abs + cur->glyphs[start_index].x_advance;
 				int end_index;
-				if (start_index + 1 < sl->len - cur_len) {
+				if (start_index + 1 < sl->len - cur_len)
 					end_index = 0;
-				} else {
+				else
 					end_index = start_index - (sl->len - cur_len) + 1;
-				}
-				for (int i = start_index; i >= 0 && cur_len < sl->len; i--) {
-					cur_len++;
-					int x;
-					if (par_is_rtl) {
-						x = cur_border - (local_border - cur->glyphs[i].x_abs);
-					} else {
-						x = cur_border + (cur->glyphs[i].x_abs - cur->glyphs[end_index].x_abs);
+				if (par_is_rtl) {
+					for (int i = start_index; i >= 0 && cur_len < sl->len; i--) {
+						cur_len++;
+						int x = cur_border - (local_border - cur->glyphs[i].x_abs);
+						ltk_draw_glyph(&cur->glyphs[i], sl->img, x, cur->glyphs[i].y_abs, fg);
 					}
-					ltk_draw_glyph(&cur->glyphs[i], sl->img, x, cur->glyphs[i].y_abs, fg);
-				}
-				if (par_is_rtl)
 					cur_border -= local_border - cur->glyphs[0].x_abs;
-				else
+				} else {
+					for (int i = end_index; i < cur->num_glyphs && cur_len < sl->len; i++) {
+						cur_len++;
+						int x = cur_border + (cur->glyphs[i].x_abs - cur->glyphs[end_index].x_abs);
+						ltk_draw_glyph(&cur->glyphs[i], sl->img, x, cur->glyphs[i].y_abs, fg);
+					}
 					cur_border += local_border - cur->glyphs[0].x_abs;
+				}
 			} else {
 				start_index = cur == sl->run ? sl->glyph_index : 0;
 				int local_border = cur->glyphs[start_index].x_abs;
 				int end_index;
-				if (cur->num_glyphs - start_index < sl->len - cur_len) {
+				if (cur->num_glyphs - start_index < sl->len - cur_len)
 					end_index = cur->num_glyphs - 1;
-				} else {
+				else
 					end_index = start_index + sl->len - cur_len - 1;
-				}
-				for (int i = start_index; i < cur->num_glyphs && cur_len < sl->len; i++) {
-					cur_len++;
-					int x;
-					if (par_is_rtl) {
-						x = cur_border - (cur->glyphs[end_index].x_abs + cur->glyphs[end_index].x_advance - cur->glyphs[i].x_abs);
-					} else {
-						x = cur_border + (cur->glyphs[i].x_abs - local_border);
+				if (par_is_rtl) {
+					for (int i = end_index; i >= 0 && cur_len < sl->len; i--) {
+						cur_len++;
+						int x = cur_border - (cur->glyphs[end_index].x_abs + cur->glyphs[end_index].x_advance - cur->glyphs[i].x_abs);
+						ltk_draw_glyph(&cur->glyphs[i], sl->img, x, cur->glyphs[i].y_abs, fg);
 					}
-					ltk_draw_glyph(&cur->glyphs[i], sl->img, x, cur->glyphs[i].y_abs, fg);
-				}
-				if (par_is_rtl)
 					cur_border -= cur->glyphs[cur->num_glyphs - 1].x_abs + cur->glyphs[cur->num_glyphs - 1].x_advance - local_border;
-				else
+				} else {
+					for (int i = start_index; i < cur->num_glyphs && cur_len < sl->len; i++) {
+						cur_len++;
+						int x = cur_border + (cur->glyphs[i].x_abs - local_border);
+						ltk_draw_glyph(&cur->glyphs[i], sl->img, x, cur->glyphs[i].y_abs, fg);
+					}
 					cur_border += cur->glyphs[cur->num_glyphs - 1].x_abs + cur->glyphs[cur->num_glyphs - 1].x_advance - local_border;
+				}
 			}
 			cur = par_is_rtl ? cur->last : cur->next;
 		}