Debian Codegraph @

texmf-dist/source/latex/l3backend/l3backend-draw.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3backend-draw.dtx
4 %
5 % Copyright (C) 2019-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 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3backend 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 \documentclass[full,kernel]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 % \title{^^A
34 % The \pkg{l3backend-draw} package\\ Backend drawing support^^A
35 % }
36 %
37 % \author{^^A
38 % The \LaTeX{} Project\thanks
39 % {^^A
40 % E-mail:
41 % \href{mailto:[email protected]}
42 % {[email protected]}^^A
43 % }^^A
44 % }
45 %
46 % \date{Released 2026-02-18}
47 %
48 % \maketitle
49 %
50 % \begin{documentation}
51 %
52 % \end{documentation}
53 %
54 % \begin{implementation}
55 %
56 % \section{\pkg{l3backend-draw} implementation}
57 %
58 % \begin{macrocode}
59 %<*package>
60 %<@@=draw>
61 % \end{macrocode}
62 %
63 % \subsection{\texttt{dvips} backend}
64 %
65 % \begin{macrocode}
66 %<*dvips>
67 % \end{macrocode}
68 %
69 % \begin{macro}{\@@_backend_literal:n, \@@_backend_literal:e}
70 % The same as literal PostScript: same arguments about positioning apply
71 % here.
72 % \begin{macrocode}
73 \cs_new_eq:NN \@@_backend_literal:n \__kernel_backend_literal_postscript:n
74 \cs_generate_variant:Nn \@@_backend_literal:n { e }
75 % \end{macrocode}
76 % \end{macro}
77 %
78 % \begin{macro}{\@@_backend_begin:, \@@_backend_end:}
79 % The |ps::[begin]| special here deals with positioning but allows us to
80 % continue on to a matching |ps::[end]|: contrast with |ps:|, which positions
81 % but where we can't split material between separate calls. The
82 % |@beginspecial|/|@endspecial| pair are from |special.pro| and correct the
83 % scale and $y$-axis direction.
84 % As for \pkg{pgf}, we need to save the current point as this is
85 % required for box placement. (Note that
86 % |@beginspecial|/|@endspecial| forms a backend scope.)
87 % \begin{macrocode}
88 \cs_new_protected:Npn \@@_backend_begin:
89 {
90 \@@_backend_literal:n { [begin] }
91 \@@_backend_literal:n { /draw.x~currentpoint~/draw.y~exch~def~def }
92 \@@_backend_literal:n { @beginspecial }
93 }
94 \cs_new_protected:Npn \@@_backend_end:
95 {
96 \@@_backend_literal:n { @endspecial }
97 \@@_backend_literal:n { [end] }
98 }
99 % \end{macrocode}
100 % \end{macro}
101 %
102 % \begin{macro}{\@@_backend_scope_begin:, \@@_backend_scope_end:}
103 % Scope here may need to contain saved definitions, so the entire memory
104 % rather than just the graphic state has to be sent to the stack.
105 % \begin{macrocode}
106 \cs_new_protected:Npn \@@_backend_scope_begin:
107 { \@@_backend_literal:n { save } }
108 \cs_new_protected:Npn \@@_backend_scope_end:
109 { \@@_backend_literal:n { restore } }
110 % \end{macrocode}
111 % \end{macro}
112 %
113 % \begin{macro}{\@@_backend_moveto:nn, \@@_backend_lineto:nn}
114 % \begin{macro}{\@@_backend_rectangle:nnnn}
115 % \begin{macro}{\@@_backend_curveto:nnnnnn}
116 % Path creation operations mainly resolve directly to PostScript primitive
117 % steps, with only the need to convert to \texttt{bp}. Notice that
118 % \texttt{e}-type expansion is included here to ensure that any variable
119 % values are forced to literals before any possible caching. There is
120 % no native rectangular path command (without also clipping, filling or
121 % stroking), so that task is done using a small amount of PostScript.
122 % \begin{macrocode}
123 \cs_new_protected:Npn \@@_backend_moveto:nn #1#2
124 {
125 \@@_backend_literal:e
126 {
127 \dim_to_decimal_in_bp:n {#1} ~
128 \dim_to_decimal_in_bp:n {#2} ~ moveto
129 }
130 }
131 \cs_new_protected:Npn \@@_backend_lineto:nn #1#2
132 {
133 \@@_backend_literal:e
134 {
135 \dim_to_decimal_in_bp:n {#1} ~
136 \dim_to_decimal_in_bp:n {#2} ~ lineto
137 }
138 }
139 \cs_new_protected:Npn \@@_backend_rectangle:nnnn #1#2#3#4
140 {
141 \@@_backend_literal:e
142 {
143 \dim_to_decimal_in_bp:n {#4} ~ \dim_to_decimal_in_bp:n {#3} ~
144 \dim_to_decimal_in_bp:n {#1} ~ \dim_to_decimal_in_bp:n {#2} ~
145 moveto~dup~0~rlineto~exch~0~exch~rlineto~neg~0~rlineto~closepath
146 }
147 }
148 \cs_new_protected:Npn \@@_backend_curveto:nnnnnn #1#2#3#4#5#6
149 {
150 \@@_backend_literal:e
151 {
152 \dim_to_decimal_in_bp:n {#1} ~ \dim_to_decimal_in_bp:n {#2} ~
153 \dim_to_decimal_in_bp:n {#3} ~ \dim_to_decimal_in_bp:n {#4} ~
154 \dim_to_decimal_in_bp:n {#5} ~ \dim_to_decimal_in_bp:n {#6} ~
155 curveto
156 }
157 }
158 % \end{macrocode}
159 % \end{macro}
160 % \end{macro}
161 % \end{macro}
162 %
163 % \begin{macro}{\@@_backend_evenodd_rule:, \@@_backend_nonzero_rule:}
164 % \begin{variable}{\g_@@_draw_eor_bool}
165 % The even-odd rule here can be implemented as a simply switch.
166 % \begin{macrocode}
167 \cs_new_protected:Npn \@@_backend_evenodd_rule:
168 { \bool_gset_true:N \g_@@_draw_eor_bool }
169 \cs_new_protected:Npn \@@_backend_nonzero_rule:
170 { \bool_gset_false:N \g_@@_draw_eor_bool }
171 \bool_new:N \g_@@_draw_eor_bool
172 % \end{macrocode}
173 % \end{variable}
174 % \end{macro}
175 %
176 % \begin{macro}
177 % {
178 % \@@_backend_closepath: ,
179 % \@@_backend_stroke: ,
180 % \@@_backend_closestroke: ,
181 % \@@_backend_fill: ,
182 % \@@_backend_fillstroke: ,
183 % \@@_backend_clip: ,
184 % \@@_backend_discardpath:
185 % }
186 % \begin{variable}{\g_@@_draw_clip_bool}
187 % Unlike PDF, PostScript doesn't track separate colors for strokes and other
188 % elements. It is also desirable to have the |clip| keyword after a stroke or
189 % fill. To achieve those outcomes, there is some work to do. For color, the
190 % stoke color is simple but the fill one has to be inserted by hand. For
191 % clipping, the required ordering is achieved using a \TeX{} switch. All of
192 % the operations end with a new path instruction as they do not terminate
193 % (again in contrast to PDF).
194 % \begin{macrocode}
195 \cs_new_protected:Npn \@@_backend_closepath:
196 { \@@_backend_literal:n { closepath } }
197 \cs_new_protected:Npn \@@_backend_stroke:
198 {
199 \@@_backend_literal:n { gsave }
200 \@@_backend_literal:n { color.sc }
201 \@@_backend_literal:n { stroke }
202 \@@_backend_literal:n { grestore }
203 \bool_if:NT \g_@@_draw_clip_bool
204 {
205 \@@_backend_literal:e
206 {
207 \bool_if:NT \g_@@_draw_eor_bool { eo }
208 clip
209 }
210 }
211 \@@_backend_literal:n { newpath }
212 \bool_gset_false:N \g_@@_draw_clip_bool
213 }
214 \cs_new_protected:Npn \@@_backend_closestroke:
215 {
216 \@@_backend_closepath:
217 \@@_backend_stroke:
218 }
219 \cs_new_protected:Npn \@@_backend_fill:
220 {
221 \@@_backend_literal:e
222 {
223 \bool_if:NT \g_@@_draw_eor_bool { eo }
224 fill
225 }
226 \bool_if:NT \g_@@_draw_clip_bool
227 {
228 \@@_backend_literal:e
229 {
230 \bool_if:NT \g_@@_draw_eor_bool { eo }
231 clip
232 }
233 }
234 \@@_backend_literal:n { newpath }
235 \bool_gset_false:N \g_@@_draw_clip_bool
236 }
237 \cs_new_protected:Npn \@@_backend_fillstroke:
238 {
239 \@@_backend_literal:e
240 {
241 \bool_if:NT \g_@@_draw_eor_bool { eo }
242 fill
243 }
244 \@@_backend_literal:n { gsave }
245 \@@_backend_literal:n { color.sc }
246 \@@_backend_literal:n { stroke }
247 \@@_backend_literal:n { grestore }
248 \bool_if:NT \g_@@_draw_clip_bool
249 {
250 \@@_backend_literal:e
251 {
252 \bool_if:NT \g_@@_draw_eor_bool { eo }
253 clip
254 }
255 }
256 \@@_backend_literal:n { newpath }
257 \bool_gset_false:N \g_@@_draw_clip_bool
258 }
259 \cs_new_protected:Npn \@@_backend_clip:
260 { \bool_gset_true:N \g_@@_draw_clip_bool }
261 \bool_new:N \g_@@_draw_clip_bool
262 \cs_new_protected:Npn \@@_backend_discardpath:
263 {
264 \bool_if:NT \g_@@_draw_clip_bool
265 {
266 \@@_backend_literal:e
267 {
268 \bool_if:NT \g_@@_draw_eor_bool { eo }
269 clip
270 }
271 }
272 \@@_backend_literal:n { newpath }
273 \bool_gset_false:N \g_@@_draw_clip_bool
274 }
275 % \end{macrocode}
276 % \end{variable}
277 % \end{macro}
278 %
279 % \begin{macro}{\@@_backend_dash_pattern:nn}
280 % \begin{macro}{\@@_backend_dash:n}
281 % \begin{macro}{\@@_backend_linewidth:n}
282 % \begin{macro}{\@@_backend_miterlimit:n}
283 % \begin{macro}
284 % {
285 % \@@_backend_cap_butt:, \@@_backend_cap_round:, \@@_backend_cap_rectangle:,
286 % \@@_backend_join_miter:, \@@_backend_join_round:, \@@_backend_join_bevel:
287 % }
288 % Converting paths to output is again a case of mapping directly to
289 % PostScript operations.
290 % \begin{macrocode}
291 \cs_new_protected:Npn \@@_backend_dash_pattern:nn #1#2
292 {
293 \@@_backend_literal:e
294 {
295 [
296 \exp_args:Nf \use:n
297 { \clist_map_function:nN {#1} \@@_backend_dash:n }
298 ] ~
299 \dim_to_decimal_in_bp:n {#2} ~ setdash
300 }
301 }
302 \cs_new:Npn \@@_backend_dash:n #1
303 { ~ \dim_to_decimal_in_bp:n {#1} }
304 \cs_new_protected:Npn \@@_backend_linewidth:n #1
305 {
306 \@@_backend_literal:e
307 { \dim_to_decimal_in_bp:n {#1} ~ setlinewidth }
308 }
309 \cs_new_protected:Npn \@@_backend_miterlimit:n #1
310 { \@@_backend_literal:n { #1 ~ setmiterlimit } }
311 \cs_new_protected:Npn \@@_backend_cap_butt:
312 { \@@_backend_literal:n { 0 ~ setlinecap } }
313 \cs_new_protected:Npn \@@_backend_cap_round:
314 { \@@_backend_literal:n { 1 ~ setlinecap } }
315 \cs_new_protected:Npn \@@_backend_cap_rectangle:
316 { \@@_backend_literal:n { 2 ~ setlinecap } }
317 \cs_new_protected:Npn \@@_backend_join_miter:
318 { \@@_backend_literal:n { 0 ~ setlinejoin } }
319 \cs_new_protected:Npn \@@_backend_join_round:
320 { \@@_backend_literal:n { 1 ~ setlinejoin } }
321 \cs_new_protected:Npn \@@_backend_join_bevel:
322 { \@@_backend_literal:n { 2 ~ setlinejoin } }
323 % \end{macrocode}
324 % \end{macro}
325 % \end{macro}
326 % \end{macro}
327 % \end{macro}
328 % \end{macro}
329 %
330 % \begin{macro}{\@@_backend_transform:nnnn}
331 % \begin{macro}{\@@_backend_shift:nn}
332 % In \texttt{dvips}, keeping the transformations in line with the engine
333 % is unfortunately not possible for scaling and rotations: even if we
334 % decompose the matrix into those operations, there is still no backend
335 % tracking (\emph{cf.}~\texttt{dvipdfmx}/\XeTeX{}). Thus we take the shortest
336 % path available and simply dump the matrix as given.
337 % \begin{macrocode}
338 \cs_new_protected:Npn \@@_backend_transform:nnnn #1#2#3#4
339 {
340 \@@_backend_literal:n
341 { [ #1 ~ #2 ~ #3 ~ #4 ~ 0 ~ 0 ] ~ concat }
342 }
343 \cs_new_protected:Npn \@@_backend_shift:nn #1#2
344 {
345 \@@_backend_literal:n
346 { [ 1 ~ 0 ~ 0 ~ 1 ~ #1 ~ #2 ] ~ concat }
347 }
348 % \end{macrocode}
349 % \end{macro}
350 % \end{macro}
351 %
352 % \begin{macro}{\@@_backend_box_use:Nnnnn}
353 % Inside a picture |@beginspecial|/|@endspecial| are active, which is
354 % normally a good thing but means that the position and scaling would be off
355 % if the box was inserted directly. To deal with that, there are a number of
356 % possible approaches. A previous implementation suggested by Tom Rokici
357 % used |@endspecial|/|@beginspecial|. This avoids needing internals of
358 % \texttt{dvips}, but fails if there the box is used inside a scope
359 % (see \url{https://github.com/latex3/latex3/issues/1504}). Instead,
360 % we use the same method as \pkg{pgf}, which means tracking the position
361 % at the PostScript level. Also note that using |@endspecial| would
362 % close the scope it creates, meaning that after a box insertion, any
363 % local changes would be lost. Keeping \texttt{dvips} on track is
364 % non-trivial, hence the |[begin]|/|[end]| pair before the
365 % |save| and around the |restore|.
366 % \begin{macrocode}
367 \cs_new_protected:Npn \@@_backend_box_use:Nnnnn #1#2#3#4#5
368 {
369 \@@_backend_literal:n { save }
370 \@@_backend_literal:n { 72~Resolution~div~72~VResolution~div~neg~scale }
371 \@@_backend_literal:n { magscale { 1~DVImag~div~dup~scale } if }
372 \@@_backend_literal:n { draw.x~neg~draw.y~neg~translate }
373 \@@_backend_literal:n { [end] }
374 \@@_backend_literal:n { [begin] }
375 \@@_backend_literal:n { save }
376 \@@_backend_literal:n { currentpoint }
377 \@@_backend_literal:n { currentpoint~translate }
378 \@@_backend_transform:nnnn { 1 } { 0 } { 0 } { -1 }
379 \@@_backend_transform:nnnn {#2} {#3} {#4} {#5}
380 \@@_backend_transform:nnnn { 1 } { 0 } { 0 } { -1 }
381 \@@_backend_literal:n { neg~exch~neg~exch~translate }
382 \@@_backend_literal:n { [end] }
383 \hbox_overlap_right:n { \box_use:N #1 }
384 \@@_backend_literal:n { [begin] }
385 \@@_backend_literal:n { restore }
386 \@@_backend_literal:n { [end] }
387 \@@_backend_literal:n { [begin] }
388 \@@_backend_literal:n { restore }
389 }
390 % \end{macrocode}
391 % \end{macro}
392 %
393 % \begin{macrocode}
394 %</dvips>
395 % \end{macrocode}
396 %
397 % \subsection{\LuaTeX{}, \pdfTeX{}, \texttt{dvipdfmx} and \XeTeX{}}
398 %
399 % \LuaTeX{}, \pdfTeX{}, \texttt{dvipdfmx} and \XeTeX{} directly produce PDF output
400 % and understand a shared set of specials for drawing commands.
401 %
402 % \begin{macrocode}
403 %<*dvipdfmx|luatex|pdftex|xetex>
404 % \end{macrocode}
405 %
406 % \subsubsection{Drawing}
407 %
408 % \begin{macro}{\@@_backend_literal:n, \@@_backend_literal:e}
409 % Pass data through using a dedicated interface.
410 % \begin{macrocode}
411 \cs_new_eq:NN \@@_backend_literal:n \__kernel_backend_literal_pdf:n
412 \cs_new_eq:NN \@@_backend_literal:e \__kernel_backend_literal_pdf:e
413 % \end{macrocode}
414 % \end{macro}
415 %
416 % \begin{macro}{\@@_backend_begin:, \@@_backend_end:}
417 % No special requirements here, so simply set up a drawing scope.
418 % \begin{macrocode}
419 \cs_new_protected:Npn \@@_backend_begin:
420 { \@@_backend_scope_begin: }
421 \cs_new_protected:Npn \@@_backend_end:
422 { \@@_backend_scope_end: }
423 % \end{macrocode}
424 % \end{macro}
425 %
426 % \begin{macro}{\@@_backend_scope_begin:, \@@_backend_scope_end:}
427 % Use the backend-level scope mechanisms.
428 % \begin{macrocode}
429 \cs_new_eq:NN \@@_backend_scope_begin: \__kernel_backend_scope_begin:
430 \cs_new_eq:NN \@@_backend_scope_end: \__kernel_backend_scope_end:
431 % \end{macrocode}
432 % \end{macro}
433 %
434 % \begin{macro}{\@@_backend_moveto:nn, \@@_backend_lineto:nn}
435 % \begin{macro}{\@@_backend_curveto:nnnnnn}
436 % \begin{macro}{\@@_backend_rectangle:nnnn}
437 % Path creation operations all resolve directly to PDF primitive steps, with
438 % only the need to convert to \texttt{bp}.
439 % \begin{macrocode}
440 \cs_new_protected:Npn \@@_backend_moveto:nn #1#2
441 {
442 \@@_backend_literal:e
443 { \dim_to_decimal_in_bp:n {#1} ~ \dim_to_decimal_in_bp:n {#2} ~ m }
444 }
445 \cs_new_protected:Npn \@@_backend_lineto:nn #1#2
446 {
447 \@@_backend_literal:e
448 { \dim_to_decimal_in_bp:n {#1} ~ \dim_to_decimal_in_bp:n {#2} ~ l }
449 }
450 \cs_new_protected:Npn \@@_backend_curveto:nnnnnn #1#2#3#4#5#6
451 {
452 \@@_backend_literal:e
453 {
454 \dim_to_decimal_in_bp:n {#1} ~ \dim_to_decimal_in_bp:n {#2} ~
455 \dim_to_decimal_in_bp:n {#3} ~ \dim_to_decimal_in_bp:n {#4} ~
456 \dim_to_decimal_in_bp:n {#5} ~ \dim_to_decimal_in_bp:n {#6} ~
457 c
458 }
459 }
460 \cs_new_protected:Npn \@@_backend_rectangle:nnnn #1#2#3#4
461 {
462 \@@_backend_literal:e
463 {
464 \dim_to_decimal_in_bp:n {#1} ~ \dim_to_decimal_in_bp:n {#2} ~
465 \dim_to_decimal_in_bp:n {#3} ~ \dim_to_decimal_in_bp:n {#4} ~
466 re
467 }
468 }
469 % \end{macrocode}
470 % \end{macro}
471 % \end{macro}
472 % \end{macro}
473 %
474 % \begin{macro}{\@@_backend_evenodd_rule:, \@@_backend_nonzero_rule:}
475 % \begin{variable}{\g_@@_draw_eor_bool}
476 % The even-odd rule here can be implemented as a simply switch.
477 % \begin{macrocode}
478 \cs_new_protected:Npn \@@_backend_evenodd_rule:
479 { \bool_gset_true:N \g_@@_draw_eor_bool }
480 \cs_new_protected:Npn \@@_backend_nonzero_rule:
481 { \bool_gset_false:N \g_@@_draw_eor_bool }
482 \bool_new:N \g_@@_draw_eor_bool
483 % \end{macrocode}
484 % \end{variable}
485 % \end{macro}
486 %
487 % \begin{macro}
488 % {
489 % \@@_backend_closepath: ,
490 % \@@_backend_stroke: ,
491 % \@@_backend_closestroke: ,
492 % \@@_backend_fill: ,
493 % \@@_backend_fillstroke: ,
494 % \@@_backend_clip: ,
495 % \@@_backend_discardpath:
496 % }
497 % Converting paths to output is again a case of mapping directly to
498 % PDF operations.
499 % \begin{macrocode}
500 \cs_new_protected:Npn \@@_backend_closepath:
501 { \@@_backend_literal:n { h } }
502 \cs_new_protected:Npn \@@_backend_stroke:
503 { \@@_backend_literal:n { S } }
504 \cs_new_protected:Npn \@@_backend_closestroke:
505 { \@@_backend_literal:n { s } }
506 \cs_new_protected:Npn \@@_backend_fill:
507 {
508 \@@_backend_literal:e
509 { f \bool_if:NT \g_@@_draw_eor_bool * }
510 }
511 \cs_new_protected:Npn \@@_backend_fillstroke:
512 {
513 \@@_backend_literal:e
514 { B \bool_if:NT \g_@@_draw_eor_bool * }
515 }
516 \cs_new_protected:Npn \@@_backend_clip:
517 {
518 \@@_backend_literal:e
519 { W \bool_if:NT \g_@@_draw_eor_bool * }
520 }
521 \cs_new_protected:Npn \@@_backend_discardpath:
522 { \@@_backend_literal:n { n } }
523 % \end{macrocode}
524 % \end{macro}
525 %
526 % \begin{macro}{\@@_backend_dash_pattern:nn}
527 % \begin{macro}{\@@_backend_dash:n}
528 % \begin{macro}{\@@_backend_linewidth:n}
529 % \begin{macro}{\@@_backend_miterlimit:n}
530 % \begin{macro}
531 % {
532 % \@@_backend_cap_butt:, \@@_backend_cap_round:, \@@_backend_cap_rectangle:,
533 % \@@_backend_join_miter:, \@@_backend_join_round:, \@@_backend_join_bevel:
534 % }
535 % Converting paths to output is again a case of mapping directly to
536 % PDF operations.
537 % \begin{macrocode}
538 \cs_new_protected:Npn \@@_backend_dash_pattern:nn #1#2
539 {
540 \@@_backend_literal:e
541 {
542 [
543 \exp_args:Nf \use:n
544 { \clist_map_function:nN {#1} \@@_backend_dash:n }
545 ] ~
546 \dim_to_decimal_in_bp:n {#2} ~ d
547 }
548 }
549 \cs_new:Npn \@@_backend_dash:n #1
550 { ~ \dim_to_decimal_in_bp:n {#1} }
551 \cs_new_protected:Npn \@@_backend_linewidth:n #1
552 {
553 \@@_backend_literal:e
554 { \dim_to_decimal_in_bp:n {#1} ~ w }
555 }
556 \cs_new_protected:Npn \@@_backend_miterlimit:n #1
557 { \@@_backend_literal:e { #1 ~ M } }
558 \cs_new_protected:Npn \@@_backend_cap_butt:
559 { \@@_backend_literal:n { 0 ~ J } }
560 \cs_new_protected:Npn \@@_backend_cap_round:
561 { \@@_backend_literal:n { 1 ~ J } }
562 \cs_new_protected:Npn \@@_backend_cap_rectangle:
563 { \@@_backend_literal:n { 2 ~ J } }
564 \cs_new_protected:Npn \@@_backend_join_miter:
565 { \@@_backend_literal:n { 0 ~ j } }
566 \cs_new_protected:Npn \@@_backend_join_round:
567 { \@@_backend_literal:n { 1 ~ j } }
568 \cs_new_protected:Npn \@@_backend_join_bevel:
569 { \@@_backend_literal:n { 2 ~ j } }
570 % \end{macrocode}
571 % \end{macro}
572 % \end{macro}
573 % \end{macro}
574 % \end{macro}
575 % \end{macro}
576 %
577 % \begin{macro}{\@@_backend_transform:nnnn}
578 % \begin{macro}{\@@_backend_transform_aux:nnnn}
579 % \begin{macro}{\@@_backend_shift:nn}
580 % Another split here between \LuaTeX{}/pdfTeX{} and \texttt{dvipdfmx}/\XeTeX{}.
581 % In the former, we have a direct method to maintain alignment: the backend
582 % can use a matrix itself. For \texttt{dvipdfmx}/\XeTeX{}, we can to decompose the
583 % matrix into rotations and a scaling, then use those operations as they
584 % are handled by the backend. (There is backend support for matrix operations in
585 % \texttt{dvipdfmx}/\XeTeX{}, but as a matched pair so not suitable for the
586 % \enquote{stand alone} transformation set up here.) The specials used here
587 % are from \texttt{xdvipdfmx} originally: they are well-tested, but probably
588 % equivalent to the \texttt{pdf:} versions! As working out the rotation is
589 % relatively expensive, we optimize for the case where there is only a
590 % scaling.
591 % \begin{macrocode}
592 \cs_new_protected:Npn \@@_backend_transform:nnnn #1#2#3#4
593 {
594 %<*luatex|pdftex>
595 \__kernel_backend_matrix:n { #1 ~ #2 ~ #3 ~ #4 }
596 %</luatex|pdftex>
597 %<*dvipdfmx|xetex>
598 \str_if_eq:nnTF { #2 ~ #3 } { 0 ~ 0 }
599 {
600 \__kernel_backend_literal:n { x:rotate~0 }
601 \__kernel_backend_literal:n { x:scale~#1~#4 }
602 \__kernel_backend_literal:n { x:rotate~0 }
603 }
604 {
605 \@@_backend_transform_decompose:nnnnN {#1} {#2} {#3} {#4}
606 \@@_backend_transform_aux:nnnn
607 }
608 %</dvipdfmx|xetex>
609 }
610 %<*dvipdfmx|xetex>
611 \cs_new_protected:Npn \@@_backend_transform_aux:nnnn #1#2#3#4
612 {
613 \__kernel_backend_literal:e
614 {
615 x:rotate~
616 \fp_compare:nNnTF {#1} = \c_zero_fp
617 { 0 }
618 { \fp_eval:n { round ( -#1 , 5 ) } }
619 }
620 \__kernel_backend_literal:e
621 {
622 x:scale~
623 \fp_eval:n { round ( #2 , 5 ) } ~
624 \fp_eval:n { round ( #3 , 5 ) }
625 }
626 \__kernel_backend_literal:e
627 {
628 x:rotate~
629 \fp_compare:nNnTF {#4} = \c_zero_fp
630 { 0 }
631 { \fp_eval:n { round ( -#4 , 5 ) } }
632 }
633 }
634 %</dvipdfmx|xetex>
635 % \end{macrocode}
636 % Much less complex for a shift: this is deliberately not tracked by the
637 % engine (we would otherwise do stuff in \TeX{}), so use the same approach
638 % for all PDF-based routes.
639 % \begin{macrocode}
640 \cs_new_protected:Npn \@@_backend_shift:nn #1#2
641 {
642 \@@_backend_literal:n
643 { 1 ~ 0 ~ 0 ~ 1 ~ #1 ~ #2 ~ cm }
644 }
645 % \end{macrocode}
646 % \end{macro}
647 % \end{macro}
648 % \end{macro}
649 %
650 % \begin{macro}{\@@_backend_transform_decompose:nnnnN}
651 % \begin{macro}
652 % {
653 % \@@_backend_transform_decompose_auxi:nnnnN,
654 % \@@_backend_transform_decompose_auxii:nnnnN,
655 % \@@_backend_transform_decompose_auxiii:nnnnN,
656 % }
657 % Internally, transformations for drawing are tracked as a matrix. Not all
658 % engines provide a way of dealing with this: if we use a raw matrix, the
659 % engine looses track of positions (for example for hyperlinks), and this is
660 % not desirable. They do, however, allow us to track rotations and scalings.
661 % Luckily, we can decompose any (two-dimensional) matrix into two rotations
662 % and a single scaling:
663 % \[
664 % \begin{bmatrix}
665 % A & B \\ C & D
666 % \end{bmatrix}
667 % =
668 % \begin{bmatrix}
669 % \cos\beta & \sin\beta \\ -\sin\beta & \cos\beta
670 % \end{bmatrix}
671 % \begin{bmatrix}
672 % w_{1} & 0 \\ 0 & w_{2}
673 % \end{bmatrix}
674 % \begin{bmatrix}
675 % \cos\gamma & \sin\gamma \\ -\sin\gamma & \cos\gamma
676 % \end{bmatrix}
677 % \]
678 % The parent matrix can be converted to
679 % \[
680 % \begin{bmatrix}
681 % A & B \\ C & D
682 % \end{bmatrix}
683 % =
684 % \begin{bmatrix}
685 % E & H \\-H & E
686 % \end{bmatrix}
687 % +
688 % \begin{bmatrix}
689 % F & G \\ G & -F
690 % \end{bmatrix}
691 % \]
692 % From these, we can find that
693 % \begin{align*}
694 % \frac{w_{1} + w_{2}}{2} &= \sqrt{E^{2} + H^{2}} \\
695 % \frac{w_{1} - w_{2}}{2} &= \sqrt{F^{2} + G^{2}} \\
696 % \gamma - \beta &= \tan^{-1}(G/F) \\
697 % \gamma + \beta &= \tan^{-1}(H/E)
698 % \end{align*}
699 % at which point we just have to do various pieces of re-arrangement to
700 % get all of the values. (See J.~Blinn, \emph{IEEE Comput.\ Graph.\ Appl.},
701 % 1996, \textbf{16}, 82--88.) There is one wrinkle: the PostScript (and PDF)
702 % way of specifying a transformation matrix exchanges where one would
703 % normally expect $B$ and $C$ to be.
704 % \begin{macrocode}
705 %<*dvipdfmx|xetex>
706 \cs_new_protected:Npn \@@_backend_transform_decompose:nnnnN #1#2#3#4#5
707 {
708 \use:e
709 {
710 \@@_backend_transform_decompose_auxi:nnnnN
711 { \fp_eval:n { (#1 + #4) / 2 } }
712 { \fp_eval:n { (#1 - #4) / 2 } }
713 { \fp_eval:n { (#3 + #2) / 2 } }
714 { \fp_eval:n { (#3 - #2) / 2 } }
715 }
716 #5
717 }
718 \cs_new_protected:Npn \@@_backend_transform_decompose_auxi:nnnnN #1#2#3#4#5
719 {
720 \use:e
721 {
722 \@@_backend_transform_decompose_auxii:nnnnN
723 { \fp_eval:n { 2 * sqrt ( #1 * #1 + #4 * #4 ) } }
724 { \fp_eval:n { 2 * sqrt ( #2 * #2 + #3 * #3 ) } }
725 { \fp_eval:n { atand ( #3 , #2 ) } }
726 { \fp_eval:n { atand ( #4 , #1 ) } }
727 }
728 #5
729 }
730 \cs_new_protected:Npn \@@_backend_transform_decompose_auxii:nnnnN #1#2#3#4#5
731 {
732 \use:e
733 {
734 \@@_backend_transform_decompose_auxiii:nnnnN
735 { \fp_eval:n { ( #4 - #3 ) / 2 } }
736 { \fp_eval:n { ( #1 + #2 ) / 2 } }
737 { \fp_eval:n { ( #1 - #2 ) / 2 } }
738 { \fp_eval:n { ( #4 + #3 ) / 2 } }
739 }
740 #5
741 }
742 \cs_new_protected:Npn \@@_backend_transform_decompose_auxiii:nnnnN #1#2#3#4#5
743 {
744 \fp_compare:nNnTF { abs( #2 ) } > { abs ( #3 ) }
745 { #5 {#1} {#2} {#3} {#4} }
746 { #5 {#1} {#3} {#2} {#4} }
747 }
748 %</dvipdfmx|xetex>
749 % \end{macrocode}
750 % \end{macro}
751 % \end{macro}
752 %
753 % \begin{macro}{\@@_backend_box_use:Nnnnn}
754 % Inserting a \TeX{} box transformed to the requested position and using
755 % the current matrix is done using a mixture of \TeX{} and low-level
756 % manipulation. The offset can be handled by \TeX{}, so only any rotation/^^A
757 % skew/scaling component needs to be done using the matrix operation. As this
758 % operation can never be cached, the scope is set directly not using the
759 % \texttt{draw} version.
760 % \begin{macrocode}
761 \cs_new_protected:Npn \@@_backend_box_use:Nnnnn #1#2#3#4#5
762 {
763 \__kernel_backend_scope_begin:
764 %<*luatex|pdftex>
765 \__kernel_backend_matrix:n { #2 ~ #3 ~ #4 ~ #5 }
766 %</luatex|pdftex>
767 %<*dvipdfmx|xetex>
768 \__kernel_backend_literal:n
769 { pdf:btrans~matrix~ #2 ~ #3 ~ #4 ~ #5 ~ 0 ~ 0 }
770 %</dvipdfmx|xetex>
771 \hbox_overlap_right:n { \box_use:N #1 }
772 %<*dvipdfmx|xetex>
773 \__kernel_backend_literal:n { pdf:etrans }
774 %</dvipdfmx|xetex>
775 \__kernel_backend_scope_end:
776 }
777 % \end{macrocode}
778 % \end{macro}
779 %
780 % \begin{macrocode}
781 %</dvipdfmx|luatex|pdftex|xetex>
782 % \end{macrocode}
783 %
784 % \subsection{\texttt{dvisvgm} backend}
785 %
786 % \begin{macrocode}
787 %<*dvisvgm>
788 % \end{macrocode}
789 %
790 % \begin{macro}{\@@_backend_literal:n, \@@_backend_literal:e}
791 % The same as the more general literal call.
792 % \begin{macrocode}
793 \cs_new_eq:NN \@@_backend_literal:n \__kernel_backend_literal_svg:n
794 \cs_generate_variant:Nn \@@_backend_literal:n { e }
795 % \end{macrocode}
796 % \end{macro}
797 %
798 % \begin{macro}{\@@_backend_scope_begin:, \@@_backend_scope_end:}
799 % Use the backend-level scope mechanisms.
800 % \begin{macrocode}
801 \cs_new_eq:NN \@@_backend_scope_begin: \__kernel_backend_scope_begin:
802 \cs_new_eq:NN \@@_backend_scope_end: \__kernel_backend_scope_end:
803 % \end{macrocode}
804 % \end{macro}
805 %
806 % \begin{macro}{\@@_backend_begin:, \@@_backend_end:}
807 % A drawing needs to be set up such that the coordinate system is
808 % translated. That is done inside a scope, which as described below
809 % \begin{macrocode}
810 \cs_new_protected:Npn \@@_backend_begin:
811 {
812 \__kernel_backend_scope_begin:
813 \__kernel_backend_scope:n { transform="translate({?x},{?y})~scale(1,-1)" }
814 }
815 \cs_new_eq:NN \@@_backend_end: \__kernel_backend_scope_end:
816 % \end{macrocode}
817 % \end{macro}
818 %
819 % \begin{macro}{\@@_backend_moveto:nn, \@@_backend_lineto:nn}
820 % \begin{macro}{\@@_backend_rectangle:nnnn}
821 % \begin{macro}{\@@_backend_curveto:nnnnnn}
822 % \begin{macro}{\@@_backend_add_to_path:n}
823 % \begin{variable}{\g_@@_backend_path_tl}
824 % Once again, some work is needed to get path constructs correct. Rather
825 % then write the values as they are given, the entire path needs to be
826 % collected up before being output in one go. For that we use a dedicated
827 % storage routine, which adds spaces as required. Since paths should
828 % be fully expanded there is no need to worry about the internal
829 % \texttt{e}-type expansion.
830 % \begin{macrocode}
831 \cs_new_protected:Npn \@@_backend_moveto:nn #1#2
832 {
833 \@@_backend_add_to_path:n
834 { M ~ \dim_to_decimal:n {#1} ~ \dim_to_decimal:n {#2} }
835 }
836 \cs_new_protected:Npn \@@_backend_lineto:nn #1#2
837 {
838 \@@_backend_add_to_path:n
839 { L ~ \dim_to_decimal:n {#1} ~ \dim_to_decimal:n {#2} }
840 }
841 \cs_new_protected:Npn \@@_backend_rectangle:nnnn #1#2#3#4
842 {
843 \@@_backend_add_to_path:n
844 {
845 M ~ \dim_to_decimal:n {#1} ~ \dim_to_decimal:n {#2}
846 h ~ \dim_to_decimal:n {#3} ~
847 v ~ \dim_to_decimal:n {#4} ~
848 h ~ \dim_to_decimal:n { -#3 } ~
849 Z
850 }
851 }
852 \cs_new_protected:Npn \@@_backend_curveto:nnnnnn #1#2#3#4#5#6
853 {
854 \@@_backend_add_to_path:n
855 {
856 C ~
857 \dim_to_decimal:n {#1} ~ \dim_to_decimal:n {#2} ~
858 \dim_to_decimal:n {#3} ~ \dim_to_decimal:n {#4} ~
859 \dim_to_decimal:n {#5} ~ \dim_to_decimal:n {#6}
860 }
861 }
862 \cs_new_protected:Npn \@@_backend_add_to_path:n #1
863 {
864 \tl_gset:Ne \g_@@_backend_path_tl
865 {
866 \g_@@_backend_path_tl
867 \tl_if_empty:NF \g_@@_backend_path_tl { \c_space_tl }
868 #1
869 }
870 }
871 \tl_new:N \g_@@_backend_path_tl
872 % \end{macrocode}
873 % \end{variable}
874 % \end{macro}
875 % \end{macro}
876 % \end{macro}
877 % \end{macro}
878 %
879 % \begin{macro}{\@@_backend_evenodd_rule:, \@@_backend_nonzero_rule:}
880 % The fill rules here have to be handled as scopes.
881 % \begin{macrocode}
882 \cs_new_protected:Npn \@@_backend_evenodd_rule:
883 { \__kernel_backend_scope:n { fill-rule="evenodd" } }
884 \cs_new_protected:Npn \@@_backend_nonzero_rule:
885 { \__kernel_backend_scope:n { fill-rule="nonzero" } }
886 % \end{macrocode}
887 % \end{macro}
888 %
889 % \begin{macro}{\@@_backend_path:n}
890 % \begin{macro}
891 % {
892 % \@@_backend_closepath: ,
893 % \@@_backend_stroke: ,
894 % \@@_backend_closestroke: ,
895 % \@@_backend_fill: ,
896 % \@@_backend_fillstroke: ,
897 % \@@_backend_clip: ,
898 % \@@_backend_discardpath:
899 % }
900 % \begin{variable}{\g_@@_draw_clip_bool}
901 % \begin{variable}{\g_@@_draw_path_int}
902 % Setting fill and stroke effects and doing clipping all has to be done using
903 % scopes. This means setting up the various requirements in a shared
904 % auxiliary which deals with the bits and pieces. Clipping paths are reused
905 % for path drawing: not essential but avoids constructing them twice.
906 % Discarding a path needs a separate function as it's not quite the same.
907 % \begin{macrocode}
908 \cs_new_protected:Npn \@@_backend_closepath:
909 { \@@_backend_add_to_path:n { Z } }
910 \cs_new_protected:Npn \@@_backend_path:n #1
911 {
912 \bool_if:NTF \g_@@_draw_clip_bool
913 {
914 \int_gincr:N \g__kernel_clip_path_int
915 \@@_backend_literal:e
916 {
917 < clipPath~id = " l3cp \int_use:N \g__kernel_clip_path_int " >
918 { ?nl }
919 <path~d=" \g_@@_backend_path_tl "/> { ?nl }
920 < /clipPath > { ? nl }
921 <
922 use~xlink:href =
923 "\c_hash_str l3path \int_use:N \g_@@_backend_path_int " ~
924 #1
925 />
926 }
927 \__kernel_backend_scope:e
928 {
929 clip-path =
930 "url( \c_hash_str l3cp \int_use:N \g__kernel_clip_path_int)"
931 }
932 }
933 {
934 \@@_backend_literal:e
935 { <path ~ d=" \g_@@_backend_path_tl " ~ #1 /> }
936 }
937 \tl_gclear:N \g_@@_backend_path_tl
938 \bool_gset_false:N \g_@@_draw_clip_bool
939 }
940 \int_new:N \g_@@_backend_path_int
941 \cs_new_protected:Npn \@@_backend_stroke:
942 { \@@_backend_path:n { style="fill:none" } }
943 \cs_new_protected:Npn \@@_backend_closestroke:
944 {
945 \@@_backend_closepath:
946 \@@_backend_stroke:
947 }
948 \cs_new_protected:Npn \@@_backend_fill:
949 { \@@_backend_path:n { style="stroke:none" } }
950 \cs_new_protected:Npn \@@_backend_fillstroke:
951 { \@@_backend_path:n { } }
952 \cs_new_protected:Npn \@@_backend_clip:
953 { \bool_gset_true:N \g_@@_draw_clip_bool }
954 \bool_new:N \g_@@_draw_clip_bool
955 \cs_new_protected:Npn \@@_backend_discardpath:
956 {
957 \bool_if:NT \g_@@_draw_clip_bool
958 {
959 \int_gincr:N \g__kernel_clip_path_int
960 \@@_backend_literal:e
961 {
962 < clipPath~id = " l3cp \int_use:N \g__kernel_clip_path_int " >
963 { ?nl }
964 <path~d=" \g_@@_backend_path_tl "/> { ?nl }
965 < /clipPath >
966 }
967 \__kernel_backend_scope:e
968 {
969 clip-path =
970 "url( \c_hash_str l3cp \int_use:N \g__kernel_clip_path_int)"
971 }
972 }
973 \tl_gclear:N \g_@@_backend_path_tl
974 \bool_gset_false:N \g_@@_draw_clip_bool
975 }
976 % \end{macrocode}
977 % \end{variable}
978 % \end{variable}
979 % \end{macro}
980 % \end{macro}
981 %
982 % \begin{macro}{\@@_backend_dash_pattern:nn}
983 % \begin{macro}{\@@_backend_dash:n}
984 % \begin{macro}{\@@_backend_dash_aux:nn}
985 % \begin{macro}{\@@_backend_linewidth:n}
986 % \begin{macro}{\@@_backend_miterlimit:n}
987 % \begin{macro}
988 % {
989 % \@@_backend_cap_butt:, \@@_backend_cap_round:, \@@_backend_cap_rectangle:,
990 % \@@_backend_join_miter:, \@@_backend_join_round:, \@@_backend_join_bevel:
991 % }
992 % All of these ideas are properties of scopes in SVG. The only slight
993 % complexity is converting the dash array properly (doing any required
994 % maths).
995 % \begin{macrocode}
996 \cs_new_protected:Npn \@@_backend_dash_pattern:nn #1#2
997 {
998 \use:e
999 {
1000 \@@_backend_dash_aux:nn
1001 { \clist_map_function:nN {#1} \@@_backend_dash:n }
1002 { \dim_to_decimal:n {#2} }
1003 }
1004 }
1005 \cs_new:Npn \@@_backend_dash:n #1
1006 { , \dim_to_decimal_in_bp:n {#1} }
1007 \cs_new_protected:Npn \@@_backend_dash_aux:nn #1#2
1008 {
1009 \__kernel_backend_scope:e
1010 {
1011 stroke-dasharray =
1012 "
1013 \tl_if_empty:nTF {#1}
1014 { none }
1015 { \use_none:n #1 }
1016 " ~
1017 stroke-offset=" #2 "
1018 }
1019 }
1020 \cs_new_protected:Npn \@@_backend_linewidth:n #1
1021 { \__kernel_backend_scope:e { stroke-width=" \dim_to_decimal:n {#1} " } }
1022 \cs_new_protected:Npn \@@_backend_miterlimit:n #1
1023 { \__kernel_backend_scope:e { stroke-miterlimit=" #1 " } }
1024 \cs_new_protected:Npn \@@_backend_cap_butt:
1025 { \__kernel_backend_scope:n { stroke-linecap="butt" } }
1026 \cs_new_protected:Npn \@@_backend_cap_round:
1027 { \__kernel_backend_scope:n { stroke-linecap="round" } }
1028 \cs_new_protected:Npn \@@_backend_cap_rectangle:
1029 { \__kernel_backend_scope:n { stroke-linecap="square" } }
1030 \cs_new_protected:Npn \@@_backend_join_miter:
1031 { \__kernel_backend_scope:n { stroke-linejoin="miter" } }
1032 \cs_new_protected:Npn \@@_backend_join_round:
1033 { \__kernel_backend_scope:n { stroke-linejoin="round" } }
1034 \cs_new_protected:Npn \@@_backend_join_bevel:
1035 { \__kernel_backend_scope:n { stroke-linejoin="bevel" } }
1036 % \end{macrocode}
1037 % \end{macro}
1038 % \end{macro}
1039 % \end{macro}
1040 % \end{macro}
1041 % \end{macro}
1042 % \end{macro}
1043 %
1044 % \begin{macro}{\@@_backend_transform:nnnn}
1045 % \begin{macro}{\@@_backend_shift:nn}
1046 % The four arguments here are floats (the affine matrix), the last
1047 % two are a displacement vector.
1048 % \begin{macrocode}
1049 \cs_new_protected:Npn \@@_backend_transform:nnnn #1#2#3#4
1050 {
1051 \__kernel_backend_scope:n
1052 {
1053 transform =
1054 " matrix ( #1 , #2 , #3 , #4 , 0pt , 0pt ) "
1055 }
1056 }
1057 \cs_new_protected:Npn \@@_backend_shift:nn #1#2
1058 {
1059 \__kernel_backend_scope:n
1060 {
1061 transform =
1062 " matrix ( 1 , 0 , 0 , 1 ,  #1pt , #2pt ) "
1063 }
1064 }
1065 % \end{macrocode}
1066 % \end{macro}
1067 % \end{macro}
1068 %
1069 % \begin{macro}{\@@_backend_box_use:Nnnnn}
1070 % No special savings can be made here: simply displace the box inside
1071 % a scope. As there is nothing to re-box, just make the box passed of
1072 % zero size.
1073 % \begin{macrocode}
1074 \cs_new_protected:Npn \@@_backend_box_use:Nnnnn #1#2#3#4#5
1075 {
1076 \__kernel_backend_scope_begin:
1077 \@@_backend_transform:nnnn {#2} {#3} {#4} {#5}
1078 \__kernel_backend_literal_svg:n
1079 {
1080 < g~
1081 stroke="none"~
1082 transform="scale(-1,1)~translate({?x},{?y})~scale(-1,-1)"
1083 >
1084 }
1085 \box_set_wd:Nn #1 { 0pt }
1086 \box_set_ht:Nn #1 { 0pt }
1087 \box_set_dp:Nn #1 { 0pt }
1088 \box_use:N #1
1089 \__kernel_backend_literal_svg:n { </g> }
1090 \__kernel_backend_scope_end:
1091 }
1092 % \end{macrocode}
1093 % \end{macro}
1094 %
1095 % \begin{macrocode}
1096 %</dvisvgm>
1097 % \end{macrocode}
1098 %
1099 % \begin{macrocode}
1100 %</package>
1101 % \end{macrocode}
1102 %
1103 % \end{implementation}
1104 %
1105 % \PrintIndex