Debian Codegraph @

texmf-dist/tex/generic/pgf/libraries/decorations/pgflibrarydecorations.shapes.code.tex

raw
1 % Copyright 2019 by Mark Wibrow
2 %
3 % This file may be distributed and/or modified
4 %
5 % 1. under the LaTeX Project Public License and/or
6 % 2. under the GNU Public License.
7 %
8 % See the file doc/generic/pgf/licenses/LICENSE for more details.
9
10 \usepgfmodule{decorations}%
11
12
13 % Options for shape decorations
14
15 \pgfkeys{
16 /pgf/decoration/.cd,
17 shape/.initial=circle,
18 anchor/.initial=center,
19 shape start width/.initial=2.5pt,
20 shape start height/.initial=2.5pt,
21 shape end width/.initial=2.5pt,
22 shape end height/.initial=2.5pt,
23 shape sep/.store in=\pgf@lib@shapedecoration@sep,
24 shape sloped/.is if=pgfshapedecorationsloped,
25 shape scaled/.is if=pgfshapedecorationscaled,
26 shape evenly spread/.store in=\pgf@lib@shapedecoration@spread,
27 shape start size/.style={%
28 shape start width={#1},
29 shape start height={#1}%
30 },%
31 shape end size/.style={%
32 shape end width={#1},
33 shape end height={#1}%
34 },%
35 shape size/.style={%
36 shape start size={#1},
37 shape end size={#1}%
38 },%
39 shape width/.style={%
40 shape start width={#1},
41 shape end width={#1}
42 },
43 shape height/.style={%
44 shape start height={#1},
45 shape end height={#1}
46 }
47 }%
48
49 \def\pgf@lib@shapedecoration@sep{.25cm, between centers}%
50 \newif\ifpgfshapedecorationsloped
51 \pgfshapedecorationslopedtrue
52 \newif\ifpgfshapedecorationscaled
53 \let\pgf@lib@shapedecoration@spread\pgfutil@empty%
54
55
56
57 % triangle decoration
58
59 \pgfdeclaredecoration{triangles}{init}
60 {%
61 \state{init}[width=+0pt,next state=triangle,persistent precomputation={
62 \pgfmathparse{\pgfkeysvalueof{/pgf/decoration/shape start width}}
63 \edef\pgf@lib@dec@ssw{\pgfmathresult pt}
64 \pgfmathparse{\pgfkeysvalueof{/pgf/decoration/shape start height}/2}
65 \edef\pgf@lib@dec@ssh{\pgfmathresult pt}
66 }]{}%
67 \state{triangle}[switch if less than=+\pgfdecorationsegmentlength to last,
68 width=+\pgfdecorationsegmentlength]
69 {
70 \pgfpathmoveto{\pgfqpoint{0pt}{\pgf@lib@dec@ssh}}
71 \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{0pt}}
72 \pgfpathlineto{\pgfqpoint{0pt}{-\pgf@lib@dec@ssh}}
73 \pgfpathclose
74 }%
75 \state{last}[switch if less than=\pgf@lib@dec@ssw to skip,
76 width=\pgfdecoratedremainingdistance,next state=final]
77 {
78 \pgfpathmoveto{\pgfqpoint{0pt}{\pgf@lib@dec@ssh}}
79 \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{0pt}}
80 \pgfpathlineto{\pgfqpoint{0pt}{-\pgf@lib@dec@ssh}}
81 \pgfpathclose
82 }%
83 \state{skip}[width=\pgfdecoratedremainingdistance]
84 {}%
85 \state{final}
86 {
87 \pgfpathmoveto{\pgfpointdecoratedpathlast}
88 }%
89 }%
90
91
92
93
94 % crosses decoration
95
96 \pgfdeclaredecoration{crosses}{init}
97 {%
98 \state{init}[width=+0pt,next state=crosses,persistent precomputation={
99 \pgfmathparse{\pgfkeysvalueof{/pgf/decoration/shape start width}/2}
100 \edef\pgf@lib@dec@ssw{\pgfmathresult pt}
101 \pgfmathparse{\pgfkeysvalueof{/pgf/decoration/shape start height}/2}
102 \edef\pgf@lib@dec@ssh{\pgfmathresult pt}
103 }]{}%
104 \state{crosses}[switch if less than=+\pgfdecorationsegmentlength to last,
105 width=+\pgfdecorationsegmentlength]
106 {
107 \pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
108 \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
109 \pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
110 \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
111 }%
112 \state{last}[width=+\pgfdecoratedremainingdistance]
113 {
114 \pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
115 \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
116 \pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
117 \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
118 }%
119 \state{final}{
120 \pgfpathmoveto{\pgfpointdecoratedpathlast}
121 }%
122 }%
123
124
125
126
127
128
129 % The shape background decoration
130 %
131 % The shape background decoration adds repeated instances of
132 % the background path of a specified shape along the path. The shape
133 % must have been declared by \pgfdeclareshape. If a shape has
134 % specialized keys (e.g. the number of points on a star, or the apex
135 % angle the isosceles triangle), these can be specified in the usual manner.
136 %
137 % The separation between shapes in the path can be specified and can
138 % be between the center of the shape or the border of the shape.
139 %
140 % The height and width of the shape can be independently or
141 % simultaneously scaled (linearly) along the path. It is also
142 % possible to prevent the shapes being sloped parallel to the
143 % path.
144
145 % internal if
146 \newif\ifpgf@lib@shapedecoration@betweenborders
147
148 \edef\pgf@lib@shapedecoration@initialise{0pt}%
149
150 \pgfdeclaredecoration{shape backgrounds}{initialise}
151 {%
152 \state{initialise}
153 [
154 width=+\pgf@lib@shapedecoration@initialise,
155 next state=shape,
156 persistent precomputation=
157 {
158 %
159 % \egroup ends the group started by the automaton before executing
160 % a decoration state. This prevents the need for (most) \global variables.
161 %
162 %
163 % Check the shape exists.
164 %
165 \pgfutil@ifundefined{pgf@sh@bg@\pgfkeysvalueof{/pgf/decoration/shape}}{%
166 \pgferror{I do not know the shape `\pgfkeysvalueof{/pgf/decoration/shape}',
167 so I cannot use it in a decoration. Check if its library been loaded or if you
168 simply mistyped the name}}{}%
169 %
170 % Calculate a `default' path size.
171 %
172 \pgfinterruptpath%
173 \pgfinterruptboundingbox%
174 \pgftransformreset%
175 \pgf@relevantforpicturesizetrue%
176 %
177 % This size of this shape is unimportant, but it should
178 % be just large/small enough to avoid huge errors when
179 % calculating the scaling factors later on.
180 %
181 \pgfkeys{/pgf/inner sep=50pt, /pgf/minimum size=1pt}% Arbitrary lengths.
182 \setbox\pgfnodeparttextbox\hbox{}% Assume shape does nothing special if box is empty.
183 \let\pgf@sh@savedmacros\pgfutil@empty%
184 \let\pgf@sh@savedpoints\pgfutil@empty%
185 \csname pgf@sh@s@\pgfkeysvalueof{/pgf/decoration/shape}\endcsname%
186 \pgf@sh@savedpoints%
187 \pgf@sh@savedmacros%
188 %
189 % Save the macros and points.
190 %
191 \expandafter\gdef\expandafter\pgf@lib@shapedecoration@points\expandafter{\pgf@sh@savedpoints}%
192 \expandafter\gdef\expandafter\pgf@lib@shapedecoration@macros\expandafter{\pgf@sh@savedmacros}%
193 \csname pgf@sh@bg@\pgfkeysvalueof{/pgf/decoration/shape}\endcsname%
194 %
195 % Save the dimensions of the shape path.
196 %
197 \pgf@x\pgf@picmaxx%
198 \pgf@y\pgf@picmaxy%
199 \advance\pgf@x-\pgf@picminx%
200 \advance\pgf@y-\pgf@picminy%
201 \xdef\pgf@lib@shapedecoration@shapepathsize{%
202 \noexpand\pgf@x\the\pgf@x%
203 \noexpand\pgf@y\the\pgf@y%
204 }%
205 \endpgfinterruptboundingbox%
206 \endpgfinterruptpath%
207 %
208 \edef\pgf@lib@shapedecoration@beforeshape{0pt}%
209 \edef\pgf@lib@shapedecoration@aftershape{0pt}%
210 %
211 \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/decoration/shape start width}}%
212 \edef\pgf@lib@shapedecoration@startwidth{\the\pgf@x}%
213 \edef\pgf@lib@shapedecoration@width{\the\pgf@x}%
214 \pgf@x-\pgf@x%
215 \pgfmathaddtolength\pgf@x{\pgfkeysvalueof{/pgf/decoration/shape end width}}%
216 \edef\pgf@lib@shapedecoration@widthchange{\the\pgf@x}%
217 %
218 \pgfmathsetlength\pgf@y{\pgfkeysvalueof{/pgf/decoration/shape start height}}%
219 \edef\pgf@lib@shapedecoration@initialheight{\the\pgf@y}%
220 \edef\pgf@lib@shapedecoration@height{\the\pgf@y}%
221 \pgf@y-\pgf@y%
222 \pgfmathaddtolength\pgf@y{\pgfkeysvalueof{/pgf/decoration/shape end height}}%
223 \edef\pgf@lib@shapedecoration@heightchange{\the\pgf@y}%
224 %
225 % Calculate the sep.
226 %
227 \ifx\pgf@lib@shapedecoration@spread\pgfutil@empty%
228 %
229 % Not spreading, so easy:
230 %
231 \def\pgf@lib@shapedecoration@borderstext{between borders}%
232 \afterassignment\pgf@lib@shapedecoration@setkeyword%
233 \expandafter\pgf@x\pgf@lib@shapedecoration@sep,\pgf@stop%
234 \edef\pgf@lib@shapedecoration@sep{\the\pgf@x}%
235 \else%
236 %
237 % Spreading (a bit of a nuiscence actually).
238 %
239 \def\pgf@lib@shapedecoration@borderstext{by borders}%
240 \afterassignment\pgf@lib@shapedecoration@setkeyword%
241 \expandafter\c@pgf@counta\pgf@lib@shapedecoration@spread,\pgf@stop%
242 \ifpgf@lib@shapedecoration@betweenborders%
243 %
244 % Ok. The required sep between borders is:
245 %
246 % (r -(n-1)((a+b)/2))/(n-1)
247 %
248 % r = decoration length (here, the remaining distance)
249 % a = initial width
250 % b = end width
251 % n = the number of shapes
252 %
253 \ifnum\c@pgf@counta>1\relax%
254 \advance\c@pgf@counta-1\relax%
255 \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/decoration/shape start width}}%
256 \ifpgfshapedecorationscaled%
257 \pgfmathaddtolength\pgf@x{\pgfkeysvalueof{/pgf/decoration/shape end width}}%
258 \else%
259 \advance\pgf@x\pgf@x%
260 \fi%
261 \[email protected]\pgf@x% (a+b)/2
262 \multiply\pgf@x-\c@pgf@counta% -(n-1)((a+b)/2)
263 \advance\pgf@x\pgfdecoratedremainingdistance%
264 \divide\pgf@x\c@pgf@counta%
265 \[email protected]\pgf@x% Hackery to control some native TeX inaccuracies.
266 %
267 % Unfortunately if the shape is scaled, and evenly spread by borders,
268 % it is necessary to do something a bit different to control for
269 % (most) inaccuracies.
270 %
271 \ifpgfshapedecorationscaled%
272 \pgf@xa\pgf@lib@shapedecoration@widthchange\relax%
273 \divide\pgf@xa\c@pgf@counta%
274 \edef\pgf@lib@shapedecoration@specialwidth{\the\pgf@xa}%
275 \fi%
276 \else%
277 \pgf@lib@shapedecoration@betweenbordersfalse%
278 \pgf@x\pgfdecoratedremainingdistance%
279 \ifnum\c@pgf@counta=1\relax%
280 \[email protected]\pgf@x%
281 \edef\pgf@lib@shapedecoration@initialise{\the\pgf@y}%
282 \else%
283 \advance\pgf@x5pt\relax% An arbitrary value >0pt.
284 \edef\pgf@lib@shapedecoration@initialise{\the\pgf@x}%
285 \fi%
286 \fi%
287 \else%
288 %
289 % Between centers.
290 %
291 \pgf@x\pgfdecoratedremainingdistance%
292 \ifnum\c@pgf@counta>1\relax%
293 \advance\c@pgf@counta-1\relax%
294 \divide\pgf@x\c@pgf@counta\relax%
295 \else%
296 \ifnum\c@pgf@counta=1\relax%
297 \[email protected]\pgf@x%
298 \edef\pgf@lib@shapedecoration@initialise{\the\pgf@y}%
299 \else%
300 \advance\pgf@x5pt\relax% An arbitrary value >0pt.
301 \edef\pgf@lib@shapedecoration@initialise{\the\pgf@x}%
302 \fi%
303 \fi%
304 \fi%
305 \edef\pgf@lib@shapedecoration@sep{\the\pgf@x}%
306 \fi%
307 }]
308 {}%
309 \state{before shape}
310 [
311 width=\pgf@lib@shapedecoration@beforeshape-1sp,
312 next state=shape,
313 persistent precomputation=
314 {
315 \ifpgfshapedecorationscaled%
316 \ifpgf@lib@shapedecoration@betweenborders%
317 \ifx\pgf@lib@shapedecoration@spread\pgfutil@empty%
318 %
319 % Not so straightforward. The required ratio is given by
320 %
321 % R = (c+W/2)/(c+r-.5*w)
322 %
323 % c = completed distance
324 % r = remaining distance
325 % W = initial width
326 % w = the change in width (i.e., end - start)
327 %
328 \pgf@x\pgfdecoratedcompleteddistance%
329 \advance\pgf@x\pgfdecoratedremainingdistance%
330 \pgf@xa\pgf@lib@shapedecoration@startwidth\relax%
331 \[email protected]\pgf@xa%
332 \advance\pgf@xa\pgfdecoratedcompleteddistance% c+W/2
333 %
334 \pgf@xb\pgf@lib@shapedecoration@widthchange\relax%
335 \[email protected]\pgf@xb%
336 \advance\pgf@xb\pgf@x% c+r-.5*w
337 %
338 \pgfmathdivide@{\pgfmath@tonumber{\pgf@xa}}{\pgfmath@tonumber{\pgf@xb}}%
339 \fi%
340 \else%
341 %
342 % Easy peasy. The required ratio is
343 %
344 % R = c / (c+r)
345 %
346 \pgf@y\pgfdecoratedcompleteddistance%
347 \advance\pgf@y\pgfdecoratedremainingdistance%
348 \pgfmathdivide@{\pgfmath@tonumber{\pgfdecoratedcompleteddistance}}{\pgfmath@tonumber{\pgf@y}}%
349 \fi%
350 %
351 % Get the new width.
352 %
353 \ifx\pgf@lib@shapedecoration@spread\pgfutil@empty%
354 \pgf@x\pgf@lib@shapedecoration@widthchange\relax%
355 \pgf@x\pgfmathresult\pgf@x%
356 \advance\pgf@x\pgf@lib@shapedecoration@startwidth\relax%
357 \else%
358 \ifpgf@lib@shapedecoration@betweenborders%
359 %
360 % Special case when decoration is scaled, and evenly spread by borders.
361 %
362 \pgf@x\pgf@lib@shapedecoration@width\relax%
363 \advance\pgf@x\pgf@lib@shapedecoration@specialwidth\relax%
364 \pgf@xa\pgf@x%
365 \advance\pgf@xa-\pgf@lib@shapedecoration@startwidth\relax%
366 \pgf@xb\pgf@lib@shapedecoration@widthchange\relax%
367 \pgfmathdivide@{\pgfmath@tonumber{\pgf@xa}}{\pgfmath@tonumber{\pgf@xb}}%
368 \else%
369 \pgf@x\pgf@lib@shapedecoration@widthchange\relax%
370 \pgf@x\pgfmathresult\pgf@x%
371 \advance\pgf@x\pgf@lib@shapedecoration@startwidth\relax%
372 \fi%
373 \fi%
374 \edef\pgf@lib@shapedecoration@width{\the\pgf@x}%
375 %
376 % New height = R*h + H
377 %
378 \pgf@y\pgf@lib@shapedecoration@heightchange\relax%
379 \pgf@y\pgfmathresult\pgf@y%
380 \advance\pgf@y\pgf@lib@shapedecoration@initialheight\relax%
381 \edef\pgf@lib@shapedecoration@height{\the\pgf@y}%
382 \fi%
383 %
384 \ifpgf@lib@shapedecoration@betweenborders%
385 \pgf@x\pgf@lib@shapedecoration@width\relax%
386 \[email protected]\pgf@x%
387 \edef\pgf@lib@shapedecoration@beforeshape{\the\pgf@x}%
388 \else%
389 \def\pgf@lib@shapedecoration@beforeshape{0pt}%
390 \fi%
391 }]
392 {}%
393 \state{shape}[width=+0sp,next state=after shape]
394 {
395 \ifpgfshapedecorationsloped%
396 \else%
397 \pgftransformrotate{-\pgfdecoratedangle}%
398 \fi%
399 %
400 % Scale the path when it is actually drawn.
401 %
402 \pgf@lib@shapedecoration@shapepathsize%
403 \pgfutil@tempdima\pgf@x%
404 \pgfutil@tempdimb\pgf@y%
405 \pgf@xa\pgf@lib@shapedecoration@width\relax%
406 \pgf@xb\pgfutil@tempdima%
407 \pgfmathdivide@{\pgfmath@tonumber{\pgf@xa}}{\pgfmath@tonumber{\pgf@xb}}%
408 \expandafter\pgftransformxscale\expandafter{\pgfmathresult}%
409 %
410 \pgf@ya\pgf@lib@shapedecoration@height\relax%
411 \pgf@yb\pgfutil@tempdimb%
412 \pgfmathdivide@{\pgfmath@tonumber{\pgf@ya}}{\pgfmath@tonumber{\pgf@yb}}%
413 \expandafter\pgftransformyscale\expandafter{\pgfmathresult}%
414 %
415 % Move to the center anchor.
416 %
417 \pgf@lib@shapedecoration@points%
418 \pgf@lib@shapedecoration@macros%
419 \pgftransformshift{%
420 \pgf@sh@reanchor{\pgfkeysvalueof{/pgf/decoration/shape}}{\pgfkeysvalueof{/pgf/decoration/anchor}}%
421 \pgf@x-\pgf@x%
422 \pgf@y-\pgf@y%
423 }%
424 %
425 % And draw the shape path.
426 %
427 \csname pgf@sh@bg@\pgfkeysvalueof{/pgf/decoration/shape}\endcsname%
428 }%
429 \state{after shape}
430 [
431 width=\pgf@lib@shapedecoration@aftershape-1sp,
432 next state=sep,
433 persistent precomputation=
434 {
435 \ifpgf@lib@shapedecoration@betweenborders%
436 \pgf@x\pgf@lib@shapedecoration@width\relax%
437 \[email protected]\pgf@x%
438 \edef\pgf@lib@shapedecoration@aftershape{\the\pgf@x}%
439 \else%
440 \edef\pgf@lib@shapedecoration@aftershape{0pt}%
441 \fi%
442 }
443 ]
444 {}%
445 \state{sep}[width=\pgf@lib@shapedecoration@sep,next state=before shape,
446 persistent precomputation=\def\pgf@lib@shapedecoration@beforeshape{0pt}]
447 {}%
448 \state{final}
449 {
450 \pgfpathmoveto{\pgfpointdecoratedpathlast}%
451 }%
452 }%
453
454 \def\pgf@lib@shapedecoration@setkeyword,{%
455 \pgfutil@ifnextchar\pgf@stop{\def\pgf@temp{}\pgf@lib@@@shapedecoration@setkeyword}{\pgf@lib@@shapedecoration@setkeyword}%
456 }%
457 \def\pgf@lib@@shapedecoration@setkeyword#1,{\def\pgf@temp{#1}\pgf@lib@@@shapedecoration@setkeyword}%
458 \def\pgf@lib@@@shapedecoration@setkeyword\pgf@stop{%
459 \ifx\pgf@temp\pgf@lib@shapedecoration@borderstext%
460 \pgf@lib@shapedecoration@betweenborderstrue%
461 \else%
462 \pgf@lib@shapedecoration@betweenbordersfalse%
463 \fi%
464 }%
465
466
467
468 \endinput