Debian Codegraph @

texmf-dist/metapost/exteps/exteps.mp

raw
1 % Copyright 2005, 2006 by Palle Jorgensen
2 %
3 % This file is free software; you can redistribute it and/or modify
4 % it under the terms of the GNU General Public License as published by
5 % the Free Software Foundation; either version 2 of the License, or
6 % (at your option) any later version.
7 %
8 % This program is distributed in the hope that it will be useful, but
9 % WITHOUT ANY WARRANTY; without even the implied warranty of
10 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 % General Public License for more details.
12 %
13 % You should have received a copy of the GNU General Public License
14 % along with this program; if not, write to the Free Software
15 % Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 % MA 02111-1307, USA.
17 %
18 % This is version 0.41 Tue Sep 26 2006 Dec
19 %
20 % The module is documentet in exteps.pdf
21
22 picture epspicture;
23 string extra_begineps; extra_begineps = "";
24 string extra_endeps; extra_endeps = "";
25 boolean extepsverbose; extepsverbose = true;
26
27 %% String handling tool
28 string string_split[];
29 def splitstring expr S =
30 begingroup
31 save __splitctr; numeric __splitctr; __splitctr = 0;
32 save __prevchar; string __prevchar, __currentchar; __prevchar = " ";
33 for i = 0 upto infinity:
34 __currentchar := substring(i, i+1) of S;
35 if (__currentchar = " ") and (__prevchar = " "):
36 relax;
37 elseif (__currentchar <> " ") and (__prevchar = " "):
38 string_split[__splitctr] := __currentchar;
39 elseif (__currentchar <> " ") and (__prevchar <> " "):
40 string_split[__splitctr] := string_split[__splitctr] & __currentchar;
41 elseif (__currentchar = " ") and (__prevchar <> " "):
42 __splitctr := __splitctr+1;
43 fi
44 __prevchar := __currentchar;
45 endfor
46 endgroup;
47 enddef;
48 %% End string handling tool
49
50 def begineps text F =
51 begingroup;
52 save file; string file; file = F;
53 save angle; numeric angle; angle = 0;
54 save clipping; boolean clipping; clipping = false;
55 save scale; pair scale; scale = (1,1);
56 save base; pair base; base = origin;
57 save __bbxfound; boolean __bbxfound; __bbxfound = false;
58 save grid; boolean grid; grid = false;
59 save gridstep; numeric gridstep; gridstep = 10;
60 save __base; pair __base;
61 save __eps__currentline; string __eps__currentline;
62 save __bbxline; string __bbxline;
63 save llx, lly, urx, ury; numeric llx, lly, urx, ury;
64 save pct; numeric pct;
65 save width; numeric width;
66 save height; numeric height;
67 save clippath; path clippath;
68 save largefile; boolean largefile; largefile = false;
69 save gridllx; numeric gridllx;
70 save gridlly; numeric gridlly;
71 save gridurx; numeric gridurx;
72 save gridury; numeric gridury;
73 %% Finding the bounding box
74 forever:
75 __eps__currentline := readfrom F;
76 if substring(0,14) of __eps__currentline = "%%BoundingBox:":
77 exitif __eps__currentline = EOF; % PATCH D. Roegel 23-sep-2006
78 __bbxline := substring(14, infinity) of __eps__currentline;
79 __bbxfound := true;
80 splitstring __bbxline;
81 llx = scantokens string_split[0];
82 lly = scantokens string_split[1];
83 urx = scantokens string_split[2];
84 ury = scantokens string_split[3];
85 fi
86 exitif __bbxfound;
87 endfor
88 if not __bbxfound:
89 message "Warning: No bounding box found.";
90 message " Setting bounding box to 0 0 1 1";
91 llx = lly = 0;
92 urx = ury = 1;
93 fi
94 closefrom F;
95 scantokens extra_begineps;
96 __base = -(llx,lly);
97 pct = (urx - llx)/100;
98 %% To ensure the right bounding box of the output file
99 %% a picture with the same size as the eps figure is added.
100 epspicture := nullpicture;
101 clippath = (0,0)--(0,ury-lly)--(urx-llx,ury-lly)--(urx-llx,0)--cycle;
102 setbounds epspicture to clippath;
103 enddef;
104
105 def endeps =
106 %% Calculating scale if width and/or height is known
107 if (known width) and (known height):
108 scale := (width/(urx - llx),height/(ury - lly));
109 elseif known width:
110 scale := (width/(urx - llx),width/(urx - llx));
111 elseif known height:
112 scale := (height/(ury - lly),height/(ury - lly));
113 fi
114 %% The graphics inclusion commands
115 special "gsave";
116 if base <> origin:
117 special decimal.xpart.base & " " & decimal.ypart.base & " translate";
118 fi
119 if angle <> 0:
120 special decimal angle & " rotate";
121 fi
122 if __base <> origin:
123 special decimal.xpart.__base & " " & decimal.ypart.__base & " translate";
124 fi
125 if scale <> (1,1):
126 epspicture := epspicture scaled xpart.scale
127 if xpart.scale <> ypart.scale:
128 yscaled (ypart.scale/xpart.scale)
129 fi;
130 special decimal xpart.scale & " " & decimal ypart.scale & " scale";
131 fi
132 if angle <> 0:
133 epspicture := epspicture rotatedaround(origin)(angle);
134 fi
135 %% Drawing the grid !! After the scaling :-)
136 if unknown gridllx:
137 gridllx = 0;
138 fi
139 if unknown gridlly:
140 gridlly = 0;
141 fi
142 if unknown gridurx:
143 gridurx = (urx - llx)/pct;
144 fi
145 if unknown gridury:
146 gridury = (ury - lly)/pct;
147 fi
148 if grid:
149 save __gridpicture; picture __gridpicture; __gridpicture := nullpicture;
150 for i = gridllx*pct step gridstep*pct until (epsilon + gridurx*pct):
151 addto __gridpicture doublepath (i*xpart.scale,gridlly*pct*ypart.scale)--(i*xpart.scale,gridury*pct*ypart.scale) withpen currentpen;
152 addto __gridpicture also thelabel.bot(((decimal.(i/pct) & "%") infont defaultfont) rotated -90, (i*xpart.scale,gridlly*pct*ypart.scale));
153 endfor
154 for i = gridlly*pct step gridstep*pct until (epsilon + gridury*pct):
155 addto __gridpicture doublepath (gridllx*pct*xpart.scale,i*ypart.scale)--(gridurx*pct*xpart.scale,i*ypart.scale) withpen currentpen;
156 addto __gridpicture also thelabel.lft(((decimal.(i/pct) & "%") infont defaultfont), (gridllx*pct*xpart.scale,i*ypart.scale));
157 endfor
158 if angle <> 0:
159 __gridpicture := __gridpicture rotatedaround(origin)(angle);
160 fi
161 addto epspicture also __gridpicture;
162 fi
163 if clipping:
164 save __clippath; path __clippath;
165 __clippath=clippath shifted (llx,lly);
166 special "newpath " & decimal.xpart.point 0 of __clippath
167 & " " & decimal.ypart.point 0 of __clippath & " moveto";
168 for i = 0 upto length.__clippath-1:
169 special decimal.xpart.postcontrol i of __clippath & " " &
170 decimal.ypart.postcontrol i of __clippath & " " &
171 decimal.xpart.precontrol (i+1) of __clippath & " " &
172 decimal.ypart.precontrol (i+1) of __clippath & " " &
173 decimal.xpart.point (i+1) of __clippath & " " &
174 decimal.ypart.point (i+1) of __clippath & " curveto";
175 endfor;
176 special "closepath clip";
177 fi
178 special "save";
179 special "userdict begin";
180 special "/showpage {} def";
181 special "%%BeginDocument: " & file;
182 if largefile:
183 special "%% MetaPost exteps large file->" & file;
184 if extepsverbose:
185 message "exteps notification: File " & file & " not inserted into " & jobname & "." & decimal.charcode;
186 message " Run 'delfin " & jobname & "." & decimal.charcode & "' to insert " & file;
187 message " This is caused by setting 'largefile:=true'";
188 message "";
189 fi
190 else:
191 if extepsverbose:
192 message "Inserting " & file & " into " & jobname & "." & decimal.charcode;
193 fi
194 forever:
195 __eps__currentline := readfrom file;
196 exitunless __eps__currentline <> EOF;
197 special __eps__currentline;
198 endfor
199 fi
200 special "%%EndDocument: " & file;
201 scantokens extra_endeps;
202 special "end restore";
203 special "grestore";
204 closefrom file;
205 if base <> (0,0):
206 epspicture := epspicture shifted base;
207 fi
208 addto currentpicture also epspicture;
209 endgroup;
210 enddef;
211
212 %% Special drawing commands
213 def epsfill expr c = addto epspicture contour c _op_ enddef;
214
215 def epsdraw expr p =
216 addto epspicture
217 if picture p:
218 also p
219 else:
220 doublepath p withpen currentpen
221 fi
222 _op_
223 enddef;
224
225 def epsfilldraw expr c =
226 addto epspicture contour c withpen currentpen
227 _op_ enddef;
228
229 def epsdrawdot expr z =
230 addto epspicture contour makepath currentpen shifted z
231 _op_ enddef;
232
233 def epslabel = epsdraw thelabel enddef;
234
235 endinput