Fix crash when long window titles are used

cairo_image_surface_create() return CAIRO_STATUS_INVALID_SIZE on too
long window titles and creates then no surface. Therefore it makes
sense to limit the window title length to a reasonable length.

Fix: https://github.com/swaywm/sway/issues/6531

Signed-off-by: Felix Weilbach <felix.weilbach@t-online.de>
This commit is contained in:
Felix Weilbach 2021-09-26 13:41:50 +02:00
parent 02b412a3d4
commit a2763262d4
3 changed files with 38 additions and 2 deletions

View file

@ -152,3 +152,33 @@ void render_text(cairo_t *cairo, const char *font,
g_object_unref(layout);
free(buf);
}
void render_text_ellipsize(cairo_t *cairo, const char *font, double scale,
bool markup, double width, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
// Add one since vsnprintf excludes null terminator.
int length = vsnprintf(NULL, 0, fmt, args) + 1;
va_end(args);
char *buf = malloc(length);
if (buf == NULL) {
sway_log(SWAY_ERROR, "Failed to allocate memory");
return;
}
va_start(args, fmt);
vsnprintf(buf, length, fmt, args);
va_end(args);
PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup);
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
pango_layout_set_width(layout, width * PANGO_SCALE);
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);
cairo_font_options_destroy(fo);
pango_cairo_update_layout(cairo, layout);
pango_cairo_show_layout(cairo, layout);
g_object_unref(layout);
free(buf);
}