Skip to content

Commit a81953c

Browse files
committed
sway_text_node: Consider all output scales when calculating buffer size
1 parent d417a8f commit a81953c

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

include/sway/sway_text_node.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ struct sway_text_node {
66
int width;
77
int max_width;
88
int height;
9-
int baseline;
9+
float baseline;
1010
bool pango_markup;
1111
float color[4];
1212
float background[4];

sway/sway_text_node.c

+35-18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "pango.h"
1010
#include "sway/config.h"
1111
#include "sway/sway_text_node.h"
12+
#include "sway/output.h"
1213

1314
struct cairo_buffer {
1415
struct wlr_buffer base;
@@ -163,6 +164,39 @@ static void render_backing_buffer(struct text_buffer *buffer) {
163164
cairo_font_options_destroy(fo);
164165
}
165166

167+
static void text_calc_size(struct text_buffer *buffer) {
168+
struct sway_text_node *props = &buffer->props;
169+
props->width = 0;
170+
171+
cairo_t *c = cairo_create(NULL);
172+
if (!c) {
173+
sway_log(SWAY_ERROR, "cairo_t allocation failed");
174+
return;
175+
}
176+
177+
cairo_set_antialias(c, CAIRO_ANTIALIAS_BEST);
178+
for (int i = 0; i < root->outputs->length; ++i) {
179+
struct sway_output *output = root->outputs->items[i];
180+
if (!output->wlr_output->enabled) {
181+
continue;
182+
}
183+
184+
int size, baseline;
185+
get_text_size(c, config->font_description, &size, NULL,
186+
&baseline, output->wlr_output->scale, props->pango_markup, "%s", buffer->text);
187+
188+
size = ceil((float)size / output->wlr_output->scale);
189+
if (props->width < size) {
190+
props->width = size;
191+
props->baseline = (float)baseline / output->wlr_output->scale;
192+
}
193+
}
194+
cairo_destroy(c);
195+
196+
wlr_scene_buffer_set_dest_size(buffer->buffer_node,
197+
get_text_width(props), props->height);
198+
}
199+
166200
static void handle_outputs_update(struct wl_listener *listener, void *data) {
167201
struct text_buffer *buffer = wl_container_of(listener, buffer, outputs_update);
168202
struct wlr_scene_outputs_update_event *event = data;
@@ -194,6 +228,7 @@ static void handle_outputs_update(struct wl_listener *listener, void *data) {
194228
if (scale != buffer->scale || subpixel != buffer->subpixel) {
195229
buffer->scale = scale;
196230
buffer->subpixel = subpixel;
231+
text_calc_size(buffer);
197232
render_backing_buffer(buffer);
198233
}
199234
}
@@ -208,24 +243,6 @@ static void handle_destroy(struct wl_listener *listener, void *data) {
208243
free(buffer);
209244
}
210245

211-
static void text_calc_size(struct text_buffer *buffer) {
212-
struct sway_text_node *props = &buffer->props;
213-
214-
cairo_t *c = cairo_create(NULL);
215-
if (!c) {
216-
sway_log(SWAY_ERROR, "cairo_t allocation failed");
217-
return;
218-
}
219-
220-
cairo_set_antialias(c, CAIRO_ANTIALIAS_BEST);
221-
get_text_size(c, config->font_description, &props->width, NULL,
222-
&props->baseline, 1, props->pango_markup, "%s", buffer->text);
223-
cairo_destroy(c);
224-
225-
wlr_scene_buffer_set_dest_size(buffer->buffer_node,
226-
get_text_width(props), props->height);
227-
}
228-
229246
struct sway_text_node *sway_text_node_create(struct wlr_scene_tree *parent,
230247
char *text, float color[4], bool pango_markup) {
231248
struct text_buffer *buffer = calloc(1, sizeof(*buffer));

0 commit comments

Comments
 (0)