Skip to content

Commit 73ed43b

Browse files
authored
Merge pull request #14352 from anntzer/pslandscape
API/MNT: Remove redundant is_landscape kwarg from backend_ps helpers, deprecate dryrun
2 parents 5ad446d + d3471ab commit 73ed43b

File tree

2 files changed

+42
-47
lines changed

2 files changed

+42
-47
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import datetime
6+
from enum import Enum
67
import glob
78
from io import StringIO, TextIOWrapper
89
import logging
@@ -805,6 +806,13 @@ def shouldstroke(self):
805806
(len(self.get_rgb()) <= 3 or self.get_rgb()[3] != 0.0))
806807

807808

809+
class _Orientation(Enum):
810+
portrait, landscape = range(2)
811+
812+
def swap_if_landscape(self, shape):
813+
return shape[::-1] if self.name == "landscape" else shape
814+
815+
808816
class FigureCanvasPS(FigureCanvasBase):
809817
fixed_dpi = 72
810818

@@ -832,28 +840,22 @@ def _print_ps(self, outfile, format, *args,
832840
papertype = papertype.lower()
833841
cbook._check_in_list(['auto', *papersize], papertype=papertype)
834842

835-
orientation = orientation.lower()
836-
cbook._check_in_list(['landscape', 'portrait'],
837-
orientation=orientation)
838-
is_landscape = (orientation == 'landscape')
843+
orientation = cbook._check_getitem(
844+
_Orientation, orientation=orientation.lower())
839845

840846
self.figure.set_dpi(72) # Override the dpi kwarg
841847

842-
if rcParams['text.usetex']:
843-
self._print_figure_tex(outfile, format, dpi, facecolor, edgecolor,
844-
orientation, is_landscape, papertype,
845-
**kwargs)
846-
else:
847-
self._print_figure(outfile, format, dpi, facecolor, edgecolor,
848-
orientation, is_landscape, papertype,
849-
**kwargs)
848+
printer = (self._print_figure_tex
849+
if rcParams['text.usetex'] else
850+
self._print_figure)
851+
printer(outfile, format, dpi, facecolor, edgecolor,
852+
orientation, papertype, **kwargs)
850853

851854
@cbook._delete_parameter("3.2", "dryrun")
852855
def _print_figure(
853-
self, outfile, format, dpi=72, facecolor='w', edgecolor='w',
854-
orientation='portrait', is_landscape=False, papertype=None,
855-
metadata=None, *,
856-
dryrun=False, bbox_inches_restore=None, **kwargs):
856+
self, outfile, format, dpi, facecolor, edgecolor,
857+
orientation, papertype, *,
858+
metadata=None, dryrun=False, bbox_inches_restore=None, **kwargs):
857859
"""
858860
Render the figure to hardcopy. Set the figure patch face and
859861
edge colors. This is useful because some of the GUIs have a
@@ -884,26 +886,18 @@ def _print_figure(
884886
# find the appropriate papertype
885887
width, height = self.figure.get_size_inches()
886888
if papertype == 'auto':
887-
if is_landscape:
888-
papertype = _get_papertype(height, width)
889-
else:
890-
papertype = _get_papertype(width, height)
889+
papertype = _get_papertype(
890+
*orientation.swap_if_landscape((width, height)))
891+
paper_width, paper_height = orientation.swap_if_landscape(
892+
papersize[papertype])
891893

892-
if is_landscape:
893-
paper_height, paper_width = papersize[papertype]
894-
else:
895-
paper_width, paper_height = papersize[papertype]
896-
897-
if rcParams['ps.usedistiller'] and papertype != 'auto':
898-
# distillers will improperly clip eps files if the pagesize is
899-
# too small
894+
if rcParams['ps.usedistiller']:
895+
# distillers improperly clip eps files if pagesize is too small
900896
if width > paper_width or height > paper_height:
901-
if is_landscape:
902-
papertype = _get_papertype(height, width)
903-
paper_height, paper_width = papersize[papertype]
904-
else:
905-
papertype = _get_papertype(width, height)
906-
paper_width, paper_height = papersize[papertype]
897+
papertype = _get_papertype(
898+
*orientation.swap_if_landscape(width, height))
899+
paper_width, paper_height = orientation.swap_if_landscape(
900+
papersize[papertype])
907901

908902
# center the figure on the paper
909903
xo = 72 * 0.5 * (paper_width - width)
@@ -915,7 +909,7 @@ def _print_figure(
915909
urx = llx + w
916910
ury = lly + h
917911
rotation = 0
918-
if is_landscape:
912+
if orientation is _Orientation.landscape:
919913
llx, lly, urx, ury = lly, llx, ury, urx
920914
xo, yo = 72 * paper_height - yo, xo
921915
rotation = 90
@@ -978,7 +972,7 @@ def print_figure_impl(fh):
978972
source_date = time.ctime()
979973
print(f"%%Creator: {creator_str}\n"
980974
f"%%CreationDate: {source_date}\n"
981-
f"%%Orientation: {orientation}\n"
975+
f"%%Orientation: {orientation.name}\n"
982976
f"%%BoundingBox: {bbox[0]} {bbox[1]} {bbox[2]} {bbox[3]}\n"
983977
f"%%EndComments\n",
984978
end="", file=fh)
@@ -1083,10 +1077,11 @@ def print_figure_impl(fh):
10831077
with open(outfile, 'w', encoding='latin-1') as fh:
10841078
print_figure_impl(fh)
10851079

1080+
@cbook._delete_parameter("3.2", "dryrun")
10861081
def _print_figure_tex(
10871082
self, outfile, format, dpi, facecolor, edgecolor,
1088-
orientation, is_landscape, papertype, metadata=None, *,
1089-
dryrun=False, bbox_inches_restore=None, **kwargs):
1083+
orientation, papertype, *,
1084+
metadata=None, dryrun=False, bbox_inches_restore=None, **kwargs):
10901085
"""
10911086
If text.usetex is True in rc, a temporary pair of tex/eps files
10921087
are created to allow tex to manage the text layout via the PSFrags
@@ -1189,18 +1184,16 @@ def write(self, *args, **kwargs):
11891184
""",
11901185
encoding="latin-1")
11911186

1192-
if is_landscape: # now we are ready to rotate
1193-
is_landscape = True
1187+
if orientation is _Orientation.landscape: # now, ready to rotate
11941188
width, height = height, width
11951189
bbox = (lly, llx, ury, urx)
11961190

11971191
# set the paper size to the figure size if is_eps. The
11981192
# resulting ps file has the given size with correct bounding
11991193
# box so that there is no need to call 'pstoeps'
12001194
if is_eps:
1201-
paper_width, paper_height = self.figure.get_size_inches()
1202-
if is_landscape:
1203-
paper_width, paper_height = paper_height, paper_width
1195+
paper_width, paper_height = orientation.swap_if_landscape(
1196+
self.figure.get_size_inches())
12041197
else:
12051198
temp_papertype = _get_papertype(width, height)
12061199
if papertype == 'auto':
@@ -1217,7 +1210,7 @@ def write(self, *args, **kwargs):
12171210
font_preamble,
12181211
custom_preamble, paper_width,
12191212
paper_height,
1220-
orientation)
1213+
orientation.name)
12211214

12221215
if (rcParams['ps.usedistiller'] == 'ghostscript'
12231216
or rcParams['text.usetex']):

lib/matplotlib/tests/test_backend_ps.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
# This tests tends to hit a TeX cache lock on AppVeyor.
2727
@pytest.mark.flaky(reruns=3)
28+
@pytest.mark.parametrize('orientation', ['portrait', 'landscape'])
2829
@pytest.mark.parametrize('format, use_log, rcParams', [
2930
('ps', False, {}),
3031
pytest.param('ps', False, {'ps.usedistiller': 'ghostscript'},
@@ -43,7 +44,8 @@
4344
'eps afm',
4445
'eps with usetex'
4546
])
46-
def test_savefig_to_stringio(format, use_log, rcParams, monkeypatch):
47+
def test_savefig_to_stringio(format, use_log, rcParams, orientation,
48+
monkeypatch):
4749
mpl.rcParams.update(rcParams)
4850
monkeypatch.setenv("SOURCE_DATE_EPOCH", "0") # For reproducibility.
4951

@@ -59,8 +61,8 @@ def test_savefig_to_stringio(format, use_log, rcParams, monkeypatch):
5961
if not mpl.rcParams["text.usetex"]:
6062
title += " \N{MINUS SIGN}\N{EURO SIGN}"
6163
ax.set_title(title)
62-
fig.savefig(s_buf, format=format)
63-
fig.savefig(b_buf, format=format)
64+
fig.savefig(s_buf, format=format, orientation=orientation)
65+
fig.savefig(b_buf, format=format, orientation=orientation)
6466

6567
s_val = s_buf.getvalue().encode('ascii')
6668
b_val = b_buf.getvalue()

0 commit comments

Comments
 (0)