backend_gtk4agg.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import numpy as np
  2. from .. import cbook
  3. from . import backend_agg, backend_gtk4
  4. from .backend_gtk4 import 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. scale = self.device_pixel_ratio
  10. allocation = self.get_allocation()
  11. Gtk.render_background(
  12. self.get_style_context(), ctx,
  13. allocation.x, allocation.y,
  14. allocation.width, allocation.height)
  15. buf = cbook._unmultiplied_rgba8888_to_premultiplied_argb32(
  16. np.asarray(self.get_renderer().buffer_rgba()))
  17. height, width, _ = buf.shape
  18. image = cairo.ImageSurface.create_for_data(
  19. buf.ravel().data, cairo.FORMAT_ARGB32, width, height)
  20. image.set_device_scale(scale, scale)
  21. ctx.set_source_surface(image, 0, 0)
  22. ctx.paint()
  23. return False
  24. @_BackendGTK4.export
  25. class _BackendGTK4Agg(_BackendGTK4):
  26. FigureCanvas = FigureCanvasGTK4Agg