Coverage for ase/io/eps.py: 100.00%
44 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1import time
3from ase.io.utils import PlottingVariables, make_patch_list
4from ase.utils import writer
7class EPS(PlottingVariables):
8 def __init__(
9 self,
10 atoms,
11 rotation='',
12 radii=None,
13 bbox=None,
14 colors=None,
15 scale=20,
16 maxwidth=500,
17 **kwargs,
18 ):
19 """Encapsulated PostScript writer.
21 show_unit_cell: int
22 0: Don't show unit cell (default). 1: Show unit cell.
23 2: Show unit cell and make sure all of it is visible.
24 """
25 PlottingVariables.__init__(
26 self,
27 atoms,
28 rotation=rotation,
29 radii=radii,
30 bbox=bbox,
31 colors=colors,
32 scale=scale,
33 maxwidth=maxwidth,
34 **kwargs,
35 )
37 def write(self, fd):
38 renderer = self._renderer(fd)
39 self.write_header(fd)
40 self.write_body(fd, renderer)
41 self.write_trailer(fd, renderer)
43 def write_header(self, fd):
44 fd.write('%!PS-Adobe-3.0 EPSF-3.0\n')
45 fd.write('%%Creator: G2\n')
46 fd.write('%%CreationDate: %s\n' % time.ctime(time.time()))
47 fd.write('%%Orientation: portrait\n')
48 bbox = (0, 0, self.w, self.h)
49 fd.write('%%%%BoundingBox: %d %d %d %d\n' % bbox)
50 fd.write('%%EndComments\n')
52 Ndict = len(psDefs)
53 fd.write('%%BeginProlog\n')
54 fd.write('/mpldict %d dict def\n' % Ndict)
55 fd.write('mpldict begin\n')
56 for d in psDefs:
57 d = d.strip()
58 for line in d.split('\n'):
59 fd.write(line.strip() + '\n')
60 fd.write('%%EndProlog\n')
62 fd.write('mpldict begin\n')
63 fd.write('%d %d 0 0 clipbox\n' % (self.w, self.h))
65 def _renderer(self, fd):
66 # Subclass can override
67 from matplotlib.backends.backend_ps import RendererPS
69 return RendererPS(self.w, self.h, fd)
71 def write_body(self, fd, renderer):
72 patch_list = make_patch_list(self)
73 for patch in patch_list:
74 patch.draw(renderer)
76 def write_trailer(self, fd, renderer):
77 fd.write('end\n')
78 fd.write('showpage\n')
81@writer
82def write_eps(fd, atoms, **parameters):
83 EPS(atoms, **parameters).write(fd)
86# Adapted from Matplotlib 3.7.3
88# The following Python dictionary psDefs contains the entries for the
89# PostScript dictionary mpldict. This dictionary implements most of
90# the matplotlib primitives and some abbreviations.
91#
92# References:
93# https://www.adobe.com/content/dam/acom/en/devnet/actionscript/articles/PLRM.pdf
94# http://preserve.mactech.com/articles/mactech/Vol.09/09.04/PostscriptTutorial
95# http://www.math.ubc.ca/people/faculty/cass/graphics/text/www/
96#
98# The usage comments use the notation of the operator summary
99# in the PostScript Language reference manual.
100psDefs = [
101 # name proc *_d* -
102 # Note that this cannot be bound to /d, because when embedding a Type3 font
103 # we may want to define a "d" glyph using "/d{...} d" which would locally
104 # overwrite the definition.
105 '/_d { bind def } bind def',
106 # x y *m* -
107 '/m { moveto } _d',
108 # x y *l* -
109 '/l { lineto } _d',
110 # x y *r* -
111 '/r { rlineto } _d',
112 # x1 y1 x2 y2 x y *c* -
113 '/c { curveto } _d',
114 # *cl* -
115 '/cl { closepath } _d',
116 # *ce* -
117 '/ce { closepath eofill } _d',
118 # w h x y *box* -
119 """/box {
120 m
121 1 index 0 r
122 0 exch r
123 neg 0 r
124 cl
125 } _d""",
126 # w h x y *clipbox* -
127 """/clipbox {
128 box
129 clip
130 newpath
131 } _d""",
132 # wx wy llx lly urx ury *setcachedevice* -
133 '/sc { setcachedevice } _d',
134]