Coverage for /builds/kinetik161/ase/ase/io/eps.py: 100.00%

44 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-12-10 11:04 +0000

1import time 

2 

3from ase.io.utils import PlottingVariables, make_patch_list 

4from ase.utils import writer 

5 

6 

7class EPS(PlottingVariables): 

8 def __init__(self, atoms, 

9 rotation='', radii=None, 

10 bbox=None, colors=None, scale=20, maxwidth=500, 

11 **kwargs): 

12 """Encapsulated PostScript writer. 

13 

14 show_unit_cell: int 

15 0: Don't show unit cell (default). 1: Show unit cell. 

16 2: Show unit cell and make sure all of it is visible. 

17 """ 

18 PlottingVariables.__init__( 

19 self, atoms, rotation=rotation, 

20 radii=radii, bbox=bbox, colors=colors, scale=scale, 

21 maxwidth=maxwidth, 

22 **kwargs) 

23 

24 def write(self, fd): 

25 renderer = self._renderer(fd) 

26 self.write_header(fd) 

27 self.write_body(fd, renderer) 

28 self.write_trailer(fd, renderer) 

29 

30 def write_header(self, fd): 

31 fd.write('%!PS-Adobe-3.0 EPSF-3.0\n') 

32 fd.write('%%Creator: G2\n') 

33 fd.write('%%CreationDate: %s\n' % time.ctime(time.time())) 

34 fd.write('%%Orientation: portrait\n') 

35 bbox = (0, 0, self.w, self.h) 

36 fd.write('%%%%BoundingBox: %d %d %d %d\n' % bbox) 

37 fd.write('%%EndComments\n') 

38 

39 Ndict = len(psDefs) 

40 fd.write('%%BeginProlog\n') 

41 fd.write('/mpldict %d dict def\n' % Ndict) 

42 fd.write('mpldict begin\n') 

43 for d in psDefs: 

44 d = d.strip() 

45 for line in d.split('\n'): 

46 fd.write(line.strip() + '\n') 

47 fd.write('%%EndProlog\n') 

48 

49 fd.write('mpldict begin\n') 

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

51 

52 def _renderer(self, fd): 

53 # Subclass can override 

54 from matplotlib.backends.backend_ps import RendererPS 

55 return RendererPS(self.w, self.h, fd) 

56 

57 def write_body(self, fd, renderer): 

58 patch_list = make_patch_list(self) 

59 for patch in patch_list: 

60 patch.draw(renderer) 

61 

62 def write_trailer(self, fd, renderer): 

63 fd.write('end\n') 

64 fd.write('showpage\n') 

65 

66 

67@writer 

68def write_eps(fd, atoms, **parameters): 

69 EPS(atoms, **parameters).write(fd) 

70 

71 

72# Adapted from Matplotlib 3.7.3 

73 

74# The following Python dictionary psDefs contains the entries for the 

75# PostScript dictionary mpldict. This dictionary implements most of 

76# the matplotlib primitives and some abbreviations. 

77# 

78# References: 

79# https://www.adobe.com/content/dam/acom/en/devnet/actionscript/articles/PLRM.pdf 

80# http://preserve.mactech.com/articles/mactech/Vol.09/09.04/PostscriptTutorial 

81# http://www.math.ubc.ca/people/faculty/cass/graphics/text/www/ 

82# 

83 

84# The usage comments use the notation of the operator summary 

85# in the PostScript Language reference manual. 

86psDefs = [ 

87 # name proc *_d* - 

88 # Note that this cannot be bound to /d, because when embedding a Type3 font 

89 # we may want to define a "d" glyph using "/d{...} d" which would locally 

90 # overwrite the definition. 

91 "/_d { bind def } bind def", 

92 # x y *m* - 

93 "/m { moveto } _d", 

94 # x y *l* - 

95 "/l { lineto } _d", 

96 # x y *r* - 

97 "/r { rlineto } _d", 

98 # x1 y1 x2 y2 x y *c* - 

99 "/c { curveto } _d", 

100 # *cl* - 

101 "/cl { closepath } _d", 

102 # *ce* - 

103 "/ce { closepath eofill } _d", 

104 # w h x y *box* - 

105 """/box { 

106 m 

107 1 index 0 r 

108 0 exch r 

109 neg 0 r 

110 cl 

111 } _d""", 

112 # w h x y *clipbox* - 

113 """/clipbox { 

114 box 

115 clip 

116 newpath 

117 } _d""", 

118 # wx wy llx lly urx ury *setcachedevice* - 

119 "/sc { setcachedevice } _d", 

120]