4040import matplotlib .textpath as textpath
4141from matplotlib .path import Path
4242
43+
44+
45+ _backend_d = {}
46+
47+ def register_backend (format , backend_class ):
48+ _backend_d [format ] = backend_class
49+
50+
51+
4352class RendererBase :
4453 """An abstract base class to handle drawing/rendering operations.
4554
@@ -1518,6 +1527,33 @@ def get_supported_filetypes_grouped(self):
15181527 groupings [name ].sort ()
15191528 return groupings
15201529
1530+
1531+ def _get_print_method (self , format ):
1532+ method_name = 'print_%s' % format
1533+
1534+ # check for registered backends
1535+ if format in _backend_d :
1536+ backend_class = _backend_d [format ]
1537+
1538+ def _print_method (* args , ** kwargs ):
1539+ backend = self .switch_backends (backend_class )
1540+ print_method = getattr (backend , method_name )
1541+ return print_method (* args , ** kwargs )
1542+
1543+ return _print_method
1544+
1545+ if (format not in self .filetypes or
1546+ not hasattr (self , method_name )):
1547+ formats = self .filetypes .keys ()
1548+ formats .sort ()
1549+ raise ValueError (
1550+ 'Format "%s" is not supported.\n '
1551+ 'Supported formats: '
1552+ '%s.' % (format , ', ' .join (formats )))
1553+
1554+ return getattr (self , method_name )
1555+
1556+
15211557 def print_figure (self , filename , dpi = None , facecolor = 'w' , edgecolor = 'w' ,
15221558 orientation = 'portrait' , format = None , ** kwargs ):
15231559 """
@@ -1573,16 +1609,8 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
15731609 filename = filename .rstrip ('.' ) + '.' + format
15741610 format = format .lower ()
15751611
1576- method_name = 'print_%s' % format
1577- if (format not in self .filetypes or
1578- not hasattr (self , method_name )):
1579- formats = self .filetypes .keys ()
1580- formats .sort ()
1581- raise ValueError (
1582- 'Format "%s" is not supported.\n '
1583- 'Supported formats: '
1584- '%s.' % (format , ', ' .join (formats )))
1585-
1612+ print_method = self ._get_print_method (format )
1613+
15861614 if dpi is None :
15871615 dpi = rcParams ['savefig.dpi' ]
15881616
@@ -1609,7 +1637,8 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
16091637 # the backend to support file-like object, i'm going
16101638 # to leave it as it is. However, a better solution
16111639 # than stringIO seems to be needed. -JJL
1612- result = getattr (self , method_name )(
1640+ #result = getattr(self, method_name)(
1641+ result = print_method (
16131642 cStringIO .StringIO (),
16141643 dpi = dpi ,
16151644 facecolor = facecolor ,
@@ -1642,7 +1671,8 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
16421671 _bbox_inches_restore = None
16431672
16441673 try :
1645- result = getattr (self , method_name )(
1674+ #result = getattr(self, method_name)(
1675+ result = print_method (
16461676 filename ,
16471677 dpi = dpi ,
16481678 facecolor = facecolor ,
0 commit comments