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

Skip to content

Commit a0f2974

Browse files
committed
matplotlibrc text.usetex now checks for dependencies during validation.
For usetex option, backend_ps does all the work in the os's temp directory. matplotlibrc ps.usedistiller can now be set to None, ghostscript, or xpdf. xpdf produces high quality output and small files, but needs testing. Dependencies are checked during the validation process. svn path=/trunk/matplotlib/; revision=1940
1 parent 1d76f5f commit a0f2974

4 files changed

Lines changed: 138 additions & 28 deletions

File tree

CHANGELOG

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
2006-1-10 Added xpdf distiller option. matplotlibrc ps.usedistiller can now be
2+
none, false, ghostscript, or xpdf. Validation checks for
3+
dependencies. This needs testing, but the xpdf option should produce
4+
the highest-quality output and small file sizes - DSD
5+
6+
2006-01-10 For the usetex option, backend_ps now does all the LaTeX work in the
7+
os's temp directory - DSD
8+
9+
2006-1-10 Added checks for usetex dependencies. - DSD
10+
111
=======================================================================
212
2006-1-9 Released 0.86
313

lib/matplotlib/__init__.py

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
__revision__ = '$Revision$'
148148
__date__ = '$Date$'
149149

150-
import sys, os, warnings, shutil
150+
import sys, os, warnings, shutil, md5
151151
import distutils.sysconfig
152152

153153
if not hasattr(sys, 'argv'): # for modpython
@@ -429,6 +429,50 @@ def _get_data_path():
429429
get_data_path = verbose.wrap('matplotlib data path %s', _get_data_path, always=False)
430430

431431

432+
def checkdep_dvipng():
433+
stdin, stdout = os.popen4('dvipng -v')
434+
if 'dvipng' in stdout.read(): return True
435+
else:
436+
verbose.report('dvipng not found!', 'helpful')
437+
return False
438+
439+
def checkdep_ghostscript():
440+
flag = False
441+
if sys.platform == 'win32':
442+
stdin, stdout = os.popen4('gswin32c -v')
443+
if 'Ghostscript' in stdout.read(): flag = True
444+
else:
445+
stdin, stdout = os.popen4('gs -v')
446+
if 'Ghostscript' in stdout.read(): flag = True
447+
if flag: return True
448+
else:
449+
verbose.report('Ghostscript not found!\n\
450+
Please install a recent version of ghostscript \n\
451+
(gnu-ghostscript-8.16 or later suggested)\n', 'helpful')
452+
return False
453+
454+
def checkdep_ps2eps():
455+
stdin, stdout = os.popen4('ps2eps -v')
456+
if 'ps2eps' in stdout.read(): return True
457+
else:
458+
verbose.report('ps2eps not found!', 'helpful')
459+
return False
460+
461+
def checkdep_tex():
462+
stdin, stdout = os.popen4('tex -v')
463+
if 'TeX' in stdout.read(): return True
464+
else:
465+
verbose.report('latex not found!\
466+
Please install the appropriate package for your platform.\n', 'helpful')
467+
return False
468+
469+
def checkdep_xpdf():
470+
stdin, stdout = os.popen4('xpdf -v')
471+
if 'xpdf' in stdout.read(): return True
472+
else:
473+
verbose.report('xpdf not found!', 'helpful')
474+
return False
475+
432476

433477
def validate_path_exists(s):
434478
'If s is a path, return s, else False'
@@ -581,8 +625,37 @@ def validate_verbose_fileo(s):
581625
'c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6'
582626
], ignorecase=True)
583627

628+
def validate_ps_distiller(s):
629+
s = s.lower()
630+
if s == 'none':
631+
return None
632+
elif s == 'false':
633+
return False
634+
elif s == 'ghostscript':
635+
if checkdep_ghostscript(): return s.lower()
636+
else: raise 'DependencyError', 'matplotlibrc ps.usedistiller can not \
637+
be set to ghostscript unless ghostscript is available on your system'
638+
elif s == 'xpdf':
639+
if checkdep_ghostscript() and checkdep_xpdf() and checkdep_ps2eps():
640+
return s.lower()
641+
else:
642+
raise 'DependencyError', 'matplotlibrc ps.usedistiller can not \
643+
be set to xpdf unless ghostscript, xpdf and ps2eps are available on your system'
644+
else:
645+
raise ValueError('matplotlibrc ps.usedistiller must either be none, ghostscript or xpdf')
646+
584647
validate_tex_engine = ValidateInStrings(['tex', 'latex'], ignorecase=True)
585648

649+
def validate_usetex(s):
650+
bl = validate_bool(s)
651+
if bl:
652+
if checkdep_tex() and checkdep_dvipng() and checkdep_ghostscript():
653+
return bl
654+
else:
655+
raise 'DependencyError', 'matplotlibrc tex.usetex can not be set to \
656+
True unless LaTeX, dvipng and ghostscript are available on your system.'
657+
else: return bl
658+
586659
validate_joinstyle = ValidateInStrings(['miter', 'round', 'bevel'], ignorecase=True)
587660

588661
validate_capstyle = ValidateInStrings(['butt', 'round', 'projecting'], ignorecase=True)
@@ -615,7 +688,6 @@ def __call__(self, s):
615688

616689

617690

618-
619691
# a map from key -> value, converter
620692
defaultParams = {
621693
'backend' : ['GTK', str],
@@ -667,7 +739,7 @@ def __call__(self, s):
667739

668740
# text props
669741
'text.color' : ['k', validate_color], # black
670-
'text.usetex' : [False, validate_bool],
742+
'text.usetex' : [False, validate_usetex],
671743
'text.tex.engine' : ['tex', validate_tex_engine], # TeX or LaTeX
672744
'text.fontstyle' : ['normal', str],
673745
'text.fontangle' : ['normal', str],
@@ -740,7 +812,7 @@ def __call__(self, s):
740812
'tk.pythoninspect' : [ False, validate_bool], # Set PYTHONINSPECT
741813
'ps.papersize' : [ 'letter', validate_ps_papersize], # Set the papersize/type
742814
'ps.useafm' : [ False, validate_bool], # Set PYTHONINSPECT
743-
'ps.usedistiller' : [ False, validate_bool], # use ghostscript to distill ps output
815+
'ps.usedistiller' : [ False, validate_ps_distiller], # use ghostscript or xpdf to distill ps output
744816
'ps.distiller.res' : [6000, validate_int], # dpi
745817
'plugins.directory' : ['.matplotlib_plugins', str], # where plugin directory is locate
746818

lib/matplotlib/backends/backend_ps.py

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from __future__ import division
77
import glob, math, md5, os, shutil, sys, time
88
def _fn_name(): return sys._getframe(1).f_code.co_name
9-
9+
10+
from tempfile import gettempdir
1011
from cStringIO import StringIO
1112
from matplotlib import verbose, __version__, rcParams, get_data_path
1213
from matplotlib._pylab_helpers import Gcf
@@ -993,8 +994,9 @@ def print_figure(self, outfile, dpi=72,
993994
# need to make some temporary files so latex can run without
994995
# writing over something important.
995996
m = md5.md5(outfile)
996-
tmpname = m.hexdigest()
997-
997+
tempdir = gettempdir()
998+
os.environ['TEXMFOUTPUT'] = tempdir
999+
tmpname = os.path.join(tempdir, m.hexdigest())
9981000
epsfile = tmpname + '.eps'
9991001
psfile = tmpname + '.ps'
10001002
texfile = tmpname + '.tex'
@@ -1140,42 +1142,64 @@ def print_figure(self, outfile, dpi=72,
11401142
"""% (fontpackage, pw, ph, pw-2, ph-2, pw, ph, '\n'.join(renderer.psfrag), epsfile)
11411143

11421144
latexh.close()
1143-
1145+
curdir = os.getcwd()
1146+
os.chdir(tempdir)
11441147
command = 'latex -interaction=nonstopmode "%s"' % texfile
1145-
11461148
verbose.report(command, 'debug-annoying')
11471149
stdin, stdout, stderr = os.popen3(command)
11481150
verbose.report(stdout.read(), 'debug-annoying')
11491151
verbose.report(stderr.read(), 'helpful')
1152+
os.chdir(curdir)
11501153
command = 'dvips -R -T %fin,%fin -o "%s" "%s"' % (pw, ph, psfile, dvifile)
11511154
verbose.report(command, 'debug-annoying')
11521155
stdin, stdout, stderr = os.popen3(command)
11531156
verbose.report(stdout.read(), 'debug-annoying')
11541157
verbose.report(stderr.read(), 'helpful')
11551158
os.remove(epsfile)
1156-
if ext.startswith('.ep'):
1157-
dpi = rcParams['ps.distiller.res']
1158-
1159-
if sys.platform == 'win32':
1160-
command = 'gswin32c -dBATCH -dNOPAUSE -dSAFER -r%d \
1161-
-sDEVICE=epswrite -dLanguageLevel=2 -dEPSFitPage \
1162-
-sOutputFile="%s" "%s"'% (dpi, epsfile, psfile)
1159+
1160+
if rcParams['ps.usedistiller'] == 'xpdf':
1161+
pdffile = tmpname + '.pdf'
1162+
if ext.startswith('.ep'):
1163+
command = 'ps2pdf "%s" "%s"'% (psfile, pdffile)
1164+
os.system(command)
1165+
command = 'pdftops -level2 "%s" "%s"'% (pdffile, psfile)
1166+
os.system(command)
1167+
os.remove(pdffile)
1168+
command = '/usr/local/bin/ps2eps -l "%s"'% psfile
1169+
stdin, stderr = os.popen4(command)
1170+
verbose.report(stderr.read(), 'helpful')
1171+
shutil.move(epsfile, outfile)
11631172
else:
1164-
command = 'gs -dBATCH -dNOPAUSE -dSAFER -r%d \
1165-
-sDEVICE=epswrite -dLanguageLevel=2 -dEPSFitPage \
1166-
-sOutputFile="%s" "%s"'% (dpi, epsfile, psfile)
1167-
1168-
verbose.report(command, 'debug-annoying')
1169-
stdin, stdout, stderr = os.popen3(command)
1170-
verbose.report(stdout.read(), 'debug-annoying')
1171-
verbose.report(stderr.read(), 'helpful')
1172-
shutil.move(epsfile, outfile)
1173-
else: shutil.move(psfile, outfile)
1173+
command = 'ps2pdf "%s" "%s"'% (psfile, pdffile)
1174+
stdin, stderr = os.popen4(command)
1175+
verbose.report(stderr.read(), 'helpful')
1176+
os.remove(psfile)
1177+
command = 'pdftops -paperw %d -paperh %d -level2 "%s" "%s"'% \
1178+
(int(pw*72), int(ph*72), pdffile, psfile)
1179+
os.system(command)
1180+
shutil.move(psfile, outfile)
1181+
else:
1182+
if ext.startswith('.ep'):
1183+
dpi = rcParams['ps.distiller.res']
1184+
if sys.platform == 'win32':
1185+
command = 'gswin32c -dBATCH -dNOPAUSE -dSAFER -r%d \
1186+
-sDEVICE=epswrite -dLanguageLevel=2 -dEPSFitPage \
1187+
-sOutputFile="%s" "%s"'% (dpi, epsfile, psfile)
1188+
else:
1189+
command = 'gs -dBATCH -dNOPAUSE -dSAFER -r%d \
1190+
-sDEVICE=epswrite -dLanguageLevel=2 -dEPSFitPage \
1191+
-sOutputFile="%s" "%s"'% (dpi, epsfile, psfile)
1192+
verbose.report(command, 'debug-annoying')
1193+
stdin, stdout, stderr = os.popen3(command)
1194+
verbose.report(stdout.read(), 'debug-annoying')
1195+
verbose.report(stderr.read(), 'helpful')
1196+
shutil.move(epsfile, outfile)
1197+
else: shutil.move(psfile, outfile)
11741198

11751199
for fname in glob.glob(tmpname+'.*'):
11761200
os.remove(fname)
11771201

1178-
if rcParams['ps.usedistiller']:
1202+
if rcParams['ps.usedistiller'] == 'ghostscript':
11791203
dpi = rcParams['ps.distiller.res']
11801204
m = md5.md5(outfile)
11811205
tmpfile = m.hexdigest()
@@ -1189,6 +1213,7 @@ def print_figure(self, outfile, dpi=72,
11891213
verbose.report(stderr.read(), 'helpful')
11901214
shutil.move(tmpfile, outfile)
11911215

1216+
11921217
class FigureManagerPS(FigureManagerBase):
11931218
pass
11941219

matplotlibrc.template

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ tk.pythoninspect : False # tk sets PYTHONINSEPCT
204204
# ps backend params
205205
ps.papersize : letter # executive, letter, legal, ledger, A0-A10, B0-B6, C0-C6
206206
ps.useafm : False # use of afm fonts -- breaks mathtext but results in small files
207-
ps.usedistiller : False # Experimental: use ghostscript to distill ps output - may yield smaller files
207+
ps.usedistiller : False # can be: None, ghostscript or xpdf
208+
# Experimental: may produce smaller files.
209+
# xpdf intended for production of publication quality files,
210+
# but requires ghostscript, xpdf and ps2eps
208211
ps.distiller.res : 6000 # dpi
209212

210213
# Set the verbose flags. This controls how much information

0 commit comments

Comments
 (0)