8000 fixed bug 6028 by crazyo · Pull Request #6073 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

fixed bug 6028 #6073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 6, 2016
Merged
Changes from all commits
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
43 changes: 25 additions & 18 deletions lib/matplotlib/mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@
##############################################################################
# FONTS

def get_unicode_index(symbol):
"""get_unicode_index(symbol) -> integer
def get_unicode_index(symbol, math=True):
"""get_unicode_index(symbol, [bool]) -> integer

Return the integer index (from the Unicode table) of symbol. *symbol*
can be a single unicode character, a TeX command (i.e. r'\pi'), or a
Type1 symbol name (i.e. 'phi').
If math is False, the current symbol should be treated as a non-math symbol.
"""
# for a non-math symbol, simply return its unicode index
if not math:
return ord(symbol)
# From UTF #25: U+2212 minus sign is the preferred
# representation of the unary and binary minus sign rather than
# the ASCII-derived U+002D hyphen-minus, because minus sign is
Expand Down Expand Up @@ -438,7 +442,7 @@ def get_kern(self, font1, fontclass1, sym1, fontsize1,
"""
return 0.

def get_metrics(self, font, font_class, sym, fontsize, dpi):
def get_metrics(self, font, font_class, sym, fontsize, dpi, math=True):
"""
*font*: one of the TeX font names::

Expand All @@ -452,6 +456,8 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi):

*dpi*: current dots-per-inch

*math*: whether sym is a math character

Returns an object with the following attributes:

- *advance*: The advance distance (in points) of the glyph.
Expand All @@ -466,7 +472,7 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi):
the glyph. This corresponds to TeX's definition of
"height".
"""
info = self._get_info(font, font_class, sym, fontsize, dpi)
info = self._get_info(font, font_class, sym, fontsize, dpi, math)
return info.metrics

def set_canvas_size(self, w, h, d):
Expand Down Expand Up @@ -582,14 +588,14 @@ def _get_offset(self, font, glyph, fontsize, dpi):
return ((glyph.height/64.0/2.0) + (fontsize/3.0 * dpi/72.0))
return 0.

def _get_info(self, fontname, font_class, sym, fontsize, dpi):
def _get_info(self, fontname, font_class, sym, fontsize, dpi, math=True):
key = fontname, font_class, sym, fontsize, dpi
bunch = self.glyphd.get(key)
if bunch is not None:
return bunch

font, num, symbol_name, fontsize, slanted = \
self._get_glyph(fontname, font_class, sym, fontsize)
self._get_glyph(fontname, font_class, sym, fontsize, math)

font.set_size(fontsize, dpi)
glyph = font.load_char(
Expand Down Expand Up @@ -679,7 +685,7 @@ def __init__(self, *args, **kwargs):

_slanted_symbols = set(r"\int \oint".split())

def _get_glyph(self, fontname, font_class, sym, fontsize):
def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
symbol_name = None
font = None
if fontname in self.fontmap and sym in latex_to_bakoma:
Expand All @@ -699,7 +705,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):

if symbol_name is None:
return self._stix_fallback._get_glyph(
fontname, font_class, sym, fontsize)
fontname, font_class, sym, fontsize, math)

return font, num, symbol_name, fontsize, slanted

Expand Down Expand Up @@ -796,7 +802,7 @@ def __init__(self, *args, **kwargs):
def _map_virtual_font(self, fontname, font_class, uniindex):
return fontname, uniindex

def _get_glyph(self, fontname, font_class, sym, fontsize):
def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
found_symbol = False

if self.use_cmex:
Expand All @@ -807,7 +813,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):

if not found_symbol:
try:
uniindex = get_unicode_index(sym)
uniindex = get_unicode_index(sym, math)
found_symbol = True
except ValueError:
uniindex = ord('?')
Expand Down Expand Up @@ -900,11 +906,11 @@ def __init__(self, *args, **kwargs):
self.fontmap[key] = fullpath
self.fontmap[name] = fullpath

def _get_glyph(self, fontname, font_class, sym, fontsize):
def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
""" Override prime symbol to use Bakoma """
if sym == r'\prime':
return self.bakoma._get_glyph(fontname,
font_class, sym, fontsize)
font_class, sym, fontsize, math)
else:
# check whether the glyph is available in the display font
uniindex = get_unicode_index(sym)
Expand All @@ -913,10 +919,10 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
glyphindex = font.get_char_index(uniindex)
if glyphindex != 0:
return super(DejaVuFonts, self)._get_glyph('ex',
font_class, sym, fontsize)
font_class, sym, fontsize, math)
# otherwise return regular glyph
return super(DejaVuFonts, self)._get_glyph(fontname,
font_class, sym, fontsize)
font_class, sym, fontsize, math)


class DejaVuSerifFonts(DejaVuFonts):
Expand Down Expand Up @@ -1124,7 +1130,7 @@ def _get_font(self, font):
self.fonts[cached_font.get_fontname()] = cached_font
return cached_font

def _get_info (self, fontname, font_class, sym, fontsize, dpi):
def _get_info (self, fontname, font_class, sym, fontsize, dpi, math=True):
'load the cmfont, metrics and glyph with caching'
key = fontname, sym, fontsize, dpi
tup = self.glyphd.get(key)
Expand Down Expand Up @@ -1450,14 +1456,15 @@ class Char(Node):
from width) must be converted into a :class:`Kern` node when the
:class:`Char` is added to its parent :class:`Hlist`.
"""
def __init__(self, c, state):
def __init__(self, c, state, math=True):
Node.__init__(self)
self.c = c
self.font_output = state.font_output
self.font = state.font
self.font_class = state.font_class
self.fontsize = state.fontsize
self.dpi = state.dpi
self.math = math
# The real width, height and depth will be set during the
# pack phase, after we know the real fontsize
self._update_metrics()
Expand All @@ -1467,7 +1474,7 @@ def __internal_repr__(self):

def _update_metrics(self):
metrics = self._metrics = self.font_output.get_metrics(
self.font, self.font_class, self.c, self.fontsize, self.dpi)
self.font, self.font_class, self.c, self.fontsize, self.dpi, self.math)
if self.c == ' ':
self.width = metrics.advance
else:
Expand Down Expand Up @@ -2589,7 +2596,7 @@ def math(self, s, loc, toks):
def non_math(self, s, loc, toks):
#~ print "non_math", toks
s = toks[0].replace(r'\$', '$')
symbols = [Char(c, self.get_state()) for c in s]
symbols = [Char(c, self.get_state(), math=False) for c in s]
hlist = Hlist(symbols)
# We're going into math now, so set font to 'it'
self.push_state()
Expand Down
0