3
3
"""
4
4
5
5
import datetime
6
+ from enum import Enum
6
7
import glob
7
8
from io import StringIO , TextIOWrapper
8
9
import logging
@@ -824,6 +825,13 @@ def shouldstroke(self):
824
825
(len (self .get_rgb ()) <= 3 or self .get_rgb ()[3 ] != 0.0 ))
825
826
826
827
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
+
827
835
class FigureCanvasPS (FigureCanvasBase ):
828
836
fixed_dpi = 72
829
837
@@ -851,28 +859,22 @@ def _print_ps(self, outfile, format, *args,
851
859
papertype = papertype .lower ()
852
860
cbook ._check_in_list (['auto' , * papersize ], papertype = papertype )
853
861
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 ())
858
864
859
865
self .figure .set_dpi (72 ) # Override the dpi kwarg
860
866
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 )
869
872
870
873
@cbook ._delete_parameter ("3.2" , "dryrun" )
871
874
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 ):
876
878
"""
877
879
Render the figure to hardcopy. Set the figure patch face and
878
880
edge colors. This is useful because some of the GUIs have a
@@ -903,26 +905,18 @@ def _print_figure(
903
905
# find the appropriate papertype
904
906
width , height = self .figure .get_size_inches ()
905
907
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 ] )
910
912
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
919
915
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 ])
926
920
927
921
# center the figure on the paper
928
922
xo = 72 * 0.5 * (paper_width - width )
@@ -934,7 +928,7 @@ def _print_figure(
934
928
urx = llx + w
935
929
ury = lly + h
936
930
rotation = 0
937
- if is_landscape :
931
+ if orientation is _Orientation . landscape :
938
932
llx , lly , urx , ury = lly , llx , ury , urx
939
933
xo , yo = 72 * paper_height - yo , xo
940
934
rotation = 90
@@ -997,7 +991,7 @@ def print_figure_impl(fh):
997
991
source_date = time .ctime ()
998
992
print (f"%%Creator: { creator_str } \n "
999
993
f"%%CreationDate: { source_date } \n "
1000
- f"%%Orientation: { orientation } \n "
994
+ f"%%Orientation: { orientation . name } \n "
1001
995
f"%%BoundingBox: { bbox [0 ]} { bbox [1 ]} { bbox [2 ]} { bbox [3 ]} \n "
1002
996
f"%%EndComments\n " ,
1003
997
end = "" , file = fh )
@@ -1102,10 +1096,11 @@ def print_figure_impl(fh):
1102
1096
with open (outfile , 'w' , encoding = 'latin-1' ) as fh :
1103
1097
print_figure_impl (fh )
1104
1098
1099
+ @cbook ._delete_parameter ("3.2" , "dryrun" )
1105
1100
def _print_figure_tex (
1106
1101
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 ):
1109
1104
"""
1110
1105
If text.usetex is True in rc, a temporary pair of tex/eps files
1111
1106
are created to allow tex to manage the text layout via the PSFrags
@@ -1208,18 +1203,16 @@ def write(self, *args, **kwargs):
1208
1203
""" ,
1209
1204
encoding = "latin-1" )
1210
1205
1211
- if is_landscape : # now we are ready to rotate
1212
- is_landscape = True
1206
+ if orientation is _Orientation .landscape : # now, ready to rotate
1213
1207
width , height = height , width
1214
1208
bbox = (lly , llx , ury , urx )
1215
1209
1216
1210
# set the paper size to the figure size if is_eps. The
1217
1211
# resulting ps file has the given size with correct bounding
1218
1212
# box so that there is no need to call 'pstoeps'
1219
1213
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 ())
1223
1216
else :
1224
1217
temp_papertype = _get_papertype (width , height )
1225
1218
if papertype == 'auto' :
@@ -1236,7 +1229,7 @@ def write(self, *args, **kwargs):
1236
1229
font_preamble ,
1237
1230
custom_preamble , paper_width ,
1238
1231
paper_height ,
1239
- orientation )
1232
+ orientation . name )
1240
1233
1241
1234
if (rcParams ['ps.usedistiller' ] == 'ghostscript'
1242
1235
or rcParams ['text.usetex' ]):
0 commit comments