Skip to content

Commit 7fdae0a

Browse files
First pass at PS backend.
1 parent 7f4f644 commit 7fdae0a

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
A PostScript backend, which can produce both PostScript .ps and .eps
33
"""
44

5+
# PY3KTODO: Get rid of "print >>fh" syntax
6+
57
from __future__ import division
68
import glob, math, os, shutil, sys, time
79
def _fn_name(): return sys._getframe(1).f_code.co_name
10+
import io
811

912
try:
1013
from hashlib import md5
1114
except ImportError:
1215
from md5 import md5 #Deprecated in 2.5
1316

1417
from tempfile import mkstemp
15-
from cStringIO import StringIO
1618
from matplotlib import verbose, __version__, rcParams
1719
from matplotlib._pylab_helpers import Gcf
1820
from matplotlib.afm import AFM
@@ -322,7 +324,7 @@ def _get_font_afm(self, prop):
322324
"Helvetica", fontext='afm', directory=self._afm_font_dir)
323325
font = self.afmfontd.get(fname)
324326
if font is None:
325-
font = AFM(file(fname))
327+
font = AFM(open(fname, 'rb'))
326328
self.afmfontd[fname] = font
327329
self.afmfontd[key] = font
328330
return font
@@ -945,7 +947,8 @@ def _print_figure(self, outfile, format, dpi=72, facecolor='w', edgecolor='w',
945947
raise ValueError("outfile must be a path or a file-like object")
946948

947949
fd, tmpfile = mkstemp()
948-
fh = os.fdopen(fd, 'w')
950+
raw_fh = os.fdopen(fd, 'wb')
951+
fh = io.TextIOWrapper(raw_fh, encoding="ascii")
949952

950953
# find the appropriate papertype
951954
width, height = self.figure.get_size_inches()
@@ -998,7 +1001,7 @@ def write(self, *kl, **kwargs):
9981001

9991002
self._pswriter = NullWriter()
10001003
else:
1001-
self._pswriter = StringIO()
1004+
self._pswriter = io.StringIO()
10021005

10031006

10041007
# mixed mode rendering
@@ -1064,7 +1067,8 @@ def write(self, *kl, **kwargs):
10641067
if is_opentype_cff_font(font_filename):
10651068
raise RuntimeError("OpenType CFF fonts can not be saved using the internal Postscript backend at this time.\nConsider using the Cairo backend.")
10661069
else:
1067-
convert_ttf_to_ps(font_filename, fh, fonttype, glyph_ids)
1070+
fh.flush()
1071+
convert_ttf_to_ps(font_filename, raw_fh, fonttype, glyph_ids)
10681072
print >>fh, "end"
10691073
print >>fh, "%%EndProlog"
10701074

@@ -1091,7 +1095,7 @@ def write(self, *kl, **kwargs):
10911095
xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
10921096

10931097
if passed_in_file_object:
1094-
fh = open(tmpfile)
1098+
fh = open(tmpfile, 'rb')
10951099
print >>outfile, fh.read()
10961100
else:
10971101
open(outfile, 'w')
@@ -1112,7 +1116,7 @@ def _print_figure_tex(self, outfile, format, dpi, facecolor, edgecolor,
11121116

11131117
# write to a temp file, we'll move it to outfile when done
11141118
fd, tmpfile = mkstemp()
1115-
fh = os.fdopen(fd, 'w')
1119+
fh = io.fdopen(fd, 'w', encoding='ascii')
11161120

11171121
self.figure.dpi = 72 # ignore the dpi kwarg
11181122
width, height = self.figure.get_size_inches()
@@ -1239,10 +1243,10 @@ def write(self, *kl, **kwargs):
12391243
rotated=psfrag_rotated)
12401244

12411245
if isinstance(outfile, file):
1242-
fh = file(tmpfile)
1243-
print >>outfile, fh.read()
1246+
fh = open(tmpfile, 'rb')
1247+
outfile.write(fh.read())
12441248
else:
1245-
open(outfile, 'w')
1249+
open(outfile, 'wb')
12461250
mode = os.stat(outfile).st_mode
12471251
shutil.move(tmpfile, outfile)
12481252
os.chmod(outfile, mode)
@@ -1261,7 +1265,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
12611265
shutil.move(tmpfile, epsfile)
12621266
latexfile = tmpfile+'.tex'
12631267
outfile = tmpfile+'.output'
1264-
latexh = file(latexfile, 'w')
1268+
latexh = io.open(latexfile, 'w', encoding='ascii')
12651269
dvifile = tmpfile+'.dvi'
12661270
psfile = tmpfile+'.ps'
12671271

@@ -1316,7 +1320,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
13161320
%(precmd, tmpdir, latexfile, outfile)
13171321
verbose.report(command, 'debug')
13181322
exit_status = os.system(command)
1319-
fh = file(outfile)
1323+
fh = io.open(outfile, 'rb')
13201324
if exit_status:
13211325
raise RuntimeError('LaTeX was not able to process your file:\
13221326
\nHere is the full report generated by LaTeX: \n\n%s'% fh.read())
@@ -1328,7 +1332,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
13281332
os.path.split(psfile)[-1], os.path.split(dvifile)[-1], outfile)
13291333
verbose.report(command, 'debug')
13301334
exit_status = os.system(command)
1331-
fh = file(outfile)
1335+
fh = io.open(outfile, 'rb')
13321336
if exit_status: raise RuntimeError('dvips was not able to \
13331337
process the following file:\n%s\nHere is the full report generated by dvips: \
13341338
\n\n'% dvifile + fh.read())
@@ -1376,7 +1380,7 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
13761380
"%s" > "%s"'% (gs_exe, dpi, paper_option, psfile, tmpfile, outfile)
13771381
verbose.report(command, 'debug')
13781382
exit_status = os.system(command)
1379-
fh = file(outfile)
1383+
fh = io.open(outfile, 'rb')
13801384
if exit_status: raise RuntimeError('ghostscript was not able to process \
13811385
your image.\nHere is the full report generated by ghostscript:\n\n' + fh.read())
13821386
else: verbose.report(fh.read(), 'debug')
@@ -1406,7 +1410,7 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
14061410
if sys.platform == 'win32': command = command.replace('=', '#')
14071411
verbose.report(command, 'debug')
14081412
exit_status = os.system(command)
1409-
fh = file(outfile)
1413+
fh = io.open(outfile, 'rb')
14101414
if exit_status: raise RuntimeError('ps2pdf was not able to process your \
14111415
image.\n\Here is the report generated by ghostscript:\n\n' + fh.read())
14121416
else: verbose.report(fh.read(), 'debug')
@@ -1416,7 +1420,7 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
14161420
(pdffile, psfile, outfile)
14171421
verbose.report(command, 'debug')
14181422
exit_status = os.system(command)
1419-
fh = file(outfile)
1423+
fh = io.open(outfile, 'rb')
14201424
if exit_status: raise RuntimeError('pdftops was not able to process your \
14211425
image.\nHere is the full report generated by pdftops: \n\n' + fh.read())
14221426
else: verbose.report(fh.read(), 'debug')
@@ -1499,9 +1503,9 @@ def pstoeps(tmpfile, bbox, rotated=False):
14991503
bbox_info, rotate = get_bbox_header(bbox, rotated=rotated)
15001504

15011505
epsfile = tmpfile + '.eps'
1502-
epsh = file(epsfile, 'w')
1506+
epsh = io.open(epsfile, 'w', encoding='ascii')
15031507

1504-
tmph = file(tmpfile)
1508+
tmph = io.open(tmpfile, 'r', encoding='ascii')
15051509
line = tmph.readline()
15061510
# Modify the header:
15071511
while line:

src/_ttconv.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ class PythonFileWriter : public TTStreamWriter
4848
PyObject* result = NULL;
4949
if (_write_method)
5050
{
51+
#if PY3K
52+
result = PyObject_CallFunction(_write_method, (char *)"y", a);
53+
#else
5154
result = PyObject_CallFunction(_write_method, (char *)"s", a);
55+
#endif
5256
if (! result)
5357
{
5458
throw PythonExceptionOccurred();

0 commit comments

Comments
 (0)