Skip to content

Commit 79532c3

Browse files
committed
Remove redundant is_landscape kwarg from backend_ps helpers.
The internale backend_ps _print_figure and _print_figure_tex helpers get passed both the orientation="portrait"/"landscape" and the is_landscape=True/False kwargs. Remove is_landscape and change orientation to be an enum. Also deprecate dryrun from _print_figure_tex (this should have happened at the same time as when it was deprecated from _print_figure (it's the same public API). Also remove unnecessary default parameters from _print_figure (these arguments are always passed positionally to the helper, and not present in _print_figure_tex).
1 parent f4ecebc commit 79532c3

File tree

1 file changed

+37
-44
lines changed

1 file changed

+37
-44
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
@@ -824,6 +825,13 @@ def shouldstroke(self):
824825
(len(self.get_rgb()) <= 3 or self.get_rgb()[3] != 0.0))
825826

826827

828+
class _Orientation(Enum):
829+
portrait, landscape = range(2)
830+
831+
def swap_if_landscape(self, shape):
832+
return shape[::-1] if self.name == "landscape" else shape
833+
834+
827835
class FigureCanvasPS(FigureCanvasBase):
828836
fixed_dpi = 72
829837

@@ -851,28 +859,22 @@ def _print_ps(self, outfile, format, *args,
851859
papertype = papertype.lower()
852860
cbook._check_in_list(['auto', *papersize], papertype=papertype)
853861

854-
orientation = orientation.lower()
855-
cbook._check_in_list(['landscape', 'portrait'],
856-
orientation=orientation)
857-
is_landscape = (orientation == 'landscape')
862+
orientation = cbook._check_getitem(
863+
_Orientation, orientation=orientation.lower())
858864

859865
self.figure.set_dpi(72) # Override the dpi kwarg
860866

861-
if rcParams['text.usetex']:
862-
self._print_figure_tex(outfile, format, dpi, facecolor, edgecolor,
863-
orientation, is_landscape, papertype,
864-
**kwargs)
865-
else:
866-
self._print_figure(outfile, format, dpi, facecolor, edgecolor,
867-
orientation, is_landscape, papertype,
868-
**kwargs)
867+
printer = (self._print_figure_tex
868+
if rcParams['text.usetex'] else
869+
self._print_figure)
870+
printer(outfile, format, dpi, facecolor, edgecolor,
871+
orientation, papertype, **kwargs)
869872

870873
@cbook._delete_parameter("3.2", "dryrun")
871874
def _print_figure(
872-
self, outfile, format, dpi=72, facecolor='w', edgecolor='w',
873-
orientation='portrait', is_landscape=False, papertype=None,
874-
metadata=None, *,
875-
dryrun=False, bbox_inches_restore=None, **kwargs):
875+
self, outfile, format, dpi, facecolor, edgecolor,
876+
orientation, papertype, *,
877+
metadata=None, dryrun=False, bbox_inches_restore=None, **kwargs):
876878
"""
877879
Render the figure to hardcopy. Set the figure patch face and
878880
edge colors. This is useful because some of the GUIs have a
@@ -903,26 +905,18 @@ def _print_figure(
903905
# find the appropriate papertype
904906
width, height = self.figure.get_size_inches()
905907
if papertype == 'auto':
906-
if is_landscape:
907-
papertype = _get_papertype(height, width)
908-
else:
909-
papertype = _get_papertype(width, height)
908+
papertype = _get_papertype(
909+
*orientation.swap_if_landscape((width, height)))
910+
paper_width, paper_height = orientation.swap_if_landscape(
911+
papersize[papertype])
910912

911-
if is_landscape:
912-
paper_height, paper_width = papersize[papertype]
913-
else:
914-
paper_width, paper_height = papersize[papertype]
915-
916-
if rcParams['ps.usedistiller'] and papertype != 'auto':
917-
# distillers will improperly clip eps files if the pagesize is
918-
# too small
913+
if rcParams['ps.usedistiller']:
914+
# distillers improperly clip eps files if pagesize is too small
919915
if width > paper_width or height > paper_height:
920-
if is_landscape:
921-
papertype = _get_papertype(height, width)
922-
paper_height, paper_width = papersize[papertype]
923-
else:
924-
papertype = _get_papertype(width, height)
925-
paper_width, paper_height = papersize[papertype]
916+
papertype = _get_papertype(
917+
*orientation.swap_if_landscape(width, height))
918+
paper_width, paper_height = orientation.swap_if_landscape(
919+
papersize[papertype])
926920

927921
# center the figure on the paper
928922
xo = 72 * 0.5 * (paper_width - width)
@@ -934,7 +928,7 @@ def _print_figure(
934928
urx = llx + w
935929
ury = lly + h
936930
rotation = 0
937-
if is_landscape:
931+
if orientation is _Orientation.landscape:
938932
llx, lly, urx, ury = lly, llx, ury, urx
939933
xo, yo = 72 * paper_height - yo, xo
940934
rotation = 90
@@ -997,7 +991,7 @@ def print_figure_impl(fh):
997991
source_date = time.ctime()
998992
print(f"%%Creator: {creator_str}\n"
999993
f"%%CreationDate: {source_date}\n"
1000-
f"%%Orientation: {orientation}\n"
994+
f"%%Orientation: {orientation.name}\n"
1001995
f"%%BoundingBox: {bbox[0]} {bbox[1]} {bbox[2]} {bbox[3]}\n"
1002996
f"%%EndComments\n",
1003997
end="", file=fh)
@@ -1102,10 +1096,11 @@ def print_figure_impl(fh):
11021096
with open(outfile, 'w', encoding='latin-1') as fh:
11031097
print_figure_impl(fh)
11041098

1099+
@cbook._delete_parameter("3.2", "dryrun")
11051100
def _print_figure_tex(
11061101
self, outfile, format, dpi, facecolor, edgecolor,
1107-
orientation, is_landscape, papertype, metadata=None, *,
1108-
dryrun=False, bbox_inches_restore=None, **kwargs):
1102+
orientation, papertype, *,
1103+
metadata=None, dryrun=False, bbox_inches_restore=None, **kwargs):
11091104
"""
11101105
If text.usetex is True in rc, a temporary pair of tex/eps files
11111106
are created to allow tex to manage the text layout via the PSFrags
@@ -1208,18 +1203,16 @@ def write(self, *args, **kwargs):
12081203
""",
12091204
encoding="latin-1")
12101205

1211-
if is_landscape: # now we are ready to rotate
1212-
is_landscape = True
1206+
if orientation is _Orientation.landscape: # now, ready to rotate
12131207
width, height = height, width
12141208
bbox = (lly, llx, ury, urx)
12151209

12161210
# set the paper size to the figure size if is_eps. The
12171211
# resulting ps file has the given size with correct bounding
12181212
# box so that there is no need to call 'pstoeps'
12191213
if is_eps:
1220-
paper_width, paper_height = self.figure.get_size_inches()
1221-
if is_landscape:
1222-
paper_width, paper_height = paper_height, paper_width
1214+
paper_width, paper_height = orientation.swap_if_landscape(
1215+
self.figure.get_size_inches())
12231216
else:
12241217
temp_papertype = _get_papertype(width, height)
12251218
if papertype == 'auto':
@@ -1236,7 +1229,7 @@ def write(self, *args, **kwargs):
12361229
font_preamble,
12371230
custom_preamble, paper_width,
12381231
paper_height,
1239-
orientation)
1232+
orientation.name)
12401233

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

0 commit comments

Comments
 (0)