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
@@ -915,7 +916,7 @@ def print_figure_impl(fh):
915
916
print (f"%%LanguageLevel: 3\n "
916
917
f"{ dsc_comments } \n "
917
918
f"%%Orientation: { orientation .name } \n "
918
- f"{ get_bbox_header (bbox )[ 0 ] } \n "
919
+ f"{ _get_bbox_header (bbox )} \n "
919
920
f"%%EndComments\n " ,
920
921
end = "" , file = fh )
921
922
@@ -1024,7 +1025,7 @@ def _print_figure_tex(
1024
1025
%!PS-Adobe-3.0 EPSF-3.0
1025
1026
%%LanguageLevel: 3
1026
1027
{ dsc_comments }
1027
- { get_bbox_header (bbox )[ 0 ] }
1028
+ { _get_bbox_header (bbox )}
1028
1029
%%EndComments
1029
1030
%%BeginProlog
1030
1031
/mpldict { len (_psDefs )} dict def
@@ -1219,21 +1220,26 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
1219
1220
pstoeps (tmpfile )
1220
1221
1221
1222
1223
+ @_api .deprecated ("3.9" )
1222
1224
def get_bbox_header (lbrt , rotated = False ):
1223
1225
"""
1224
1226
Return a postscript header string for the given bbox lbrt=(l, b, r, t).
1225
1227
Optionally, return rotate command.
1226
1228
"""
1229
+ return _get_bbox_header (lbrt ), (_get_rotate_command (lbrt ) if rotated else "" )
1227
1230
1231
+
1232
+ def _get_bbox_header (lbrt ):
1233
+ """Return a PostScript header string for bounding box *lbrt*=(l, b, r, t)."""
1228
1234
l , b , r , t = lbrt
1229
- if rotated :
1230
- rotate = f"{ l + r :.2f} { 0 :.2f} translate\n 90 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
+
1235
1238
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\n 90 rotate"
1237
1243
1238
1244
1239
1245
def pstoeps (tmpfile , bbox = None , rotated = False ):
@@ -1243,12 +1249,6 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
1243
1249
None, original bbox will be used.
1244
1250
"""
1245
1251
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
-
1252
1252
epsfile = tmpfile + '.eps'
1253
1253
with open (epsfile , 'wb' ) as epsh , open (tmpfile , 'rb' ) as tmph :
1254
1254
write = epsh .write
@@ -1257,7 +1257,7 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
1257
1257
if line .startswith (b'%!PS' ):
1258
1258
write (b"%!PS-Adobe-3.0 EPSF-3.0\n " )
1259
1259
if bbox :
1260
- write (bbox_info .encode ('ascii' ) + b'\n ' )
1260
+ write (_get_bbox_header ( bbox ) .encode ('ascii' ) + b'\n ' )
1261
1261
elif line .startswith (b'%%EndComments' ):
1262
1262
write (line )
1263
1263
write (b'%%BeginProlog\n '
@@ -1269,8 +1269,8 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
1269
1269
b'/setpagedevice {pop} def\n '
1270
1270
b'%%EndProlog\n '
1271
1271
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 ' )
1274
1274
break
1275
1275
elif bbox and line .startswith ((b'%%Bound' , b'%%HiResBound' ,
1276
1276
b'%%DocumentMedia' , b'%%Pages' )):
0 commit comments