Skip to content

Commit

Permalink
Merge pull request #2402 from pwuertz/fix_2342
Browse files Browse the repository at this point in the history
support tight_bbox for pgf output, fixes #2342 (v1.3.x)
  • Loading branch information
pwuertz committed Sep 10, 2013
2 parents 9b593ae + 5c411e0 commit 2449199
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions lib/matplotlib/backends/backend_pgf.py
Expand Up @@ -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.
Expand All @@ -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}")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tight_bbox.py
Expand Up @@ -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

0 comments on commit 2449199

Please sign in to comment.