From: <evi...@us...> - 2009-07-26 17:54:02
|
Revision: 7295 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7295&view=rev Author: evilguru Date: 2009-07-26 17:53:54 +0000 (Sun, 26 Jul 2009) Log Message: ----------- Make mathtex an optional dependency. Warn when trying to render math when it is not available. Modified Paths: -------------- branches/mathtex/lib/matplotlib/backends/backend_agg.py branches/mathtex/lib/matplotlib/backends/backend_cairo.py branches/mathtex/lib/matplotlib/backends/backend_gdk.py branches/mathtex/lib/matplotlib/backends/backend_macosx.py branches/mathtex/lib/matplotlib/backends/backend_pdf.py branches/mathtex/lib/matplotlib/backends/backend_ps.py branches/mathtex/lib/matplotlib/backends/backend_svg.py Modified: branches/mathtex/lib/matplotlib/backends/backend_agg.py =================================================================== --- branches/mathtex/lib/matplotlib/backends/backend_agg.py 2009-07-26 00:06:34 UTC (rev 7294) +++ branches/mathtex/lib/matplotlib/backends/backend_agg.py 2009-07-26 17:53:54 UTC (rev 7295) @@ -21,6 +21,7 @@ * integrate screen dpi w/ ppi and text """ from __future__ import division +import warnings import numpy as npy @@ -37,8 +38,12 @@ from _backend_agg import RendererAgg as _RendererAgg from matplotlib import _png -from mathtex.mathtex_main import Mathtex -from mathtex.backends.backend_image import MathtexBackendImage +try: + from mathtex.mathtex_main import Mathtex + from mathtex.backends.backend_image import MathtexBackendImage + HAVE_MATHTEX = True +except ImportError: + HAVE_MATHTEX = False backend_version = 'v2.2' @@ -104,12 +109,13 @@ """ if __debug__: verbose.report('RendererAgg.draw_mathtext', 'debug-annoying') - m = Mathtex(s, rcParams['mathtext.fontset'], prop.get_size_in_points(), self.dpi) - b = MathtexBackendImage() + if HAVE_MATHTEX: + m = Mathtex(s, rcParams['mathtext.fontset'], prop.get_size_in_points(), self.dpi) + b = MathtexBackendImage() - m.render_to_backend(b) + m.render_to_backend(b) - self._renderer.draw_text_image(b.image, int(x), int(y) + 1, angle, gc) + self._renderer.draw_text_image(b.image, int(x), int(y) + 1, angle, gc) def draw_text(self, gc, x, y, s, prop, angle, ismath): """ @@ -153,8 +159,13 @@ return w, h, d if ismath: - m = Mathtex(s, rcParams['mathtext.fontset'], prop.get_size_in_points(), self.dpi) - return m.width, m.height, m.depth + if not HAVE_MATHTEX: + warnings.warn('matplotlib was compiled without mathtex support. ' + + 'Math will not be rendered.') + return 0.0, 0.0, 0.0 + else: + m = Mathtex(s, rcParams['mathtext.fontset'], prop.get_size_in_points(), self.dpi) + return m.width, m.height, m.depth font = self._get_agg_font(prop) font.set_text(s, 0.0, flags=LOAD_FORCE_AUTOHINT) # the width and height of unrotated string w, h = font.get_width_height() @@ -250,7 +261,7 @@ >>> x1, y1, x2, y2 = region.get_extents() >>> renderer.restore_region(region, bbox=(x1+dx, y1, x2, y2), xy=(x1-dx, y1)) - + """ if bbox is not None or xy is not None: if bbox is None: Modified: branches/mathtex/lib/matplotlib/backends/backend_cairo.py =================================================================== --- branches/mathtex/lib/matplotlib/backends/backend_cairo.py 2009-07-26 00:06:34 UTC (rev 7294) +++ branches/mathtex/lib/matplotlib/backends/backend_cairo.py 2009-07-26 17:53:54 UTC (rev 7295) @@ -47,8 +47,12 @@ from matplotlib.font_manager import ttfFontProperty from matplotlib import rcParams -from mathtex.mathtex_main import Mathtex -from mathtex.backends.backend_cairo import MathtexBackendCairo +try: + from mathtex.mathtex_main import Mathtex + from mathtex.backends.backend_cairo import MathtexBackendCairo + HAVE_MATHTEX = True +except ImportError: + HAVE_MATHTEX = False _debug = False #_debug = True @@ -202,6 +206,7 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle): if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name()) + if not HAVE_MATHTEX: return ctx = gc.ctx @@ -234,8 +239,13 @@ def get_text_width_height_descent(self, s, prop, ismath): if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name()) if ismath: - m = Mathtex(s, rcParams['mathtext.fontset'], prop.get_size_in_points(), self.dpi) - return m.width, m.height, m.depth + if HAVE_MATHTEX: + m = Mathtex(s, rcParams['mathtext.fontset'], prop.get_size_in_points(), self.dpi) + return m.width, m.height, m.depth + else: + warnings.warn('matplotlib was compiled without mathtex support. ' + + 'Math will not be rendered.') + return 0.0, 0.0, 0.0 ctx = self.text_ctx ctx.save() Modified: branches/mathtex/lib/matplotlib/backends/backend_gdk.py =================================================================== --- branches/mathtex/lib/matplotlib/backends/backend_gdk.py 2009-07-26 00:06:34 UTC (rev 7294) +++ branches/mathtex/lib/matplotlib/backends/backend_gdk.py 2009-07-26 17:53:54 UTC (rev 7295) @@ -27,8 +27,12 @@ from matplotlib.transforms import Affine2D from matplotlib.backends._backend_gdk import pixbuf_get_pixels_array -from mathtex.mathtex_main import Mathtex -from mathtex.backends.backend_image import MathtexBackendImage +try: + from mathtex.mathtex_main import Mathtex + from mathtex.backends.backend_image import MathtexBackendImage + HAVE_MATHTEX = True +except ImportError: + HAVE_MATHTEX = False backend_version = "%d.%d.%d" % gtk.pygtk_version _debug = False @@ -159,6 +163,9 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle): + if not HAVE_MATHTEX: + return + m = Mathtex(s, matplotlib.rcParams['mathtext.fontset'], prop.get_size_in_points(), self.dpi) b = MathtexBackendImage() @@ -305,9 +312,14 @@ def get_text_width_height_descent(self, s, prop, ismath): if ismath: - m = Mathtex(s, matplotlib.rcParams['mathtext.fontset'], - prop.get_size_in_points(), self.dpi) - return m.width, m.height, m.depth + if not HAVE_MATHTEX: + warnings.warn('matplotlib was compiled without mathtex support. ' + + 'Math will not be rendered.') + return 0.0, 0.0, 0.0 + else: + m = Mathtex(s, matplotlib.rcParams['mathtext.fontset'], + prop.get_size_in_points(), self.dpi) + return m.width, m.height, m.depth layout, inkRect, logicalRect = self._get_pango_layout(s, prop) l, b, w, h = inkRect Modified: branches/mathtex/lib/matplotlib/backends/backend_macosx.py =================================================================== --- branches/mathtex/lib/matplotlib/backends/backend_macosx.py 2009-07-26 00:06:34 UTC (rev 7294) +++ branches/mathtex/lib/matplotlib/backends/backend_macosx.py 2009-07-26 17:53:54 UTC (rev 7295) @@ -11,8 +11,12 @@ from matplotlib.path import Path from matplotlib.colors import colorConverter -from mathtex.mathtex_main import Mathtex -from mathtex.backends.backend_image import MathtexBackendImage +try: + from mathtex.mathtex_main import Mathtex + from mathtex.backends.backend_image import MathtexBackendImage + HAVE_MATHTEX = True +except ImportError: + HAVE_MATHTEX = False from matplotlib.widgets import SubplotTool @@ -90,6 +94,9 @@ gc.draw_mathtext(x, y, angle, Z) def _draw_mathtext(self, gc, x, y, s, prop, angle): + if not HAVE_MATHTEX: + return + m = Mathtex(s, rcParams['mathtext.fontset'], prop.get_size_in_points(), self.dpi) b = MathtexBackendImage() m.render_to_backend(b) @@ -116,9 +123,14 @@ renderer=self) return w, h, d if ismath: - m = Mathtex(s, rcParams['mathtext.fontset'], - prop.get_size_in_points(), self.dpi) - return m.width, m.height, m.depth + if HAVE_MATHTEX: + m = Mathtex(s, rcParams['mathtext.fontset'], + prop.get_size_in_points(), self.dpi) + return m.width, m.height, m.depth + else: + warnings.warn('matplotlib was compiled without mathtex support. ' + + 'Math will not be rendered.') + return 0.0, 0.0, 0.0 family = prop.get_family() weight = prop.get_weight() style = prop.get_style() Modified: branches/mathtex/lib/matplotlib/backends/backend_pdf.py =================================================================== --- branches/mathtex/lib/matplotlib/backends/backend_pdf.py 2009-07-26 00:06:34 UTC (rev 7294) +++ branches/mathtex/lib/matplotlib/backends/backend_pdf.py 2009-07-26 17:53:54 UTC (rev 7295) @@ -1,7 +1,7 @@ # -*- coding: iso-8859-1 -*- """ A PDF matplotlib backend (not yet complete) -Author: Jouni K Sepp\xE4nen <jk...@ik...> +Author: Jouni K Sepp�nen <jk...@ik...> """ from __future__ import division @@ -42,7 +42,11 @@ from matplotlib.path import Path from matplotlib import ttconv -from mathtex.mathtex_main import Mathtex +try: + from mathtex.mathtex_main import Mathtex + HAVE_MATHTEX = True +except ImportError: + HAVE_MATHTEX = False # Overview # @@ -1344,6 +1348,9 @@ def draw_mathtext(self, gc, x, y, s, prop, angle): # TODO: fix positioning and encoding + if not HAVE_MATHTEX: + return + m = Mathtex(s, rcParams['mathtext.fontset'], prop.get_size_in_points(), 72.0) # Generate the dict of used characters @@ -1654,9 +1661,15 @@ return w, h, d if ismath: - m = Mathtex(s, rcParams['mathtext.fontset'], - prop.get_size_in_points(), 72.0) - w, h, d = m.width, m.height, m.depth + # Ensure mathtex is available + if not HAVE_MATHTEX: + warnings.warn('matplotlib was compiled without mathtex support. ' + + 'Math will not be rendered.') + w, h, d = 0.0, 0.0, 0.0 + else: + m = Mathtex(s, rcParams['mathtext.fontset'], + prop.get_size_in_points(), 72.0) + w, h, d = m.width, m.height, m.depth elif rcParams['pdf.use14corefonts']: font = self._get_font_afm(prop) Modified: branches/mathtex/lib/matplotlib/backends/backend_ps.py =================================================================== --- branches/mathtex/lib/matplotlib/backends/backend_ps.py 2009-07-26 00:06:34 UTC (rev 7294) +++ branches/mathtex/lib/matplotlib/backends/backend_ps.py 2009-07-26 17:53:54 UTC (rev 7295) @@ -3,7 +3,7 @@ """ from __future__ import division -import glob, math, os, shutil, sys, time +import glob, math, os, shutil, sys, time, warnings def _fn_name(): return sys._getframe(1).f_code.co_name try: @@ -34,7 +34,11 @@ from matplotlib.backends.backend_mixed import MixedModeRenderer -from mathtex.mathtex_main import Mathtex +try: + from mathtex.mathtex_main import Mathtex + HAVE_MATHTEX = True +except ImportError: + HAVE_MATHTEX = False import numpy as npy import binascii @@ -277,8 +281,13 @@ return w, h, d if ismath: - m = Mathtex(s, rcParams['mathtext.fontset'], prop.get_size_in_points(), 72.0) - return m.width, m.height, m.depth + if HAVE_MATHTEX: + m = Mathtex(s, rcParams['mathtext.fontset'], prop.get_size_in_points(), 72.0) + return m.width, m.height, m.depth + else: + warnings.warn('matplotlib was compiled without mathtex support. ' + + 'Math will not be rendered.') + return 0.0, 0.0, 0.0 if rcParams['ps.useafm']: if ismath: s = s[1:-1] @@ -738,6 +747,8 @@ """ if debugPS: self._pswriter.write("% mathtext\n") + if not HAVE_MATHTEX: + return m = Mathtex(s, rcParams['mathtext.fontset'], prop.get_size_in_points(), 72.0) Modified: branches/mathtex/lib/matplotlib/backends/backend_svg.py =================================================================== --- branches/mathtex/lib/matplotlib/backends/backend_svg.py 2009-07-26 00:06:34 UTC (rev 7294) +++ branches/mathtex/lib/matplotlib/backends/backend_svg.py 2009-07-26 17:53:54 UTC (rev 7295) @@ -1,6 +1,6 @@ from __future__ import division -import os, codecs, base64, tempfile, urllib, gzip, cStringIO +import os, codecs, base64, tempfile, urllib, gzip, cStringIO, warnings try: from hashlib import md5 @@ -20,7 +20,11 @@ from matplotlib.transforms import Affine2D from matplotlib import _png -from mathtex.mathtex_main import Mathtex +try: + from mathtex.mathtex_main import Mathtex + HAVE_MATHTEX = True +except ImportError: + HAVE_MATHTEX = False from xml.sax.saxutils import escape as escape_xml_text @@ -487,6 +491,9 @@ """ Draw math text using mathtex """ + if not HAVE_MATHTEX: + return + m = Mathtex(s, rcParams['mathtext.fontset'], prop.get_size_in_points(), 72.0) # Extract the glyphs and rects to render @@ -592,9 +599,14 @@ def get_text_width_height_descent(self, s, prop, ismath): if ismath: - m = Mathtex(s, rcParams['mathtext.fontset'], - prop.get_size_in_points(), 72.0) - return m.width, m.height, m.depth + if HAVE_MATHTEX: + m = Mathtex(s, rcParams['mathtext.fontset'], + prop.get_size_in_points(), 72.0) + return m.width, m.height, m.depth + else: + warnings.warn('matplotlib was compiled without mathtex support. ' + + 'Math will not be rendered.') + return 0.0, 0.0, 0.0 font = self._get_font(prop) font.set_text(s, 0.0, flags=LOAD_NO_HINTING) w, h = font.get_width_height() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |