From 5c411e030af467a4a16a139306a74d6dfe30a631 Mon Sep 17 00:00:00 2001 From: pwuertz Date: Sat, 7 Sep 2013 20:20:02 +0200 Subject: [PATCH] support tight_bbox for pgf output, fixes #2342 (v1.3.x) --- lib/matplotlib/backends/backend_pgf.py | 25 +++++++++++++++++++++++-- lib/matplotlib/tight_bbox.py | 2 +- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/backends/backend_pgf.py b/lib/matplotlib/backends/backend_pgf.py index 917810b31518..058a7abcb747 100644 --- a/lib/matplotlib/backends/backend_pgf.py +++ b/lib/matplotlib/backends/backend_pgf.py @@ -390,7 +390,7 @@ def get_width_height_descent(self, text, prop): class RendererPgf(RendererBase): - def __init__(self, figure, fh): + def __init__(self, figure, fh, dummy=False): """ Creates a new PGF renderer that translates any drawing instruction into text commands to be interpreted in a latex pgfpicture environment. @@ -408,6 +408,13 @@ def __init__(self, figure, fh): # get LatexManager instance self.latexManager = LatexManagerFactory.get_latex_manager() + # dummy==True deactivate all methods + if dummy: + nop = lambda *args, **kwargs: None + for m in RendererPgf.__dict__.keys(): + if m.startswith("draw_"): + self.__dict__[m] = nop + def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None): writeln(self.fh, r"\begin{pgfscope}") @@ -741,6 +748,11 @@ def get_default_filetype(self): return 'pdf' def _print_pgf_to_fh(self, fh, *args, **kwargs): + if kwargs.get("dryrun", False): + renderer = RendererPgf(self.figure, None, dummy=True) + self.figure.draw(renderer) + return + header_text = r"""%% Creator: Matplotlib, PGF backend %% %% To include the figure in your LaTeX document, write @@ -797,6 +809,7 @@ def print_pgf(self, fname_or_fh, *args, **kwargs): rendered in latex documents. """ if kwargs.get("dryrun", False): + self._print_pgf_to_fh(None, *args, **kwargs) return # figure out where the pgf is to be written to @@ -859,6 +872,10 @@ def print_pdf(self, fname_or_fh, *args, **kwargs): """ Use LaTeX to compile a Pgf generated figure to PDF. """ + if kwargs.get("dryrun", False): + self._print_pgf_to_fh(None, *args, **kwargs) + return + # figure out where the pdf is to be written to if is_string_like(fname_or_fh): with open(fname_or_fh, "wb") as fh: @@ -892,6 +909,10 @@ def print_png(self, fname_or_fh, *args, **kwargs): """ Use LaTeX to compile a pgf figure to pdf and convert it to png. """ + if kwargs.get("dryrun", False): + self._print_pgf_to_fh(None, *args, **kwargs) + return + if is_string_like(fname_or_fh): with open(fname_or_fh, "wb") as fh: self._print_png_to_fh(fh, *args, **kwargs) @@ -901,7 +922,7 @@ def print_png(self, fname_or_fh, *args, **kwargs): raise ValueError("filename must be a path or a file-like object") def get_renderer(self): - return RendererPgf(self.figure, None) + return RendererPgf(self.figure, None, dummy=True) class FigureManagerPgf(FigureManagerBase): diff --git a/lib/matplotlib/tight_bbox.py b/lib/matplotlib/tight_bbox.py index 36c255b9fb92..a3305811de8e 100644 --- a/lib/matplotlib/tight_bbox.py +++ b/lib/matplotlib/tight_bbox.py @@ -131,5 +131,5 @@ def process_figure_for_rasterizing(figure, _adjust_bbox_handler_d = {} for format in ["png", "raw", "rgba", "jpg", "jpeg", "tiff"]: _adjust_bbox_handler_d[format] = adjust_bbox_png -for format in ["pdf", "eps", "svg", "svgz"]: +for format in ["pgf", "pdf", "eps", "svg", "svgz"]: _adjust_bbox_handler_d[format] = adjust_bbox_pdf