aboutsummaryrefslogtreecommitdiff
path: root/swaybar
diff options
context:
space:
mode:
authorHugo Osvaldo Barrera <hugo@barrera.io>2022-07-01 12:23:04 +0200
committerSimon Ser <contact@emersion.fr>2022-07-01 13:05:58 +0200
commit80e386fd97c0da00970f0acc007574151048cfbf (patch)
treedf11d53184b20e3f1afe86b8272420349fd9b552 /swaybar
parent75605491a54f8647740fdba75dd2ad7bae9e0ca7 (diff)
Reuse parsed PangoFontDescription
Avoids parsing the configured font each time text is rendered.
Diffstat (limited to 'swaybar')
-rw-r--r--swaybar/config.c4
-rw-r--r--swaybar/ipc.c9
-rw-r--r--swaybar/render.c36
3 files changed, 25 insertions, 24 deletions
diff --git a/swaybar/config.c b/swaybar/config.c
index abedaec0..5e828773 100644
--- a/swaybar/config.c
+++ b/swaybar/config.c
@@ -26,7 +26,7 @@ struct swaybar_config *init_config(void) {
config->status_command = NULL;
config->pango_markup = false;
config->position = parse_position("bottom");
- config->font = strdup("monospace 10");
+ config->font_description = pango_font_description_from_string("monospace 10");
config->mode = strdup("dock");
config->hidden_state = strdup("hide");
config->sep_symbol = NULL;
@@ -105,7 +105,7 @@ void free_tray_binding(struct tray_binding *binding) {
void free_config(struct swaybar_config *config) {
free(config->status_command);
- free(config->font);
+ pango_font_description_free(config->font_description);
free(config->mode);
free(config->hidden_state);
free(config->sep_symbol);
diff --git a/swaybar/ipc.c b/swaybar/ipc.c
index 2cb235bf..9d81a9fb 100644
--- a/swaybar/ipc.c
+++ b/swaybar/ipc.c
@@ -147,8 +147,10 @@ static bool ipc_parse_config(
json_object *font = json_object_object_get(bar_config, "font");
if (font) {
- free(config->font);
- config->font = parse_font(json_object_get_string(font));
+ pango_font_description_free(config->font_description);
+ char *font_value = parse_font(json_object_get_string(font));
+ config->font_description = pango_font_description_from_string(font_value);
+ free(font_value);
}
json_object *gaps = json_object_object_get(bar_config, "gaps");
@@ -485,8 +487,7 @@ static bool handle_barconfig_update(struct swaybar *bar, const char *payload,
destroy_layer_surface(output);
wl_list_remove(&output->link);
wl_list_insert(&bar->unused_outputs, &output->link);
- } else if (!oldcfg->font || !newcfg->font ||
- strcmp(oldcfg->font, newcfg->font) != 0) {
+ } else if (!pango_font_description_equal(oldcfg->font_description, newcfg->font_description)) {
output->height = 0; // force update height
}
}
diff --git a/swaybar/render.c b/swaybar/render.c
index 7e2f97b7..a878805e 100644
--- a/swaybar/render.c
+++ b/swaybar/render.c
@@ -62,7 +62,7 @@ static uint32_t render_status_line_error(struct render_context *ctx, double *x)
int margin = 3;
double ws_vertical_padding = output->bar->config->status_padding;
- char *font = output->bar->config->font;
+ PangoFontDescription *font = output->bar->config->font_description;
int text_width, text_height;
get_text_size(cairo, font, &text_width, &text_height, NULL,
1, false, "%s", error);
@@ -97,7 +97,7 @@ static uint32_t render_status_line_text(struct render_context *ctx, double *x) {
cairo_set_source_u32(cairo, fontcolor);
int text_width, text_height;
- get_text_size(cairo, config->font, &text_width, &text_height, NULL,
+ get_text_size(cairo, config->font_description, &text_width, &text_height, NULL,
1, config->pango_markup, "%s", text);
double ws_vertical_padding = config->status_padding;
@@ -115,7 +115,7 @@ static uint32_t render_status_line_text(struct render_context *ctx, double *x) {
double text_y = height / 2.0 - text_height / 2.0;
cairo_move_to(cairo, *x, (int)floor(text_y));
choose_text_aa_mode(ctx, fontcolor);
- render_text(cairo, config->font, 1, config->pango_markup, "%s", text);
+ render_text(cairo, config->font_description, 1, config->pango_markup, "%s", text);
*x -= margin;
return output->height;
}
@@ -190,7 +190,7 @@ static uint32_t render_status_block(struct render_context *ctx,
struct swaybar_output *output = ctx->output;
struct swaybar_config *config = output->bar->config;
int text_width, text_height;
- get_text_size(cairo, config->font, &text_width, &text_height, NULL, 1,
+ get_text_size(cairo, config->font_description, &text_width, &text_height, NULL, 1,
block->markup, "%s", text);
int margin = 3;
@@ -199,7 +199,7 @@ static uint32_t render_status_block(struct render_context *ctx,
int width = text_width;
if (block->min_width_str) {
int w;
- get_text_size(cairo, config->font, &w, NULL, NULL, 1, block->markup,
+ get_text_size(cairo, config->font_description, &w, NULL, NULL, 1, block->markup,
"%s", block->min_width_str);
block->min_width = w;
}
@@ -229,7 +229,7 @@ static uint32_t render_status_block(struct render_context *ctx,
int sep_block_width = block->separator_block_width;
if (!edge) {
if (config->sep_symbol) {
- get_text_size(cairo, config->font, &sep_width, &sep_height, NULL,
+ get_text_size(cairo, config->font_description, &sep_width, &sep_height, NULL,
1, false, "%s", config->sep_symbol);
uint32_t _ideal_height = sep_height + ws_vertical_padding * 2;
uint32_t _ideal_surface_height = _ideal_height;
@@ -307,7 +307,7 @@ static uint32_t render_status_block(struct render_context *ctx,
color = block->urgent ? config->colors.urgent_workspace.text : color;
cairo_set_source_u32(cairo, color);
choose_text_aa_mode(ctx, color);
- render_text(cairo, config->font, 1, block->markup, "%s", text);
+ render_text(cairo, config->font_description, 1, block->markup, "%s", text);
x_pos += width;
if (block->border_set || block->urgent) {
@@ -331,7 +331,7 @@ static uint32_t render_status_block(struct render_context *ctx,
double sep_y = height / 2.0 - sep_height / 2.0;
cairo_move_to(cairo, offset, (int)floor(sep_y));
choose_text_aa_mode(ctx, color);
- render_text(cairo, config->font, 1, false,
+ render_text(cairo, config->font_description, 1, false,
"%s", config->sep_symbol);
} else {
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
@@ -354,7 +354,7 @@ static void predict_status_block_pos(cairo_t *cairo,
struct swaybar_config *config = output->bar->config;
int text_width, text_height;
- get_text_size(cairo, config->font, &text_width, &text_height, NULL, 1,
+ get_text_size(cairo, config->font_description, &text_width, &text_height, NULL, 1,
block->markup, "%s", block->full_text);
int margin = 3;
@@ -364,7 +364,7 @@ static void predict_status_block_pos(cairo_t *cairo,
if (block->min_width_str) {
int w;
- get_text_size(cairo, config->font, &w, NULL, NULL,
+ get_text_size(cairo, config->font_description, &w, NULL, NULL,
1, block->markup, "%s", block->min_width_str);
block->min_width = w;
}
@@ -391,7 +391,7 @@ static void predict_status_block_pos(cairo_t *cairo,
int sep_block_width = block->separator_block_width;
if (!edge) {
if (config->sep_symbol) {
- get_text_size(cairo, config->font, &sep_width, &sep_height, NULL,
+ get_text_size(cairo, config->font_description, &sep_width, &sep_height, NULL,
1, false, "%s", config->sep_symbol);
uint32_t _ideal_height = sep_height + ws_vertical_padding * 2;
uint32_t _ideal_surface_height = _ideal_height;
@@ -426,7 +426,7 @@ static uint32_t predict_workspace_button_length(cairo_t *cairo,
struct swaybar_config *config = output->bar->config;
int text_width, text_height;
- get_text_size(cairo, config->font, &text_width, &text_height, NULL, 1,
+ get_text_size(cairo, config->font_description, &text_width, &text_height, NULL, 1,
config->pango_markup, "%s", ws->label);
int ws_vertical_padding = WS_VERTICAL_PADDING;
@@ -474,7 +474,7 @@ static uint32_t predict_binding_mode_indicator_length(cairo_t *cairo,
}
int text_width, text_height;
- get_text_size(cairo, config->font, &text_width, &text_height, NULL,
+ get_text_size(cairo, config->font_description, &text_width, &text_height, NULL,
1, output->bar->mode_pango_markup,
"%s", mode);
@@ -551,7 +551,7 @@ static uint32_t render_binding_mode_indicator(struct render_context *ctx,
cairo_t *cairo = ctx->cairo;
struct swaybar_config *config = output->bar->config;
int text_width, text_height;
- get_text_size(cairo, config->font, &text_width, &text_height, NULL,
+ get_text_size(cairo, config->font_description, &text_width, &text_height, NULL,
1, output->bar->mode_pango_markup,
"%s", mode);
@@ -592,7 +592,7 @@ static uint32_t render_binding_mode_indicator(struct render_context *ctx,
cairo_set_source_u32(cairo, config->colors.binding_mode.text);
cairo_move_to(cairo, x + width / 2 - text_width / 2, (int)floor(text_y));
choose_text_aa_mode(ctx, config->colors.binding_mode.text);
- render_text(cairo, config->font, 1, output->bar->mode_pango_markup,
+ render_text(cairo, config->font_description, 1, output->bar->mode_pango_markup,
"%s", mode);
return output->height;
}
@@ -626,7 +626,7 @@ static uint32_t render_workspace_button(struct render_context *ctx,
cairo_t *cairo = ctx->cairo;
int text_width, text_height;
- get_text_size(cairo, config->font, &text_width, &text_height, NULL,
+ get_text_size(cairo, config->font_description, &text_width, &text_height, NULL,
1, config->pango_markup, "%s", ws->label);
int ws_vertical_padding = WS_VERTICAL_PADDING;
@@ -666,7 +666,7 @@ static uint32_t render_workspace_button(struct render_context *ctx,
cairo_set_source_u32(cairo, box_colors.text);
cairo_move_to(cairo, *x + width / 2 - text_width / 2, (int)floor(text_y));
choose_text_aa_mode(ctx, box_colors.text);
- render_text(cairo, config->font, 1, config->pango_markup,
+ render_text(cairo, config->font_description, 1, config->pango_markup,
"%s", ws->label);
struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot));
@@ -699,7 +699,7 @@ static uint32_t render_to_cairo(struct render_context *ctx) {
cairo_paint(cairo);
int th;
- get_text_size(cairo, config->font, NULL, &th, NULL, 1, false, "");
+ get_text_size(cairo, config->font_description, NULL, &th, NULL, 1, false, "");
uint32_t max_height = (th + WS_VERTICAL_PADDING * 4);
/*
* Each render_* function takes the actual height of the bar, and returns