Debian Codegraph @

texmf-dist/source/latex/l3experimental/l3draw/l3draw-transforms.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3draw-transforms.dtx
4 %
5 % Copyright (C) 2018-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % http://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3experimental bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \RequirePackage{expl3}
27 \documentclass[full]{l3doc}
28 \begin{document}
29 \DocInput{\jobname.dtx}
30 \end{document}
31 %</driver>
32 % \fi
33 %
34 % \title{^^A
35 % The \pkg{l3draw-transforms} package\\ Transformations^^A
36 % }
37 %
38 % \author{^^A
39 % The \LaTeX{} Project\thanks
40 % {^^A
41 % E-mail:
42 % \href{mailto:[email protected]}
43 % {[email protected]}^^A
44 % }^^A
45 % }
46 %
47 % \date{Released 2026-05-26}
48 %
49 % \maketitle
50 %
51 % \begin{implementation}
52 %
53 % \section{\pkg{l3draw-transforms} implementation}
54 %
55 % \begin{macrocode}
56 %<*package>
57 % \end{macrocode}
58 %
59 % \begin{macrocode}
60 %<@@=draw>
61 % \end{macrocode}
62 %
63 % This sub-module covers more-or-less the same ideas as
64 % \texttt{pgfcoretransformations.code.tex}. At present, equivalents of the
65 % following are currently absent:
66 % \begin{itemize}
67 % \item \cs{pgfgettransform}, \cs{pgfgettransformentries}: Awaiting use cases.
68 % \item \cs{pgftransformlineattime}, \cs{pgftransformarcaxesattime},
69 % \cs{pgftransformcurveattime}: Need to look at the use cases for
70 % these to fully understand them.
71 % \item \cs{pgftransformarrow}: Likely to be done when other arrow functions
72 % are added.
73 % \item \cs{pgftransformationadjustments}: Used mainly by CircuiTi\textit{k}Z
74 % although also for shapes, likely needs more use cases before addressing.
75 % \item \cs{pgflowlevelsynccm}, \cs{pgflowlevel}: Likely to be added when
76 % use cases are encountered in other parts of the code.
77 % \item \cs{pgfviewboxscope}: Seems very specialized, need to understand the
78 % requirements here.
79 % \end{itemize}
80 %
81 % \begin{variable}{\l_@@_matrix_active_bool}
82 % An internal flag to avoid redundant calculations.
83 % \begin{macrocode}
84 \bool_new:N \l_@@_matrix_active_bool
85 % \end{macrocode}
86 % \end{variable}
87 %
88 % \begin{variable}
89 % {
90 % \l_@@_matrix_a_fp,
91 % \l_@@_matrix_b_fp,
92 % \l_@@_matrix_c_fp,
93 % \l_@@_matrix_a_fp,
94 % \l_@@_xshift_dim,
95 % \l_@@_yshift_dim
96 % }
97 % The active matrix and shifts.
98 % \begin{macrocode}
99 \fp_new:N \l_@@_matrix_a_fp
100 \fp_new:N \l_@@_matrix_b_fp
101 \fp_new:N \l_@@_matrix_c_fp
102 \fp_new:N \l_@@_matrix_d_fp
103 \dim_new:N \l_@@_xshift_dim
104 \dim_new:N \l_@@_yshift_dim
105 % \end{macrocode}
106 % \end{variable}
107 %
108 % \begin{macro}{\draw_transform_matrix_reset:, \draw_transform_shift_reset:}
109 % Fast resetting.
110 % \begin{macrocode}
111 \cs_new_protected:Npn \draw_transform_matrix_reset:
112 {
113 \fp_set:Nn \l_@@_matrix_a_fp { 1 }
114 \fp_zero:N \l_@@_matrix_b_fp
115 \fp_zero:N \l_@@_matrix_c_fp
116 \fp_set:Nn \l_@@_matrix_d_fp { 1 }
117 \bool_set_false:N \l_@@_matrix_active_bool
118 }
119 \cs_new_protected:Npn \draw_transform_shift_reset:
120 {
121 \dim_zero:N \l_@@_xshift_dim
122 \dim_zero:N \l_@@_yshift_dim
123 }
124 \draw_transform_matrix_reset:
125 \draw_transform_shift_reset:
126 % \end{macrocode}
127 % \end{macro}
128 %
129 % \begin{macro}{\draw_transform_matrix_absolute:nnnn}
130 % \begin{macro}{\draw_transform_shift_absolute:n}
131 % \begin{macro}{\@@_transform_shift_absolute:nn}
132 % Setting the transform matrix is straight-forward, with just a bit
133 % of expansion to sort out. With the mechanism active, the identity
134 % matrix is set.
135 % \begin{macrocode}
136 \cs_new_protected:Npn \draw_transform_matrix_absolute:nnnn #1#2#3#4
137 {
138 \fp_set:Nn \l_@@_matrix_a_fp {#1}
139 \fp_set:Nn \l_@@_matrix_b_fp {#2}
140 \fp_set:Nn \l_@@_matrix_c_fp {#3}
141 \fp_set:Nn \l_@@_matrix_d_fp {#4}
142 \bool_lazy_all:nTF
143 {
144 { \fp_compare_p:nNn \l_@@_matrix_a_fp = \c_one_fp }
145 { \fp_compare_p:nNn \l_@@_matrix_b_fp = \c_zero_fp }
146 { \fp_compare_p:nNn \l_@@_matrix_c_fp = \c_zero_fp }
147 { \fp_compare_p:nNn \l_@@_matrix_d_fp = \c_one_fp }
148 }
149 { \bool_set_false:N \l_@@_matrix_active_bool }
150 { \bool_set_true:N \l_@@_matrix_active_bool }
151 }
152 \cs_new_protected:Npn \draw_transform_shift_absolute:n #1
153 {
154 \@@_point_process:nn
155 { \@@_transform_shift_absolute:nn } { \draw_point:n {#1} }
156 }
157 \cs_new_protected:Npn \@@_transform_shift_absolute:nn #1#2
158 {
159 \dim_set:Nn \l_@@_xshift_dim {#1}
160 \dim_set:Nn \l_@@_yshift_dim {#2}
161 }
162 % \end{macrocode}
163 % \end{macro}
164 % \end{macro}
165 % \end{macro}
166 %
167 % \begin{macro}{\draw_transform_matrix:nnnn}
168 % \begin{macro}{\@@_transform:nnnn}
169 % \begin{macro}{\draw_transform_shift:n}
170 % \begin{macro}{\@@_transform_shift:nn}
171 % Much the same story for adding to an existing matrix, with a bit of
172 % pre-expansion so that the calculation uses \enquote{frozen} values.
173 % \begin{macrocode}
174 \cs_new_protected:Npn \draw_transform_matrix:nnnn #1#2#3#4
175 {
176 \use:e
177 {
178 \@@_transform:nnnn
179 { \fp_eval:n {#1} }
180 { \fp_eval:n {#2} }
181 { \fp_eval:n {#3} }
182 { \fp_eval:n {#4} }
183 }
184 }
185 \cs_new_protected:Npn \@@_transform:nnnn #1#2#3#4
186 {
187 \use:e
188 {
189 \draw_transform_matrix_absolute:nnnn
190 { #1 * \l_@@_matrix_a_fp + #2 * \l_@@_matrix_c_fp }
191 { #1 * \l_@@_matrix_b_fp + #2 * \l_@@_matrix_d_fp }
192 { #3 * \l_@@_matrix_a_fp + #4 * \l_@@_matrix_c_fp }
193 { #3 * \l_@@_matrix_b_fp + #4 * \l_@@_matrix_d_fp }
194 }
195 }
196 \cs_new_protected:Npn \draw_transform_shift:n #1
197 {
198 \@@_point_process:nn
199 { \@@_transform_shift:nn } { \draw_point:n {#1} }
200 }
201 \cs_new_protected:Npn \@@_transform_shift:nn #1#2
202 {
203 \@@_transform_shift:nnnn
204 \l_@@_xshift_dim
205 \l_@@_yshift_dim
206 {#1} {#2}
207 }
208 % \end{macrocode}
209 % \end{macro}
210 % \end{macro}
211 % \end{macro}
212 % \end{macro}
213 %
214 % \begin{macro}{\@@_transform_shift:nnnn}
215 % Apply the current transformation matrix to the shift, then store
216 % the resulting values: we may or may not have a non-zero starting
217 % point here.
218 % \begin{macrocode}
219 \cs_new_protected:Npn \@@_transform_shift:nnnn #1#2#3#4
220 {
221 \dim_set:Nn \l_@@_xshift_dim
222 {
223 \fp_to_dim:n
224 {
225 #1 +
226 ( #3 * \l_@@_matrix_a_fp + #4 * \l_@@_matrix_c_fp )
227 }
228 }
229 \dim_set:Nn \l_@@_yshift_dim
230 {
231 \fp_to_dim:n
232 {
233 #2 +
234 ( #3 * \l_@@_matrix_b_fp + #4 * \l_@@_matrix_d_fp )
235 }
236 }
237 }
238 % \end{macrocode}
239 % \end{macro}
240 %
241 % \begin{macro}{\draw_transform_matrix_invert:}
242 % \begin{macro}{\@@_transform_invert:n, \@@_transform_invert:e}
243 % \begin{macro}{\draw_transform_shift_invert:}
244 % \begin{macro}{\@@_transform_shift_invert:n, \@@_transform_shift_invert:e}
245 % Standard mathematics: calculate the inverse affine transformation.
246 % For the matrix, the inverse is calculated and applied. For the shift,
247 % the inverse vector is evaluated as $-M^{-1} \cdot S$. We use
248 % \texttt{e}-type expansion to snapshot the current floating-point values
249 % of the matrix and shift prior to assignment.
250 % \begin{macrocode}
251 \cs_new_protected:Npn \draw_transform_matrix_invert:
252 {
253 \bool_if:NT \l_@@_matrix_active_bool
254 {
255 \@@_transform_invert:e
256 {
257 \fp_eval:n
258 {
259 1 /
260 (
261 \l_@@_matrix_a_fp * \l_@@_matrix_d_fp
262 - \l_@@_matrix_b_fp * \l_@@_matrix_c_fp
263 )
264 }
265 }
266 }
267 }
268 \cs_new_protected:Npn \@@_transform_invert:n #1
269 {
270 \use:e
271 {
272 \draw_transform_matrix_absolute:nnnn
273 { \l_@@_matrix_d_fp * #1 }
274 { -\l_@@_matrix_b_fp * #1 }
275 { -\l_@@_matrix_c_fp * #1 }
276 { \l_@@_matrix_a_fp * #1 }
277 }
278 }
279 \cs_generate_variant:Nn \@@_transform_invert:n { e }
280 \cs_new_protected:Npn \draw_transform_shift_invert:
281 {
282 \bool_if:NTF \l_@@_matrix_active_bool
283 {
284 \@@_transform_shift_invert:e
285 {
286 \fp_eval:n
287 {
288 1 /
289 (
290 \l_@@_matrix_a_fp * \l_@@_matrix_d_fp
291 - \l_@@_matrix_b_fp * \l_@@_matrix_c_fp
292 )
293 }
294 }
295 }
296 {
297 \dim_set:Nn \l_@@_xshift_dim { -\l_@@_xshift_dim }
298 \dim_set:Nn \l_@@_yshift_dim { -\l_@@_yshift_dim }
299 }
300 }
301 \cs_new_protected:Npn \@@_transform_shift_invert:n #1
302 {
303 \use:e
304 {
305 \@@_transform_shift:nn
306 {
307 \fp_to_dim:n
308 {
309 (
310 \l_@@_matrix_c_fp * \l_@@_yshift_dim
311 - \l_@@_matrix_d_fp * \l_@@_xshift_dim
312 ) * #1
313 }
314 }
315 {
316 \fp_to_dim:n
317 {
318 (
319 \l_@@_matrix_b_fp * \l_@@_xshift_dim
320 - \l_@@_matrix_a_fp * \l_@@_yshift_dim
321 ) * #1
322 }
323 }
324 }
325 }
326 \cs_generate_variant:Nn \@@_transform_shift_invert:n { e }
327 % \end{macrocode}
328 % \end{macro}
329 % \end{macro}
330 % \end{macro}
331 % \end{macro}
332 %
333 % \begin{macro}{\draw_transform_triangle:nnn}
334 % Simple maths to move the canvas origin to |#1| and the two axes to
335 % |#2| and |#3|.
336 % \begin{macrocode}
337 \cs_new_protected:Npn \draw_transform_triangle:nnn #1#2#3
338 {
339 \@@_point_process:nnnn
340 { \@@_transform_triangle:nnnnnn }
341 { \draw_point:n {#1} }
342 { \draw_point:n {#2} }
343 { \draw_point:n {#3} }
344 }
345 \cs_new_protected:Npn \@@_transform_triangle:nnnnnn #1#2#3#4#5#6
346 {
347 \use:e
348 {
349 \draw_transform_matrix:nnnn
350 { #3 - #1 }
351 { #4 - #2 }
352 { #5 - #1 }
353 { #6 - #2 }
354 \draw_transform_shift:n { #1 , #2 }
355 }
356 }
357 % \end{macrocode}
358 % \end{macro}
359 %
360 % \begin{macro}
361 % {\draw_transform_scale:n, \draw_transform_xscale:n, \draw_transform_yscale:n}
362 % \begin{macro}
363 % {\draw_transform_xshift:n, \draw_transform_yshift:n}
364 % \begin{macro}
365 % {\draw_transform_xslant:n, \draw_transform_yslant:n}
366 % Lots of shortcuts.
367 % \begin{macrocode}
368 \cs_new_protected:Npn \draw_transform_scale:n #1
369 { \draw_transform_matrix:nnnn { #1 } { 0 } { 0 } { #1 } }
370 \cs_new_protected:Npn \draw_transform_xscale:n #1
371 { \draw_transform_matrix:nnnn { #1 } { 0 } { 0 } { 1 } }
372 \cs_new_protected:Npn \draw_transform_yscale:n #1
373 { \draw_transform_matrix:nnnn { 1 } { 0 } { 0 } { #1 } }
374 \cs_new_protected:Npn \draw_transform_xshift:n #1
375 { \draw_transform_shift:n { #1 , 0pt } }
376 \cs_new_protected:Npn \draw_transform_yshift:n #1
377 { \draw_transform_shift:n { 0pt , #1 } }
378 \cs_new_protected:Npn \draw_transform_xslant:n #1
379 { \draw_transform_matrix:nnnn { 1 } { 0 } { #1 } { 1 } }
380 \cs_new_protected:Npn \draw_transform_yslant:n #1
381 { \draw_transform_matrix:nnnn { 1 } { #1 } { 0 } { 1 } }
382 % \end{macrocode}
383 % \end{macro}
384 % \end{macro}
385 % \end{macro}
386 %
387 % \begin{macro}{\draw_transform_rotate:n}
388 % \begin{macro}
389 % {
390 % \@@_transform_rotate:n, \@@_transform_rotate:e,
391 % \@@_transform_rotate:nn, \@@_transform_rotate:ee
392 % }
393 % Slightly more involved: evaluate the angle only once, and the sine and
394 % cosine only once.
395 % \begin{macrocode}
396 \cs_new_protected:Npn \draw_transform_rotate:n #1
397 { \@@_transform_rotate:e { \fp_eval:n {#1} } }
398 \cs_new_protected:Npn \@@_transform_rotate:n #1
399 {
400 \@@_transform_rotate:ee
401 { \fp_eval:n { cosd(#1) } }
402 { \fp_eval:n { sind(#1) } }
403 }
404 \cs_generate_variant:Nn \@@_transform_rotate:n { e }
405 \cs_new_protected:Npn \@@_transform_rotate:nn #1#2
406 { \draw_transform_matrix:nnnn {#1} {#2} { -#2 } { #1 } }
407 \cs_generate_variant:Nn \@@_transform_rotate:nn { ee }
408 % \end{macrocode}
409 % \end{macro}
410 % \end{macro}
411 %
412 % \begin{macrocode}
413 %</package>
414 % \end{macrocode}
415 %
416 % \end{implementation}
417 %
418 % \PrintIndex