1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
% MetaUML, a MetaPost library for typesetting exquisite UML diagrams.
% Copyright (C) 2005 Ovidiu Gheorghies
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
if known _metauml_class_mp:
expandafter endinput
fi;
_metauml_class_mp:=1;
% Sadly, this copy of the macro is needed to prevent multiple file loads being shown by MetaPost.
% The guard values (such as _metauml_mp) do ensure that the file isn't loaded multiple times,
% but this macro makes sure that MetaPost won't try to load the file and display a message for that.
def inputonce text libraryFile=
if not known scantokens ("_" & str libraryFile & "_mp"):
%includeonce% show "Loading " & str libraryFile;
scantokens ("input " & str libraryFile);
else:
%includeonce% show str libraryFile & " already loaded.";
fi;
enddef;
inputonce metauml_defaults;
inputonce util_log;
inputonce util_picture;
inputonce util_picture_stack;
inputonce util_shade;
string accessPublic, accessProtected, accessPrivate;
accessPublic:="+";
accessProtected:="#";
accessPrivate:="-";
vardef ClassInfo@#=
attributes(@#);
var(numeric) featureAccessSpacing, accessWidth, accessHeight;
var(color) foreColor, borderColor;
FontInfo.@#nameFont(metauml_defaultFont, defaultscale);
FontInfo.@#attributeFont (metauml_defaultFont, defaultscale);
FontInfo.@#methodFont (metauml_defaultFont, defaultscale);
FontInfo.@#stereotypeFont(metauml_defaultFont, .7);
ShadeInfo.@#iShade;
@#featureAccessSpacing := 2;
@#accessWidth := 7;
@#accessHeight := 6.75;
@#foreColor := .9white;
@#borderColor := black;
PictureInfo.@#iName (2, 2, 1, 3)(@#nameFont);
PictureInfo.@#iStereotype(2, -5, 2, 2)(@#stereotypeFont);
PictureInfo.@#iAttribute (2, 2, 1.25, 0)(@#attributeFont);
PictureInfo.@#iMethod (2, 2, 1.25, 0)(@#methodFont);
PictureStackInfo.@#iStereotypeStack(2, 2, 1, 1)(5.5)(@#iStereotype);
PictureStackInfo.@#iAttributeStack (2, 2, 2.5, 2)(10.5)(@#iAttribute); % 8.5
PictureStackInfo.@#iMethodStack (2, 2, 2.5, 2)(10.5)(@#iMethod);
@#iAttributeStack.iPict.bottom := 2;
%@#iAttributeStack.iPict.boxed := 1;
@#iMethodStack.iPict.bottom := 2;
%@#iMethodStack.iPict.boxed := 1;
LockInfo.@#iLock(@#accessWidth, @#accessHeight, .6, .15, .55, .4white, .6white, .7white, .3white);
@#iName.ignoreNegativeBase := 1;
@#iAttributeStack.iPict.ignoreNegativeBase := 1;
@#iMethodStack.iPict.ignoreNegativeBase := 1;
@#iStereotypeStack.iPict.ignoreNegativeBase := 1;
enddef;
vardef ClassInfoCopy@#(text src)=
log "ClassInfoCopy: Copying class";
attributes(@#);
var(numeric) featureAccessSpacing, accessWidth, accessHeight;
var(color) foreColor, borderColor;
FontInfoCopy.@#nameFont(src.nameFont);
FontInfoCopy.@#attributeFont(src.attributeFont);
FontInfoCopy.@#methodFont(src.methodFont);
FontInfoCopy.@#stereotypeFont(src.stereotypeFont);
ShadeInfoCopy.@#iShade(src.iShade);
@#featureAccessSpacing := src.featureAccessSpacing;
@#accessWidth := src.accessWidth;
@#accessHeight := src.accessHeight;
@#foreColor := src.foreColor;
@#borderColor := src.borderColor;
PictureInfoCopy.@#iName (src.iName);
PictureInfoCopy.@#iStereotype(src.iStereotype);
PictureInfoCopy.@#iAttribute (src.iAttribute);
PictureInfoCopy.@#iMethod (src.iMethod);
PictureStackInfoCopy.@#iStereotypeStack(src.iStereotypeStack);
PictureStackInfoCopy.@#iAttributeStack (src.iAttributeStack);
PictureStackInfoCopy.@#iMethodStack (src.iMethodStack);
LockInfoCopy.@#iLock(src.iLock);
enddef;
ClassInfo.iClass;
ClassInfoCopy.iClassNameOnly(iClass);
iClassNameOnly.iName.top := 10;
iClassNameOnly.iName.bottom := 10;
iClassNameOnly.iAttributeStack.top := 0;
iClassNameOnly.iAttributeStack.bottom := 0;
iClassNameOnly.iMethodStack.top := 0;
iClassNameOnly.iMethodStack.bottom := 0;
ClassInfoCopy.iInterface(iClass);
iInterface.iAttributeStack.top := 0;
iInterface.iAttributeStack.bottom := 0;
iInterface.iName.iFont.name := metauml_defaultFontOblique;
ClassInfoCopy.iAbstractClass(iInterface);
%
% CLASS
%
vardef defClass@#(expr pname) =
ObjectEquations(@#);
@#className := "Class";
string @#name;
@#name = pname;
numeric @#nStereotypes;
string @#stereotypes[];
string @#attributes[];
string @#attributesAccess[];
string @#methods[];
string @#methodsAccess[];
numeric @#nAttrs;
numeric @#nMethods;
@#nStereotypes := 0;
@#nAttrs := 0;
@#nMethods := 0;
enddef;
vardef addAttribute@#(expr pcontent) =
string access;
access := substring(0,1) of pcontent;
if (not (access = accessPublic)) and
(not (access = accessPrivate)) and
(not (access = accessProtected)):
@#.attributes[@#.nAttrs] := pcontent;
@#.attributesAccess[@#.nAttrs] := accessProtected;
else:
save from;
from := 1;
if (substring(1,2) of pcontent) = " ": from := 2; fi;
@#.attributes[@#.nAttrs] := substring(from, infinity) of pcontent;
@#.attributesAccess[@#.nAttrs] := access;
fi;
@#.nAttrs := @#.nAttrs + 1;
enddef;
vardef addMethod@#(expr pcontent) =
string access;
access := substring(0,1) of pcontent;
if (not (access = accessPublic)) and
(not (access = accessPrivate)) and
(not (access = accessProtected)):
@#.methods[@#.nMethods] := pcontent;
@#.methodsAccess[@#.nMethods] := accessProtected;
else:
@#.methods[@#.nMethods] := substring(1, 999) of pcontent;
@#.methodsAccess[@#.nMethods] := access;
fi;
@#.nMethods := @#.nMethods + 1;
enddef;
vardef classStereotype@#(expr pcontent) =
@#stereotypes[@#nStereotypes] := pcontent;
@#nStereotypes := @#nStereotypes + 1;
enddef;
vardef classStereotypes@#(text stereotypes)=
for stereotype = stereotypes:
classStereotype@#(stereotype);
endfor;
enddef;
vardef EClass@#(text _info)(expr name)(text attributes)(text methods)=
log "EClass begin: " & str @#;
defClass@#(name);
log "Copying class info";
ClassInfoCopy.@#info(_info);
for a=attributes:
log "Adding attribute ", a;
addAttribute@#(a);
endfor;
for m=methods:
log "Adding method ", m;
addMethod@#(m);
endfor;
enddef;
vardef Class@#(expr name)(text attributes)(text methods)=
EClass@#(iClass)(name)(attributes)(methods);
enddef;
vardef Interface@#(expr name)(text methods)=
EClass@#(iInterface)(name)()(methods);
enddef;
vardef EInterface@#(text _info)(expr name)(text methods)=
EClass@#(_info)(name)()(methods);
enddef;
vardef ClassName@#(expr name)=
EClass@#(iClassNameOnly)(name)()();
enddef;
vardef EClassName@#(text _info)(expr name)=
EClass@#(_info)(name)()();
enddef;
vardef AbstractClass@#(expr name)(text attributes)(text methods)=
EClass@#(iAbstractClass)(name)(attributes)(methods);
enddef;
vardef EAbstractClass@#(text _info)(expr name)(text methods)=
EClass@#(_info)(name)()(methods);
enddef;
vardef Class_border@#=
objectBox(@#)
enddef;
vardef Class_layout@# =
if @#layedout = 1:
log "Class " & (str @#) & " has already been layed out";
else:
@#layedout := 1;
log "Class layout: " & (str @#);
@#maxFeatureWidth := 0;
EPictureStack.@#stereotypeStack(@#info.iStereotypeStack)
(scantokens listArray(@#stereotypes)(@#nStereotypes))("vcenterbase");
EPicture.@#namePict(@#info.iName)(@#name);
EPictureStack.@#attributeStack(@#info.iAttributeStack)
(scantokens listArray(@#attributes)(@#nAttrs))("vleftbase");
EPictureStack.@#methodStack(@#info.iMethodStack)
(scantokens listArray(@#methods)(@#nMethods))("vleftbase");
layoutObjects(@#stereotypeStack, @#namePict, @#attributeStack, @#methodStack);
log "Computing maxFeatureWidth";
log @#stereotypeStack.width;
log @#namePict.width;
log @#attributeStack.width;
log @#methodStack.width;
log "...";
@#maxFeatureWidth := lmax(@#stereotypeStack.width, @#namePict.width,
@#attributeStack.width, @#methodStack.width);
log "Done computing maxFeatureWidth";
log "max feature width: " & decimal @#maxFeatureWidth;
@#namePict.midx = @#midx;
@#stereotypeStack.midx = @#namePict.midx;
@#stereotypeStack.top = @#top;
@#namePict.top = @#stereotypeStack.bottom;
@#attributeStack.top = @#namePict.bottom;
@#methodStack.top = @#attributeStack.bottom;
@#attributeStack.left = @#methodStack.left =
@#left + @#info.accessWidth + @#info.featureAccessSpacing;
@#width = @#maxFeatureWidth + @#info.accessWidth + @#info.featureAccessSpacing;
@#bottom = @#methodStack.bottom;
log "Class layout done...";
fi;
enddef;
vardef Class_paintAccess@#(expr _access, _se) =
save lock;
ELock.lock(@#info.iLock)(_access);
lock.se = _se;
drawObject(lock);
enddef;
vardef Class_drawFeatures@#=
log "Drawing stereotypes";
drawObject(@#stereotypeStack);
log "Drawing name";
drawObject(@#namePict);
log "Drawing attribute stack";
drawObject(@#attributeStack);
log "Drawing method stack";
drawObject(@#methodStack);
log "********************************** Drawing access locks";
for i = 0 upto @#nAttrs-1:
log @#attributesAccess[i];
Class_paintAccess@#(@#attributesAccess[i],
@#attributeStack.pict[i].sw + (-@#info.featureAccessSpacing, @#info.iAttributeStack.iPict.bottom));
endfor;
for i = 0 upto @#nMethods-1:
log @#methodsAccess[i];
Class_paintAccess@#(@#methodsAccess[i],
@#methodStack.pict[i].sw + (-@#info.featureAccessSpacing, @#info.iMethodStack.iPict.bottom));
endfor;
enddef;
vardef Class_paintSkin@# =
log "Painting class skin...";
nameAttributeY := @#attributeStack.top;
attributeMethodY := @#methodStack.top;
%fill Class_border.@# shifted (@#info.shade,-@#info.shade)
% withcolor .7white withpen currentpen scaled 1;
drawObjectShade(@#);
fill Class_border.@# withcolor @#info.foreColor;
draw Class_border.@# withcolor @#info.borderColor;
draw (xpart @#nw, nameAttributeY)--(xpart @#se, nameAttributeY) withcolor @#info.borderColor;
draw (xpart @#nw, attributeMethodY)--(xpart @#se, attributeMethodY) withcolor @#info.borderColor;
enddef;
vardef Class_draw@#=
log "draw class begin...";
Class_layout.@#;
objectEnsurePositioning.@#;
Class_paintSkin.@#;
Class_drawFeatures.@#;
enddef;
vardef Class_setDebugMode@#=
@#.info.iName.boxed := 1;
@#.info.iStereotypeStack.boxed := 1;
@#.info.iStereotypeStack.iPict.boxed := 1;
@#.info.iAttributeStack.boxed := 1;
@#.info.iAttributeStack.iPict.boxed := 1;
@#.info.iMethodStack.boxed := 1;
@#.info.iMethodStack.iPict.boxed := 1;
enddef;
|