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

Skip to content

Commit ad8533c

Browse files
committed
fixed bug 6028
1 parent 90a6984 commit ad8533c

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

lib/matplotlib/mathtext.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,21 @@
6868
##############################################################################
6969
# FONTS
7070

71-
def get_unicode_index(symbol):
72-
"""get_unicode_index(symbol) -> integer
71+
def get_unicode_index(symbol, nonMath=False):
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 nonMath is True, the current symbol should be treated as a non-math symbol.
7778
"""
7879
# From UTF #25: U+2212 minus sign is the preferred
7980
# representation of the unary and binary minus sign rather than
8081
# the ASCII-derived U+002D hyphen-minus, because minus sign is
8182
# unambiguous and because it is rendered with a more desirable
8283
# length, usually longer than a hyphen.
83-
if symbol == '-':
84+
# Hyphens in texts will not be affected.
85+
if not nonMath and symbol == '-':
8486
return 0x2212
8587
try:# This will succeed if symbol is a single unicode char
8688
return ord(symbol)
@@ -438,7 +440,7 @@ def get_kern(self, font1, fontclass1, sym1, fontsize1,
438440
"""
439441
return 0.
440442

441-
def get_metrics(self, font, font_class, sym, fontsize, dpi):
443+
def get_metrics(self, font, font_class, sym, fontsize, dpi, nonMath=False):
442444
"""
443445
*font*: one of the TeX font names::
444446
@@ -452,6 +454,8 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi):
452454
453455
*dpi*: current dots-per-inch
454456
457+
*nonMath*: whether sym is a nonMath character
458+
455459
Returns an object with the following attributes:
456460
457461
- *advance*: The advance distance (in points) of the glyph.
@@ -466,7 +470,7 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi):
466470
the glyph. This corresponds to TeX's definition of
467471
"height".
468472
"""
469-
info = self._get_info(font, font_class, sym, fontsize, dpi)
473+
info = self._get_info(font, font_class, sym, fontsize, dpi, nonMath)
470474
return info.metrics
471475

472476
def set_canvas_size(self, w, h, d):
@@ -582,14 +586,14 @@ def _get_offset(self, font, glyph, fontsize, dpi):
582586
return ((glyph.height/64.0/2.0) + (fontsize/3.0 * dpi/72.0))
583587
return 0.
584588

585-
def _get_info(self, fontname, font_class, sym, fontsize, dpi):
589+
def _get_info(self, fontname, font_class, sym, fontsize, dpi, nonMath=False):
586590
key = fontname, font_class, sym, fontsize, dpi
587591
bunch = self.glyphd.get(key)
588592
if bunch is not None:
589593
return bunch
590594

591595
font, num, symbol_name, fontsize, slanted = \
592-
self._get_glyph(fontname, font_class, sym, fontsize)
596+
self._get_glyph(fontname, font_class, sym, fontsize, nonMath)
593597

594598
font.set_size(fontsize, dpi)
595599
glyph = font.load_char(
@@ -679,7 +683,7 @@ def __init__(self, *args, **kwargs):
679683

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

682-
def _get_glyph(self, fontname, font_class, sym, fontsize):
686+
def _get_glyph(self, fontname, font_class, sym, fontsize, nonMath=False):
683687
symbol_name = None
684688
font = None
685689
if fontname in self.fontmap and sym in latex_to_bakoma:
@@ -699,7 +703,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
699703

700704
if symbol_name is None:
701705
return self._stix_fallback._get_glyph(
702-
fontname, font_class, sym, fontsize)
706+
fontname, font_class, sym, fontsize, nonMath)
703707

704708
return font, num, symbol_name, fontsize, slanted
705709

@@ -796,7 +800,7 @@ def __init__(self, *args, **kwargs):
796800
def _map_virtual_font(self, fontname, font_class, uniindex):
797801
return fontname, uniindex
798802

799-
def _get_glyph(self, fontname, font_class, sym, fontsize):
803+
def _get_glyph(self, fontname, font_class, sym, fontsize, nonMath=False):
800804
found_symbol = False
801805

802806
if self.use_cmex:
@@ -807,7 +811,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
807811

808812
if not found_symbol:
809813
try:
810-
uniindex = get_unicode_index(sym)
814+
uniindex = get_unicode_index(sym, nonMath)
811815
found_symbol = True
812816
except ValueError:
813817
uniindex = ord('?')
@@ -900,11 +904,11 @@ def __init__(self, *args, **kwargs):
900904
self.fontmap[key] = fullpath
901905
self.fontmap[name] = fullpath
902906

903-
def _get_glyph(self, fontname, font_class, sym, fontsize):
907+
def _get_glyph(self, fontname, font_class, sym, fontsize, nonMath=False):
904908
""" Override prime symbol to use Bakoma """
905909
if sym == r'\prime':
906910
return self.bakoma._get_glyph(fontname,
907-
font_class, sym, fontsize)
911+
font_class, sym, fontsize, nonMath)
908912
else:
909913
# check whether the glyph is available in the display font
910914
uniindex = get_unicode_index(sym)
@@ -913,10 +917,10 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
913917
glyphindex = font.get_char_index(uniindex)
914918
if glyphindex != 0:
915919
return super(DejaVuFonts, self)._get_glyph('ex',
916-
font_class, sym, fontsize)
920+
font_class, sym, fontsize, nonMath)
917921
# otherwise return regular glyph
918922
return super(DejaVuFonts, self)._get_glyph(fontname,
919-
font_class, sym, fontsize)
923+
font_class, sym, fontsize, nonMath)
920924

921925

922926
class DejaVuSerifFonts(DejaVuFonts):
@@ -1450,14 +1454,15 @@ class Char(Node):
14501454
from width) must be converted into a :class:`Kern` node when the
14511455
:class:`Char` is added to its parent :class:`Hlist`.
14521456
"""
1453-
def __init__(self, c, state):
1457+
def __init__(self, c, state, nonMath=False):
14541458
Node.__init__(self)
14551459
self.c = c
14561460
self.font_output = state.font_output
14571461
self.font = state.font
14581462
self.font_class = state.font_class
14591463
self.fontsize = state.fontsize
14601464
self.dpi = state.dpi
1465+
self.nonMath = nonMath
14611466
# The real width, height and depth will be set during the
14621467
# pack phase, after we know the real fontsize
14631468
self._update_metrics()
@@ -1467,7 +1472,7 @@ def __internal_repr__(self):
14671472

14681473
def _update_metrics(self):
14691474
metrics = self._metrics = self.font_output.get_metrics(
1470-
self.font, self.font_class, self.c, self.fontsize, self.dpi)
1475+
self.font, self.font_class, self.c, self.fontsize, self.dpi, self.nonMath)
14711476
if self.c == ' ':
14721477
self.width = metrics.advance
14731478
else:
@@ -2589,7 +2594,7 @@ def math(self, s, loc, toks):
25892594
def non_math(self, s, loc, toks):
25902595
#~ print "non_math", toks
25912596
s = toks[0].replace(r'\$', '$')
2592-
symbols = [Char(c, self.get_state()) for c in s]
2597+
symbols = [Char(c, self.get_state(), nonMath=True) for c in s]
25932598
hlist = Hlist(symbols)
25942599
# We're going into math now, so set font to 'it'
25952600
self.push_state()

0 commit comments

Comments
 (0)