-
Notifications
You must be signed in to change notification settings - Fork 1
/
pgflibraryoffsetpath.code.tex
1252 lines (1164 loc) · 52.3 KB
/
pgflibraryoffsetpath.code.tex
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% pgflibraryoffsetpath.code.tex
%% Copyright 2023 Jonathan Schulz
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
% http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008-05-04 or later.
%
% This work has the LPPL maintenance status 'maintained'.
%
% The Current Maintainer of this work is Jonathan Schulz.
%
% This work consists of the files pgflibrarybezieroffset.code.tex,
% tikzlibrarynfold.code.tex, tikz-nfold-doc.tex, and tikz-nfold-doc.pdf.
\usepgflibrary{bezieroffset}
%
% General idea
% ============
%
% In order to offset a path, we must first analyse and slightly modify it. For example,
% the start and end points of segments need to be relocated slightly in order to make room
% for the joins, and most curves must be subdivided for the offsetting algorithm. To avoid
% redundant computations in n-fold paths, we pre-compute as much data as possible and store
% all this information in a "parsed path" macro. The structure of the parsed path resembles
% the structure of a pgf softpath, but it consists of different tokens.
%
% Parsing is a two-step process: We iterate over the path in the first step and separate
% it into connected sub-paths. This is necessary for closed paths, because we need to know
% the location of the \pgfpathclose-segment in order to correctly offset the first segment
% of this connected sub-path.
%
% Once we have parsed a connected sub-path and stored all the information necessary, the
% second step, called _processing_, begins. This involves relocating start and end points
% to make room for joins, storing all the information for a given join, subdividing curves,
% and handling a number of edge cases.
%
% The resulting parsed and processed path (shortened to "parsed path") consists of tokens
% like \pgf@nfold@token@lineto. Once a few macros are defined that control by how much
% the curve should be offset, the parsed path can be invoked directly.
%
%
% Intercepting join settings
% --------------------------
%
% The current settings of the line joins are not stored in any TeX registers; instead, direct system
% calls are made to apply the settings. Therefore, we need to modify the pgf macros in order
% to cache the current settings.
%
% The initial value is miter limit=10 according to the pdf standard (it is not set by pgf)
\gdef\pgf@nfold@cached@miterlimit{10}
\let\pgf@nfold@old@miterlimit\pgfsetmiterlimit
\def\pgfsetmiterlimit#1{%
\pgf@nfold@old@miterlimit{#1}%
\edef\pgf@nfold@cached@miterlimit{#1}%
}
% default line join is "miter"
\global\let\pgf@cached@linejoin=m% % b = bevel, m=miter, r=round
\let\pgf@nfold@old@setbeveljoin\pgfsetbeveljoin
\let\pgf@nfold@old@setmiterjoin\pgfsetmiterjoin
\let\pgf@nfold@old@setroundjoin\pgfsetroundjoin
\def\pgfsetbeveljoin{%
\pgf@nfold@old@setbeveljoin%
% do NOT change this globally! Needs to be changed back at the end of groups for scoping reasons
\let\pgf@cached@linejoin=b%
}
\def\pgfsetmiterjoin{%
\pgf@nfold@old@setmiterjoin%
\let\pgf@cached@linejoin=m%
}
\def\pgfsetroundjoin{%
\pgf@nfold@old@setroundjoin%
\let\pgf@cached@linejoin=r%
}
%
% Joining offset lines
% --------------------
%
% One of the more difficult aspects is joining the segments of an offset path. Without this step,
% the path would be interrupted or self-intersect whenever there is a non-zero angle between
% two segments. This code reproduces the existing line joins "bevel", "miter" and "round".
%
\def\pgf@nfold@miterjoin{
% The tip of the miter join is computed starting from the original (unshifted) centre of the join;
% we then move orthorgonal to the average of the old and new angle
\pgfpathlineto{
\pgfpointadd{\pgf@nfold@join@centre}{%
\pgfpointpolar{\pgf@nfold@firstang+.5*\pgf@nfold@deltaphi+90}% do not change
{\pgf@nfold@shiftamount/cos(.5*\pgf@nfold@deltaphi)}%
}%
}%
}
\def\pgf@nfold@beveljoin{
% The bevel join for one component line consists of three parts:
% 1) an extension of the ingoing line,
% 2) a middle line, angled at the average of the incoming and outgoing line,
% 3) an extension of the outgoing line.
% Different components of the ingoing and outgoing lines have a constant distance from each other.
% For a good-looking output, the mittle parts of the component lines thus should also have a constant distance,
% which is a non-trivial condition. To generate such an output the outer lines get a bevel-like join
% and the inner lines get a miter-like join; the threshold depends on deltaphi.
% We first compute by how much the outermost line must be continued from the beginning of the join.
% The protrusion amount must be lowered by a little bit for a rather complicated reason: The offset would be
% dead on if the outermost offset line were centered on the _edge_ of the wide line, but we want to draw
% the outside line _fully inside_ the wide line. The factor of tan(deltaphi/4) can be derived, but is not obvious.
\pgfmathsetlengthmacro{\bevelouterprotrusion}%
{\pgf@nfold@shortenstartjoin pt - .5*\pgflinewidth*abs(tan(.25*\pgf@nfold@deltaphi))}
% The following applies to middle lines only: We compute by how much they need to be shortened so the distance
% between the lines in the join is correct.
\pgfmathsetmacro{\bevelshorten}{2*\insidepercentage*abs(tan(.25*\pgf@nfold@deltaphi))}
% This threshold decides if the inside line has a bevel or a miter join
\pgfmathparse{\bevelshorten < abs(sin(.5*\pgf@nfold@deltaphi))}
\ifnum\pgfmathresult=1\relax
\pgfmathsetlengthmacro{\bevelextension}{\bevelouterprotrusion-\bevelshorten*\pgf@nfold@hwidth}
\pgfpointadd{\pgf@nfold@join@start}{\pgfpointpolar{\pgf@nfold@firstang}{\bevelextension}}
\pgfpathlineto{}
\pgfpointadd{\pgf@nfold@join@end}{\pgfpointpolar{\pgf@nfold@secondang}{-\bevelextension}}
\pgfpathlineto{}
\else
\pgf@nfold@miterjoin
\fi
}
\def\pgf@nfold@roundjoin{
% The outer half of the lines get arcs, the others get miters
\ifdim\insidepercentage pt<.5pt\relax
\pgfpointadd{\pgf@nfold@join@start}{\pgfpointpolar{\pgf@nfold@firstang}{\pgf@nfold@shortenstartjoin}}
\pgfpathlineto{}
% TODO needs unit tests for all cases left, right, across the 360 gap etc.
\pgfmathsetmacro\pgf@tmp@firstang{\pgf@nfold@firstang+90*\turnindicator}
\pgfpatharc%
{\pgf@tmp@firstang}%
{\pgf@tmp@firstang+\pgf@nfold@deltaphi}%
{abs(\pgf@nfold@shift@fraction)*\pgf@nfold@hwidth}%
\else
\pgf@nfold@miterjoin
\fi
}
% The handler that will be added to the parsed path
% #1: The centre of the join
% #2: The end point of the first segment
% #3: The starting point of the second segment
% #4: The angle of the first segment
% #5: The angle of the second segment
% #6: deltaphi in the range [-180, 180]
% #7: the distance between #1 and #2
% #8: the join + finish macro
\def\pgf@nfold@token@join#1#2#3#4#5#6#7#8{%
\def\pgf@nfold@join@centre{#1}%
\def\pgf@nfold@join@prevend{#2}%
\def\pgf@nfold@join@nextstart{#3}%
\def\pgf@nfold@firstang{#4}%
\def\pgf@nfold@secondang{#5}%
% Offset the start and end of this join
\pgfextract@process\pgf@nfold@join@start{%
\pgfpointadd{#2}{\pgfpointpolar{\pgf@nfold@firstang+90}{\pgf@nfold@shiftamount}}}%
\pgfextract@process\pgf@nfold@join@end{%
\pgfpointadd{#3}{\pgfpointpolar{\pgf@nfold@secondang+90}{\pgf@nfold@shiftamount}}}%
\pgf@process{\pgfpointdiff{\pgf@nfold@join@start}{\pgf@nfold@join@end}}
% Check if the start of this segment is very close to the end of the previous segment.
% In that case we don't need a join at all
\pgfpointtaxicabnorm\pgfutil@tempdima
\ifdim\pgfutil@tempdima>0.1pt\relax
\def\pgf@nfold@deltaphi{#6}%
\def\pgf@nfold@shortenstartjoin{#7}%
% First step: Check if left or right turn (-1=left, 1=right)
\ifdim\pgf@nfold@deltaphi pt<0pt
\def\turnindicator{1}
\else
\def\turnindicator{-1}
\fi
% \insidepercentage: between 0.0 and 1.0;
% 0=no distance to cover in the join, 1=maximum distance to cover
\pgfutil@tempdima=\pgf@nfold@shift@fraction pt\relax
\pgfutil@tempdima=\turnindicator\pgfutil@tempdima
\advance\pgfutil@tempdima by-1pt\relax
\pgfutil@tempdima=-.5\pgfutil@tempdima
% \insidepercentage = .5 * (1 - \turnindicator*\pgf@nfold@shift@fraction)
\edef\insidepercentage{\pgf@sys@tonumber\pgfutil@tempdima}%
#8%
\fi
}
%
% Join handler
%
\def\pgf@nfold@process@join{%
% \pgf@xa := abs(deltaphi@start)
\pgf@xa=\pgf@nfold@deltaphi@start pt\relax
\ifdim\pgf@xa<0pt\relax
\pgf@xa=-\pgf@xa
\fi
% Skip the entire join if abs(deltaphi) is too small
\ifdim\pgf@xa>1pt\relax
\edef\pgf@nfold@jointype{\pgf@cached@linejoin}
\if\pgf@cached@linejoin m% \ifx is not needed because both are only one character
% miter join
% First we implement the miter limit: If the angle is too sharp, the miter join is replaced
% by a bevel join. This is controlled e.g. by /tikz/miter limit=..., initially 10.
\pgf@xa=.5\pgf@xa
\pgfmathcos@{\pgf@sys@tonumber\pgf@xa}
\pgf@xa=\pgfmathresult pt\relax
\pgf@xa=\pgf@nfold@cached@miterlimit\pgf@xa
% Switch to bevel if miterlimit*cos(.5*abs(deltaphi)) <= 1
\ifdim\pgf@xa>1pt\relax
\def\pgf@nfold@tmp@joinmacro{\pgf@nfold@miterjoin}
\else
\def\pgf@nfold@tmp@joinmacro{\pgf@nfold@beveljoin}
\fi
\else
\if b\pgf@cached@linejoin\relax
\def\pgf@nfold@tmp@joinmacro{\pgf@nfold@beveljoin}
\else
\def\pgf@nfold@tmp@joinmacro{\pgf@nfold@roundjoin}
\fi
\fi
% The last parameter is a macro to be called when this segment of the join is non-trivial.
% It consists of
% - the macro to actually draw the join,
% - either finish@normal or finish@edgecase.
\edef\pgf@nfold@macrotoadd{%
\noexpand\pgf@nfold@token@join{\pgf@nfold@cur@first}{\pgf@nfold@prev@segment@end}
{\pgf@nfold@cur@movedfirst}{\pgf@nfold@prev@angle@ii}{\pgf@nfold@cur@angle@i}%
{\pgf@nfold@deltaphi@start}{\pgf@nfold@shortenstartjoin}{%
\expandafter\noexpand\pgf@nfold@tmp@joinmacro%
\expandafter\noexpand\ifpgf@nfold@closejoinsedgecase%
\pgf@nfold@token@finish@edgecase{\pgf@nfold@cur@movedlast}%
\else%
\pgf@nfold@token@finish@normal%
\fi%
}%
}%
\pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
\fi% end if abs(deltaphi) > 1
}
% All non-trivial joins connect to the end of the join, which is the starting point of the next segment.
% Note that this entire macro is skipped by if the start and end of the join coincide, so we never create a zero length segment here.
%
% There is one edge case here: If two subsequent joins are so close that \pgf@nfold@cur@movedlast
% and \pgf@nfold@cur@movedfirst exchange places *and* we are on the outside of the first join
% (implying that we are on the inside of the second join), the first join must not connect all the way to
% to \pgf@nfold@cur@movedfirst, because that would overshoot the second join. Instead we connect to
% \pgf@nfold@cur@movedlast (which is *closer* to the first join than \pgf@nfold@cur@movedfirst in this edge case).
% To accomodate for the vertical offset we connect to \pgf@nfold@join@end which is the vertical
% offset of \pgf@nfold@cur@movedlast by definition.
\def\pgf@nfold@token@finish@normal{%
\pgfpathlineto{\pgf@nfold@join@end}%
}
% parameter #1: @movedlast of the next segment
\def\pgf@nfold@token@finish@edgecase#1{%
\pgf@nfold@join@end%
\ifdim\insidepercentage pt<.5pt\relax%
\pgf@process{\pgfpointadd{#1}{\pgfpointpolar{\pgf@nfold@secondang+90}{\pgf@nfold@shiftamount}}}
\fi%
\pgfpathlineto{}%
}
%
% Main rendering pipeline
% -----------------------
%
% This stores whether the current segment should begin with a moveto to its offset
\newif\ifpgf@nfold@continuesegment
% This stores whether we are in some edge case of very close joins, see below for details
\newif\ifpgf@nfold@closejoinsedgecase
% This stores whether we are in an error case where we need to avoid dividing by zero
\newif\ifpgf@nfold@angletoosharp
% This stores whether we need the intersections library for an arrow tip but it is not loaded
\newif\ifpgf@nfold@intersectionsnotloaded
% This stores whether the currently processed connected sub-path is the final one on this path
\newif\ifpgf@nfold@lastconnsubpath
\def\pgf@nfold@process@segment{%
\if\pgf@nfold@cur@visible0
% first, last and moveto are invisible
\if\pgf@nfold@cur@type m
% We don't need to do anything for a moveto; if a visible segment follows, it will move to
% its starting location by itself. However, we might need to draw the arrow tip extension
% at the start (if present).
% Draw a tip extension only if all of the following conditions are met:
% 1. There is an Implies tip at the start of this path
% 2. We are currently on the last connected sub-path of this path
% 3. The next segment is visible
% 4. This moveto is the first segment on this path
\if\pgf@nfold@start@arrowcode1
\ifpgf@nfold@lastconnsubpath
\if\pgf@nfold@prev@type f
\if\pgf@nfold@next@visible1
\edef\pgf@nfold@macrotoadd{%
\noexpand\pgf@nfold@extendtotip{s}{\pgf@nfold@cur@last}{\pgf@nfold@next@angle@i}
}%
\pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
% hack: We make the next segment believe that this segment was a lineto
% so the path does not get interrupted
\let\pgf@nfold@cur@type l
\let\pgf@nfold@cur@visible1
\let\pgf@nfold@cur@angle@ii\pgf@nfold@next@angle@i
\let\pgf@nfold@cur@tang@ii\pgf@nfold@next@tang@i
\def\pgf@nfold@deltaphi@end{0}
\pgf@nfold@angletoosharpfalse
\fi
\fi%
\fi
\fi
\fi%
\else%
\let\pgf@nfold@cur@movedfirst\pgf@nfold@cur@first
\let\pgf@nfold@cur@movedlast\pgf@nfold@cur@last
%%% Step 1: Make room for joins if necessary
% In order to make room for the join, it may be necessary to shorten the current segment
% at the start and/or the end. In here we store by how much the segment needs to be shortened.
\def\pgf@nfold@shortenstartjoin{0}
\def\pgf@nfold@shortenendjoin{0}
\pgf@nfold@closejoinsedgecasefalse
% Step 1.1: Make room for the join at the start if needed
\if\pgf@nfold@prev@visible0
\pgf@nfold@continuesegmentfalse
\else
\pgf@nfold@continuesegmenttrue
% deltaphi@start can be recycled from deltaphi@end;
% \ifpgf@nfold@angletoosharp is also still set
\let\pgf@nfold@deltaphi@start\pgf@nfold@deltaphi@end
% set \pgf@xb := abs(deltaphi@start)
\pgf@xb=\pgf@nfold@deltaphi@start pt\relax
\ifdim\pgf@xb<0pt\relax
\pgf@xb=-\pgf@xb
\fi
\ifpgf@nfold@angletoosharp\else
\ifdim\pgf@xb>0.5pt\relax
% make room for the start join if the angle is nonzero;
% shortenstartjoin := hwidth*tan(.5*abs(deltaphi@start))
\pgf@yb=.5\pgf@xb
\pgfmathtan@{\pgf@sys@tonumber\pgf@yb}
\pgf@yb=\pgf@nfold@hwidth\relax
\pgf@yb=\pgfmathresult\pgf@yb
\edef\pgf@nfold@shortenstartjoin{\pgf@sys@tonumber\pgf@yb}
\pgfextract@process\pgf@nfold@cur@movedfirst{%
\pgfpointadd{\pgf@nfold@cur@first}%
{\pgfqpointscale{\pgf@nfold@shortenstartjoin}{\pgf@nfold@cur@tang@i}}}%
% If the current segment is a curve, we need to relocate @supporta point as well,
% as otherwise the @first point could overtake it
\if\pgf@nfold@cur@type c
\pgf@process{\pgfpointdiff{\pgf@nfold@cur@first}{\pgf@nfold@cur@supporta}}
\pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}
\ifdim\pgfmathresult pt>0.1pt\relax
% regular curves (supporta != first):
% Increase dist(first, supporta) to sqrt(a^2 + b^2) where a=dist(first, supporta) and b=shortenstart. This way, the order of first and supporta is guaranteed to be preserved, and the change to supporta is as small as reasonably possible.
\pgfmathveclen@{\pgfmathresult}{\pgf@sys@tonumber\pgf@yb}
\pgfextract@process\pgf@nfold@cur@supporta{\pgfpointadd%
{\pgf@nfold@cur@first}%
{\pgfqpointscale{\pgfmathresult}{\pgf@nfold@cur@tang@i}}}%
\else
% special treatment for singular curves (supporta = first) to avoid rounding error glitches.
% In this special case, a slight corner at the end of the join is unavoidable unless we
% also relocate @supportb, which may have unintended side effects
\let\pgf@nfold@cur@supporta\pgf@nfold@cur@movedfirst
\fi
\fi
\fi
\fi
\fi
% Step 1.2: Make room for the join at the end if needed
\if\pgf@nfold@next@visible1
% Compute the angle difference at the end (between -180 and +180 degrees)
% using \pgfmathsubtract@ is more readable and no less efficient than computing this manually
\pgfmathsubtract@{\pgf@nfold@next@angle@i}{\pgf@nfold@cur@angle@ii}
\pgf@nfold@clampangle
\edef\pgf@nfold@deltaphi@end{\pgfmathresult}
\pgf@xb=\pgf@nfold@deltaphi@end pt\relax
\ifdim\pgf@xb<0pt\relax
\pgf@xb=-\pgf@xb
\fi
\ifdim\pgf@xb>178pt\relax
\pgfutil@packagewarning{tikz-nfold}{Angle too sharp, expect visual errors}
\pgf@nfold@angletoosharptrue
\else
\pgf@nfold@angletoosharpfalse
\ifdim\pgf@xb>0.5pt\relax
% make room for the start join if the angle is nonzero
% shortenendjoin := hwidth*tan(.5*abs(deltaphi@end))
\pgf@yb=.5\pgf@xb
\pgfmathtan@{\pgf@sys@tonumber\pgf@yb}
\pgf@yb=\pgf@nfold@hwidth\relax
\pgf@yb=\pgfmathresult\pgf@yb
\edef\pgf@nfold@shortenendjoin{\pgf@sys@tonumber\pgf@yb}
\pgf@yb=-\pgf@yb
\pgfextract@process\pgf@nfold@cur@movedlast{%
% the use of the minus sign is fine here because \pgf@nfold@shortenendjoin >= 0.0
\pgfpointadd{\pgf@nfold@cur@last}%
{\pgfqpointscale{-\pgf@nfold@shortenendjoin}{\pgf@nfold@cur@tang@ii}}}%
\pgfextract@process\pgf@nfold@cur@movedlast{%
\pgfpointadd{\pgf@nfold@cur@last}%
{\pgfqpointpolar{\pgf@nfold@cur@angle@ii}{\pgf@yb}}}%
\if\pgf@nfold@cur@type c
% Same procedure as above: relocate supportb if we have a curve
\pgf@process{\pgfpointdiff{\pgf@nfold@cur@supportb}{\pgf@nfold@cur@last}}
\pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}
\ifdim\pgfmathresult pt>0.1pt\relax
\pgfmathveclen@{\pgfmathresult}{\pgf@sys@tonumber\pgf@yb}
\pgfextract@process\pgf@nfold@cur@supportb{\pgfpointadd%
{\pgf@nfold@cur@last}%
% can use qpointscale and a minus because \pgfmathresult is guaranteed to be positive
{\pgfqpointscale{-\pgfmathresult}{\pgf@nfold@cur@tang@ii}}}%
\else
\let\pgf@nfold@cur@supportb\pgf@nfold@cur@movedlast
\fi
\fi
\fi
\fi
% Step 1.3: Detect an edge case
% This edge case appears whenever the current segment is such a short line that we would
% have to reduce its length to less than zero to make room for the joins. In such cases,
% the line is not drawn at all, and slight modifications must be made to the joins to ensure
% a correct output (i.e. one join is immediately followed by the next without a segment in between).
%
% This edge case can appear for curves as well, but they are much harder to deal with.
\if\pgf@nfold@cur@type l
% No need to check for \pgf@nfold@inputsegmentclosepath as it should not be followed by any further segments
% Now: Check if the total amount of shortening is larger than the length of the segment
\pgf@process{\pgfpointdiff{\pgf@nfold@cur@first}{\pgf@nfold@cur@last}}
\pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}
\pgf@xa=\pgf@nfold@shortenstartjoin pt\relax
\advance\pgf@xa by\pgf@nfold@shortenendjoin pt\relax
\ifdim\pgf@xa>\pgfmathresult pt\relax
\pgf@nfold@closejoinsedgecasetrue
\fi
\fi
\fi% end if next segment visible
%
% Step 2.1: Draw the join at the start if applicable
%
\if\pgf@nfold@prev@visible0%
\ifpgf@nfold@closejoinsedgecase
% If the previous segment is a moveto and the current segment is a "close joins" edge case,
% nothing needs to be drawn here (the relevant draw call will be made at the join of the subsequent
% segment). We must therefore make sure that we move to the correct end point of this segment.
% Counterintuitively, this is given by the offset of \pgf@nfold@cur@movedFIRST since the start and end
% are reversed in the edge case.
\edef\pgf@nfold@macrotoadd{%
\noexpand\pgf@nfold@token@edgecase@movetostart{\pgf@nfold@cur@movedfirst}{\pgf@nfold@cur@angle@i}%
}%
\pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
\fi
\else
% If we draw the join when the start angle is close to 180 degrees, we get a division by zero
\ifpgf@nfold@angletoosharp\else
\if\pgf@nfold@cur@type z\else
\pgf@nfold@process@join
\fi
\fi
\fi
% Step 2.2: Store information that may be needed later
% Step 2.2.1: Store where the current (non-offset) end point was relocated
% in order to make space for the end join. This may be used if the next
% segment begins with a join
\let\pgf@nfold@prev@segment@end\pgf@nfold@cur@movedlast
% Step 2.2.2: Store the tangent of the first proper segment of closed path.
% This is needed in case the \pgfpathclose-segment has length zero,
% since in that case its tangent cannot be computed.
\if\pgf@nfold@cur@type i
\let\pgf@nfold@connsubpath@tang@i\pgf@nfold@next@tang@i%
\let\pgf@nfold@connsubpath@angle@i\pgf@nfold@next@angle@i%
\fi
%
% Step 3: Draw the new segment.
%
% The value of \ifpgf@nfold@continuesegment decides whether we start with a moveto.
\if\pgf@nfold@cur@type l
% In the edge case, one join is followed immediately by the next. The line segment
% thus has a negative length and will be skipped.
\ifpgf@nfold@closejoinsedgecase\else
\edef\pgf@nfold@macrotoadd{%
\expandafter\noexpand\ifpgf@nfold@continuesegment%
\pgf@nfold@token@lineto@continue%
\else%
\pgf@nfold@token@lineto%
\fi{\pgf@nfold@cur@movedfirst}{\pgf@nfold@cur@movedlast}{\pgf@nfold@cur@tang@i}%
}%
\pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
\fi
\fi
\if\pgf@nfold@cur@type o
\def\pgf@nfold@macrotoadd{\pgf@nfold@token@closepath}%
\pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
\fi
\if\pgf@nfold@cur@type z
% zero and non-zero closepath's differ in the join handling, but are identical in rendering
\def\pgf@nfold@macrotoadd{\pgf@nfold@token@closepath}%
\pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
\fi
\if\pgf@nfold@cur@type c
\ifpgf@nfold@continuesegment
\pgf@subdividecurve{\pgf@nfold@cur@movedfirst}{\pgf@nfold@cur@supporta}{\pgf@nfold@cur@supportb}{\pgf@nfold@cur@movedlast}{\pgf@offset@max@recursion}{0}{\pgf@nfold@addcurvesegment@callback@continue}
\else
\pgf@subdividecurve{\pgf@nfold@cur@movedfirst}{\pgf@nfold@cur@supporta}{\pgf@nfold@cur@supportb}{\pgf@nfold@cur@movedlast}{\pgf@offset@max@recursion}{0}{\pgf@nfold@addcurvesegment@callback}%
\fi
\fi
\if\pgf@nfold@cur@type i
\edef\pgf@nfold@macrotoadd{%
\noexpand\pgf@nfold@token@invisibleline{\pgf@nfold@cur@movedfirst}{\pgf@nfold@cur@movedlast}{\pgf@nfold@cur@tang@i}%
}%
\pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
\fi
% Step 4: Extend into the arrow tip at the end (if present)
\if\pgf@nfold@next@type t%
\ifnum\pgf@nfold@end@arrowcode=1
\edef\pgf@nfold@macrotoadd{%
\noexpand\pgf@nfold@extendtotip{e}{\pgf@nfold@cur@last}{\pgf@nfold@cur@angle@ii}}%
\pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
\fi%
\fi%
\fi% end if current visible
}
\def\pgf@nfold@addcurvesegment@callback#1#2#3#4#5{%
\if#50%
\edef\pgf@nfold@macrotoadd{%
% The subdivision algorithm has already computed the tangents
\noexpand\pgf@nfold@token@curveto{#1}{#2}{#3}{#4}{\pgf@tmp@tang@i}{\pgf@tmp@tang@ii}%
}%
\pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
\else%
\pgf@nfold@addcurvesegment@callback@continue{#1}{#2}{#3}{#4}{#5}%
\fi%
}
\def\pgf@nfold@addcurvesegment@callback@continue#1#2#3#4#5{%
% The subdivision algorithm has already computed the tangents
\edef\pgf@nfold@macrotoadd{%
\noexpand\pgf@nfold@token@curveto@continue{#1}{#2}{#3}{#4}{\pgf@tmp@tang@i}{\pgf@tmp@tang@ii}%
}%
\pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
}
% Rendering arrow tips
% --------------------
% Precomputed intersections
%
% For arrows of order n > 2 with an Implies tip, the constituent parts of the n-fold arrow
% end somewhere in the middle of the tip. The exact end point must be computed using
% the intersections library. To speed up compilation times, the intersection points are precomputed
% up to n = 5. If your document contains arrows of order 6 or larger, consider adding those
% as well; the values are output in the log file.
\expandafter\def\csname pgf@nfold@intersec@cache@2@3\endcsname{\pgfqpoint{2pt}{0pt}}
\expandafter\def\csname pgf@nfold@intersec@cache@2@4\endcsname{\pgfqpoint{0.94063pt}{-0.33333pt}}
\expandafter\def\csname pgf@nfold@intersec@cache@3@4\endcsname{\pgfqpoint{0.94063pt}{0.33333pt}}
\expandafter\def\csname pgf@nfold@intersec@cache@2@5\endcsname{\pgfqpoint{0.64167pt}{-0.5pt}}
\expandafter\def\csname pgf@nfold@intersec@cache@3@5\endcsname{\pgfqpoint{2pt}{0pt}}
\expandafter\def\csname pgf@nfold@intersec@cache@4@5\endcsname{\pgfqpoint{0.64167pt}{0.5pt}}
% intersections are precomputed up to this order
\def\pgf@nfold@intersec@numcached{5}
\newif\ifpgf@nfold@ontheedge
% This macro extends the arrow body to the tips
% #1: s=start, e=end
% #2: start/end point of the path
% #3: angle
\def\pgf@nfold@extendtotip#1#2#3{
\ifpgf@nfold@intersectionsnotloaded
\pgfutil@packageerror{tikz-nfold}{%
If `nfold' is larger than \pgf@nfold@intersec@numcached\space and you use
an `Implies' arrow tip you need to say \string\usetikzlibrary{intersections}}{}
\else
\pgf@nfold@ontheedgetrue
% Do not extend the arrow for index=1 and index=order, it already ends in the right place
\ifnum\pgf@nfold@index>1\relax\ifnum\pgf@nfold@index<\pgf@nfold@order\relax%
\pgf@nfold@ontheedgefalse
\fi\fi
\ifpgf@nfold@ontheedge%
% the extension at the start needs a moveto to the correct starting point
\if#1s
\pgfpathmoveto{\pgfpointadd{#2}{\pgfpointpolar{#3+90}{\pgf@nfold@shiftamount}}}%
\fi
\else
% Step 1: Find the intersection of the arrow's path with the head
\ifcsname pgf@nfold@intersec@cache@\the\pgf@nfold@index @\the\pgf@nfold@order\endcsname
\pgfextract@process\pgf@nfold@arrowintersect
{\csname pgf@nfold@intersec@cache@\the\pgf@nfold@index @\the\pgf@nfold@order\endcsname}%
\else
% the intersection has not been precomputed, thus compute on the fly here
\pgfintersectionofpaths{
% specify the tip
\pgfpathmoveto{\pgfqpoint{-1.4pt}{2.65pt}}
\pgfpathcurveto{\pgfqpoint{-0.75pt}{1.25pt}}{\pgfqpoint{1pt}{0.05pt}}{\pgfqpoint{2pt}{0pt}}
\pgfpathcurveto{\pgfqpoint{1pt}{-0.05pt}}{\pgfqpoint{-0.75pt}{-1.25pt}}{\pgfqpoint{-1.4pt}{-2.65pt}}
}{
% extend the body to intersect the tip
\pgfpathmoveto{\pgfqpoint{-3pt}{\pgf@nfold@shift@fraction pt}}
\pgfpathlineto{\pgfqpoint{3pt}{\pgf@nfold@shift@fraction pt}}
}
\ifnum\pgfintersectionsolutions>0
\pgfextract@process\pgf@nfold@arrowintersect{\pgfpointintersectionsolution{1}}%
\immediate\write17{tikz-nfold: computed intersection cache@\the\pgf@nfold@index @\the\pgf@nfold@order: \string\pgfqpoint{\the\pgf@x}{\the\pgf@y}^^J}
% add the new intersection to the cache
\expandafter\xdef\csname pgf@nfold@intersec@cache@\the\pgf@nfold@index @\the\pgf@nfold@order\endcsname{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}
\else
% this is a failsafe and should never be reached
\pgfutil@packagewarning{tikz-nfold}{did not find intersection}
\pgfextract@process\pgf@nfold@arrowintersect{\pgfqpoint{0pt}{\pgf@nfold@shift@fraction pt}}%
\fi
\fi% if precomputed
% Step 2: Extend the arrow body to the intersection point.
% If the tip is at the beginning of the path, we have to move to the intersection
% and then draw a line to the "regular" starting point. The subsequent segment then
% should omit its moveto.
% If the tip is at the end, we are already in the right position and only need to extend
% the current path to the intersection point.
\begingroup
\pgftransformreset
\pgftransformshift{#2}
\pgftransformrotate{#3}
\if#1s
\pgftransformxscale{-1}
\fi
% we don't want to undo the shift by .42\pgflinewidth after the scaling
\pgfutil@tempdima=\pgf@nfold@hwidth
\pgfutil@tempdima=\pgf@nfold@shift@fraction\pgfutil@tempdima
\pgfextract@process\pgf@nfold@startofextension
{\pgfpointtransformed{\pgfqpoint{0pt}{\pgfutil@tempdima}}}
% 0.5 - 0.06 = 0.42
\pgftransformshift{\pgfqpoint{.42\pgflinewidth}{0pt}}
\pgftransformscale{\pgf@nfold@hwidth}
\pgfextract@process\pgf@nfold@arrowintersect{\pgfpointtransformed{\pgf@nfold@arrowintersect}}
\global\let\pgf@nfold@startofextension\pgf@nfold@startofextension
\global\let\pgf@nfold@arrowintersect\pgf@nfold@arrowintersect
\endgroup
\if#1s
\pgfpathmoveto{\pgf@nfold@arrowintersect}
% This is precisely the start of the body, shifted vertically
\pgfpathlineto{\pgf@nfold@startofextension}
\else\if#1e
\pgfpathlineto{\pgf@nfold@arrowintersect}
\else
\pgfutil@packageerror{tikz-nfold}{Invalid argument to \string\pgf@nfold@extendtotip: \meaning#1}{}
\fi\fi
\pgftransformreset
\fi% if 1 < i < nArrows
\fi% if intersections is needed and not loaded
}
% Parsing the arrow tips
% ----------------------
%
% We need to detect if the user has set Implies[] arrows at the start and/or end tip.
% To do so, we parse \pgf@start@tip@sequence. If the user specifies Implies[] manually,
% we find that
% pgf@start@tip@sequence=\pgf@arrow@handle{Implies}{...}
% However, in other cases (like tikz-cd) we may find
% \pgf@arrow@handle@shorthand@empty {\csname pgf@ar@means@tikzcd implies cap\endcsname }
% In such cases we must expand the first parameter once and then match as above.
% Set global defaults
\def\pgf@nfold@start@arrowcode{0}
\def\pgf@nfold@end@arrowcode{0}
\def\pgf@nfold@parsearrows{
\ifpgfutil@tempswa% this is set in \pgfusepath and stores whether we draw arrow tips at all
\expandafter\pgf@nfold@parsearrowmacro\pgf@start@tip@sequence\relax
\let\pgf@nfold@start@arrowcode\pgf@nfold@detectedarrow
\expandafter\pgf@nfold@parsearrowmacro\pgf@end@tip@sequence\relax
\let\pgf@nfold@end@arrowcode\pgf@nfold@detectedarrow
\else
\def\pgf@nfold@start@arrowcode{0}
\def\pgf@nfold@end@arrowcode{0}
\fi
}
\def\pgf@nfold@parsearrowmacro#1{%
\def\pgf@nfold@detectedarrow{0}
\ifx#1\relax
\let\pgf@next\relax
\else
\ifx#1\pgf@arrow@handle
% found \pgf@arrow@handle{...}, now parse the first parameter
\let\pgf@next\pgf@nfold@parse@arrow@handle
\else
\ifx#1\pgf@arrow@handle@shorthand@empty
\let\pgf@next\pgf@nfold@parse@shorthandempty
\else
% found nothing
\let\pgf@next\pgfutil@gobble@until@relax
\fi
\fi
\fi
\pgf@next
}
\def\pgf@nfold@param@Implies{Implies}
\def\pgf@nfold@parse@arrow@handle#1{%
\def\pgf@tmp{#1}
\ifx\pgf@tmp\pgf@nfold@param@Implies
\def\pgf@nfold@detectedarrow{1}
\fi
\pgfutil@gobble@until@relax
}
\def\pgf@nfold@parse@shorthandempty#1{
% Expand #1 once (\pgf@arrow@handle@shorthand@empty is just an identity operator)
\expandafter\def\expandafter\pgf@tmp\expandafter{#1}
\expandafter\pgf@nfold@parsearrowmacro\pgf@tmp\relax
% still need to gobble the rest of the orginal arrow definition
\pgfutil@gobble@until@relax
}
%
% Hooking into pgf's rendering pipeline
% -------------------------------------
%
% The new code has to be injected into \pgfusepath (pgfcorepathusage.code.tex). For rendering the new paths,
% \pgf@stroke@inner@line is a natural choice as this is where /tikz/double is rendered. However, we also
% need to disable rendering the ordinary path, which is not as easy. In the future I will make a pull request
% to TikZ to simplify such injections.
%
% The call to draw the path comes right before \pgf@stroke@inner@line. The macro before \pgf@stroke@inner@line
% is either \pgf@path@check@proper or \pgf@prepare@start@of@path (depending on the result of the proper check).
% We therefore must inject code into both of them to see if nfold is enabled. If it is, we call the old macro,
% cache and delete the current softpath (so the call to \pgfsyssoftpath@invokecurrentpath has no effect), then we
% restore and offset the cached softpath in \pgf@stroke@inner@line.
%
% The macros \pgf@path@check@proper and \pgf@prepare@start@of@path are also used in \pgf@up@draw@arrows@only,
% so we must make sure that the latter is unaffected by the modifications. Luckily, this turns out not to be
% a problem - the only macros that are called after the modified ones are \pgf@add@arrow@at@start and
% \pgf@add@arrow@at@end, which do not change their behaviour even if we modify the paths.
%
\newcount\pgf@nfold@order
\pgf@nfold@order=1
\def\pgf@nfold@preparenfoldpath{%
\ifnum\pgf@nfold@order>1\relax
\ifdim\pgfinnerlinewidth>0pt\relax
% Hack the rendering pipeline: There is a \pgfsyssoftpath@invokecurrentpath call following
% which we do not want if nfold is active. We therefore clear the current path here
% and then perform the nfold drawing in our modification of \pgf@stroke@inner@line
\pgfsyssoftpath@getcurrentpath\pgf@nfold@cachedpath%
\pgfsyssoftpath@setcurrentpath\pgfutil@empty%
\else
\pgfutil@packageerror{tikz-nfold}{Must set \string\pgfinnerlinewidth\space to use nfold, e.g. by setting /tikz/double distance}{}
\fi
\fi
}
\let\pgf@nfold@old@path@check@proper\pgf@path@check@proper
\def\pgf@path@check@proper{%
\pgf@nfold@old@path@check@proper%
\ifpgfutil@tempswa\else%
% if \pgfutil@tempswa is false, this is the last macro we can overwrite before the draw call.
% Otherwise, we inject into \pgf@prepare@start@of@path%
\pgf@nfold@preparenfoldpath%
\fi%
}
\let\pgf@nfold@old@prepare@start@of@path\pgf@prepare@start@of@path
\def\pgf@prepare@start@of@path{%
\pgf@nfold@old@prepare@start@of@path%
\pgf@nfold@preparenfoldpath%
}
\let\pgf@nfold@old@stroke@inner@line\pgf@stroke@inner@line
\def\pgf@stroke@inner@line{%
\ifnum\pgf@nfold@order>1\relax%
\pgf@nfold@render@cached@softpath%
\else%
% Old behaviour
\pgf@nfold@old@stroke@inner@line%
\fi%
}
%
% Parsing the soft path
% ---------------------
%
% Part of the code below is based on pgfmoduledecorations.code.tex (c) 2019 Mark Wibrow and Till Tantau.
% Quite similar to decorations we parse the current soft path and put it into a form that makes it easier
% to iterate over.
%
% The parsing macros below add \pgf@nfold@[email protected] to \pgf@cur@conn@subpath.
% Once a connected sub-path is parsed, calling \pgf@cur@conn@subpath triggers processing.
\def\pgf@nfold@parsemoveto#1#2{%
\ifx\pgf@cur@conn@subpath\pgfutil@empty%
% This case happens for the very first segment or for double movetos.
% We need special treatment here, as otherwise the arrow tip extension
% does not work correctly. The \pgf@nfold@processmoveto will be called
% in \pgf@nfold@process@conn@subpath.
\def\pgf@nfold@parser@last@moveto{{#1}{#2}}%
% Make sure \pgf@cur@conn@subpath is no longer empty so double movetos are not
% treated the same as single movetos. This is also relevant to arrow tip extensions
\def\pgf@cur@conn@subpath{\relax}%
\else%
\def\pgf@nfold@macrotoadd{\pgf@nfold@processmoveto{#1}{#2}}%
\pgf@nfold@addmacro\pgf@cur@conn@subpath%
\pgf@nfold@lastconnsubpathfalse%
% A moveto marks the beginning/end of one connected sub-path
\pgf@nfold@process@conn@subpath%
\let\pgf@nfold@last@closepath@from\pgfutil@empty%
\let\pgf@cur@conn@subpath\pgfutil@empty%
% set up "first" again which may be needed for the arrow tip extension
\pgf@nfold@parser@setupfirst%
\def\pgf@nfold@parser@last@moveto{{#1}{#2}}%
\fi%
\pgf@nfold@parsesoftpath@finish{#1}{#2}%
}
\def\pgf@nfold@parselineto#1#2{%
\def\pgf@nfold@macrotoadd{\pgf@nfold@processlineto{#1}{#2}}%
\pgf@nfold@addmacro\pgf@cur@conn@subpath%
\pgf@nfold@parsesoftpath@finish{#1}{#2}%
}
\def\pgf@nfold@parseclosepath#1#2{%
\let\pgf@nfold@last@closepath@from\pgf@nfold@parser@previous@pt%
\def\pgf@nfold@macrotoadd{\pgf@nfold@processclosepath{#1}{#2}}%
\pgf@nfold@addmacro\pgf@cur@conn@subpath%
\pgf@nfold@parsesoftpath@finish{#1}{#2}%
}
\def\pgf@nfold@parsecurveto#1#2{%
\edef\pgf@nfold@macrotoadd{\noexpand\pgf@nfold@processcurveto\pgf@nfold@parser@supporta\pgf@nfold@parser@supportb{#1}{#2}}%
\pgf@nfold@addmacro\pgf@cur@conn@subpath%
\pgf@nfold@parsesoftpath@finish{#1}{#2}%
}
% Mostly for the sake of completeness; using TikZ' "\path (0,0) rectangle (1,1);" does not call this code
\def\pgf@nfold@parserect#1#2#3#4{%
\pgf@nfold@parsemoveto{#1}{#2}%
\pgf@xc=#1\relax
\pgf@yc=#2\relax
\pgf@xd=#3\relax
\pgf@yd=#4\relax
\advance\pgf@yc\pgf@yd%
\edef\pgf@temp{{\the\pgf@xc}{\the\pgf@yc}}%
\expandafter\pgf@nfold@parselineto\pgf@temp%
\advance\pgf@xc\pgf@xd%
\edef\pgf@temp{{\the\pgf@xc}{\the\pgf@yc}}%
\expandafter\pgf@nfold@parselineto\pgf@temp%
\advance\pgf@yc-\pgf@yd%
\edef\pgf@temp{{\the\pgf@xc}{\the\pgf@yc}}%
\expandafter\pgf@nfold@parselineto\pgf@temp%
\advance\pgf@xc-\pgf@xd%
\edef\pgf@temp{{\the\pgf@xc}{\the\pgf@yc}}%
\expandafter\pgf@nfold@parseclosepath\pgf@temp%
\pgf@nfold@parsemoveto{#1}{#2}%
}
%
% In order to correctly implement \pgfpathclose we already need to know about
% the \pgfpathclose (and the penultimate point) at the beginning of this
% connected sub-path, as these data affect the first/last join.
% We therefore parse one connected sub-path of the softpath and store it in a modified
% form in \pgf@cur@conn@subpath. When reaching the end of the connected sub-path we commence
% the processing to turn \pgf@cur@conn@subpath into a parsed path. We do this for
% all connected sub-paths and concatenate all the parsed paths.
%
\def\pgf@nfold@parsesoftpath#1#2{%
\let\pgf@cur@conn@subpath\pgfutil@empty%
\let\pgf@all@parsed@subpaths\pgfutil@empty%
\edef\pgf@nfold@parser@last@moveto{{\the\pgf@path@lastx}{\the\pgf@path@lasty}}%
\let\pgf@nfold@last@closepath@from\pgfutil@empty%
\pgf@nfold@parser@setupfirst%
\expandafter\pgf@nfold@@parsesoftpath#1\pgf@stop{}{}%
\let#2\pgf@all@parsed@subpaths%
}%
\def\pgf@nfold@@parsesoftpath#1#2#3{%
\let\pgf@next\pgf@nfold@@parsesoftpath%
\ifx#1\pgf@stop%
\def\pgf@nfold@macrotoadd{\pgf@nfold@processlast}%
\pgf@nfold@addmacro\pgf@cur@conn@subpath%
\pgf@nfold@lastconnsubpathtrue%
\pgf@nfold@process@conn@subpath%
\let\pgf@next\relax%
\else%
\ifx#1\pgfsyssoftpath@movetotoken%
\pgf@nfold@parsemoveto{#2}{#3}%
\else%
\ifx#1\pgfsyssoftpath@linetotoken%
\pgf@nfold@parselineto{#2}{#3}%
\else%
\ifx#1\pgfsyssoftpath@curvetosupportatoken%
\def\pgf@nfold@parser@supporta{{#2}{#3}}%
\else%
\ifx#1\pgfsyssoftpath@curvetosupportbtoken%
\def\pgf@nfold@parser@supportb{{#2}{#3}}%
\else%
\ifx#1\pgfsyssoftpath@curvetotoken%
\pgf@nfold@parsecurveto{#2}{#3}%
\else%
\ifx#1\pgfsyssoftpath@closepathtoken%
\pgf@nfold@parseclosepath{#2}{#3}%
\else%
\ifx#1\pgfsyssoftpath@rectcornertoken%
\def\pgf@nfold@parser@rectcorner{{#2}{#3}}%
\else%
\ifx#1\pgfsyssoftpath@rectsizetoken%
\expandafter\pgf@nfold@parserect\pgf@nfold@parser@rectcorner{#2}{#3}%
\else%
\pgfutil@packageerror{tikz-nfold}{Unrecognised soft path token `#1'}{}%
\fi%
\fi%
\fi%
\fi%
\fi%
\fi%
\fi%
\fi%
\fi%
% We call \pgf@nfold@parsesoftpath@finish in all parsing macros instead of here because
% \pgf@nfold@parserect calls other parsing macros internally
\pgf@next%
}%
\def\pgf@nfold@parsesoftpath@finish#1#2{%
\def\pgf@nfold@parser@previous@pt{{#1}{#2}}%
% store the last two segments, as they might be needed for zero-length \pgfpathclose-segments
\let\pgf@nfold@sectolast@segment\pgf@nfold@last@segment%
\let\pgf@nfold@last@segment\pgf@nfold@macrotoadd%
}
\def\pgf@nfold@process@conn@subpath{%
\let\pgf@parsed@cur@conn@seg\pgfutil@empty%
\ifx\pgf@nfold@last@closepath@from\pgfutil@empty%
\expandafter\pgf@nfold@processmoveto\pgf@nfold@parser@last@moveto%
\else%
% This connected sub-path ends on a closepath. In order to get
% the join right, we prepend the current sub-path with an invisible line
% identical to the line of the \pgfpathclose.
\expandafter\pgf@nfold@processmoveto\pgf@nfold@last@closepath@from%
\expandafter\pgf@nfold@processinvisibleline\pgf@nfold@parser@last@moveto%
\fi%
\pgf@cur@conn@subpath%
\let\pgf@nfold@macrotoadd\pgf@parsed@cur@conn@seg%
\pgf@nfold@addmacro\pgf@all@parsed@subpaths%
}
%
% Values for \pgf@nfold@next@type:
% f=first, m=moveto, c=curveto, l=lineto, r=rect, o=closepath, z=zero length closepath, t=last, i=invisibleline
%
\def\pgf@nfold@processmoveto#1#2{%
\pgf@nfold@process@moveup%
\let\pgf@nfold@next@type=m%
\let\pgf@nfold@next@visible=0%
\edef\pgf@nfold@next@last{\pgf@x#1\pgf@y#2}%
\pgf@nfold@process@segment%
}%
% Common code for processlineto and processclosepath
\def\pgf@nfold@process@line@common#1#2#3{%
\pgf@nfold@process@moveup%
\pgfextract@process\pgf@tmp@tang@i{\pgfpointnormalised{}\global\let\pgf@nfold@tmp\pgf@tmp}
\let\pgf@nfold@next@type=#3%
\let\pgf@nfold@next@visible=1%
\let\pgf@nfold@next@tang@i\pgf@tmp@tang@i
\let\pgf@nfold@next@tang@ii\pgf@tmp@tang@i
\let\pgf@nfold@next@angle@i\pgf@nfold@tmp
\let\pgf@nfold@next@angle@ii\pgf@nfold@tmp
\let\pgf@nfold@next@first\pgf@nfold@cur@last
\def\pgf@nfold@next@last{\pgf@x#1\pgf@y#2}%
\pgf@nfold@process@segment%
}
\def\pgf@nfold@processlineto#1#2{%
% the current end is still stored in next@last because process@moveup has not been called yet
\pgf@process{\pgfpointdiff{\pgf@nfold@next@last}{\pgf@x#1\pgf@y#2}}%
\pgfpointtaxicabnorm\pgf@xa%
% remove degenerate line segments (reduces glitches)
\ifdim\pgf@xa>.1pt\relax%
\pgf@nfold@process@line@common{#1}{#2}{l}%