From 79532c365c782ce400e8fefe482e22ca0d6ceb4a Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 28 May 2019 11:52:35 +0200 Subject: [PATCH 1/2] 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). --- lib/matplotlib/backends/backend_ps.py | 81 ++++++++++++--------------- 1 file changed, 37 insertions(+), 44 deletions(-) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 6cc7f9322e74..3103eb489a97 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -3,6 +3,7 @@ """ import datetime +from enum import Enum import glob from io import StringIO, TextIOWrapper import logging @@ -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 @@ -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 @@ -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) @@ -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 @@ -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) @@ -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 @@ -1208,8 +1203,7 @@ 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) @@ -1217,9 +1211,8 @@ def write(self, *args, **kwargs): # 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': @@ -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']): From d3471abea14e11db5475a6fe6db55dba1695c9f6 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sat, 24 Aug 2019 16:55:08 -0400 Subject: [PATCH 2/2] TST: exercise changing the orientation --- lib/matplotlib/tests/test_backend_ps.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_backend_ps.py b/lib/matplotlib/tests/test_backend_ps.py index 32daee439a41..fc81c1d153ab 100644 --- a/lib/matplotlib/tests/test_backend_ps.py +++ b/lib/matplotlib/tests/test_backend_ps.py @@ -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'}, @@ -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. @@ -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()