From 186b0e7be01868821907bd76cc83f6a3d4c66f9f Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Thu, 3 Oct 2024 17:32:53 +0200 Subject: [PATCH] [Textbox] Use FontMetrics font height (if available) to get height --- source/widgets/textbox.c | 30 +++++++++++++++++++++++++++--- test/textbox-test.c | 6 ++++++ test/widget-test.c | 7 +++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/source/widgets/textbox.c b/source/widgets/textbox.c index 592504039..0c36d1286 100644 --- a/source/widgets/textbox.c +++ b/source/widgets/textbox.c @@ -31,6 +31,7 @@ #include "helper.h" #include "keyb.h" #include "mode.h" +#include "timings.h" #include "view.h" #include "widgets/textbox.h" #include @@ -140,6 +141,22 @@ static void textbox_initialize_font(textbox *tb) { PangoRectangle rect; pango_layout_get_pixel_extents(layout, NULL, &rect); tbfc->height = rect.y + rect.height; + + // Try to find height from font. Might be slow? + TICK_N("Get font height"); + PangoFont *font = pango_context_load_font(p_context, tbfc->pfd); + if (font) { + PangoFontMetrics *fm = pango_font_get_metrics(font, NULL); + if (fm) { + int h = pango_font_metrics_get_height(fm) / PANGO_SCALE; + if (h > 0) { + tbfc->height = h; + } + pango_font_metrics_unref(fm); + } + g_object_unref(font); + } + TICK_N("Get font height"); g_object_unref(layout); // Cast away consts. (*yuck*) because table_insert does not know it is @@ -502,9 +519,10 @@ static void textbox_draw(widget *wid, cairo_t *draw) { // use text color as fallback for themes that don't specify the cursor color rofi_theme_get_color(WIDGET(tb), "text-color", draw); - { int rem = - MAX(0, tb->widget.w - widget_padding_get_padding_width(WIDGET(tb)) - - line_width - dot_offset); + { + int rem = + MAX(0, tb->widget.w - widget_padding_get_padding_width(WIDGET(tb)) - + line_width - dot_offset); switch (pango_layout_get_alignment(tb->layout)) { case PANGO_ALIGN_CENTER: x = rem * (tb->xalign - 0.5); @@ -942,6 +960,12 @@ void textbox_set_pango_context(const char *font, PangoContext *p) { PangoRectangle rect; pango_layout_get_pixel_extents(layout, NULL, &rect); tbfc->height = rect.y + rect.height; + if (tbfc->metrics) { + int h = pango_font_metrics_get_height(tbfc->metrics) / PANGO_SCALE; + if (h > 0) { + tbfc->height = h; + } + } g_object_unref(layout); tbfc_default = tbfc; diff --git a/test/textbox-test.c b/test/textbox-test.c index 8ae3c9b6f..b52f26ac0 100644 --- a/test/textbox-test.c +++ b/test/textbox-test.c @@ -58,6 +58,12 @@ int rofi_is_in_dmenu_mode = 0; ThemeWidget *rofi_configuration = NULL; +void rofi_timings_tick(G_GNUC_UNUSED const char *file, + G_GNUC_UNUSED char const *str, G_GNUC_UNUSED int line, + G_GNUC_UNUSED char const *msg); +void rofi_timings_tick(G_GNUC_UNUSED const char *file, + G_GNUC_UNUSED char const *str, G_GNUC_UNUSED int line, + G_GNUC_UNUSED char const *msg) {} uint32_t rofi_icon_fetcher_query(G_GNUC_UNUSED const char *name, G_GNUC_UNUSED const int size) { return 0; diff --git a/test/widget-test.c b/test/widget-test.c index 29783d673..10c44ec62 100644 --- a/test/widget-test.c +++ b/test/widget-test.c @@ -26,6 +26,7 @@ */ #include "display.h" +#include "glibconfig.h" #include "rofi-icon-fetcher.h" #include "rofi.h" #include "xcb.h" @@ -58,6 +59,12 @@ uint32_t rofi_icon_fetcher_query_advanced(G_GNUC_UNUSED const char *name, G_GNUC_UNUSED const int hsize) { return 0; } +void rofi_timings_tick(G_GNUC_UNUSED const char *file, + G_GNUC_UNUSED char const *str, G_GNUC_UNUSED int line, + G_GNUC_UNUSED char const *msg); +void rofi_timings_tick(G_GNUC_UNUSED const char *file, + G_GNUC_UNUSED char const *str, G_GNUC_UNUSED int line, + G_GNUC_UNUSED char const *msg) {} cairo_surface_t *rofi_icon_fetcher_get(G_GNUC_UNUSED const uint32_t uid) { return NULL;