|
From: <md...@us...> - 2007-08-20 16:11:35
|
Revision: 3719
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3719&view=rev
Author: mdboom
Date: 2007-08-20 09:11:29 -0700 (Mon, 20 Aug 2007)
Log Message:
-----------
Change mathtext.* rcParams so they accept a string which is a set of
arguments to the FontProperties constructor. This has been added to
both the classic and traits based configuration frameworks.
Modified Paths:
--------------
trunk/matplotlib/lib/matplotlib/config/mplconfig.py
trunk/matplotlib/lib/matplotlib/config/mpltraits.py
trunk/matplotlib/lib/matplotlib/config/rcsetup.py
trunk/matplotlib/lib/matplotlib/mathtext.py
trunk/matplotlib/lib/matplotlib/mpl-data/matplotlibrc
trunk/matplotlib/lib/matplotlib/rcsetup.py
trunk/matplotlib/matplotlibrc.template
Modified: trunk/matplotlib/lib/matplotlib/config/mplconfig.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/config/mplconfig.py 2007-08-20 12:47:45 UTC (rev 3718)
+++ trunk/matplotlib/lib/matplotlib/config/mplconfig.py 2007-08-20 16:11:29 UTC (rev 3719)
@@ -161,12 +161,12 @@
dvipnghack = T.false
class mathtext(TConfig):
- cal = T.Trait('cursive', 'cursive', 'normal', 'normal')
- rm = T.Trait('serif', 'serif', 'normal', 'normal')
- tt = T.Trait('monospace', 'monospace', 'normal', 'normal')
- it = T.Trait('serif', 'serif', 'normal', 'italic')
- bf = T.Trait('serif', 'serif', 'bold', 'normal')
- sf = T.Trait('sans-serif', 'sans-serif', 'normal', 'normal')
+ cal = T.Trait("['cursive']", mplT.FontPropertiesHandler())
+ rm = T.Trait("['serif']", mplT.FontPropertiesHandler())
+ tt = T.Trait("['monospace']", mplT.FontPropertiesHandler())
+ it = T.Trait("['serif'], style='oblique'", mplT.FontPropertiesHandler())
+ bf = T.Trait("['serif'], weight='bold'", mplT.FontPropertiesHandler())
+ sf = T.Trait("['sans-serif']", mplT.FontPropertiesHandler())
use_cm = T.true
fallback_to_cm = T.true
Modified: trunk/matplotlib/lib/matplotlib/config/mpltraits.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/config/mpltraits.py 2007-08-20 12:47:45 UTC (rev 3718)
+++ trunk/matplotlib/lib/matplotlib/config/mpltraits.py 2007-08-20 16:11:29 UTC (rev 3719)
@@ -144,3 +144,65 @@
'hsv', 'hsv_r', 'jet', 'jet_r', 'pink', 'pink_r',
'prism', 'prism_r', 'spectral', 'spectral_r', 'spring',
'spring_r', 'summer', 'summer_r', 'winter', 'winter_r']
+
+class FontPropertiesHandler(T.TraitHandler):
+ class FontPropertiesProxy:
+ # In order to build a FontProperties object, various rcParams must
+ # already be known in order to set default values. That means a
+ # FontProperties object can not be created from a config file,
+ # since it depends on other values in the same config file. This
+ # proxy class is used as a temporary storage area for the settings
+ # in the config file, and the full FontProperties class is created
+ # only when the class is first used. It is defined here rather than
+ # in font_manager.py to avoid a cyclical import.
+ def __init__(self,
+ family = None,
+ style = None,
+ variant= None,
+ weight = None,
+ stretch= None,
+ size = None,
+ fname = None, # if this is set, it's a hardcoded filename to use
+ ):
+ self.__family = family
+ self.__style = style
+ self.__variant = variant
+ self.__weight = weight
+ self.__stretch = stretch
+ self.__size = size
+ self.__fname = fname
+
+ self.__child = None
+
+ def __get_child(self):
+ if self.__child is None:
+ from matplotlib.font_manager import FontProperties
+ self.__child = FontProperties(
+ family = self.__family,
+ style = self.__style,
+ variant = self.__variant,
+ weight = self.__weight,
+ stretch = self.__stretch,
+ size = self.__size,
+ fname = self.__fname)
+ return self.__child
+
+ def __getattr__(self, attr):
+ return getattr(self.__get_child(), attr)
+
+ def validate(self, object, name, value):
+ from matplotlib.font_manager import FontProperties
+ if isinstance(value, FontProperties):
+ return value
+ if is_string_like(value):
+ try:
+ proxy = eval("FontProperties(%s)" % value,
+ {}, {'FontProperties': self.FontPropertiesProxy})
+ except:
+ pass
+ else:
+ return proxy
+ self.error(object, name, value)
+
+ def info(self):
+ return 'Represents a set of font properties. Must be a FontProperties object or a string containing the parameters to the FontProperties constructor.'
Modified: trunk/matplotlib/lib/matplotlib/config/rcsetup.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/config/rcsetup.py 2007-08-20 12:47:45 UTC (rev 3718)
+++ trunk/matplotlib/lib/matplotlib/config/rcsetup.py 2007-08-20 16:11:29 UTC (rev 3719)
@@ -198,12 +198,64 @@
except ValueError:
raise ValueError('not a valid font size')
-def validate_mathtext_font(s):
- s = eval(s)
- if type(s) in (list, tuple) and len(s) == 3:
- return s
- raise ValueError('Mathtext font specifier must be a 3-tuple of (family, weight, style)')
+class FontPropertiesProxy:
+ # In order to build a FontProperties object, various rcParams must
+ # already be known in order to set default values. That means a
+ # FontProperties object can not be created from a config file,
+ # since it depends on other values in the same config file. This
+ # proxy class is used as a temporary storage area for the settings
+ # in the config file, and the full FontProperties class is created
+ # only when the class is first used. It is defined here rather than
+ # in font_manager.py to avoid a cyclical import.
+ def __init__(self,
+ family = None,
+ style = None,
+ variant= None,
+ weight = None,
+ stretch= None,
+ size = None,
+ fname = None, # if this is set, it's a hardcoded filename to use
+ ):
+ self.__family = family
+ self.__style = style
+ self.__variant = variant
+ self.__weight = weight
+ self.__stretch = stretch
+ self.__size = size
+ self.__fname = fname
+
+ self.__child = None
+
+ def __get_child(self):
+ if self.__child is None:
+ from font_manager import FontProperties
+ self.__child = FontProperties(
+ family = self.__family,
+ style = self.__style,
+ variant = self.__variant,
+ weight = self.__weight,
+ stretch = self.__stretch,
+ size = self.__size,
+ fname = self.__fname)
+ return self.__child
+
+ def __getattr__(self, attr):
+ return getattr(self.__get_child(), attr)
+def validate_font_properties(s):
+ parsed = False
+ try:
+ prop = eval(u'FontProperties(%s)' % s,
+ {}, {'FontProperties': FontPropertiesProxy})
+ except:
+ pass
+ else:
+ parsed = isinstance(prop, FontPropertiesProxy)
+ if not parsed:
+ raise ValueError(
+ 'Mathtext font specifier must be a set of arguments to the FontProperty constructor.')
+ return prop
+
validate_markup = ValidateInStrings(
'markup',
['plain', 'tex'],
@@ -366,12 +418,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', 'italic'), validate_mathtext_font],
- 'mathtext.bf' : [('serif', 'bold', 'normal'), validate_mathtext_font],
- 'mathtext.sf' : [('sans-serif', 'normal', 'normal'), validate_mathtext_font],
+ 'mathtext.cal' : [FontPropertiesProxy(['cursive']), validate_font_properties],
+ 'mathtext.rm' : [FontPropertiesProxy(['serif']), validate_font_properties],
+ 'mathtext.tt' : [FontPropertiesProxy(['monospace']), validate_font_properties],
+ 'mathtext.it' : [FontPropertiesProxy(['serif'], style='oblique'), validate_font_properties],
+ 'mathtext.bf' : [FontPropertiesProxy(['serif'], weight='bold'), validate_font_properties],
+ 'mathtext.sf' : [FontPropertiesProxy(['sans-serif']), validate_font_properties],
'mathtext.use_cm' : [True, validate_bool],
'mathtext.fallback_to_cm' : [True, validate_bool],
Modified: trunk/matplotlib/lib/matplotlib/mathtext.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/mathtext.py 2007-08-20 12:47:45 UTC (rev 3718)
+++ trunk/matplotlib/lib/matplotlib/mathtext.py 2007-08-20 16:11:29 UTC (rev 3719)
@@ -686,9 +686,7 @@
TruetypeFonts.__init__(self, *args, **kwargs)
if not len(self.fontmap):
for texfont in "cal rm tt it bf sf".split():
- setting = rcParams['mathtext.' + texfont]
- family, weight, style = setting
- prop = FontProperties(family=family, weight=weight, style=style)
+ prop = rcParams['mathtext.' + texfont]
font = findfont(prop)
self.fontmap[texfont] = font
Modified: trunk/matplotlib/lib/matplotlib/mpl-data/matplotlibrc
===================================================================
--- trunk/matplotlib/lib/matplotlib/mpl-data/matplotlibrc 2007-08-20 12:47:45 UTC (rev 3718)
+++ trunk/matplotlib/lib/matplotlib/mpl-data/matplotlibrc 2007-08-20 16:11:29 UTC (rev 3719)
@@ -150,7 +150,7 @@
#text.dvipnghack : False # some versions of dvipng don't handle
# alpha channel properly. Use True to correct and flush
# ~/.matplotlib/tex.cache before testing
-#text.markup : 'plain' # Affects how text, such as titles and lables, are
+#text.markup : 'plain' # Affects how text, such as titles and labels, are
# interpreted by default.
# 'plain': As plain, unformatted text
# 'tex': As TeX-like text. Text between $'s will be
@@ -160,16 +160,16 @@
# processing.
# The following settings allow you to select the fonts in math mode.
-# They map from a TeX font name to a 3-tuple of the form:
-# (family, weight, style)
+# They map from a TeX font name to a set of arguments for the FontProperties constructor.
+# (See FontProperties for more details).
# These settings are only used if mathtext.use_cm is False, otherwise, the
# Bakoma TeX Computer Modern fonts are used.
-#mathtext.cal : (['cursive'], 'normal', 'normal')
-#mathtext.rm : (['serif'], 'normal', 'normal')
-#mathtext.tt : (['monospace'], 'normal', 'normal')
-#mathtext.it : (['serif'], 'normal', 'oblique')
-#mathtext.bf : (['serif'], 'bold', 'normal')
-#mathtext.sf : (['sans-serif'], 'normal', 'normal')
+#mathtext.cal : ['cursive']
+#mathtext.rm : ['serif']
+#mathtext.tt : ['monospace']
+#mathtext.it : ['serif'], style='oblique'
+#mathtext.bf : ['serif'], weight='bold'
+#mathtext.sf : ['sans-serif']
#mathtext.use_cm : True
#mathtext.fallback_to_cm : True # When True, use symbols from the Computer Modern
# fonts when a symbol can not be found in one of
Modified: trunk/matplotlib/lib/matplotlib/rcsetup.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/rcsetup.py 2007-08-20 12:47:45 UTC (rev 3718)
+++ trunk/matplotlib/lib/matplotlib/rcsetup.py 2007-08-20 16:11:29 UTC (rev 3719)
@@ -198,12 +198,64 @@
except ValueError:
raise ValueError('not a valid font size')
-def validate_mathtext_font(s):
- s = eval(s)
- if type(s) in (list, tuple) and len(s) == 3:
- return s
- raise ValueError('Mathtext font specifier must be a 3-tuple of (family, weight, style)')
+class FontPropertiesProxy:
+ # In order to build a FontProperties object, various rcParams must
+ # already be known in order to set default values. That means a
+ # FontProperties object can not be created from a config file,
+ # since it depends on other values in the same config file. This
+ # proxy class is used as a temporary storage area for the settings
+ # in the config file, and the full FontProperties class is created
+ # only when the class is first used. It is defined here rather than
+ # in font_manager.py to avoid a cyclical import.
+ def __init__(self,
+ family = None,
+ style = None,
+ variant= None,
+ weight = None,
+ stretch= None,
+ size = None,
+ fname = None, # if this is set, it's a hardcoded filename to use
+ ):
+ self.__family = family
+ self.__style = style
+ self.__variant = variant
+ self.__weight = weight
+ self.__stretch = stretch
+ self.__size = size
+ self.__fname = fname
+
+ self.__child = None
+
+ def __get_child(self):
+ if self.__child is None:
+ from font_manager import FontProperties
+ self.__child = FontProperties(
+ family = self.__family,
+ style = self.__style,
+ variant = self.__variant,
+ weight = self.__weight,
+ stretch = self.__stretch,
+ size = self.__size,
+ fname = self.__fname)
+ return self.__child
+
+ def __getattr__(self, attr):
+ return getattr(self.__get_child(), attr)
+def validate_font_properties(s):
+ parsed = False
+ try:
+ prop = eval(u'FontProperties(%s)' % s,
+ {}, {'FontProperties': FontPropertiesProxy})
+ except:
+ pass
+ else:
+ parsed = isinstance(prop, FontPropertiesProxy)
+ if not parsed:
+ raise ValueError(
+ 'Mathtext font specifier must be a set of arguments to the FontProperty constructor.')
+ return prop
+
validate_markup = ValidateInStrings(
'markup',
['plain', 'tex'],
@@ -366,12 +418,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', 'italic'), validate_mathtext_font],
- 'mathtext.bf' : [('serif', 'bold', 'normal'), validate_mathtext_font],
- 'mathtext.sf' : [('sans-serif', 'normal', 'normal'), validate_mathtext_font],
+ 'mathtext.cal' : [FontPropertiesProxy(['cursive']), validate_font_properties],
+ 'mathtext.rm' : [FontPropertiesProxy(['serif']), validate_font_properties],
+ 'mathtext.tt' : [FontPropertiesProxy(['monospace']), validate_font_properties],
+ 'mathtext.it' : [FontPropertiesProxy(['serif'], style='oblique'), validate_font_properties],
+ 'mathtext.bf' : [FontPropertiesProxy(['serif'], weight='bold'), validate_font_properties],
+ 'mathtext.sf' : [FontPropertiesProxy(['sans-serif']), validate_font_properties],
'mathtext.use_cm' : [True, validate_bool],
'mathtext.fallback_to_cm' : [True, validate_bool],
Modified: trunk/matplotlib/matplotlibrc.template
===================================================================
--- trunk/matplotlib/matplotlibrc.template 2007-08-20 12:47:45 UTC (rev 3718)
+++ trunk/matplotlib/matplotlibrc.template 2007-08-20 16:11:29 UTC (rev 3719)
@@ -150,7 +150,7 @@
#text.dvipnghack : False # some versions of dvipng don't handle
# alpha channel properly. Use True to correct and flush
# ~/.matplotlib/tex.cache before testing
-#text.markup : 'plain' # Affects how text, such as titles and lables, are
+#text.markup : 'plain' # Affects how text, such as titles and labels, are
# interpreted by default.
# 'plain': As plain, unformatted text
# 'tex': As TeX-like text. Text between $'s will be
@@ -160,16 +160,16 @@
# processing.
# The following settings allow you to select the fonts in math mode.
-# They map from a TeX font name to a 3-tuple of the form:
-# (family, weight, style)
+# They map from a TeX font name to a set of arguments for the FontProperties constructor.
+# (See FontProperties for more details).
# These settings are only used if mathtext.use_cm is False, otherwise, the
# Bakoma TeX Computer Modern fonts are used.
-#mathtext.cal : (['cursive'], 'normal', 'normal')
-#mathtext.rm : (['serif'], 'normal', 'normal')
-#mathtext.tt : (['monospace'], 'normal', 'normal')
-#mathtext.it : (['serif'], 'normal', 'oblique')
-#mathtext.bf : (['serif'], 'bold', 'normal')
-#mathtext.sf : (['sans-serif'], 'normal', 'normal')
+#mathtext.cal : ['cursive']
+#mathtext.rm : ['serif']
+#mathtext.tt : ['monospace']
+#mathtext.it : ['serif'], style='oblique'
+#mathtext.bf : ['serif'], weight='bold'
+#mathtext.sf : ['sans-serif']
#mathtext.use_cm : True
#mathtext.fallback_to_cm : True # When True, use symbols from the Computer Modern
# fonts when a symbol can not be found in one of
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|