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

Skip to content

Commit 89a87d0

Browse files
committed
Fix vector output from cairo.
Vector surfaces do not report their extents, so we need to pass them in manually.
1 parent 5087c05 commit 89a87d0

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

lib/matplotlib/backends/backend_cairo.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ def __init__(self, dpi):
106106

107107
def set_ctx_from_surface(self, surface):
108108
self.gc.ctx = cairo.Context(surface)
109-
self.set_width_height(surface.get_width(), surface.get_height())
109+
# Although it may appear natural to automatically call
110+
# `self.set_width_height(surface.get_width(), surface.get_height())`
111+
# here (instead of having the caller do so separately), this would fail
112+
# for PDF/PS/SVG surfaces, which have no way to report their extents.
110113

111114
def set_width_height(self, width, height):
112115
self.width = width
@@ -441,9 +444,9 @@ def print_png(self, fobj, *args, **kwargs):
441444
width, height = self.get_width_height()
442445

443446
renderer = RendererCairo(self.figure.dpi)
444-
renderer.set_width_height(width, height)
445447
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
446448
renderer.set_ctx_from_surface(surface)
449+
renderer.set_width_height(width, height)
447450

448451
self.figure.draw(renderer)
449452
surface.write_to_png(fobj)
@@ -500,6 +503,7 @@ def _save(self, fo, fmt, **kwargs):
500503
# surface.set_dpi() can be used
501504
renderer = RendererCairo(self.figure.dpi)
502505
renderer.set_ctx_from_surface(surface)
506+
renderer.set_width_height(width_in_points, height_in_points)
503507
ctx = renderer.gc.ctx
504508

505509
if orientation == 'landscape':

lib/matplotlib/backends/backend_qt5cairo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def paintEvent(self, event):
1515
surface = backend_cairo.cairo.ImageSurface(
1616
backend_cairo.cairo.FORMAT_ARGB32, width, height)
1717
self._renderer.set_ctx_from_surface(surface)
18+
self._renderer.set_width_height(width, height)
1819
self.figure.draw(self._renderer)
1920
buf = surface.get_data()
2021
qimage = QtGui.QImage(buf, width, height,

0 commit comments

Comments
 (0)