Debian Codegraph @

texmf-dist/tex/latex/pgf-umlcd/tikzlibraryumlcd.code.tex

raw
1 % SPDX-License-Identifier: GPL-2.0-only OR LPPL-1.3c
2
3 \usetikzlibrary{arrows,shapes.multipart,backgrounds,fit}
4
5 %
6 % Utilities
7 %
8
9 \def\pgfumlcd@ifx#1#2{%
10 \ifx#1#2%
11 \expandafter\pgfutil@firstoftwo
12 \else
13 \expandafter\pgfutil@secondoftwo
14 \fi}
15
16 \def\pgfumlcd@ifdim#1#2#3{%
17 \ifdim\dimexpr#1\relax#2\dimexpr#3\relax
18 \expandafter\pgfutil@firstoftwo
19 \else
20 \expandafter\pgfutil@secondoftwo
21 \fi}
22
23 \def\pgfumlcd@ifnum#1#2#3{%
24 \ifnum\numexpr#1\relax#2\numexpr#3\relax
25 \expandafter\pgfutil@firstoftwo
26 \else
27 \expandafter\pgfutil@secondoftwo
28 \fi}
29
30 \def\pgfumlcd@newif#1{%
31 \csname newif\expandafter\endcsname\csname ifpgfumlcd@#1\endcsname
32 \expandafter\def\csname pgfumlcd@if#1\expandafter\endcsname\expandafter{%
33 \csname ifpgfumlcd@#1\endcsname
34 \expandafter\pgfutil@firstoftwo
35 \else
36 \expandafter\pgfutil@secondoftwo
37 \fi}%
38 }
39
40 \expandafter\let\expandafter\pgfumlcd@xdef\csname\ifcsname protected@xdef\endcsname protected@\fi xdef\endcsname
41
42 \def\umltextcolor{black}
43 \def\umldrawcolor{purple}
44 \def\umlfillcolor{yellow!20}
45
46 \tikzset{%
47 help lines/.append style = {blue!50,very thin,dashed},
48 umlcolor/.style = {color=\umldrawcolor,fill=\umlfillcolor,text=\umltextcolor},
49 umlcd style/.style = {umlcolor, >=angle 90},
50 package/.style = {matrix, column sep=1mm, row sep=1cm, node distance=2cm},
51 packagename/.style = {rectangle, minimum height=2em},
52 umlcd style implement line/.style = {color=\umldrawcolor, open triangle 45-,dashed},
53 objectline/.style = {color=\umldrawcolor, diamond->},
54 umlcd style inherit line/.style = {color=\umldrawcolor, open triangle 45-},
55 splitline/.style = {color=\umldrawcolor, dotted,font=\pgfutil@font@itshape},
56 umlcd style class/.style = {rectangle split, rectangle split parts=3,
57 every text node part/.style={text centered},
58 draw, minimum height=2em, umlcolor, minimum width=2cm, text width=5cm,
59 minimum height=1cm, node distance=2cm},
60 umlcd style dashed line/.style = {color=\umldrawcolor, >=angle 90,dashed},
61 umlcd style school/.style = {},
62 }
63
64 \pgfumlcd@newif{school}
65 \pgfumlcd@newif{simplified}
66
67 % declare layers
68 \pgfdeclarelayer{background}
69 \pgfdeclarelayer{connectionlayers}
70 \pgfsetlayers{background,connectionlayers,main}
71
72 \newcount\pgfumlcd@ClassAttributesNum
73 \newcount\pgfumlcd@ClassOperationsNum
74 \newcount\pgfumlcd@ClassAbstractClassNum
75 \newcount\pgfumlcd@ClassInterfaceNum
76 \newcount\pgfumlcd@ClassSplitPartNum
77
78 \def\pgfumlcd@PackageFit{}
79
80 \def\pgfumlcd@class{\pgfutil@ifnextchar[{\pgfumlcd@class@}{\pgfumlcd@class@[]}}
81 \def\pgfumlcd@class@[#1]#2#3{%
82 \pgfumlcd@classAndInterfaceCommon{#1}{#2}{#3}%
83 }
84 \def\endpgfumlcd@class{%
85 \pgfumlcd@calcuateNumberOfParts{}%
86 \node[anchor=north,this umlcd style] (\pgfumlcd@ClassName) at (\pgfumlcd@ClassPos) {%
87 {\pgfutil@font@bfseries\pgfumlcd@ClassName} % <-- explicit space
88 \pgfumlcd@insertAttributesAndOperations{}%
89 };%
90 \endpgfumlcd@classAndInterfaceCommon
91 }
92
93 \def\pgfumlcd@interface{\pgfutil@ifnextchar[{\pgfumlcd@interface@}{\pgfumlcd@interface@[]}}
94 \def\pgfumlcd@interface@[#1]#2#3{%
95 \pgfumlcd@classAndInterfaceCommon{#1}{#2}{#3}%
96 }
97 \def\endpgfumlcd@interface{%
98 \pgfumlcd@calcuateNumberOfParts{}%
99 \node[anchor=north,this umlcd style] (\pgfumlcd@ClassName) at (\pgfumlcd@ClassPos) {%
100 $<<$interface$>>$\break
101 {\pgfutil@font@bfseries\pgfumlcd@ClassName} % <-- explicit space
102 \pgfumlcd@insertAttributesAndOperations{}%
103 };%
104 \endpgfumlcd@classAndInterfaceCommon
105 }
106
107 \def\pgfumlcd@abstractclass{\pgfutil@ifnextchar[{\pgfumlcd@abstractclass@}{\pgfumlcd@abstractclass@[]}}
108 \def\pgfumlcd@abstractclass@[#1]#2#3{%
109 \pgfumlcd@classAndInterfaceCommon{#1}{#2}{#3}%
110 }
111 \def\endpgfumlcd@abstractclass{%
112 \pgfumlcd@calcuateNumberOfParts{}
113 \node[anchor=north,this umlcd style] (\pgfumlcd@ClassName) at (\pgfumlcd@ClassPos) {%
114 $<<$abstract$>>$\break
115 {\pgfutil@font@bfseries\pgfumlcd@ClassName} % <-- explicit space
116 \pgfumlcd@insertAttributesAndOperations{}%
117 };%
118 \endpgfumlcd@classAndInterfaceCommon
119 }
120
121 \def\pgfumlcd@staticclass{\pgfutil@ifnextchar[{\pgfumlcd@staticclass@}{\pgfumlcd@staticclass@[]}}
122 \def\pgfumlcd@staticclass@[#1]#2#3{%
123 \pgfumlcd@classAndInterfaceCommon{#1}{#2}{#3}%
124 }
125 \def\endpgfumlcd@staticclass{%
126 \pgfumlcd@calcuateNumberOfParts{}
127 \node[anchor=north,this umlcd style] (\pgfumlcd@ClassName) at (\pgfumlcd@ClassPos) {%
128 $<<$static$>>$\break
129 {\pgfutil@font@bfseries\pgfumlcd@ClassName} % <-- explicit space
130 \pgfumlcd@insertAttributesAndOperations{}%
131 };%
132 \endpgfumlcd@classAndInterfaceCommon
133 }
134
135 % TODO: here the name of \pgfumlcd@ClassName and \umlObjectName should be
136 % switched, it is only for reusing \classAndInterfaceCommon at the
137 % moment.
138 \def\pgfumlcd@object{\pgfutil@ifnextchar[{\pgfumlcd@object@}{\pgfumlcd@object@[]}}
139 \def\pgfumlcd@object@[#1]#2#3{%
140 \pgfumlcd@classAndInterfaceCommon{#1}{#2}{#3}%
141 \def\pgfumlcd@@instanceOf{}
142 }
143 \def\endpgfumlcd@object{%
144 % customized
145 \pgfumlcd@ifsimplified{%
146 \pgfumlcd@calcuateNumberOfParts{}%
147 }{%
148 \pgfumlcd@ifnum{\pgfumlcd@ClassOperationsNum}{>}{0}{%
149 \pgfumlcd@ClassSplitPartNum=3
150 \pgfumlcd@xdef\pgfumlcd@SplitPart{3}%
151 }{%
152 \pgfumlcd@ClassSplitPartNum=2
153 \pgfumlcd@xdef\pgfumlcd@SplitPart{2}%
154 }%
155 }%
156 \pgfumlcd@ifx{\pgfumlcd@@instanceOf}{\pgfutil@empty}{%
157 \def\pgfumlcd@ObjectName{\pgfumlcd@ClassName}%
158 }{%
159 \def\pgfumlcd@ObjectName{\pgfumlcd@ClassName : \pgfumlcd@@instanceOf}%
160 }%
161 \node[anchor=north,this umlcd style,umlcd style school] (\pgfumlcd@ClassName) at (\pgfumlcd@ClassPos) {%
162 \pgfumlcd@ifschool{%
163 {\pgfutil@font@bfseries\pgfumlcd@ObjectName}%
164 }{%
165 \underline{\pgfutil@font@bfseries\pgfumlcd@ObjectName}%
166 } % <-- explicit space
167 \pgfumlcd@insertAttributesAndOperations{}%
168 };%
169 \endpgfumlcd@classAndInterfaceCommon
170 }
171
172 \def\pgfumlcd@insertAttributesAndOperations{%
173 \pgfumlcd@ifnum{\pgfumlcd@ClassSplitPartNum}{>}{1}{%
174 \nodepart{second}%
175 }{}%
176 \pgfumlcd@ClassAttributes
177 \pgfumlcd@ifnum{\pgfumlcd@ClassSplitPartNum}{>}{2}{%
178 \nodepart{third}%
179 }{}%
180 \pgfumlcd@ClassOperations
181 }
182
183 \def\pgfumlcd@calcuateNumberOfParts{%
184 % calcuate the number of parts
185 \pgfumlcd@ifsimplified{%
186 \pgfumlcd@ClassSplitPartNum=1
187 \pgfumlcd@ifnum{\pgfumlcd@ClassAttributesNum}{>}{0}{%
188 \advance\pgfumlcd@ClassSplitPartNum by 1
189 }{}%
190 \pgfumlcd@ifnum{\pgfumlcd@ClassOperationsNum}{>}{0}{%
191 \advance\pgfumlcd@ClassSplitPartNum by 1
192 }{}%
193 }{%
194 \pgfumlcd@ClassSplitPartNum=3 % three parts by default
195 }%
196 \pgfumlcd@xdef\pgfumlcd@SplitPart{3}
197 \pgfumlcd@ifnum{\pgfumlcd@ClassSplitPartNum}{=}{1}{%
198 \pgfumlcd@xdef\pgfumlcd@SplitPart{1}%
199 }{}%
200 \pgfumlcd@ifnum{\pgfumlcd@ClassSplitPartNum}{=}{2}{%
201 \pgfumlcd@xdef\pgfumlcd@SplitPart{2}%
202 }{}%
203 }
204
205 \def\pgfumlcd@classAndInterfaceCommon#1#2#3{%
206 \def\pgfumlcd@ClassName{#2}%
207 \def\pgfumlcd@ClassPos{#3}
208 \def\pgfumlcd@ClassAttributes{}%
209 \def\pgfumlcd@ClassOperations{}%
210 \def\pgfumlcd@ClassAbstractClass{}%
211 \def\pgfumlcd@ClassInterface{}%
212 \pgfumlcd@ClassAttributesNum=0
213 \pgfumlcd@ClassOperationsNum=0
214 \pgfumlcd@ClassAbstractClassNum=0
215 \pgfumlcd@ClassInterfaceNum=0
216 \tikzset{%
217 this umlcd style/.style={%
218 umlcd style class,%
219 rectangle split parts=\pgfumlcd@SplitPart,%
220 #1}%
221 }
222 }%
223 \def\endpgfumlcd@classAndInterfaceCommon{%
224 %% connections
225 \pgfonlayer{connectionlayers}%
226 \pgfumlcd@ifnum{\pgfumlcd@ClassAbstractClassNum}{>}{0}{%
227 \foreach \c in \pgfumlcd@ClassAbstractClass {%
228 \draw [umlcd style inherit line] (\c) -- (\pgfumlcd@ClassName);%
229 }%
230 }{}%
231 \pgfumlcd@ifnum{\pgfumlcd@ClassInterfaceNum}{>}{0}{%
232 \foreach \c in \pgfumlcd@ClassInterface {%
233 \draw [umlcd style implement line] (\c) -- (\pgfumlcd@ClassName);%
234 }%
235 }{}%
236 \endpgfonlayer
237 %% add to fit
238 \let\pgfumlcd@PackageFitOld\pgfumlcd@PackageFit
239 \pgfumlcd@xdef\pgfumlcd@PackageFit{\pgfumlcd@PackageFitOld (\pgfumlcd@ClassName)}%
240 }
241
242 \def\pgfumlcd@attribute#1{%
243 \pgfumlcd@ifnum{\pgfumlcd@ClassAttributesNum}{=}{0}{%
244 \pgfumlcd@xdef\pgfumlcd@ClassAttributes{#1}%
245 }{%
246 \let\pgfumlcd@ClassAttributesOld\pgfumlcd@ClassAttributes
247 \pgfumlcd@xdef\pgfumlcd@ClassAttributes{\pgfumlcd@ClassAttributesOld\break#1}%
248 }%
249 \advance\pgfumlcd@ClassAttributesNum by 1
250 }
251
252 \def\pgfumlcd@operation{\pgfutil@ifnextchar[{\pgfumlcd@operation@}{\pgfumlcd@operation@[1]}}
253 \def\pgfumlcd@operation@[#1]#2{%
254 \advance\pgfumlcd@ClassOperationsNum by 1
255 \def\pgfumlcd@virtualoperation{#2}
256 \pgfumlcd@ifnum{0}{=}{#1}{%
257 \def\pgfumlcd@virtualoperation{{\pgfutil@font@itshape#2}}%
258 }{}%
259 \pgfumlcd@ifnum{\pgfumlcd@ClassOperationsNum}{=}{1}{%
260 \pgfumlcd@xdef\pgfumlcd@ClassOperations{\pgfumlcd@virtualoperation}%
261 }{%
262 \let\pgfumlcd@ClassOperationsOld\pgfumlcd@ClassOperations
263 \pgfumlcd@xdef\pgfumlcd@ClassOperations{\pgfumlcd@ClassOperationsOld\break\pgfumlcd@virtualoperation}%
264 }%
265 }
266
267 \def\pgfumlcd@inherit#1{%
268 \advance\pgfumlcd@ClassAbstractClassNum by 1
269 \pgfumlcd@ifnum{\pgfumlcd@ClassAbstractClassNum}{=}{1}{%
270 \pgfumlcd@xdef\pgfumlcd@ClassAbstractClass{#1}%
271 }{%
272 \let\pgfumlcd@ClassAbstractClassOld\pgfumlcd@ClassAbstractClass
273 \pgfumlcd@xdef\pgfumlcd@ClassAbstractClass{\pgfumlcd@ClassAbstractClassOld, #1}%
274 }%
275 }
276
277 \def\pgfumlcd@implement#1{%
278 \advance\pgfumlcd@ClassInterfaceNum by 1
279 \pgfumlcd@ifnum{\pgfumlcd@ClassInterfaceNum}{=}{1}{%
280 \pgfumlcd@xdef\pgfumlcd@ClassInterface{#1}
281 }{%
282 \let\pgfumlcd@ClassInterfaceOld\pgfumlcd@ClassInterface
283 \pgfumlcd@xdef\pgfumlcd@ClassInterface{\pgfumlcd@ClassInterfaceOld, #1}
284 }%
285 }
286
287 \def\pgfumlcd@instanceOf#1{\def\pgfumlcd@@instanceOf{#1}}
288
289 \def\pgfumlcd@association{\pgfutil@ifnextchar[{\pgfumlcd@association@}{\pgfumlcd@association@[]}}
290 \def\pgfumlcd@association@[#1]#2#3#4#5#6#7{%
291 \draw [umlcd style,#1] (#2) -- (#5)
292 node[near start, auto]{#3}
293 node[near start, auto, swap]{#4}
294 node[near end, auto]{#6}
295 node[near end, auto, swap]{#7};%
296 }
297
298 \def\pgfumlcd@unidirectionalAssociation{%
299 \pgfutil@ifnextchar[{\pgfumlcd@unidirectionalAssociation@}{\pgfumlcd@unidirectionalAssociation@[]}}
300 \def\pgfumlcd@unidirectionalAssociation@[#1]#2#3#4#5{%
301 \draw [umlcd style,->,#1] (#2) -- (#5)
302 node[near end, auto]{#3}
303 node[near end, auto, swap]{#4};%
304 }
305
306 \def\pgfumlcd@aggregation{\pgfutil@ifnextchar[{\pgfumlcd@aggregation@}{\pgfumlcd@aggregation@[]}}
307 \def\pgfumlcd@aggregation@[#1]#2#3#4#5{%
308 \draw[umlcd style,open diamond->,#1] (#2) -- (#5)
309 node[near end, auto]{#3}
310 node[near end, auto, swap]{#4};%
311 }
312
313 \def\pgfumlcd@composition{\pgfutil@ifnextchar[{\pgfumlcd@composition@}{\pgfumlcd@composition@[]}}
314 \def\pgfumlcd@composition@[#1]#2#3#4#5{%
315 \draw[umlcd style,fill=\umldrawcolor,diamond->,#1] (#2) -- (#5)
316 node[near end, auto]{#3}
317 node[near end, auto, swap]{#4};%
318 }
319
320 \def\pgfumlcd@package#1{%
321 \def\pgfumlcd@PackageFit{}%
322 \def\pgfumlcd@PackageName{#1}%
323 }
324 \def\endpgfumlcd@package{%
325 \pgfonlayer{background}%
326 \node[umlcd style,draw,inner sep=0.5cm,fit=\pgfumlcd@PackageFit] (\pgfumlcd@PackageName) {};%
327 \node[umlcd style,draw,outer ysep=-0.5,anchor=south west] (\pgfumlcd@PackageName caption) at
328 (\[email protected] west) {\pgfumlcd@PackageName};%
329 \endpgfonlayer
330 }
331
332 \def\switchUmlcdSchool{%
333 \pgfumlcd@ifschool{%
334 \tikzset{umlcd style school/.style={}}%
335 \pgfumlcd@schoolfalse
336 }{%
337 \tikzset{umlcd style school/.style = {rounded corners}}%
338 \pgfumlcd@schooltrue
339 }%
340 }
341
342 \pgfdeclareshape{umlcdnote}{%
343 \inheritsavedanchors[from=rectangle]% this is nearly a rectangle
344 \inheritanchorborder[from=rectangle]%
345 \inheritanchor[from=rectangle]{center}%
346 \inheritanchor[from=rectangle]{north}%
347 \inheritanchor[from=rectangle]{south}%
348 \inheritanchor[from=rectangle]{west}%
349 \inheritanchor[from=rectangle]{east}%
350 % ... and possibly more
351 \backgroundpath{% this is new
352 % store lower right in xa/ya and upper right in xb/yb
353 \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
354 \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
355 % compute corner of flipped page
356 \pgf@xc=\pgf@xb \advance\pgf@xc by-10pt % this should be a parameter
357 \pgf@yc=\pgf@yb \advance\pgf@yc by-10pt
358 % construct main path
359 \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}%
360 \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}%
361 \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yb}}%
362 \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}%
363 \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}%
364 \pgfpathclose
365 % add little corner
366 \pgfpathmoveto{\pgfpoint{\pgf@xc}{\pgf@yb}}%
367 \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}%
368 \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}%
369 \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}%
370 }%
371 }
372
373 \def\pgfumlcd@umlnote{\pgfutil@ifnextchar[{\pgfumlcd@umlnote@}{\pgfumlcd@umlnote@[]}}
374 \def\pgfumlcd@umlnote@[#1]{%
375 \node[anchor=north,draw,shape=umlcdnote,text width=4cm,umlcd style,#1]%
376 }
377
378 \tikzaddtikzonlycommandshortcutlet{\class}{\pgfumlcd@class}
379 \tikzaddtikzonlycommandshortcutlet{\endclass}{\endpgfumlcd@class}
380 \tikzaddtikzonlycommandshortcutlet{\interface}{\pgfumlcd@interface}
381 \tikzaddtikzonlycommandshortcutlet{\endinterface}{\endpgfumlcd@interface}
382 \tikzaddtikzonlycommandshortcutlet{\abstractclass}{\pgfumlcd@abstractclass}
383 \tikzaddtikzonlycommandshortcutlet{\endabstractclass}{\endpgfumlcd@abstractclass}
384 \tikzaddtikzonlycommandshortcutlet{\staticclass}{\pgfumlcd@staticclass}
385 \tikzaddtikzonlycommandshortcutlet{\endstaticclass}{\endpgfumlcd@staticclass}
386 \tikzaddtikzonlycommandshortcutlet{\object}{\pgfumlcd@object}
387 \tikzaddtikzonlycommandshortcutlet{\endobject}{\endpgfumlcd@object}
388 \tikzaddtikzonlycommandshortcutlet{\classAndInterfaceCommon}{\pgfumlcd@classAndInterfaceCommon}
389 \tikzaddtikzonlycommandshortcutlet{\endclassAndInterfaceCommon}{\endpgfumlcd@classAndInterfaceCommon}
390 \tikzaddtikzonlycommandshortcutlet{\package}{\pgfumlcd@package}
391 \tikzaddtikzonlycommandshortcutlet{\endpackage}{\endpgfumlcd@package}
392
393 \tikzaddtikzonlycommandshortcutlet{\attribute}{\pgfumlcd@attribute}
394 \tikzaddtikzonlycommandshortcutlet{\operation}{\pgfumlcd@operation}
395 \tikzaddtikzonlycommandshortcutlet{\inherit}{\pgfumlcd@inherit}
396 \tikzaddtikzonlycommandshortcutlet{\implement}{\pgfumlcd@implement}
397 \tikzaddtikzonlycommandshortcutlet{\instanceOf}{\pgfumlcd@instanceOf}
398 \tikzaddtikzonlycommandshortcutlet{\association}{\pgfumlcd@association}
399 \tikzaddtikzonlycommandshortcutlet{\unidirectionalAssociation}{\pgfumlcd@unidirectionalAssociation}
400 \tikzaddtikzonlycommandshortcutlet{\aggregation}{\pgfumlcd@aggregation}
401 \tikzaddtikzonlycommandshortcutlet{\composition}{\pgfumlcd@composition}
402 \tikzaddtikzonlycommandshortcutlet{\umlnote}{\pgfumlcd@umlnote}