Debian Codegraph @

texmf-dist/tex/generic/pgf/libraries/decorations/pgflibrarydecorations.text.code.tex

raw
1 % Copyright 2018 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 \usepgfmodule{decorations}%
11
12
13 % Options for text decorations
14
15 \pgfkeys{
16 /pgf/decoration/.cd,
17 text/.store in=\pgfdecorationtext,%
18 text color/.store in=\pgf@lib@decorationtextcolor,%
19 text format delimiters/.code={\expandafter\pgfsetdecoratetextformatdelimiters#1},
20 text align/.code={\pgfkeys{/pgf/decoration/text align/.cd,#1}},
21 text align/.cd,
22 align/.initial=left,
23 left/.style={/pgf/decoration/text align/align=left},
24 right/.style={/pgf/decoration/text align/align=right},
25 center/.style={/pgf/decoration/text align/align=center},
26 left indent/.initial=0pt,
27 right indent/.initial=0pt,
28 fit to path/.is if=pgf@lib@dec@text@fit,
29 fit to path stretching spaces/.default=true,
30 fit to path stretching spaces/.code={%
31 \csname pgf@lib@dec@text@fit#1\endcsname%
32 \csname pgf@lib@dec@text@stretch@spaces#1\endcsname%
33 }%
34 }%
35
36 \newif\ifpgf@lib@dec@text@scan@space
37 \newif\ifpgf@lib@dec@text@stretch@spaces
38 \newif\ifpgf@lib@dec@text@fit
39
40 \def\pgf@lib@decorationtextcolor{black}%
41
42 \def\pgf@lib@dec@text@left@text{left}%
43 \def\pgf@lib@dec@text@right@text{right}%
44 \def\pgf@lib@dec@text@right@center{center}%
45
46 % Decorates a path with a text. The path is removed during this
47 % process
48
49 \pgfdeclaredecoration{text along path}{initial}{%
50 \state{initial}[
51 width=+0pt, next state=left indent,
52 persistent precomputation={%
53 \edef\pgf@lib@dec@text@indent@left{\pgfkeysvalueof{/pgf/decoration/text align/left indent}}%
54 \edef\pgf@lib@dec@text@indent@right{\pgfkeysvalueof{/pgf/decoration/text align/right indent}}%
55 \edef\pgf@lib@dec@text@align{\pgfkeysvalueof{/pgf/decoration/text align/align}}%
56 \pgfdecoratedremainingdistance=\pgfdecoratedpathlength%
57 \advance\pgfdecoratedremainingdistance by-\pgf@lib@dec@text@indent@right\relax%
58 \edef\pgfdecoratedpathlength{\the\pgfdecoratedremainingdistance}%
59 \pgf@lib@dec@text@getwidth%
60 \pgf@x=\pgf@lib@dec@text@width\relax%
61 \pgf@y=\pgfdecoratedremainingdistance%
62 \ifpgf@lib@dec@text@fit%
63 \advance\pgf@y by-\pgf@lib@dec@text@indent@left\relax%
64 \advance\pgf@y by-\pgf@x%
65 \ifpgf@lib@dec@text@stretch@spaces%
66 \def\pgf@lib@dec@text@character@shift{0pt}%
67 \divide\pgf@y by\pgf@lib@dec@space@count\relax%
68 \edef\pgf@lib@dec@text@space@shift{\the\pgf@y}%
69 \else%
70 \c@pgf@counta=\pgf@lib@dec@character@count\relax%
71 \advance\c@pgf@counta by-1\relax%
72 \divide\pgf@y by\c@pgf@counta\relax%
73 \edef\pgf@lib@dec@text@character@shift{\the\pgf@y}%
74 \def\pgf@lib@dec@text@space@shift{0pt}%
75 \fi%
76 \ifdim\pgf@y<0pt\relax%
77 \pgf@lib@dec@text@fitfalse%
78 \pgf@lib@dec@text@stretch@spacesfalse%
79 \def\pgf@lib@dec@text@character@shift{0pt}%
80 \def\pgf@lib@dec@text@space@shift{0pt}%
81 \fi%
82 \else%
83 \def\pgf@lib@dec@text@character@shift{0pt}%
84 \def\pgf@lib@dec@text@space@shift{0pt}%
85 \ifx\pgf@lib@dec@text@align\pgf@lib@dec@text@left@text%
86 \else%
87 \ifx\pgf@lib@dec@text@align\pgf@lib@dec@text@right@text%
88 \advance\pgf@y by-\pgf@x%
89 \edef\pgf@lib@dec@text@indent@left{\the\pgf@y}%
90 \else%
91 \advance\pgf@y by-\pgf@x%
92 \advance\pgf@y by-\pgf@lib@dec@text@indent@left\relax%
93 \pgf@y=0.5\pgf@y%
94 \advance\pgf@y by\pgf@lib@dec@text@indent@left\relax%
95 \edef\pgf@lib@dec@text@indent@left{\the\pgf@y}%
96 \fi%
97 \fi%
98 \fi%
99 \let\pgfdecorationrestoftext=\pgfdecorationtext%
100 }]{}%
101 \state{left indent}[width=+\pgf@lib@dec@text@indent@left, next state=scan]{}%
102 %
103 \state{scan}[
104 width=+0pt,
105 next state=before typeset,
106 persistent precomputation={
107 \pgf@lib@dec@text@scanchar%
108 \ifvoid\pgf@lib@dec@text@box%
109 \setbox\pgf@lib@dec@text@box\hbox{}%
110 \wd\pgf@lib@dec@text@box16383pt\relax%
111 \fi%
112 }]{}%
113 %
114 \state{before typeset}[width=+.5\wd\pgf@lib@dec@text@box, next state=typeset]{}%
115 %
116 \state{typeset}[width=+0pt, next state=after typeset]
117 {%
118 \pgftransformxshift{+-.5\wd\pgf@lib@dec@text@box}%
119 \setbox\pgf@hbox\hbox{\copy\pgf@lib@dec@text@box}%
120 \pgfqboxsynced\pgf@hbox%
121 }%
122 \state{after typeset}[width=+.5\wd\pgf@lib@dec@text@box, next state=shift,
123 persistent precomputation={%
124 \ifpgf@lib@dec@text@fit%
125 \ifpgf@lib@dec@text@stretch@spaces%
126 \ifpgf@lib@dec@text@scan@space%
127 \let\pgf@lib@dec@text@shift=\pgf@lib@dec@text@space@shift%
128 \else%
129 \def\pgf@lib@dec@text@shift{0pt}%
130 \fi%
131 \else%
132 \let\pgf@lib@dec@text@shift=\pgf@lib@dec@text@character@shift%
133 \fi%
134 \else%
135 \def\pgf@lib@dec@text@shift{0pt}%
136 \fi%
137 }]{}%
138 \state{shift}[width=+\pgf@lib@dec@text@shift, next state=scan]{}%
139 \state{final}{}%
140 }%
141
142
143 % \pgfsetdecoratetextformatdelimiters
144 %
145 % Set the delimiters for formatting in the text decoration.
146 % NB: Catcodes for delimiters should be 11 or 12.
147 %
148 % Examples:
149 %
150 % \pgfsetdecoratetextformatdelimiters{|}{}% 2nd argument can be empty.
151 %
152 % \def\pgfdecoratetext{A big |\color{red}|red|| apple.}
153 %
154 % \pgfsetdecoratetextformatdelimiters{[}{]}
155 %
156 % \def\pgfdecoratetext{The [\it]very[+\color{green}]green[] sprouts.}
157 %
158 \def\pgfsetdecoratetextformatdelimiters#1#2{%
159 \def\pgf@lib@dec@text@formatchar{#1}%
160 \def\pgf@test{#2}%
161 \ifx\pgf@test\pgfutil@empty%
162 \def\pgf@lib@dec@text@collectformat##1#1{%
163 \pgf@lib@dec@text@@collectformat##1\pgf@stop}%
164 \else%
165 \def\pgf@lib@dec@text@collectformat##1#2{%
166 \pgf@lib@dec@text@@collectformat##1\pgf@stop}%
167 \fi%
168 }%
169
170 \pgfsetdecoratetextformatdelimiters{|}{}%
171
172 \newbox\pgf@lib@dec@text@box
173 \newif\ifpgf@lib@decorate@textmathmode
174
175 \let\pgfdecorationtext=\pgfutil@empty
176 \let\pgfdecorationrestoftext=\pgfutil@empty%
177 \let\pgf@lib@dec@text@format=\pgfutil@empty
178
179 \def\pgf@lib@dec@text@scanchar{%
180 \pgf@lib@dec@text@scan@spacefalse%
181 \ifx\pgfdecorationrestoftext\pgfutil@empty%
182 \let\pgf@lib@dec@text@char\pgfutil@empty%
183 \setbox\pgf@lib@dec@text@box\box\pgfutil@voidb@x%
184 \let\pgf@next\relax%
185 \else\ifx\pgfdecorationrestoftext\pgf@lib@dec@mathshift%
186 \ifpgf@lib@decorate@textmathmode%
187 \pgf@lib@decorate@textmathmodefalse%
188 \fi%
189 \let\pgf@lib@dec@text@char\pgfutil@empty%
190 \setbox\pgf@lib@dec@text@box\box\pgfutil@voidb@x%
191 \let\pgf@next\relax%
192 \else
193 \let\pgf@next\pgf@lib@dec@text@@scanchar%
194 \fi\fi%
195 \pgf@next}%
196
197 \def\pgf@lib@dec@text@@scanchar{%
198 \expandafter\pgf@lib@dec@text@@@scanchar\pgfdecorationrestoftext\pgf@stop}%
199
200 \def\pgf@lib@dec@text@@@scanchar{%
201 \futurelet\pgf@lib@dec@lettoken%
202 \pgf@lib@dec@text@@@@scanchar}%
203
204 \def\pgf@lib@dec@text@@@@scanchar{%
205 \ifx\pgf@lib@dec@lettoken\pgfutil@sptoken%
206 \let\pgf@next\pgf@lib@dec@text@insertspace%
207 \else%
208 \let\pgf@next\pgf@lib@dec@text@@@@@scanchar%
209 \fi%
210 \pgf@next}%
211
212 \def\pgf@lib@dec@text@@@@@scanchar{%
213 \pgfutil@ifnextchar\bgroup{\pgf@lib@dec@text@collectgroup}%
214 {\pgf@lib@dec@text@@@@@@scanchar}}%
215
216 \def\pgf@lib@dec@text@collectgroup#1{%
217 \def\pgf@lib@dec@text@char{#1}%
218 \pgf@lib@dec@text@collectrestoftext}%
219
220 \def\pgf@lib@dec@text@@@@@@scanchar#1{%
221 \ifx#1\pgf@stop%
222 \pgf@lib@dec@text@box\box\pgfutil@voidb@x%
223 \let\pgf@next\pgf@lib@dec@text@endoftext%
224 \else%
225 \def\pgf@lib@dec@text@char{#1}%
226 \ifx#1\space%
227 \let\pgf@next\pgf@lib@dec@text@collectrestoftext%
228 \else%
229 \ifx#1\ %
230 \let\pgf@next\pgf@lib@dec@text@collectrestoftext%
231 \else%
232 \ifx\pgf@lib@dec@text@char\pgf@lib@dec@text@formatchar%
233 \let\pgf@next\pgf@lib@dec@text@collectformat%
234 \else%
235 \expandafter\ifcat\noexpand#1\relax%
236 \let\pgf@next\pgf@lib@dec@text@expandcs%
237 \else%
238 \ifnum\catcode`#1=3\relax%
239 \ifpgf@lib@decorate@textmathmode%
240 \pgf@lib@decorate@textmathmodefalse%
241 \else%
242 \pgf@lib@decorate@textmathmodetrue%
243 \fi%
244 \let\pgf@next\pgf@lib@dec@text@@@scanchar%
245 \else%
246 \let\pgf@next\pgf@lib@dec@text@collectrestoftext%
247 \fi%
248 \fi%
249 \fi%
250 \fi%
251 \fi%
252 \fi%
253 \pgf@next%
254 }%
255
256 \def\pgf@lib@dec@text@@collectformat{%
257 \pgfutil@ifnextchar+{\pgf@lib@dec@text@addtoformat}{\pgf@lib@dec@text@setformat}}%
258
259 \def\pgf@lib@dec@text@setformat#1\pgf@stop{%
260 \def\pgf@lib@dec@text@format{#1}%
261 \pgf@lib@dec@text@@@scanchar%
262 }%
263
264 \def\pgf@lib@dec@text@addtoformat+#1\pgf@stop{%
265 \expandafter\def\expandafter\pgf@lib@dec@text@format\expandafter{\pgf@lib@dec@text@format#1}%
266 \pgf@lib@dec@text@@@scanchar%
267 }%
268
269 \def\pgf@lib@dec@text@insertspace{%
270 \pgf@lib@dec@text@scan@spacetrue%
271 \pgfutil@ifnextchar\bgroup{\pgf@lib@dec@text@@insertspacegrp}%
272 {\pgf@lib@dec@text@@insertspace}}%
273
274 \def\pgf@lib@dec@text@@insertspacegrp#1{%
275 \pgf@lib@dec@text@@@@@@scanchar\space{#1}}%
276
277 \def\pgf@lib@dec@text@@insertspace#1{%
278 \pgf@lib@dec@text@@@@@@scanchar\space#1}%
279
280 \def\pgf@lib@dec@text@expandcs{%
281 \expandafter\expandafter\expandafter\pgf@lib@dec@text@@@@@scanchar%
282 \pgf@lib@dec@text@char}%
283
284 \def\pgf@lib@dec@text@endoftext{%
285 \let\pgfdecorationrestoftext\pgfutil@empty%
286 \let\pgf@lib@dec@text@char\pgfutil@empty%
287 }%
288 \def\pgf@lib@dec@text@collectrestoftext{%
289 \pgf@lib@dec@text@dobox%
290 \futurelet\pgf@lib@dec@text@lettoken%
291 \pgf@lib@dec@text@@collectrestoftext}%
292
293 \def\pgf@lib@dec@text@@collectrestoftext{%
294 \ifx\bgroup\pgf@lib@dec@text@lettoken%
295 \let\pgf@next\pgf@lib@dec@text@@@collectrestoftextgrp%
296 \else%
297 \let\pgf@next\pgf@lib@dec@text@@@collectrestoftext%
298 \fi%
299 \pgf@next}%
300
301 \def\pgf@lib@dec@text@@@collectrestoftextgrp#1#2\pgf@stop{\def\pgfdecorationrestoftext{{#1}#2}%
302 }%
303
304 \def\pgf@lib@dec@text@@@collectrestoftext#1\pgf@stop{\def\pgfdecorationrestoftext{#1}}%
305
306 {%
307 \catcode`\$3 %
308 \gdef\pgf@lib@dec@mathshift{$}%
309 \catcode`\$9 $% For editors with annoying syntax highlighting.
310 }%
311
312 \def\pgf@lib@dec@text@dobox{%
313 \setbox\pgf@lib@dec@text@box\hbox{%
314 \pgfinterruptpicture%
315 \begingroup%
316 \pgfsetcolor{\pgf@lib@decorationtextcolor}%
317 \ifpgf@lib@decorate@textmathmode\pgf@lib@dec@mathshift\fi%
318 \pgf@lib@dec@text@format\relax%
319 \pgf@lib@dec@text@char%
320 \ifpgf@lib@decorate@textmathmode\pgf@lib@dec@mathshift\fi%
321 \endgroup%
322 \endpgfinterruptpicture%
323 }%
324 }%
325
326 \def\pgf@lib@dec@text@getwidth{%
327 \begingroup%
328 \let\c@pgf@lib@dec@characters=\c@pgf@counta%
329 \let\c@pgf@lib@dec@spaces=\c@pgf@countb%
330 \let\pgfdecorationrestoftext=\pgfdecorationtext%
331 \c@pgf@lib@dec@spaces=0\relax%
332 \c@pgf@lib@dec@characters=0\relax%
333 \pgfmathloop%
334 \pgf@lib@dec@text@scanchar%
335 \ifvoid\pgf@lib@dec@text@box%
336 \else%
337 \setbox\pgf@hbox=\hbox{{%
338 \unhbox\pgf@hbox%
339 \unhbox\pgf@lib@dec@text@box%
340 }}%
341 \advance\c@pgf@lib@dec@characters by1\relax%
342 \ifpgf@lib@dec@text@scan@space%
343 \advance\c@pgf@lib@dec@spaces by1\relax%
344 \fi%
345 \repeatpgfmathloop%
346 \global\pgf@x=\the\wd\pgf@hbox%
347 \xdef\pgf@lib@dec@temp{\the\c@pgf@lib@dec@characters}%
348 \xdef\pgf@lib@dec@@temp{\the\c@pgf@lib@dec@spaces}%
349 \endgroup%
350 \edef\pgf@lib@dec@text@width{\the\pgf@x}%
351 \let\pgf@lib@dec@character@count=\pgf@lib@dec@temp%
352 \let\pgf@lib@dec@space@count=\pgf@lib@dec@@temp%
353 }%
354
355
356
357 \endinput