From 5acabc0cbe17cf9ae31f39caf7faff592c416f82 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 12 Dec 2011 16:07:50 -0500 Subject: [PATCH] Fix usetex with .eps and .ps files in backend_ps.py --- lib/matplotlib/backends/backend_ps.py | 96 ++++++++++++++++----------- 1 file changed, 58 insertions(+), 38 deletions(-) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 19a1af80d23e..a4bcc2d16882 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -90,7 +90,11 @@ def gs_version(self): from subprocess import Popen, PIPE pipe = Popen(self.gs_exe + " --version", shell=True, stdout=PIPE).stdout - gs_version = tuple(map(int, pipe.read().strip().split("."))) + if sys.version_info[0] >= 3: + ver = pipe.read().decode('ascii') + else: + ver = pipe.read() + gs_version = tuple(map(int, ver.strip().split("."))) self._cached["gs_version"] = gs_version return gs_version @@ -1198,7 +1202,10 @@ def write(self, *kl, **kwargs): self._pswriter = NullWriter() else: - self._pswriter = StringIO() + if sys.version_info[0] >= 3: + self._pswriter = io.StringIO() + else: + self._pswriter = cStringIO.StringIO() # mixed mode rendering @@ -1219,7 +1226,11 @@ def write(self, *kl, **kwargs): # write to a temp file, we'll move it to outfile when done fd, tmpfile = mkstemp() - with io.fdopen(fd, 'w', encoding='ascii') as fh: + if sys.version_info[0] >= 3: + fh = io.open(fd, 'w', encoding='ascii') + else: + fh = io.open(fd, 'wb') + with fh: # write the Encapsulated PostScript headers print("%!PS-Adobe-3.0 EPSF-3.0", file=fh) if title: print("%%Title: "+title, file=fh) @@ -1298,7 +1309,15 @@ def write(self, *kl, **kwargs): else: gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox, rotated=psfrag_rotated) - if isinstance(outfile, file): + is_file = False + if sys.version_info[0] >= 3: + if isinstance(outfile, io.IOBase): + is_file = True + else: + if isinstance(outfile, file): + is_file = True + + if is_file: with open(tmpfile, 'rb') as fh: outfile.write(fh.read()) else: @@ -1355,12 +1374,12 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble, paperWidth, paperHeight, '\n'.join(psfrags), angle, os.path.split(epsfile)[-1]) - with io.open(latexfile, 'w', encoding='ascii') as latexh: + with io.open(latexfile, 'wb') as latexh: if rcParams['text.latex.unicode']: latexh.write(s.encode('utf8')) else: try: - latexh.write(s) + latexh.write(s.encode('ascii')) except UnicodeEncodeError: verbose.report("You are using unicode and latex, but have " "not enabled the matplotlib 'text.latex.unicode' " @@ -1375,7 +1394,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble, %(precmd, tmpdir, latexfile, outfile) verbose.report(command, 'debug') exit_status = os.system(command) - + with io.open(outfile, 'rb') as fh: if exit_status: raise RuntimeError('LaTeX was not able to process your file:\ @@ -1447,7 +1466,7 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False): verbose.report(command, 'debug') exit_status = os.system(command) - with io.open(outfile, 'rb'): + with io.open(outfile, 'rb') as fh: if exit_status: raise RuntimeError('ghostscript was not able to process \ your image.\nHere is the full report generated by ghostscript:\n\n' + fh.read()) @@ -1597,55 +1616,56 @@ def pstoeps(tmpfile, bbox=None, rotated=False): bbox_info, rotate = None, None epsfile = tmpfile + '.eps' - with io.open(epsfile, 'w', encoding='ascii') as epsh: - with io.open(tmpfile, 'r', encoding='ascii') as tmph: + with io.open(epsfile, 'wb') as epsh: + write = epsh.write + with io.open(tmpfile, 'rb') as tmph: line = tmph.readline() # Modify the header: while line: - if line.startswith('%!PS'): - print("%!PS-Adobe-3.0 EPSF-3.0", file=epsh) + if line.startswith(b'%!PS'): + write(b"%!PS-Adobe-3.0 EPSF-3.0\n") if bbox: - print(bbox_info, file=epsh) - elif line.startswith('%%EndComments'): - epsh.write(line) - print('%%BeginProlog', file=epsh) - print('save', file=epsh) - print('countdictstack', file=epsh) - print('mark', file=epsh) - print('newpath', file=epsh) - print('/showpage {} def', file=epsh) - print('/setpagedevice {pop} def', file=epsh) - print('%%EndProlog', file=epsh) - print('%%Page 1 1', file=epsh) + write(bbox_info.encode('ascii') + b'\n') + elif line.startswith(b'%%EndComments'): + write(line) + write(b'%%BeginProlog\n') + write(b'save\n') + write(b'countdictstack\n') + write(b'mark\n') + write(b'newpath\n') + write(b'/showpage {} def\n') + write(b'/setpagedevice {pop} def\n') + write(b'%%EndProlog\n') + write(b'%%Page 1 1\n') if rotate: - print(rotate, file=epsh) + write(rotate.encode('ascii') + b'\n') break - elif bbox and (line.startswith('%%Bound') \ - or line.startswith('%%HiResBound') \ - or line.startswith('%%DocumentMedia') \ - or line.startswith('%%Pages')): + elif bbox and (line.startswith(b'%%Bound') \ + or line.startswith(b'%%HiResBound') \ + or line.startswith(b'%%DocumentMedia') \ + or line.startswith(b'%%Pages')): pass else: - epsh.write(line) + write(line) line = tmph.readline() # Now rewrite the rest of the file, and modify the trailer. # This is done in a second loop such that the header of the embedded # eps file is not modified. line = tmph.readline() while line: - if line.startswith('%%Trailer'): - print('%%Trailer', file=epsh) - print('cleartomark', file=epsh) - print('countdictstack', file=epsh) - print('exch sub { end } repeat', file=epsh) - print('restore', file=epsh) + if line.startswith(b'%%Trailer'): + write(b'%%Trailer\n') + write(b'cleartomark\n') + write(b'countdictstack\n') + write(b'exch sub { end } repeat\n') + write(b'restore\n') if rcParams['ps.usedistiller'] == 'xpdf': # remove extraneous "end" operator: line = tmph.readline() - elif line.startswith('%%PageBoundingBox'): + elif line.startswith(b'%%PageBoundingBox'): pass else: - epsh.write(line) + write(line) line = tmph.readline() os.remove(tmpfile)