Scale fonts when Wayland updates scaling factor

Before this commit, the code in src/wl_window.c seemed to handle the Wayland
scaling [1] correctly, it was sending resize events to imv to update the buffer
size accordingly. One thing it didn't update is the scaling of fonts that Pango
renders on Cairo.

This commit simply forwards the scaling factor (computed as [1] requests)
together with updated buffer dimentions in resize event, and when the resize
event is handled it calls cairo_surface_set_device_scale to notify Pango/Cairo
of the scaling.

For X11, I simply assume the scaline factor is always 1. This seems to be what
the old code did: `grep scale src/x11_window.c` gives no matches. AFAIK, X11
does not have an established way of telling clients what scaling factor to use
(and never updates it at runtime).

[1]: https://wayland-book.com/surfaces-in-depth/hidpi.html
This commit is contained in:
Ivan Oleynikov 2021-07-27 00:19:42 +06:00 committed by Harry Jeffery
parent eeaf5e3cb9
commit 3c1d27d885
6 changed files with 11 additions and 5 deletions

View file

@ -69,7 +69,7 @@ void imv_canvas_free(struct imv_canvas *canvas)
free(canvas);
}
void imv_canvas_resize(struct imv_canvas *canvas, int width, int height)
void imv_canvas_resize(struct imv_canvas *canvas, int width, int height, double scale)
{
cairo_destroy(canvas->cairo);
cairo_surface_destroy(canvas->surface);
@ -80,6 +80,7 @@ void imv_canvas_resize(struct imv_canvas *canvas, int width, int height)
canvas->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
canvas->width, canvas->height);
assert(canvas->surface);
cairo_surface_set_device_scale(canvas->surface, scale, scale);
canvas->cairo = cairo_create(canvas->surface);
assert(canvas->cairo);
}

View file

@ -19,7 +19,7 @@ struct imv_canvas *imv_canvas_create(int width, int height);
void imv_canvas_free(struct imv_canvas *canvas);
/* Set the buffer size of the canvas */
void imv_canvas_resize(struct imv_canvas *canvas, int width, int height);
void imv_canvas_resize(struct imv_canvas *canvas, int width, int height, double scale);
/* Blank the canvas to be empty and transparent */
void imv_canvas_clear(struct imv_canvas *canvas);

View file

@ -447,8 +447,9 @@ static void event_handler(void *data, const struct imv_event *e)
const int wh = e->data.resize.height;
const int bw = e->data.resize.buffer_width;
const int bh = e->data.resize.buffer_height;
const double scale = e->data.resize.scale;
imv_viewport_update(imv->view, ww, wh, bw, bh, imv->current_image, imv->scaling_mode);
imv_canvas_resize(imv->canvas, bw, bh);
imv_canvas_resize(imv->canvas, bw, bh, scale);
break;
}
case IMV_EVENT_KEYBOARD:

View file

@ -24,6 +24,7 @@ struct imv_event {
int height;
int buffer_width;
int buffer_height;
double scale;
} resize;
struct {
int scancode;

View file

@ -558,7 +558,8 @@ static void update_scale(struct imv_window *window)
.width = window->width,
.height = window->height,
.buffer_width = buffer_width,
.buffer_height = buffer_height
.buffer_height = buffer_height,
.scale = window->scale,
}
}
};
@ -639,6 +640,7 @@ static void toplevel_configure(void *data, struct xdg_toplevel *toplevel,
.height = window->height,
.buffer_width = buffer_width,
.buffer_height = buffer_height,
.scale = window->scale,
}
}
};

View file

@ -346,7 +346,8 @@ void imv_window_pump_events(struct imv_window *window, imv_event_handler handler
.width = wa.width,
.height = wa.height,
.buffer_width = wa.width,
.buffer_height = wa.height
.buffer_height = wa.height,
.scale = 1,
}
}
};