|
From: <lee...@us...> - 2010-06-05 18:32:18
|
Revision: 8385
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8385&view=rev
Author: leejjoon
Date: 2010-06-05 18:32:11 +0000 (Sat, 05 Jun 2010)
Log Message:
-----------
eps output restores the correct bounding box when convert_psfrags creates a ps in landscape mode
Modified Paths:
--------------
trunk/matplotlib/lib/matplotlib/backends/backend_ps.py
Modified: trunk/matplotlib/lib/matplotlib/backends/backend_ps.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/backends/backend_ps.py 2010-06-05 18:32:06 UTC (rev 8384)
+++ trunk/matplotlib/lib/matplotlib/backends/backend_ps.py 2010-06-05 18:32:11 UTC (rev 8385)
@@ -1284,17 +1284,21 @@
font_preamble = texmanager.get_font_preamble()
custom_preamble = texmanager.get_custom_preamble()
- convert_psfrags(tmpfile, ps_renderer.psfrag, font_preamble,
- custom_preamble, paperWidth, paperHeight,
- orientation)
+ psfrag_rotated = convert_psfrags(tmpfile, ps_renderer.psfrag,
+ font_preamble,
+ custom_preamble, paperWidth, paperHeight,
+ orientation)
if rcParams['ps.usedistiller'] == 'ghostscript':
- gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
+ gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox,
+ rotated=psfrag_rotated)
elif rcParams['ps.usedistiller'] == 'xpdf':
- xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
+ xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox,
+ rotated=psfrag_rotated)
elif rcParams['text.usetex']:
if False: pass # for debugging
- else: gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
+ else: gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox,
+ rotated=psfrag_rotated)
if isinstance(outfile, file):
fh = file(tmpfile)
@@ -1395,12 +1399,26 @@
os.remove(outfile)
os.remove(epsfile)
shutil.move(psfile, tmpfile)
+
+ # check if the dvips created a ps in landscape paper. Somehow,
+ # above latex+dvips results in a ps file in a landscape mode for a
+ # certain figure sizes (e.g., 8.3in,5.8in which is a5). And the
+ # bounding box of the final output got messed up. We check see if
+ # the generated ps file is in landscape and return this
+ # information. The return value is used in pstoeps step to recover
+ # the correct bounding box. 2010-06-05 JJL
+ if "Landscape" in open(tmpfile).read(1000):
+ psfrag_rotated = True
+ else:
+ psfrag_rotated = False
+
if not debugPS:
for fname in glob.glob(tmpfile+'.*'):
os.remove(fname)
+ return psfrag_rotated
-def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None):
+def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
"""
Use ghostscript's pswrite or epswrite device to distill a file.
This yields smaller files without illegal encapsulated postscript
@@ -1434,10 +1452,10 @@
# the input to eps format, but also restores the original bbox.
if eps:
- pstoeps(tmpfile, bbox)
+ pstoeps(tmpfile, bbox, rotated=rotated)
-def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None):
+def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
"""
Use ghostscript's ps2pdf and xpdf's/poppler's pdftops to distill a file.
This yields smaller files without illegal encapsulated postscript
@@ -1482,20 +1500,26 @@
# 8.61). Thus, the original bbox need to be resotred.
if eps:
- pstoeps(tmpfile, bbox)
+ pstoeps(tmpfile, bbox, rotated)
for fname in glob.glob(tmpfile+'.*'):
os.remove(fname)
-def get_bbox_header(l, b, r, t):
+def get_bbox_header(lbrt, rotated=False):
"""
- return a postscript header stringfor the given bbox (l, b, r, t)
+ return a postscript header stringfor the given bbox lbrt=(l, b, r, t).
+ Optionally, return rotate command.
"""
+ l, b, r, t = lbrt
+ if rotated:
+ rotate = "%.2f %.2f translate\n90 rotate" % (l+r, 0)
+ else:
+ rotate = ""
bbox_info = '%%%%BoundingBox: %d %d %d %d' % (l, b, np.ceil(r), np.ceil(t))
hires_bbox_info = '%%%%HiResBoundingBox: %.6f %.6f %.6f %.6f' % (l, b, r, t)
- return '\n'.join([bbox_info, hires_bbox_info])
+ return '\n'.join([bbox_info, hires_bbox_info]), rotate
# get_bbox is deprecated. I don't see any reason to use ghostscript to
@@ -1543,12 +1567,14 @@
return '\n'.join([bbox_info, hires_bbox_info])
-def pstoeps(tmpfile, bbox):
+def pstoeps(tmpfile, bbox, rotated=False):
"""
Convert the postscript to encapsulated postscript.
"""
- bbox_info = get_bbox_header(*bbox)
+ # if rotated==True, the output eps file need to be rotated
+ bbox_info, rotate = get_bbox_header(bbox, rotated=rotated)
+
epsfile = tmpfile + '.eps'
epsh = file(epsfile, 'w')
@@ -1570,9 +1596,12 @@
print >>epsh, '/setpagedevice {pop} def'
print >>epsh, '%%EndProlog'
print >>epsh, '%%Page 1 1'
+ if rotate:
+ print >>epsh, rotate
break
elif line.startswith('%%Bound') \
or line.startswith('%%HiResBound') \
+ or line.startswith('%%DocumentMedia') \
or line.startswith('%%Pages'):
pass
else:
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|