@@ -22,6 +22,7 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
22
22
from matplotlib .ft2font import FT2Font , KERNING_DEFAULT , LOAD_NO_HINTING
23
23
from matplotlib .ttconv import convert_ttf_to_ps
24
24
from matplotlib .mathtext import MathTextParser
25
+ from matplotlib ._mathtext_data import uni2type1
25
26
from matplotlib .text import Text
26
27
27
28
from matplotlib .transforms import get_vec6_scales
@@ -700,8 +701,10 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
700
701
elif ismath :
701
702
return self .draw_mathtext (gc , x , y , s , prop , angle )
702
703
704
+ elif isinstance (s , unicode ):
705
+ return self .draw_unicode (gc , x , y , s , prop , angle )
706
+
703
707
elif rcParams ['ps.useafm' ]:
704
- if ismath : s = s [1 :- 1 ]
705
708
font = self ._get_font_afm (prop )
706
709
707
710
l ,b ,w ,h = font .get_str_bbox (s )
@@ -735,8 +738,6 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
735
738
""" % locals ()
736
739
self ._draw_ps (ps , gc , None )
737
740
738
- elif isinstance (s , unicode ):
739
- return self .draw_unicode (gc , x , y , s , prop , angle )
740
741
else :
741
742
font = self ._get_font_ttf (prop )
742
743
font .set_text (s , 0 , flags = LOAD_NO_HINTING )
@@ -762,48 +763,90 @@ def draw_unicode(self, gc, x, y, s, prop, angle):
762
763
"""draw a unicode string. ps doesn't have unicode support, so
763
764
we have to do this the hard way
764
765
"""
766
+ if rcParams ['ps.useafm' ]:
767
+ self .set_color (* gc .get_rgb ())
765
768
766
- font = self ._get_font_ttf (prop )
767
-
768
- self .set_color (* gc .get_rgb ())
769
- self .set_font (font .get_sfnt ()[(1 ,0 ,0 ,6 )], prop .get_size_in_points ())
770
- self .track_characters (font , s )
771
-
772
- cmap = font .get_charmap ()
773
- lastgind = None
774
- #print 'text', s
775
- lines = []
776
- thisx , thisy = 0 ,0
777
- for c in s :
778
- ccode = ord (c )
779
- gind = cmap .get (ccode )
780
- if gind is None :
781
- ccode = ord ('?' )
782
- name = '.notdef'
783
- gind = 0
784
- else :
785
- name = font .get_glyph_name (gind )
786
- glyph = font .load_char (ccode , flags = LOAD_NO_HINTING )
787
-
788
- if lastgind is not None :
789
- kern = font .get_kerning (lastgind , gind , KERNING_DEFAULT )
790
- else :
791
- kern = 0
792
- lastgind = gind
793
- thisx += kern / 64.0
794
-
795
- lines .append ('%f %f m /%s glyphshow' % (thisx , thisy , name ))
796
- thisx += glyph .linearHoriAdvance / 65536.0
797
-
798
-
799
- thetext = '\n ' .join (lines )
800
- ps = """gsave
769
+ font = self ._get_font_afm (prop )
770
+ fontname = font .get_fontname ()
771
+ fontsize = prop .get_size_in_points ()
772
+ scale = 0.001 * fontsize
773
+
774
+ thisx , thisy = 0 , 0
775
+ last_name = None
776
+ lines = []
777
+ for c in s :
778
+ name = uni2type1 .get (ord (c ), 'question' )
779
+ try :
780
+ width = font .get_width_from_char_name (name )
781
+ except KeyError :
782
+ name = 'question'
783
+ width = font .get_width_char ('?' )
784
+ if last_name is not None :
785
+ kern = font .get_kern_dist_from_name (last_name , name )
786
+ else :
787
+ kern = 0
788
+ last_name = name
789
+ thisx += kern * scale
790
+
791
+ lines .append ('%f %f m /%s glyphshow' % (thisx , thisy , name ))
792
+
793
+ thisx += width * scale
794
+
795
+ thetext = "\n " .join (lines )
796
+ ps = """\
797
+ gsave
798
+ /%(fontname)s findfont
799
+ %(fontsize)s scalefont
800
+ setfont
801
801
%(x)f %(y)f translate
802
802
%(angle)f rotate
803
803
%(thetext)s
804
804
grestore
805
- """ % locals ()
806
- self ._pswriter .write (ps )
805
+ """ % locals ()
806
+ self ._pswriter .write (ps )
807
+
808
+ else :
809
+ font = self ._get_font_ttf (prop )
810
+
811
+ self .set_color (* gc .get_rgb ())
812
+ self .set_font (font .get_sfnt ()[(1 ,0 ,0 ,6 )], prop .get_size_in_points ())
813
+ self .track_characters (font , s )
814
+
815
+ cmap = font .get_charmap ()
816
+ lastgind = None
817
+ #print 'text', s
818
+ lines = []
819
+ thisx , thisy = 0 ,0
820
+ for c in s :
821
+ ccode = ord (c )
822
+ gind = cmap .get (ccode )
823
+ if gind is None :
824
+ ccode = ord ('?' )
825
+ name = '.notdef'
826
+ gind = 0
827
+ else :
828
+ name = font .get_glyph_name (gind )
829
+ glyph = font .load_char (ccode , flags = LOAD_NO_HINTING )
830
+
831
+ if lastgind is not None :
832
+ kern = font .get_kerning (lastgind , gind , KERNING_DEFAULT )
833
+ else :
834
+ kern = 0
835
+ lastgind = gind
836
+ thisx += kern / 64.0
837
+
838
+ lines .append ('%f %f m /%s glyphshow' % (thisx , thisy , name ))
839
+ thisx += glyph .linearHoriAdvance / 65536.0
840
+
841
+
842
+ thetext = '\n ' .join (lines )
843
+ ps = """gsave
844
+ %(x)f %(y)f translate
845
+ %(angle)f rotate
846
+ %(thetext)s
847
+ grestore
848
+ """ % locals ()
849
+ self ._pswriter .write (ps )
807
850
808
851
809
852
def draw_mathtext (self , gc ,
0 commit comments