Convert the postscript to encapsulated postscript. The bbox of the eps file will be replaced with the given *bbox* argument. If None, original bbox will be used.
(tmpfile, bbox=None, rotated=False)
| 1385 | |
| 1386 | |
| 1387 | def pstoeps(tmpfile, bbox=None, rotated=False): |
| 1388 | """ |
| 1389 | Convert the postscript to encapsulated postscript. The bbox of |
| 1390 | the eps file will be replaced with the given *bbox* argument. If |
| 1391 | None, original bbox will be used. |
| 1392 | """ |
| 1393 | |
| 1394 | epsfile = tmpfile + '.eps' |
| 1395 | with open(epsfile, 'wb') as epsh, open(tmpfile, 'rb') as tmph: |
| 1396 | write = epsh.write |
| 1397 | # Modify the header: |
| 1398 | for line in tmph: |
| 1399 | if line.startswith(b'%!PS'): |
| 1400 | write(b"%!PS-Adobe-3.0 EPSF-3.0\n") |
| 1401 | if bbox: |
| 1402 | write(_get_bbox_header(bbox).encode('ascii') + b'\n') |
| 1403 | elif line.startswith(b'%%EndComments'): |
| 1404 | write(line) |
| 1405 | write(b'%%BeginProlog\n' |
| 1406 | b'save\n' |
| 1407 | b'countdictstack\n' |
| 1408 | b'mark\n' |
| 1409 | b'newpath\n' |
| 1410 | b'/showpage {} def\n' |
| 1411 | b'/setpagedevice {pop} def\n' |
| 1412 | b'%%EndProlog\n' |
| 1413 | b'%%Page 1 1\n') |
| 1414 | if rotated: # The output eps file need to be rotated. |
| 1415 | write(_get_rotate_command(bbox).encode('ascii') + b'\n') |
| 1416 | break |
| 1417 | elif bbox and line.startswith((b'%%Bound', b'%%HiResBound', |
| 1418 | b'%%DocumentMedia', b'%%Pages')): |
| 1419 | pass |
| 1420 | else: |
| 1421 | write(line) |
| 1422 | # Now rewrite the rest of the file, and modify the trailer. |
| 1423 | # This is done in a second loop such that the header of the embedded |
| 1424 | # eps file is not modified. |
| 1425 | for line in tmph: |
| 1426 | if line.startswith(b'%%EOF'): |
| 1427 | write(b'cleartomark\n' |
| 1428 | b'countdictstack\n' |
| 1429 | b'exch sub { end } repeat\n' |
| 1430 | b'restore\n' |
| 1431 | b'showpage\n' |
| 1432 | b'%%EOF\n') |
| 1433 | elif line.startswith(b'%%PageBoundingBox'): |
| 1434 | pass |
| 1435 | else: |
| 1436 | write(line) |
| 1437 | |
| 1438 | os.remove(tmpfile) |
| 1439 | shutil.move(epsfile, tmpfile) |
| 1440 | |
| 1441 | |
| 1442 | FigureManagerPS = FigureManagerBase |
no test coverage detected
searching dependent graphs…