Skip to content

Commit

Permalink
Reuse parsed PangoFontDescription
Browse files Browse the repository at this point in the history
Avoids parsing the configured font each time text is rendered.
  • Loading branch information
Hugo Osvaldo Barrera authored and emersion committed Jul 1, 2022
1 parent 7560549 commit 80e386f
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 49 deletions.
12 changes: 5 additions & 7 deletions common/pango.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ size_t escape_markup_text(const char *src, char *dest) {
return length;
}

PangoLayout *get_pango_layout(cairo_t *cairo, const char *font,
PangoLayout *get_pango_layout(cairo_t *cairo, const PangoFontDescription *desc,
const char *text, double scale, bool markup) {
PangoLayout *layout = pango_cairo_create_layout(cairo);
PangoAttrList *attrs;
Expand All @@ -73,16 +73,14 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font,
}

pango_attr_list_insert(attrs, pango_attr_scale_new(scale));
PangoFontDescription *desc = pango_font_description_from_string(font);
pango_layout_set_font_description(layout, desc);
pango_layout_set_single_paragraph_mode(layout, 1);
pango_layout_set_attributes(layout, attrs);
pango_attr_list_unref(attrs);
pango_font_description_free(desc);
return layout;
}

void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
void get_text_size(cairo_t *cairo, const PangoFontDescription *desc, int *width, int *height,
int *baseline, double scale, bool markup, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
Expand All @@ -99,7 +97,7 @@ void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
vsnprintf(buf, length, fmt, args);
va_end(args);

PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup);
PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup);
pango_cairo_update_layout(cairo, layout);
pango_layout_get_pixel_size(layout, width, height);
if (baseline) {
Expand All @@ -123,7 +121,7 @@ void get_text_metrics(const PangoFontDescription *description, int *height, int
cairo_destroy(cairo);
}

void render_text(cairo_t *cairo, const char *font,
void render_text(cairo_t *cairo, const PangoFontDescription *desc,
double scale, bool markup, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
Expand All @@ -140,7 +138,7 @@ void render_text(cairo_t *cairo, const char *font,
vsnprintf(buf, length, fmt, args);
va_end(args);

PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup);
PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup);
cairo_font_options_t *fo = cairo_font_options_create();
cairo_get_font_options(cairo, fo);
pango_cairo_context_set_font_options(pango_layout_get_context(layout), fo);
Expand Down
8 changes: 4 additions & 4 deletions include/pango.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
* escaped string to dest if provided.
*/
size_t escape_markup_text(const char *src, char *dest);
PangoLayout *get_pango_layout(cairo_t *cairo, const char *font,
PangoLayout *get_pango_layout(cairo_t *cairo, const PangoFontDescription *desc,
const char *text, double scale, bool markup);
void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
void get_text_size(cairo_t *cairo, const PangoFontDescription *desc, int *width, int *height,
int *baseline, double scale, bool markup, const char *fmt, ...);
void get_text_metrics(const PangoFontDescription *font, int *height, int *baseline);
void render_text(cairo_t *cairo, const char *font,
void get_text_metrics(const PangoFontDescription *desc, int *height, int *baseline);
void render_text(cairo_t *cairo, PangoFontDescription *desc,
double scale, bool markup, const char *fmt, ...);

#endif
2 changes: 1 addition & 1 deletion include/sway/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ struct sway_config {
char *floating_scroll_right_cmd;
enum sway_container_layout default_orientation;
enum sway_container_layout default_layout;
char *font; // Use mostly for IPC.
char *font; // Used for IPC.
PangoFontDescription *font_description; // Used internally for rendering and validating.
int font_height;
int font_baseline;
Expand Down
3 changes: 2 additions & 1 deletion include/swaybar/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../include/config.h"
#include "list.h"
#include "util.h"
#include <pango/pangocairo.h>

struct box_colors {
uint32_t border;
Expand All @@ -28,7 +29,7 @@ struct swaybar_config {
char *status_command;
bool pango_markup;
uint32_t position; // zwlr_layer_surface_v1_anchor
char *font;
PangoFontDescription *font_description;
char *sep_symbol;
char *mode;
char *hidden_state;
Expand Down
3 changes: 2 additions & 1 deletion include/swaynag/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
struct swaynag_type {
char *name;

char *font;
char *font; // Used for debugging.
PangoFontDescription *font_description;
char *output;
uint32_t anchors;
int32_t layer; // enum zwlr_layer_shell_v1_layer or -1 if unset
Expand Down
4 changes: 2 additions & 2 deletions sway/tree/container.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ static void render_titlebar_text_texture(struct sway_output *output,
to_cairo_subpixel_order(output->wlr_output->subpixel));
}
cairo_set_font_options(c, fo);
get_text_size(c, config->font, &width, NULL, &baseline, scale,
get_text_size(c, config->font_description, &width, NULL, &baseline, scale,
config->pango_markup, "%s", text);
cairo_surface_destroy(dummy_surface);
cairo_destroy(c);
Expand Down Expand Up @@ -554,7 +554,7 @@ static void render_titlebar_text_texture(struct sway_output *output,
class->text[2], class->text[3]);
cairo_move_to(cairo, 0, config->font_baseline * scale - baseline);

render_text(cairo, config->font, scale, pango_markup, "%s", text);
render_text(cairo, config->font_description, scale, pango_markup, "%s", text);

cairo_surface_flush(surface);
unsigned char *data = cairo_image_surface_get_data(surface);
Expand Down
4 changes: 2 additions & 2 deletions swaybar/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions swaybar/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
}
}
Expand Down
36 changes: 18 additions & 18 deletions swaybar/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions swaynag/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
case 'f': // Font
if (type) {
free(type->font);
pango_font_description_free(type->font_description);
type->font = strdup(optarg);
type->font_description = pango_font_description_from_string(type->font);
}
break;
case 'l': // Detailed Message
Expand Down
Loading

0 comments on commit 80e386f

Please sign in to comment.