9
9
from io import StringIO
10
10
import itertools
11
11
import logging
12
+ import math
12
13
import os
13
14
import pathlib
14
15
import shutil
@@ -912,7 +913,7 @@ def print_figure_impl(fh):
912
913
print (f"%%LanguageLevel: 3\n "
913
914
f"{ dsc_comments } \n "
914
915
f"%%Orientation: { orientation .name } \n "
915
- f"{ get_bbox_header (bbox )[ 0 ] } \n "
916
+ f"{ _get_bbox_header (bbox )} \n "
916
917
f"%%EndComments\n " ,
917
918
end = "" , file = fh )
918
919
@@ -1021,7 +1022,7 @@ def _print_figure_tex(
1021
1022
%!PS-Adobe-3.0 EPSF-3.0
1022
1023
%%LanguageLevel: 3
1023
1024
{ dsc_comments }
1024
- { get_bbox_header (bbox )[ 0 ] }
1025
+ { _get_bbox_header (bbox )}
1025
1026
%%EndComments
1026
1027
%%BeginProlog
1027
1028
/mpldict { len (_psDefs )} dict def
@@ -1213,21 +1214,26 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
1213
1214
pstoeps (tmpfile )
1214
1215
1215
1216
1217
+ @_api .deprecated ("3.9" )
1216
1218
def get_bbox_header (lbrt , rotated = False ):
1217
1219
"""
1218
1220
Return a postscript header string for the given bbox lbrt=(l, b, r, t).
1219
1221
Optionally, return rotate command.
1220
1222
"""
1223
+ return _get_bbox_header (lbrt ), (_get_rotate_command (lbrt ) if rotated else "" )
1221
1224
1225
+
1226
+ def _get_bbox_header (lbrt ):
1227
+ """Return a PostScript header string for bounding box *lbrt*=(l, b, r, t)."""
1222
1228
l , b , r , t = lbrt
1223
- if rotated :
1224
- rotate = f"{ l + r :.2f} { 0 :.2f} translate\n 90 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
+
1229
1232
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\n 90 rotate"
1231
1237
1232
1238
1233
1239
def pstoeps (tmpfile , bbox = None , rotated = False ):
@@ -1237,12 +1243,6 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
1237
1243
None, original bbox will be used.
1238
1244
"""
1239
1245
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
-
1246
1246
epsfile = tmpfile + '.eps'
1247
1247
with open (epsfile , 'wb' ) as epsh , open (tmpfile , 'rb' ) as tmph :
1248
1248
write = epsh .write
@@ -1251,7 +1251,7 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
1251
1251
if line .startswith (b'%!PS' ):
1252
1252
write (b"%!PS-Adobe-3.0 EPSF-3.0\n " )
1253
1253
if bbox :
1254
- write (bbox_info .encode ('ascii' ) + b'\n ' )
1254
+ write (_get_bbox_header ( bbox ) .encode ('ascii' ) + b'\n ' )
1255
1255
elif line .startswith (b'%%EndComments' ):
1256
1256
write (line )
1257
1257
write (b'%%BeginProlog\n '
@@ -1263,8 +1263,8 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
1263
1263
b'/setpagedevice {pop} def\n '
1264
1264
b'%%EndProlog\n '
1265
1265
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 ' )
1268
1268
break
1269
1269
elif bbox and line .startswith ((b'%%Bound' , b'%%HiResBound' ,
1270
1270
b'%%DocumentMedia' , b'%%Pages' )):
0 commit comments