From 4794a57face4e6e521c8a60a120ebf00bbc8a94f Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Sat, 16 Mar 2024 21:10:58 +0100 Subject: Add draw_compute_text_size --- src/draw.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/draw.c') diff --git a/src/draw.c b/src/draw.c index 968f111..5a43a51 100644 --- a/src/draw.c +++ b/src/draw.c @@ -165,12 +165,12 @@ static void layout_adjust(GList *start, GList *end, int off) static void layout_set_text(Layout *layout, PangoFontDescription *desc, int height) { g_assert(layout->btn->simple); - const char *text = CAST(layout->btn, ButtonSimple)->text; pango_layout_set_font_description(layout->pl, desc); - pango_layout_set_text(layout->pl, text, -1); pango_layout_set_alignment(layout->pl, PANGO_ALIGN_CENTER); + pango_layout_set_height(layout->pl, 0); + pango_layout_set_text(layout->pl, text, -1); pango_layout_get_pixel_size(layout->pl, &layout->text_w, &layout->text_h); // If there is only one glyph the button should be round @@ -263,7 +263,9 @@ retry: void draw_compute_layout(Drawer *draw, Window *win, GList *btns) { + // Move this somewhere else compute_width(draw, win); + memset(draw->layout_end, 0, sizeof(draw->layout_end)); memset(draw->layout_bx, 0, sizeof(draw->layout_bx)); @@ -304,6 +306,17 @@ void draw_compute_layout(Drawer *draw, Window *win, GList *btns) log_debug("Updated layouts"); } +void draw_compute_text_size(Drawer *draw, const char *text, int *text_w, int *text_h) +{ + PangoLayout *pl = pango_layout_new(draw->context); + pango_layout_set_font_description(pl, draw->desc); + pango_layout_set_height(pl, 0); + pango_layout_set_alignment(pl, PANGO_ALIGN_CENTER); + pango_layout_set_text(pl, text, -1); + pango_layout_get_pixel_size(pl, text_w, text_h); + g_object_unref(pl); +} + void draw_set_background(Drawer *draw, Color background) { draw->background = background; -- cgit v1.2.3