Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add DejaVu mathtext fontset, make it default
Add mathtext options to use dejavu

use dejavusans as default subsuper metric for custom

set dejavusans as default mathtext.fontset

fix sized alternatives in dejavu mathtext
  • Loading branch information
zblz committed Nov 5, 2015
commit 6a5df0b471e856b7c26a1f6bd5dabedfdfe514d3
119 changes: 87 additions & 32 deletions lib/matplotlib/mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,10 +841,16 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):

if not found_symbol:
if self.cm_fallback:
warn("Substituting with a symbol from Computer Modern.",
MathTextWarning)
return self.cm_fallback._get_glyph(
fontname, 'it', sym, fontsize)
if isinstance(self.cm_fallback, BakomaFonts):
warn("Substituting with a symbol from Computer Modern.",
MathTextWarning)
if (fontname in ('it', 'regular') and
isinstance(self.cm_fallback, StixFonts)):
return self.cm_fallback._get_glyph(
'rm', font_class, sym, fontsize)
else:
return self.cm_fallback._get_glyph(
fontname, 'it', sym, fontsize)
else:
if fontname in ('it', 'regular') and isinstance(self, StixFonts):
return self._get_glyph('rm', font_class, sym, fontsize)
Expand All @@ -870,6 +876,56 @@ def get_sized_alternatives_for_symbol(self, fontname, sym):
fontname, sym)
return [(fontname, sym)]

class DejaVuFonts(UnicodeFonts):
use_cmex = False

def __init__(self, *args, **kwargs):
# This must come first so the backend's owner is set correctly
if isinstance(self, DejaVuSerifFonts):
self.cm_fallback = StixFonts(*args, **kwargs)
else:
self.cm_fallback = StixSansFonts(*args, **kwargs)
TruetypeFonts.__init__(self, *args, **kwargs)
self.fontmap = {}
# Include Stix sized alternatives for glyphs
self._fontmap.update({
0 : 'STIXGeneral',
1 : 'STIXSizeOneSym',
2 : 'STIXSizeTwoSym',
3 : 'STIXSizeThreeSym',
4 : 'STIXSizeFourSym',
5 : 'STIXSizeFiveSym'})
for key, name in six.iteritems(self._fontmap):
fullpath = findfont(name)
self.fontmap[key] = fullpath
self.fontmap[name] = fullpath

class DejaVuSerifFonts(DejaVuFonts):
"""
A font handling class for the DejaVu Serif fonts

If a glyph is not found it will fallback to Stix Serif
"""
_fontmap = { 'rm' : 'DejaVu Serif',
'it' : 'DejaVu Serif:italic',
'bf' : 'DejaVu Serif:weight=bold',
'sf' : 'DejaVu Sans',
'tt' : 'DejaVu Sans Mono',
}

class DejaVuSansFonts(DejaVuFonts):
"""
A font handling class for the DejaVu Sans fonts

If a glyph is not found it will fallback to Stix Sans
"""
_fontmap = { 'rm' : 'DejaVu Sans',
'it' : 'DejaVu Sans:italic',
'bf' : 'DejaVu Sans:weight=bold',
'sf' : 'DejaVu Sans',
'tt' : 'DejaVu Sans Mono',
}

class StixFonts(UnicodeFonts):
"""
A font handling class for the STIX fonts.
Expand Down Expand Up @@ -1178,46 +1234,54 @@ def get_underline_thickness(self, font, fontsize, dpi):
SCRIPT_SPACE = {'cm': 0.075,
'stix': 0.10,
'stixsans': 0.05,
'arevsans': 0.05}
'dejavuserif': 0.05,
'dejavusans': 0.05}
## Percentage of x-height that sub/superscripts drop below the baseline
SUBDROP = {'cm': 0.2,
'stix': 0.4,
'stixsans': 0.4,
'arevsans': 0.4}
'dejavuserif': 0.4,
'dejavusans': 0.4}
# Percentage of x-height that superscripts are raised from the baseline
SUP1 = {'cm': 0.45,
'stix': 0.8,
'stixsans': 0.8,
'arevsans': 0.7}
'dejavuserif': 0.7,
'dejavusans': 0.7}
# Percentage of x-height that subscripts drop below the baseline
SUB1 = {'cm': 0.2,
'stix': 0.3,
'stixsans': 0.3,
'arevsans': 0.3}
'dejavuserif': 0.3,
'dejavusans': 0.3}
# Percentage of x-height that subscripts drop below the baseline when a
# superscript is present
SUB2 = {'cm': 0.3,
'stix': 0.6,
'stixsans': 0.5,
'arevsans': 0.5}
'dejavuserif': 0.5,
'dejavusans': 0.5}
# Percentage of x-height that sub/supercripts are offset relative to the
# nucleus edge for non-slanted nuclei
DELTA = {'cm': 0.075,
'stix': 0.05,
'stixsans': 0.025,
'arevsans': 0.025}
'dejavuserif': 0.025,
'dejavusans': 0.025}
# Additional percentage of last character height above 2/3 of the x-height that
# supercripts are offset relative to the subscript for slanted nuclei
DELTASLANTED = {'cm': 0.3,
'stix': 0.3,
'stixsans': 0.6,
'arevsans': 0.2}
'dejavuserif': 0.2,
'dejavusans': 0.2}
# Percentage of x-height that supercripts and subscripts are offset for
# integrals
DELTAINTEGRAL = {'cm': 0.3,
'stix': 0.3,
'stixsans': 0.3,
'arevsans': 0.3}
'dejavuserif': 0.1,
'dejavusans': 0.1}

class MathTextWarning(Warning):
pass
Expand Down Expand Up @@ -2676,19 +2740,6 @@ def is_slanted(self, nucleus):
return nucleus.is_slanted()
return False

def _get_fontset_name(self):
fs = rcParams['mathtext.fontset']
# If a custom fontset is used, check if it is Arev Sans, otherwise use
# CM parameters.
if fs == 'custom':
if (rcParams['mathtext.rm'] == 'sans' and
rcParams['font.sans-serif'][0].lower() == 'Arev Sans'.lower()):
fs = 'arevsans'
else:
fs = 'cm'

return fs

def subsuper(self, s, loc, toks):
assert(len(toks)==1)

Expand Down Expand Up @@ -2811,7 +2862,9 @@ def subsuper(self, s, loc, toks):

# Handle regular sub/superscripts

fs = self._get_fontset_name()
fs = rcParams['mathtext.fontset']
if fs == 'custom':
fs = 'dejavusans'

lc_height = last_char.height
lc_baseline = 0
Expand Down Expand Up @@ -3056,10 +3109,12 @@ class MathTextParser(object):
}

_font_type_mapping = {
'cm' : BakomaFonts,
'stix' : StixFonts,
'stixsans' : StixSansFonts,
'custom' : UnicodeFonts
'cm' : BakomaFonts,
'dejavuserif' : DejaVuSerifFonts,
'dejavusans' : DejaVuSansFonts,
'stix' : StixFonts,
'stixsans' : StixSansFonts,
'custom' : UnicodeFonts
}

def __init__(self, output):
Expand Down Expand Up @@ -3100,8 +3155,8 @@ def parse(self, s, dpi = 72, prop = None):
font_output = fontset_class(prop, backend)
else:
raise ValueError(
"mathtext.fontset must be either 'cm', 'stix', "
"'stixsans', or 'custom'")
"mathtext.fontset must be either 'cm', 'dejavuserif', "
"'dejavusans', 'stix', 'stixsans', or 'custom'")

fontsize = prop.get_size_in_points()

Expand Down
10 changes: 5 additions & 5 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def validate_font_properties(s):

validate_fontset = ValidateInStrings(
'fontset',
['cm', 'stix', 'stixsans', 'custom'])
['dejavusans', 'dejavuserif', 'cm', 'stix', 'stixsans', 'custom'])

validate_mathtext_default = ValidateInStrings(
'default',
Expand Down Expand Up @@ -886,10 +886,10 @@ def validate_cycler(s):
'mathtext.cal': ['cursive', validate_font_properties],
'mathtext.rm': ['serif', validate_font_properties],
'mathtext.tt': ['monospace', validate_font_properties],
'mathtext.it': ['serif:italic', validate_font_properties],
'mathtext.bf': ['serif:bold', validate_font_properties],
'mathtext.sf': ['sans\-serif', validate_font_properties],
'mathtext.fontset': ['cm', validate_fontset],
'mathtext.it': ['sans:italic', validate_font_properties],
'mathtext.bf': ['sans:bold', validate_font_properties],
'mathtext.sf': ['sans', validate_font_properties],
'mathtext.fontset': ['dejavusans', validate_fontset],
'mathtext.default': ['it', validate_mathtext_default],
'mathtext.fallback_to_cm': [True, validate_bool],

Expand Down
5 changes: 3 additions & 2 deletions matplotlibrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,9 @@ backend : %(backend)s
#mathtext.it : serif:italic
#mathtext.bf : serif:bold
#mathtext.sf : sans
#mathtext.fontset : cm # Should be 'cm' (Computer Modern), 'stix',
# 'stixsans' or 'custom'
#mathtext.fontset : dejavusans # Should be 'cm' (Computer Modern),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put dejavusans first in the list and say "'dejavusans' (default),"

# 'dejavusans', 'dejavuserif', 'stix',
# 'stixsans' or 'custom'
#mathtext.fallback_to_cm : True # When True, use symbols from the Computer Modern
# fonts when a symbol can not be found in one of
# the custom math fonts.
Expand Down