File: eps.py

package info (click to toggle)
python-ase 3.22.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,344 kB
  • sloc: python: 126,379; xml: 946; makefile: 111; javascript: 47
file content (70 lines) | stat: -rw-r--r-- 2,226 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import time
from ase.utils import writer
from ase.io.utils import PlottingVariables, make_patch_list


class EPS(PlottingVariables):
    def __init__(self, atoms,
                 rotation='', radii=None,
                 bbox=None, colors=None, scale=20, maxwidth=500,
                 **kwargs):
        """Encapsulated PostScript writer.

        show_unit_cell: int
            0: Don't show unit cell (default).  1: Show unit cell.
            2: Show unit cell and make sure all of it is visible.
        """
        PlottingVariables.__init__(
            self, atoms, rotation=rotation,
            radii=radii, bbox=bbox, colors=colors, scale=scale,
            maxwidth=maxwidth,
            **kwargs)

    def write(self, fd):
        renderer = self._renderer(fd)
        self.write_header(fd)
        self.write_body(fd, renderer)
        self.write_trailer(fd, renderer)

    def write_header(self, fd):
        from matplotlib.backends.backend_ps import psDefs

        fd.write('%!PS-Adobe-3.0 EPSF-3.0\n')
        fd.write('%%Creator: G2\n')
        fd.write('%%CreationDate: %s\n' % time.ctime(time.time()))
        fd.write('%%Orientation: portrait\n')
        bbox = (0, 0, self.w, self.h)
        fd.write('%%%%BoundingBox: %d %d %d %d\n' % bbox)
        fd.write('%%EndComments\n')

        Ndict = len(psDefs)
        fd.write('%%BeginProlog\n')
        fd.write('/mpldict %d dict def\n' % Ndict)
        fd.write('mpldict begin\n')
        for d in psDefs:
            d = d.strip()
            for l in d.split('\n'):
                fd.write(l.strip() + '\n')
        fd.write('%%EndProlog\n')

        fd.write('mpldict begin\n')
        fd.write('%d %d 0 0 clipbox\n' % (self.w, self.h))

    def _renderer(self, fd):
        # Subclass can override
        from matplotlib.backends.backend_ps import RendererPS
        return RendererPS(self.w, self.h, fd)

    def write_body(self, fd, renderer):
        patch_list = make_patch_list(self)
        for patch in patch_list:
            patch.draw(renderer)

    def write_trailer(self, fd, renderer):
        fd.write('end\n')
        fd.write('showpage\n')


@writer
def write_eps(fd, atoms, **parameters):
    EPS(atoms, **parameters).write(fd)