Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a8b8c27

Browse files
committed
make backends registerable
svn path=/trunk/matplotlib/; revision=8108
1 parent 1cf0f14 commit a8b8c27

2 files changed

Lines changed: 51 additions & 17 deletions

File tree

lib/matplotlib/backend_bases.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@
4040
import matplotlib.textpath as textpath
4141
from 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+
4352
class 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,

lib/matplotlib/tight_bbox.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from matplotlib.transforms import Bbox, TransformedBbox, Affine2D
77

88

9+
910
def adjust_bbox(fig, format, bbox_inches):
1011
"""
1112
Temporarily adjust the figure so that only the specified area
@@ -46,11 +47,9 @@ def restore_bbox():
4647
fig.transFigure.invalidate()
4748
fig.patch.set_bounds(0, 0, 1, 1)
4849

49-
if format in ["png", "raw", "rgba"]:
50-
adjust_bbox_png(fig, bbox_inches)
51-
return restore_bbox
52-
elif format in ["pdf", "eps", "svg", "svgz"]:
53-
adjust_bbox_pdf(fig, bbox_inches)
50+
adjust_bbox_handler = _adjust_bbox_handler_d.get(format)
51+
if adjust_bbox_handler is not None:
52+
adjust_bbox_handler(fig, bbox_inches)
5453
return restore_bbox
5554
else:
5655
warnings.warn("bbox_inches option for %s backend is not implemented yet." % (format))
@@ -125,3 +124,8 @@ def process_figure_for_rasterizing(figure,
125124
return bbox_inches, r
126125

127126

127+
_adjust_bbox_handler_d = {}
128+
for format in ["png", "raw", "rgba"]:
129+
_adjust_bbox_handler_d[format] = adjust_bbox_png
130+
for format in ["pdf", "eps", "svg", "svgz"]:
131+
_adjust_bbox_handler_d[format] = adjust_bbox_pdf

0 commit comments

Comments
 (0)