@@ -831,10 +831,29 @@ def print_ps(self, outfile, *args, **kwargs):
831
831
def print_eps (self , outfile , * args , ** kwargs ):
832
832
return self ._print_ps (outfile , 'eps' , * args , ** kwargs )
833
833
834
- def _print_ps (self , outfile , format , * args ,
835
- papertype = None , dpi = 72 , facecolor = 'w' , edgecolor = 'w' ,
836
- orientation = 'portrait' ,
837
- ** kwargs ):
834
+ def _print_ps (
835
+ self , outfile , format , * args ,
836
+ dpi = 72 , metadata = None , papertype = None , orientation = 'portrait' ,
837
+ ** kwargs ):
838
+
839
+ self .figure .set_dpi (72 ) # Override the dpi kwarg
840
+
841
+ dsc_comments = {}
842
+ if isinstance (outfile , (str , os .PathLike )):
843
+ dsc_comments ["Title" ] = \
844
+ os .fspath (outfile ).encode ("ascii" , "replace" ).decode ("ascii" )
845
+ dsc_comments ["Creator" ] = (metadata or {}).get (
846
+ "Creator" ,
847
+ f"matplotlib version { __version__ } , http://matplotlib.org/" )
848
+ source_date_epoch = os .getenv ("SOURCE_DATE_EPOCH" )
849
+ dsc_comments ["CreationDate" ] = (
850
+ datetime .datetime .utcfromtimestamp (
851
+ int (source_date_epoch )).strftime ("%a %b %d %H:%M:%S %Y" )
852
+ if source_date_epoch
853
+ else time .ctime ())
854
+ dsc_comments = "\n " .join (
855
+ f"%%{ k } : { v } " for k , v in dsc_comments .items ())
856
+
838
857
if papertype is None :
839
858
papertype = rcParams ['ps.papersize' ]
840
859
papertype = papertype .lower ()
@@ -843,42 +862,29 @@ def _print_ps(self, outfile, format, *args,
843
862
orientation = cbook ._check_getitem (
844
863
_Orientation , orientation = orientation .lower ())
845
864
846
- self .figure .set_dpi (72 ) # Override the dpi kwarg
847
-
848
865
printer = (self ._print_figure_tex
849
866
if rcParams ['text.usetex' ] else
850
867
self ._print_figure )
851
- printer (outfile , format , dpi , facecolor , edgecolor ,
852
- orientation , papertype , ** kwargs )
868
+ printer (outfile , format , dpi = dpi , dsc_comments = dsc_comments ,
869
+ orientation = orientation , papertype = papertype , ** kwargs )
853
870
854
871
@cbook ._delete_parameter ("3.2" , "dryrun" )
855
872
def _print_figure (
856
- self , outfile , format , dpi , facecolor , edgecolor ,
857
- orientation , papertype , * ,
858
- metadata = None , dryrun = False , bbox_inches_restore = None , ** kwargs ):
873
+ self , outfile , format , * ,
874
+ dpi , dsc_comments , orientation , papertype ,
875
+ dryrun = False , bbox_inches_restore = None , ** kwargs ):
859
876
"""
860
- Render the figure to hardcopy. Set the figure patch face and
861
- edge colors. This is useful because some of the GUIs have a
862
- gray figure face color background and you'll probably want to
863
- override this on hardcopy
864
-
865
- If outfile is a string, it is interpreted as a file name.
866
- If the extension matches .ep* write encapsulated postscript,
867
- otherwise write a stand-alone PostScript file.
868
-
869
- If outfile is a file object, a stand-alone PostScript file is
870
- written into this file object.
877
+ Render the figure to a filesystem path or a file-like object.
871
878
872
- metadata must be a dictionary. Currently, only the value for
873
- the key 'Creator' is used.
879
+ Parameters are as for `.print_figure`, except that *dsc_comments* is a
880
+ all string containing Document Structuring Convention comments,
881
+ generated from the *metadata* parameter to `.print_figure`.
874
882
"""
875
883
is_eps = format == 'eps'
876
884
if isinstance (outfile , (str , os .PathLike )):
877
- outfile = title = os .fspath (outfile )
878
- title = title .encode ("ascii" , "replace" ).decode ("ascii" )
885
+ outfile = os .fspath (outfile )
879
886
passed_in_file_object = False
880
887
elif is_writable_file_like (outfile ):
881
- title = None
882
888
passed_in_file_object = True
883
889
else :
884
890
raise ValueError ("outfile must be a path or a file-like object" )
@@ -915,12 +921,6 @@ def _print_figure(
915
921
rotation = 90
916
922
bbox = (llx , lly , urx , ury )
917
923
918
- # generate PostScript code for the figure and store it in a string
919
- origfacecolor = self .figure .get_facecolor ()
920
- origedgecolor = self .figure .get_edgecolor ()
921
- self .figure .set_facecolor (facecolor )
922
- self .figure .set_edgecolor (edgecolor )
923
-
924
924
if dryrun :
925
925
class NullWriter :
926
926
def write (self , * args , ** kwargs ):
@@ -941,16 +941,6 @@ def write(self, *args, **kwargs):
941
941
if dryrun : # return immediately if dryrun (tightbbox=True)
942
942
return
943
943
944
- self .figure .set_facecolor (origfacecolor )
945
- self .figure .set_edgecolor (origedgecolor )
946
-
947
- # check for custom metadata
948
- if metadata is not None and 'Creator' in metadata :
949
- creator_str = metadata ['Creator' ]
950
- else :
951
- creator_str = "matplotlib version " + __version__ + \
952
- ", http://matplotlib.org/"
953
-
954
944
def print_figure_impl (fh ):
955
945
# write the PostScript headers
956
946
if is_eps :
@@ -960,18 +950,7 @@ def print_figure_impl(fh):
960
950
f"%%DocumentPaperSizes: { papertype } \n "
961
951
f"%%Pages: 1\n " ,
962
952
end = "" , file = fh )
963
- if title :
964
- print ("%%Title: " + title , file = fh )
965
- # get source date from SOURCE_DATE_EPOCH, if set
966
- # See https://reproducible-builds.org/specs/source-date-epoch/
967
- source_date_epoch = os .getenv ("SOURCE_DATE_EPOCH" )
968
- if source_date_epoch :
969
- source_date = datetime .datetime .utcfromtimestamp (
970
- int (source_date_epoch )).strftime ("%a %b %d %H:%M:%S %Y" )
971
- else :
972
- source_date = time .ctime ()
973
- print (f"%%Creator: { creator_str } \n "
974
- f"%%CreationDate: { source_date } \n "
953
+ print (f"{ dsc_comments } \n "
975
954
f"%%Orientation: { orientation .name } \n "
976
955
f"%%BoundingBox: { bbox [0 ]} { bbox [1 ]} { bbox [2 ]} { bbox [3 ]} \n "
977
956
f"%%EndComments\n " ,
@@ -1079,28 +1058,18 @@ def print_figure_impl(fh):
1079
1058
1080
1059
@cbook ._delete_parameter ("3.2" , "dryrun" )
1081
1060
def _print_figure_tex (
1082
- self , outfile , format , dpi , facecolor , edgecolor ,
1083
- orientation , papertype , * ,
1084
- metadata = None , dryrun = False , bbox_inches_restore = None , ** kwargs ):
1061
+ self , outfile , format , * ,
1062
+ dpi , dsc_comments , orientation , papertype ,
1063
+ dryrun = False , bbox_inches_restore = None , ** kwargs ):
1085
1064
"""
1086
- If text.usetex is True in rc , a temporary pair of tex/eps files
1065
+ If :rc:` text.usetex` is True, a temporary pair of tex/eps files
1087
1066
are created to allow tex to manage the text layout via the PSFrags
1088
1067
package. These files are processed to yield the final ps or eps file.
1089
1068
1090
- metadata must be a dictionary. Currently, only the value for
1091
- the key 'Creator' is used.
1069
+ The rest of the behavior is as for `._print_figure`.
1092
1070
"""
1093
1071
is_eps = format == 'eps'
1094
- if is_writable_file_like (outfile ):
1095
- title = None
1096
- else :
1097
- try :
1098
- title = os .fspath (outfile )
1099
- except TypeError :
1100
- raise ValueError (
1101
- "outfile must be a path or a file-like object" )
1102
1072
1103
- self .figure .dpi = 72 # ignore the dpi kwarg
1104
1073
width , height = self .figure .get_size_inches ()
1105
1074
xo = 0
1106
1075
yo = 0
@@ -1112,12 +1081,6 @@ def _print_figure_tex(
1112
1081
ury = lly + h
1113
1082
bbox = (llx , lly , urx , ury )
1114
1083
1115
- # generate PostScript code for the figure and store it in a string
1116
- origfacecolor = self .figure .get_facecolor ()
1117
- origedgecolor = self .figure .get_edgecolor ()
1118
- self .figure .set_facecolor (facecolor )
1119
- self .figure .set_edgecolor (edgecolor )
1120
-
1121
1084
if dryrun :
1122
1085
class NullWriter :
1123
1086
def write (self , * args , ** kwargs ):
@@ -1138,35 +1101,13 @@ def write(self, *args, **kwargs):
1138
1101
if dryrun : # return immediately if dryrun (tightbbox=True)
1139
1102
return
1140
1103
1141
- self .figure .set_facecolor (origfacecolor )
1142
- self .figure .set_edgecolor (origedgecolor )
1143
-
1144
- # check for custom metadata
1145
- if metadata is not None and 'Creator' in metadata :
1146
- creator_str = metadata ['Creator' ]
1147
- else :
1148
- creator_str = "matplotlib version " + __version__ + \
1149
- ", http://matplotlib.org/"
1150
-
1151
1104
# write to a temp file, we'll move it to outfile when done
1152
-
1153
1105
with TemporaryDirectory () as tmpdir :
1154
1106
tmpfile = os .path .join (tmpdir , "tmp.ps" )
1155
- # get source date from SOURCE_DATE_EPOCH, if set
1156
- # See https://reproducible-builds.org/specs/source-date-epoch/
1157
- source_date_epoch = os .getenv ("SOURCE_DATE_EPOCH" )
1158
- if source_date_epoch :
1159
- source_date = datetime .datetime .utcfromtimestamp (
1160
- int (source_date_epoch )).strftime ("%a %b %d %H:%M:%S %Y" )
1161
- else :
1162
- source_date = time .ctime ()
1163
1107
pathlib .Path (tmpfile ).write_text (
1164
1108
f"""\
1165
1109
%!PS-Adobe-3.0 EPSF-3.0
1166
- { f'''%%Title: { title }
1167
- ''' if title else "" } \
1168
- %%Creator: { creator_str }
1169
- %%CreationDate: { source_date }
1110
+ { dsc_comments }
1170
1111
%%BoundingBox: { bbox [0 ]} { bbox [1 ]} { bbox [2 ]} { bbox [3 ]}
1171
1112
%%EndComments
1172
1113
%%BeginProlog
@@ -1195,12 +1136,9 @@ def write(self, *args, **kwargs):
1195
1136
paper_width , paper_height = orientation .swap_if_landscape (
1196
1137
self .figure .get_size_inches ())
1197
1138
else :
1198
- temp_papertype = _get_papertype (width , height )
1199
1139
if papertype == 'auto' :
1200
- papertype = temp_papertype
1201
- paper_width , paper_height = papersize [temp_papertype ]
1202
- else :
1203
- paper_width , paper_height = papersize [papertype ]
1140
+ papertype = _get_papertype (width , height )
1141
+ paper_width , paper_height = papersize [papertype ]
1204
1142
1205
1143
texmanager = ps_renderer .get_texmanager ()
1206
1144
font_preamble = texmanager .get_font_preamble ()
0 commit comments