Skip to content

Commit 297507b

Browse files
authored
Merge pull request matplotlib#26960 from anntzer/psgbh
Deprecate backend_ps.get_bbox_header, and split it for internal use.
2 parents 56a407c + 6245534 commit 297507b

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
@@ -912,7 +913,7 @@ def print_figure_impl(fh):
912913
print(f"%%LanguageLevel: 3\n"
913914
f"{dsc_comments}\n"
914915
f"%%Orientation: {orientation.name}\n"
915-
f"{get_bbox_header(bbox)[0]}\n"
916+
f"{_get_bbox_header(bbox)}\n"
916917
f"%%EndComments\n",
917918
end="", file=fh)
918919

@@ -1021,7 +1022,7 @@ def _print_figure_tex(
10211022
%!PS-Adobe-3.0 EPSF-3.0
10221023
%%LanguageLevel: 3
10231024
{dsc_comments}
1024-
{get_bbox_header(bbox)[0]}
1025+
{_get_bbox_header(bbox)}
10251026
%%EndComments
10261027
%%BeginProlog
10271028
/mpldict {len(_psDefs)} dict def
@@ -1213,21 +1214,26 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
12131214
pstoeps(tmpfile)
12141215

12151216

1217+
@_api.deprecated("3.9")
12161218
def get_bbox_header(lbrt, rotated=False):
12171219
"""
12181220
Return a postscript header string for the given bbox lbrt=(l, b, r, t).
12191221
Optionally, return rotate command.
12201222
"""
1223+
return _get_bbox_header(lbrt), (_get_rotate_command(lbrt) if rotated else "")
12211224

1225+
1226+
def _get_bbox_header(lbrt):
1227+
"""Return a PostScript header string for bounding box *lbrt*=(l, b, r, t)."""
12221228
l, b, r, t = lbrt
1223-
if rotated:
1224-
rotate = f"{l+r:.2f} {0:.2f} translate\n90 rotate"
1225-
else:
1226-
rotate = ""
1227-
bbox_info = '%%%%BoundingBox: %d %d %d %d' % (l, b, np.ceil(r), np.ceil(t))
1228-
hires_bbox_info = f'%%HiResBoundingBox: {l:.6f} {b:.6f} {r:.6f} {t:.6f}'
1229+
return (f"%%BoundingBox: {int(l)} {int(b)} {math.ceil(r)} {math.ceil(t)}\n"
1230+
f"%%HiResBoundingBox: {l:.6f} {b:.6f} {r:.6f} {t:.6f}")
1231+
12291232

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

12321238

12331239
def pstoeps(tmpfile, bbox=None, rotated=False):
@@ -1237,12 +1243,6 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
12371243
None, original bbox will be used.
12381244
"""
12391245

1240-
# if rotated==True, the output eps file need to be rotated
1241-
if bbox:
1242-
bbox_info, rotate = get_bbox_header(bbox, rotated=rotated)
1243-
else:
1244-
bbox_info, rotate = None, None
1245-
12461246
epsfile = tmpfile + '.eps'
12471247
with open(epsfile, 'wb') as epsh, open(tmpfile, 'rb') as tmph:
12481248
write = epsh.write
@@ -1251,7 +1251,7 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
12511251
if line.startswith(b'%!PS'):
12521252
write(b"%!PS-Adobe-3.0 EPSF-3.0\n")
12531253
if bbox:
1254-
write(bbox_info.encode('ascii') + b'\n')
1254+
write(_get_bbox_header(bbox).encode('ascii') + b'\n')
12551255
elif line.startswith(b'%%EndComments'):
12561256
write(line)
12571257
write(b'%%BeginProlog\n'
@@ -1263,8 +1263,8 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
12631263
b'/setpagedevice {pop} def\n'
12641264
b'%%EndProlog\n'
12651265
b'%%Page 1 1\n')
1266-
if rotate:
1267-
write(rotate.encode('ascii') + b'\n')
1266+
if rotated: # The output eps file need to be rotated.
1267+
write(_get_rotate_command(bbox).encode('ascii') + b'\n')
12681268
break
12691269
elif bbox and line.startswith((b'%%Bound', b'%%HiResBound',
12701270
b'%%DocumentMedia', b'%%Pages')):

0 commit comments

Comments
 (0)