From: <md...@us...> - 2007-08-21 15:19:33
|
Revision: 3722 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3722&view=rev Author: mdboom Date: 2007-08-21 08:19:04 -0700 (Tue, 21 Aug 2007) Log Message: ----------- Prevent the mathtext cache from getting out of hand, by clearing it after every plot. Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/backends/backend_agg.py trunk/matplotlib/lib/matplotlib/backends/backend_agg2.py trunk/matplotlib/lib/matplotlib/backends/backend_cairo.py trunk/matplotlib/lib/matplotlib/backends/backend_gdk.py trunk/matplotlib/lib/matplotlib/backends/backend_pdf.py trunk/matplotlib/lib/matplotlib/backends/backend_ps.py trunk/matplotlib/lib/matplotlib/backends/backend_qt.py trunk/matplotlib/lib/matplotlib/backends/backend_qt4.py trunk/matplotlib/lib/matplotlib/backends/backend_svg.py trunk/matplotlib/lib/matplotlib/mathtext.py Modified: trunk/matplotlib/lib/matplotlib/backends/backend_agg.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_agg.py 2007-08-21 15:13:49 UTC (rev 3721) +++ trunk/matplotlib/lib/matplotlib/backends/backend_agg.py 2007-08-21 15:19:04 UTC (rev 3722) @@ -83,7 +83,7 @@ from matplotlib.figure import Figure from matplotlib.font_manager import findfont from matplotlib.ft2font import FT2Font, LOAD_DEFAULT -from matplotlib.mathtext import math_parse_s_ft2font +from matplotlib.mathtext import MathTextParser from matplotlib.transforms import lbwh_to_bbox from _backend_agg import RendererAgg as _RendererAgg @@ -125,8 +125,8 @@ self.copy_from_bbox = self._renderer.copy_from_bbox self.restore_region = self._renderer.restore_region + self.mathtext_parser = MathTextParser('Agg') - self.bbox = lbwh_to_bbox(0,0, self.width, self.height) if __debug__: verbose.report('RendererAgg.__init__ done', 'debug-annoying') @@ -173,7 +173,7 @@ """ if __debug__: verbose.report('RendererAgg.draw_mathtext', 'debug-annoying') - width, height, fonts, used_characters = math_parse_s_ft2font( + width, height, fonts, used_characters = self.mathtext_parser.parse( s, self.dpi.get(), prop) if angle == 90: @@ -230,7 +230,7 @@ return n,m if ismath: - width, height, fonts, used_characters = math_parse_s_ft2font( + width, height, fonts, used_characters = self.mathtext_parser.parse( s, self.dpi.get(), prop) return width, height font = self._get_agg_font(prop) Modified: trunk/matplotlib/lib/matplotlib/backends/backend_agg2.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_agg2.py 2007-08-21 15:13:49 UTC (rev 3721) +++ trunk/matplotlib/lib/matplotlib/backends/backend_agg2.py 2007-08-21 15:19:04 UTC (rev 3722) @@ -16,7 +16,7 @@ from matplotlib.cbook import enumerate, is_string_like, exception_to_str from matplotlib.figure import Figure from matplotlib.ft2font import FT2Font -from matplotlib.mathtext import math_parse_s_ft2font +from matplotlib.mathtext import MathTextParser from _backend_agg import RendererAgg as _RendererAgg Modified: trunk/matplotlib/lib/matplotlib/backends/backend_cairo.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_cairo.py 2007-08-21 15:13:49 UTC (rev 3721) +++ trunk/matplotlib/lib/matplotlib/backends/backend_cairo.py 2007-08-21 15:19:04 UTC (rev 3722) @@ -38,7 +38,7 @@ FigureManagerBase, FigureCanvasBase from matplotlib.cbook import enumerate, izip from matplotlib.figure import Figure -from matplotlib.mathtext import math_parse_s_cairo +from matplotlib.mathtext import MathTextParser from matplotlib.transforms import Bbox from matplotlib.font_manager import ttfFontProperty from matplotlib import rcParams @@ -92,8 +92,8 @@ self.dpi = dpi self.text_ctx = cairo.Context ( cairo.ImageSurface (cairo.FORMAT_ARGB32,1,1)) + self.mathtext_parser = MathTextParser('Cairo') - def set_ctx_from_surface (self, surface): self.ctx = cairo.Context (surface) self.ctx.save() # restore, save - when call new_gc() @@ -304,7 +304,7 @@ if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name()) ctx = gc.ctx - width, height, glyphs, rects = math_parse_s_cairo( + width, height, glyphs, rects = self.mathtext_parser.parse( s, self.dpi.get(), prop) ctx.save() @@ -352,7 +352,7 @@ def get_text_width_height(self, s, prop, ismath): if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name()) if ismath: - width, height, fonts, used_characters = math_parse_s_cairo( + width, height, fonts, used_characters = self.mathtext_parser.parse( s, self.dpi.get(), prop) return width, height Modified: trunk/matplotlib/lib/matplotlib/backends/backend_gdk.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_gdk.py 2007-08-21 15:13:49 UTC (rev 3721) +++ trunk/matplotlib/lib/matplotlib/backends/backend_gdk.py 2007-08-21 15:19:04 UTC (rev 3722) @@ -24,7 +24,7 @@ FigureManagerBase, FigureCanvasBase from matplotlib.cbook import is_string_like, enumerate from matplotlib.figure import Figure -from matplotlib.mathtext import math_parse_s_ft2font +from matplotlib.mathtext import MathTextParser from matplotlib.backends._backend_gdk import pixbuf_get_pixels_array @@ -71,6 +71,7 @@ self.gtkDA = gtkDA self.dpi = dpi self._cmap = gtkDA.get_colormap() + self.mathtext_parser = MathTextParser("Agg") def set_pixmap (self, pixmap): self.gdkDrawable = pixmap @@ -198,7 +199,7 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle): - width, height, fonts, used_characters = math_parse_s_ft2font( + width, height, fonts, used_characters = self.mathtext_parser.parse( s, self.dpi.get(), prop) if angle==90: @@ -341,7 +342,7 @@ def get_text_width_height(self, s, prop, ismath): if ismath: - width, height, fonts, used_characters = math_parse_s_ft2font( + width, height, fonts, used_characters = self.mathtext_parser.parse( s, self.dpi.get(), prop) return width, height Modified: trunk/matplotlib/lib/matplotlib/backends/backend_pdf.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_pdf.py 2007-08-21 15:13:49 UTC (rev 3721) +++ trunk/matplotlib/lib/matplotlib/backends/backend_pdf.py 2007-08-21 15:19:04 UTC (rev 3722) @@ -29,7 +29,7 @@ from matplotlib.dviread import Dvi from matplotlib.ft2font import FT2Font, FIXED_WIDTH, ITALIC, LOAD_NO_SCALE, \ LOAD_NO_HINTING, KERNING_UNFITTED -from matplotlib.mathtext import math_parse_s_pdf +from matplotlib.mathtext import MathTextParser from matplotlib.transforms import Bbox from matplotlib import ttconv @@ -1032,6 +1032,7 @@ self.encode_string = self.encode_string_type3 else: self.encode_string = self.encode_string_type42 + self.mathtext_parser = MathTextParser("Pdf") def finalize(self): self.gc.finalize() @@ -1219,7 +1220,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle): # TODO: fix positioning and encoding width, height, glyphs, rects, used_characters = \ - math_parse_s_pdf(s, 72, prop) + self.mathtext_parser.parse(s, 72, prop) self.merge_used_characters(used_characters) # When using Type 3 fonts, we can't use character codes higher @@ -1466,7 +1467,7 @@ if ismath: w, h, glyphs, rects, used_characters = \ - math_parse_s_pdf(s, 72, prop) + self.mathtext_parser.parse(s, 72, prop) elif rcParams['pdf.use14corefonts']: font = self._get_font_afm(prop) Modified: trunk/matplotlib/lib/matplotlib/backends/backend_ps.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_ps.py 2007-08-21 15:13:49 UTC (rev 3721) +++ trunk/matplotlib/lib/matplotlib/backends/backend_ps.py 2007-08-21 15:19:04 UTC (rev 3722) @@ -21,7 +21,7 @@ from matplotlib.font_manager import findfont from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, LOAD_NO_HINTING from matplotlib.ttconv import convert_ttf_to_ps -from matplotlib.mathtext import math_parse_s_ps +from matplotlib.mathtext import MathTextParser from matplotlib.text import Text from matplotlib.transforms import get_vec6_scales @@ -144,6 +144,7 @@ self.fontd = {} self.afmfontd = {} self.used_characters = {} + self.mathtext_parser = MathTextParser("PS") def track_characters(self, font, s): """Keeps track of which characters are required from @@ -277,8 +278,8 @@ return w, h if ismath: - width, height, pswriter, used_characters = math_parse_s_ps( - s, 72, prop) + width, height, pswriter, used_characters = \ + self.mathtext_parser.parse(s, 72, prop) return width, height if rcParams['ps.useafm']: @@ -814,7 +815,7 @@ self._pswriter.write("% mathtext\n") width, height, pswriter, used_characters = \ - math_parse_s_ps(s, 72, prop) + self.mathtext_parser.parse(s, 72, prop) self.merge_used_characters(used_characters) self.set_color(*gc.get_rgb()) thetext = pswriter.getvalue() Modified: trunk/matplotlib/lib/matplotlib/backends/backend_qt.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_qt.py 2007-08-21 15:13:49 UTC (rev 3721) +++ trunk/matplotlib/lib/matplotlib/backends/backend_qt.py 2007-08-21 15:19:04 UTC (rev 3722) @@ -10,7 +10,7 @@ FigureManagerBase, FigureCanvasBase, NavigationToolbar2, cursors from matplotlib._pylab_helpers import Gcf from matplotlib.figure import Figure -from matplotlib.mathtext import math_parse_s_ft2font +from matplotlib.mathtext import MathTextParser from matplotlib.widgets import SubplotTool import qt Modified: trunk/matplotlib/lib/matplotlib/backends/backend_qt4.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_qt4.py 2007-08-21 15:13:49 UTC (rev 3721) +++ trunk/matplotlib/lib/matplotlib/backends/backend_qt4.py 2007-08-21 15:19:04 UTC (rev 3722) @@ -10,7 +10,7 @@ FigureManagerBase, FigureCanvasBase, NavigationToolbar2, cursors from matplotlib._pylab_helpers import Gcf from matplotlib.figure import Figure -from matplotlib.mathtext import math_parse_s_ft2font +from matplotlib.mathtext import MathTextParser from matplotlib.widgets import SubplotTool from PyQt4 import QtCore, QtGui Modified: trunk/matplotlib/lib/matplotlib/backends/backend_svg.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_svg.py 2007-08-21 15:13:49 UTC (rev 3721) +++ trunk/matplotlib/lib/matplotlib/backends/backend_svg.py 2007-08-21 15:19:04 UTC (rev 3722) @@ -9,7 +9,7 @@ from matplotlib.figure import Figure from matplotlib.font_manager import findfont, FontProperties from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, LOAD_NO_HINTING -from matplotlib.mathtext import math_parse_s_ft2font_svg +from matplotlib.mathtext import MathTextParser from xml.sax.saxutils import escape as escape_xml_text @@ -40,6 +40,7 @@ self._imaged = {} self._clipd = {} self._char_defs = {} + self.mathtext_parser = MathTextParser('SVG') svgwriter.write(svgProlog%(width,height,width,height)) def _draw_svg_element(self, element, details, gc, rgbFace): @@ -338,7 +339,7 @@ Draw math text using matplotlib.mathtext """ width, height, svg_elements, used_characters = \ - math_parse_s_ft2font_svg(s, 72, prop) + self.mathtext_parser.parse(s, 72, prop) svg_glyphs = svg_elements.svg_glyphs svg_rects = svg_elements.svg_rects color = rgb2hex(gc.get_rgb()) @@ -428,7 +429,7 @@ def get_text_width_height(self, s, prop, ismath): if ismath: width, height, trash, used_characters = \ - math_parse_s_ft2font_svg(s, 72, prop) + self.mathtext_parser.parse(s, 72, prop) return width, height font = self._get_font(prop) font.set_text(s, 0.0, flags=LOAD_NO_HINTING) Modified: trunk/matplotlib/lib/matplotlib/mathtext.py =================================================================== --- trunk/matplotlib/lib/matplotlib/mathtext.py 2007-08-21 15:13:49 UTC (rev 3721) +++ trunk/matplotlib/lib/matplotlib/mathtext.py 2007-08-21 15:19:04 UTC (rev 3722) @@ -532,7 +532,7 @@ slanted = slanted ) - self.glyphd[key] = Bunch( + result = self.glyphd[key] = Bunch( font = font, fontsize = fontsize, postscript_name = font.postscript_name, @@ -542,7 +542,7 @@ glyph = glyph, offset = offset ) - return self.glyphd[key] + return result def get_xheight(self, font, fontsize, dpi): cached_font = self._get_font(font) @@ -2343,7 +2343,7 @@ ############################################################################## # MAIN -class math_parse_s_ft2font_common: +class MathTextParser: """ Parse the math expression s, return the (bbox, fonts) tuple needed to render it. @@ -2352,40 +2352,38 @@ return is width, height, fonts """ - major, minor1, minor2, tmp, tmp = sys.version_info - if major==2 and minor1==2: - raise SystemExit('mathtext broken on python2.2. We hope to get this fixed soon') - _parser = None - + _backend_mapping = { 'Agg' : MathtextBackendAgg, 'PS' : MathtextBackendPs, - 'PDF' : MathtextBackendPdf, + 'Pdf' : MathtextBackendPdf, 'SVG' : MathtextBackendSvg, 'Cairo' : MathtextBackendCairo } def __init__(self, output): - self.output = output - self.cache = {} + self._output = output + self._cache = {} - def __call__(self, s, dpi, prop): + def parse(self, s, dpi, prop): cacheKey = (s, dpi, hash(prop)) - if self.cache.has_key(cacheKey): - result = self.cache[cacheKey] + result = self._cache.get(cacheKey) + if result is not None: return result - if self.output == 'PS' and rcParams['ps.useafm']: + if self._output == 'PS' and rcParams['ps.useafm']: font_output = StandardPsFonts(prop) else: - backend = self._backend_mapping[self.output]() + backend = self._backend_mapping[self._output]() if rcParams['mathtext.use_cm']: font_output = BakomaFonts(prop, backend) else: font_output = UnicodeFonts(prop, backend) fontsize = prop.get_size_in_points() + # This is a class variable so we don't rebuild the parser + # with each request. if self._parser is None: self.__class__._parser = Parser() box = self._parser.parse(s, font_output, fontsize, dpi) @@ -2395,7 +2393,7 @@ font_output.set_canvas_size(w, h) ship(2, 2, box) result = font_output.get_results() - self.cache[cacheKey] = result + self._cache[cacheKey] = result # Free up the transient data structures self._parser.clear() # Remove a cyclical reference @@ -2403,8 +2401,8 @@ return result -math_parse_s_ft2font = math_parse_s_ft2font_common('Agg') -math_parse_s_ft2font_svg = math_parse_s_ft2font_common('SVG') -math_parse_s_ps = math_parse_s_ft2font_common('PS') -math_parse_s_pdf = math_parse_s_ft2font_common('PDF') -math_parse_s_cairo = math_parse_s_ft2font_common('Cairo') +# math_parse_s_ft2font = math_parse_s_ft2font_common('Agg') +# math_parse_s_ft2font_svg = math_parse_s_ft2font_common('SVG') +# math_parse_s_ps = math_parse_s_ft2font_common('PS') +# math_parse_s_pdf = math_parse_s_ft2font_common('PDF') +# math_parse_s_cairo = math_parse_s_ft2font_common('Cairo') This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |