]> git.mdlowis.com Git - proto/labwc.git/commitdiff
Deal with font_buffer_create() failures
authorConsolatis <35009135+Consolatis@users.noreply.github.com>
Thu, 26 May 2022 13:34:08 +0000 (15:34 +0200)
committerJohan Malm <johanmalm@users.noreply.github.com>
Thu, 26 May 2022 21:19:56 +0000 (22:19 +0100)
src/common/font.c
src/menu/menu.c
src/ssd/ssd_titlebar.c

index 6925a5047e4ff1d1d693bf31d67f5ca086fdc3d9..47117fae73bb8cc63eeaa082127e417d399af2ed 100644 (file)
@@ -74,6 +74,11 @@ font_buffer_create(struct lab_data_buffer **buffer, int max_width,
        }
        /* TODO: scale */
        *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_surface_t *surf = cairo_get_target(cairo);
index 846125a81cad222f234a872658763c46f8d7fdd7..50bc97243ee575d24b298efc2843469a0492439f 100644 (file)
@@ -103,6 +103,19 @@ item_create(struct menu *menu, const char *text)
                text, &font, theme->menu_items_text_color);
        font_buffer_create(&menuitem->selected.buffer, item_max_width,
                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 */
        menuitem->normal.background = &wlr_scene_rect_create(parent,
index 768936a0769c11ae917f887c5fe2b2f1c6c46514..7a80a0a79d62019388d0dabf13fe72ff965da15b 100644 (file)
@@ -150,6 +150,7 @@ ssd_update_title_positions(struct view *view)
        int full_width = width + 2 * view->server->theme->border_width;
 
        int x, y;
+       int buffer_height, buffer_width;
        struct wlr_scene_rect *rect;
        struct ssd_part *part;
        struct ssd_sub_tree *subtree;
@@ -160,8 +161,10 @@ ssd_update_title_positions(struct view *view)
                        continue;
                }
 
+               buffer_width = part->buffer ? part->buffer->base.width : 0;
+               buffer_height = part->buffer ? part->buffer->base.height : 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);
                if (rect->width <= 0) {
                        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 (part->buffer->base.width + BUTTON_WIDTH * 2 <= rect->width) {
+                       if (buffer_width + BUTTON_WIDTH * 2 <= rect->width) {
                                /* Center based on the full width */
-                               x = (full_width - part->buffer->base.width) / 2;
+                               x = (full_width - buffer_width) / 2;
                                x -= BUTTON_WIDTH;
                        } else {
                                /*
@@ -179,10 +182,10 @@ ssd_update_title_positions(struct view *view)
                                 * Title jumps around once this is hit but its still
                                 * 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) {
-                       x = rect->width - part->buffer->base.width;
+                       x = rect->width - buffer_width;
                } else if (theme->window_label_text_justify == LAB_JUSTIFY_LEFT) {
                        /* 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);
                if (!part) {
+                       /* Initialize part and wlr_scene_buffer without attaching a buffer */
                        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);
-               part->node = &wlr_scene_buffer_create(
-                       parent_part->node, &part->buffer->base)->node;
-               dstate->width = part->buffer->base.width;
+
+               /* (Re)set the buffer */
+               wlr_scene_buffer_set_buffer(
+                       wlr_scene_buffer_from_node(part->node),
+                       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;
        } FOR_EACH_END