Debian Codegraph @

texmf-dist/metapost/base/boxes.mp

raw
1 % $Id: boxes.mp,v 1.5 2005/02/25 11:28:56 taco Exp $
2 % Public domain.
3
4 % Macros for boxes
5
6
7 % Find the length of the prefix of string s for which cond is true for each
8 % character c of the prefix
9 vardef str_prefix(expr s)(text cond) =
10 save i_, c; string c;
11 i_ = 0;
12 forever:
13 c := substring (i_,i_+1) of s;
14 exitunless cond;
15 exitif incr i_=length s;
16 endfor
17 i_
18 enddef;
19
20 % Take a string returned by the str operator and return the same string
21 % with explicit numeric subscripts replaced by generic subscript symbols [].
22 %
23 % Fixed version supplied by Eddie Kohler
24
25 vardef generisize (expr ss) =
26 save res, s, stest, l; string res, s, stest;
27 res = ""; % result so far
28 s = ss; % left to process
29 forever: exitif s="";
30 l := str_prefix(s, (c<>"[") and ((c<"0") or (c>"9")));
31 res := res & substring (0,l) of s;
32 s := substring (l,infinity) of s;
33 if s<>"":
34 if (s>="[") and (length s > 1):
35 stest := substring (1,2) of s;
36 if stest="[":
37 l := 2;
38 res := res & "[[";
39 else:
40 l := 1 + str_prefix(s, c<>"]");
41 res := res & "[]";
42 fi
43 else:
44 res := res & "[]";
45 l := str_prefix(s, (c=".") or ("0"<=c) and (c<="9"));
46 fi
47 s := substring(l,infinity) of s;
48 fi
49 endfor
50 res
51 enddef;
52
53
54 % Make sure the string _n_gen_ is generisize(_n_):
55 vardef set_n_gen_ =
56 if _n_ <> _n_cur_:
57 _n_cur_:=_n_;
58 _n_gen_:=generisize(_n_);
59 fi
60 enddef;
61
62 string _n_, _n_cur_, _n_gen_;
63 _n_cur_ := "]"; % this won't match _n_
64
65
66 % Given a type t and list of variable names vars, make sure that they are
67 % of type t and redeclare them as necessary. In the vars list _n represents
68 % scantokens _n_, a suffix that might contain numeric subscripts.
69 % This suffix needs to be replaced by scantokens _n_gen_ in order to get
70 % a variable that can be declared to be of type t.
71 vardef generic_declare(text t) text vars =
72 set_n_gen_;
73 forsuffixes v_=vars:
74 if forsuffixes _n=scantokens _n_: not t v_ endfor:
75 def _gdmac_ text _n = t v_ enddef;
76 expandafter _gdmac_ scantokens _n_gen_;
77 fi
78 endfor
79 enddef;
80
81 % Here is another version that redeclares the vars even if they are already
82 % of the right type.
83 vardef generic_redeclare(text t) text vars =
84 set_n_gen_;
85 def _gdmac_ text _n = t vars enddef;
86 expandafter _gdmac_ scantokens _n_gen_;
87 enddef;
88
89
90
91
92
93
94
95 % pp should be a string giving the name of a macro that finds the boundary path
96 % sp should be a string that names a macro for fixing the size and shape
97 % The suffix $ is the name of the box
98 % The text t gives the box contents: either empty, a picture, or a string to
99 % typeset
100 def beginbox_(expr pp,sp)(suffix $)(text t) =
101 _n_ := str $;
102 generic_declare(pair) _n.off, _n.c;
103 generic_declare(string) pproc_._n, sproc_._n;
104 generic_declare(picture) pic_._n;
105 pproc_$:=pp; sproc_$:=sp;
106 pic_$ := nullpicture;
107 for _p_=t:
108 pic_$:=
109 if picture _p_: _p_
110 else: _p_ infont defaultfont scaled defaultscale
111 fi;
112 endfor
113 $c = $off + .5[llcorner pic_$, urcorner pic_$]
114 enddef;
115
116
117 % The suffix cl names a vardef macro that clears box-related variables
118 % The suffix $ is the name of the box being ended.
119 def endbox_(suffix cl, $) =
120 if known pic_.prevbox: _dojoin(prevbox,$); fi
121 def prevbox = $ enddef;
122 expandafter def expandafter clearboxes expandafter =
123 clearboxes cl($);
124 enddef
125 enddef;
126
127
128 % Text t gives equations for joining box a to box b.
129 def boxjoin(text t) =
130 def prevbox=_ enddef;
131 def _dojoin(suffix a,b) = t enddef;
132 enddef;
133
134
135 extra_beginfig := extra_beginfig
136 & "boxjoin();save pic_,sproc_,pproc_;def clearboxes=enddef;";
137 extra_endfig := extra_endfig & "clearboxes;";
138
139
140 % Given a list of box names, give whatever default values are necessary
141 % in order to fix the size and shape of each box.
142 vardef fixsize(text t) =
143 forsuffixes $=t: scantokens sproc_$($); endfor
144 enddef;
145
146
147 % Given a list of box names, give default values for any unknown positioning
148 % offsets
149 vardef fixpos(text t) =
150 forsuffixes $=t:
151 if unknown xpart $.off: xpart $.off=0; fi
152 if unknown ypart $.off: ypart $.off=0; fi
153 endfor
154 enddef;
155
156
157 % Return the boundary path for the given box
158 vardef bpath suffix $ =
159 fixsize($); fixpos($);
160 scantokens pproc_$($)
161 enddef;
162
163
164 % Return the contents of the given box. First define a private version that
165 % the user can't accidently clobber.
166 vardef pic_mac_ suffix $ =
167 fixsize($); fixpos($);
168 pic_$ shifted $off
169 enddef;
170
171 vardef pic suffix $ = pic_mac_ $ enddef;
172
173
174 def drawboxed(text t) = % Draw each box
175 fixsize(t); fixpos(t);
176 forsuffixes s=t: draw pic_mac_.s; draw bpath.s; endfor
177 enddef;
178
179 def drawunboxed(text t) = % Draw contents of each box
180 fixsize(t); fixpos(t);
181 forsuffixes s=t: draw pic_mac_.s; endfor
182 enddef;
183
184 def drawboxes(text t) = % Draw boundary path for each box
185 forsuffixes s=t: draw bpath.s; endfor
186 enddef;
187
188
189
190
191
192 % Rectangular boxes
193
194 newinternal defaultdx, defaultdy;
195 defaultdx := defaultdy := 3bp;
196
197 vardef boxit@#(text tt) =
198 beginbox_("boxpath_","sizebox_",@#,tt);
199 generic_declare(pair) _n.sw, _n.s, _n.se, _n.e, _n.ne, _n.n, _n.nw, _n.w;
200 0 = xpart (@#nw-@#sw) = ypart(@#se-@#sw);
201 0 = xpart(@#ne-@#se) = ypart(@#ne-@#nw);
202 @#w = .5[@#nw,@#sw];
203 @#s = .5[@#sw,@#se];
204 @#e = .5[@#ne,@#se];
205 @#n = .5[@#ne,@#nw];
206 @#ne-@#c = @#c-@#sw = (@#dx,@#dy) + .5*(urcorner pic_@# - llcorner pic_@#);
207 endbox_(clearb_,@#);
208 enddef;
209
210 def boxpath_(suffix $) =
211 $.sw -- $.se -- $.ne -- $.nw -- cycle
212 enddef;
213
214 def sizebox_(suffix $) =
215 if unknown $.dx: $.dx=defaultdx; fi
216 if unknown $.dy: $.dy=defaultdy; fi
217 enddef;
218
219 vardef clearb_(suffix $) =
220 _n_ := str $;
221 generic_redeclare(numeric) _n.sw, _n.s, _n.se, _n.e, _n.ne, _n.n, _n.nw, _n.w,
222 _n.c, _n.off, _n.dx, _n.dy;
223 enddef;
224
225
226
227
228
229 % Circular and oval boxes
230
231 newinternal circmargin; circmargin:=2bp; % default clearance for picture corner
232
233 vardef circleit@#(text tt) =
234 beginbox_("thecirc_","sizecirc_",@#,tt);
235 generic_declare(pair) _n.n, _n.s, _n.e, _n.w;
236 @#e-@#c = @#c-@#w = (@#dx,0) + .5*(lrcorner pic_@#-llcorner pic_@#);
237 @#n-@#c = @#c-@#s = (0,@#dy) + .5*(ulcorner pic_@#-llcorner pic_@#);
238 endbox_(clearc_,@#);
239 enddef;
240
241 def thecirc_(suffix $) =
242 $.e{up} ... $.n{left} ... $.w{down} ... $.s{right} ... cycle
243 enddef;
244
245 vardef clearc_(suffix $) =
246 _n_ := str $;
247 generic_redeclare(numeric) _n.n, _n.s, _n.e, _n.w, _n.c, _n.off, _n.dx, _n.dy;
248 enddef;
249
250 vardef sizecirc_(suffix $) =
251 save a_, b_;
252 (a_,b_) = .5*(urcorner pic_$ - llcorner pic_$);
253 if unknown $dx:
254 if unknown $dy:
255 if unknown($dy-$dx): a_+$dx=b_+$dy; fi
256 if a_+$dx=b_+$dy: a_+$dx = a_++b_ + circmargin;
257 else: $dx =
258 pathsel_(max(a_,b_+$dx-$dy), (a_+d_,0){up}...(0,b_+d_+$dy-$dx){left});
259 fi
260 else: $dx = pathsel_(a_, (a_+d_,0){up}...(0,b_+$dy){left});
261 fi
262 elseif unknown $dy:
263 $dy = pathsel_(b_, (a_+$dx,0){up}...(0,b_+d_){left});
264 fi
265 enddef;
266
267 vardef pathsel_(expr dhi)(text tt) =
268 save f_, p_; path p_;
269 p_ = origin..(a_,b_)+circmargin*unitvector(a_,b_);
270 vardef f_(expr d_) =
271 xpart((tt) intersectiontimes p_) >= 0
272 enddef;
273 solve f_(0,dhi+1.5circmargin)
274 enddef;