changeset 46:e37e56835647

Memoized functions marked with the appropriate TODOs.
author Atul Varma <varmaa@toolness.com>
date Mon, 25 Feb 2008 09:26:29 -0600
parents d7c68ba81215
children f746d6311a58
files enso/graphics/font.py enso/graphics/measurement.py enso/graphics/xmltextlayout.py
diffstat 3 files changed, 26 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/enso/graphics/font.py	Mon Feb 25 09:15:44 2008 -0600
+++ b/enso/graphics/font.py	Mon Feb 25 09:26:29 2008 -0600
@@ -46,6 +46,8 @@
 
 import cairo
 
+from enso.utils.memoize import memoized
+
 
 # ----------------------------------------------------------------------------
 # Fonts
@@ -59,7 +61,6 @@
 
     _cairoContext = None
 
-    # TODO: Memoize this or make a flyweight pool.
     def __init__( self, name, size, isItalic ):
         """
         Creates a Font with the given properties.
@@ -94,14 +95,25 @@
         
         self.cairoContext.restore()
 
+    @classmethod
+    @memoized
+    def get( cls, name, size, isItalic ):
+        """
+        Retrieves the Font object with the given properties.
+
+        The fact that this class method is memoized effectively makes
+        this mechanism a flyweight pool of Font objects.
+        """
+
+        return cls( name, size, isItalic )
+
+    @memoized
     def getGlyph( self, char ):
         """
         Returns a glyph of the font corresponding to the given Unicode
         character.
         """
 
-        # TODO: Memoize this function.
-
         return FontGlyph( char, self, self.cairoContext )
 
     def getKerningDistance( self, charLeft, charRight ):
--- a/enso/graphics/measurement.py	Mon Feb 25 09:15:44 2008 -0600
+++ b/enso/graphics/measurement.py	Mon Feb 25 09:26:29 2008 -0600
@@ -40,6 +40,13 @@
 """
 
 # ----------------------------------------------------------------------------
+# Imports
+# ----------------------------------------------------------------------------
+
+from enso.utils.memoize import memoized
+
+
+# ----------------------------------------------------------------------------
 # Pixels-Per-Inch (PPI) Getter/Setter
 # ----------------------------------------------------------------------------
 
@@ -127,6 +134,7 @@
     scaleFactor = getPixelsPerInch() / 72.0
     cairoContext.scale( scaleFactor, scaleFactor )
 
+@memoized
 def strToPoints( unitsStr ):
     """
     Converts from a string such as '2pt', '3in', '5pc', or '20px' into
@@ -149,8 +157,6 @@
       ValueError: Bad measurement string: 125em
     """
 
-    # TODO: memoize this function for performance improvement.
-
     units = float( unitsStr[:-2] )
     if unitsStr.endswith( "pt" ):
         return units
--- a/enso/graphics/xmltextlayout.py	Mon Feb 25 09:15:44 2008 -0600
+++ b/enso/graphics/xmltextlayout.py	Mon Feb 25 09:26:29 2008 -0600
@@ -47,6 +47,7 @@
 import xml.sax
 import xml.sax.handler
 
+from enso.utils.memoize import memoized
 from enso.graphics import measurement
 from enso.graphics import textlayout
 from enso.graphics import font
@@ -65,6 +66,7 @@
 # Utility functions
 # ----------------------------------------------------------------------------
 
+@memoized
 def colorHashToRgba( colorHash ):
     """
     Converts the given HTML-style color hash (e.g., '#aabbcc') or
@@ -80,8 +82,6 @@
     (1.0, 0.0, 0.0, 0.0)
     """
 
-    # TODO: Memoize this function.
-
     colorHash = colorHash[1:]
     if len(colorHash) == 6:
         # It's a RGB hash.
@@ -433,7 +433,7 @@
 
         glyphs = []
         
-        fontObj = font.Font(
+        fontObj = font.Font.get(
             self._property( "font_family" ),
             self._propertyToPoints( "font_size" ),
             self._property( "font_style" ) == "italic"