summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/basiclayer/pgfcorescopes.code.tex
blob: 50a820f61265963a366f998781f03babc453641b (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
% Copyright 2006 by Till Tantau
%
% 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.

\ProvidesFileRCS{pgfcorescopes.code.tex}

% Globals

\newbox\pgfpic
\newbox\pgf@hbox

\newbox\pgf@layerbox@main

\newcount\pgf@picture@serial@count



% This if decides whether the position of pictures on the page is
% protocolled or not. Normally,
% this is switched off as it works only with certain drivers and it
% causes external files to be written. When switched on, the position
% of pgfpictures are protocolled and can be referenced using
% \pgfsys@getposition{XXX} where XXX is the value of \pgfpictureid
% inside the picture.
\newif\ifpgfrememberpicturepositiononpage




% Scopes


% Pgf scope environment. All changes of the graphic state are local to
% the scope.
%
% Example:
%
% \begin{pgfscope}
%    \pgfsetlinewidth{3pt}
%    \pgfline{\pgfxy(0,0)}{\pgfxy(3,3)}
% \end{pgfscope}

\def\pgfscope{%
  \pgfsyssoftpath@setcurrentpath\pgfutil@empty%
  \pgfsys@beginscope%
    \pgf@resetpathsizes%
    \edef\pgfscope@linewidth{\the\pgflinewidth}%
    \let\pgfscope@stroke@color=\pgf@strokecolor@global%
    \let\pgfscope@fill@color=\pgf@fillcolor@global%
    \begingroup}
\def\endpgfscope{%
    \endgroup%
    \global\pgflinewidth=\pgfscope@linewidth%
    \global\let\pgf@strokecolor@global=\pgfscope@stroke@color%
    \global\let\pgf@fillcolor@global=\pgfscope@fill@color%
  \pgfsys@endscope}




% Quickly insert a box can contain normal TeX text at the origin.
%
% #1 = box of width/height and depth 0pt
%
% Example:
%
% \pgfqbox{\mybox}

\def\pgfqbox#1{%
  \pgfsys@hbox#1%
}


% Insert a box that can contain normal TeX text at the origin, but
% with the current coordinate transformation matrix synced with the
% low-level transformation matrix.
%
% #1 = box of width/height and depth 0pt
%
% In essence, this command does the same as if you first said
% \pgflowlevelsynccm and then \pgfqbox. However, pgf will use a
% ``TeX-translation'' for the translation part of the transformation
% cm. This will ensure that hyperlinks ``survive'' at least
% translations.
%
% Example:
%
% \pgfqboxsynced{\mybox}

\def\pgfqboxsynced#1{%
  \pgfsys@hboxsynced#1%
}


% Puts some text in a box and inserts it with the current
% transformations applied.
%
% #1 = List of optional positioning. Possible values are ``left'', ``right'',
%      ``top'', ``bottom'' and ``base''.
% #2 = TeX text. May contain verbatims.
%
% Example:
%
% \pgftransformshift{\pgfpoint{1cm}{0cm}}
% \pgftext{Hello World!}

\def\pgftext{\pgfutil@ifnextchar[\pgf@text{\pgf@text[]}}%
\def\pgf@text[#1]{%
  \def\pgf@text@options{#1}%
  \pgf@maketext\pgf@after@text}
\def\pgf@after@text{%
  {%
  \def\pgf@text@hshift{center}%
  \def\pgf@text@vshift{center}%
  \expandafter\pgf@text@setkeys\expandafter{\pgf@text@options}%
  \csname pgf@halign\pgf@text@hshift\endcsname%
  \csname pgf@valign\pgf@text@vshift\endcsname%
  %
  \pgfapproximatenonlineartransformation%
  % Protocol sizes:
  \pgf@process{\pgfpointtransformed{\pgfqpoint{0pt}{\dp\pgf@hbox}}}%
  \pgf@protocolsizes{\pgf@x}{\pgf@y}%
  \pgf@process{\pgfpointtransformed{\pgfqpoint{\wd\pgf@hbox}{\dp\pgf@hbox}}}%
  \pgf@protocolsizes{\pgf@x}{\pgf@y}%
  \pgf@process{\pgfpointtransformed{\pgfqpoint{0pt}{\ht\pgf@hbox}}}%
  \pgf@protocolsizes{\pgf@x}{\pgf@y}%
  \pgf@process{\pgfpointtransformed{\pgfqpoint{\wd\pgf@hbox}{\ht\pgf@hbox}}}%
  \pgf@protocolsizes{\pgf@x}{\pgf@y}%
  \pgfqboxsynced{\pgf@hbox}%
  }%
}
\def\pgf@text@setkeys{\pgfqkeys{/pgf/text}}%

\pgfkeys{/pgf/text/left/.code=\def\pgf@text@hshift{left}}
\pgfkeys{/pgf/text/center/.code=}
\pgfkeys{/pgf/text/right/.code=\def\pgf@text@hshift{right}}
\pgfkeys{/pgf/text/top/.code=\def\pgf@text@vshift{top}}
\pgfkeys{/pgf/text/bottom/.code=\def\pgf@text@vshift{bottom}}
\pgfkeys{/pgf/text/base/.code=\def\pgf@text@vshift{base}}
\pgfkeys{/pgf/text/at/.cd,.code=\pgftransformshift{#1},.value required}
\pgfkeys{/pgf/text/x/.cd,.code=\pgftransformxshift{#1},.value required}
\pgfkeys{/pgf/text/y/.cd,.code=\pgftransformyshift{#1},.value required}
\pgfkeys{/pgf/text/rotate/.cd,.code=\pgftransformrotate{#1},.value required}

\def\pgf@halignleft{}% do nothing
\def\pgf@haligncenter{\pgftransformxshift{+-.5\wd\pgf@hbox}}
\def\pgf@halignright{\pgftransformxshift{+-\wd\pgf@hbox}}%
\def\pgf@valignbase{}% do nothing
\def\pgf@valignbottom{\pgftransformyshift{+\dp\pgf@hbox}}%
\def\pgf@valigncenter{\pgftransformyshift{+.5\dp\pgf@hbox}\pgftransformyshift{+-.5\ht\pgf@hbox}}%
\def\pgf@valigntop{\pgftransformyshift{+-\ht\pgf@hbox}}%


% Internal function for creating a hbox.
\def\pgf@maketext#1{%
  \def\pgf@@maketextafter{#1}%
  \setbox\pgf@hbox=\hbox\bgroup%
    \pgfinterruptpicture%
      \bgroup%
        \aftergroup\pgf@collectresetcolor%
        \let\next=%
}
\def\pgf@collectresetcolor{%
  \pgfutil@ifnextchar\reset@color%
  {\reset@color\afterassignment\pgf@collectresetcolor\let\pgf@temp=}%
  {\pgf@textdone}%
}
\def\pgf@textdone{%
    \endpgfinterruptpicture%
  \egroup%
  \pgf@@maketextafter%
}

\long\def\pgf@makehbox#1{%
  \setbox\pgf@hbox=\hbox{{%
    \pgfinterruptpicture%
      #1%
    \endpgfinterruptpicture%
    }}}



% Picture environment
%
% Example:
%
% \begin{pgfpicture}
%   \pgfsetendarrow{\pgfarrowto}
%   \pgfpathmoveto{\pgfpointxy{-0.9}{0.2}}
%   \pgfpathlineto{\pgfpointxy{0.9}{0.4}}
%   \pgfusepath{stroke}
% \end{pgfpicture}

\def\pgfresetboundingbox{%
    \global\pgf@picmaxx=-16000pt\relax%
    \global\pgf@picminx=16000pt\relax%
    \global\pgf@picmaxy=-16000pt\relax%
    \global\pgf@picminy=16000pt\relax%
}%

\def\pgfpicture{%
  \begingroup%
    \pgfpicturetrue%
    \global\advance\pgf@picture@serial@count by1\relax%
    \edef\pgfpictureid{pgfid\the\pgf@picture@serial@count}%
    \let\pgf@nodecallback=\pgfutil@gobble%
    \pgf@picmaxx=-16000pt\relax%
    \pgf@picminx=16000pt\relax%
    \pgf@picmaxy=-16000pt\relax%
    \pgf@picminy=16000pt\relax%
    \pgf@relevantforpicturesizetrue%
    \pgf@resetpathsizes%
    \pgfutil@ifnextchar\bgroup\pgf@oldpicture\pgf@picture}
\def\pgf@oldpicture#1#2#3#4{%
  \pgfmathsetlength\pgf@picminx{#1}%
  \pgfmathsetlength\pgf@picminy{#2}%
  \pgfmathsetlength\pgf@picmaxx{#3}%
  \pgfmathsetlength\pgf@picmaxy{#4}%
  \pgf@relevantforpicturesizefalse%
  \pgf@picture}

\def\pgf@picture{%
  \setbox\pgfpic\hbox to0pt\bgroup%
    \begingroup%
    \pgfsys@beginpicture%
      \pgfsys@beginscope%
        \begingroup%
        \pgfsetcolor{.}%
        \pgfsetlinewidth{0.4pt}%
        \pgftransformreset%
        \pgfsyssoftpath@setcurrentpath\pgfutil@empty%
        \begingroup%
          \let\pgf@setlengthorig=\setlength%
          \let\pgf@addtolengthorig=\addtolength%
          \let\pgf@selectfontorig=\selectfont%
          \let\setlength=\pgf@setlength%
          \let\addtolength=\pgf@addtolength%
          \let\selectfont=\pgf@selectfont%
          \nullfont\spaceskip0pt\xspaceskip0pt%
          \setbox\pgf@layerbox@main\hbox to0pt\bgroup%
            \begingroup%
  }
\def\endpgfpicture{%
              \ifpgfrememberpicturepositiononpage%
                \hbox to0pt{\pgfsys@markposition{\pgfpictureid}}%
              \fi%
              % ok, now let's position the box
              \ifdim\pgf@picmaxx=-16000pt\relax%
                % empty picture. make size 0.
                \global\pgf@picmaxx=0pt\relax%
                \global\pgf@picminx=0pt\relax%
                \global\pgf@picmaxy=0pt\relax%
                \global\pgf@picminy=0pt\relax%
              \fi%
              % Shift baseline outside:
              \pgf@relevantforpicturesizefalse%
              \pgf@process{\pgf@baseline}%
              \xdef\pgf@shift@baseline{\the\pgf@y}%
              %
              \pgf@process{\pgf@trimleft}%
              \global\advance\pgf@x by-\pgf@picminx
              % prepare \hskip\pgf@trimleft@final.
              % note that \pgf@trimleft@final is also queried
              % by the pgf image externalization.
              \xdef\pgf@trimleft@final{-\the\pgf@x}%
              %
              \pgf@process{\pgf@trimright}%
              \global\advance\pgf@x by-\pgf@picmaxx
              % prepare \hskip\pgf@trimright@final.
              % note that \pgf@trimright@final is also queried
              % by the pgf image externalization.
              \xdef\pgf@trimright@final{\the\pgf@x}%
        %
        \pgf@remember@layerlist@globally
            \endgroup%
            \hss%
          \egroup%
      \pgf@restore@layerlist@from@global
          \pgf@insertlayers%
        \endgroup%
        \pgfsys@discardpath%
        \endgroup%
      \pgfsys@endscope%
    \pgfsys@endpicture%
    \endgroup%
    \hss
  \egroup%
  \pgfsys@typesetpicturebox\pgfpic%
  \endgroup%
}

\def\pgf@remember@layerlist@globally{%
  \global\let\pgf@layerlist@=\pgf@layerlist
}%
\def\pgf@restore@layerlist@from@global{%
  \let\pgf@layerlist=\pgf@layerlist@
}%
\def\pgf@insertlayers{%
  \box\pgf@layerbox@main%
}

\def\pgf@selectfont{\pgf@selectfontorig\nullfont}

\def\pgf@setlength#1#2{% these will be used only when \nullfont is active
  \begingroup% keep font setting local
    \pgfutil@selectfont% restore font
    \pgf@setlengthorig#1{#2}% calculate dimension (possibly using calc)
    \expandafter%
  \endgroup%
  \expandafter#1\expandafter=\the#1\relax}
\def\pgf@addtolength#1#2{%
  \begingroup% keep font setting local
    \pgfutil@selectfont% restore font
    \pgf@addtolengthorig#1{#2}% calculate dimension (possibly using calc)
    \expandafter%
  \endgroup%
  \expandafter#1\expandafter=\the#1\relax}


% Sets the baseline at the y-coordinate of a given point
%
% #1 = point
%
% Sets the baseline of the picture to the y-coordinate of a given
% point. However, the point will be evaluated *at the end of the
% picture*.
%
% Example:
%
% \pgfsetbaselinepointlater{\pgfpointanchor{mynode}{base}}

\def\pgfsetbaselinepointlater#1{\def\pgf@baseline{#1}}


% Sets the baseline at the y-coordinate of a given point, now
%
% #1 = point
%
% Sets the baseline of the picture to the y-coordinate of a given
% point.
%
% Example:
%
% \pgfsetbaselinepointnow{\pgfpoint{1cm}{2pt}}

\def\pgfsetbaselinepointnow#1{%
  \pgf@process{#1}%
  \edef\pgf@setter@baseline{\noexpand\pgfpoint{\the\pgf@x}{\the\pgf@y}}%
  \pgfsetbaselinepointlater{\pgf@setter@baseline}%
}

\def\pgf@default@text{default}%

% Sets the baseline
%
% #1 = baseline
%
% Sets the baseline of the picture. Default is the lower border, which
% is the same as \pgf@picminy
%
% Example:
%
% \pgfsetbaseline{1cm+2pt}
% \pgfsetbaseline{default}% resets to default value

\def\pgfsetbaseline#1{%
  \def\pgf@temp{#1}%
  \ifx\pgf@temp\pgf@default@text
    \pgfsetbaseline{\pgf@picminy}%
  \else
    \pgfsetbaselinepointlater{\pgfpoint{0pt}{#1}}%
  \fi
}
\pgfsetbaseline{\pgf@picminy}

% controls how the image externalization implements trim:
\newif\ifpgf@trim@lowlevel
\pgfkeys{
  /pgf/trim lowlevel/.is if=pgf@trim@lowlevel,
  /pgf/trim lowlevel/.default=true,
}

% Same as the y-baseline for horizontal alignment.
% The effect is different, though: it is some kind of trimming which
% leaves the bounding box intact.
\def\pgfsettrimleftpointlater#1{\def\pgf@trimleft{#1}}
\def\pgfsettrimleftpointnow#1{%
  \pgf@process{#1}%
  \edef\pgf@setter@baseline{\noexpand\pgfpoint{\the\pgf@x}{\the\pgf@y}}%
  \pgfsettrimleftpointlater{\pgf@setter@baseline}%
}
% \pgfsettrimleft{<x coord>}
% or
% \pgfsettrimleft{default}
\def\pgfsettrimleft#1{%
  \def\pgf@temp{#1}%
  \ifx\pgf@temp\pgf@default@text
    \pgfsettrimleft{\pgf@picminx}
  \else
    \pgfsettrimleftpointlater{\pgfpoint{#1}{0pt}}%
  \fi
}
\pgfsettrimleft{\pgf@picminx}

\def\pgfsettrimrightpointlater#1{\def\pgf@trimright{#1}}
\def\pgfsettrimrightpointnow#1{%
  \pgf@process{#1}%
  \edef\pgf@setter@baseline{\noexpand\pgfpoint{\the\pgf@x}{\the\pgf@y}}%
  \pgfsettrimrightpointlater{\pgf@setter@baseline}%
}
% \pgfsettrimright{<x coord>}
% or
% \pgfsettrimright{default}
\def\pgfsettrimright#1{%
  \def\pgf@temp{#1}%
  \ifx\pgf@temp\pgf@default@text
    \pgfsettrimright{\pgf@picmaxx}%
  \else
    \pgfsettrimrightpointlater{\pgfpoint{#1}{0pt}}%
  \fi
}
\pgfsettrimright{\pgf@picmaxx}


% Interrupt path
%
% Description:
%
% The environment can be used to insert some drawing commands while
% constructing a path. The drawing commands inside the environment
% will not interfere with the path being constructed ``outside.''
% However, you must ward against graphic state changes using a scope.
%
% Example: Draw two parallel lines
%
% \pgfmoveto{\pgfpoint{0cm}{0cm}}
% \begin{pgfinterruptpath}
%   \pgfmoveto{\pgfpoint{1cm}{0cm}}
%   \pgfmoveto{\pgfpoint{1cm}{1cm}}
%   \pgfusepath{stroke}
% \end{pgfinterruptpath}
% \pgflineto{\pgfpoint{0cm}{1cm}}
% \pgfusepath{stroke}

\def\pgfinterruptpath
{%
  \begingroup%
  % save all sorts of things...
  \edef\pgf@interrupt@savex{\the\pgf@path@lastx}%
  \edef\pgf@interrupt@savey{\the\pgf@path@lasty}%
  \pgf@getpathsizes\pgf@interrupt@pathsizes%
  \pgfsyssoftpath@getcurrentpath\pgf@interrupt@path%
  \pgfsyssoftpath@setcurrentpath\pgfutil@empty%
  \edef\pgfscope@linewidth{\the\pgflinewidth}%
  \let\pgf@interrupt@lastmoveto=\pgfsyssoftpath@lastmoveto%
  \begingroup%
}
\def\endpgfinterruptpath
{%
  \endgroup%
  \global\pgflinewidth=\pgfscope@linewidth%
  \pgfsyssoftpath@setcurrentpath\pgf@interrupt@path%
  \pgf@setpathsizes\pgf@interrupt@pathsizes%
  \global\pgf@path@lastx\pgf@interrupt@savex%
  \global\pgf@path@lasty\pgf@interrupt@savey%
  \global\let\pgfsyssoftpath@lastmoveto\pgf@interrupt@lastmoveto%
  \endgroup%
}



% Interrupt bounding box
%
% Description:
%
% The environment can be used to temporarily setup a new bounding box
% computation. The bounding box will be made empty at the beginning of
% the environment and will be reset to its old value after the
% environment.
%
% Example:
%
% \begin{pgfinterruptboundinbox}
%   \pgfmoveto{\pgfpoint{1cm}{0cm}}
%   \pgfmoveto{\pgfpoint{1cm}{1cm}}
%   \pgfusepath{stroke}
% \end{pgfinterruptboundinbox}

\def\pgfinterruptboundingbox
{%
  \begingroup%
    \edef\pgf@interrupt@savemaxx{\the\pgf@picmaxx}%
    \edef\pgf@interrupt@saveminx{\the\pgf@picminx}%
    \edef\pgf@interrupt@savemaxy{\the\pgf@picmaxy}%
    \edef\pgf@interrupt@saveminy{\the\pgf@picminy}%
    \pgf@picmaxx=-16000pt\relax%
    \pgf@picminx=16000pt\relax%
    \pgf@picmaxy=-16000pt\relax%
    \pgf@picminy=16000pt\relax%
    \pgf@size@hookedfalse%
    \let\pgf@path@size@hook=\pgfutil@empty%
}
\def\endpgfinterruptboundingbox
{%
    \global\pgf@picmaxx=\pgf@interrupt@savemaxx%
    \global\pgf@picmaxy=\pgf@interrupt@savemaxy%
    \global\pgf@picminx=\pgf@interrupt@saveminx%
    \global\pgf@picminy=\pgf@interrupt@saveminy%
  \endgroup%
}




% Creates an id scope.
%
% Description:
%
% An id scope is not (conceptually) a graphic scope, but a scope that
% has a unique name or "id". This id can be used as a reference point
% for instance for a hyperlink or for an animation (currently, only
% SVG supports ids, all other drivers ignore id scopes).
%
% Prior to using \pgfidscope, call \pgfuseid to install an
% id (and possibly \pgfusetype). The scope will then get this id and,
% likely, a graphic scope is installed. If \pgfuseid has not been
% called or if you try to call \pgfidscope again for the same id,
% nothing happens (except for a \TeX\ group being established).

\def\pgfidscope{\pgfsys@begin@idscope}
\def\endpgfidscope{\pgfsys@end@idscope}

\let\startpgfidscope\pgfidscope
\let\stoppgfidscope\pgfendidscope




% Use an id on the next graphic object
%
% #1 = a name
%
% The next scope or path can be the target of an animation command or
% a hyperlink.

\def\pgfuseid#1{%
  \edef\pgf@temp{#1}%
  \ifx\pgf@temp\pgfutil@empty%
  \else%
    \pgf@lookup@id{#1}%
    \pgfsys@use@id{\pgf@next@id}%
    \expandafter\xdef\csname pgf@id@names@#1\endcsname{{\pgf@next@id}{}}%
  \fi%
}

\def\pgf@lookup@id#1{%
  \expandafter\let\expandafter\pgf@id@name\csname pgf@id@names@#1\endcsname%
  \ifx\pgf@id@name\relax%
    \let\pgf@prev@id\pgfutil@empty%
    \let\pgf@next@id\pgfutil@empty%
  \else%
    \expandafter\pgf@id@parse\pgf@id@name%
  \fi%
  \ifx\pgf@next@id\pgfutil@empty%
    \pgfsys@new@id\pgf@next@id%
    \expandafter\xdef\csname pgf@id@names@#1\endcsname{{\pgf@prev@id}{\pgf@next@id}}%
  \fi%
}
\def\pgf@id@parse#1#2{%
  \def\pgf@prev@id{#1}%
  \def\pgf@next@id{#2}%
}



% Clear the current id
%
% Description:
%
% Sets the current id to be empty so that it cannot be referenced.

\def\pgfclearid{%
  \pgfsys@clear@id%
}


% Get the id that will be given to a name upon the next use
%
% #1 = a macro
% #2 = a name
%
% Description:
%
% #1 will get the id that will be used by \pgfuseid next time for the
% given name.

\def\pgfidrefnextuse#1#2{%
  \edef\pgf@temp{#2}\ifx\pgf@temp\pgfutil@empty\pgferror{Missing name reference for ``\string#1''}\fi%
  \pgf@lookup@id{#2}%
  \let#1\pgf@next@id%
}


% Get the id was last used
%
% #1 = a macro
% #2 = a name
%
% Description:
%
% #1 will get the id that was last used by \pgfuseid for #2.

\def\pgfidrefprevuse#1#2{%
  \edef\pgf@temp{#2}\ifx\pgf@temp\pgfutil@empty\pgferror{Missing name reference for ``\string#1''}\fi%
  \pgf@lookup@id{#2}%
  \let#1\pgf@prev@id%
}


% Use a type
%
% #1 = an id type; when starting with a dot, it is added to the
% current type.
%
% The next scope or path can be the target of an animation command or
% a hyperlink.

\def\pgfusetype#1{%
  \edef\pgf@temp{#1}%
  \expandafter\pgfutil@ifnextchar\expandafter.\expandafter\pgf@id@use@type@add\expandafter\pgf@id@use@type@replace\pgf@temp\pgf@stop
}
\def\pgf@id@use@type@add.#1\pgf@stop{\pgfsys@append@type{#1}}
\def\pgf@id@use@type@replace#1\pgf@stop{\pgfsys@use@type{#1}}

% Push the current type on a global stack
%
% Description:
%
% The current type is pushed onto a global type stack. It can be
% restored using \pgfpoptype.

\def\pgfpushtype{\pgfsys@push@type}


% Pop a type from the type stack
%
% Description:
%
% Restores the most current value from the type stack.

\def\pgfpoptype{\pgfsys@pop@type}




% Alias an id locally or globally
%
% #1 = the alias name
% #2 = a existing id name
%
% Description:
%
% Creates an alias of a name

\def\pgfaliasid#1#2{%
  \expandafter\let\expandafter\pgf@temp\csname pgf@id@names@#2\endcsname%
  \expandafter\let\csname pgf@id@names@#1\endcsname\pgf@temp%
}
\def\pgfgaliasid#1#2{%
  \expandafter\let\expandafter\pgf@temp\csname pgf@id@names@#2\endcsname%
  \expandafter\global\expandafter\let\csname pgf@id@names@#1\endcsname\pgf@temp%
}


% Check whether a name has been forward-referenced
%
% #1 = a name
% #2 = what to do, when has been forward-referenced
% #3 = what to do otherwise
%
% Description:
%
% Depending on whether #1 has been forward-referenced, #2 or #3 will be executed.

\def\pgfifidreferenced#1#2#3{%
  \expandafter\let\expandafter\pgf@id@name\csname pgf@id@names@#1\endcsname%
  \ifx\pgf@id@name\relax%
    #3%
  \else
    \expandafter\pgf@id@parse\pgf@id@name%
    \ifx\pgf@next@id\pgfutil@empty%
      #3%
    \else%
      #2%
    \fi%
  \fi%
}


% Interrupts a picture
%
% Description:
%
% This environment interrupts a picture and temporarily returns to
% normal TeX mode. All sorts of things are saved and restored by this
% environment.
%
% WARNING: Using this environment in conjunction with low level
% transformations can *strongly* upset the typesetting. Typically, the
% contents of this environment should have size/height/depth 0pt in
% the end.
%
% WARNING: This environment should only be used inside typesetting a
% box and this box must in turn be inserted using \pgfqbox.
%
% Example: Draw two parallel lines
%
% \pgfmoveto{\pgfpoint{0cm}{0cm}}
% \setbox\mybox=\hbox{
%    \begin{pgfinterruptpicture}
%      This is normal text.
%      \begin{pgfpicture} % a subpicture
%        \pgfmoveto{\pgfpoint{1cm}{0cm}}
%        \pgfmoveto{\pgfpoint{1cm}{1cm}}
%        \pgfusepath{stroke}
%      \end{pgfpicture}
%      More text.
%    \end{pgfinterruptpicture}
%  }
% \ht\mybox=0pt
% \wd\mybox=0pt
% \dp\mybox=0pt
% \pgfqbox{\mybox}%
% \pgfpathlineto{\pgfpoint{0cm}{1cm}}
% \pgfusepath{stroke}

\def\pgfinterruptpicture
{%
  \begingroup%
    \pgfinterruptboundingbox%
      \pgftransformreset%
      \pgfinterruptpath%
        \ifx\pgf@selectfontorig\@undefined%
        \else%
          \let\setlength\pgf@setlengthorig%
          \let\addtolength\pgf@addtolengthorig%
          \let\selectfont\pgf@selectfontorig%
        \fi%
        \pgfutil@selectfont%
        \pgfpicturefalse%
        \let\pgf@positionnodelater@macro\relax%
        \pgf@savelayers%
}
\def\endpgfinterruptpicture
{%
        \pgf@restorelayers%
      \endpgfinterruptpath%
    \endpgfinterruptboundingbox%
  \endgroup%
}

\let\pgf@savelayers=\relax
\let\pgf@restorelayers=\relax

\endinput