commit d02e1da3846868b392d6af9451d160c0fc07b2eb
parent f1e4126fb66f7b15d1a0ed50a4b5da72e8224fb4
Author: lumidify <nobody@lumidify.org>
Date:   Wed,  3 Mar 2021 16:46:40 +0100
Add line breaking to pango text backend
Diffstat:
2 files changed, 21 insertions(+), 19 deletions(-)
diff --git a/src/text_pango.c b/src/text_pango.c
@@ -72,9 +72,21 @@ ltk_cleanup_text(void) {
 
 void
 ltk_text_line_set_width(ltk_text_line *tl, int width) {
-	(void)tl;
-	(void)width;
-	/* TODO: Implement! */
+	pango_layout_set_width(tl->layout, width * PANGO_SCALE);
+	pango_layout_get_size(tl->layout, &tl->w, &tl->h);
+	tl->w /= PANGO_SCALE;
+	tl->h /= PANGO_SCALE;
+	tl->w = tl->w == 0 ? 1 : tl->w;
+	tl->h = tl->h == 0 ? 1 : tl->h;
+	/* FIXME: make this nicer */
+	if (tl->draw) {
+		XftDrawDestroy(tl->draw);
+		XFreePixmap(tm.dpy, tl->pixmap);
+	}
+	XWindowAttributes attrs;
+	XGetWindowAttributes(tm.dpy, tl->window, &attrs);
+	tl->pixmap = XCreatePixmap(tm.dpy, tl->window, tl->w, tl->h, attrs.depth);
+	tl->draw = XftDrawCreate(tm.dpy, tl->pixmap, DefaultVisual(tm.dpy, tm.screen), tm.cm);
 }
 
 ltk_text_line *
@@ -86,27 +98,15 @@ ltk_text_line_create(Window window, uint16_t font_size, char *text, int width) {
 	line->font_size = font_size;
 	line->layout = pango_layout_new(tm.context);
 
-	if (width > 0) {
-		pango_layout_set_width(line->layout, width * PANGO_SCALE);
-		pango_layout_set_wrap(line->layout, PANGO_WRAP_WORD_CHAR);
-	}
-
 	PangoFontDescription *desc = pango_font_description_from_string(tm.default_font);
 	pango_font_description_set_size(desc, font_size * PANGO_SCALE);
 	pango_layout_set_font_description(line->layout, desc);
 	pango_font_description_free(desc);
-
-	pango_layout_set_text(line->layout, text, -1);
-	pango_layout_get_size(line->layout, &line->w, &line->h);
-	line->w /= PANGO_SCALE;
-	line->h /= PANGO_SCALE;
-	line->w = line->w == 0 ? 1 : line->w;
-	line->h = line->h == 0 ? 1 : line->h;
-	XWindowAttributes attrs;
-	XGetWindowAttributes(tm.dpy, window, &attrs);
-	line->pixmap = XCreatePixmap(tm.dpy, window, line->w, line->h, attrs.depth);
-	line->draw = XftDrawCreate(tm.dpy, line->pixmap, DefaultVisual(tm.dpy, tm.screen), tm.cm);
 	line->window = window;
+	line->draw = NULL;
+	pango_layout_set_wrap(line->layout, PANGO_WRAP_WORD_CHAR);
+	pango_layout_set_text(line->layout, text, -1);
+	ltk_text_line_set_width(line, width);
 
 	return line;
 }
diff --git a/src/text_stb.c b/src/text_stb.c
@@ -648,5 +648,7 @@ ltk_text_line_destroy(ltk_text_line *tl) {
 	ltk_free(tl->text);
 	/* FIXME: Reference count glyph infos */
 	ltk_free(tl->glyphs);
+	if (tl->line_indeces)
+		ltk_free(tl->line_indeces);
 	ltk_free(tl);
 }