summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/libraries/decorations/pgflibrarydecorations.shapes.code.tex
blob: 6b1571518b6f97cb599291a5b850f0239f96ef38 (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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
% Copyright 2008 by Mark Wibrow
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\usepgfmodule{decorations}%


% Options for shape decorations

\pgfkeys{
  /pgf/decoration/.cd,
  shape/.initial=circle,
  anchor/.initial=center,
  shape start width/.initial=2.5pt,
  shape start height/.initial=2.5pt,
  shape end width/.initial=2.5pt,
  shape end height/.initial=2.5pt,
  shape sep/.store in=\pgf@lib@shapedecoration@sep,
  shape sloped/.is if=pgfshapedecorationsloped,
  shape scaled/.is if=pgfshapedecorationscaled,
  shape evenly spread/.store in=\pgf@lib@shapedecoration@spread,
  shape start size/.style={%
    shape start width=#1,
    shape start height=#1%
  },%
  shape end size/.style={%
    shape end width=#1,
    shape end height=#1%
  },%
  shape size/.style={%
    shape start size=#1,
    shape end size=#1%
  },%
  shape width/.style={%
    shape start width=#1,
    shape end width=#1
  },
  shape height/.style={%
    shape start height=#1,
    shape end height=#1
  }
}%

\def\pgf@lib@shapedecoration@sep{.25cm, between centers}%
\newif\ifpgfshapedecorationsloped
\pgfshapedecorationslopedtrue
\newif\ifpgfshapedecorationscaled
\let\pgf@lib@shapedecoration@spread\pgfutil@empty%



% triangle decoration

\pgfdeclaredecoration{triangles}{init}
{%
  \state{init}[width=+0pt,next state=triangle,persistent precomputation={
    \pgfmathparse{\pgfkeysvalueof{/pgf/decoration/shape start width}}
    \edef\pgf@lib@dec@ssw{\pgfmathresult pt}
    \pgfmathparse{\pgfkeysvalueof{/pgf/decoration/shape start height}/2}
    \edef\pgf@lib@dec@ssh{\pgfmathresult pt}
  }]{}%
  \state{triangle}[switch if less than=+\pgfdecorationsegmentlength to last,
                   width=+\pgfdecorationsegmentlength]
  {
    \pgfpathmoveto{\pgfqpoint{0pt}{\pgf@lib@dec@ssh}}
    \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{0pt}}
    \pgfpathlineto{\pgfqpoint{0pt}{-\pgf@lib@dec@ssh}}
    \pgfpathclose
  }%
  \state{last}[switch if less than=\pgf@lib@dec@ssw to skip,
               width=\pgfdecoratedremainingdistance,next state=final]
  {
    \pgfpathmoveto{\pgfqpoint{0pt}{\pgf@lib@dec@ssh}}
    \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{0pt}}
    \pgfpathlineto{\pgfqpoint{0pt}{-\pgf@lib@dec@ssh}}
    \pgfpathclose
  }%
  \state{skip}[width=\pgfdecoratedremainingdistance]
  {}%
  \state{final}
  {
    \pgfpathmoveto{\pgfpointdecoratedpathlast}
  }%
}%




% crosses decoration

\pgfdeclaredecoration{crosses}{init}
{%
  \state{init}[width=+0pt,next state=crosses,persistent precomputation={
    \pgfmathparse{\pgfkeysvalueof{/pgf/decoration/shape start width}/2}
    \edef\pgf@lib@dec@ssw{\pgfmathresult pt}
    \pgfmathparse{\pgfkeysvalueof{/pgf/decoration/shape start height}/2}
    \edef\pgf@lib@dec@ssh{\pgfmathresult pt}
  }]{}%
  \state{crosses}[switch if less than=+\pgfdecorationsegmentlength to last,
                  width=+\pgfdecorationsegmentlength]
  {
    \pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
    \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
    \pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
    \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
  }%
  \state{last}[width=+\pgfdecoratedremainingdistance]
  {
    \pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
    \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
    \pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
    \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
  }%
  \state{final}{
    \pgfpathmoveto{\pgfpointdecoratedpathlast}
  }%
}%






% The shape background decoration
%	
% The shape background decoration adds repeated instances of
% the background path of a specified shape along the path. The shape
% must have been declared  by \pgfdeclareshape. If a shape has
% specialized keys (e.g. the number of points on a star, or the apex
% angle the isosceles triangle), these can be specified in the usual manner.
%	
% The separation between shapes in the path can be specified and can
% be between the center of the shape or the border of the shape.
%	
% The height and width of the shape can be independently or
% simultaneously scaled (linearly) along the path. It is also
% possible to prevent the shapes being sloped parallel to the
% path.

% internal if
\newif\ifpgf@lib@shapedecoration@betweenborders

\edef\pgf@lib@shapedecoration@initialise{0pt}%

\pgfdeclaredecoration{shape backgrounds}{initialise}
{%
  \state{initialise}
  [
    width=+\pgf@lib@shapedecoration@initialise,
    next state=shape,
    persistent precomputation=
    {
      %
      % \egroup ends the group started by the automaton before executing
      % a decoration state. This prevents the need for (most) \global variables.
      %
      %
      % Check the shape exists.
      %
      \pgfutil@ifundefined{pgf@sh@bg@\pgfkeysvalueof{/pgf/decoration/shape}}{%
        \pgferror{I do not know the shape `\pgfkeysvalueof{/pgf/decoration/shape}',
          so I cannot use it in a decoration. Check if its library been loaded or if you
          simply mistyped the name}}{}%
      %
      % Calculate a `default' path size.
      %
      \pgfinterruptpath%
        \pgfinterruptboundingbox%
          \pgftransformreset%
          \pgf@relevantforpicturesizetrue%
          %
          % This size of this shape is unimportant, but it should
          % be just large/small enough to avoid huge errors when
          % calculating the scaling factors later on.
          %
          \pgfkeys{/pgf/inner sep=50pt, /pgf/minimum size=1pt}% Arbitrary lengths.
          \setbox\pgfnodeparttextbox\hbox{}% Assume shape does nothing special if box is empty.
          \let\pgf@sh@savedmacros\pgfutil@empty%
          \let\pgf@sh@savedpoints\pgfutil@empty%
          \csname pgf@sh@s@\pgfkeysvalueof{/pgf/decoration/shape}\endcsname%
          \pgf@sh@savedpoints%
          \pgf@sh@savedmacros%
          %
          % Save the macros and points.
          %
          \expandafter\gdef\expandafter\pgf@lib@shapedecoration@points\expandafter{\pgf@sh@savedpoints}%
          \expandafter\gdef\expandafter\pgf@lib@shapedecoration@macros\expandafter{\pgf@sh@savedmacros}%
          \csname pgf@sh@bg@\pgfkeysvalueof{/pgf/decoration/shape}\endcsname%
          %
          % Save the dimensions of the shape path.
          %
          \pgf@x\pgf@picmaxx%
          \pgf@y\pgf@picmaxy%
          \advance\pgf@x-\pgf@picminx%
          \advance\pgf@y-\pgf@picminy%
          \xdef\pgf@lib@shapedecoration@shapepathsize{%
            \noexpand\pgf@x\the\pgf@x%
            \noexpand\pgf@y\the\pgf@y%
          }%
        \endpgfinterruptboundingbox%
      \endpgfinterruptpath%
      %
      \edef\pgf@lib@shapedecoration@beforeshape{0pt}%
      \edef\pgf@lib@shapedecoration@aftershape{0pt}%
      %
      \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/decoration/shape start width}}%
      \edef\pgf@lib@shapedecoration@startwidth{\the\pgf@x}%
      \edef\pgf@lib@shapedecoration@width{\the\pgf@x}%
      \pgf@x-\pgf@x%
      \pgfmathaddtolength\pgf@x{\pgfkeysvalueof{/pgf/decoration/shape end width}}%
      \edef\pgf@lib@shapedecoration@widthchange{\the\pgf@x}%
      %
      \pgfmathsetlength\pgf@y{\pgfkeysvalueof{/pgf/decoration/shape start height}}%
      \edef\pgf@lib@shapedecoration@initialheight{\the\pgf@y}%
      \edef\pgf@lib@shapedecoration@height{\the\pgf@y}%
      \pgf@y-\pgf@y%
      \pgfmathaddtolength\pgf@y{\pgfkeysvalueof{/pgf/decoration/shape end height}}%
      \edef\pgf@lib@shapedecoration@heightchange{\the\pgf@y}%		
      %
      % Calculate the sep.
      %
      \ifx\pgf@lib@shapedecoration@spread\pgfutil@empty%
        %
        % Not spreading, so easy:
        %
        \def\pgf@lib@shapedecoration@borderstext{between borders}%
        \afterassignment\pgf@lib@shapedecoration@setkeyword%
        \expandafter\pgf@x\pgf@lib@shapedecoration@sep,\pgf@stop%
        \edef\pgf@lib@shapedecoration@sep{\the\pgf@x}%
      \else%
        %
        % Spreading (a bit of a nuiscence actually).
        %
        \def\pgf@lib@shapedecoration@borderstext{by borders}%
        \afterassignment\pgf@lib@shapedecoration@setkeyword%
        \expandafter\c@pgf@counta\pgf@lib@shapedecoration@spread,\pgf@stop%
        \ifpgf@lib@shapedecoration@betweenborders%
          %
          % Ok. The required sep between borders is:
          %
          % (r -(n-1)((a+b)/2))/(n-1)
          %
          % r = decoration length (here, the remaining distance)
          % a = initial width
          % b = end width
          % n = the number of shapes
          %
          \ifnum\c@pgf@counta>1\relax%
            \advance\c@pgf@counta-1\relax%
            \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/decoration/shape start width}}%
            \ifpgfshapedecorationscaled%
              \pgfmathaddtolength\pgf@x{\pgfkeysvalueof{/pgf/decoration/shape end width}}%
            \else%
              \advance\pgf@x\pgf@x%
            \fi%
            \pgf@x.5\pgf@x% (a+b)/2
            \multiply\pgf@x-\c@pgf@counta% -(n-1)((a+b)/2)
            \advance\pgf@x\pgfdecoratedremainingdistance%
            \divide\pgf@x\c@pgf@counta%
            \pgf@x.9999\pgf@x% Hackery to control some native TeX inaccuracies.
            %
            % Unfortunately if the shape is scaled, and evenly spread by borders,
            % it is necessary to do something a bit different to control for
            % (most) inaccuracies.
            %
            \ifpgfshapedecorationscaled%
              \pgf@xa\pgf@lib@shapedecoration@widthchange\relax%
              \divide\pgf@xa\c@pgf@counta%
              \edef\pgf@lib@shapedecoration@specialwidth{\the\pgf@xa}%		
            \fi%
          \else%
            \pgf@lib@shapedecoration@betweenbordersfalse%
            \pgf@x\pgfdecoratedremainingdistance%
            \ifnum\c@pgf@counta=1\relax%
              \pgf@y.5\pgf@x%
              \edef\pgf@lib@shapedecoration@initialise{\the\pgf@y}%
            \else%
              \advance\pgf@x5pt\relax% An arbitrary value >0pt.
              \edef\pgf@lib@shapedecoration@initialise{\the\pgf@x}%
            \fi%
          \fi%	
        \else%
          %
          % Between centers.
          %
          \pgf@x\pgfdecoratedremainingdistance%
          \ifnum\c@pgf@counta>1\relax%
            \advance\c@pgf@counta-1\relax%
            \divide\pgf@x\c@pgf@counta\relax%
          \else%
            \ifnum\c@pgf@counta=1\relax%
              \pgf@y.5\pgf@x%
              \edef\pgf@lib@shapedecoration@initialise{\the\pgf@y}%
            \else%
              \advance\pgf@x5pt\relax% An arbitrary value >0pt.
              \edef\pgf@lib@shapedecoration@initialise{\the\pgf@x}%
            \fi%
          \fi%
        \fi%
        \edef\pgf@lib@shapedecoration@sep{\the\pgf@x}%
      \fi%
    }]
  {}%
  \state{before shape}
  [
    width=\pgf@lib@shapedecoration@beforeshape-1sp,
    next state=shape,
    persistent precomputation=
    {
      \ifpgfshapedecorationscaled%
        \ifpgf@lib@shapedecoration@betweenborders%
          \ifx\pgf@lib@shapedecoration@spread\pgfutil@empty%
            %
            % Not so straightforward. The required ratio is given by
            %
            % R = (c+W/2)/(c+r-.5*w)
            %
            % c = completed distance
            % r = remaining distance
            % W = initial width
            % w = the change in width (i.e., end - start)
            %
            \pgf@x\pgfdecoratedcompleteddistance%
            \advance\pgf@x\pgfdecoratedremainingdistance%
            \pgf@xa\pgf@lib@shapedecoration@startwidth\relax%
            \pgf@xa.5\pgf@xa%
            \advance\pgf@xa\pgfdecoratedcompleteddistance% c+W/2
            %
            \pgf@xb\pgf@lib@shapedecoration@widthchange\relax%
            \pgf@xb-.5\pgf@xb%
            \advance\pgf@xb\pgf@x% c+r-.5*w
            %
            \pgfmathdivide@{\pgfmath@tonumber{\pgf@xa}}{\pgfmath@tonumber{\pgf@xb}}%
          \fi%
        \else%
          %
          % Easy peasy. The required ratio is
          %
          % R = c / (c+r)
          %
          \pgf@y\pgfdecoratedcompleteddistance%
          \advance\pgf@y\pgfdecoratedremainingdistance%
          \pgfmathdivide@{\pgfmath@tonumber{\pgfdecoratedcompleteddistance}}{\pgfmath@tonumber{\pgf@y}}%			
        \fi%
        %
        % Get the new width.
        %
        \ifx\pgf@lib@shapedecoration@spread\pgfutil@empty%
          \pgf@x\pgf@lib@shapedecoration@widthchange\relax%
          \pgf@x\pgfmathresult\pgf@x%
          \advance\pgf@x\pgf@lib@shapedecoration@startwidth\relax%
        \else%
          \ifpgf@lib@shapedecoration@betweenborders%
            %
            % Special case when decoration is scaled, and evenly spread by borders.
            %
            \pgf@x\pgf@lib@shapedecoration@width\relax%
            \advance\pgf@x\pgf@lib@shapedecoration@specialwidth\relax%
            \pgf@xa\pgf@x%
            \advance\pgf@xa-\pgf@lib@shapedecoration@startwidth\relax%
            \pgf@xb\pgf@lib@shapedecoration@widthchange\relax%
            \pgfmathdivide@{\pgfmath@tonumber{\pgf@xa}}{\pgfmath@tonumber{\pgf@xb}}%
          \else%
            \pgf@x\pgf@lib@shapedecoration@widthchange\relax%
            \pgf@x\pgfmathresult\pgf@x%
            \advance\pgf@x\pgf@lib@shapedecoration@startwidth\relax%
          \fi%
        \fi%		
        \edef\pgf@lib@shapedecoration@width{\the\pgf@x}%		
        %
        % New height = R*h + H
        %
        \pgf@y\pgf@lib@shapedecoration@heightchange\relax%
        \pgf@y\pgfmathresult\pgf@y%
        \advance\pgf@y\pgf@lib@shapedecoration@initialheight\relax%
        \edef\pgf@lib@shapedecoration@height{\the\pgf@y}%
      \fi%
      %
      \ifpgf@lib@shapedecoration@betweenborders%
        \pgf@x\pgf@lib@shapedecoration@width\relax%
        \pgf@x.5\pgf@x%
        \edef\pgf@lib@shapedecoration@beforeshape{\the\pgf@x}%
      \else%
        \def\pgf@lib@shapedecoration@beforeshape{0pt}%
      \fi%	
    }]
  {}%
  \state{shape}[width=+0sp,next state=after shape]
  {
    \ifpgfshapedecorationsloped%
    \else%
      \pgftransformrotate{-\pgfdecoratedangle}%
    \fi%
    %
    % Scale the path when it is actually drawn.
    %
    \pgf@lib@shapedecoration@shapepathsize%
    \pgfutil@tempdima\pgf@x%
    \pgfutil@tempdimb\pgf@y%
    \pgf@xa\pgf@lib@shapedecoration@width\relax%
    \pgf@xb\pgfutil@tempdima%
    \pgfmathdivide@{\pgfmath@tonumber{\pgf@xa}}{\pgfmath@tonumber{\pgf@xb}}%
    \expandafter\pgftransformxscale\expandafter{\pgfmathresult}%
    %
    \pgf@ya\pgf@lib@shapedecoration@height\relax%
    \pgf@yb\pgfutil@tempdimb%
    \pgfmathdivide@{\pgfmath@tonumber{\pgf@ya}}{\pgfmath@tonumber{\pgf@yb}}%
    \expandafter\pgftransformyscale\expandafter{\pgfmathresult}%
    %
    % Move to the center anchor.
    %
    \pgf@lib@shapedecoration@points%
    \pgf@lib@shapedecoration@macros%
    \pgftransformshift{%
      \pgf@sh@reanchor{\pgfkeysvalueof{/pgf/decoration/shape}}{\pgfkeysvalueof{/pgf/decoration/anchor}}%
      \pgf@x-\pgf@x%
      \pgf@y-\pgf@y%
    }%
    %
    % And draw the shape path.
    %
    \csname pgf@sh@bg@\pgfkeysvalueof{/pgf/decoration/shape}\endcsname%	
  }%
  \state{after shape}
  [
    width=\pgf@lib@shapedecoration@aftershape-1sp,
    next state=sep,
    persistent precomputation=
    {
      \ifpgf@lib@shapedecoration@betweenborders%
        \pgf@x\pgf@lib@shapedecoration@width\relax%
        \pgf@x.5\pgf@x%
        \edef\pgf@lib@shapedecoration@aftershape{\the\pgf@x}%
      \else%
        \edef\pgf@lib@shapedecoration@aftershape{0pt}%
      \fi%
    }
  ]
  {}%
  \state{sep}[width=\pgf@lib@shapedecoration@sep,next state=before shape,
              persistent precomputation=\def\pgf@lib@shapedecoration@beforeshape{0pt}]
  {}%
  \state{final}
  {
    \pgfpathmoveto{\pgfpointdecoratedpathlast}%
  }%
}%

\def\pgf@lib@shapedecoration@setkeyword,{%
  \pgfutil@ifnextchar\pgf@stop{\def\pgf@temp{}\pgf@lib@@@shapedecoration@setkeyword}{\pgf@lib@@shapedecoration@setkeyword}%
}%
\def\pgf@lib@@shapedecoration@setkeyword#1,{\def\pgf@temp{#1}\pgf@lib@@@shapedecoration@setkeyword}%
\def\pgf@lib@@@shapedecoration@setkeyword\pgf@stop{%
  \ifx\pgf@temp\pgf@lib@shapedecoration@borderstext%
    \pgf@lib@shapedecoration@betweenborderstrue%
  \else%
    \pgf@lib@shapedecoration@betweenbordersfalse%
  \fi%
}%



\endinput