Skip to content

Fix PdfPages+cairo. #9114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2081,27 +2081,25 @@ def get_supported_filetypes_grouped(cls):
groupings[name].sort()
return groupings

def _get_output_canvas(self, format):
"""Return a canvas that is suitable for saving figures to a specified
file format. If necessary, this function will switch to a registered
backend that supports the format.
def _get_output_canvas(self, fmt):
"""
method_name = 'print_%s' % format
Return a canvas suitable for saving figures to a specified file format.

# check if this canvas supports the requested format
If necessary, this function will switch to a registered backend that
supports the format.
"""
method_name = 'print_%s' % fmt
# Return the current canvas if it supports the requested format.
if hasattr(self, method_name):
return self

# check if there is a default canvas for the requested format
canvas_class = get_registered_canvas_class(format)
# Return a default canvas for the requested format, if it exists.
canvas_class = get_registered_canvas_class(fmt)
if canvas_class:
return self.switch_backends(canvas_class)

# else report error for unsupported format
formats = sorted(self.get_supported_filetypes())
raise ValueError('Format "%s" is not supported.\n'
'Supported formats: '
'%s.' % (format, ', '.join(formats)))
# Else report error for unsupported format.
raise ValueError(
"Format {!r} is not supported (supported formats: {})"
.format(fmt, ", ".join(sorted(self.get_supported_filetypes()))))

def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
orientation='portrait', format=None, **kwargs):
Expand Down
23 changes: 13 additions & 10 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2523,18 +2523,21 @@ def savefig(self, figure=None, **kwargs):
instance is provided, this figure is saved. If an int is specified,
the figure instance to save is looked up by number.
"""
if isinstance(figure, Figure):
figure.savefig(self, format='pdf', **kwargs)
else:
if not isinstance(figure, Figure):
if figure is None:
figureManager = Gcf.get_active()
else:
figureManager = Gcf.get_fig_manager(figure)
if figureManager is None:
raise ValueError("No such figure: " + repr(figure))
manager = Gcf.get_active()
else:
figureManager.canvas.figure.savefig(self, format='pdf',
**kwargs)
manager = Gcf.get_fig_manager(figure)
if manager is None:
raise ValueError("No figure {}".format(figure))
figure = manager.canvas.figure
# Force use of pdf backend, as PdfPages is tightly coupled with it.
try:
orig_canvas = figure.canvas
figure.canvas = FigureCanvasPdf(figure)
figure.savefig(self, format="pdf", **kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we were already using the pdf backend? How much extra work would this create? Does the new canvas need to get explicitly destroyed to free memory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating the FigureCanvasPdf is really cheap (it is defined just below -- its init is directly inherited from FigureCanvasBase, which doesn't do anything fancy either).
The temporary canvas will get GC'd per normal python semantics.

finally:
figure.canvas = orig_canvas

def get_pagecount(self):
"""
Expand Down