summaryrefslogtreecommitdiff
path: root/graphics/pgf/base/tex/pgfcorepatterns.code.tex
blob: 7f72030c42e8984329cc9d434054c75e4a0bb5e2 (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
% Copyright 2019 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{pgfcorepatterns.code.tex}

% Creates a new uncolored pattern
%
% [#1] = optional list of variables.
% #2   = pattern name
% #3   = lower left of bounding box
% #4   = upper right of bounding box
% #5   = step vector
% #6   = pattern code
%
% Description:
%
% Declares a new uncolored pattern. An uncolored pattern is a pattern
% that has no inherent color. When the pattern is used, a color for
% the pattern must be supplied.
%
% See the pdf-manual for more details on uncolored patterns.
%
% Specifying a variable list makes the pattern ``mutable''.
%
% The bounding box must be a bounding box of the pattern cell.
%
% The step vector must provide the x- and y-step by which the cells
% are spaced.
%
% The pattern code should be pgf code that can be protocolled.
%
% The transformation matrix will be reset prior to invoking the
% pattern code.
%
% Example:
%
% \pgfdeclarepatternformonly{stars}{\pgfpointorigin}{\pgfpoint{1cm}{1cm}}
% {\pgfpoint{1cm}{1cm}}{
%   \pgftransformshift{\pgfpoint{.5cm}{.5cm}}
%   \pgfpathmoveto{\pgfpointpolar{0}{4mm}}
%   \pgfpathlineto{\pgfpointpolar{144}{4mm}}
%   \pgfpathlineto{\pgfpointpolar{288}{4mm}}
%   \pgfpathlineto{\pgfpointpolar{72}{4mm}}
%   \pgfpathlineto{\pgfpointpolar{216}{4mm}}
%   \pgfpathclose%
%   \pgfusepath{stroke}
% }
%
\def\pgfdeclarepatternformonly{%
  \def\pgf@pattern@type{formonly}%
  \pgfutil@ifnextchar[{\pgf@declarepattern@checkarg}{\pgf@declarepattern@checkarg[]}%
}


% Creates a new colored pattern
%
% [#1] = optional list of variables.
% #2   = pattern name
% #3   = lower left of bounding box
% #4   = upper right of bounding box
% #5   = step vector
% #6   = pattern code
%
% Description:
%
% Declares a new colored pattern. Such patterns have a one or more
% fixed inherent colors. See the pdf-manual for more details on
% uncolored patterns.
%
% The parameters have the same effect as for uncolored patterns.
%
% Example:
%
% \pgfdeclarepatterninherentlycolored{red stars}{\pgfpointorigin}{\pgfpoint{1cm}{1cm}}
% {\pgfpoint{1cm}{1cm}}{
%   \pgfsetfillcolor{red}
%   \pgfsetstrokecolor{black}
%   \pgftransformshift{\pgfpoint{.5cm}{.5cm}}
%   \pgfpathmoveto{\pgfpointpolar{0}{4mm}}
%   \pgfpathlineto{\pgfpointpolar{144}{4mm}}
%   \pgfpathlineto{\pgfpointpolar{288}{4mm}}
%   \pgfpathlineto{\pgfpointpolar{72}{4mm}}
%   \pgfpathlineto{\pgfpointpolar{216}{4mm}}
%   \pgfpathclose%
%   \pgfusepath{stroke,fill}
% }
%
\def\pgfdeclarepatterninherentlycolored{%
  \def\pgf@pattern@type{inherentlycolored}%
  \pgfutil@ifnextchar[{\pgf@declarepattern@checkarg}{\pgf@declarepattern@checkarg[]}%
}





\def\pgf@declarepattern@checkarg[#1]{%
  \def\pgf@pattern@tempvars{#1}%
  \ifx\pgf@pattern@tempvars\pgfutil@empty%
    \expandafter\let\expandafter\pgf@next\csname pgf@declarepattern\pgf@pattern@type\endcsname%
  \else%
    \expandafter\let\expandafter\pgf@next\csname pgf@declarepattern\pgf@pattern@type @mutable\endcsname%
  \fi%
  \pgf@next}

\long\def\pgf@declarepatternformonly#1#2#3#4#5{\pgf@declarepattern{#1}{#2}{#3}{#4}{#5}{0}}

\long\def\pgf@declarepatterninherentlycolored#1#2#3#4#5{\pgf@declarepattern{#1}{#2}{#3}{#4}{#5}{1}}

\long\def\pgf@declarepatternformonly@mutable#1#2#3#4#5{%
  \def\pgf@marshal{\pgf@declarepatternmutable{#1}}%
  \expandafter\pgf@marshal\expandafter{\pgf@pattern@tempvars}{#2}{#3}{#4}{#5}{2}%
}

\long\def\pgf@declarepatterninherentlycolored@mutable#1#2#3#4#5#6{%
  \def\pgf@marshal{\pgf@declarepatternmutable{#1}}%
  \expandafter\pgf@marshal\expandafter{\pgf@pattern@tempvars}{#2}{#3}{#4}{#5}{3}%
}

\def\pgfsys@patternmatrix{{1.0}{0.0}{0.0}{1.0}{0.0pt}{0.0pt}}
\def\pgf@declarepattern#1#2#3#4#5#6{%
  \pgfutil@ifundefined{pgf@pattern@name@#1}{%
  \pgfsysprotocol@getcurrentprotocol\pgf@pattern@temp%
  {%
    \pgfinterruptpath%
      \pgfpicturetrue%
      \pgf@relevantforpicturesizefalse%
      \pgftransformreset%
      \pgfsysprotocol@setcurrentprotocol\pgfutil@empty%
      \pgfsysprotocol@bufferedtrue%
      \pgfsys@beginscope%
      \pgfsetarrows{-}%
      #5%
      \pgfsys@endscope%
      \pgfsysprotocol@getcurrentprotocol\pgf@pattern@code%
      \global\let\pgf@pattern@code=\pgf@pattern@code%
    \endpgfinterruptpath%
    \pgf@process{#2}%
    \pgf@xa=\pgf@x%
    \pgf@ya=\pgf@y%
    \pgf@process{#3}%
    \pgf@xb=\pgf@x%
    \pgf@yb=\pgf@y%
    \pgf@process{#4}%
    \pgf@xc=\pgf@x%
    \pgf@yc=\pgf@y%
    % Now, build a name for the pattern
    \pgfutil@tempcnta=\pgf@pattern@number%
    \advance\pgfutil@tempcnta by1\relax%
    \xdef\pgf@pattern@number{\the\pgfutil@tempcnta}%
    \expandafter\xdef\csname pgf@pattern@name@#1\endcsname{\the\pgfutil@tempcnta}%
    \expandafter\gdef\csname pgf@pattern@type@#1\endcsname{#6}%
    \xdef\pgf@marshal{\noexpand\pgfsys@declarepattern
      {\csname pgf@pattern@name@#1\endcsname}% name
      {\the\pgf@xa}{\the\pgf@ya}{\the\pgf@xb}{\the\pgf@yb}% bbox
      {\the\pgf@xc}{\the\pgf@yc}% x/y step
      \pgfsys@patternmatrix% transformation matrix
      {\pgf@pattern@code}{#6}}%
  }%
  \pgfsysprotocol@setcurrentprotocol\pgf@pattern@temp%
  \expandafter\global\expandafter\let\csname pgf@pattern@instantiate@#1\endcsname=\pgf@marshal%
  }
  {%
    \pgferror{Pattern `#1' already defined}%
  }%
}

\def\pgf@pattern@number{0}%

\def\pgf@declarepatternmutable#1#2#3#4#5#6#7{%
  \pgfutil@ifundefined{pgf@pattern@name@#1}{%
    \expandafter\gdef\csname pgf@pattern@name@#1\endcsname{#1}%
    \expandafter\gdef\csname pgf@pattern@variables@#1\endcsname{#2}%
    \expandafter\gdef\csname pgf@pattern@lowerleft@#1\endcsname{#3}%
    \expandafter\gdef\csname pgf@pattern@upperright@#1\endcsname{#4}%
    \expandafter\gdef\csname pgf@pattern@tilesize@#1\endcsname{#5}%
    \expandafter\long\expandafter\gdef\csname pgf@pattern@code@#1\endcsname{#6}%
    \expandafter\gdef\csname pgf@pattern@type@#1\endcsname{7}%
  }{\pgferror{The pattern `#1' is already defined}}%
}

\def\pgf@ifpatternisinherentlycolored#1#2#3{%
  \expandafter\ifodd\csname pgf@pattern@type@#1\endcsname\relax#2\else#3\fi%
}

\def\pgf@ifpatternismutable#1#2#3{%
  \expandafter\ifnum\csname pgf@pattern@type@#1\endcsname>1\relax#2\else#3\fi%
}

\def\pgfpatternreleasename#1{\expandafter\let\csname pgf@pattern@name@#1\endcsname\relax}






% Set an pattern as fill color
%
% #1 = Name of the pattern
% #2 = Color to be used for the pattern. If the pattern is already
%      colored, this parameter is ignored.
%
% Description:
%
% Sets the pattern for subsequent filling operations.
%
% Example:
%
% \pgfsetfillpattern{stars}{red}
%
\def\pgfsetfillpattern#1#2{%
  \pgfutil@ifundefined{pgf@pattern@name@#1}%
  {%
    \pgferror{Undefined pattern `#1'}%
  }%
  {%
    \pgf@ifpatternismutable{#1}%
    {%
      % So, a mutable pattern. Check to see if the pattern
      % has been used with the current variable values...
      \let\pgf@pattern@tempvars\pgfutil@empty%
      \expandafter\expandafter\expandafter\pgf@pattern@check@vars%
        \csname pgf@pattern@variables@#1\endcsname,\pgf@stop,%
      \edef\pgf@pattern@nametemp{pgf@pattern@#1@\pgf@pattern@tempvars}%
      \expandafter\pgfutil@ifundefined\expandafter{\pgf@pattern@nametemp}%
      {%
        % ...Nope. So declare a new instance of the mutable pattern.
        \pgf@ifpatternisinherentlycolored{#1}%
          {\let\pgf@set@fillpattern@declare\pgfdeclarepatterninherentlycolored}%
          {\let\pgf@set@fillpattern@declare\pgfdeclarepatternformonly}%
        \pgf@set@fillpattern@declare%
          {\pgf@pattern@nametemp}%
          {\csname pgf@pattern@lowerleft@#1\endcsname}%
          {\csname pgf@pattern@upperright@#1\endcsname}%
          {\csname pgf@pattern@tilesize@#1\endcsname}%
          {\csname pgf@pattern@code@#1\endcsname}%
      }%
      {%
        % ...Yep. So do nothing.
      }%
      \expandafter\pgf@set@fillpattern\expandafter{\pgf@pattern@nametemp}{#2}%
      \expandafter\gdef\csname\pgf@pattern@nametemp\endcsname{#1}%
    }%
    {%
      % A non-mutable pattern.
      \pgf@set@fillpattern{#1}{#2}%
    }%
  }%
}


\def\pgf@set@fillpattern#1#2{%
  \pgfutil@ifundefined{pgf@pattern@name@#1}{%
    \pgferror{Undefined pattern `#1'}}
  {%
    \csname pgf@pattern@instantiate@#1\endcsname%
    \expandafter\global\expandafter\let\csname pgf@pattern@instantiate@#1\endcsname=\relax%
    \pgf@ifpatternisinherentlycolored{#1}{%
      \pgfsys@setpatterncolored{\csname pgf@pattern@name@#1\endcsname}%
    }{%
      \pgfutil@colorlet{pgf@tempcolor}{#2}%
      \pgfutil@ifundefined{applycolormixins}{}{\applycolormixins{pgf@tempcolor}}%
      \pgfutil@extractcolorspec{pgf@tempcolor}{\pgf@tempcolor}%
      \expandafter\pgfutil@convertcolorspec\pgf@tempcolor{rgb}{\pgf@rgbcolor}%
      \expandafter\pgf@set@fill@patternuncolored\pgf@rgbcolor\relax{#1}%
    }%
  }%
}

\def\pgf@set@fill@patternuncolored#1,#2,#3\relax#4{%
  \pgfsys@setpatternuncolored{\csname pgf@pattern@name@#4\endcsname}{#1}{#2}{#3}%
}

\def\pgf@pattern@check@vars#1,{%
  \ifx#1\pgf@stop%
    \let\pgf@next\relax%
  \else%
    \afterassignment\pgfmath@gobbletilpgfmath@%
    \expandafter\let\expandafter\pgf@pattern@char#1#1\pgfmath@%
    \ifx\pgf@pattern@char/%
      \edef\pgf@pattern@tempvar{(\pgfkeysvalueof{#1})}%
    \else%
      \expandafter\ifcat\pgf@pattern@char\relax%
        \edef\pgf@pattern@tempvar{(\the#1)}%
      \else%
        \edef\pgf@pattern@tempvar{(#1)}%
      \fi%
    \fi%
    \edef\pgf@pattern@tempvars{\pgf@pattern@tempvars\pgf@pattern@tempvar}%
    \let\pgf@next\pgf@pattern@check@vars%
  \fi%
  \pgf@next%
}

\endinput