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

Skip to content

Commit 43288e0

Browse files
committed
fixed landscape/portrait handling
svn path=/trunk/matplotlib/; revision=2067
1 parent 5ffd672 commit 43288e0

2 files changed

Lines changed: 43 additions & 33 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2006-02-21 Fixed portrait/landscape orientation in postscript backend - DSD
2+
13
2006-02-21 fix bug introduced in yesterday's bug fix - SC
24

35
2006-02-20 backend_gtk.py FigureCanvasGTK.draw(): fix bug reported by

lib/matplotlib/backends/backend_ps.py

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,20 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
6565
'c4': (9.01,12.75),
6666
'c5': (6.38,9.01),
6767
'c6': (4.49,6.38)}
68-
defaultPaperType = rcParams['ps.papersize']
69-
defaultPaperSize = papersize[defaultPaperType]
7068

71-
def _get_papersize(w,h):
69+
def _get_papersize(w,h,isLandscape=False):
7270
keys = papersize.keys()
7371
keys.sort()
7472
keys.reverse()
7573
for key in keys:
7674
if key.startswith('l'): continue
77-
val = papersize[key]
78-
# will the image fit within latex margins?
79-
if w+2 < val[0] and h+2 < val[1]: return val
80-
else: return papersize['a0']
75+
pw, ph = papersize[key]
76+
if isLandscape: ph, pw = pw, ph
77+
# Assume 1in margins
78+
if (w < pw-2) and (h < ph-2): return (pw, ph, key)
79+
else:
80+
pw, ph = papersize['a0']
81+
return (pw, ph, 'a0')
8182

8283
def _num_to_str(val):
8384
if is_string_like(val): return val
@@ -999,13 +1000,16 @@ def print_figure(self, outfile, dpi=72,
9991000
# center the figure on the paper
10001001
self.figure.dpi.set(72) # ignore the passsed dpi setting for PS
10011002
width, height = self.figure.get_size_inches()
1002-
1003+
1004+
papertype = rcParams['ps.papersize']
1005+
paperWidth, paperHeight = papersize[papertype]
10031006
if orientation=='landscape':
10041007
isLandscape = True
1005-
paperHeight, paperWidth = defaultPaperSize
1008+
paperHeight, paperWidth = paperWidth, paperHeight
10061009
else:
10071010
isLandscape = False
1008-
paperWidth, paperHeight = defaultPaperSize
1011+
if (width>paperWidth-2) or (height>paperHeight-2):
1012+
paperWidth, paperHeight, papertype = _get_papersize(width,height,isLandscape)
10091013

10101014
xo = 72*0.5*(paperWidth - width)
10111015
yo = 72*0.5*(paperHeight - height)
@@ -1015,7 +1019,7 @@ def print_figure(self, outfile, dpi=72,
10151019
lly = yo
10161020
urx = llx + w
10171021
ury = lly + h
1018-
1022+
10191023
if isLandscape:
10201024
xo, yo = 72*paperHeight - yo, xo
10211025
llx, lly, urx, ury = lly, llx, ury, urx
@@ -1045,14 +1049,10 @@ def print_figure(self, outfile, dpi=72,
10451049
print >>fh, ("%%Creator: matplotlib version "
10461050
+__version__+", http://matplotlib.sourceforge.net/")
10471051
print >>fh, "%%CreationDate: "+time.ctime(time.time())
1052+
print >>fh, "%%Orientation: " + orientation
10481053
if not isEPSF:
1049-
if paperWidth > paperHeight:
1050-
ostr="Landscape"
1051-
else:
1052-
ostr="Portrait"
1053-
print >>fh, "%%Orientation: "+ostr
1054-
print >>fh, "%%DocumentPaperSizes: "+defaultPaperType
1055-
print >>fh, "%%%%BoundingBox: %d %d %d %d" % (llx, lly, urx, ury)
1054+
print >>fh, "%%DocumentPaperSizes: "+papertype
1055+
if isEPSF: print >>fh, "%%%%BoundingBox: %d %d %d %d" % (llx, lly, urx, ury)
10561056
if not isEPSF: print >>fh, "%%Pages: 1"
10571057
print >>fh, "%%EndComments"
10581058

@@ -1101,23 +1101,23 @@ def print_figure(self, outfile, dpi=72,
11011101

11021102
if rcParams['text.usetex']:
11031103
convert_psfrags(tmpfile, renderer.psfrag,
1104-
renderer.texmanager.get_font_preamble(), width, height)
1104+
renderer.texmanager.get_font_preamble(), paperWidth, paperHeight)
11051105
if ext == '.eps': pstoeps(tmpfile)
11061106

11071107
if rcParams['ps.usedistiller'] == 'ghostscript':
1108-
gs_distill(tmpfile, ext=='.eps')
1108+
gs_distill(tmpfile, ext=='.eps', ptype=papertype)
11091109
elif rcParams['ps.usedistiller'] == 'xpdf':
1110-
xpdf_distill(tmpfile, ext=='.eps')
1110+
xpdf_distill(tmpfile, ext=='.eps', ptype=papertype)
11111111
elif rcParams['text.usetex']:
1112-
gs_distill(tmpfile, ext=='.eps')
1112+
gs_distill(tmpfile, ext=='.eps', ptype=papertype)
11131113

11141114
if isinstance(outfile, file):
11151115
fh = file(tmpfile)
11161116
print >>outfile, fh.read()
11171117
else: shutil.move(tmpfile, outfile)
11181118

11191119

1120-
def convert_psfrags(tmpfile, psfrags, font_preamble, width, height):
1120+
def convert_psfrags(tmpfile, psfrags, font_preamble, pw, ph):
11211121
"""
11221122
When we want to use the LaTeX backend with postscript, we write PSFrag tags
11231123
to a temporary postscript file, each one marking a position for LaTeX to
@@ -1132,16 +1132,20 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, width, height):
11321132
dvifile = tmpfile+'.dvi'
11331133
psfile = tmpfile+'.ps'
11341134

1135-
pw, ph = defaultPaperSize
1136-
if (width>pw-2) or (height>ph-2): pw,ph = _get_papersize(width,height)
11371135
print >>latexh, r"""\documentclass{scrartcl}
11381136
%s
11391137
\usepackage{psfrag}
11401138
\usepackage[dvips]{graphicx}
11411139
\usepackage{color}
11421140
\pagestyle{empty}
1143-
\setlength{\paperheight}{%fin}
1141+
\setlength{\oddsidemargin}{0in}
1142+
\setlength{\evensidemargin}{0in}
1143+
\setlength{\topmargin}{0in}
1144+
\setlength{\headheight}{0in}
1145+
\setlength{\headsep}{0in}
1146+
\setlength{\parindent}{0in}
11441147
\setlength{\paperwidth}{%fin}
1148+
\setlength{\paperheight}{%fin}
11451149
\setlength{\textwidth}{%fin}
11461150
\setlength{\textheight}{%fin}
11471151
\special{papersize=%fin,%fin}
@@ -1174,17 +1178,17 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, width, height):
11741178
os.chdir(curdir)
11751179

11761180

1177-
def gs_distill(tmpfile, eps=False):
1181+
def gs_distill(tmpfile, eps=False, ptype='letter'):
11781182
"""
11791183
Use ghostscript's pswrite or epswrite device to distill a file.
11801184
This yields smaller files without illegal encapsulated postscript
11811185
operators. The output is low-level, converting text to outlines.
11821186
"""
11831187
if eps:
1184-
device = 'epswrite'
1188+
device = 'epswrite -sPAPERSIZE=%s'% ptype
11851189
outputfile = tmpfile + '.eps'
11861190
else:
1187-
device = 'pswrite'
1191+
device = 'pswrite -sPAPERSIZE=%s'% ptype
11881192
outputfile = tmpfile + '.ps'
11891193
dpi = rcParams['ps.distiller.res']
11901194
if sys.platform == 'win32': gs_exe = 'gswin32c'
@@ -1199,7 +1203,7 @@ def gs_distill(tmpfile, eps=False):
11991203
shutil.move(outputfile, tmpfile)
12001204

12011205

1202-
def xpdf_distill(tmpfile, eps=False):
1206+
def xpdf_distill(tmpfile, eps=False, ptype='letter'):
12031207
"""
12041208
Use ghostscript's ps2pdf and xpdf's/poppler's pdftops to distill a file.
12051209
This yields smaller files without illegal encapsulated postscript
@@ -1209,10 +1213,10 @@ def xpdf_distill(tmpfile, eps=False):
12091213
pdffile = tmpfile + '.pdf'
12101214
psfile = tmpfile + '.ps'
12111215
shutil.move(tmpfile, psfile)
1212-
command = 'ps2pdf "%s" "%s"'% (psfile, pdffile)
1216+
command = 'ps2pdf -sPAPERSIZE=%s "%s" "%s"'% (ptype, psfile, pdffile)
12131217
stdin, stderr = os.popen4(command)
12141218
verbose.report(stderr.read(), 'helpful')
1215-
command = 'pdftops -level2 "%s" "%s"'% (pdffile, psfile)
1219+
command = 'pdftops -paper=%s -level2 "%s" "%s"'% (ptype, pdffile, psfile)
12161220
stdin, stderr = os.popen4(command)
12171221
verbose.report(stderr.read(), 'helpful')
12181222
shutil.move(psfile, tmpfile)
@@ -1231,7 +1235,11 @@ def pstoeps(tmpfile):
12311235
command = 'gs -dBATCH -dNOPAUSE -sDEVICE=bbox "%s"' % tmpfile
12321236
verbose.report(command, 'debug-annoying')
12331237
stdin, stdout, stderr = os.popen3(command)
1234-
bbox_info = stderr.read()
1238+
l, b, r, t = [float(i) for i in stderr.read().split()[-4:]]
1239+
bbox_info = '%%%%BoundingBox: %d %d %d %d\n\
1240+
%%%%HiResBoundingBox: %.6f %.6f %.6f %.6f\n' % \
1241+
(l-1, b-1, r+1, t+1, l-1, b-1, r+1, t+1)
1242+
12351243
verbose.report(stdout.read(), 'debug-annoying')
12361244
verbose.report(bbox_info, 'helpful')
12371245

0 commit comments

Comments
 (0)