@@ -741,17 +741,16 @@ def configure_event(self, widget, event):
741741 if w == 1 or h == 1 : return # empty fig
742742
743743 # compute desired figure size in inches
744- dpival = self .figure .dpi .get ()
745- winch = w / dpival
746- hinch = h / dpival
744+ dpi = self .figure .dpi .get ()
745+ winch = w / dpi
746+ hinch = h / dpi
747747 self .figure .set_figsize_inches (winch , hinch )
748748
749749 self ._new_pixmap = True
750750 return True
751751
752752
753753 def draw (self ):
754- #if self._doplot:
755754 self ._new_pixmap = True
756755 self .expose_event (self , None )
757756
@@ -789,65 +788,69 @@ def print_figure(self, filename, dpi=150, facecolor='w', edgecolor='w',
789788 ext = ext [1 :]
790789 if ext == '' :
791790 ext = IMAGE_FORMAT_DEFAULT
792- filename = filename + '.' + ext
793-
794- if self .flags () & gtk .REALIZED == 0 :
795- gtk .DrawingArea .realize (self ) # for self.window(for pixmap) and figure sizing?
791+ filename = filename + '.' + ext
796792
797793 # save figure settings
798794 origDPI = self .figure .dpi .get ()
799795 origfacecolor = self .figure .get_facecolor ()
800796 origedgecolor = self .figure .get_edgecolor ()
801- origW , origH = int (self .figure .bbox .width ()), int (self .figure .bbox .height ())
797+ origWIn , origHIn = self .figure .get_size_inches ()
798+
799+ if self .flags () & gtk .REALIZED == 0 :
800+ # for self.window(for pixmap) and has a side effect of altering figure width,height
801+ gtk .DrawingArea .realize (self )
802802
803803 self .figure .dpi .set (dpi )
804804 self .figure .set_facecolor (facecolor )
805805 self .figure .set_edgecolor (edgecolor )
806806
807807 ext = ext .lower ()
808- if ext in ('eps' , 'ps' , 'svg' ,):
808+ if ext in ('jpg' , 'png' ): # native printing
809+ width , height = self .figure .get_width_height ()
810+ width , height = int (width ), int (height )
811+ pixmap = gtk .gdk .Pixmap (self .window , width , height )
812+ renderer = RendererGTK (self , pixmap , width , height ,
813+ self .figure .dpi )
814+
815+ self .figure .draw (renderer )
816+ gdk_pixmap_save (pixmap , filename , ext , width , height )
817+
818+ elif ext in ('eps' , 'ps' , 'svg' ,): # print through other backends
809819 if ext in ('svg' ,):
810820 from backend_svg import FigureCanvasSVG as FigureCanvas
811821 else :
812822 from backend_ps import FigureCanvasPS as FigureCanvas
813823 fc = self .switch_backends (FigureCanvas )
814824 fc .print_figure (filename , dpi , facecolor , edgecolor , orientation )
815825
816- elif ext in ('jpg' , 'png' ):
817- l ,b ,width , height = self .figure .bbox .get_bounds ()
818- width , height = int (width ), int (height )
819- pixmap = gtk .gdk .Pixmap (self .window , width , height )
820- renderer = RendererGTK (self , pixmap , width , height ,
821- self .figure .dpi )
822-
823- self .figure .draw (renderer )
824-
825- pixbuf = gtk .gdk .Pixbuf (gtk .gdk .COLORSPACE_RGB , 0 , 8 ,
826- width , height )
827- pixbuf .get_from_drawable (pixmap , self .window .get_colormap (),
828- 0 , 0 , 0 , 0 , width , height )
829-
830- # pixbuf.save() only recognises this name
831- if ext == 'jpg' : ext = 'jpeg'
832- try : pixbuf .save (filename , ext )
833- except gobject .GError , msg :
834- msg = raise_msg_to_str (msg )
835- error_msg ('Could not save figure to %s\n \n %s' % (
836- filename , msg ))
837826 else :
838827 error_msg ('Format "%s" is not supported.\n Supported formats are %s.' %
839828 (ext , ', ' .join (IMAGE_FORMAT )))
840829
841830 # restore figure settings
831+ self .figure .dpi .set (origDPI )
842832 self .figure .set_facecolor (origfacecolor )
843833 self .figure .set_edgecolor (origedgecolor )
844- self .figure .dpi .set (origDPI )
845- self .figure .set_figsize_inches (origW / origDPI , origH / origDPI )
834+ self .figure .set_figsize_inches (origWIn , origHIn )
846835
847836
848- #def set_do_plot(self, b):
849- # 'True if you want to render to screen, False is hardcopy only'
850- # self._doplot = b
837+ def gdk_pixmap_save (pixmap , filename , ext , width , height ):
838+ """Save a gdk.Pixmap as a png or jpg file.
839+ Can be called by other gtk backends after they have rendered to a pixmap.
840+ """
841+ # jpg colors don't match the display very well, png colors match better
842+ pixbuf = gtk .gdk .Pixbuf (gtk .gdk .COLORSPACE_RGB , 0 , 8 ,
843+ width , height )
844+ pixbuf .get_from_drawable (pixmap , pixmap .get_colormap (),
845+ 0 , 0 , 0 , 0 , width , height )
846+
847+ # pixbuf.save() recognises 'jpeg' not 'jpg'
848+ if ext == 'jpg' : ext = 'jpeg'
849+ try : pixbuf .save (filename , ext )
850+ except gobject .GError , msg :
851+ msg = raise_msg_to_str (msg )
852+ error_msg ('Could not save figure to %s\n \n %s' % (
853+ filename , msg ))
851854
852855
853856class FigureManagerGTK (FigureManagerBase ):
0 commit comments