backend_gtk4agg.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import numpy as np
  2. from .. import cbook
  3. from . import backend_agg, backend_gtk4
  4. from .backend_gtk4 import GLib, Gtk, _BackendGTK4
  5. import cairo # Presence of cairo is already checked by _backend_gtk.
  6. class FigureCanvasGTK4Agg(backend_agg.FigureCanvasAgg,
  7. backend_gtk4.FigureCanvasGTK4):
  8. def on_draw_event(self, widget, ctx):
  9. if self._idle_draw_id:
  10. GLib.source_remove(self._idle_draw_id)
  11. self._idle_draw_id = 0
  12. self.draw()
  13. scale = self.device_pixel_ratio
  14. allocation = self.get_allocation()
  15. Gtk.render_background(
  16. self.get_style_context(), ctx,
  17. allocation.x, allocation.y,
  18. allocation.width, allocation.height)
  19. buf = cbook._unmultiplied_rgba8888_to_premultiplied_argb32(
  20. np.asarray(self.get_renderer().buffer_rgba()))
  21. height, width, _ = buf.shape
  22. image = cairo.ImageSurface.create_for_data(
  23. buf.ravel().data, cairo.FORMAT_ARGB32, width, height)
  24. image.set_device_scale(scale, scale)
  25. ctx.set_source_surface(image, 0, 0)
  26. ctx.paint()
  27. return False
  28. @_BackendGTK4.export
  29. class _BackendGTK4Agg(_BackendGTK4):
  30. FigureCanvas = FigureCanvasGTK4Agg