2525def _fn_name (): return sys ._getframe (1 ).f_code .co_name
2626
2727import cairo
28- _version_required = (1 ,0 , 2 )
28+ _version_required = (1 ,2 , 0 )
2929if cairo .version_info < _version_required :
3030 raise SystemExit ("Pycairo %d.%d.%d is installed\n "
3131 "Pycairo %d.%d.%d or later is required"
@@ -518,53 +518,39 @@ def new_figure_manager(num, *args, **kwargs): # called by backends/__init__.py
518518
519519
520520class FigureCanvasCairo (FigureCanvasBase ):
521- def print_figure (self , filename , dpi = 150 , facecolor = 'w' , edgecolor = 'w' ,
521+ def print_figure (self , fo , dpi = 150 , facecolor = 'w' , edgecolor = 'w' ,
522522 orientation = 'portrait' , ** kwargs ):
523523 if _debug : print '%s.%s()' % (self .__class__ .__name__ , _fn_name ())
524524 # settings for printing
525525 self .figure .dpi .set (dpi )
526526 self .figure .set_facecolor (facecolor )
527527 self .figure .set_edgecolor (edgecolor )
528528
529- if isinstance (filename , file ): # eg when do savefig(sys.stdout)
530- self ._save_png (filename ) # assume PNG format
529+ if isinstance (fo , file ): # eg when do savefig(sys.stdout)
530+ ext = 'png' # assume PNG format
531+ # should be able to save fileobject to other formats too,
532+ # print_figure() needs a 'format' argument
531533 else :
532- root , ext = os .path .splitext (filename )
534+ root , ext = os .path .splitext (fo )
533535 ext = ext [1 :]
534536 if ext == '' :
537+ fo = fo + '.' + ext
535538 ext = IMAGE_FORMAT_DEFAULT
536- filename = filename + '.' + ext
537-
538- ext = ext .lower ()
539- if ext in ('pdf' , 'png' , 'ps' ): # native formats
540- # try:
541- # fileObject = file(filename,'wb')
542- # except IOError, exc:
543- # warnings.warn("%s: %s" % (exc.filename, exc.strerror))
544- # else:
545-
546- if ext == 'png' :
547- self ._save_png (filename )
548- # _save_png (fileObject)
549- else :
550- self ._save_ps_pdf (self .figure , filename , ext , orientation ,
551- ** kwargs )
552- # self._save_ps_pdf (self.figure, fileObject, ext,
553- # orientation)
554- # fileObject.close()
555-
556- elif ext in ('eps' , 'svg' ): # backend_svg/ps
557- if ext == 'svg' :
558- from backend_svg import FigureCanvasSVG as FigureCanvas
559- else :
560- from backend_ps import FigureCanvasPS as FigureCanvas
561- fc = FigureCanvas (self .figure )
562- fc .print_figure (filename , dpi , facecolor , edgecolor ,
563- orientation , ** kwargs )
564- else :
565- warnings .warn ('Format "%s" is not supported.\n '
566- 'Supported formats: '
567- '%s.' % (ext , ', ' .join (IMAGE_FORMAT )))
539+
540+ ext = ext .lower ()
541+ if ext == 'png' :
542+ self ._save_png (fo )
543+ elif ext in ('pdf' , 'ps' , 'svg' ):
544+ self ._save (self .figure , fo , ext , orientation , ** kwargs )
545+ elif ext == 'eps' : # backend_ps for eps
546+ from backend_ps import FigureCanvasPS as FigureCanvas
547+ fc = FigureCanvas (self .figure )
548+ fc .print_figure (filename , dpi , facecolor , edgecolor ,
549+ orientation , ** kwargs )
550+ else :
551+ warnings .warn ('Format "%s" is not supported.\n '
552+ 'Supported formats: '
553+ '%s.' % (ext , ', ' .join (IMAGE_FORMAT )))
568554
569555
570556 def _save_png (self , fobj ):
@@ -579,9 +565,10 @@ def _save_png (self, fobj):
579565 surface .write_to_png (fobj )
580566
581567
582- def _save_ps_pdf (self , figure , filename , ext , orientation , ** kwargs ):
568+ def _save (self , figure , fo , ext , orientation , ** kwargs ):
569+ # save PDF/PS/SVG
583570 orientation = kwargs .get ('orientation' , 'portrait' )
584-
571+
585572 dpi = 72
586573 figure .dpi .set (dpi )
587574 w_in , h_in = figure .get_size_inches ()
@@ -595,14 +582,21 @@ def _save_ps_pdf (self, figure, filename, ext, orientation, **kwargs):
595582 if not cairo .HAS_PS_SURFACE :
596583 raise RuntimeError ('cairo has not been compiled with PS '
597584 'support enabled' )
598- surface = cairo .PSSurface (filename , width_in_points ,
599- height_in_points )
600- else : # pdf
585+ surface = cairo .PSSurface (fo , width_in_points , height_in_points )
586+ elif ext == 'pdf' :
601587 if not cairo .HAS_PDF_SURFACE :
602588 raise RuntimeError ('cairo has not been compiled with PDF '
603589 'support enabled' )
604- surface = cairo .PDFSurface (filename , width_in_points ,
605- height_in_points )
590+ surface = cairo .PDFSurface (fo , width_in_points , height_in_points )
591+ elif ext == 'svg' :
592+ if not cairo .HAS_SVG_SURFACE :
593+ raise RuntimeError ('cairo has not been compiled with SVG '
594+ 'support enabled' )
595+ surface = cairo .SVGSurface (fo , width_in_points , height_in_points )
596+ else :
597+ warnings .warn ("unknown extension: %s" % ext )
598+ return
599+
606600 # surface.set_dpi() can be used
607601 renderer = RendererCairo (figure .dpi )
608602 renderer .set_width_height (width_in_points , height_in_points )
@@ -634,3 +628,4 @@ def _save_ps_pdf (self, figure, filename, ext, orientation, **kwargs):
634628 ctx .show_text ('Origin corner' )
635629
636630 ctx .show_page ()
631+ surface .finish ()
0 commit comments