Skip to content

Commit 7fa9f24

Browse files
authored
Merge pull request #28506 from meeseeksmachine/auto-backport-of-pr-28451-on-v3.9.x
Backport PR #28451 on branch v3.9.x (Fix GTK cairo backends)
2 parents 560fdc4 + c43313a commit 7fa9f24

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

lib/matplotlib/backends/backend_gtk3cairo.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ def on_draw_event(self, widget, ctx):
1313

1414
with (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
1515
else nullcontext()):
16-
self._renderer.set_context(ctx)
17-
scale = self.device_pixel_ratio
18-
# Scale physical drawing to logical size.
19-
ctx.scale(1 / scale, 1 / scale)
2016
allocation = self.get_allocation()
17+
# Render the background before scaling, as the allocated size here is in
18+
# logical pixels.
2119
Gtk.render_background(
2220
self.get_style_context(), ctx,
23-
allocation.x, allocation.y,
24-
allocation.width, allocation.height)
21+
0, 0, allocation.width, allocation.height)
22+
scale = self.device_pixel_ratio
23+
# Scale physical drawing to logical size.
24+
ctx.scale(1 / scale, 1 / scale)
25+
self._renderer.set_context(ctx)
26+
# Set renderer to physical size so it renders in full resolution.
27+
self._renderer.width = allocation.width * scale
28+
self._renderer.height = allocation.height * scale
2529
self._renderer.dpi = self.figure.dpi
2630
self.figure.draw(self._renderer)
2731

lib/matplotlib/backends/backend_gtk4.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class FigureCanvasGTK4(_FigureCanvasGTK, Gtk.DrawingArea):
3434
required_interactive_framework = "gtk4"
3535
supports_blit = False
3636
manager_class = _api.classproperty(lambda cls: FigureManagerGTK4)
37-
_context_is_scaled = False
3837

3938
def __init__(self, figure=None):
4039
super().__init__(figure=figure)
@@ -228,13 +227,8 @@ def _post_draw(self, widget, ctx):
228227

229228
lw = 1
230229
dash = 3
231-
if not self._context_is_scaled:
232-
x0, y0, w, h = (dim / self.device_pixel_ratio
233-
for dim in self._rubberband_rect)
234-
else:
235-
x0, y0, w, h = self._rubberband_rect
236-
lw *= self.device_pixel_ratio
237-
dash *= self.device_pixel_ratio
230+
x0, y0, w, h = (dim / self.device_pixel_ratio
231+
for dim in self._rubberband_rect)
238232
x1 = x0 + w
239233
y1 = y0 + h
240234

lib/matplotlib/backends/backend_gtk4cairo.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66

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

1013
def on_draw_event(self, widget, ctx):
1114
if self._idle_draw_id:
@@ -16,15 +19,11 @@ def on_draw_event(self, widget, ctx):
1619
with (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
1720
else nullcontext()):
1821
self._renderer.set_context(ctx)
19-
scale = self.device_pixel_ratio
20-
# Scale physical drawing to logical size.
21-
ctx.scale(1 / scale, 1 / scale)
2222
allocation = self.get_allocation()
2323
Gtk.render_background(
2424
self.get_style_context(), ctx,
2525
allocation.x, allocation.y,
2626
allocation.width, allocation.height)
27-
self._renderer.dpi = self.figure.dpi
2827
self.figure.draw(self._renderer)
2928

3029

0 commit comments

Comments
 (0)