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

@ -82,34 +82,19 @@ font_width(struct font *font, const char *string)
void
font_buffer_create(struct lab_data_buffer **buffer, int max_width,
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)) {
return;
}
PangoRectangle text_extents = font_extents(font, text);
PangoRectangle arrow_extents = font_extents(font, arrow);
if (arrow) {
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) {
if (max_width > 0 && text_extents.width > max_width) {
text_extents.width = max_width;
}
*buffer = buffer_create_cairo(text_extents.width + arrow_extents.width,
text_extents.height, scale);
*buffer = buffer_create_cairo(text_extents.width, text_extents.height, scale);
if (!*buffer) {
wlr_log(WLR_ERROR, "Failed to create font buffer");
return;
@ -161,13 +146,6 @@ font_buffer_create(struct lab_data_buffer **buffer, int max_width,
pango_cairo_update_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);
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 */
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) {
wlr_log(WLR_ERROR, "font_buffer_create() failed");
@ -40,7 +40,6 @@ _destroy(struct scaled_scene_buffer *scaled_buffer)
zfree(self->text);
zfree(self->font.name);
zfree(self->arrow);
free(self);
}
@ -63,8 +62,7 @@ _equal(struct scaled_scene_buffer *scaled_buffer_a,
&& a->font.slant == b->font.slant
&& a->font.weight == b->font.weight
&& !memcmp(a->color, b->color, sizeof(a->color))
&& !memcmp(a->bg_color, b->bg_color, sizeof(a->bg_color))
&& str_equal(a->arrow, b->arrow);
&& !memcmp(a->bg_color, b->bg_color, sizeof(a->bg_color));
}
static const struct scaled_scene_buffer_impl impl = {
@ -95,7 +93,7 @@ scaled_font_buffer_create(struct wlr_scene_tree *parent)
void
scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
int max_width, struct font *font, const float *color,
const float *bg_color, const char *arrow)
const float *bg_color)
{
assert(self);
assert(text);
@ -105,7 +103,6 @@ scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
/* Clean up old internal state */
zfree(self->text);
zfree(self->font.name);
zfree(self->arrow);
/* Update internal state */
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;
memcpy(self->color, color, sizeof(self->color));
memcpy(self->bg_color, bg_color, sizeof(self->bg_color));
self->arrow = arrow ? xstrdup(arrow) : NULL;
/* Invalidate cache and force a new render */
scaled_scene_buffer_invalidate_cache(self->scaled_buffer);