Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e740ce

Browse files
committed
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. svn path=/trunk/matplotlib/; revision=3719
1 parent 4f9fe2d commit 2e740ce

7 files changed

Lines changed: 213 additions & 49 deletions

File tree

lib/matplotlib/config/mplconfig.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ class latex(TConfig):
161161
dvipnghack = T.false
162162

163163
class mathtext(TConfig):
164-
cal = T.Trait('cursive', 'cursive', 'normal', 'normal')
165-
rm = T.Trait('serif', 'serif', 'normal', 'normal')
166-
tt = T.Trait('monospace', 'monospace', 'normal', 'normal')
167-
it = T.Trait('serif', 'serif', 'normal', 'italic')
168-
bf = T.Trait('serif', 'serif', 'bold', 'normal')
169-
sf = T.Trait('sans-serif', 'sans-serif', 'normal', 'normal')
164+
cal = T.Trait("['cursive']", mplT.FontPropertiesHandler())
165+
rm = T.Trait("['serif']", mplT.FontPropertiesHandler())
166+
tt = T.Trait("['monospace']", mplT.FontPropertiesHandler())
167+
it = T.Trait("['serif'], style='oblique'", mplT.FontPropertiesHandler())
168+
bf = T.Trait("['serif'], weight='bold'", mplT.FontPropertiesHandler())
169+
sf = T.Trait("['sans-serif']", mplT.FontPropertiesHandler())
170170
use_cm = T.true
171171
fallback_to_cm = T.true
172172

lib/matplotlib/config/mpltraits.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,65 @@ def validate(self, object, name, value):
144144
'hsv', 'hsv_r', 'jet', 'jet_r', 'pink', 'pink_r',
145145
'prism', 'prism_r', 'spectral', 'spectral_r', 'spring',
146146
'spring_r', 'summer', 'summer_r', 'winter', 'winter_r']
147+
148+
class FontPropertiesHandler(T.TraitHandler):
149+
class FontPropertiesProxy:
150+
# In order to build a FontProperties object, various rcParams must
151+
# already be known in order to set default values. That means a
152+
# FontProperties object can not be created from a config file,
153+
# since it depends on other values in the same config file. This
154+
# proxy class is used as a temporary storage area for the settings
155+
# in the config file, and the full FontProperties class is created
156+
# only when the class is first used. It is defined here rather than
157+
# in font_manager.py to avoid a cyclical import.
158+
def __init__(self,
159+
family = None,
160+
style = None,
161+
variant= None,
162+
weight = None,
163+
stretch= None,
164+
size = None,
165+
fname = None, # if this is set, it's a hardcoded filename to use
166+
):
167+
self.__family = family
168+
self.__style = style
169+
self.__variant = variant
170+
self.__weight = weight
171+
self.__stretch = stretch
172+
self.__size = size
173+
self.__fname = fname
174+
175+
self.__child = None
176+
177+
def __get_child(self):
178+
if self.__child is None:
179+
from matplotlib.font_manager import FontProperties
180+
self.__child = FontProperties(
181+
family = self.__family,
182+
style = self.__style,
183+
variant = self.__variant,
184+
weight = self.__weight,
185+
stretch = self.__stretch,
186+
size = self.__size,
187+
fname = self.__fname)
188+
return self.__child
189+
190+
def __getattr__(self, attr):
191+
return getattr(self.__get_child(), attr)
192+
193+
def validate(self, object, name, value):
194+
from matplotlib.font_manager import FontProperties
195+
if isinstance(value, FontProperties):
196+
return value
197+
if is_string_like(value):
198+
try:
199+
proxy = eval("FontProperties(%s)" % value,
200+
{}, {'FontProperties': self.FontPropertiesProxy})
201+
except:
202+
pass
203+
else:
204+
return proxy
205+
self.error(object, name, value)
206+
207+
def info(self):
208+
return 'Represents a set of font properties. Must be a FontProperties object or a string containing the parameters to the FontProperties constructor.'

lib/matplotlib/config/rcsetup.py

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,63 @@ def validate_fontsize(s):
198198
except ValueError:
199199
raise ValueError('not a valid font size')
200200

201-
def validate_mathtext_font(s):
202-
s = eval(s)
203-
if type(s) in (list, tuple) and len(s) == 3:
204-
return s
205-
raise ValueError('Mathtext font specifier must be a 3-tuple of (family, weight, style)')
201+
class FontPropertiesProxy:
202+
# In order to build a FontProperties object, various rcParams must
203+
# already be known in order to set default values. That means a
204+
# FontProperties object can not be created from a config file,
205+
# since it depends on other values in the same config file. This
206+
# proxy class is used as a temporary storage area for the settings
207+
# in the config file, and the full FontProperties class is created
208+
# only when the class is first used. It is defined here rather than
209+
# in font_manager.py to avoid a cyclical import.
210+
def __init__(self,
211+
family = None,
212+
style = None,
213+
variant= None,
214+
weight = None,
215+
stretch= None,
216+
size = None,
217+
fname = None, # if this is set, it's a hardcoded filename to use
218+
):
219+
self.__family = family
220+
self.__style = style
221+
self.__variant = variant
222+
self.__weight = weight
223+
self.__stretch = stretch
224+
self.__size = size
225+
self.__fname = fname
226+
227+
self.__child = None
228+
229+
def __get_child(self):
230+
if self.__child is None:
231+
from font_manager import FontProperties
232+
self.__child = FontProperties(
233+
family = self.__family,
234+
style = self.__style,
235+
variant = self.__variant,
236+
weight = self.__weight,
237+
stretch = self.__stretch,
238+
size = self.__size,
239+
fname = self.__fname)
240+
return self.__child
241+
242+
def __getattr__(self, attr):
243+
return getattr(self.__get_child(), attr)
244+
245+
def validate_font_properties(s):
246+
parsed = False
247+
try:
248+
prop = eval(u'FontProperties(%s)' % s,
249+
{}, {'FontProperties': FontPropertiesProxy})
250+
except:
251+
pass
252+
else:
253+
parsed = isinstance(prop, FontPropertiesProxy)
254+
if not parsed:
255+
raise ValueError(
256+
'Mathtext font specifier must be a set of arguments to the FontProperty constructor.')
257+
return prop
206258

207259
validate_markup = ValidateInStrings(
208260
'markup',
@@ -366,12 +418,12 @@ def __call__(self, s):
366418
'text.fontsize' : ['medium', validate_fontsize],
367419
'text.markup' : ['plain', validate_markup],
368420

369-
'mathtext.cal' : [('cursive', 'normal', 'normal'), validate_mathtext_font],
370-
'mathtext.rm' : [('serif', 'normal', 'normal'), validate_mathtext_font],
371-
'mathtext.tt' : [('monospace', 'normal', 'normal'), validate_mathtext_font],
372-
'mathtext.it' : [('serif', 'normal', 'italic'), validate_mathtext_font],
373-
'mathtext.bf' : [('serif', 'bold', 'normal'), validate_mathtext_font],
374-
'mathtext.sf' : [('sans-serif', 'normal', 'normal'), validate_mathtext_font],
421+
'mathtext.cal' : [FontPropertiesProxy(['cursive']), validate_font_properties],
422+
'mathtext.rm' : [FontPropertiesProxy(['serif']), validate_font_properties],
423+
'mathtext.tt' : [FontPropertiesProxy(['monospace']), validate_font_properties],
424+
'mathtext.it' : [FontPropertiesProxy(['serif'], style='oblique'), validate_font_properties],
425+
'mathtext.bf' : [FontPropertiesProxy(['serif'], weight='bold'), validate_font_properties],
426+
'mathtext.sf' : [FontPropertiesProxy(['sans-serif']), validate_font_properties],
375427
'mathtext.use_cm' : [True, validate_bool],
376428
'mathtext.fallback_to_cm' : [True, validate_bool],
377429

lib/matplotlib/mathtext.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,7 @@ def __init__(self, *args, **kwargs):
686686
TruetypeFonts.__init__(self, *args, **kwargs)
687687
if not len(self.fontmap):
688688
for texfont in "cal rm tt it bf sf".split():
689-
setting = rcParams['mathtext.' + texfont]
690-
family, weight, style = setting
691-
prop = FontProperties(family=family, weight=weight, style=style)
689+
prop = rcParams['mathtext.' + texfont]
692690
font = findfont(prop)
693691
self.fontmap[texfont] = font
694692

lib/matplotlib/mpl-data/matplotlibrc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ numerix : numpy # numpy, Numeric or numarray
150150
#text.dvipnghack : False # some versions of dvipng don't handle
151151
# alpha channel properly. Use True to correct and flush
152152
# ~/.matplotlib/tex.cache before testing
153-
#text.markup : 'plain' # Affects how text, such as titles and lables, are
153+
#text.markup : 'plain' # Affects how text, such as titles and labels, are
154154
# interpreted by default.
155155
# 'plain': As plain, unformatted text
156156
# 'tex': As TeX-like text. Text between $'s will be
@@ -160,16 +160,16 @@ numerix : numpy # numpy, Numeric or numarray
160160
# processing.
161161

162162
# The following settings allow you to select the fonts in math mode.
163-
# They map from a TeX font name to a 3-tuple of the form:
164-
# (family, weight, style)
163+
# They map from a TeX font name to a set of arguments for the FontProperties constructor.
164+
# (See FontProperties for more details).
165165
# These settings are only used if mathtext.use_cm is False, otherwise, the
166166
# Bakoma TeX Computer Modern fonts are used.
167-
#mathtext.cal : (['cursive'], 'normal', 'normal')
168-
#mathtext.rm : (['serif'], 'normal', 'normal')
169-
#mathtext.tt : (['monospace'], 'normal', 'normal')
170-
#mathtext.it : (['serif'], 'normal', 'oblique')
171-
#mathtext.bf : (['serif'], 'bold', 'normal')
172-
#mathtext.sf : (['sans-serif'], 'normal', 'normal')
167+
#mathtext.cal : ['cursive']
168+
#mathtext.rm : ['serif']
169+
#mathtext.tt : ['monospace']
170+
#mathtext.it : ['serif'], style='oblique'
171+
#mathtext.bf : ['serif'], weight='bold'
172+
#mathtext.sf : ['sans-serif']
173173
#mathtext.use_cm : True
174174
#mathtext.fallback_to_cm : True # When True, use symbols from the Computer Modern
175175
# fonts when a symbol can not be found in one of

lib/matplotlib/rcsetup.py

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,63 @@ def validate_fontsize(s):
198198
except ValueError:
199199
raise ValueError('not a valid font size')
200200

201-
def validate_mathtext_font(s):
202-
s = eval(s)
203-
if type(s) in (list, tuple) and len(s) == 3:
204-
return s
205-
raise ValueError('Mathtext font specifier must be a 3-tuple of (family, weight, style)')
201+
class FontPropertiesProxy:
202+
# In order to build a FontProperties object, various rcParams must
203+
# already be known in order to set default values. That means a
204+
# FontProperties object can not be created from a config file,
205+
# since it depends on other values in the same config file. This
206+
# proxy class is used as a temporary storage area for the settings
207+
# in the config file, and the full FontProperties class is created
208+
# only when the class is first used. It is defined here rather than
209+
# in font_manager.py to avoid a cyclical import.
210+
def __init__(self,
211+
family = None,
212+
style = None,
213+
variant= None,
214+
weight = None,
215+
stretch= None,
216+
size = None,
217+
fname = None, # if this is set, it's a hardcoded filename to use
218+
):
219+
self.__family = family
220+
self.__style = style
221+
self.__variant = variant
222+
self.__weight = weight
223+
self.__stretch = stretch
224+
self.__size = size
225+
self.__fname = fname
226+
227+
self.__child = None
228+
229+
def __get_child(self):
230+
if self.__child is None:
231+
from font_manager import FontProperties
232+
self.__child = FontProperties(
233+
family = self.__family,
234+
style = self.__style,
235+
variant = self.__variant,
236+
weight = self.__weight,
237+
stretch = self.__stretch,
238+
size = self.__size,
239+
fname = self.__fname)
240+
return self.__child
241+
242+
def __getattr__(self, attr):
243+
return getattr(self.__get_child(), attr)
244+
245+
def validate_font_properties(s):
246+
parsed = False
247+
try:
248+
prop = eval(u'FontProperties(%s)' % s,
249+
{}, {'FontProperties': FontPropertiesProxy})
250+
except:
251+
pass
252+
else:
253+
parsed = isinstance(prop, FontPropertiesProxy)
254+
if not parsed:
255+
raise ValueError(
256+
'Mathtext font specifier must be a set of arguments to the FontProperty constructor.')
257+
return prop
206258

207259
validate_markup = ValidateInStrings(
208260
'markup',
@@ -366,12 +418,12 @@ def __call__(self, s):
366418
'text.fontsize' : ['medium', validate_fontsize],
367419
'text.markup' : ['plain', validate_markup],
368420

369-
'mathtext.cal' : [('cursive', 'normal', 'normal'), validate_mathtext_font],
370-
'mathtext.rm' : [('serif', 'normal', 'normal'), validate_mathtext_font],
371-
'mathtext.tt' : [('monospace', 'normal', 'normal'), validate_mathtext_font],
372-
'mathtext.it' : [('serif', 'normal', 'italic'), validate_mathtext_font],
373-
'mathtext.bf' : [('serif', 'bold', 'normal'), validate_mathtext_font],
374-
'mathtext.sf' : [('sans-serif', 'normal', 'normal'), validate_mathtext_font],
421+
'mathtext.cal' : [FontPropertiesProxy(['cursive']), validate_font_properties],
422+
'mathtext.rm' : [FontPropertiesProxy(['serif']), validate_font_properties],
423+
'mathtext.tt' : [FontPropertiesProxy(['monospace']), validate_font_properties],
424+
'mathtext.it' : [FontPropertiesProxy(['serif'], style='oblique'), validate_font_properties],
425+
'mathtext.bf' : [FontPropertiesProxy(['serif'], weight='bold'), validate_font_properties],
426+
'mathtext.sf' : [FontPropertiesProxy(['sans-serif']), validate_font_properties],
375427
'mathtext.use_cm' : [True, validate_bool],
376428
'mathtext.fallback_to_cm' : [True, validate_bool],
377429

matplotlibrc.template

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ numerix : %(numerix)s # numpy, Numeric or numarray
150150
#text.dvipnghack : False # some versions of dvipng don't handle
151151
# alpha channel properly. Use True to correct and flush
152152
# ~/.matplotlib/tex.cache before testing
153-
#text.markup : 'plain' # Affects how text, such as titles and lables, are
153+
#text.markup : 'plain' # Affects how text, such as titles and labels, are
154154
# interpreted by default.
155155
# 'plain': As plain, unformatted text
156156
# 'tex': As TeX-like text. Text between $'s will be
@@ -160,16 +160,16 @@ numerix : %(numerix)s # numpy, Numeric or numarray
160160
# processing.
161161

162162
# The following settings allow you to select the fonts in math mode.
163-
# They map from a TeX font name to a 3-tuple of the form:
164-
# (family, weight, style)
163+
# They map from a TeX font name to a set of arguments for the FontProperties constructor.
164+
# (See FontProperties for more details).
165165
# These settings are only used if mathtext.use_cm is False, otherwise, the
166166
# Bakoma TeX Computer Modern fonts are used.
167-
#mathtext.cal : (['cursive'], 'normal', 'normal')
168-
#mathtext.rm : (['serif'], 'normal', 'normal')
169-
#mathtext.tt : (['monospace'], 'normal', 'normal')
170-
#mathtext.it : (['serif'], 'normal', 'oblique')
171-
#mathtext.bf : (['serif'], 'bold', 'normal')
172-
#mathtext.sf : (['sans-serif'], 'normal', 'normal')
167+
#mathtext.cal : ['cursive']
168+
#mathtext.rm : ['serif']
169+
#mathtext.tt : ['monospace']
170+
#mathtext.it : ['serif'], style='oblique'
171+
#mathtext.bf : ['serif'], weight='bold'
172+
#mathtext.sf : ['sans-serif']
173173
#mathtext.use_cm : True
174174
#mathtext.fallback_to_cm : True # When True, use symbols from the Computer Modern
175175
# fonts when a symbol can not be found in one of

0 commit comments

Comments
 (0)