Skip to content

Commit

Permalink
gtk3: Fix Cairo backend
Browse files Browse the repository at this point in the history
With GTK3, the Cairo surface we get is for the whole window, which means
the automatic size inference from matplotlib#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.
  • Loading branch information
QuLogic committed Jun 25, 2024
1 parent 5c55265 commit 6fee86a
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/matplotlib/backends/backend_gtk3cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 6fee86a

Please sign in to comment.