@@ -264,7 +264,7 @@ def restore_region(self, region):
264264
265265 def _print_vector (self , renderer_factory ,
266266 path_or_stream , * , metadata = None ,
267- _fixed_72dpi = True ,
267+ _fixed_72dpi = True , _forced_size = None , _offset = None ,
268268 ** kwargs ):
269269 _check_print_extra_kwargs (** kwargs )
270270 dpi = self .figure .get_dpi ()
@@ -273,6 +273,10 @@ def _print_vector(self, renderer_factory,
273273 draw_raises_done = False
274274 with cbook .open_file_cm (path_or_stream , "wb" ) as stream :
275275 renderer = renderer_factory (stream , * self .figure .bbox .size , dpi )
276+ if _forced_size is not None :
277+ renderer ._set_size (* _forced_size , dpi )
278+ if _offset is not None :
279+ renderer ._set_init_translation (* _offset )
276280 try :
277281 # Setting invalid metadata can also throw, in which case the
278282 # rendered needs to be _finish()ed (to avoid later writing to a
@@ -332,21 +336,33 @@ def _print_ps_impl(self, is_eps, path_or_stream, *,
332336 f"%%Orientation: { orientation } " ]
333337 if "Title" in metadata :
334338 dsc_comments .append ("%%Title: {}" .format (metadata .pop ("Title" )))
339+ fig_wh = self .figure .get_size_inches () * np .array (72 )
335340 if not is_eps and papertype != "figure" :
336341 dsc_comments .append (f"%%DocumentPaperSizes: { papertype } " )
337- print_method = partial (self ._print_vector ,
338- GraphicsContextRendererCairo ._for_eps_output
339- if is_eps else
340- GraphicsContextRendererCairo ._for_ps_output )
342+ paper_wh = backend_ps .papersize [papertype ] * np .array (72 )
343+ if orientation == "landscape" :
344+ paper_wh = paper_wh [::- 1 ]
345+ fig_wh = fig_wh [::- 1 ]
346+ offset = (paper_wh - fig_wh ) / 2 * [1 , - 1 ]
347+ # FIXME: We should set the init transform, including the rotation
348+ # for landscape orientation, instead of just the offset.
349+ else :
350+ paper_wh = offset = None
351+ print_method = partial (
352+ self ._print_vector ,
353+ GraphicsContextRendererCairo ._for_eps_output if is_eps else
354+ GraphicsContextRendererCairo ._for_ps_output ,
355+ _forced_size = paper_wh , _offset = offset )
341356 if mpl .rcParams ["ps.usedistiller" ]:
342357 with TemporaryDirectory () as tmp_dirname :
343358 tmp_name = Path (tmp_dirname , "tmp" )
344359 print_method (tmp_name , metadata = metadata , ** kwargs )
345- # Assume we can get away without passing the bbox.
346360 {"ghostscript" : backend_ps .gs_distill ,
347361 "xpdf" : backend_ps .xpdf_distill }[
348362 mpl .rcParams ["ps.usedistiller" ]](
349- str (tmp_name ), is_eps , ptype = papertype )
363+ str (tmp_name ), is_eps , ptype = papertype ,
364+ # Assume we can get away with just bbox width/height.
365+ bbox = (None , None , * fig_wh ))
350366 # If path_or_stream is *already* a text-mode stream then
351367 # tmp_name needs to be opened in text-mode too.
352368 with cbook .open_file_cm (path_or_stream , "wb" ) as stream , \
0 commit comments