Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Let cairo track surface size; fix Pyside refcnt bug.
  • Loading branch information
anntzer committed Jan 9, 2018
commit bcb08e93a7a82441d58bc2ed3035a1f4a3db6558
11 changes: 1 addition & 10 deletions lib/matplotlib/backends/backend_cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,11 @@ def __init__(self, dpi):

def set_ctx_from_surface(self, surface):
self.gc.ctx = cairo.Context(surface)
self.set_width_height(surface.get_width(), surface.get_height())

def set_width_height(self, width, height):
self.width = width
self.height = height
self.matrix_flipy = cairo.Matrix(yy=-1, y0=self.height)
# use matrix_flipy for ALL rendering?
# - problem with text? - will need to switch matrix_flipy off, or do a
# font transform?

def _fill_and_stroke(self, ctx, fill_c, alpha, alpha_overrides):
if fill_c is not None:
Expand Down Expand Up @@ -303,11 +300,6 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):

ctx.restore()

def flipy(self):
return True
#return False # tried - all draw objects ok except text (and images?)
# which comes out mirrored!

def get_canvas_width_height(self):
return self.width, self.height

Expand Down Expand Up @@ -503,7 +495,6 @@ def _save(self, fo, fmt, **kwargs):

# surface.set_dpi() can be used
renderer = RendererCairo(self.figure.dpi)
renderer.set_width_height(width_in_points, height_in_points)
renderer.set_ctx_from_surface(surface)
ctx = renderer.gc.ctx

Expand Down
8 changes: 4 additions & 4 deletions lib/matplotlib/backends/backend_qt5agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ def paintEvent(self, e):
reg = self.copy_from_bbox(bbox)
buf = reg.to_string_argb()
qimage = QtGui.QImage(buf, w, h, QtGui.QImage.Format_ARGB32)
# Adjust the buf reference count to work around a memory leak bug
# in QImage under PySide on Python 3.
if QT_API == 'PySide' and six.PY3:
ctypes.c_long.from_address(id(buf)).value = 1
if hasattr(qimage, 'setDevicePixelRatio'):
# Not available on Qt4 or some older Qt5.
qimage.setDevicePixelRatio(self._dpi_ratio)
origin = QtCore.QPoint(l, self.renderer.height - t)
painter.drawImage(origin / self._dpi_ratio, qimage)
# Adjust the buf reference count to work around a memory
# leak bug in QImage under PySide on Python 3.
if QT_API == 'PySide' and six.PY3:
ctypes.c_long.from_address(id(buf)).value = 1

self._draw_rect_callback(painter)

Expand Down
10 changes: 7 additions & 3 deletions lib/matplotlib/backends/backend_qt5cairo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from . import backend_cairo # Keep the RendererCairo class swappable.
from .backend_qt5 import QtCore, QtGui, _BackendQT5, FigureCanvasQT
from .qt_compat import QT_API


class FigureCanvasQTCairo(FigureCanvasQT):
Expand All @@ -13,11 +14,14 @@ def paintEvent(self, event):
surface = backend_cairo.cairo.ImageSurface(
backend_cairo.cairo.FORMAT_ARGB32, width, height)
self._renderer.set_ctx_from_surface(surface)
# This should be really done by set_ctx_from_surface...
self._renderer.set_width_height(width, height)
self.figure.draw(self._renderer)
qimage = QtGui.QImage(surface.get_data(), width, height,
buf = surface.get_data()
qimage = QtGui.QImage(buf, width, height,
QtGui.QImage.Format_ARGB32_Premultiplied)
# Adjust the buf reference count to work around a memory leak bug in
# QImage under PySide on Python 3.
if QT_API == 'PySide' and six.PY3:
ctypes.c_long.from_address(id(buf)).value = 1
painter = QtGui.QPainter(self)
painter.drawImage(0, 0, qimage)
self._draw_rect_callback(painter)
Expand Down