labwc/include/common/scaled_font_buffer.h
John Lindgren 4fa51b950c 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
2024-03-17 19:09:53 +00:00

61 lines
1.9 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef LABWC_SCALED_FONT_BUFFER_H
#define LABWC_SCALED_FONT_BUFFER_H
#include "common/font.h"
struct wlr_scene_tree;
struct wlr_scene_buffer;
struct scaled_scene_buffer;
struct scaled_font_buffer {
struct wlr_scene_buffer *scene_buffer;
int width; /* unscaled, read only */
int height; /* unscaled, read only */
/* Private */
char *text;
int max_width;
float color[4];
float bg_color[4];
char *arrow;
struct font font;
struct scaled_scene_buffer *scaled_buffer;
};
/**
* Create an auto scaling font buffer, providing a wlr_scene_buffer node for
* display. It gets destroyed automatically when the backing scaled_scene_buffer
* is being destroyed which in turn happens automatically when the backing
* wlr_scene_buffer (or one of its parents) is being destroyed.
*
* To actually show some text, scaled_font_buffer_update() has to be called.
*
*/
struct scaled_font_buffer *scaled_font_buffer_create(struct wlr_scene_tree *parent);
/**
* Update an existing auto scaling font buffer.
*
* No steps are taken to detect if its actually required to render a new buffer.
* This should be done by the caller to prevent useless recreation of the same
* buffer in case nothing actually changed.
*
* Some basic checks could be something like
* - truncated = buffer->width == max_width
* - text_changed = strcmp(old_text, new_text)
* - font and color the same
*/
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);
/**
* Update the max width of an existing auto scaling font buffer
* and force a new render.
*
* No steps are taken to detect if its actually required to render a new buffer.
*/
void scaled_font_buffer_set_max_width(struct scaled_font_buffer *self, int max_width);
#endif /* LABWC_SCALED_FONT_BUFFER_H */