scaled-buffer: introduce scaled_font_buffer_update_markup()

This function behaves identically to `scaled_font_buffer_update()`
but allows setting the text as pango markup, supporting further
customization like underscores.
This commit is contained in:
Alex Chernika 2026-04-26 12:33:54 +02:00 committed by Johan Malm
parent 0caefa6a9e
commit 07a0a4e59b
4 changed files with 46 additions and 9 deletions

View file

@ -4,6 +4,7 @@
#include <cairo.h> #include <cairo.h>
#include <pango/pango-font.h> #include <pango/pango-font.h>
#include <stdbool.h>
struct lab_data_buffer; struct lab_data_buffer;
@ -43,10 +44,11 @@ void font_get_buffer_size(int max_width, const char *text, struct font *font,
* @font: font description * @font: font description
* @color: foreground color in rgba format * @color: foreground color in rgba format
* @bg_pattern: background pattern * @bg_pattern: background pattern
* @use_markup: flag to render pango markup
*/ */
void font_buffer_create(struct lab_data_buffer **buffer, int max_width, void font_buffer_create(struct lab_data_buffer **buffer, int max_width,
int height, const char *text, struct font *font, const float *color, int height, const char *text, struct font *font, const float *color,
cairo_pattern_t *bg_pattern, double scale); cairo_pattern_t *bg_pattern, double scale, bool use_markup);
/** /**
* font_finish - free some font related resources * font_finish - free some font related resources

View file

@ -15,6 +15,7 @@ struct scaled_font_buffer {
/* Private */ /* Private */
char *text; char *text;
bool use_markup;
int max_width; int max_width;
float color[4]; float color[4];
float bg_color[4]; float bg_color[4];
@ -69,8 +70,18 @@ scaled_font_buffer_create_for_titlebar(struct wlr_scene_tree *parent,
* bg_color is ignored for font buffers created with * bg_color is ignored for font buffers created with
* scaled_font_buffer_create_for_titlebar(). * scaled_font_buffer_create_for_titlebar().
*/ */
void scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text, void scaled_font_buffer_update(struct scaled_font_buffer *self,
int max_width, struct font *font, const float *color, const char *text, int max_width, struct font *font, const float *color,
const float *bg_color); const float *bg_color);
/**
* Update an existing auto scaling font buffer allowing the use of pango markup.
*
* Behaves identically to scaled_font_buffer_update(), but allows customization
* of the `use_markup` field of the @self struct via @use_markup.
*/
void scaled_font_buffer_update_markup(struct scaled_font_buffer *self,
const char *text, int max_width, struct font *font, const float *color,
const float *bg_color, bool use_markup);
#endif /* LABWC_SCALED_FONT_BUFFER_H */ #endif /* LABWC_SCALED_FONT_BUFFER_H */

View file

@ -79,7 +79,7 @@ font_get_buffer_size(int max_width, const char *text, struct font *font,
void void
font_buffer_create(struct lab_data_buffer **buffer, int max_width, font_buffer_create(struct lab_data_buffer **buffer, int max_width,
int height, const char *text, struct font *font, const float *color, int height, const char *text, struct font *font, const float *color,
cairo_pattern_t *bg_pattern, double scale) cairo_pattern_t *bg_pattern, double scale, bool use_markup)
{ {
if (string_null_or_empty(text)) { if (string_null_or_empty(text)) {
return; return;
@ -123,7 +123,13 @@ font_buffer_create(struct lab_data_buffer **buffer, int max_width,
PangoLayout *layout = pango_cairo_create_layout(cairo); PangoLayout *layout = pango_cairo_create_layout(cairo);
pango_context_set_round_glyph_positions(pango_layout_get_context(layout), false); pango_context_set_round_glyph_positions(pango_layout_get_context(layout), false);
pango_layout_set_width(layout, width * PANGO_SCALE); pango_layout_set_width(layout, width * PANGO_SCALE);
pango_layout_set_text(layout, text, -1);
if (use_markup) {
pango_layout_set_markup(layout, text, -1);
} else {
pango_layout_set_text(layout, text, -1);
}
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
if (!opaque_bg) { if (!opaque_bg) {

View file

@ -26,7 +26,7 @@ _create_buffer(struct scaled_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->height, self->text, font_buffer_create(&buffer, self->max_width, self->height, self->text,
&self->font, self->color, bg_pattern, scale); &self->font, self->color, bg_pattern, scale, self->use_markup);
if (!buffer) { if (!buffer) {
wlr_log(WLR_ERROR, "font_buffer_create() failed"); wlr_log(WLR_ERROR, "font_buffer_create() failed");
@ -56,6 +56,7 @@ _equal(struct scaled_buffer *scaled_buffer_a,
struct scaled_font_buffer *b = scaled_buffer_b->data; struct scaled_font_buffer *b = scaled_buffer_b->data;
return str_equal(a->text, b->text) return str_equal(a->text, b->text)
&& a->use_markup == b->use_markup
&& a->max_width == b->max_width && a->max_width == b->max_width
&& str_equal(a->font.name, b->font.name) && str_equal(a->font.name, b->font.name)
&& a->font.size == b->font.size && a->font.size == b->font.size
@ -104,10 +105,10 @@ scaled_font_buffer_create_for_titlebar(struct wlr_scene_tree *parent,
return self; return self;
} }
void static 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 float *bg_color, bool use_markup)
{ {
assert(self); assert(self);
assert(text); assert(text);
@ -120,6 +121,7 @@ scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
/* Update internal state */ /* Update internal state */
self->text = xstrdup(text); self->text = xstrdup(text);
self->use_markup = use_markup;
self->max_width = max_width; self->max_width = max_width;
if (font->name) { if (font->name) {
self->font.name = xstrdup(font->name); self->font.name = xstrdup(font->name);
@ -139,3 +141,19 @@ scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
scaled_buffer_request_update(self->scaled_buffer, scaled_buffer_request_update(self->scaled_buffer,
self->width, self->height); self->width, self->height);
} }
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)
{
_scaled_font_buffer_update(self, text, max_width, font, color, bg_color, false);
}
void
scaled_font_buffer_update_markup(struct scaled_font_buffer *self, const char *text,
int max_width, struct font *font, const float *color,
const float *bg_color, bool use_markup)
{
_scaled_font_buffer_update(self, text, max_width, font, color, bg_color, use_markup);
}