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
@@ -805,6 +806,13 @@ def shouldstroke(self):
805
806
(len (self .get_rgb ()) <= 3 or self .get_rgb ()[3 ] != 0.0 ))
806
807
807
808
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
+
808
816
class FigureCanvasPS (FigureCanvasBase ):
809
817
fixed_dpi = 72
810
818
@@ -832,28 +840,22 @@ def _print_ps(self, outfile, format, *args,
832
840
papertype = papertype .lower ()
833
841
cbook ._check_in_list (['auto' , * papersize ], papertype = papertype )
834
842
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 ())
839
845
840
846
self .figure .set_dpi (72 ) # Override the dpi kwarg
841
847
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 )
850
853
851
854
@cbook ._delete_parameter ("3.2" , "dryrun" )
852
855
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 ):
857
859
"""
858
860
Render the figure to hardcopy. Set the figure patch face and
859
861
edge colors. This is useful because some of the GUIs have a
@@ -884,26 +886,18 @@ def _print_figure(
884
886
# find the appropriate papertype
885
887
width , height = self .figure .get_size_inches ()
886
888
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 ] )
891
893
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
900
896
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 ])
907
901
908
902
# center the figure on the paper
909
903
xo = 72 * 0.5 * (paper_width - width )
@@ -915,7 +909,7 @@ def _print_figure(
915
909
urx = llx + w
916
910
ury = lly + h
917
911
rotation = 0
918
- if is_landscape :
912
+ if orientation is _Orientation . landscape :
919
913
llx , lly , urx , ury = lly , llx , ury , urx
920
914
xo , yo = 72 * paper_height - yo , xo
921
915
rotation = 90
@@ -978,7 +972,7 @@ def print_figure_impl(fh):
978
972
source_date = time .ctime ()
979
973
print (f"%%Creator: { creator_str } \n "
980
974
f"%%CreationDate: { source_date } \n "
981
- f"%%Orientation: { orientation } \n "
975
+ f"%%Orientation: { orientation . name } \n "
982
976
f"%%BoundingBox: { bbox [0 ]} { bbox [1 ]} { bbox [2 ]} { bbox [3 ]} \n "
983
977
f"%%EndComments\n " ,
984
978
end = "" , file = fh )
@@ -1083,10 +1077,11 @@ def print_figure_impl(fh):
1083
1077
with open (outfile , 'w' , encoding = 'latin-1' ) as fh :
1084
1078
print_figure_impl (fh )
1085
1079
1080
+ @cbook ._delete_parameter ("3.2" , "dryrun" )
1086
1081
def _print_figure_tex (
1087
1082
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 ):
1090
1085
"""
1091
1086
If text.usetex is True in rc, a temporary pair of tex/eps files
1092
1087
are created to allow tex to manage the text layout via the PSFrags
@@ -1189,18 +1184,16 @@ def write(self, *args, **kwargs):
1189
1184
""" ,
1190
1185
encoding = "latin-1" )
1191
1186
1192
- if is_landscape : # now we are ready to rotate
1193
- is_landscape = True
1187
+ if orientation is _Orientation .landscape : # now, ready to rotate
1194
1188
width , height = height , width
1195
1189
bbox = (lly , llx , ury , urx )
1196
1190
1197
1191
# set the paper size to the figure size if is_eps. The
1198
1192
# resulting ps file has the given size with correct bounding
1199
1193
# box so that there is no need to call 'pstoeps'
1200
1194
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 ())
1204
1197
else :
1205
1198
temp_papertype = _get_papertype (width , height )
1206
1199
if papertype == 'auto' :
@@ -1217,7 +1210,7 @@ def write(self, *args, **kwargs):
1217
1210
font_preamble ,
1218
1211
custom_preamble , paper_width ,
1219
1212
paper_height ,
1220
- orientation )
1213
+ orientation . name )
1221
1214
1222
1215
if (rcParams ['ps.usedistiller' ] == 'ghostscript'
1223
1216
or rcParams ['text.usetex' ]):
0 commit comments