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

Skip to content

Commit bcb08e9

Browse files
committed
Let cairo track surface size; fix Pyside refcnt bug.
1 parent f884ffa commit bcb08e9

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
@@ -102,14 +102,11 @@ def __init__(self, dpi):
102102

103103
def set_ctx_from_surface(self, surface):
104104
self.gc.ctx = cairo.Context(surface)
105+
self.set_width_height(surface.get_width(), surface.get_height())
105106

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

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

304301
ctx.restore()
305302

306-
def flipy(self):
307-
return True
308-
#return False # tried - all draw objects ok except text (and images?)
309-
# which comes out mirrored!
310-
311303
def get_canvas_width_height(self):
312304
return self.width, self.height
313305

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

504496
# surface.set_dpi() can be used
505497
renderer = RendererCairo(self.figure.dpi)
506-
renderer.set_width_height(width_in_points, height_in_points)
507498
renderer.set_ctx_from_surface(surface)
508499
ctx = renderer.gc.ctx
509500

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ def paintEvent(self, e):
7878
reg = self.copy_from_bbox(bbox)
7979
buf = reg.to_string_argb()
8080
qimage = QtGui.QImage(buf, w, h, QtGui.QImage.Format_ARGB32)
81+
# Adjust the buf reference count to work around a memory leak bug
82+
# in QImage under PySide on Python 3.
83+
if QT_API == 'PySide' and six.PY3:
84+
ctypes.c_long.from_address(id(buf)).value = 1
8185
if hasattr(qimage, 'setDevicePixelRatio'):
8286
# Not available on Qt4 or some older Qt5.
8387
qimage.setDevicePixelRatio(self._dpi_ratio)
8488
origin = QtCore.QPoint(l, self.renderer.height - t)
8589
painter.drawImage(origin / self._dpi_ratio, qimage)
86-
# Adjust the buf reference count to work around a memory
87-
# leak bug in QImage under PySide on Python 3.
88-
if QT_API == 'PySide' and six.PY3:
89-
ctypes.c_long.from_address(id(buf)).value = 1
9090

9191
self._draw_rect_callback(painter)
9292

lib/matplotlib/backends/backend_qt5cairo.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from . import backend_cairo # Keep the RendererCairo class swappable.
22
from .backend_qt5 import QtCore, QtGui, _BackendQT5, FigureCanvasQT
3+
from .qt_compat import QT_API
34

45

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

0 commit comments

Comments
 (0)