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 , math = True ):
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 math is False, the current symbol should be treated as a non-math symbol.
77
78
"""
79
+ # for a non-math symbol, simply return its unicode index
80
+ if not math :
81
+ return ord (symbol )
78
82
# From UTF #25: U+2212 minus sign is the preferred
79
83
# representation of the unary and binary minus sign rather than
80
84
# the ASCII-derived U+002D hyphen-minus, because minus sign is
@@ -438,7 +442,7 @@ def get_kern(self, font1, fontclass1, sym1, fontsize1,
438
442
"""
439
443
return 0.
440
444
441
- def get_metrics (self , font , font_class , sym , fontsize , dpi ):
445
+ def get_metrics (self , font , font_class , sym , fontsize , dpi , math = True ):
442
446
"""
443
447
*font*: one of the TeX font names::
444
448
@@ -452,6 +456,8 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi):
452
456
453
457
*dpi*: current dots-per-inch
454
458
459
+ *math*: whether sym is a math character
460
+
455
461
Returns an object with the following attributes:
456
462
457
463
- *advance*: The advance distance (in points) of the glyph.
@@ -466,7 +472,7 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi):
466
472
the glyph. This corresponds to TeX's definition of
467
473
"height".
468
474
"""
469
- info = self ._get_info (font , font_class , sym , fontsize , dpi )
475
+ info = self ._get_info (font , font_class , sym , fontsize , dpi , math )
470
476
return info .metrics
471
477
472
478
def set_canvas_size (self , w , h , d ):
@@ -582,14 +588,14 @@ def _get_offset(self, font, glyph, fontsize, dpi):
582
588
return ((glyph .height / 64.0 / 2.0 ) + (fontsize / 3.0 * dpi / 72.0 ))
583
589
return 0.
584
590
585
- def _get_info (self , fontname , font_class , sym , fontsize , dpi ):
591
+ def _get_info (self , fontname , font_class , sym , fontsize , dpi , math = True ):
586
592
key = fontname , font_class , sym , fontsize , dpi
587
593
bunch = self .glyphd .get (key )
588
594
if bunch is not None :
589
595
return bunch
590
596
591
597
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 )
593
599
594
600
font .set_size (fontsize , dpi )
595
601
glyph = font .load_char (
@@ -679,7 +685,7 @@ def __init__(self, *args, **kwargs):
679
685
680
686
_slanted_symbols = set (r"\int \oint" .split ())
681
687
682
- def _get_glyph (self , fontname , font_class , sym , fontsize ):
688
+ def _get_glyph (self , fontname , font_class , sym , fontsize , math = True ):
683
689
symbol_name = None
684
690
font = None
685
691
if fontname in self .fontmap and sym in latex_to_bakoma :
@@ -699,7 +705,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
699
705
700
706
if symbol_name is None :
701
707
return self ._stix_fallback ._get_glyph (
702
- fontname , font_class , sym , fontsize )
708
+ fontname , font_class , sym , fontsize , math )
703
709
704
710
return font , num , symbol_name , fontsize , slanted
705
711
@@ -796,7 +802,7 @@ def __init__(self, *args, **kwargs):
796
802
def _map_virtual_font (self , fontname , font_class , uniindex ):
797
803
return fontname , uniindex
798
804
799
- def _get_glyph (self , fontname , font_class , sym , fontsize ):
805
+ def _get_glyph (self , fontname , font_class , sym , fontsize , math = True ):
800
806
found_symbol = False
801
807
802
808
if self .use_cmex :
@@ -807,7 +813,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
807
813
808
814
if not found_symbol :
809
815
try :
810
- uniindex = get_unicode_index (sym )
816
+ uniindex = get_unicode_index (sym , math )
811
817
found_symbol = True
812
818
except ValueError :
813
819
uniindex = ord ('?' )
@@ -901,11 +907,11 @@ def __init__(self, *args, **kwargs):
901
907
self .fontmap [key ] = fullpath
902
908
self .fontmap [name ] = fullpath
903
909
904
- def _get_glyph (self , fontname , font_class , sym , fontsize ):
910
+ def _get_glyph (self , fontname , font_class , sym , fontsize , math = True ):
905
911
""" Override prime symbol to use Bakoma """
906
912
if sym == r'\prime' :
907
913
return self .bakoma ._get_glyph (fontname ,
908
- font_class , sym , fontsize )
914
+ font_class , sym , fontsize , math )
909
915
else :
910
916
# check whether the glyph is available in the display font
911
917
uniindex = get_unicode_index (sym )
@@ -914,10 +920,10 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
914
920
glyphindex = font .get_char_index (uniindex )
915
921
if glyphindex != 0 :
916
922
return super (DejaVuFonts , self )._get_glyph ('ex' ,
917
- font_class , sym , fontsize )
923
+ font_class , sym , fontsize , math )
918
924
# otherwise return regular glyph
919
925
return super (DejaVuFonts , self )._get_glyph (fontname ,
920
- font_class , sym , fontsize )
926
+ font_class , sym , fontsize , math )
921
927
922
928
923
929
class DejaVuSerifFonts (DejaVuFonts ):
@@ -1123,7 +1129,7 @@ def _get_font(self, font):
1123
1129
self .fonts [cached_font .get_fontname ()] = cached_font
1124
1130
return cached_font
1125
1131
1126
- def _get_info (self , fontname , font_class , sym , fontsize , dpi ):
1132
+ def _get_info (self , fontname , font_class , sym , fontsize , dpi , math = True ):
1127
1133
'load the cmfont, metrics and glyph with caching'
1128
1134
key = fontname , sym , fontsize , dpi
1129
1135
tup = self .glyphd .get (key )
@@ -1449,14 +1455,15 @@ class Char(Node):
1449
1455
from width) must be converted into a :class:`Kern` node when the
1450
1456
:class:`Char` is added to its parent :class:`Hlist`.
1451
1457
"""
1452
- def __init__ (self , c , state ):
1458
+ def __init__ (self , c , state , math = True ):
1453
1459
Node .__init__ (self )
1454
1460
self .c = c
1455
1461
self .font_output = state .font_output
1456
1462
self .font = state .font
1457
1463
self .font_class = state .font_class
1458
1464
self .fontsize = state .fontsize
1459
1465
self .dpi = state .dpi
1466
+ self .math = math
1460
1467
# The real width, height and depth will be set during the
1461
1468
# pack phase, after we know the real fontsize
1462
1469
self ._update_metrics ()
@@ -1466,7 +1473,7 @@ def __internal_repr__(self):
1466
1473
1467
1474
def _update_metrics (self ):
1468
1475
metrics = self ._metrics = self .font_output .get_metrics (
1469
- self .font , self .font_class , self .c , self .fontsize , self .dpi )
1476
+ self .font , self .font_class , self .c , self .fontsize , self .dpi , self . math )
1470
1477
if self .c == ' ' :
1471
1478
self .width = metrics .advance
1472
1479
else :
@@ -2580,7 +2587,7 @@ def math(self, s, loc, toks):
2580
2587
def non_math (self , s , loc , toks ):
2581
2588
#~ print "non_math", toks
2582
2589
s = toks [0 ].replace (r'\$' , '$' )
2583
- symbols = [Char (c , self .get_state ()) for c in s ]
2590
+ symbols = [Char (c , self .get_state (), math = False ) for c in s ]
2584
2591
hlist = Hlist (symbols )
2585
2592
# We're going into math now, so set font to 'it'
2586
2593
self .push_state ()
0 commit comments