From 6fee86a1cfcf6a7b9b2e0fa8e6e6158b37c91357 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 25 Jun 2024 02:10:48 -0400 Subject: [PATCH] gtk3: Fix Cairo backend With GTK3, the Cairo surface we get is for the whole window, which means the automatic size inference from #22004 gets the wrong size. For the GtkDrawingArea, the Cairo context is aligned and clipped to the widget, so nothing goes out-of-bounds. However, since the Cairo renderer flips the origin using the height in the calculation (which is, for the window, bigger than the drawing widget), everything is drawn lower than it should. --- lib/matplotlib/backends/backend_gtk3cairo.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/backends/backend_gtk3cairo.py b/lib/matplotlib/backends/backend_gtk3cairo.py index 24a26111f062..371b8dc1b31f 100644 --- a/lib/matplotlib/backends/backend_gtk3cairo.py +++ b/lib/matplotlib/backends/backend_gtk3cairo.py @@ -13,15 +13,19 @@ def on_draw_event(self, widget, ctx): with (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar else nullcontext()): - self._renderer.set_context(ctx) - scale = self.device_pixel_ratio - # Scale physical drawing to logical size. - ctx.scale(1 / scale, 1 / scale) allocation = self.get_allocation() + # Render the background before scaling, as the allocated size here is in + # logical pixels. Gtk.render_background( self.get_style_context(), ctx, - allocation.x, allocation.y, - allocation.width, allocation.height) + 0, 0, allocation.width, allocation.height) + scale = self.device_pixel_ratio + # Scale physical drawing to logical size. + ctx.scale(1 / scale, 1 / scale) + self._renderer.set_context(ctx) + # Set renderer to physical size so it renders in full resolution. + self._renderer.width = allocation.width * scale + self._renderer.height = allocation.height * scale self._renderer.dpi = self.figure.dpi self.figure.draw(self._renderer)