From 985c557dcda88f21063659321ca668ad1c0822e4 Mon Sep 17 00:00:00 2001 From: o9000 Date: Sun, 18 Oct 2015 16:51:08 +0200 Subject: [PATCH] Refactored text size computation --- src/battery/battery.c | 10 +++++++-- src/clock/clock.c | 10 +++++++-- src/taskbar/taskbar.c | 4 ++-- src/taskbar/taskbarname.c | 6 +++++- src/util/window.c | 44 ++++++++++++++------------------------- src/util/window.h | 12 +++++++++-- 6 files changed, 49 insertions(+), 37 deletions(-) diff --git a/src/battery/battery.c b/src/battery/battery.c index 7e67aec..67114ed 100644 --- a/src/battery/battery.c +++ b/src/battery/battery.c @@ -267,6 +267,8 @@ void draw_battery (void *obj, cairo_t *c) pango_layout_set_font_description(layout, bat2_font_desc); pango_layout_set_indent(layout, 0); + pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); + pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); pango_layout_set_text(layout, buf_bat_time, strlen(buf_bat_time)); pango_layout_set_width(layout, battery->area.width * PANGO_SCALE); @@ -295,9 +297,13 @@ int resize_battery(void *obj) snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes); } get_text_size2(bat1_font_desc, &bat_percentage_height_ink, &bat_percentage_height, &bat_percentage_width, - panel->area.height, panel->area.width, buf_bat_percentage, strlen(buf_bat_percentage)); + panel->area.height, panel->area.width, buf_bat_percentage, strlen(buf_bat_percentage), + PANGO_WRAP_WORD_CHAR, + PANGO_ELLIPSIZE_NONE); get_text_size2(bat2_font_desc, &bat_time_height_ink, &bat_time_height, &bat_time_width, - panel->area.height, panel->area.width, buf_bat_time, strlen(buf_bat_time)); + panel->area.height, panel->area.width, buf_bat_time, strlen(buf_bat_time), + PANGO_WRAP_WORD_CHAR, + PANGO_ELLIPSIZE_NONE); if (panel_horizontal) { int new_size = (bat_percentage_width > bat_time_width) ? bat_percentage_width : bat_time_width; diff --git a/src/clock/clock.c b/src/clock/clock.c index 91dda59..e62fb6b 100644 --- a/src/clock/clock.c +++ b/src/clock/clock.c @@ -213,6 +213,8 @@ void draw_clock (void *obj, cairo_t *c) pango_layout_set_font_description (layout, time1_font_desc); pango_layout_set_width (layout, clock->area.width * PANGO_SCALE); pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER); + pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); + pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); pango_layout_set_text (layout, buf_time, strlen(buf_time)); cairo_set_source_rgba (c, clock->font.color[0], clock->font.color[1], clock->font.color[2], clock->font.alpha); @@ -244,10 +246,14 @@ int resize_clock (void *obj) date_height = date_width = 0; strftime(buf_time, sizeof(buf_time), time1_format, clock_gettime_for_tz(time1_timezone)); - get_text_size2(time1_font_desc, &time_height_ink, &time_height, &time_width, panel->area.height, panel->area.width, buf_time, strlen(buf_time)); + get_text_size2(time1_font_desc, &time_height_ink, &time_height, &time_width, panel->area.height, panel->area.width, buf_time, strlen(buf_time), + PANGO_WRAP_WORD_CHAR, + PANGO_ELLIPSIZE_NONE); if (time2_format) { strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone)); - get_text_size2(time2_font_desc, &date_height_ink, &date_height, &date_width, panel->area.height, panel->area.width, buf_date, strlen(buf_date)); + get_text_size2(time2_font_desc, &date_height_ink, &date_height, &date_width, panel->area.height, panel->area.width, buf_date, strlen(buf_date), + PANGO_WRAP_WORD_CHAR, + PANGO_ELLIPSIZE_NONE); } if (panel_horizontal) { diff --git a/src/taskbar/taskbar.c b/src/taskbar/taskbar.c index 9be17f2..d55d3ab 100644 --- a/src/taskbar/taskbar.c +++ b/src/taskbar/taskbar.c @@ -232,8 +232,8 @@ void init_taskbar_panel(void *p) } // compute vertical position : text and icon - int height_ink, height; - get_text_size(panel->g_task.font_desc, &height_ink, &height, panel->area.height, "TAjpg", 5); + int height_ink, height, width; + get_text_size2(panel->g_task.font_desc, &height_ink, &height, &width, panel->area.height, panel->area.width, "TAjpg", 5, PANGO_WRAP_WORD_CHAR, PANGO_ELLIPSIZE_END); if (!panel->g_task.maximum_width && panel_horizontal) panel->g_task.maximum_width = server.monitor[panel->monitor].width; diff --git a/src/taskbar/taskbarname.c b/src/taskbar/taskbarname.c index e97e502..4d26c15 100644 --- a/src/taskbar/taskbarname.c +++ b/src/taskbar/taskbarname.c @@ -124,6 +124,8 @@ void draw_taskbarname (void *obj, cairo_t *c) pango_layout_set_font_description (layout, panel_config.taskbarname_font_desc); pango_layout_set_width (layout, taskbar_name->area.width * PANGO_SCALE); pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER); + pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); + pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); pango_layout_set_text (layout, taskbar_name->name, strlen(taskbar_name->name)); cairo_set_source_rgba (c, config_text->color[0], config_text->color[1], config_text->color[2], config_text->alpha); @@ -144,7 +146,9 @@ int resize_taskbarname(void *obj) int ret = 0; taskbar_name->area.redraw = 1; - get_text_size2(panel_config.taskbarname_font_desc, &name_height_ink, &name_height, &name_width, panel->area.height, panel->area.width, taskbar_name->name, strlen(taskbar_name->name)); + get_text_size2(panel_config.taskbarname_font_desc, &name_height_ink, &name_height, &name_width, panel->area.height, panel->area.width, taskbar_name->name, strlen(taskbar_name->name), + PANGO_WRAP_WORD_CHAR, + PANGO_ELLIPSIZE_NONE); if (panel_horizontal) { int new_size = name_width + (2* (taskbar_name->area.paddingxlr + taskbar_name->area.bg->border.width)); diff --git a/src/util/window.c b/src/util/window.c index 5ae8082..ec61b6b 100644 --- a/src/util/window.c +++ b/src/util/window.c @@ -321,41 +321,29 @@ gulong *get_best_icon (gulong *data, int icon_count, int num, int *iw, int *ih, } -void get_text_size(PangoFontDescription *font, int *height_ink, int *height, int panel_height, char *text, int len) +void get_text_size2(PangoFontDescription *font, + int *height_ink, + int *height, + int *width, + int panel_height, + int panel_width, + char *text, + int len, + PangoWrapMode wrap, + PangoEllipsizeMode ellipsis) { PangoRectangle rect_ink, rect; - Pixmap pmap = XCreatePixmap (server.dsp, server.root_win, panel_height, panel_height, server.depth); + Pixmap pmap = XCreatePixmap (server.dsp, server.root_win, panel_height, panel_width, server.depth); - cairo_surface_t *cs = cairo_xlib_surface_create (server.dsp, pmap, server.visual, panel_height, panel_height); - cairo_t *c = cairo_create (cs); - - PangoLayout *layout = pango_cairo_create_layout (c); - pango_layout_set_font_description (layout, font); - pango_layout_set_text (layout, text, len); - - pango_layout_get_pixel_extents(layout, &rect_ink, &rect); - *height_ink = rect_ink.height; - *height = rect.height; - //printf("dimension : %d - %d\n", rect_ink.height, rect.height); - - g_object_unref (layout); - cairo_destroy (c); - cairo_surface_destroy (cs); - XFreePixmap (server.dsp, pmap); -} - - -void get_text_size2(PangoFontDescription *font, int *height_ink, int *height, int *width, int panel_height, int panel_with, char *text, int len) -{ - PangoRectangle rect_ink, rect; - - Pixmap pmap = XCreatePixmap (server.dsp, server.root_win, panel_height, panel_height, server.depth); - - cairo_surface_t *cs = cairo_xlib_surface_create (server.dsp, pmap, server.visual, panel_height, panel_with); + cairo_surface_t *cs = cairo_xlib_surface_create (server.dsp, pmap, server.visual, panel_height, panel_width); cairo_t *c = cairo_create (cs); PangoLayout *layout = pango_cairo_create_layout (c); + pango_layout_set_width(layout, panel_width * PANGO_SCALE); + pango_layout_set_height(layout, panel_height * PANGO_SCALE); + pango_layout_set_wrap(layout, wrap); + pango_layout_set_ellipsize(layout, ellipsis); pango_layout_set_font_description (layout, font); pango_layout_set_text (layout, text, len); diff --git a/src/util/window.h b/src/util/window.h index de90e9b..3e3c1d5 100644 --- a/src/util/window.h +++ b/src/util/window.h @@ -33,8 +33,16 @@ void windows_set_desktop (Window win, int desktop); int window_get_monitor (Window win); Window window_get_active (); -void get_text_size(PangoFontDescription *font, int *height_ink, int *height, int panel_height, char *text, int len); -void get_text_size2(PangoFontDescription *font, int *height_ink, int *height, int *width, int panel_height, int panel_with, char *text, int len); +void get_text_size2(PangoFontDescription *font, + int *height_ink, + int *height, + int *width, + int panel_height, + int panel_with, + char *text, + int len, + PangoWrapMode wrap, + PangoEllipsizeMode ellipsis); #endif