aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-03-16 21:10:58 +0100
committerFederico Angelilli <code@fedang.net>2024-03-16 21:10:58 +0100
commit4794a57face4e6e521c8a60a120ebf00bbc8a94f (patch)
tree8fe933c87ce176cf0b641e2d17efeda3ca0fa2d2 /src
parentfe293d9503a57df0236be85cc82093074e7bbccb (diff)
Add draw_compute_text_size
Diffstat (limited to 'src')
-rw-r--r--src/draw.c17
-rw-r--r--src/draw.h2
2 files changed, 17 insertions, 2 deletions
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;
diff --git a/src/draw.h b/src/draw.h
index 6851e87..6a14ad1 100644
--- a/src/draw.h
+++ b/src/draw.h
@@ -48,6 +48,8 @@ void draw_paint(Drawer *draw, Window *win);
// TODO: Rework the api so that we don't need to pass the window
void draw_compute_layout(Drawer *draw, Window *win, GList *btns);
+void draw_compute_text_size(Drawer *draw, const char *text, int *text_w, int *text_h);
+
void draw_set_background(Drawer *draw, Color background);
void draw_set_separator(Drawer *draw, int sep);