Skip to content

Commit 85089a1

Browse files
committed
Merge pull request #6073 from crazyo/master
fixed #6028
2 parents 1fb465a + 707c8b3 commit 85089a1

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

lib/matplotlib/mathtext.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,17 @@
6868
##############################################################################
6969
# FONTS
7070

71-
def get_unicode_index(symbol):
72-
"""get_unicode_index(symbol) -> integer
71+
def get_unicode_index(symbol, math=True):
72+
"""get_unicode_index(symbol, [bool]) -> integer
7373
7474
Return the integer index (from the Unicode table) of symbol. *symbol*
7575
can be a single unicode character, a TeX command (i.e. r'\pi'), or a
7676
Type1 symbol name (i.e. 'phi').
77+
If math is False, the current symbol should be treated as a non-math symbol.
7778
"""
79+
# for a non-math symbol, simply return its unicode index
80+
if not math:
81+
return ord(symbol)
7882
# From UTF #25: U+2212 minus sign is the preferred
7983
# representation of the unary and binary minus sign rather than
8084
# the ASCII-derived U+002D hyphen-minus, because minus sign is
@@ -438,7 +442,7 @@ def get_kern(self, font1, fontclass1, sym1, fontsize1,
438442
"""
439443
return 0.
440444

441-
def get_metrics(self, font, font_class, sym, fontsize, dpi):
445+
def get_metrics(self, font, font_class, sym, fontsize, dpi, math=True):
442446
"""
443447
*font*: one of the TeX font names::
444448
@@ -452,6 +456,8 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi):
452456
453457
*dpi*: current dots-per-inch
454458
459+
*math*: whether sym is a math character
460+
455461
Returns an object with the following attributes:
456462
457463
- *advance*: The advance distance (in points) of the glyph.
@@ -466,7 +472,7 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi):
466472
the glyph. This corresponds to TeX's definition of
467473
"height".
468474
"""
469-
info = self._get_info(font, font_class, sym, fontsize, dpi)
475+
info = self._get_info(font, font_class, sym, fontsize, dpi, math)
470476
return info.metrics
471477

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

585-
def _get_info(self, fontname, font_class, sym, fontsize, dpi):
591+
def _get_info(self, fontname, font_class, sym, fontsize, dpi, math=True):
586592
key = fontname, font_class, sym, fontsize, dpi
587593
bunch = self.glyphd.get(key)
588594
if bunch is not None:
589595
return bunch
590596

591597
font, num, symbol_name, fontsize, slanted = \
592-
self._get_glyph(fontname, font_class, sym, fontsize)
598+
self._get_glyph(fontname, font_class, sym, fontsize, math)
593599

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

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

682-
def _get_glyph(self, fontname, font_class, sym, fontsize):
688+
def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
683689
symbol_name = None
684690
font = None
685691
if fontname in self.fontmap and sym in latex_to_bakoma:
@@ -699,7 +705,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
699705

700706
if symbol_name is None:
701707
return self._stix_fallback._get_glyph(
702-
fontname, font_class, sym, fontsize)
708+
fontname, font_class, sym, fontsize, math)
703709

704710
return font, num, symbol_name, fontsize, slanted
705711

@@ -796,7 +802,7 @@ def __init__(self, *args, **kwargs):
796802
def _map_virtual_font(self, fontname, font_class, uniindex):
797803
return fontname, uniindex
798804

799-
def _get_glyph(self, fontname, font_class, sym, fontsize):
805+
def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
800806
found_symbol = False
801807

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

808814
if not found_symbol:
809815
try:
810-
uniindex = get_unicode_index(sym)
816+
uniindex = get_unicode_index(sym, math)
811817
found_symbol = True
812818
except ValueError:
813819
uniindex = ord('?')
@@ -900,11 +906,11 @@ def __init__(self, *args, **kwargs):
900906
self.fontmap[key] = fullpath
901907
self.fontmap[name] = fullpath
902908

903-
def _get_glyph(self, fontname, font_class, sym, fontsize):
909+
def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
904910
""" Override prime symbol to use Bakoma """
905911
if sym == r'\prime':
906912
return self.bakoma._get_glyph(fontname,
907-
font_class, sym, fontsize)
913+
font_class, sym, fontsize, math)
908914
else:
909915
# check whether the glyph is available in the display font
910916
uniindex = get_unicode_index(sym)
@@ -913,10 +919,10 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
913919
glyphindex = font.get_char_index(uniindex)
914920
if glyphindex != 0:
915921
return super(DejaVuFonts, self)._get_glyph('ex',
916-
font_class, sym, fontsize)
922+
font_class, sym, fontsize, math)
917923
# otherwise return regular glyph
918924
return super(DejaVuFonts, self)._get_glyph(fontname,
919-
font_class, sym, fontsize)
925+
font_class, sym, fontsize, math)
920926

921927

922928
class DejaVuSerifFonts(DejaVuFonts):
@@ -1124,7 +1130,7 @@ def _get_font(self, font):
11241130
self.fonts[cached_font.get_fontname()] = cached_font
11251131
return cached_font
11261132

1127-
def _get_info (self, fontname, font_class, sym, fontsize, dpi):
1133+
def _get_info (self, fontname, font_class, sym, fontsize, dpi, math=True):
11281134
'load the cmfont, metrics and glyph with caching'
11291135
key = fontname, sym, fontsize, dpi
11301136
tup = self.glyphd.get(key)
@@ -1450,14 +1456,15 @@ class Char(Node):
14501456
from width) must be converted into a :class:`Kern` node when the
14511457
:class:`Char` is added to its parent :class:`Hlist`.
14521458
"""
1453-
def __init__(self, c, state):
1459+
def __init__(self, c, state, math=True):
14541460
Node.__init__(self)
14551461
self.c = c
14561462
self.font_output = state.font_output
14571463
self.font = state.font
14581464
self.font_class = state.font_class
14591465
self.fontsize = state.fontsize
14601466
self.dpi = state.dpi
1467+
self.math = math
14611468
# The real width, height and depth will be set during the
14621469
# pack phase, after we know the real fontsize
14631470
self._update_metrics()
@@ -1467,7 +1474,7 @@ def __internal_repr__(self):
14671474

14681475
def _update_metrics(self):
14691476
metrics = self._metrics = self.font_output.get_metrics(
1470-
self.font, self.font_class, self.c, self.fontsize, self.dpi)
1477+
self.font, self.font_class, self.c, self.fontsize, self.dpi, self.math)
14711478
if self.c == ' ':
14721479
self.width = metrics.advance
14731480
else:
@@ -2589,7 +2596,7 @@ def math(self, s, loc, toks):
25892596
def non_math(self, s, loc, toks):
25902597
#~ print "non_math", toks
25912598
s = toks[0].replace(r'\$', '$')
2592-
symbols = [Char(c, self.get_state()) for c in s]
2599+
symbols = [Char(c, self.get_state(), math=False) for c in s]
25932600
hlist = Hlist(symbols)
25942601
# We're going into math now, so set font to 'it'
25952602
self.push_state()

0 commit comments

Comments
 (0)