Debian Codegraph @

texmf-dist/metapost/metauml/util_positioning.mp

raw
1 % Copyright 2005 2015 Ovidiu Gheorghies
2 % Licensed under the Apache License, Version 2.0.
3
4 if known _util_positioning_mp:
5 expandafter endinput
6 fi;
7 _util_positioning_mp:=1;
8
9 % Sadly, this copy of the macro is needed to prevent multiple file loads being shown by MetaPost.
10 % The guard values (such as _metauml_mp) do ensure that the file isn't loaded multiple times,
11 % but this macro makes sure that MetaPost won't try to load the file and display a message for that.
12 def inputonce text libraryFile=
13 if not known scantokens ("_" & str libraryFile & "_mp"):
14 %includeonce% show "Loading " & str libraryFile;
15 scantokens ("input " & str libraryFile);
16 else:
17 %includeonce% show str libraryFile & " already loaded.";
18 fi;
19 enddef;
20
21 inputonce util_log;
22
23 %% Gives a point which is below the given @point with the given @value.
24 %%
25 def below(expr point, value) =
26 (point - (0, value))
27 enddef;
28
29 %% Gives a point which is above the given @point with the given @value.
30 %%
31 def above(expr point, value) =
32 (point + (0, value))
33 enddef;
34
35 %% Gives a point which is at the right of the given @point with the given @value.
36 %%
37 def atright(expr point, value) =
38 (point + (value, 0))
39 enddef;
40
41 %% Gives a point which is at the left of the given @point with the given @value.
42 %%
43 def atleft(expr point, value) =
44 (point - (value, 0))
45 enddef;
46
47 %% Positions the given objects so that they:
48 %% * have their tops aligned
49 %% * the distance between the objects is @distance
50 %% * the center of gravity of the objects (taken on the top line) is at @middlePoint
51 %%
52 %% @distance
53 %% |---------------------------|
54 %% | |
55 %% | @middlePoint |
56 %% __________|_____________._____________|____________ the same top
57 %% [objectA] | [objectB]
58 %%
59 vardef centered_align_top(suffix objectA, objectB)(expr distance, middlePoint)=
60 log middlePoint;
61 objectA.top = objectB.top;
62 objectB.left - objectA.right = distance;
63 middlePoint = .5[objectB.nw, objectA.ne];
64 enddef;
65
66 %% Deprecated, kept for backward compatibility (align is misspelled).
67 %%
68 vardef centered_allign_top(suffix objectA, objectB)(expr distance, middlePoint)=
69 log middlePoint;
70 objectA.top = objectB.top;
71 objectB.left - objectA.right = distance;
72 middlePoint = .5[objectB.nw, objectA.ne];
73 enddef;
74
75 %% theString means here not a sequence of characters but
76 %% the horizontal or vertical line on which objects are placed.
77 %% For example, if theString is top, then the objects are
78 %% "hanging" on a horizontal line, like freshly washed
79 %% clothes on a string.
80
81 vardef align(suffix theString, extremityNew, extremityOld)
82 (text distanceBetweenObjects)(expr sign)(text objects)=
83 string objectsAsString__;
84 objectsAsString__ := enumToString(objects)("");
85
86 log "Aligning '" & objectsAsString__ & "' at " & str theString;
87 log sign;
88
89 if (objectsAsString__ = ""):
90 log "Nothing to do, bailing out.";
91 else:
92 string previousObject__;
93 previousObject__ := "";
94 objectIndex__ := 0;
95
96 forsuffixes obj = objects:
97 log "object: " & str obj;
98 if objectIndex__ = 0:
99 objectIndex__ := objectIndex__ + 1;
100 previousObject__ := str obj;
101 else:
102 objectIndex__ := objectIndex__ + 1;
103
104 log str theString;
105 log str extremityOld;
106 log str extremityNew;
107 log distanceBetweenObjects;
108
109 string eqA__, eqB__;
110 eqA__ := previousObject__ & "." & str theString & " = " & str obj & "." & str theString;
111
112 if (sign = "+"):
113 eqB__ := previousObject__ & "." & str extremityOld & " + " & (str distanceBetweenObjects) & " = " & str obj & "." & str extremityNew;
114 %eqB__ := previousObject__ & "." & str extremityOld & " + distanceBetweenObjects = " & str obj & "." & str extremityNew;
115 else:
116 eqB__ := previousObject__ & "." & str extremityOld & " - " & (str distanceBetweenObjects) & " = " & str obj & "." & str extremityNew;
117 fi;
118
119 log eqA__, eqB__;
120 scantokens eqA__;
121 scantokens eqB__;
122
123 previousObject__ := str obj;
124 fi;
125 endfor;
126 fi;
127 enddef;
128
129 vardef leftToRight@#(text distanceBetweenObjects)(text objects)=
130 if str @# = "":
131 log "String is empty, aligning to midy";
132 align(midy, left, right)(distanceBetweenObjects)("+")(objects);
133 else:
134 align(@#, left, right)(distanceBetweenObjects)("+")(objects);
135 fi;
136 enddef;
137
138 vardef rightToLeft@#(text distanceBetweenObjects)(text objects)=
139 if str @# = "":
140 log "String is empty, aligning to midy";
141 align(midy, right, left)(distanceBetweenObjects)("-")(objects);
142 else:
143 align(@#, right, left)(distanceBetweenObjects)("-")(objects);
144 fi;
145 enddef;
146
147 vardef topToBottom@#(text distanceBetweenObjects)(text objects)=
148 if str @# = "":
149 log "String is empty, aligning to midx";
150 align(midx, top, bottom)(distanceBetweenObjects)("-")(objects);
151 else:
152 align(@#, top, bottom)(distanceBetweenObjects)("-")(objects);
153 fi;
154 enddef;
155
156 vardef bottomToTop@#(text distanceBetweenObjects)(text objects)=
157 if str @# = "":
158 log "String is empty, aligning to midx";
159 align(midx, bottom, top)(distanceBetweenObjects)("+")(objects);
160 else:
161 align(@#, bottom, top)(distanceBetweenObjects)("+")(objects);
162 fi;
163 enddef;
164
165 %%
166 %% Defines an equation which sets the given prefix to be equal for all the given objects.
167 %% For example:
168 %%
169 %% same.top(a, b, c);
170 %%
171 %% is equivalent to
172 %% a.top = b.top = c.top;
173 %%
174 vardef same@#(text objects)=
175 log "begin macro: same@#(text objects)=";
176
177 string equation__, property__;
178 equation__ := "";
179 property__ := str @#;
180
181 objectIndex__ := 0;
182 forsuffixes obj = objects:
183 log "object: " & str obj;
184
185 if objectIndex__ = 0:
186 objectIndex__ := objectIndex__ + 1;
187 equation__ := equation__ & str obj & "." & property__;
188 else:
189 objectIndex__ := objectIndex__ + 1;
190 equation__ := equation__ & "=" & str obj & "." & property__;
191 fi;
192 endfor;
193
194 equation__ := equation__ & ";";
195
196 log "Equation is: '" & equation__ & "'";
197
198 if objectIndex__ <= 1:
199 log "One or no objects, nothing to do!";
200 else:
201 scantokens equation__;
202 fi;
203 enddef;
204
205 %% this macro is NOT usable (yet)
206 vardef leftToRightCentered@#(expr distanceBetweenObjects, middlePoint)(text objects)=
207 string objectsAsString__;
208 objectsAsString__ := enumToString(objects)("");
209
210 if (objectsAsString__ = ""):
211 log "Nothing to do, bailing out.";
212 else:
213 log "ASDFGHJ";
214 leftToRight@#(distanceBetweenObjects)(objects);
215
216 string firstObject__, lastObject__;
217 objectIndex__ := 0;
218 forsuffixes obj = objects:
219 if objectIndex__ = 0:
220 objectIndex__ := objectIndex__ + 1;
221 firstObject__ := str obj;
222 else:
223 objectIndex__ := objectIndex__ + 1;
224 lastObject__ := str obj;
225 fi;
226 endfor;
227
228 log "___";
229 .5[authA.nw,authB.ne] = middlePoint;
230 log "___";
231
232 string eqCenter__;
233 eqCenter__ := ".5[" & firstObject__ & ".nw," & lastObject__ & ".ne] = middlePoint";
234
235 log eqCenter__;
236 log middlePoint;
237
238 log "XXX";
239 scantokens eqCenter__;
240 log "XXX";
241 fi;
242 enddef;