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

Skip to content

Commit 08b841a

Browse files
committed
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. svn path=/trunk/matplotlib/; revision=3683
1 parent f14a022 commit 08b841a

2 files changed

Lines changed: 91 additions & 74 deletions

File tree

lib/matplotlib/font_manager.py

Lines changed: 72 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -477,24 +477,6 @@ def createFontDict(fontfiles, fontext='ttf'):
477477
except: continue
478478

479479
add_filename(fontdict, prop, fpath)
480-
481-
# !!!! Default font algorithm needs improvement
482-
if prop.name.lower() in ['bitstream vera serif', 'times']:
483-
prop.name = 'serif'
484-
add_filename(fontdict, prop, fpath)
485-
elif prop.name.lower() in ['bitstream vera sans', 'helvetica']:
486-
prop.name = 'sans-serif'
487-
add_filename(fontdict, prop, fpath)
488-
elif prop.name.lower() in ['zapf chancery', 'itc zapf chancery']:
489-
prop.name = 'cursive'
490-
add_filename(fontdict, prop, fpath)
491-
elif prop.name.lower() in ['western', 'itc avant garde gothic']:
492-
prop.name = 'fantasy'
493-
add_filename(fontdict, prop, fpath)
494-
elif prop.name.lower() in ['bitstream vera sans mono', 'courier']:
495-
prop.name = 'monospace'
496-
add_filename(fontdict, prop, fpath)
497-
498480
return fontdict
499481

500482
def setWeights(font):
@@ -868,6 +850,13 @@ def rebuild():
868850
break
869851
verbose.report('loaded ttfcache file %s'%ttfcache)
870852

853+
def flatten(d, path):
854+
if isinstance(d, dict):
855+
for key, val in d.items():
856+
flatten(val, path + [key])
857+
elif isinstance(d, str):
858+
print path, os.path.basename(d)
859+
flatten(self.ttfdict, [])
871860
#self.ttfdict = createFontDict(self.ttffiles)
872861

873862
# Load AFM fonts for PostScript
@@ -928,74 +917,99 @@ def findfont(self, prop, fontext='ttf'):
928917
else:
929918
fontdict = self.ttfdict
930919

931-
name = prop.get_family()[0]
932-
style = prop.get_style()
933-
variant = prop.get_variant()
934-
weight = weight_as_number(prop.get_weight())
935-
stretch = prop.get_stretch()
936-
size = str(prop.get_size_in_points())
937-
938-
try:
939-
fname = fontdict[name][style][variant][weight][stretch][size]
940-
verbose.report('\tfindfont cached %(name)s, %(style)s, %(variant)s, %(weight)s, %(stretch)s, %(size)s'%locals(), 'debug')
941-
verbose.report('findfont returning %s'%fname, 'debug')
942-
return fname
943-
except KeyError:
944-
pass
920+
original_name = prop.get_family()[0]
921+
style = prop.get_style()
922+
variant = prop.get_variant()
923+
weight = weight_as_number(prop.get_weight())
924+
stretch = prop.get_stretch()
925+
size = str(prop.get_size_in_points())
945926

946-
for name in prop.get_family():
927+
def lookup_name(name):
928+
try:
929+
fname = fontdict[name][style][variant][weight][stretch][size]
930+
verbose.report('\tfindfont cached %(name)s, %(style)s, %(variant)s, %(weight)s, %(stretch)s, %(size)s'%locals(), 'debug')
931+
verbose.report('findfont returning %s'%fname, 'debug')
932+
return fname
933+
except KeyError:
934+
pass
935+
936+
fname = None
947937
font = fontdict
938+
print font.keys()
948939
if font.has_key(name):
949940
font = font[name]
950941
else:
951942
verbose.report('\tfindfont failed %(name)s'%locals(), 'debug')
952-
continue
943+
return None
953944

945+
print font.keys()
954946
if font.has_key(style):
955947
font = font[style]
956-
elif style == 'italics' and font.has_key('oblique'):
948+
elif style == 'italic' and font.has_key('oblique'):
957949
font = font['oblique']
950+
elif style == 'oblique' and font.has_key('italic'):
951+
font = font['italic']
958952
else:
959953
verbose.report('\tfindfont failed %(name)s, %(style)s'%locals(), 'debug')
960-
continue
954+
return None
961955

962956
if font.has_key(variant):
963957
font = font[variant]
964958
else:
965959
verbose.report('\tfindfont failed %(name)s, %(style)s, %(variant)s'%locals(), 'debug')
966-
continue
960+
return None
967961

968962
if not font.has_key(weight):
969963
setWeights(font)
970964
font = font[weight]
971965

972-
# !!!! need improvement
973966
if font.has_key(stretch):
974-
font = font[stretch]
975-
else:
967+
stretch_font = font[stretch]
968+
if stretch_font.has_key('scalable'):
969+
fname = stretch_font['scalable']
970+
elif stretch_font.has_key(size):
971+
fname = stretch_font[size]
972+
973+
if fname is None:
974+
for val in font.values():
975+
if val.has_key('scalable'):
976+
fname = val['scalable']
977+
break
978+
979+
if fname is None:
980+
for val in font.values():
981+
if val.has_key(size):
982+
fname = val[size]
983+
break
984+
985+
if fname is None:
976986
verbose.report('\tfindfont failed %(name)s, %(style)s, %(variant)s %(weight)s, %(stretch)s'%locals(), 'debug')
977-
continue
978-
979-
if font.has_key('scalable'):
980-
fname = font['scalable']
981-
elif font.has_key(size):
982-
fname = font[size]
983987
else:
984-
verbose.report('\tfindfont failed %(name)s, %(style)s, %(variant)s %(weight)s, %(stretch)s, %(size)s'%locals(), 'debug')
985-
continue
986-
987-
fontkey = FontKey(name, style, variant, weight, stretch, size)
988-
add_filename(fontdict, fontkey, fname)
989-
verbose.report('\tfindfont found %(name)s, %(style)s, %(variant)s %(weight)s, %(stretch)s, %(size)s'%locals(), 'debug')
990-
verbose.report('findfont returning %s'%fname, 'debug')
991-
988+
fontkey = FontKey(original_name, style, variant, weight, stretch, size)
989+
add_filename(fontdict, fontkey, fname)
990+
verbose.report('\tfindfont found %(name)s, %(style)s, %(variant)s %(weight)s, %(stretch)s, %(size)s'%locals(), 'debug')
991+
verbose.report('findfont returning %s'%fname, 'debug')
992992
return fname
993993

994-
fontkey = FontKey(name, style, variant, weight, stretch, size)
995-
add_filename(fontdict, fontkey, self.defaultFont)
996-
verbose.report('Could not match %s, %s, %s. Returning %s' % (name, style, variant, self.defaultFont))
994+
font_family_aliases = ['serif', 'sans-serif', 'cursive', 'fantasy', 'monospace']
995+
996+
for name in prop.get_family():
997+
if name in font_family_aliases:
998+
for name2 in rcParams['font.' + name]:
999+
fname = lookup_name(name2)
1000+
if fname:
1001+
break
1002+
else:
1003+
fname = lookup_name(name)
1004+
if fname:
1005+
break
9971006

998-
return self.defaultFont
1007+
if not fname:
1008+
fontkey = FontKey(original_name, style, variant, weight, stretch, size)
1009+
add_filename(fontdict, fontkey, self.defaultFont)
1010+
verbose.report('Could not match %s, %s, %s. Returning %s' % (name, style, variant, self.defaultFont))
1011+
return self.defaultFont
1012+
return fname
9991013

10001014
def _get_afm_font_dict(self):
10011015
cache_message = "Saving AFM font cache for PS and PDF backends to %s.\n" \

lib/matplotlib/rcsetup.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -334,21 +334,24 @@ def __call__(self, s):
334334
'font.stretch' : ['normal', str], #
335335
'font.weight' : ['normal', str], #
336336
'font.size' : [12.0, validate_float], #
337-
'font.serif' : [['Bitstream Vera Serif','New Century Schoolbook',
338-
'Century Schoolbook L','Utopia','ITC Bookman',
339-
'Bookman','Nimbus Roman No9 L','Times New Roman',
340-
'Times','Palatino','Charter','serif'],
337+
'font.serif' : [['Bitstream Vera Serif', 'DejaVu Serif',
338+
'New Century Schoolbook', 'Century Schoolbook L',
339+
'Utopia', 'ITC Bookman', 'Bookman',
340+
'Nimbus Roman No9 L','Times New Roman',
341+
'Times','Palatino','Charter','serif'],
342+
validate_stringlist],
343+
'font.sans-serif' : [['Bitstream Vera Sans', 'DejaVu Sans',
344+
'Lucida Grande', 'Verdana', 'Geneva', 'Lucid',
345+
'Arial', 'Helvetica', 'Avant Garde', 'sans-serif'],
341346
validate_stringlist],
342-
'font.sans-serif' : [['Bitstream Vera Sans','Lucida Grande','Verdana',
343-
'Geneva','Lucid','Arial','Helvetica','Avant Garde',
344-
'sans-serif'], validate_stringlist],
345347
'font.cursive' : [['Apple Chancery','Textile','Zapf Chancery',
346348
'Sand','cursive'], validate_stringlist],
347349
'font.fantasy' : [['Comic Sans MS','Chicago','Charcoal','Impact'
348350
'Western','fantasy'], validate_stringlist],
349-
'font.monospace' : [['Bitstream Vera Sans Mono','Andale Mono'
350-
'Nimbus Mono L','Courier New','Courier','Fixed'
351-
'Terminal','monospace'], validate_stringlist],
351+
'font.monospace' : [['Bitstream Vera Sans Mono', 'DejaVu Sans Mono',
352+
'Andale Mono', 'Nimbus Mono L', 'Courier New',
353+
'Courier','Fixed', 'Terminal','monospace'],
354+
validate_stringlist],
352355

353356
# text props
354357
'text.color' : ['k', validate_color], # black
@@ -363,12 +366,12 @@ def __call__(self, s):
363366
'text.fontsize' : ['medium', validate_fontsize],
364367
'text.markup' : ['plain', validate_markup],
365368

366-
'mathtext.cal' : [(['cursive'], 'normal', 'normal'), validate_mathtext_font],
367-
'mathtext.rm' : [(['serif'], 'normal', 'normal'), validate_mathtext_font],
368-
'mathtext.tt' : [(['monospace'], 'normal', 'normal'), validate_mathtext_font],
369-
'mathtext.it' : [(['serif'], 'normal', 'oblique'), validate_mathtext_font],
370-
'mathtext.bf' : [(['serif'], 'bold', 'normal'), validate_mathtext_font],
371-
'mathtext.sf' : [(['sans-serif'], 'normal', 'normal'), validate_mathtext_font],
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],
372375
'mathtext.use_cm' : [True, validate_bool],
373376
'mathtext.fallback_to_cm' : [True, validate_bool],
374377

0 commit comments

Comments
 (0)