summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/metauml/util_group.mp
blob: 732deba4bdfe2dc8aa600173aa5ad3559e3664cc (plain)
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
% MetaUtil, an easier MetaPost life
% 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 _util_group_mp:
  expandafter endinput
fi;
_util_group_mp:=1;

input util_object;
input util_commons;
input util_margins;
input util_log;

vardef GroupInfo@#(expr pleft, pright, ptop, pbottom)=
  attributes(@#);
  var(color)   borderColor;
  var(numeric) boxed;

  @#boxed := 0;
  @#borderColor := red;

  Margins.@#(pleft, pright, ptop, pbottom);
enddef;

vardef GroupInfoCopy@#(text src)=
  GroupInfo@#(src.left, src.right, src.top, src.bottom);

  @#boxed := src.boxed;
  @#borderColor := src.borderColor;

enddef;

GroupInfo.iGroup(2, 2, 2, 2);

GroupInfoCopy.iGroupBoxed(iGroup);
iGroupBoxed.boxed := 1;

vardef EGroup@#(text groupInfo)(text objects)=
  ObjectEquations(@#);
  @#className := "Group";

  var(numeric) minx, maxx, miny, maxy;
  var(numeric) nObjects;
  var(string)  objectsAsString;

  GroupInfoCopy.@#info(groupInfo);

  @#objectsAsString := enumToString(objects)("");
  log @#objectsAsString;

  @#nObjects := 0;
  if @#objectsAsString <> "":
    forsuffixes obj = scantokens @#objectsAsString:
        @#nObjects := @#nObjects + 1;
    endfor;
  fi;

enddef;

vardef Group@#(text objects)=
  EGroup@#(iGroup)(objects);
enddef;

vardef Group_layout@#=
  if @#layedout = 0:
    log "Needing layout for group " & (str @#);
    @#layedout := 1;

    if (@#objectsAsString = ""):
      @#width = @#info.left + @#info.right;
      @#height = @#info.top + @#info.bottom;
    else:
      log "Calling layout for objects group '" & (str @#) & "' ...";
      log " object are: '" & @#objectsAsString & "'.";

      layoutObjects(scantokens @#objectsAsString);
      log "All objects in group '" & (str @#) & "' are now layed out.";

      % As already tested, @#objectsAsString must be <> "".
      % See page 53, User's manual for MetaPost
      % "if some of the suffixes are empty, the loop gets
      % executed, with the loop index parameter set to
      % the empty suffix" (!)

      log "Proceeding to finding min/max x/y of objects:";
      log "   " & @#objectsAsString;

      __objectIndex := 0;
      forsuffixes obj__ = scantokens @#objectsAsString:
        if __objectIndex = 0:
            log "First object " & str obj__;
            @#minx := obj__.left;
            log @#maxx;
            log obj__.right;
            @#maxx := obj__.right;
            log @#maxx;
            @#miny := obj__.bottom;
            @#maxy := obj__.top;

            __objectIndex := __objectIndex + 1;
        else:
            log "Current object " & str obj__;

            log "comparing(min)";
            log @#minx;
            log @#obj__.left;
            @#minx := min(@#minx)(obj__.left);

            log "comparing(max)";
            log @#maxx;
            log @#obj__.right;
            @#maxx := max(@#maxx)(obj__.right);

            log "comparing(min)";
            log @#miny;
            log @#obj__.bottom;
            @#miny := min(@#miny)(obj__.bottom);

            log "comparing(max)";
            log @#maxy;
            log @#obj__.top;
            @#maxy := max(@#maxy)(obj__.top);

            __objectIndex := __objectIndex + 1;
        fi;
      endfor;
      log "... done iterating";
      log "maxx = ";
      log @#maxx;

      @#nw = (@#minx - @#info.left, @#maxy + @#info.top);
      @#se = (@#maxx + @#info.right, @#miny - @#info.bottom);

      log "Done performing actual layout for group " & str @#;
    fi;
  else:
    log "NOT needing layout for group " & (str @#);
  fi;
enddef;

vardef Group_draw@#=
  log "Drawing group....";
  Group_layout@#;
  objectEnsurePositioning.@#;

  if @#objectsAsString <> "":
    drawObjects(scantokens @#objectsAsString);
  fi;

  if (@#info.boxed = 1):
    draw objectBox(@#) withcolor @#info.borderColor;
  fi;
enddef;

vardef Group_border@#=
    objectBox(@#)
enddef;