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

Skip to content

Commit e24bb60

Browse files
committed
eps output restores the correct bounding box when convert_psfrags creates a ps in landscape mode
svn path=/trunk/matplotlib/; revision=8385
1 parent eecc180 commit e24bb60

1 file changed

Lines changed: 44 additions & 15 deletions

File tree

lib/matplotlib/backends/backend_ps.py

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,17 +1284,21 @@ def write(self, *kl, **kwargs):
12841284
font_preamble = texmanager.get_font_preamble()
12851285
custom_preamble = texmanager.get_custom_preamble()
12861286

1287-
convert_psfrags(tmpfile, ps_renderer.psfrag, font_preamble,
1288-
custom_preamble, paperWidth, paperHeight,
1289-
orientation)
1287+
psfrag_rotated = convert_psfrags(tmpfile, ps_renderer.psfrag,
1288+
font_preamble,
1289+
custom_preamble, paperWidth, paperHeight,
1290+
orientation)
12901291

12911292
if rcParams['ps.usedistiller'] == 'ghostscript':
1292-
gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
1293+
gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox,
1294+
rotated=psfrag_rotated)
12931295
elif rcParams['ps.usedistiller'] == 'xpdf':
1294-
xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
1296+
xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox,
1297+
rotated=psfrag_rotated)
12951298
elif rcParams['text.usetex']:
12961299
if False: pass # for debugging
1297-
else: gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
1300+
else: gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox,
1301+
rotated=psfrag_rotated)
12981302

12991303
if isinstance(outfile, file):
13001304
fh = file(tmpfile)
@@ -1395,12 +1399,26 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
13951399
os.remove(outfile)
13961400
os.remove(epsfile)
13971401
shutil.move(psfile, tmpfile)
1402+
1403+
# check if the dvips created a ps in landscape paper. Somehow,
1404+
# above latex+dvips results in a ps file in a landscape mode for a
1405+
# certain figure sizes (e.g., 8.3in,5.8in which is a5). And the
1406+
# bounding box of the final output got messed up. We check see if
1407+
# the generated ps file is in landscape and return this
1408+
# information. The return value is used in pstoeps step to recover
1409+
# the correct bounding box. 2010-06-05 JJL
1410+
if "Landscape" in open(tmpfile).read(1000):
1411+
psfrag_rotated = True
1412+
else:
1413+
psfrag_rotated = False
1414+
13981415
if not debugPS:
13991416
for fname in glob.glob(tmpfile+'.*'):
14001417
os.remove(fname)
14011418

1419+
return psfrag_rotated
14021420

1403-
def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None):
1421+
def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
14041422
"""
14051423
Use ghostscript's pswrite or epswrite device to distill a file.
14061424
This yields smaller files without illegal encapsulated postscript
@@ -1434,10 +1452,10 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None):
14341452
# the input to eps format, but also restores the original bbox.
14351453

14361454
if eps:
1437-
pstoeps(tmpfile, bbox)
1455+
pstoeps(tmpfile, bbox, rotated=rotated)
14381456

14391457

1440-
def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None):
1458+
def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
14411459
"""
14421460
Use ghostscript's ps2pdf and xpdf's/poppler's pdftops to distill a file.
14431461
This yields smaller files without illegal encapsulated postscript
@@ -1482,20 +1500,26 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None):
14821500
# 8.61). Thus, the original bbox need to be resotred.
14831501

14841502
if eps:
1485-
pstoeps(tmpfile, bbox)
1503+
pstoeps(tmpfile, bbox, rotated)
14861504
for fname in glob.glob(tmpfile+'.*'):
14871505
os.remove(fname)
14881506

14891507

1490-
def get_bbox_header(l, b, r, t):
1508+
def get_bbox_header(lbrt, rotated=False):
14911509
"""
1492-
return a postscript header stringfor the given bbox (l, b, r, t)
1510+
return a postscript header stringfor the given bbox lbrt=(l, b, r, t).
1511+
Optionally, return rotate command.
14931512
"""
14941513

1514+
l, b, r, t = lbrt
1515+
if rotated:
1516+
rotate = "%.2f %.2f translate\n90 rotate" % (l+r, 0)
1517+
else:
1518+
rotate = ""
14951519
bbox_info = '%%%%BoundingBox: %d %d %d %d' % (l, b, np.ceil(r), np.ceil(t))
14961520
hires_bbox_info = '%%%%HiResBoundingBox: %.6f %.6f %.6f %.6f' % (l, b, r, t)
14971521

1498-
return '\n'.join([bbox_info, hires_bbox_info])
1522+
return '\n'.join([bbox_info, hires_bbox_info]), rotate
14991523

15001524

15011525
# get_bbox is deprecated. I don't see any reason to use ghostscript to
@@ -1543,11 +1567,13 @@ def get_bbox(tmpfile, bbox):
15431567
return '\n'.join([bbox_info, hires_bbox_info])
15441568

15451569

1546-
def pstoeps(tmpfile, bbox):
1570+
def pstoeps(tmpfile, bbox, rotated=False):
15471571
"""
15481572
Convert the postscript to encapsulated postscript.
15491573
"""
1550-
bbox_info = get_bbox_header(*bbox)
1574+
1575+
# if rotated==True, the output eps file need to be rotated
1576+
bbox_info, rotate = get_bbox_header(bbox, rotated=rotated)
15511577

15521578
epsfile = tmpfile + '.eps'
15531579
epsh = file(epsfile, 'w')
@@ -1570,9 +1596,12 @@ def pstoeps(tmpfile, bbox):
15701596
print >>epsh, '/setpagedevice {pop} def'
15711597
print >>epsh, '%%EndProlog'
15721598
print >>epsh, '%%Page 1 1'
1599+
if rotate:
1600+
print >>epsh, rotate
15731601
break
15741602
elif line.startswith('%%Bound') \
15751603
or line.startswith('%%HiResBound') \
1604+
or line.startswith('%%DocumentMedia') \
15761605
or line.startswith('%%Pages'):
15771606
pass
15781607
else:

0 commit comments

Comments
 (0)