Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix text scale and enable glyph position rounding again #8429

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions common/pango.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ size_t escape_markup_text(const char *src, char *dest) {
PangoLayout *get_pango_layout(cairo_t *cairo, const PangoFontDescription *desc,
const char *text, double scale, bool markup) {
PangoLayout *layout = pango_cairo_create_layout(cairo);
pango_context_set_round_glyph_positions(pango_layout_get_context(layout), false);

PangoAttrList *attrs;
if (markup) {
char *buf;
Expand Down Expand Up @@ -106,7 +104,6 @@ void get_text_size(cairo_t *cairo, const PangoFontDescription *desc, int *width,
void get_text_metrics(const PangoFontDescription *description, int *height, int *baseline) {
cairo_t *cairo = cairo_create(NULL);
PangoContext *pango = pango_cairo_create_context(cairo);
pango_context_set_round_glyph_positions(pango, false);
// When passing NULL as a language, pango uses the current locale.
PangoFontMetrics *metrics = pango_context_get_metrics(pango, description, NULL);

Expand Down
2 changes: 1 addition & 1 deletion include/sway/sway_text_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct sway_text_node {
int width;
int max_width;
int height;
int baseline;
float baseline;
bool pango_markup;
float color[4];
float background[4];
Expand Down
53 changes: 35 additions & 18 deletions sway/sway_text_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "pango.h"
#include "sway/config.h"
#include "sway/sway_text_node.h"
#include "sway/output.h"

struct cairo_buffer {
struct wlr_buffer base;
Expand Down Expand Up @@ -163,6 +164,39 @@ static void render_backing_buffer(struct text_buffer *buffer) {
cairo_font_options_destroy(fo);
}

static void text_calc_size(struct text_buffer *buffer) {
struct sway_text_node *props = &buffer->props;
props->width = 0;

cairo_t *c = cairo_create(NULL);
if (!c) {
sway_log(SWAY_ERROR, "cairo_t allocation failed");
return;
}

cairo_set_antialias(c, CAIRO_ANTIALIAS_BEST);
for (int i = 0; i < root->outputs->length; ++i) {
struct sway_output *output = root->outputs->items[i];
if (!output->wlr_output->enabled) {
continue;
}

int size, baseline;
get_text_size(c, config->font_description, &size, NULL,
&baseline, output->wlr_output->scale, props->pango_markup, "%s", buffer->text);
Comment on lines +177 to +186
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be fine to just take the largest scale from all outputs.

get_text_size is quite expensive - a quick test on my Ryzen laptop on battery power clocks get_text_size with a random title around 400µs, although it gets a lot faster on external power of course.


size = ceil((float)size / output->wlr_output->scale);
if (props->width < size) {
props->width = size;
props->baseline = (float)baseline / output->wlr_output->scale;
}
}
cairo_destroy(c);

wlr_scene_buffer_set_dest_size(buffer->buffer_node,
get_text_width(props), props->height);
}

static void handle_outputs_update(struct wl_listener *listener, void *data) {
struct text_buffer *buffer = wl_container_of(listener, buffer, outputs_update);
struct wlr_scene_outputs_update_event *event = data;
Expand Down Expand Up @@ -194,6 +228,7 @@ static void handle_outputs_update(struct wl_listener *listener, void *data) {
if (scale != buffer->scale || subpixel != buffer->subpixel) {
buffer->scale = scale;
buffer->subpixel = subpixel;
text_calc_size(buffer);
render_backing_buffer(buffer);
}
}
Expand All @@ -208,24 +243,6 @@ static void handle_destroy(struct wl_listener *listener, void *data) {
free(buffer);
}

static void text_calc_size(struct text_buffer *buffer) {
struct sway_text_node *props = &buffer->props;

cairo_t *c = cairo_create(NULL);
if (!c) {
sway_log(SWAY_ERROR, "cairo_t allocation failed");
return;
}

cairo_set_antialias(c, CAIRO_ANTIALIAS_BEST);
get_text_size(c, config->font_description, &props->width, NULL,
&props->baseline, 1, props->pango_markup, "%s", buffer->text);
cairo_destroy(c);

wlr_scene_buffer_set_dest_size(buffer->buffer_node,
get_text_width(props), props->height);
}

struct sway_text_node *sway_text_node_create(struct wlr_scene_tree *parent,
char *text, float color[4], bool pango_markup) {
struct text_buffer *buffer = calloc(1, sizeof(*buffer));
Expand Down