Debian Codegraph @

texmf-dist/tex/generic/pgf/systemlayer/pgfsyssoftpath.code.tex

raw
1 % Copyright 2019 by Till Tantau
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 \ProvidesFileRCS{pgfsyssoftpath.code.tex}
11
12
13 % This package provides the pgf system path abstraction layer. This
14 % layer is used to construct paths first ``abstractly.'' These
15 % abstract paths can still be modified, before they are ``made
16 % concrete.''
17 %
18 % An abstract path consists of a sequence of basic building blocks,
19 % each of which is a tuple consisting of two numbers (specifying a
20 % coordinate) and a token. Possible tokens are:
21 %
22 % 1. moveto
23 % 2. lineto
24 % 3. rectcorner
25 % 4. rectsize
26 % 5. curvetosupporta
27 % 6. curvetosupportb
28 % 7. curveto
29 % 8. closepath
30 % 9. specialround
31 %
32 % A curveto must always be preceded by a curvetosupporta and a
33 % curvetosupportb. A non-empty path must always begin with a
34 % moveto. The coordinates of a closepath are non-specified, currently,
35 % but they might be set to the coordinate of the path segment start,
36 % in the future.
37
38
39 % Access to the current path:
40 %
41 % Lots of stuff is added to the current path and it can get very long
42 % (containing literally tens of thousands of tokens). For such macros,
43 % adding things using a simple "g@addtomacro" takes more and more
44 % time, resulting in quadratic runtime. To avoid this, ideally, one
45 % would collect things in an array and then use a divide and conquer
46 % merger. A simple intermediate solution is the following, implemented
47 % here: We have the path and two buffers. New tokens are added to the
48 % first buffer, which is quite small. When it overflow, its contents
49 % is added to the second buffer, which is large. If that buffer
50 % overflow, the contents is finally added to the main path (which can
51 % have arbitrary length). Whenever the main path is set or read, the
52 % buffers are flushed.
53 %
54 % Because of this buffering, it is imperative that the main path is
55 % accessed only via appropriate interface macros.
56
57
58
59 % The current path
60 \let\pgfsyssoftpath@thepath=\pgfutil@empty
61 \let\pgfsyssoftpath@bigbuffer=\pgfutil@empty
62 \let\pgfsyssoftpath@smallbuffer=\pgfutil@empty
63 \newcount\pgfsyssoftpath@smallbuffer@items
64 \newcount\pgfsyssoftpath@bigbuffer@items
65
66 \def\pgfsyssoftpath@flushbuffers{%
67 \expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\gdef%
68 \expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\pgfsyssoftpath@thepath%
69 \expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter{%
70 \expandafter\expandafter\expandafter\pgfsyssoftpath@thepath%
71 \expandafter\pgfsyssoftpath@bigbuffer\pgfsyssoftpath@smallbuffer}%
72 \global\let\pgfsyssoftpath@smallbuffer=\pgfutil@empty
73 \global\let\pgfsyssoftpath@bigbuffer=\pgfutil@empty
74 \global\pgfsyssoftpath@bigbuffer@items0\relax%
75 \global\pgfsyssoftpath@smallbuffer@items0\relax%
76 }
77
78 % Getting and setting the current path
79 \def\pgfsyssoftpath@getcurrentpath#1{%
80 \pgfsyssoftpath@flushbuffers%
81 \let#1=\pgfsyssoftpath@thepath%
82 }
83
84 \def\pgfsyssoftpath@setcurrentpath#1{%
85 \global\let\pgfsyssoftpath@thepath=#1%
86 \global\let\pgfsyssoftpath@smallbuffer=\pgfutil@empty
87 \global\let\pgfsyssoftpath@bigbuffer=\pgfutil@empty
88 \global\pgfsyssoftpath@bigbuffer@items0\relax%
89 \global\pgfsyssoftpath@smallbuffer@items0\relax%
90 }
91
92
93 % Invoking the current path (slightly optimized)
94 \def\pgfsyssoftpath@invokecurrentpath{%
95 \pgfsyssoftpath@thepath%
96 \pgfsyssoftpath@bigbuffer%
97 \pgfsyssoftpath@smallbuffer%
98 }
99
100 \def\pgfsyssoftpath@flushcurrentpath{%
101 \pgfsyssoftpath@invokecurrentpath%
102 \pgfsyssoftpath@setcurrentpath\pgfutil@empty%
103 }
104
105 % Add an item at the end
106 \def\pgfsyssoftpath@addtocurrentpath#1{%
107 \global\advance\pgfsyssoftpath@smallbuffer@items by1\relax%
108 \ifnum\pgfsyssoftpath@smallbuffer@items<40\relax% good number?
109 \expandafter\gdef\expandafter\pgfsyssoftpath@smallbuffer\expandafter{\pgfsyssoftpath@smallbuffer#1}%
110 \else%
111 \pgfsyssoftpath@smalloverflow{#1}%
112 \fi%
113 }
114 \def\pgfsyssoftpath@smalloverflow#1{%
115 \global\advance\pgfsyssoftpath@bigbuffer@items by1\relax%
116 \ifnum\pgfsyssoftpath@bigbuffer@items<30\relax% good number?
117 \expandafter\expandafter\expandafter\gdef%
118 \expandafter\expandafter\expandafter\pgfsyssoftpath@bigbuffer%
119 \expandafter\expandafter\expandafter{\expandafter\pgfsyssoftpath@bigbuffer\pgfsyssoftpath@smallbuffer#1}%
120 \global\let\pgfsyssoftpath@smallbuffer=\pgfutil@empty%
121 \global\pgfsyssoftpath@smallbuffer@items0\relax%
122 \else%
123 \expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\gdef%
124 \expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\pgfsyssoftpath@thepath%
125 \expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter{%
126 \expandafter\expandafter\expandafter\pgfsyssoftpath@thepath
127 \expandafter\pgfsyssoftpath@bigbuffer\pgfsyssoftpath@smallbuffer#1}%
128 \global\let\pgfsyssoftpath@smallbuffer=\pgfutil@empty
129 \global\let\pgfsyssoftpath@bigbuffer=\pgfutil@empty
130 \global\pgfsyssoftpath@bigbuffer@items0\relax%
131 \global\pgfsyssoftpath@smallbuffer@items0\relax%
132 \fi%
133 }
134
135
136
137 \def\pgfsyssoftpath@lastmoveto{{0pt}{0pt}}
138
139 \newif\ifpgfsyssoftpathmovetorelevant
140 \pgfsyssoftpathmovetorelevanttrue
141
142 % Add a moveto element to the current path
143 \def\pgfsyssoftpath@moveto#1#2{%
144 \edef\pgfsyssoftpath@coord{{#1}{#2}}%
145 \expandafter\pgfsyssoftpath@addtocurrentpath\expandafter{\expandafter\pgfsyssoftpath@movetotoken\pgfsyssoftpath@coord}%
146 \ifpgfsyssoftpathmovetorelevant%
147 \global\let\pgfsyssoftpath@lastmoveto\pgfsyssoftpath@coord%
148 \fi%
149 }
150
151 % Add a lineto element to the current path
152 \def\pgfsyssoftpath@lineto#1#2{%
153 \edef\pgfsyssoftpath@coord{{#1}{#2}}%
154 \expandafter\pgfsyssoftpath@addtocurrentpath\expandafter{\expandafter\pgfsyssoftpath@linetotoken\pgfsyssoftpath@coord}%
155 }
156
157
158 % Add curveto elements to the current path
159 \def\pgfsyssoftpath@curveto#1#2#3#4#5#6{%
160 \edef\pgfsyssoftpath@temp{{%
161 \noexpand\pgfsyssoftpath@curvetosupportatoken{#1}{#2}%
162 \noexpand\pgfsyssoftpath@curvetosupportbtoken{#3}{#4}%
163 \noexpand\pgfsyssoftpath@curvetotoken{#5}{#6}%
164 }}%
165 \expandafter\pgfsyssoftpath@addtocurrentpath\pgfsyssoftpath@temp%
166 }
167
168
169 % Add rectangle elements to the current path
170 \def\pgfsyssoftpath@rect#1#2#3#4{%
171 \edef\pgfsyssoftpath@temp{{%
172 \noexpand\pgfsyssoftpath@rectcornertoken{#1}{#2}%
173 \noexpand\pgfsyssoftpath@rectsizetoken{#3}{#4}%
174 }}%
175 \expandafter\pgfsyssoftpath@addtocurrentpath\pgfsyssoftpath@temp%
176 }
177
178 % Add closepath element to the current path
179 \def\pgfsyssoftpath@closepath{%
180 \expandafter\pgfsyssoftpath@addtocurrentpath\expandafter{\expandafter\pgfsyssoftpath@closepathtoken\pgfsyssoftpath@lastmoveto}%
181 }
182
183 % Add special element to the current path
184 \def\pgfsyssoftpath@specialround#1#2{%
185 \edef\pgfsyssoftpath@temp{{#1}{#2}}%
186 \expandafter\pgfsyssoftpath@addtocurrentpath\expandafter{\expandafter\pgfsyssoftpath@specialroundtoken\pgfsyssoftpath@temp}%
187 }
188
189 % Marshallers
190 \def\pgfsyssoftpath@movetotoken#1#2{\pgfsys@moveto{#1}{#2}}
191 \def\pgfsyssoftpath@linetotoken#1#2{\pgfsys@lineto{#1}{#2}}
192 \def\pgfsyssoftpath@rectcornertoken#1#2#3#4#5{\pgfsys@rect{#1}{#2}{#4}{#5}} % #3 = \pgfsyssoftpath@rectsizetoken
193 \def\pgfsyssoftpath@curvetosupportatoken#1#2#3#4#5#6#7#8{\pgfsys@curveto{#1}{#2}{#4}{#5}{#7}{#8}}
194 \def\pgfsyssoftpath@closepathtoken#1#2{\pgfsys@closepath}
195 \let\pgfsyssoftpath@specialroundtoken=\pgfutil@gobbletwo
196 \def\pgfsyssoftpath@curvetosupportbtoken#1#2{curvetotokenb} % make sure this \ifx-equal only to itself
197 \def\pgfsyssoftpath@curvetotoken#1#2{curvetotoken}% make sure this \ifx-equal only to itself
198
199
200 \endinput
201
202 %%% Local Variables:
203 %%% mode: latex
204 %%% End: