Revision: 3683
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3683&view=rev
Author:   mdboom
Date:     2007-08-08 09:44:38 -0700 (Wed, 08 Aug 2007)

Log Message:
-----------
Improve font manager so default font names like "serif", "sans-serif"
etc. are looked for across all fonts in that mapping, not just a
single hard-coded one.
Add DejaVu fonts as an alternative to Vera fonts if present.
Fix how mathtext non-Computer Modern fonts are specified.

Modified Paths:
--------------
    trunk/matplotlib/lib/matplotlib/font_manager.py
    trunk/matplotlib/lib/matplotlib/rcsetup.py

Modified: trunk/matplotlib/lib/matplotlib/font_manager.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/font_manager.py     2007-08-07 17:47:30 UTC 
(rev 3682)
+++ trunk/matplotlib/lib/matplotlib/font_manager.py     2007-08-08 16:44:38 UTC 
(rev 3683)
@@ -477,24 +477,6 @@
             except: continue
 
         add_filename(fontdict, prop, fpath)
-
-        #  !!!!  Default font algorithm needs improvement
-        if   prop.name.lower() in ['bitstream vera serif', 'times']:
-            prop.name = 'serif'
-            add_filename(fontdict, prop, fpath)
-        elif prop.name.lower() in ['bitstream vera sans', 'helvetica']:
-            prop.name = 'sans-serif'
-            add_filename(fontdict, prop, fpath)
-        elif prop.name.lower() in ['zapf chancery', 'itc zapf chancery']:
-            prop.name = 'cursive'
-            add_filename(fontdict, prop, fpath)
-        elif prop.name.lower() in ['western', 'itc avant garde gothic']:
-            prop.name = 'fantasy'
-            add_filename(fontdict, prop, fpath)
-        elif prop.name.lower() in ['bitstream vera sans mono', 'courier']:
-            prop.name = 'monospace'
-            add_filename(fontdict, prop, fpath)
-
     return fontdict
 
 def setWeights(font):
@@ -868,6 +850,13 @@
                     break
             verbose.report('loaded ttfcache file %s'%ttfcache)
 
+        def flatten(d, path):
+            if isinstance(d, dict):
+                for key, val in d.items():
+                    flatten(val, path + [key])
+            elif isinstance(d, str):
+                print path, os.path.basename(d)
+        flatten(self.ttfdict, [])
         #self.ttfdict = createFontDict(self.ttffiles)
 
         #  Load AFM fonts for PostScript
@@ -928,74 +917,99 @@
         else:
             fontdict = self.ttfdict
 
-        name    = prop.get_family()[0]
-        style   = prop.get_style()
-        variant = prop.get_variant()
-        weight  = weight_as_number(prop.get_weight())
-        stretch = prop.get_stretch()
-        size    = str(prop.get_size_in_points())
+        original_name = prop.get_family()[0]
+        style         = prop.get_style()
+        variant       = prop.get_variant()
+        weight        = weight_as_number(prop.get_weight())
+        stretch       = prop.get_stretch()
+        size          = str(prop.get_size_in_points())
 
-        try:
-            fname = fontdict[name][style][variant][weight][stretch][size]
-            verbose.report('\tfindfont cached %(name)s, %(style)s, 
%(variant)s, %(weight)s, %(stretch)s, %(size)s'%locals(), 'debug')
-            verbose.report('findfont returning %s'%fname, 'debug')
-            return fname
-        except KeyError:
-            pass
+        def lookup_name(name):
+            try:
+                fname = fontdict[name][style][variant][weight][stretch][size]
+                verbose.report('\tfindfont cached %(name)s, %(style)s, 
%(variant)s, %(weight)s, %(stretch)s, %(size)s'%locals(), 'debug')
+                verbose.report('findfont returning %s'%fname, 'debug')
+                return fname
+            except KeyError:
+                pass
 
-        for name in prop.get_family():
+            fname = None
             font = fontdict
+            print font.keys()
             if font.has_key(name):
                 font = font[name]
             else:
                 verbose.report('\tfindfont failed %(name)s'%locals(), 'debug')
-                continue
+                return None
 
+            print font.keys()
             if font.has_key(style):
                 font = font[style]
-            elif style == 'italics' and font.has_key('oblique'):
+            elif style == 'italic' and font.has_key('oblique'):
                 font = font['oblique']
+            elif style == 'oblique' and font.has_key('italic'):
+                font = font['italic']
             else:
                 verbose.report('\tfindfont failed %(name)s, 
%(style)s'%locals(), 'debug')
-                continue
+                return None
 
             if font.has_key(variant):
                 font = font[variant]
             else:
                 verbose.report('\tfindfont failed %(name)s, %(style)s, 
%(variant)s'%locals(), 'debug')
-                continue
+                return None
 
             if not font.has_key(weight):
                 setWeights(font)
             font = font[weight]
 
-            # !!!!  need improvement
             if font.has_key(stretch):
-                font = font[stretch]
-            else:
-                verbose.report('\tfindfont failed %(name)s, %(style)s, 
%(variant)s %(weight)s, %(stretch)s'%locals(), 'debug')
-                continue
+                stretch_font = font[stretch]
+                if stretch_font.has_key('scalable'):
+                    fname = stretch_font['scalable']
+                elif stretch_font.has_key(size):
+                    fname = stretch_font[size]
 
-            if font.has_key('scalable'):
-                fname = font['scalable']
-            elif font.has_key(size):
-                fname = font[size]
-            else:
-                verbose.report('\tfindfont failed %(name)s, %(style)s, 
%(variant)s %(weight)s, %(stretch)s, %(size)s'%locals(), 'debug')
-                continue
+            if fname is None:
+                for val in font.values():
+                    if val.has_key('scalable'):
+                        fname = val['scalable']
+                        break
 
-            fontkey = FontKey(name, style, variant, weight, stretch, size)
-            add_filename(fontdict, fontkey, fname)
-            verbose.report('\tfindfont found %(name)s, %(style)s, %(variant)s 
%(weight)s, %(stretch)s, %(size)s'%locals(), 'debug')
-            verbose.report('findfont returning %s'%fname, 'debug')
+            if fname is None:
+                for val in font.values():
+                    if val.has_key(size):
+                        fname = val[size]
+                        break
 
+            if fname is None:
+                verbose.report('\tfindfont failed %(name)s, %(style)s, 
%(variant)s %(weight)s, %(stretch)s'%locals(), 'debug')
+            else:
+                fontkey = FontKey(original_name, style, variant, weight, 
stretch, size)
+                add_filename(fontdict, fontkey, fname)
+                verbose.report('\tfindfont found %(name)s, %(style)s, 
%(variant)s %(weight)s, %(stretch)s, %(size)s'%locals(), 'debug')
+                verbose.report('findfont returning %s'%fname, 'debug')
             return fname
 
-        fontkey = FontKey(name, style, variant, weight, stretch, size)
-        add_filename(fontdict, fontkey, self.defaultFont)
-        verbose.report('Could not match %s, %s, %s.  Returning %s' % (name, 
style, variant, self.defaultFont))
+        font_family_aliases = ['serif', 'sans-serif', 'cursive', 'fantasy', 
'monospace']
+        
+        for name in prop.get_family():
+            if name in font_family_aliases:
+                for name2 in rcParams['font.' + name]:
+                    fname = lookup_name(name2)
+                    if fname:
+                        break
+            else:
+                fname = lookup_name(name)
+            if fname:
+                break
 
-        return self.defaultFont
+        if not fname:
+            fontkey = FontKey(original_name, style, variant, weight, stretch, 
size)
+            add_filename(fontdict, fontkey, self.defaultFont)
+            verbose.report('Could not match %s, %s, %s.  Returning %s' % 
(name, style, variant, self.defaultFont))
+            return self.defaultFont
+        return fname
 
     def _get_afm_font_dict(self):
         cache_message = "Saving AFM font cache for PS and PDF backends to 
%s.\n" \

Modified: trunk/matplotlib/lib/matplotlib/rcsetup.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/rcsetup.py  2007-08-07 17:47:30 UTC (rev 
3682)
+++ trunk/matplotlib/lib/matplotlib/rcsetup.py  2007-08-08 16:44:38 UTC (rev 
3683)
@@ -334,21 +334,24 @@
     'font.stretch'      : ['normal', str],           #
     'font.weight'       : ['normal', str],           #
     'font.size'         : [12.0, validate_float], #
-    'font.serif'        : [['Bitstream Vera Serif','New Century Schoolbook',
-                           'Century Schoolbook L','Utopia','ITC Bookman',
-                           'Bookman','Nimbus Roman No9 L','Times New Roman',
-                           'Times','Palatino','Charter','serif'],
+    'font.serif'        : [['Bitstream Vera Serif', 'DejaVu Serif',
+                            'New Century Schoolbook', 'Century Schoolbook L',
+                            'Utopia', 'ITC Bookman', 'Bookman',
+                            'Nimbus Roman No9 L','Times New Roman',
+                            'Times','Palatino','Charter','serif'],
                            validate_stringlist],
-    'font.sans-serif'   : [['Bitstream Vera Sans','Lucida Grande','Verdana',
-                           'Geneva','Lucid','Arial','Helvetica','Avant Garde',
-                           'sans-serif'], validate_stringlist],
+    'font.sans-serif'   : [['Bitstream Vera Sans', 'DejaVu Sans',
+                            'Lucida Grande', 'Verdana', 'Geneva', 'Lucid',
+                            'Arial', 'Helvetica', 'Avant Garde', 'sans-serif'],
+                           validate_stringlist],
     'font.cursive'      : [['Apple Chancery','Textile','Zapf Chancery',
                            'Sand','cursive'], validate_stringlist],
     'font.fantasy'      : [['Comic Sans MS','Chicago','Charcoal','Impact'
                            'Western','fantasy'], validate_stringlist],
-    'font.monospace'    : [['Bitstream Vera Sans Mono','Andale Mono'
-                           'Nimbus Mono L','Courier New','Courier','Fixed'
-                           'Terminal','monospace'], validate_stringlist],
+    'font.monospace'    : [['Bitstream Vera Sans Mono', 'DejaVu Sans Mono',
+                            'Andale Mono', 'Nimbus Mono L', 'Courier New',
+                            'Courier','Fixed', 'Terminal','monospace'],
+                           validate_stringlist],
 
     # text props
     'text.color'          : ['k', validate_color],     # black
@@ -363,12 +366,12 @@
     'text.fontsize'       : ['medium', validate_fontsize],
     'text.markup'         : ['plain', validate_markup],
 
-    'mathtext.cal'        : [(['cursive'], 'normal', 'normal'), 
validate_mathtext_font],
-    'mathtext.rm'         : [(['serif'], 'normal', 'normal'), 
validate_mathtext_font],
-    'mathtext.tt'         : [(['monospace'], 'normal', 'normal'), 
validate_mathtext_font],
-    'mathtext.it'         : [(['serif'], 'normal', 'oblique'), 
validate_mathtext_font],
-    'mathtext.bf'         : [(['serif'], 'bold', 'normal'), 
validate_mathtext_font],
-    'mathtext.sf'         : [(['sans-serif'], 'normal', 'normal'), 
validate_mathtext_font],
+    'mathtext.cal'        : [('cursive', 'normal', 'normal'), 
validate_mathtext_font],
+    'mathtext.rm'         : [('serif', 'normal', 'normal'), 
validate_mathtext_font],
+    'mathtext.tt'         : [('monospace', 'normal', 'normal'), 
validate_mathtext_font],
+    'mathtext.it'         : [('serif', 'normal', 'italic'), 
validate_mathtext_font],
+    'mathtext.bf'         : [('serif', 'bold', 'normal'), 
validate_mathtext_font],
+    'mathtext.sf'         : [('sans-serif', 'normal', 'normal'), 
validate_mathtext_font],
     'mathtext.use_cm'     : [True, validate_bool],
     'mathtext.fallback_to_cm' : [True, validate_bool],
     


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
Matplotlib-checkins@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to