font: remove arrow character from font buffer

Arrow signs are specific to submenu items, so they would be more natural
to be handled in menu.c rather than accepting "arrow" in
font_buffer_create().

Also I allowed non-positive numbers for max_width in font_buffer_create(),
in which case the natural font width is used as the buffer width.
This commit is contained in:
tokyo4j 2024-12-03 16:09:40 +09:00 committed by Johan Malm
parent 10fc656c23
commit 01032ef3bd
7 changed files with 28 additions and 43 deletions

View file

@ -44,11 +44,10 @@ int font_width(struct font *font, const char *string);
* @font: font description * @font: font description
* @color: foreground color in rgba format * @color: foreground color in rgba format
* @bg_color: background color in rgba format * @bg_color: background color in rgba format
* @arrow: arrow (utf8) character to show or NULL for none
*/ */
void font_buffer_create(struct lab_data_buffer **buffer, int max_width, void font_buffer_create(struct lab_data_buffer **buffer, int max_width,
const char *text, struct font *font, const float *color, const char *text, struct font *font, const float *color,
const float *bg_color, const char *arrow, double scale); const float *bg_color, double scale);
/** /**
* font_finish - free some font related resources * font_finish - free some font related resources

View file

@ -18,7 +18,6 @@ struct scaled_font_buffer {
int max_width; int max_width;
float color[4]; float color[4];
float bg_color[4]; float bg_color[4];
char *arrow;
struct font font; struct font font;
struct scaled_scene_buffer *scaled_buffer; struct scaled_scene_buffer *scaled_buffer;
}; };
@ -48,7 +47,7 @@ struct scaled_font_buffer *scaled_font_buffer_create(struct wlr_scene_tree *pare
*/ */
void scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text, void scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
int max_width, struct font *font, const float *color, int max_width, struct font *font, const float *color,
const float *bg_color, const char *arrow); const float *bg_color);
/** /**
* Update the max width of an existing auto scaling font buffer * Update the max width of an existing auto scaling font buffer

View file

@ -82,34 +82,19 @@ font_width(struct font *font, const char *string)
void void
font_buffer_create(struct lab_data_buffer **buffer, int max_width, font_buffer_create(struct lab_data_buffer **buffer, int max_width,
const char *text, struct font *font, const float *color, const char *text, struct font *font, const float *color,
const float *bg_color, const char *arrow, double scale) const float *bg_color, double scale)
{ {
/* Allow a minimum of one pixel each for text and arrow */
if (max_width < 2) {
max_width = 2;
}
if (string_null_or_empty(text)) { if (string_null_or_empty(text)) {
return; return;
} }
PangoRectangle text_extents = font_extents(font, text); PangoRectangle text_extents = font_extents(font, text);
PangoRectangle arrow_extents = font_extents(font, arrow);
if (arrow) { if (max_width > 0 && text_extents.width > max_width) {
if (arrow_extents.width >= max_width - 1) {
/* It would be weird to get here, but just in case */
arrow_extents.width = max_width - 1;
text_extents.width = 1;
} else {
text_extents.width = max_width - arrow_extents.width;
}
} else if (text_extents.width > max_width) {
text_extents.width = max_width; text_extents.width = max_width;
} }
*buffer = buffer_create_cairo(text_extents.width + arrow_extents.width, *buffer = buffer_create_cairo(text_extents.width, text_extents.height, scale);
text_extents.height, scale);
if (!*buffer) { if (!*buffer) {
wlr_log(WLR_ERROR, "Failed to create font buffer"); wlr_log(WLR_ERROR, "Failed to create font buffer");
return; return;
@ -161,13 +146,6 @@ font_buffer_create(struct lab_data_buffer **buffer, int max_width,
pango_cairo_update_layout(cairo, layout); pango_cairo_update_layout(cairo, layout);
pango_cairo_show_layout(cairo, layout); pango_cairo_show_layout(cairo, layout);
if (arrow) {
cairo_move_to(cairo, text_extents.width, 0);
pango_layout_set_width(layout, arrow_extents.width * PANGO_SCALE);
pango_layout_set_text(layout, arrow, -1);
pango_cairo_show_layout(cairo, layout);
}
g_object_unref(layout); g_object_unref(layout);
cairo_surface_flush(surf); cairo_surface_flush(surf);

View file

@ -23,7 +23,7 @@ _create_buffer(struct scaled_scene_buffer *scaled_buffer, double scale)
/* Buffer gets free'd automatically along the backing wlr_buffer */ /* Buffer gets free'd automatically along the backing wlr_buffer */
font_buffer_create(&buffer, self->max_width, self->text, font_buffer_create(&buffer, self->max_width, self->text,
&self->font, self->color, self->bg_color, self->arrow, scale); &self->font, self->color, self->bg_color, scale);
if (!buffer) { if (!buffer) {
wlr_log(WLR_ERROR, "font_buffer_create() failed"); wlr_log(WLR_ERROR, "font_buffer_create() failed");
@ -40,7 +40,6 @@ _destroy(struct scaled_scene_buffer *scaled_buffer)
zfree(self->text); zfree(self->text);
zfree(self->font.name); zfree(self->font.name);
zfree(self->arrow);
free(self); free(self);
} }
@ -63,8 +62,7 @@ _equal(struct scaled_scene_buffer *scaled_buffer_a,
&& a->font.slant == b->font.slant && a->font.slant == b->font.slant
&& a->font.weight == b->font.weight && a->font.weight == b->font.weight
&& !memcmp(a->color, b->color, sizeof(a->color)) && !memcmp(a->color, b->color, sizeof(a->color))
&& !memcmp(a->bg_color, b->bg_color, sizeof(a->bg_color)) && !memcmp(a->bg_color, b->bg_color, sizeof(a->bg_color));
&& str_equal(a->arrow, b->arrow);
} }
static const struct scaled_scene_buffer_impl impl = { static const struct scaled_scene_buffer_impl impl = {
@ -95,7 +93,7 @@ scaled_font_buffer_create(struct wlr_scene_tree *parent)
void void
scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text, scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
int max_width, struct font *font, const float *color, int max_width, struct font *font, const float *color,
const float *bg_color, const char *arrow) const float *bg_color)
{ {
assert(self); assert(self);
assert(text); assert(text);
@ -105,7 +103,6 @@ scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
/* Clean up old internal state */ /* Clean up old internal state */
zfree(self->text); zfree(self->text);
zfree(self->font.name); zfree(self->font.name);
zfree(self->arrow);
/* Update internal state */ /* Update internal state */
self->text = xstrdup(text); self->text = xstrdup(text);
@ -118,7 +115,6 @@ scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
self->font.weight = font->weight; self->font.weight = font->weight;
memcpy(self->color, color, sizeof(self->color)); memcpy(self->color, color, sizeof(self->color));
memcpy(self->bg_color, bg_color, sizeof(self->bg_color)); memcpy(self->bg_color, bg_color, sizeof(self->bg_color));
self->arrow = arrow ? xstrdup(arrow) : NULL;
/* Invalidate cache and force a new render */ /* Invalidate cache and force a new render */
scaled_scene_buffer_invalidate_cache(self->scaled_buffer); scaled_scene_buffer_invalidate_cache(self->scaled_buffer);

View file

@ -168,18 +168,32 @@ item_create_scene_for_state(struct menuitem *item, float *text_color,
int arrow_width = item->arrow ? int arrow_width = item->arrow ?
font_width(&rc.font_menuitem, item->arrow) : 0; font_width(&rc.font_menuitem, item->arrow) : 0;
int text_width = bg_width - 2 * theme->menu_items_padding_x - arrow_width; int label_max_width = bg_width - 2 * theme->menu_items_padding_x - arrow_width;
/* Create label */ /* Create label */
struct scaled_font_buffer *label_buffer = scaled_font_buffer_create(tree); struct scaled_font_buffer *label_buffer = scaled_font_buffer_create(tree);
assert(label_buffer); assert(label_buffer);
scaled_font_buffer_update(label_buffer, item->text, text_width, scaled_font_buffer_update(label_buffer, item->text, label_max_width,
&rc.font_menuitem, text_color, bg_color, item->arrow); &rc.font_menuitem, text_color, bg_color);
/* Vertically center and left-align label */ /* Vertically center and left-align label */
int x = theme->menu_items_padding_x; int x = theme->menu_items_padding_x;
int y = (theme->menu_item_height - label_buffer->height) / 2; int y = (theme->menu_item_height - label_buffer->height) / 2;
wlr_scene_node_set_position(&label_buffer->scene_buffer->node, x, y); wlr_scene_node_set_position(&label_buffer->scene_buffer->node, x, y);
if (!item->arrow) {
return tree;
}
/* Create arrow for submenu items */
struct scaled_font_buffer *arrow_buffer = scaled_font_buffer_create(tree);
assert(arrow_buffer);
scaled_font_buffer_update(arrow_buffer, item->arrow, -1,
&rc.font_menuitem, text_color, bg_color);
/* Vertically center and right-align arrow */
x += label_max_width;
y = (theme->menu_item_height - label_buffer->height) / 2;
wlr_scene_node_set_position(&arrow_buffer->scene_buffer->node, x, y);
return tree; return tree;
} }
@ -298,7 +312,7 @@ title_create_scene(struct menuitem *menuitem, int *item_y)
assert(title_font_buffer); assert(title_font_buffer);
scaled_font_buffer_update(title_font_buffer, menuitem->text, scaled_font_buffer_update(title_font_buffer, menuitem->text,
bg_width - 2 * theme->menu_items_padding_x, bg_width - 2 * theme->menu_items_padding_x,
&rc.font_menuheader, text_color, bg_color, /* arrow */ NULL); &rc.font_menuheader, text_color, bg_color);
int title_x = 0; int title_x = 0;
switch (theme->menu_title_text_justify) { switch (theme->menu_title_text_justify) {

View file

@ -205,8 +205,7 @@ resize_indicator_update(struct view *view)
wlr_scene_node_set_position(&indicator->tree->node, x, y); wlr_scene_node_set_position(&indicator->tree->node, x, y);
scaled_font_buffer_update(indicator->text, text, width, &rc.font_osd, scaled_font_buffer_update(indicator->text, text, width, &rc.font_osd,
rc.theme->osd_label_text_color, rc.theme->osd_bg_color, rc.theme->osd_label_text_color, rc.theme->osd_bg_color);
NULL /* const char *arrow */);
} }
void void

View file

@ -504,7 +504,7 @@ ssd_update_title(struct ssd *ssd)
if (part->buffer) { if (part->buffer) {
scaled_font_buffer_update(part->buffer, title, scaled_font_buffer_update(part->buffer, title,
title_bg_width, font, title_bg_width, font,
text_color, bg_color, NULL); text_color, bg_color);
} }
/* And finally update the cache */ /* And finally update the cache */