Deal with font_buffer_create() failures

This commit is contained in:
Consolatis 2022-05-26 15:34:08 +02:00 committed by Johan Malm
parent 94f199e3c5
commit bda48da68d
3 changed files with 42 additions and 12 deletions

View file

@ -74,6 +74,11 @@ font_buffer_create(struct lab_data_buffer **buffer, int max_width,
} }
/* TODO: scale */ /* TODO: scale */
*buffer = buffer_create_cairo(rect.width, rect.height, 1, true); *buffer = buffer_create_cairo(rect.width, rect.height, 1, true);
if (!*buffer) {
wlr_log(WLR_ERROR, "Failed to create font buffer of size %dx%d",
rect.width, rect.height);
return;
}
cairo_t *cairo = (*buffer)->cairo; cairo_t *cairo = (*buffer)->cairo;
cairo_surface_t *surf = cairo_get_target(cairo); cairo_surface_t *surf = cairo_get_target(cairo);

View file

@ -103,6 +103,19 @@ item_create(struct menu *menu, const char *text)
text, &font, theme->menu_items_text_color); text, &font, theme->menu_items_text_color);
font_buffer_create(&menuitem->selected.buffer, item_max_width, font_buffer_create(&menuitem->selected.buffer, item_max_width,
text, &font, theme->menu_items_active_text_color); text, &font, theme->menu_items_active_text_color);
if (!menuitem->normal.buffer || !menuitem->selected.buffer) {
wlr_log(WLR_ERROR, "Failed to create menu item '%s'", text);
if (menuitem->normal.buffer) {
wlr_buffer_drop(&menuitem->normal.buffer->base);
menuitem->normal.buffer = NULL;
}
if (menuitem->selected.buffer) {
wlr_buffer_drop(&menuitem->selected.buffer->base);
menuitem->selected.buffer = NULL;
}
free(menuitem);
return NULL;
}
/* Item background nodes */ /* Item background nodes */
menuitem->normal.background = &wlr_scene_rect_create(parent, menuitem->normal.background = &wlr_scene_rect_create(parent,

View file

@ -150,6 +150,7 @@ ssd_update_title_positions(struct view *view)
int full_width = width + 2 * view->server->theme->border_width; int full_width = width + 2 * view->server->theme->border_width;
int x, y; int x, y;
int buffer_height, buffer_width;
struct wlr_scene_rect *rect; struct wlr_scene_rect *rect;
struct ssd_part *part; struct ssd_part *part;
struct ssd_sub_tree *subtree; struct ssd_sub_tree *subtree;
@ -160,8 +161,10 @@ ssd_update_title_positions(struct view *view)
continue; continue;
} }
buffer_width = part->buffer ? part->buffer->base.width : 0;
buffer_height = part->buffer ? part->buffer->base.height : 0;
x = 0; x = 0;
y = (theme->title_height - part->buffer->base.height) / 2; y = (theme->title_height - buffer_height) / 2;
rect = lab_wlr_scene_get_rect(part->node->parent); rect = lab_wlr_scene_get_rect(part->node->parent);
if (rect->width <= 0) { if (rect->width <= 0) {
wlr_scene_node_set_position(part->node, x, y); wlr_scene_node_set_position(part->node, x, y);
@ -169,9 +172,9 @@ ssd_update_title_positions(struct view *view)
} }
if (theme->window_label_text_justify == LAB_JUSTIFY_CENTER) { if (theme->window_label_text_justify == LAB_JUSTIFY_CENTER) {
if (part->buffer->base.width + BUTTON_WIDTH * 2 <= rect->width) { if (buffer_width + BUTTON_WIDTH * 2 <= rect->width) {
/* Center based on the full width */ /* Center based on the full width */
x = (full_width - part->buffer->base.width) / 2; x = (full_width - buffer_width) / 2;
x -= BUTTON_WIDTH; x -= BUTTON_WIDTH;
} else { } else {
/* /*
@ -179,10 +182,10 @@ ssd_update_title_positions(struct view *view)
* Title jumps around once this is hit but its still * Title jumps around once this is hit but its still
* better than to hide behind the buttons on the right. * better than to hide behind the buttons on the right.
*/ */
x = (rect->width - part->buffer->base.width) / 2; x = (rect->width - buffer_width) / 2;
} }
} else if (theme->window_label_text_justify == LAB_JUSTIFY_RIGHT) { } else if (theme->window_label_text_justify == LAB_JUSTIFY_RIGHT) {
x = rect->width - part->buffer->base.width; x = rect->width - buffer_width;
} else if (theme->window_label_text_justify == LAB_JUSTIFY_LEFT) { } else if (theme->window_label_text_justify == LAB_JUSTIFY_LEFT) {
/* TODO: maybe add some theme x padding here? */ /* TODO: maybe add some theme x padding here? */
} }
@ -244,16 +247,25 @@ ssd_update_title(struct view *view)
part = ssd_get_part(&subtree->parts, LAB_SSD_PART_TITLE); part = ssd_get_part(&subtree->parts, LAB_SSD_PART_TITLE);
if (!part) { if (!part) {
/* Initialize part and wlr_scene_buffer without attaching a buffer */
part = add_scene_part(&subtree->parts, LAB_SSD_PART_TITLE); part = add_scene_part(&subtree->parts, LAB_SSD_PART_TITLE);
part->node = &wlr_scene_buffer_create(parent_part->node, NULL)->node;
} }
if (part->node) {
wlr_scene_node_destroy(part->node); /* Generate and update the lab_data_buffer, drops the old buffer */
font_buffer_update(&part->buffer, rect->width, title, &font, text_color);
if (!part->buffer) {
/* This can happen for example by defining a font size of 0 */
wlr_log(WLR_ERROR, "Failed to create title buffer");
} }
font_buffer_update(
&part->buffer, rect->width, title, &font, text_color); /* (Re)set the buffer */
part->node = &wlr_scene_buffer_create( wlr_scene_buffer_set_buffer(
parent_part->node, &part->buffer->base)->node; wlr_scene_buffer_from_node(part->node),
dstate->width = part->buffer->base.width; part->buffer ? &part->buffer->base : NULL);
/* And finally update the cache */
dstate->width = part->buffer ? part->buffer->base.width : 0;
dstate->truncated = rect->width <= dstate->width; dstate->truncated = rect->width <= dstate->width;
} FOR_EACH_END } FOR_EACH_END