Revision: 3750
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3750&view=rev
Author:   mdboom
Date:     2007-08-29 10:54:42 -0700 (Wed, 29 Aug 2007)

Log Message:
-----------
Fix leaks of FT2Font objects.

Modified Paths:
--------------
    trunk/matplotlib/lib/matplotlib/backends/backend_agg.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-28 
20:29:37 UTC (rev 3749)
+++ trunk/matplotlib/lib/matplotlib/backends/backend_agg.py     2007-08-29 
17:54:42 UTC (rev 3750)
@@ -89,9 +89,7 @@
 from _backend_agg import RendererAgg as _RendererAgg
 
 backend_version = 'v2.2'
-_fontd = {}     # a map from fname to font instances
 
-
 class RendererAgg(RendererBase):
     """
     The renderer handles all the drawing primitives using a graphics
@@ -126,7 +124,8 @@
         self.copy_from_bbox = self._renderer.copy_from_bbox
         self.restore_region = self._renderer.restore_region
         self.mathtext_parser = MathTextParser('Agg')
-
+        self._fontd = {}
+        
         self.bbox = lbwh_to_bbox(0,0, self.width, self.height)
         if __debug__: verbose.report('RendererAgg.__init__ done',
                                      'debug-annoying')
@@ -298,12 +297,12 @@
                                      'debug-annoying')
 
         key = hash(prop)
-        font = _fontd.get(key)
+        font = self._fontd.get(key)
 
         if font is None:
             fname = findfont(prop)
             font = FT2Font(str(fname))
-            _fontd[key] = font
+            self._fontd[key] = font
 
         font.clear()
         size = prop.get_size_in_points()

Modified: trunk/matplotlib/lib/matplotlib/backends/backend_svg.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/backends/backend_svg.py     2007-08-28 
20:29:37 UTC (rev 3749)
+++ trunk/matplotlib/lib/matplotlib/backends/backend_svg.py     2007-08-29 
17:54:42 UTC (rev 3750)
@@ -23,7 +23,6 @@
     return manager
 
 
-_fontd = {}
 _capstyle_d = {'projecting' : 'square', 'butt' : 'butt', 'round': 'round',}
 class RendererSVG(RendererBase):
     FONT_SCALE = 1200.0
@@ -41,6 +40,7 @@
         self._clipd = {}
         self._char_defs = {}
         self.mathtext_parser = MathTextParser('SVG')
+        self.fontd = {}
         svgwriter.write(svgProlog%(width,height,width,height))
 
     def _draw_svg_element(self, element, details, gc, rgbFace):
@@ -56,11 +56,11 @@
 
     def _get_font(self, prop):
         key = hash(prop)
-        font = _fontd.get(key)
+        font = self.fontd.get(key)
         if font is None:
             fname = findfont(prop)
             font = FT2Font(str(fname))
-            _fontd[key] = font
+            self.fontd[key] = font
         font.clear()
         size = prop.get_size_in_points()
         font.set_size(size, 72.0)

Modified: trunk/matplotlib/lib/matplotlib/mathtext.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/mathtext.py 2007-08-28 20:29:37 UTC (rev 
3749)
+++ trunk/matplotlib/lib/matplotlib/mathtext.py 2007-08-29 17:54:42 UTC (rev 
3750)
@@ -430,6 +430,11 @@
         self.mathtext_backend.fonts_object = self
         self.used_characters = {}
 
+    def destroy(self):
+        """Fix any cyclical references before the object is about
+        to be destroyed."""
+        self.used_characters = None
+        
     def get_kern(self, font1, sym1, fontsize1,
                  font2, sym2, fontsize2, dpi):
         """
@@ -461,7 +466,7 @@
           xmin, xmax, ymin, ymax  - the ink rectangle of the glyph
           iceberg - the distance from the baseline to the top of the glyph.
              horiBearingY in Truetype parlance, height in TeX parlance
-          """
+        """
         info = self._get_info(font, sym, fontsize, dpi)
         return info.metrics
 
@@ -494,8 +499,10 @@
         return self.mathtext_backend.get_results(box)
 
     def get_sized_alternatives_for_symbol(self, fontname, sym):
-        """Override if your font provides multiple sizes of the same
-        symbol."""
+        """
+        Override if your font provides multiple sizes of the same
+        symbol.
+        """
         return [(fontname, sym)]
 
 class TruetypeFonts(Fonts):
@@ -503,10 +510,6 @@
     A generic base class for all font setups that use Truetype fonts
     (through ft2font)
     """
-    """
-    Use the Bakoma true type fonts for rendering
-    """
-    # allocate a new set of fonts
     basepath = os.path.join( get_data_path(), 'fonts', 'ttf' )
 
     class CachedFont:
@@ -529,6 +532,14 @@
 
         self.fonts['default'] = default_font
 
+    def destroy(self):
+        self.glyphd = None
+        for cached_font in self.fonts.values():
+            cached_font.charmap = None
+            cached_font.glyphmap = None
+            cached_font.font = None
+        Fonts.destroy(self)
+        
     def _get_font(self, font):
         """Looks up a CachedFont with its charmap and inverse charmap.
         font may be a TeX font name (cal, rm, it etc.), or postscript name."""
@@ -2401,7 +2412,7 @@
 ##############################################################################
 # MAIN
 
-class MathTextParser:
+class MathTextParser(object):
     """
     Parse the math expression s, return the (bbox, fonts) tuple needed
     to render it.
@@ -2453,7 +2464,10 @@
         self._cache[cacheKey] = result
         # Free up the transient data structures
         self._parser.clear()
-        # Remove a cyclical reference
+
+        # Fix cyclical references
+        font_output.destroy()
         font_output.mathtext_backend.fonts_object = None
-
+        font_output.mathtext_backend = None
+        
         return result


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to