@@ -764,10 +764,30 @@ def print_ps(self, outfile, *args, **kwargs):
764
764
def print_eps (self , outfile , * args , ** kwargs ):
765
765
return self ._print_ps (outfile , 'eps' , * args , ** kwargs )
766
766
767
- def _print_ps (self , outfile , format , * args ,
768
- papertype = None , dpi = 72 , facecolor = 'w' , edgecolor = 'w' ,
769
- orientation = 'portrait' ,
770
- ** kwargs ):
767
+ def _print_ps (
768
+ self , outfile , format , * args ,
769
+ dpi = 72 , metadata = None , papertype = None , orientation = 'portrait' ,
770
+ ** kwargs ):
771
+
772
+ self .figure .set_dpi (72 ) # Override the dpi kwarg
773
+
774
+ dsc_comments = {}
775
+ if isinstance (outfile , (str , os .PathLike )):
776
+ dsc_comments ["Title" ] = \
777
+ os .fspath (outfile ).encode ("ascii" , "replace" ).decode ("ascii" )
778
+ dsc_comments ["Creator" ] = (metadata or {}).get (
779
+ "Creator" ,
780
+ f"matplotlib version { mpl .__version__ } , http://matplotlib.org/" )
781
+ # See https://reproducible-builds.org/specs/source-date-epoch/
782
+ source_date_epoch = os .getenv ("SOURCE_DATE_EPOCH" )
783
+ dsc_comments ["CreationDate" ] = (
784
+ datetime .datetime .utcfromtimestamp (
785
+ int (source_date_epoch )).strftime ("%a %b %d %H:%M:%S %Y" )
786
+ if source_date_epoch
787
+ else time .ctime ())
788
+ dsc_comments = "\n " .join (
789
+ f"%%{ k } : { v } " for k , v in dsc_comments .items ())
790
+
771
791
if papertype is None :
772
792
papertype = mpl .rcParams ['ps.papersize' ]
773
793
papertype = papertype .lower ()
@@ -776,42 +796,29 @@ def _print_ps(self, outfile, format, *args,
776
796
orientation = cbook ._check_getitem (
777
797
_Orientation , orientation = orientation .lower ())
778
798
779
- self .figure .set_dpi (72 ) # Override the dpi kwarg
780
-
781
799
printer = (self ._print_figure_tex
782
800
if mpl .rcParams ['text.usetex' ] else
783
801
self ._print_figure )
784
- printer (outfile , format , dpi , facecolor , edgecolor ,
785
- orientation , papertype , ** kwargs )
802
+ printer (outfile , format , dpi = dpi , dsc_comments = dsc_comments ,
803
+ orientation = orientation , papertype = papertype , ** kwargs )
786
804
787
805
@cbook ._delete_parameter ("3.2" , "dryrun" )
788
806
def _print_figure (
789
- self , outfile , format , dpi , facecolor , edgecolor ,
790
- orientation , papertype , * ,
791
- metadata = None , dryrun = False , bbox_inches_restore = None , ** kwargs ):
807
+ self , outfile , format , * ,
808
+ dpi , dsc_comments , orientation , papertype ,
809
+ dryrun = False , bbox_inches_restore = None , ** kwargs ):
792
810
"""
793
- Render the figure to hardcopy. Set the figure patch face and
794
- edge colors. This is useful because some of the GUIs have a
795
- gray figure face color background and you'll probably want to
796
- override this on hardcopy
797
-
798
- If outfile is a string, it is interpreted as a file name.
799
- If the extension matches .ep* write encapsulated postscript,
800
- otherwise write a stand-alone PostScript file.
801
-
802
- If outfile is a file object, a stand-alone PostScript file is
803
- written into this file object.
811
+ Render the figure to a filesystem path or a file-like object.
804
812
805
- metadata must be a dictionary. Currently, only the value for
806
- the key 'Creator' is used.
813
+ Parameters are as for `.print_figure`, except that *dsc_comments* is a
814
+ all string containing Document Structuring Convention comments,
815
+ generated from the *metadata* parameter to `.print_figure`.
807
816
"""
808
817
is_eps = format == 'eps'
809
818
if isinstance (outfile , (str , os .PathLike )):
810
- outfile = title = os .fspath (outfile )
811
- title = title .encode ("ascii" , "replace" ).decode ("ascii" )
819
+ outfile = os .fspath (outfile )
812
820
passed_in_file_object = False
813
821
elif is_writable_file_like (outfile ):
814
- title = None
815
822
passed_in_file_object = True
816
823
else :
817
824
raise ValueError ("outfile must be a path or a file-like object" )
@@ -848,12 +855,6 @@ def _print_figure(
848
855
rotation = 90
849
856
bbox = (llx , lly , urx , ury )
850
857
851
- # generate PostScript code for the figure and store it in a string
852
- origfacecolor = self .figure .get_facecolor ()
853
- origedgecolor = self .figure .get_edgecolor ()
854
- self .figure .set_facecolor (facecolor )
855
- self .figure .set_edgecolor (edgecolor )
856
-
857
858
if dryrun :
858
859
class NullWriter :
859
860
def write (self , * args , ** kwargs ):
@@ -874,16 +875,6 @@ def write(self, *args, **kwargs):
874
875
if dryrun : # return immediately if dryrun (tightbbox=True)
875
876
return
876
877
877
- self .figure .set_facecolor (origfacecolor )
878
- self .figure .set_edgecolor (origedgecolor )
879
-
880
- # check for custom metadata
881
- if metadata is not None and 'Creator' in metadata :
882
- creator_str = metadata ['Creator' ]
883
- else :
884
- creator_str = \
885
- f"matplotlib version { mpl .__version__ } , http://matplotlib.org/"
886
-
887
878
def print_figure_impl (fh ):
888
879
# write the PostScript headers
889
880
if is_eps :
@@ -893,18 +884,7 @@ def print_figure_impl(fh):
893
884
f"%%DocumentPaperSizes: { papertype } \n "
894
885
f"%%Pages: 1\n " ,
895
886
end = "" , file = fh )
896
- if title :
897
- print ("%%Title: " + title , file = fh )
898
- # get source date from SOURCE_DATE_EPOCH, if set
899
- # See https://reproducible-builds.org/specs/source-date-epoch/
900
- source_date_epoch = os .getenv ("SOURCE_DATE_EPOCH" )
901
- if source_date_epoch :
902
- source_date = datetime .datetime .utcfromtimestamp (
903
- int (source_date_epoch )).strftime ("%a %b %d %H:%M:%S %Y" )
904
- else :
905
- source_date = time .ctime ()
906
- print (f"%%Creator: { creator_str } \n "
907
- f"%%CreationDate: { source_date } \n "
887
+ print (f"{ dsc_comments } \n "
908
888
f"%%Orientation: { orientation .name } \n "
909
889
f"%%BoundingBox: { bbox [0 ]} { bbox [1 ]} { bbox [2 ]} { bbox [3 ]} \n "
910
890
f"%%EndComments\n " ,
@@ -1008,28 +988,18 @@ def print_figure_impl(fh):
1008
988
1009
989
@cbook ._delete_parameter ("3.2" , "dryrun" )
1010
990
def _print_figure_tex (
1011
- self , outfile , format , dpi , facecolor , edgecolor ,
1012
- orientation , papertype , * ,
1013
- metadata = None , dryrun = False , bbox_inches_restore = None , ** kwargs ):
991
+ self , outfile , format , * ,
992
+ dpi , dsc_comments , orientation , papertype ,
993
+ dryrun = False , bbox_inches_restore = None , ** kwargs ):
1014
994
"""
1015
- If text.usetex is True in rc , a temporary pair of tex/eps files
995
+ If :rc:` text.usetex` is True, a temporary pair of tex/eps files
1016
996
are created to allow tex to manage the text layout via the PSFrags
1017
997
package. These files are processed to yield the final ps or eps file.
1018
998
1019
- metadata must be a dictionary. Currently, only the value for
1020
- the key 'Creator' is used.
999
+ The rest of the behavior is as for `._print_figure`.
1021
1000
"""
1022
1001
is_eps = format == 'eps'
1023
- if is_writable_file_like (outfile ):
1024
- title = None
1025
- else :
1026
- try :
1027
- title = os .fspath (outfile )
1028
- except TypeError as err :
1029
- raise ValueError (
1030
- "outfile must be a path or a file-like object" ) from err
1031
1002
1032
- self .figure .dpi = 72 # ignore the dpi kwarg
1033
1003
width , height = self .figure .get_size_inches ()
1034
1004
xo = 0
1035
1005
yo = 0
@@ -1041,12 +1011,6 @@ def _print_figure_tex(
1041
1011
ury = lly + h
1042
1012
bbox = (llx , lly , urx , ury )
1043
1013
1044
- # generate PostScript code for the figure and store it in a string
1045
- origfacecolor = self .figure .get_facecolor ()
1046
- origedgecolor = self .figure .get_edgecolor ()
1047
- self .figure .set_facecolor (facecolor )
1048
- self .figure .set_edgecolor (edgecolor )
1049
-
1050
1014
if dryrun :
1051
1015
class NullWriter :
1052
1016
def write (self , * args , ** kwargs ):
@@ -1067,35 +1031,13 @@ def write(self, *args, **kwargs):
1067
1031
if dryrun : # return immediately if dryrun (tightbbox=True)
1068
1032
return
1069
1033
1070
- self .figure .set_facecolor (origfacecolor )
1071
- self .figure .set_edgecolor (origedgecolor )
1072
-
1073
- # check for custom metadata
1074
- if metadata is not None and 'Creator' in metadata :
1075
- creator_str = metadata ['Creator' ]
1076
- else :
1077
- creator_str = \
1078
- f"matplotlib version { mpl .__version__ } , http://matplotlib.org/"
1079
-
1080
1034
# write to a temp file, we'll move it to outfile when done
1081
-
1082
1035
with TemporaryDirectory () as tmpdir :
1083
1036
tmpfile = os .path .join (tmpdir , "tmp.ps" )
1084
- # get source date from SOURCE_DATE_EPOCH, if set
1085
- # See https://reproducible-builds.org/specs/source-date-epoch/
1086
- source_date_epoch = os .getenv ("SOURCE_DATE_EPOCH" )
1087
- if source_date_epoch :
1088
- source_date = datetime .datetime .utcfromtimestamp (
1089
- int (source_date_epoch )).strftime ("%a %b %d %H:%M:%S %Y" )
1090
- else :
1091
- source_date = time .ctime ()
1092
1037
pathlib .Path (tmpfile ).write_text (
1093
1038
f"""\
1094
1039
%!PS-Adobe-3.0 EPSF-3.0
1095
- { f'''%%Title: { title }
1096
- ''' if title else "" } \
1097
- %%Creator: { creator_str }
1098
- %%CreationDate: { source_date }
1040
+ { dsc_comments }
1099
1041
%%BoundingBox: { bbox [0 ]} { bbox [1 ]} { bbox [2 ]} { bbox [3 ]}
1100
1042
%%EndComments
1101
1043
%%BeginProlog
@@ -1124,12 +1066,9 @@ def write(self, *args, **kwargs):
1124
1066
paper_width , paper_height = orientation .swap_if_landscape (
1125
1067
self .figure .get_size_inches ())
1126
1068
else :
1127
- temp_papertype = _get_papertype (width , height )
1128
1069
if papertype == 'auto' :
1129
- papertype = temp_papertype
1130
- paper_width , paper_height = papersize [temp_papertype ]
1131
- else :
1132
- paper_width , paper_height = papersize [papertype ]
1070
+ papertype = _get_papertype (width , height )
1071
+ paper_width , paper_height = papersize [papertype ]
1133
1072
1134
1073
texmanager = ps_renderer .get_texmanager ()
1135
1074
font_preamble = texmanager .get_font_preamble ()
0 commit comments