@@ -849,7 +849,8 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
849849 file = codecs .getwriter ("utf-8" )(file )
850850 self ._print_pgf_to_fh (file , * args , ** kwargs )
851851
852- def _print_pdf_to_fh (self , fh , * args , metadata = None , ** kwargs ):
852+ def print_pdf (self , fname_or_fh , * args , metadata = None , ** kwargs ):
853+ """Use LaTeX to compile a pgf generated figure to pdf."""
853854 w , h = self .figure .get_figwidth (), self .figure .get_figheight ()
854855
855856 info_dict = _create_pdf_info_dict ('pgf' , metadata or {})
@@ -882,29 +883,22 @@ def _print_pdf_to_fh(self, fh, *args, metadata=None, **kwargs):
882883 [texcommand , "-interaction=nonstopmode" , "-halt-on-error" ,
883884 "figure.tex" ], _log , cwd = tmpdir )
884885
885- with (tmppath / "figure.pdf" ).open ("rb" ) as fh_src :
886- shutil .copyfileobj (fh_src , fh ) # copy file contents to target
886+ with (tmppath / "figure.pdf" ).open ("rb" ) as orig , \
887+ cbook .open_file_cm (fname_or_fh , "wb" ) as dest :
888+ shutil .copyfileobj (orig , dest ) # copy file contents to target
887889
888- def print_pdf (self , fname_or_fh , * args , ** kwargs ):
889- """Use LaTeX to compile a Pgf generated figure to PDF."""
890- with cbook .open_file_cm (fname_or_fh , "wb" ) as file :
891- self ._print_pdf_to_fh (file , * args , ** kwargs )
892-
893- def _print_png_to_fh (self , fh , * args , ** kwargs ):
890+ def print_png (self , fname_or_fh , * args , ** kwargs ):
891+ """Use LaTeX to compile a pgf figure to pdf and convert it to png."""
894892 converter = make_pdf_to_png_converter ()
895893 with TemporaryDirectory () as tmpdir :
896894 tmppath = pathlib .Path (tmpdir )
897895 pdf_path = tmppath / "figure.pdf"
898896 png_path = tmppath / "figure.png"
899897 self .print_pdf (pdf_path , * args , ** kwargs )
900898 converter (pdf_path , png_path , dpi = self .figure .dpi )
901- with png_path .open ("rb" ) as fh_src :
902- shutil .copyfileobj (fh_src , fh ) # copy file contents to target
903-
904- def print_png (self , fname_or_fh , * args , ** kwargs ):
905- """Use LaTeX to compile a pgf figure to pdf and convert it to png."""
906- with cbook .open_file_cm (fname_or_fh , "wb" ) as file :
907- self ._print_png_to_fh (file , * args , ** kwargs )
899+ with png_path .open ("rb" ) as orig , \
900+ cbook .open_file_cm (fname_or_fh , "wb" ) as dest :
901+ shutil .copyfileobj (orig , dest ) # copy file contents to target
908902
909903 def get_renderer (self ):
910904 return RendererPgf (self .figure , None )
0 commit comments