| 1 |
% Copyright 2019 by Mark Wibrow |
| 2 |
% |
| 3 |
% This file may be distributed and/or modified |
| 4 |
% |
| 5 |
% 1. under the LaTeX Project Public License and/or |
| 6 |
% 2. under the GNU Public License. |
| 7 |
% |
| 8 |
% See the file doc/generic/pgf/licenses/LICENSE for more details. |
| 9 |
|
| 10 |
% Declare a function for the parser. |
| 11 |
% |
| 12 |
% #1 - the name of the function. |
| 13 |
% #2 - arity (or ... for variable arguments). |
| 14 |
% #3 - the code for the function. |
| 15 |
% |
| 16 |
% Use \pgfmathdeclarefunction* to force a redefinition if the function exists already. |
| 17 |
\def\pgfmathdeclarefunction{\pgfmath@ifnextchar*\pgfmathdeclarefunction@force\pgfmathdeclarefunction@check} |
| 18 |
\def\pgfmathdeclarefunction@check#1{% |
| 19 |
\expandafter\ifx\csname pgfmath@function@#1\endcsname\relax\else% |
| 20 |
\pgfmath@error{The function `#1' already exists}{}% |
| 21 |
\fi% |
| 22 |
\pgfmath@declarefunction{#1}% |
| 23 |
} |
| 24 |
\def\pgfmathdeclarefunction@force*{\pgfmath@declarefunction}% |
| 25 |
|
| 26 |
\def\pgfmathredeclarefunction#1#2{% |
| 27 |
\expandafter\ifx\csname pgfmath@function@#1\endcsname\relax% |
| 28 |
\pgfmath@error{The function `#1' does not exist}{}% |
| 29 |
\else% |
| 30 |
% was: \pgfmath@namedef{pgfmath#1@}{#2}% TT |
| 31 |
\edef\pgf@marshal{\noexpand\pgfmathdeclarefunction*{#1}{\csname pgfmath@operation@#1@arity\endcsname}}% |
| 32 |
\pgf@marshal{#2}% |
| 33 |
\fi% |
| 34 |
} |
| 35 |
|
| 36 |
\def\pgfmath@declarefunction#1#2{% |
| 37 |
\pgfmath@namedef{pgfmath@function@#1}{#1}% |
| 38 |
\pgfmath@namedef{pgfmath@operation@#1@type}{function}% |
| 39 |
\pgfmath@namedef{pgfmath@operation@#1@arity}{#2}% |
| 40 |
\pgfmath@namedef{pgfmath@operation@#1@infix}{#1}% |
| 41 |
\pgfmath@namedef{pgfmath@operation@#1@precedence}{1000}% |
| 42 |
\expandafter\ifx\csname pgfmath@operation@#1@arity\endcsname\pgfmath@dots% |
| 43 |
\pgfmath@namedef{pgfmath@operation@#1@arity}{1}% |
| 44 |
\else% |
| 45 |
\ifnum\csname pgfmath@operation@#1@arity\endcsname>9\relax% |
| 46 |
\pgfmath@namedef{pgfmath@operation@#1@arity}{1}% |
| 47 |
\fi% |
| 48 |
\fi% |
| 49 |
\edef\pgfmath@marshal{% |
| 50 |
\noexpand\pgfmath@@declarefunction% |
| 51 |
{\expandafter\noexpand\csname pgfmath#1\endcsname}% |
| 52 |
{\expandafter\noexpand\csname pgfmath#1@\endcsname}% |
| 53 |
{\csname pgfmath@operation@#1@arity\endcsname}% |
| 54 |
}% |
| 55 |
\pgfkeysvalueof{/pgf/declare function/execute at begin function}% |
| 56 |
\pgfmath@marshal% |
| 57 |
} |
| 58 |
|
| 59 |
% #1 - \pgfmath<name>. |
| 60 |
% #2 - \pgfmath<name>@. |
| 61 |
% #3 - arity. |
| 62 |
% #4 - code for function <name>. |
| 63 |
% |
| 64 |
\def\pgfmath@@declarefunction#1#2#3#4{% Function of arity 0 - 9. |
| 65 |
\pgfkeysvalueof{/pgf/declare function/execute at end function}% |
| 66 |
\ifcase#3\relax% |
| 67 |
\def#1{#2}% |
| 68 |
\def\pgfmath@@head{\def#2}% |
| 69 |
\or |
| 70 |
\def#1##1{\pgfmathparse{##1}\expandafter#2\expandafter{\pgfmathresult}}% |
| 71 |
\def\pgfmath@@head{\def#2####1}% |
| 72 |
\else% |
| 73 |
\pgfmath@toks={}% |
| 74 |
\pgfmathloop% |
| 75 |
\ifnum\pgfmathcounter>#3\relax% |
| 76 |
\else% |
| 77 |
\expandafter\pgfmath@addto@toks\expandafter{\pgfmath@char@hash}% |
| 78 |
\expandafter\pgfmath@addto@toks\expandafter{\pgfmathcounter}% |
| 79 |
\repeatpgfmathloop% |
| 80 |
\edef\pgfmath@head{\noexpand\def\noexpand#1\the\pgfmath@toks}% |
| 81 |
\edef\pgfmath@@head{\noexpand\def\noexpand#2\the\pgfmath@toks}% |
| 82 |
\pgfmath@toks={}% |
| 83 |
\def\pgfmath@arguments{}% |
| 84 |
\pgfmathloop% |
| 85 |
\ifnum\pgfmathcounter>#3\relax% |
| 86 |
\else% |
| 87 |
\pgfmath@addto@toks{\pgfmathparse}% |
| 88 |
\expandafter\expandafter\expandafter\pgfmath@addto@toks% |
| 89 |
\expandafter\expandafter\expandafter{\expandafter\expandafter\expandafter% |
| 90 |
{\expandafter\pgfmath@char@hash\pgfmathcounter}}% |
| 91 |
\pgfmath@addto@toks{\let}% |
| 92 |
\expandafter\pgfmath@addto@toks\expandafter{% |
| 93 |
\csname pgfmath@argument@\pgfmathcounter\endcsname=\pgfmathresult}% |
| 94 |
\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter% |
| 95 |
\pgfmath@arguments\expandafter\expandafter\expandafter{\expandafter% |
| 96 |
\pgfmath@arguments\expandafter{\csname pgfmath@argument@\pgfmathcounter\endcsname}}% |
| 97 |
\repeatpgfmathloop% |
| 98 |
\expandafter\pgfmath@addto@toks\expandafter{\expandafter#2\pgfmath@arguments}% |
| 99 |
\edef\pgfmath@body{{\the\pgfmath@toks}}% |
| 100 |
\expandafter\pgfmath@head\pgfmath@body% |
| 101 |
\fi% |
| 102 |
\pgfmath@@head{#4}% |
| 103 |
} |
| 104 |
|
| 105 |
\def\pgfmath@addto@toks#1{% |
| 106 |
\edef\pgfmath@toks@expanded{\the\pgfmath@toks}% |
| 107 |
\expandafter\pgfmath@toks\expandafter{\pgfmath@toks@expanded#1}} |
| 108 |
|
| 109 |
% Key for declaring local functions. |
| 110 |
% |
| 111 |
\pgfkeys{% |
| 112 |
/pgf/declare function/.code={% |
| 113 |
\pgfmath@local@functions#1@=@;% |
| 114 |
}, |
| 115 |
/pgf/declare function/execute at begin function/.initial={}, |
| 116 |
/pgf/declare function/execute at end function/.initial={}, |
| 117 |
/pgf/declare function/ignore spaces/.is choice, |
| 118 |
/pgf/declare function/ignore spaces/.default=true, |
| 119 |
/pgf/declare function/ignore spaces/true/.style={% |
| 120 |
/pgf/declare function/execute at begin function={% |
| 121 |
\begingroup |
| 122 |
\catcode`\^^I=9\relax |
| 123 |
\catcode`\ =9\relax |
| 124 |
\catcode`\~=10\relax |
| 125 |
\endlinechar=`\ \relax |
| 126 |
}, |
| 127 |
/pgf/declare function/execute at end function={% |
| 128 |
\endgroup |
| 129 |
}, |
| 130 |
}, |
| 131 |
/pgf/declare function/ignore spaces/false/.style={% |
| 132 |
/pgf/declare function/execute at begin function={}, |
| 133 |
/pgf/declare function/execute at end function={}, |
| 134 |
}, |
| 135 |
} |
| 136 |
|
| 137 |
\def\pgfmath@local@gobbleone#1{} |
| 138 |
\def\pgfmath@local@at{@}% |
| 139 |
|
| 140 |
\def\pgfmath@local@functions{% |
| 141 |
\pgfutil@ifnextchar x{\pgfmath@local@@functions}{\pgfmath@local@@functions}% |
| 142 |
} |
| 143 |
|
| 144 |
\def\pgfmath@local@@functions#1=#2;{% |
| 145 |
\def\pgfmath@local@temp{#1}% |
| 146 |
\ifx\pgfmath@local@temp\pgfmath@local@at% |
| 147 |
\let\pgfmath@local@next=\relax% |
| 148 |
\else% |
| 149 |
\pgfmath@local@function#1=#2;% |
| 150 |
\let\pgfmath@local@next=\pgfmath@local@functions% |
| 151 |
\fi% |
| 152 |
\pgfmath@local@next% |
| 153 |
} |
| 154 |
|
| 155 |
\def\pgfmath@local@function{% |
| 156 |
\let\pgfmath@local@name=\pgfutil@empty% |
| 157 |
\let\pgfmath@local@args=\pgfutil@empty% |
| 158 |
\let\pgfmath@local@body=\pgfutil@empty% |
| 159 |
\pgfmath@local@@function} |
| 160 |
|
| 161 |
\def\pgfmath@local@@function#1{% |
| 162 |
\if#1=% |
| 163 |
\let\pgfmath@local@next=\pgfmath@local@function@body% |
| 164 |
\else% |
| 165 |
\if#1(% |
| 166 |
\let\pgfmath@local@next=\pgfmath@local@function@args% |
| 167 |
\else% |
| 168 |
\expandafter\def\expandafter\pgfmath@local@name\expandafter{\pgfmath@local@name#1}% |
| 169 |
\let\pgfmath@local@next=\pgfmath@local@@function% |
| 170 |
\fi% |
| 171 |
\fi% |
| 172 |
\pgfmath@local@next% |
| 173 |
} |
| 174 |
|
| 175 |
\def\pgfmath@local@function@args#1){% |
| 176 |
\def\pgfmath@local@args{#1}% |
| 177 |
\pgfmath@local@@function} |
| 178 |
|
| 179 |
\def\pgfmath@local@function@body#1;{% |
| 180 |
\def\pgfmath@local@body{#1}% |
| 181 |
\begingroup% |
| 182 |
\c@pgf@counta=0\relax% |
| 183 |
\ifx\pgfmath@local@args\pgfmath@empty% |
| 184 |
\expandafter\pgfmath@toks\expandafter=\expandafter{\pgfmath@local@body}% |
| 185 |
\else% |
| 186 |
\pgfmath@toks={}% |
| 187 |
\expandafter\pgfmath@local@function@@body\pgfmath@local@args,,% |
| 188 |
\fi% |
| 189 |
\xdef\pgfmath@local@temp{% |
| 190 |
\noexpand\pgfmathnotifynewdeclarefunction{\pgfmath@local@name}{\the\c@pgf@counta}% |
| 191 |
{\the\pgfmath@toks}% |
| 192 |
}% |
| 193 |
\endgroup% |
| 194 |
\pgfmath@local@temp% |
| 195 |
} |
| 196 |
|
| 197 |
% #1: the name |
| 198 |
% #2: the number of arguments <n> |
| 199 |
% #3: a math expression containing '#1', '#2', ... ,'#'<n> |
| 200 |
% This callback is overwritten by pgflibraryluamath. |
| 201 |
\def\pgfmathnotifynewdeclarefunction#1#2#3{% |
| 202 |
\pgfmathdeclarefunction{#1}{#2}{\pgfmathparse{#3}}% |
| 203 |
}% |
| 204 |
|
| 205 |
\def\pgfmath@local@function@@body#1,{% |
| 206 |
\def\pgfmath@local@test{#1}% |
| 207 |
\ifx\pgfmath@local@test\pgfutil@empty% |
| 208 |
\let\pgfmath@local@next=\relax% |
| 209 |
\else% |
| 210 |
\edef\pgfmath@local@var@string{@\expandafter\pgfmath@local@gobbleone\string#1}% |
| 211 |
\advance\c@pgf@counta by1\relax% |
| 212 |
\expandafter\expandafter\expandafter\pgfmath@toks\expandafter\expandafter\expandafter=% |
| 213 |
\expandafter\expandafter\expandafter{\expandafter\pgfmath@char@hash\the\c@pgf@counta}% |
| 214 |
\edef#1{\the\pgfmath@toks}% |
| 215 |
\pgfmath@toks={}% |
| 216 |
\expandafter\pgfmath@local@function@@@body\pgfmath@local@body @% |
| 217 |
\edef\pgfmath@local@body{\the\pgfmath@toks}% |
| 218 |
\let\pgfmath@local@next=\pgfmath@local@function@@body% |
| 219 |
\fi% |
| 220 |
\pgfmath@local@next% |
| 221 |
} |
| 222 |
|
| 223 |
\def\pgfmath@local@function@@@body{% |
| 224 |
\pgfutil@ifnextchar\bgroup{\pgfmath@local@function@@@body@bgroup}% |
| 225 |
{\pgfmath@local@function@@@body@nobgroup}}% |
| 226 |
|
| 227 |
\def\pgfmath@local@function@@@body@bgroup#1{% |
| 228 |
\begingroup% |
| 229 |
\pgfmath@toks={}% |
| 230 |
\pgfmath@local@function@@@body#1 @% |
| 231 |
\xdef\pgfmath@local@temp{{\the\pgfmath@toks}}% |
| 232 |
\endgroup% |
| 233 |
\expandafter\pgfmath@addto@toks\expandafter{\pgfmath@local@temp}% |
| 234 |
\pgfmath@local@function@@@body% |
| 235 |
} |
| 236 |
|
| 237 |
\def\pgfmath@local@function@@@body@nobgroup#1{% |
| 238 |
\ifx#1@% |
| 239 |
\let\pgfmath@local@next=\relax% |
| 240 |
\else% |
| 241 |
\let\pgfmath@local@next=\pgfmath@local@function@@@body% |
| 242 |
\edef\pgfmath@local@var@@string{@\expandafter\pgfmath@local@gobbleone\string#1}% |
| 243 |
\ifx\pgfmath@local@var@@string\pgfmath@local@var@string% |
| 244 |
\expandafter\pgfmath@addto@toks\expandafter{#1}% |
| 245 |
\else% |
| 246 |
\edef\pgfmath@local@temp{\the\pgfmath@toks}% |
| 247 |
\pgfmath@toks={#1}% |
| 248 |
\edef\pgfmath@local@@temp{\the\pgfmath@toks}% |
| 249 |
\expandafter\pgfmath@toks\expandafter=\expandafter{\pgfmath@local@temp}% |
| 250 |
\expandafter\pgfmath@addto@toks\expandafter{\pgfmath@local@@temp}% |
| 251 |
\fi% |
| 252 |
\fi% |
| 253 |
\pgfmath@local@next% |
| 254 |
} |
| 255 |
|
| 256 |
% Allows to define 'x' as some \TeX command '#2'. |
| 257 |
\def\pgfmathdeclarepseudoconstant#1#2{% |
| 258 |
\pgfmathdeclarefunction*{#1}{0}{#2}% |
| 259 |
}% |
| 260 |
% Redefines a pseudo constant '#1'. |
| 261 |
\def\pgfmathredeclarepseudoconstant#1#2{% |
| 262 |
\pgfmathredeclarefunction{#1}{#2}% |
| 263 |
}% |
| 264 |
|
| 265 |
% Local Variables: |
| 266 |
% tab-width: 2 |
| 267 |
% End: |