|
From: <lee...@us...> - 2010-02-03 19:42:06
|
Revision: 8108
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8108&view=rev
Author: leejjoon
Date: 2010-02-03 19:41:53 +0000 (Wed, 03 Feb 2010)
Log Message:
-----------
make backends registerable
Modified Paths:
--------------
trunk/matplotlib/lib/matplotlib/backend_bases.py
trunk/matplotlib/lib/matplotlib/tight_bbox.py
Modified: trunk/matplotlib/lib/matplotlib/backend_bases.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/backend_bases.py 2010-02-03 17:56:03 UTC (rev 8107)
+++ trunk/matplotlib/lib/matplotlib/backend_bases.py 2010-02-03 19:41:53 UTC (rev 8108)
@@ -40,6 +40,15 @@
import matplotlib.textpath as textpath
from matplotlib.path import Path
+
+
+_backend_d = {}
+
+def register_backend(format, backend_class):
+ _backend_d[format] = backend_class
+
+
+
class RendererBase:
"""An abstract base class to handle drawing/rendering operations.
@@ -1518,6 +1527,33 @@
groupings[name].sort()
return groupings
+
+ def _get_print_method(self, format):
+ method_name = 'print_%s' % format
+
+ # check for registered backends
+ if format in _backend_d:
+ backend_class = _backend_d[format]
+
+ def _print_method(*args, **kwargs):
+ backend = self.switch_backends(backend_class)
+ print_method = getattr(backend, method_name)
+ return print_method(*args, **kwargs)
+
+ return _print_method
+
+ if (format not in self.filetypes or
+ not hasattr(self, method_name)):
+ formats = self.filetypes.keys()
+ formats.sort()
+ raise ValueError(
+ 'Format "%s" is not supported.\n'
+ 'Supported formats: '
+ '%s.' % (format, ', '.join(formats)))
+
+ return getattr(self, method_name)
+
+
def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
orientation='portrait', format=None, **kwargs):
"""
@@ -1573,16 +1609,8 @@
filename = filename.rstrip('.') + '.' + format
format = format.lower()
- method_name = 'print_%s' % format
- if (format not in self.filetypes or
- not hasattr(self, method_name)):
- formats = self.filetypes.keys()
- formats.sort()
- raise ValueError(
- 'Format "%s" is not supported.\n'
- 'Supported formats: '
- '%s.' % (format, ', '.join(formats)))
-
+ print_method = self._get_print_method(format)
+
if dpi is None:
dpi = rcParams['savefig.dpi']
@@ -1609,7 +1637,8 @@
# the backend to support file-like object, i'm going
# to leave it as it is. However, a better solution
# than stringIO seems to be needed. -JJL
- result = getattr(self, method_name)(
+ #result = getattr(self, method_name)(
+ result = print_method(
cStringIO.StringIO(),
dpi=dpi,
facecolor=facecolor,
@@ -1642,7 +1671,8 @@
_bbox_inches_restore = None
try:
- result = getattr(self, method_name)(
+ #result = getattr(self, method_name)(
+ result = print_method(
filename,
dpi=dpi,
facecolor=facecolor,
Modified: trunk/matplotlib/lib/matplotlib/tight_bbox.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/tight_bbox.py 2010-02-03 17:56:03 UTC (rev 8107)
+++ trunk/matplotlib/lib/matplotlib/tight_bbox.py 2010-02-03 19:41:53 UTC (rev 8108)
@@ -6,6 +6,7 @@
from matplotlib.transforms import Bbox, TransformedBbox, Affine2D
+
def adjust_bbox(fig, format, bbox_inches):
"""
Temporarily adjust the figure so that only the specified area
@@ -46,12 +47,10 @@
fig.transFigure.invalidate()
fig.patch.set_bounds(0, 0, 1, 1)
- if format in ["png", "raw", "rgba"]:
- adjust_bbox_png(fig, bbox_inches)
+ adjust_bbox_handler = _adjust_bbox_handler_d.get(format)
+ if adjust_bbox_handler is not None:
+ adjust_bbox_handler(fig, bbox_inches)
return restore_bbox
- elif format in ["pdf", "eps", "svg", "svgz"]:
- adjust_bbox_pdf(fig, bbox_inches)
- return restore_bbox
else:
warnings.warn("bbox_inches option for %s backend is not implemented yet." % (format))
return None
@@ -125,3 +124,8 @@
return bbox_inches, r
+_adjust_bbox_handler_d = {}
+for format in ["png", "raw", "rgba"]:
+ _adjust_bbox_handler_d[format] = adjust_bbox_png
+for format in ["pdf", "eps", "svg", "svgz"]:
+ _adjust_bbox_handler_d[format] = adjust_bbox_pdf
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|