common: render text buffers with opaque background

After a roundabout discussion[1] with wlroots devs, it's become apparent
that subpixel text rendering (a.k.a. "ClearType") does not work properly
when rendering over a transparent background, as labwc currently does.

Basically it comes down to the fact that the color of semi-transparent
pixels (which is adjusted redder or bluer to compensate for RGB subpixel
alignment) depends somewhat on background color. When rendering over
transparency, the text engine doesn't know the intended background color
and can't adjust the pixel colors correctly.

With Pango/Cairo, the end result can range from grayscale rendering (no
subpixel rendering at all) to wrong/oversaturated colors (for example,
bright pink pixels when rendering white text on blue background).

This change solves the issue by first filling the text buffer with an
opaque background color before rendering the text over it. Currently,
this is easy since the background is always a solid color. It may be a
little more complex (but doable) if we implement gradients in future.

Note that GTK 4 (and to some degree, recent versions of Microsoft
Windows) avoid this issue by disabling subpixel rendering altogether. I
would much prefer that labwc NOT do this -- it results in noticeably
blurrier text on non-retina LCD screens, which are still common.

[1] https://gitlab.freedesktop.org/wlroots/wlroots/-/issues/3822
This commit is contained in:
John Lindgren 2024-03-16 22:20:40 -04:00 committed by Johan Malm
parent 5a20014f50
commit 4fa51b950c
9 changed files with 34 additions and 15 deletions

View file

@ -77,8 +77,8 @@ 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, float *color, const char *arrow,
double scale)
const char *text, struct font *font, const float *color,
const float *bg_color, const char *arrow, double scale)
{
/* Allow a minimum of one pixel each for text and arrow */
if (max_width < 2) {
@ -114,6 +114,15 @@ font_buffer_create(struct lab_data_buffer **buffer, int max_width,
cairo_t *cairo = (*buffer)->cairo;
cairo_surface_t *surf = cairo_get_target(cairo);
/*
* Fill background color first - necessary for subpixel
* rendering, which does not work properly on transparency
*/
set_cairo_color(cairo, bg_color);
cairo_rectangle(cairo, 0, 0, (*buffer)->unscaled_width,
(*buffer)->unscaled_height);
cairo_fill(cairo);
set_cairo_color(cairo, color);
cairo_move_to(cairo, 0, 0);

View file

@ -83,7 +83,7 @@ draw_cairo_border(cairo_t *cairo, struct wlr_fbox fbox, double line_width)
/* Sets the cairo color. Splits the single color channels */
void
set_cairo_color(cairo_t *cairo, float *c)
set_cairo_color(cairo_t *cairo, const float *c)
{
cairo_set_source_rgba(cairo, c[0], c[1], c[2], c[3]);
}

View file

@ -19,7 +19,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->arrow, scale);
&self->font, self->color, self->bg_color, self->arrow, scale);
self->width = buffer ? buffer->unscaled_width : 0;
self->height = buffer ? buffer->unscaled_height : 0;
@ -64,8 +64,8 @@ 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, float *color,
const char *arrow)
int max_width, struct font *font, const float *color,
const float *bg_color, const char *arrow)
{
assert(self);
assert(text);
@ -87,6 +87,7 @@ scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
self->font.slant = font->slant;
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 */