changeset 33:605d8cb2728c

Got rid of the font registry and opted to use cairo's standard select_font_face() calling semantics instead of the one we hacked on win32; we'll figure out how to get things to work on win32 later.
author Atul Varma <varmaa@toolness.com>
date Sun, 24 Feb 2008 12:34:11 -0600
parents d2dbc90035ab
children b49cdbe98427
files enso/graphics/font.py enso/graphics/xmltextlayout.py
diffstat 2 files changed, 27 insertions(+), 96 deletions(-) [+]
line wrap: on
line diff
--- a/enso/graphics/font.py	Sun Feb 24 12:03:08 2008 -0600
+++ b/enso/graphics/font.py	Sun Feb 24 12:34:11 2008 -0600
@@ -56,25 +56,33 @@
     Encapsulates a font face, which describes both a given typeface
     and style.
     """
-    
-    def __init__( self, fileName, size, cairoContext ):
+
+    _cairoContext = None
+
+    # TODO: Memoize this or make a flyweight pool.
+    def __init__( self, name, size, isItalic ):
         """
-        Creates a Font from the given filename pointing to a TrueType
-        font file, at the given size (in points).
+        Creates a Font with the given properties.
         """
-        
-        import os
+
+        self.name = name
+        self.size = size
+        self.isItalic = isItalic
 
-        if not os.path.exists( fileName ):
-            raise IOError( "file not found: %s" % fileName )
+        if self.isItalic:
+            self.slant = cairo.FONT_SLANT_ITALIC
+        else:
+            self.slant = cairo.FONT_SLANT_NORMAL
 
-        self.fileName = fileName
-        self.size = size
-        self.cairoContext = cairoContext
+        if not Font._cairoContext:
+            dummySurface = cairo.ImageSurface( cairo.FORMAT_ARGB32, 1, 1 )
+            Font._cairoContext = cairo.Context( dummySurface )
 
-        cairoContext.save()
+        self.cairoContext = Font._cairoContext
 
-        self.loadInto( cairoContext )
+        self.cairoContext.save()
+
+        self.loadInto( self.cairoContext )
 
         # Make our font metrics information visible to the client.
         
@@ -82,9 +90,9 @@
           self.descent,
           self.height,
           self.maxXAdvance,
-          self.maxYAdvance ) = cairoContext.font_extents()
+          self.maxYAdvance ) = self.cairoContext.font_extents()
         
-        cairoContext.restore()
+        self.cairoContext.restore()
 
     def getGlyph( self, char ):
         """
@@ -111,22 +119,12 @@
         Sets the cairo context's current font to this font.
         """
 
-        # Note that we are using our own modified 'interpretation' of
-        # the select_font_face() function here, as outlined in our
-        # modified version of the Cairo FreeType 2 Font Module; see
-        # the file 'cairo-ft-font.c' in our modified version of the
-        # Cairo library for more information.
-
-        # TODO: Note also that our modified interpretation of the
-        # function takes an 8-bit string, so we need to do any
-        # necessary encoding/transformation from unicode file-paths
-        # here.
-
         cairoContext.select_font_face(
-            self.fileName,
-            cairo.FONT_SLANT_NORMAL,
+            self.name,
+            self.slant,
             cairo.FONT_WEIGHT_NORMAL
             )
+
         cairoContext.set_font_size( self.size )
 
 
@@ -176,70 +174,3 @@
         self.advance = xAdvance
         
         cairoContext.restore()
-
-
-# ----------------------------------------------------------------------------
-# The Font Registry
-# ----------------------------------------------------------------------------
-
-class FontRegistry:
-    """
-    This singleton represents a registry of font faces, allowing for a
-    client to simply retrieve a Font object in a particular size and
-    style rather without having to know the location of a specific
-    TrueType file.
-    """
-    
-    def __init__( self ):
-        """
-        Initializes the font registry.
-        """
-        
-        self._registry = {}
-
-        dummySurface = cairo.ImageSurface( cairo.FORMAT_ARGB32, 1, 1 )
-        self.cairoContext = cairo.Context( dummySurface )
-
-
-    def register( self, fileName, name, italic=False ):
-        """
-        Registers the given TrueType font filename as representing the
-        given font name with the given style.
-        """
-        
-        registryKey = (name, italic)
-
-        if self._registry.has_key( registryKey ):
-            raise FontAlreadyRegisteredError( registryKey )
-        else:
-            self._registry[registryKey] = fileName
-
-    def get( self, name, size, italic=False ):
-        """
-        Retrieves a Font object corresponding to the given font name
-        at the given size and style.
-        """
-
-        # TODO: Memoize this function.
-        
-        registryKey = (name, italic)
-
-        fileName = self._registry[registryKey]
-        return Font( fileName, size, self.cairoContext )
-
-
-class FontAlreadyRegisteredError( Exception ):
-    """
-    Exception raised when the client attempts to register a font when
-    that font has already been registered.
-    """
-
-    pass
-
-
-# ----------------------------------------------------------------------------
-# Singleton Instance
-# ----------------------------------------------------------------------------
-
-# The font registry singleton instance.
-theFontRegistry = FontRegistry()
--- a/enso/graphics/xmltextlayout.py	Sun Feb 24 12:03:08 2008 -0600
+++ b/enso/graphics/xmltextlayout.py	Sun Feb 24 12:34:11 2008 -0600
@@ -433,7 +433,7 @@
 
         glyphs = []
         
-        fontObj = font.theFontRegistry.get(
+        fontObj = font.Font(
             self._property( "font_family" ),
             self._propertyToPoints( "font_size" ),
             self._property( "font_style" ) == "italic"