8
8
def _fn_name (): return sys ._getframe (1 ).f_code .co_name
9
9
10
10
from cStringIO import StringIO
11
- from matplotlib import verbose , __version__ , rcParams
11
+ from matplotlib import verbose , __version__ , rcParams , get_data_path
12
12
from matplotlib ._pylab_helpers import Gcf
13
13
import matplotlib .agg as agg
14
14
from matplotlib .afm import AFM
@@ -22,11 +22,10 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
22
22
from matplotlib .ft2font import FT2Font , KERNING_UNFITTED , KERNING_DEFAULT , KERNING_UNSCALED
23
23
from matplotlib .mathtext import math_parse_s_ps , bakoma_fonts
24
24
from matplotlib .text import Text
25
+ from matplotlib .texmanager import TexManager
25
26
26
27
from matplotlib .transforms import get_vec6_scales
27
28
28
- from matplotlib import get_data_path
29
-
30
29
from matplotlib .numerix import fromstring , UInt8 , Float32 , equal , alltrue , \
31
30
nonzero , take , where , ones , put
32
31
import binascii
@@ -123,6 +122,10 @@ def __init__(self, width, height, pswriter):
123
122
self .width = width
124
123
self .height = height
125
124
self ._pswriter = pswriter
125
+ if rcParams ['text.usetex' ]:
126
+ self .textcnt = 0
127
+ self .psfrag = []
128
+ self .texmanager = TexManager ()
126
129
127
130
# current renderer state (None=uninitialised)
128
131
self .color = None
@@ -189,6 +192,13 @@ def get_text_width_height(self, s, prop, ismath):
189
192
with FontPropertry prop
190
193
191
194
"""
195
+ if rcParams ['text.usetex' ]:
196
+ fontsize = prop .get_size_in_points ()
197
+ l ,b ,r ,t = self .texmanager .get_ps_bbox (s )
198
+ w = (r - l )* fontsize / 10.
199
+ h = (t - b )* fontsize / 10.
200
+ #print s, w, h
201
+ return w , h
192
202
193
203
if rcParams ['ps.useafm' ]:
194
204
if ismath : s = s [1 :- 1 ]
@@ -211,7 +221,7 @@ def get_text_width_height(self, s, prop, ismath):
211
221
w , h = font .get_width_height ()
212
222
w /= 64.0 # convert from subpixels
213
223
h /= 64.0
214
- print s , w , h
224
+ # print s, w, h
215
225
return w , h
216
226
217
227
def flipy (self ):
@@ -552,6 +562,29 @@ def draw_rectangle(self, gc, rgbFace, x, y, width, height):
552
562
ps = '%s box' % _nums_to_str (width , height , x , y )
553
563
self ._draw_ps (ps , gc , rgbFace , "rectangle" )
554
564
565
+ def draw_tex (self , gc , x , y , s , prop , angle , ismath = 'TeX!' ):
566
+ """
567
+ draw a Text instance
568
+ """
569
+ w , h = self .get_text_width_height (s , prop , ismath )
570
+ fontsize = prop .get_size_in_points ()
571
+ corr = 0 #w/2*(fontsize-10)/10
572
+ pos = _nums_to_str (x - corr , y )
573
+ thetext = 'psmarker%d' % self .textcnt
574
+ scale = float (fontsize / 10.0 )
575
+ color = '%1.3f,%1.3f,%1.3f' % gc .get_rgb ()
576
+ tex = r'\color[rgb]{%s} %s' % (color , s )
577
+ self .psfrag .append (r'\psfrag{%s}[bl][bl][%f][%f]{%s}' % (thetext , scale , angle , tex ))
578
+ ps = """\
579
+ gsave
580
+ %(pos)s moveto
581
+ (%(thetext)s)
582
+ show
583
+ grestore
584
+ """ % locals ()
585
+
586
+ self ._pswriter .write (ps )
587
+ self .textcnt += 1
555
588
556
589
def draw_text (self , gc , x , y , s , prop , angle , ismath ):
557
590
"""
@@ -876,9 +909,21 @@ def print_figure(self, outfile, dpi=72,
876
909
title = None
877
910
else :
878
911
basename , ext = os .path .splitext (outfile )
879
- if not ext : outfile += '.ps'
880
- isEPSF = ext .lower ().startswith ('.ep' )
881
- fh = file (outfile , 'w' )
912
+ if not ext :
913
+ if rcParams ['text.usetex' ]:
914
+ ext = '.eps'
915
+ outfile += ext
916
+ else :
917
+ ext = '.ps'
918
+ outfile += ext
919
+ if rcParams ['text.usetex' ]:
920
+ epsfile = basename + '.eps'
921
+ psfile = basename + '.ps'
922
+ texfile = basename + '.tex'
923
+ latexh = file (texfile , 'w' )
924
+ fh = file (epsfile , 'w' )
925
+ else : fh = file (outfile , 'w' )
926
+ isEPSF = ext .lower ().startswith ('.ep' ) or rcParams ['text.usetex' ]
882
927
needsClose = True
883
928
title = outfile
884
929
@@ -942,26 +987,29 @@ def print_figure(self, outfile, dpi=72,
942
987
if not isEPSF : print >> fh , "%%Pages: 1"
943
988
print >> fh , "%%EndComments"
944
989
945
- type42 = _type42 + [os .path .join (self .basepath , name ) + '.ttf' \
946
- for name in bakoma_fonts ]
947
- print >> fh , "%%BeginProlog"
948
990
Ndict = len (psDefs )
949
- if not rcParams ['ps.useafm' ]:
950
- Ndict += len (type42 )
991
+ print >> fh , "%%BeginProlog"
992
+ if not rcParams ['text.usetex' ]:
993
+ type42 = _type42 + [os .path .join (self .basepath , name ) + '.ttf' \
994
+ for name in bakoma_fonts ]
995
+ if not rcParams ['ps.useafm' ]:
996
+ Ndict += len (type42 )
997
+
951
998
print >> fh , "/mpldict %d dict def" % Ndict
952
999
print >> fh , "mpldict begin"
953
1000
954
1001
for d in psDefs :
955
1002
d = d .strip ()
956
1003
for l in d .split ('\n ' ):
957
1004
print >> fh , l .strip ()
958
- if not rcParams ['ps.useafm' ]:
959
- for font in type42 :
960
- print >> fh , "%%BeginFont: " + FT2Font (str (font )).postscript_name
961
- print >> fh , encodeTTFasPS (font )
962
- print >> fh , "%%EndFont"
963
-
964
- print >> fh , "%%EndProlog"
1005
+ if not rcParams ['text.usetex' ]:
1006
+ if not rcParams ['ps.useafm' ]:
1007
+ for font in type42 :
1008
+ print >> fh , "%%BeginFont: " + FT2Font (str (font )).postscript_name
1009
+ print >> fh , encodeTTFasPS (font )
1010
+ print >> fh , "%%EndFont"
1011
+
1012
+ print >> fh , "%%EndProlog"
965
1013
966
1014
if not isEPSF : print >> fh , "%%Page: 1 1"
967
1015
print >> fh , "mpldict begin"
@@ -981,6 +1029,48 @@ def print_figure(self, outfile, dpi=72,
981
1029
982
1030
if not isEPSF : print >> fh , "%%EOF"
983
1031
if needsClose : fh .close ()
1032
+
1033
+ if rcParams ['text.usetex' ]:
1034
+ if defaultPaperType in ['a4' ,'a5' ,'b5' ,'letter' ,'legal' ,'executive' ]:
1035
+ latexPaperType = defaultPaperType + 'paper'
1036
+ else :
1037
+ verbose .report ('"%s" is not a valid LaTeX papertype.' % defaultPaperType + \
1038
+ 'Defaulting to letter.' )
1039
+ latexPaperType = 'letterpaper'
1040
+ print >> latexh , r"""\documentclass[%s]{article}
1041
+ \usepackage{psfrag}
1042
+ \usepackage{type1cm}
1043
+ \usepackage[dvips]{graphicx}
1044
+ \usepackage{color}
1045
+ \pagestyle{empty}
1046
+ \begin{document}
1047
+ \begin{figure}[th!]
1048
+ \begin{center}
1049
+ %s
1050
+ \includegraphics{%s}
1051
+ \end{center}
1052
+ \end{figure}
1053
+ \end{document}
1054
+ """ % (latexPaperType , '\n ' .join (renderer .psfrag ), epsfile )
1055
+
1056
+ latexh .close ()
1057
+
1058
+ command = 'latex %s' % texfile
1059
+ os .system (command )
1060
+ command = 'dvips -o %s %s' % (psfile , basename + '.dvi' )
1061
+ os .system (command )
1062
+ if ext .startswith ('.ep' ):
1063
+ command = 'ps2epsi %s %s' % (psfile , outfile )
1064
+ msg = os .system (command )
1065
+ if msg == 32512 : raise 'ps2epsi: command not found! Please install Ghostscript.'
1066
+ os .remove (psfile )
1067
+ os .remove (texfile )
1068
+ os .remove (basename + '.dvi' )
1069
+ try : os .remove (basename + '.log' )
1070
+ except OSError : pass
1071
+ try : os .remove (basename + '.aux' )
1072
+ except OSError : pass
1073
+
984
1074
985
1075
class FigureManagerPS (FigureManagerBase ):
986
1076
pass
0 commit comments