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
@@ -904,7 +905,7 @@ def print_figure_impl(fh):
904
905
print (f"%%LanguageLevel: 3\n "
905
906
f"{ dsc_comments } \n "
906
907
f"%%Orientation: { orientation .name } \n "
907
- f"{ get_bbox_header (bbox )[ 0 ] } \n "
908
+ f"{ _get_bbox_header (bbox )} \n "
908
909
f"%%EndComments\n " ,
909
910
end = "" , file = fh )
910
911
@@ -1013,7 +1014,7 @@ def _print_figure_tex(
1013
1014
%!PS-Adobe-3.0 EPSF-3.0
1014
1015
%%LanguageLevel: 3
1015
1016
{ dsc_comments }
1016
- { get_bbox_header (bbox )[ 0 ] }
1017
+ { _get_bbox_header (bbox )}
1017
1018
%%EndComments
1018
1019
%%BeginProlog
1019
1020
/mpldict { len (_psDefs )} dict def
@@ -1205,21 +1206,26 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
1205
1206
pstoeps (tmpfile )
1206
1207
1207
1208
1209
+ @_api .deprecated ("3.9" )
1208
1210
def get_bbox_header (lbrt , rotated = False ):
1209
1211
"""
1210
1212
Return a postscript header string for the given bbox lbrt=(l, b, r, t).
1211
1213
Optionally, return rotate command.
1212
1214
"""
1215
+ return _get_bbox_header (lbrt ), (_get_rotate_command (lbrt ) if rotated else "" )
1213
1216
1217
+
1218
+ def _get_bbox_header (lbrt ):
1219
+ """Return a PostScript header string for bounding box *lbrt*=(l, b, r, t)."""
1214
1220
l , b , r , t = lbrt
1215
- if rotated :
1216
- rotate = f"{ l + r :.2f} { 0 :.2f} translate\n 90 rotate"
1217
- else :
1218
- rotate = ""
1219
- bbox_info = '%%%%BoundingBox: %d %d %d %d' % (l , b , np .ceil (r ), np .ceil (t ))
1220
- hires_bbox_info = f'%%HiResBoundingBox: { l :.6f} { b :.6f} { r :.6f} { t :.6f} '
1221
+ return (f"%%BoundingBox: { int (l )} { int (b )} { math .ceil (r )} { math .ceil (t )} \n "
1222
+ f"%%HiResBoundingBox: { l :.6f} { b :.6f} { r :.6f} { t :.6f} " )
1223
+
1221
1224
1222
- return '\n ' .join ([bbox_info , hires_bbox_info ]), rotate
1225
+ def _get_rotate_command (lbrt ):
1226
+ """Return a PostScript 90° rotation command for bounding box *lbrt*=(l, b, r, t)."""
1227
+ l , b , r , t = lbrt
1228
+ return f"{ l + r :.2f} { 0 :.2f} translate\n 90 rotate"
1223
1229
1224
1230
1225
1231
def pstoeps (tmpfile , bbox = None , rotated = False ):
@@ -1229,12 +1235,6 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
1229
1235
None, original bbox will be used.
1230
1236
"""
1231
1237
1232
- # if rotated==True, the output eps file need to be rotated
1233
- if bbox :
1234
- bbox_info , rotate = get_bbox_header (bbox , rotated = rotated )
1235
- else :
1236
- bbox_info , rotate = None , None
1237
-
1238
1238
epsfile = tmpfile + '.eps'
1239
1239
with open (epsfile , 'wb' ) as epsh , open (tmpfile , 'rb' ) as tmph :
1240
1240
write = epsh .write
@@ -1243,7 +1243,7 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
1243
1243
if line .startswith (b'%!PS' ):
1244
1244
write (b"%!PS-Adobe-3.0 EPSF-3.0\n " )
1245
1245
if bbox :
1246
- write (bbox_info .encode ('ascii' ) + b'\n ' )
1246
+ write (_get_bbox_header ( bbox ) .encode ('ascii' ) + b'\n ' )
1247
1247
elif line .startswith (b'%%EndComments' ):
1248
1248
write (line )
1249
1249
write (b'%%BeginProlog\n '
@@ -1255,8 +1255,8 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
1255
1255
b'/setpagedevice {pop} def\n '
1256
1256
b'%%EndProlog\n '
1257
1257
b'%%Page 1 1\n ' )
1258
- if rotate :
1259
- write (rotate .encode ('ascii' ) + b'\n ' )
1258
+ if rotated : # The output eps file need to be rotated.
1259
+ write (_get_rotate_command ( bbox ) .encode ('ascii' ) + b'\n ' )
1260
1260
break
1261
1261
elif bbox and line .startswith ((b'%%Bound' , b'%%HiResBound' ,
1262
1262
b'%%DocumentMedia' , b'%%Pages' )):
0 commit comments