Skip to content

Backport PR #28451 on branch v3.9.x (Fix GTK cairo backends) #28506

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

Merged
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
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
10 changes: 2 additions & 8 deletions lib/matplotlib/backends/backend_gtk4.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class FigureCanvasGTK4(_FigureCanvasGTK, Gtk.DrawingArea):
required_interactive_framework = "gtk4"
supports_blit = False
manager_class = _api.classproperty(lambda cls: FigureManagerGTK4)
_context_is_scaled = False

def __init__(self, figure=None):
super().__init__(figure=figure)
Expand Down Expand Up @@ -228,13 +227,8 @@ def _post_draw(self, widget, ctx):

lw = 1
dash = 3
if not self._context_is_scaled:
x0, y0, w, h = (dim / self.device_pixel_ratio
for dim in self._rubberband_rect)
else:
x0, y0, w, h = self._rubberband_rect
lw *= self.device_pixel_ratio
dash *= self.device_pixel_ratio
x0, y0, w, h = (dim / self.device_pixel_ratio
for dim in self._rubberband_rect)
x1 = x0 + w
y1 = y0 + h

Expand Down
9 changes: 4 additions & 5 deletions lib/matplotlib/backends/backend_gtk4cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@


class FigureCanvasGTK4Cairo(FigureCanvasCairo, FigureCanvasGTK4):
_context_is_scaled = True
def _set_device_pixel_ratio(self, ratio):
# Cairo in GTK4 always uses logical pixels, so we don't need to do anything for
# changes to the device pixel ratio.
return False

def on_draw_event(self, widget, ctx):
if self._idle_draw_id:
Expand All @@ -16,15 +19,11 @@ 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()
Gtk.render_background(
self.get_style_context(), ctx,
allocation.x, allocation.y,
allocation.width, allocation.height)
self._renderer.dpi = self.figure.dpi
self.figure.draw(self._renderer)


Expand Down
Loading