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

Skip to content

Commit 68786e6

Browse files
committed
Let cairo track surface size; fix Pyside refcnt bug.
1 parent eddbc7b commit 68786e6

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

lib/matplotlib/backends/backend_cairo.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,11 @@ def __init__(self, dpi):
103103

104104
def set_ctx_from_surface(self, surface):
105105
self.gc.ctx = cairo.Context(surface)
106+
self.set_width_height(surface.get_width(), surface.get_height())
106107

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

115112
def _fill_and_stroke(self, ctx, fill_c, alpha, alpha_overrides):
116113
if fill_c is not None:
@@ -306,11 +303,6 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
306303

307304
ctx.restore()
308305

309-
def flipy(self):
310-
return True
311-
#return False # tried - all draw objects ok except text (and images?)
312-
# which comes out mirrored!
313-
314306
def get_canvas_width_height(self):
315307
return self.width, self.height
316308

@@ -505,7 +497,6 @@ def _save(self, fo, fmt, **kwargs):
505497

506498
# surface.set_dpi() can be used
507499
renderer = RendererCairo(self.figure.dpi)
508-
renderer.set_width_height(width_in_points, height_in_points)
509500
renderer.set_ctx_from_surface(surface)
510501
ctx = renderer.gc.ctx
511502

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ def paintEvent(self, e):
6464
reg = self.copy_from_bbox(bbox)
6565
buf = reg.to_string_argb()
6666
qimage = QtGui.QImage(buf, w, h, QtGui.QImage.Format_ARGB32)
67+
# Adjust the buf reference count to work around a memory leak bug
68+
# in QImage under PySide on Python 3.
69+
if QT_API == 'PySide' and six.PY3:
70+
ctypes.c_long.from_address(id(buf)).value = 1
6771
if hasattr(qimage, 'setDevicePixelRatio'):
6872
# Not available on Qt4 or some older Qt5.
6973
qimage.setDevicePixelRatio(self._dpi_ratio)
7074
origin = QtCore.QPoint(l, self.renderer.height - t)
7175
painter.drawImage(origin / self._dpi_ratio, qimage)
72-
# Adjust the buf reference count to work around a memory
73-
# leak bug in QImage under PySide on Python 3.
74-
if QT_API == 'PySide' and six.PY3:
75-
ctypes.c_long.from_address(id(buf)).value = 1
7676

7777
self._draw_rect_callback(painter)
7878

lib/matplotlib/backends/backend_qt5cairo.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from .backend_cairo import RendererCairo
44
from .backend_qt5 import QtCore, QtGui, _BackendQT5, FigureCanvasQT
5+
from .qt_compat import QT_API
56

67

78
class FigureCanvasQTCairo(FigureCanvasQT):
@@ -14,11 +15,14 @@ def paintEvent(self, event):
1415
height = self.height()
1516
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
1617
self._renderer.set_ctx_from_surface(surface)
17-
# This should be really done by set_ctx_from_surface...
18-
self._renderer.set_width_height(width, height)
1918
self.figure.draw(self._renderer)
20-
qimage = QtGui.QImage(surface.get_data(), width, height,
19+
buf = surface.get_data()
20+
qimage = QtGui.QImage(buf, width, height,
2121
QtGui.QImage.Format_ARGB32_Premultiplied)
22+
# Adjust the buf reference count to work around a memory leak bug in
23+
# QImage under PySide on Python 3.
24+
if QT_API == 'PySide' and six.PY3:
25+
ctypes.c_long.from_address(id(buf)).value = 1
2226
painter = QtGui.QPainter(self)
2327
painter.drawImage(0, 0, qimage)
2428
self._draw_rect_callback(painter)

0 commit comments

Comments
 (0)