@@ -264,7 +264,7 @@ def restore_region(self, region):
264
264
265
265
def _print_vector (self , renderer_factory ,
266
266
path_or_stream , * , metadata = None ,
267
- _fixed_72dpi = True ,
267
+ _fixed_72dpi = True , _forced_size = None , _offset = None ,
268
268
** kwargs ):
269
269
_check_print_extra_kwargs (** kwargs )
270
270
dpi = self .figure .get_dpi ()
@@ -273,6 +273,10 @@ def _print_vector(self, renderer_factory,
273
273
draw_raises_done = False
274
274
with cbook .open_file_cm (path_or_stream , "wb" ) as stream :
275
275
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 )
276
280
try :
277
281
# Setting invalid metadata can also throw, in which case the
278
282
# 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, *,
332
336
f"%%Orientation: { orientation } " ]
333
337
if "Title" in metadata :
334
338
dsc_comments .append ("%%Title: {}" .format (metadata .pop ("Title" )))
339
+ fig_wh = self .figure .get_size_inches () * np .array (72 )
335
340
if not is_eps and papertype != "figure" :
336
341
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 )
341
356
if mpl .rcParams ["ps.usedistiller" ]:
342
357
with TemporaryDirectory () as tmp_dirname :
343
358
tmp_name = Path (tmp_dirname , "tmp" )
344
359
print_method (tmp_name , metadata = metadata , ** kwargs )
345
- # Assume we can get away without passing the bbox.
346
360
{"ghostscript" : backend_ps .gs_distill ,
347
361
"xpdf" : backend_ps .xpdf_distill }[
348
362
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 ))
350
366
# If path_or_stream is *already* a text-mode stream then
351
367
# tmp_name needs to be opened in text-mode too.
352
368
with cbook .open_file_cm (path_or_stream , "wb" ) as stream , \
0 commit comments