Skip to content

Remove redundant is_landscape kwarg from backend_ps helpers. #14352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 37 additions & 44 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import datetime
from enum import Enum
import glob
from io import StringIO, TextIOWrapper
import logging
Expand Down Expand Up @@ -824,6 +825,13 @@ def shouldstroke(self):
(len(self.get_rgb()) <= 3 or self.get_rgb()[3] != 0.0))


class _Orientation(Enum):
portrait, landscape = range(2)

def swap_if_landscape(self, shape):
return shape[::-1] if self.name == "landscape" else shape


class FigureCanvasPS(FigureCanvasBase):
fixed_dpi = 72

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

orientation = orientation.lower()
cbook._check_in_list(['landscape', 'portrait'],
orientation=orientation)
is_landscape = (orientation == 'landscape')
orientation = cbook._check_getitem(
_Orientation, orientation=orientation.lower())

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

if rcParams['text.usetex']:
self._print_figure_tex(outfile, format, dpi, facecolor, edgecolor,
orientation, is_landscape, papertype,
**kwargs)
else:
self._print_figure(outfile, format, dpi, facecolor, edgecolor,
orientation, is_landscape, papertype,
**kwargs)
printer = (self._print_figure_tex
if rcParams['text.usetex'] else
self._print_figure)
printer(outfile, format, dpi, facecolor, edgecolor,
orientation, papertype, **kwargs)

@cbook._delete_parameter("3.2", "dryrun")
def _print_figure(
self, outfile, format, dpi=72, facecolor='w', edgecolor='w',
orientation='portrait', is_landscape=False, papertype=None,
metadata=None, *,
dryrun=False, bbox_inches_restore=None, **kwargs):
self, outfile, format, dpi, facecolor, edgecolor,
orientation, papertype, *,
metadata=None, dryrun=False, bbox_inches_restore=None, **kwargs):
"""
Render the figure to hardcopy. Set the figure patch face and
edge colors. This is useful because some of the GUIs have a
Expand Down Expand Up @@ -903,26 +905,18 @@ def _print_figure(
# find the appropriate papertype
width, height = self.figure.get_size_inches()
if papertype == 'auto':
if is_landscape:
papertype = _get_papertype(height, width)
else:
papertype = _get_papertype(width, height)
papertype = _get_papertype(
*orientation.swap_if_landscape((width, height)))
paper_width, paper_height = orientation.swap_if_landscape(
papersize[papertype])

if is_landscape:
paper_height, paper_width = papersize[papertype]
else:
paper_width, paper_height = papersize[papertype]

if rcParams['ps.usedistiller'] and papertype != 'auto':
# distillers will improperly clip eps files if the pagesize is
# too small
if rcParams['ps.usedistiller']:
# distillers improperly clip eps files if pagesize is too small
if width > paper_width or height > paper_height:
if is_landscape:
papertype = _get_papertype(height, width)
paper_height, paper_width = papersize[papertype]
else:
papertype = _get_papertype(width, height)
paper_width, paper_height = papersize[papertype]
papertype = _get_papertype(
*orientation.swap_if_landscape(width, height))
paper_width, paper_height = orientation.swap_if_landscape(
papersize[papertype])

# center the figure on the paper
xo = 72 * 0.5 * (paper_width - width)
Expand All @@ -934,7 +928,7 @@ def _print_figure(
urx = llx + w
ury = lly + h
rotation = 0
if is_landscape:
if orientation is _Orientation.landscape:
llx, lly, urx, ury = lly, llx, ury, urx
xo, yo = 72 * paper_height - yo, xo
rotation = 90
Expand Down Expand Up @@ -997,7 +991,7 @@ def print_figure_impl(fh):
source_date = time.ctime()
print(f"%%Creator: {creator_str}\n"
f"%%CreationDate: {source_date}\n"
f"%%Orientation: {orientation}\n"
f"%%Orientation: {orientation.name}\n"
f"%%BoundingBox: {bbox[0]} {bbox[1]} {bbox[2]} {bbox[3]}\n"
f"%%EndComments\n",
end="", file=fh)
Expand Down Expand Up @@ -1102,10 +1096,11 @@ def print_figure_impl(fh):
with open(outfile, 'w', encoding='latin-1') as fh:
print_figure_impl(fh)

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

if is_landscape: # now we are ready to rotate
is_landscape = True
if orientation is _Orientation.landscape: # now, ready to rotate
width, height = height, width
bbox = (lly, llx, ury, urx)

# set the paper size to the figure size if is_eps. The
# resulting ps file has the given size with correct bounding
# box so that there is no need to call 'pstoeps'
if is_eps:
paper_width, paper_height = self.figure.get_size_inches()
if is_landscape:
paper_width, paper_height = paper_height, paper_width
paper_width, paper_height = orientation.swap_if_landscape(
self.figure.get_size_inches())
else:
temp_papertype = _get_papertype(width, height)
if papertype == 'auto':
Expand All @@ -1236,7 +1229,7 @@ def write(self, *args, **kwargs):
font_preamble,
custom_preamble, paper_width,
paper_height,
orientation)
orientation.name)

if (rcParams['ps.usedistiller'] == 'ghostscript'
or rcParams['text.usetex']):
Expand Down
8 changes: 5 additions & 3 deletions lib/matplotlib/tests/test_backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

# This tests tends to hit a TeX cache lock on AppVeyor.
@pytest.mark.flaky(reruns=3)
@pytest.mark.parametrize('orientation', ['portrait', 'landscape'])
@pytest.mark.parametrize('format, use_log, rcParams', [
('ps', False, {}),
pytest.param('ps', False, {'ps.usedistiller': 'ghostscript'},
Expand All @@ -45,7 +46,8 @@
'eps afm',
'eps with usetex'
])
def test_savefig_to_stringio(format, use_log, rcParams, monkeypatch):
def test_savefig_to_stringio(format, use_log, rcParams, orientation,
monkeypatch):
mpl.rcParams.update(rcParams)
monkeypatch.setenv("SOURCE_DATE_EPOCH", "0") # For reproducibility.

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

s_val = s_buf.getvalue().encode('ascii')
b_val = b_buf.getvalue()
Expand Down