Skip to content

Commit 6245534

Browse files
committed
Deprecate backend_ps.get_bbox_header, and split it for internal use.
It's clearly an internal helper, and the two parts (the actual bbox header and the rotation command) don't really benefit from being smushed together, so make the function private and split the two parts.
1 parent e237bbd commit 6245534

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``backend_ps.get_bbox_header``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
... is deprecated, as it is considered an internal helper.

lib/matplotlib/backends/backend_ps.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from io import StringIO
1010
import itertools
1111
import logging
12+
import math
1213
import os
1314
import pathlib
1415
import shutil
@@ -915,7 +916,7 @@ def print_figure_impl(fh):
915916
print(f"%%LanguageLevel: 3\n"
916917
f"{dsc_comments}\n"
917918
f"%%Orientation: {orientation.name}\n"
918-
f"{get_bbox_header(bbox)[0]}\n"
919+
f"{_get_bbox_header(bbox)}\n"
919920
f"%%EndComments\n",
920921
end="", file=fh)
921922

@@ -1024,7 +1025,7 @@ def _print_figure_tex(
10241025
%!PS-Adobe-3.0 EPSF-3.0
10251026
%%LanguageLevel: 3
10261027
{dsc_comments}
1027-
{get_bbox_header(bbox)[0]}
1028+
{_get_bbox_header(bbox)}
10281029
%%EndComments
10291030
%%BeginProlog
10301031
/mpldict {len(_psDefs)} dict def
@@ -1219,21 +1220,26 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
12191220
pstoeps(tmpfile)
12201221

12211222

1223+
@_api.deprecated("3.9")
12221224
def get_bbox_header(lbrt, rotated=False):
12231225
"""
12241226
Return a postscript header string for the given bbox lbrt=(l, b, r, t).
12251227
Optionally, return rotate command.
12261228
"""
1229+
return _get_bbox_header(lbrt), (_get_rotate_command(lbrt) if rotated else "")
12271230

1231+
1232+
def _get_bbox_header(lbrt):
1233+
"""Return a PostScript header string for bounding box *lbrt*=(l, b, r, t)."""
12281234
l, b, r, t = lbrt
1229-
if rotated:
1230-
rotate = f"{l+r:.2f} {0:.2f} translate\n90 rotate"
1231-
else:
1232-
rotate = ""
1233-
bbox_info = '%%%%BoundingBox: %d %d %d %d' % (l, b, np.ceil(r), np.ceil(t))
1234-
hires_bbox_info = f'%%HiResBoundingBox: {l:.6f} {b:.6f} {r:.6f} {t:.6f}'
1235+
return (f"%%BoundingBox: {int(l)} {int(b)} {math.ceil(r)} {math.ceil(t)}\n"
1236+
f"%%HiResBoundingBox: {l:.6f} {b:.6f} {r:.6f} {t:.6f}")
1237+
12351238

1236-
return '\n'.join([bbox_info, hires_bbox_info]), rotate
1239+
def _get_rotate_command(lbrt):
1240+
"""Return a PostScript 90° rotation command for bounding box *lbrt*=(l, b, r, t)."""
1241+
l, b, r, t = lbrt
1242+
return f"{l+r:.2f} {0:.2f} translate\n90 rotate"
12371243

12381244

12391245
def pstoeps(tmpfile, bbox=None, rotated=False):
@@ -1243,12 +1249,6 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
12431249
None, original bbox will be used.
12441250
"""
12451251

1246-
# if rotated==True, the output eps file need to be rotated
1247-
if bbox:
1248-
bbox_info, rotate = get_bbox_header(bbox, rotated=rotated)
1249-
else:
1250-
bbox_info, rotate = None, None
1251-
12521252
epsfile = tmpfile + '.eps'
12531253
with open(epsfile, 'wb') as epsh, open(tmpfile, 'rb') as tmph:
12541254
write = epsh.write
@@ -1257,7 +1257,7 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
12571257
if line.startswith(b'%!PS'):
12581258
write(b"%!PS-Adobe-3.0 EPSF-3.0\n")
12591259
if bbox:
1260-
write(bbox_info.encode('ascii') + b'\n')
1260+
write(_get_bbox_header(bbox).encode('ascii') + b'\n')
12611261
elif line.startswith(b'%%EndComments'):
12621262
write(line)
12631263
write(b'%%BeginProlog\n'
@@ -1269,8 +1269,8 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
12691269
b'/setpagedevice {pop} def\n'
12701270
b'%%EndProlog\n'
12711271
b'%%Page 1 1\n')
1272-
if rotate:
1273-
write(rotate.encode('ascii') + b'\n')
1272+
if rotated: # The output eps file need to be rotated.
1273+
write(_get_rotate_command(bbox).encode('ascii') + b'\n')
12741274
break
12751275
elif bbox and line.startswith((b'%%Bound', b'%%HiResBound',
12761276
b'%%DocumentMedia', b'%%Pages')):

0 commit comments

Comments
 (0)