68
68
##############################################################################
69
69
# FONTS
70
70
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
73
73
74
74
Return the integer index (from the Unicode table) of symbol. *symbol*
75
75
can be a single unicode character, a TeX command (i.e. r'\pi'), or a
76
76
Type1 symbol name (i.e. 'phi').
77
+ If nonMath is True, the current symbol should be treated as a non-math symbol.
77
78
"""
78
79
# From UTF #25: U+2212 minus sign is the preferred
79
80
# representation of the unary and binary minus sign rather than
80
81
# the ASCII-derived U+002D hyphen-minus, because minus sign is
81
82
# unambiguous and because it is rendered with a more desirable
82
83
# length, usually longer than a hyphen.
83
- if symbol == '-' :
84
+ # Hyphens in texts will not be affected.
85
+ if not nonMath and symbol == '-' :
84
86
return 0x2212
85
87
try :# This will succeed if symbol is a single unicode char
86
88
return ord (symbol )
@@ -438,7 +440,7 @@ def get_kern(self, font1, fontclass1, sym1, fontsize1,
438
440
"""
439
441
return 0.
440
442
441
- def get_metrics (self , font , font_class , sym , fontsize , dpi ):
443
+ def get_metrics (self , font , font_class , sym , fontsize , dpi , nonMath = False ):
442
444
"""
443
445
*font*: one of the TeX font names::
444
446
@@ -452,6 +454,8 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi):
452
454
453
455
*dpi*: current dots-per-inch
454
456
457
+ *nonMath*: whether sym is a nonMath character
458
+
455
459
Returns an object with the following attributes:
456
460
457
461
- *advance*: The advance distance (in points) of the glyph.
@@ -466,7 +470,7 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi):
466
470
the glyph. This corresponds to TeX's definition of
467
471
"height".
468
472
"""
469
- info = self ._get_info (font , font_class , sym , fontsize , dpi )
473
+ info = self ._get_info (font , font_class , sym , fontsize , dpi , nonMath )
470
474
return info .metrics
471
475
472
476
def set_canvas_size (self , w , h , d ):
@@ -582,14 +586,14 @@ def _get_offset(self, font, glyph, fontsize, dpi):
582
586
return ((glyph .height / 64.0 / 2.0 ) + (fontsize / 3.0 * dpi / 72.0 ))
583
587
return 0.
584
588
585
- def _get_info (self , fontname , font_class , sym , fontsize , dpi ):
589
+ def _get_info (self , fontname , font_class , sym , fontsize , dpi , nonMath = False ):
586
590
key = fontname , font_class , sym , fontsize , dpi
587
591
bunch = self .glyphd .get (key )
588
592
if bunch is not None :
589
593
return bunch
590
594
591
595
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 )
593
597
594
598
font .set_size (fontsize , dpi )
595
599
glyph = font .load_char (
@@ -679,7 +683,7 @@ def __init__(self, *args, **kwargs):
679
683
680
684
_slanted_symbols = set (r"\int \oint" .split ())
681
685
682
- def _get_glyph (self , fontname , font_class , sym , fontsize ):
686
+ def _get_glyph (self , fontname , font_class , sym , fontsize , nonMath = False ):
683
687
symbol_name = None
684
688
font = None
685
689
if fontname in self .fontmap and sym in latex_to_bakoma :
@@ -699,7 +703,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
699
703
700
704
if symbol_name is None :
701
705
return self ._stix_fallback ._get_glyph (
702
- fontname , font_class , sym , fontsize )
706
+ fontname , font_class , sym , fontsize , nonMath )
703
707
704
708
return font , num , symbol_name , fontsize , slanted
705
709
@@ -796,7 +800,7 @@ def __init__(self, *args, **kwargs):
796
800
def _map_virtual_font (self , fontname , font_class , uniindex ):
797
801
return fontname , uniindex
798
802
799
- def _get_glyph (self , fontname , font_class , sym , fontsize ):
803
+ def _get_glyph (self , fontname , font_class , sym , fontsize , nonMath = False ):
800
804
found_symbol = False
801
805
802
806
if self .use_cmex :
@@ -807,7 +811,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
807
811
808
812
if not found_symbol :
809
813
try :
810
- uniindex = get_unicode_index (sym )
814
+ uniindex = get_unicode_index (sym , nonMath )
811
815
found_symbol = True
812
816
except ValueError :
813
817
uniindex = ord ('?' )
@@ -900,11 +904,11 @@ def __init__(self, *args, **kwargs):
900
904
self .fontmap [key ] = fullpath
901
905
self .fontmap [name ] = fullpath
902
906
903
- def _get_glyph (self , fontname , font_class , sym , fontsize ):
907
+ def _get_glyph (self , fontname , font_class , sym , fontsize , nonMath = False ):
904
908
""" Override prime symbol to use Bakoma """
905
909
if sym == r'\prime' :
906
910
return self .bakoma ._get_glyph (fontname ,
907
- font_class , sym , fontsize )
911
+ font_class , sym , fontsize , nonMath )
908
912
else :
909
913
# check whether the glyph is available in the display font
910
914
uniindex = get_unicode_index (sym )
@@ -913,10 +917,10 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
913
917
glyphindex = font .get_char_index (uniindex )
914
918
if glyphindex != 0 :
915
919
return super (DejaVuFonts , self )._get_glyph ('ex' ,
916
- font_class , sym , fontsize )
920
+ font_class , sym , fontsize , nonMath )
917
921
# otherwise return regular glyph
918
922
return super (DejaVuFonts , self )._get_glyph (fontname ,
919
- font_class , sym , fontsize )
923
+ font_class , sym , fontsize , nonMath )
920
924
921
925
922
926
class DejaVuSerifFonts (DejaVuFonts ):
@@ -1450,14 +1454,15 @@ class Char(Node):
1450
1454
from width) must be converted into a :class:`Kern` node when the
1451
1455
:class:`Char` is added to its parent :class:`Hlist`.
1452
1456
"""
1453
- def __init__ (self , c , state ):
1457
+ def __init__ (self , c , state , nonMath = False ):
1454
1458
Node .__init__ (self )
1455
1459
self .c = c
1456
1460
self .font_output = state .font_output
1457
1461
self .font = state .font
1458
1462
self .font_class = state .font_class
1459
1463
self .fontsize = state .fontsize
1460
1464
self .dpi = state .dpi
1465
+ self .nonMath = nonMath
1461
1466
# The real width, height and depth will be set during the
1462
1467
# pack phase, after we know the real fontsize
1463
1468
self ._update_metrics ()
@@ -1467,7 +1472,7 @@ def __internal_repr__(self):
1467
1472
1468
1473
def _update_metrics (self ):
1469
1474
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 )
1471
1476
if self .c == ' ' :
1472
1477
self .width = metrics .advance
1473
1478
else :
@@ -2589,7 +2594,7 @@ def math(self, s, loc, toks):
2589
2594
def non_math (self , s , loc , toks ):
2590
2595
#~ print "non_math", toks
2591
2596
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 ]
2593
2598
hlist = Hlist (symbols )
2594
2599
# We're going into math now, so set font to 'it'
2595
2600
self .push_state ()
0 commit comments