From 6198c03746c3adcb1cf54b9c89440304a21c0e6f Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Sun, 2 Jan 2022 17:42:39 -0500 Subject: [PATCH] Change text rendering from cairo to pango --- src/batmon.c | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/batmon.c b/src/batmon.c index 024a763..203bc07 100644 --- a/src/batmon.c +++ b/src/batmon.c @@ -62,6 +62,10 @@ #define BAT_LOW_LEVEL 20 #define BAT_CRITICAL_LEVEL 10 +#define FONT_NAME "Sans Condensed Bold" +#define FONT_SIZE 9 + +static int font_size; static int capacity; static int ac_online = false; static const char battery[] = "BAT0"; @@ -172,19 +176,30 @@ color_set_yellow(cairo_t *cr) } static void -display_string(cairo_t *cr, char *str, int center_x, int center_y) +display_string_pango(cairo_t *cr, char *str, int center_x, int center_y) { - cairo_text_extents_t extents; - double xx, yy; + PangoLayout *layout; + PangoFontDescription *desc; + int x, y; + char font[256]; + + sprintf(font, "%s %d", FONT_NAME, font_size); + + /* Create a PangoLayout, set the font and text */ + layout = pango_cairo_create_layout(cr); + pango_layout_set_text(layout, str, -1); + desc = pango_font_description_from_string(font); + pango_layout_set_font_description(layout, desc); + pango_font_description_free(desc); /* Get string dimensions */ - cairo_text_extents(cr, str, &extents); + pango_layout_get_pixel_size(layout, &x, &y); - xx = (extents.width / 2) + 1; - yy = extents.height / 2; + cairo_move_to(cr, center_x + 1 - (x / 2), center_y - (y / 2)); - cairo_move_to(cr, center_x - xx, center_y + yy); - cairo_show_text(cr, str); + pango_cairo_show_layout(cr, layout); + + g_object_unref(layout); } static void @@ -194,15 +209,13 @@ display_capacity(cairo_t *cr) int center_x = 32; int center_y = 32; - cairo_select_font_face(cr, "serif", CAIRO_FONT_SLANT_NORMAL, - CAIRO_FONT_WEIGHT_BOLD); - cairo_set_font_size(cr, 10); + font_size = FONT_SIZE; /* Convert digit to string */ - if (capacity == 100) - sprintf(str, "F"); - else - sprintf(str, "%d%%", capacity); + if (capacity >= 100) + font_size -= 1; + + sprintf(str, "%d%%", capacity); if (capacity <= 40) { color_set_white(cr); @@ -212,7 +225,7 @@ display_capacity(cairo_t *cr) center_y += 15; } - display_string(cr, str, center_x, center_y); + display_string_pango(cr, str, center_x, center_y); } static void -- 2.20.1