Skip to content

Commit

Permalink
Merge pull request #28506 from meeseeksmachine/auto-backport-of-pr-28…
Browse files Browse the repository at this point in the history
…451-on-v3.9.x

Backport PR #28451 on branch v3.9.x (Fix GTK cairo backends)
  • Loading branch information
QuLogic authored Jul 4, 2024
2 parents 560fdc4 + c43313a commit 7fa9f24
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 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
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

0 comments on commit 7fa9f24

Please sign in to comment.