Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3backend-basics.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-basics} module\\ Backend basics^^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-basics} implementation}
57 %
58 % \begin{macrocode}
59 %<*package>
60 % \end{macrocode}
61 %
62 % Whilst there is a reasonable amount of code overlap between backends,
63 % it is much clearer to have the blocks more-or-less separated than run
64 % in together and DocStripped out in parts. As such, most of the following
65 % is set up on a per-backend basis, though there is some common code (again
66 % given in blocks not interspersed with other material).
67 %
68 % All the file identifiers are up-front so that they come out in the right
69 % place in the files.
70 % \begin{macrocode}
71 \ProvidesExplFile
72 %<*dvipdfmx>
73 {l3backend-dvipdfmx.def}{2026-02-18}{}
74 {L3 backend support: dvipdfmx}
75 %</dvipdfmx>
76 %<*dvips>
77 {l3backend-dvips.def}{2026-02-18}{}
78 {L3 backend support: dvips}
79 %</dvips>
80 %<*dvisvgm>
81 {l3backend-dvisvgm.def}{2026-02-18}{}
82 {L3 backend support: dvisvgm}
83 %</dvisvgm>
84 %<*luatex>
85 {l3backend-luatex.def}{2026-02-18}{}
86 {L3 backend support: PDF output (LuaTeX)}
87 %</luatex>
88 %<*pdftex>
89 {l3backend-pdftex.def}{2026-02-18}{}
90 {L3 backend support: PDF output (pdfTeX)}
91 %</pdftex>
92 %<*xetex>
93 {l3backend-xetex.def}{2026-02-18}{}
94 {L3 backend support: XeTeX}
95 %</xetex>
96 % \end{macrocode}
97 %
98 % Check if the loaded kernel is at least enough to load this file.
99 % The kernel date has to be at least equal to \cs{ExplBackendFileDate}
100 % or later. If \cs{__kernel_dependency_version_check:Nn} doesn't
101 % exist we're loading in an older kernel, so it's an error anyway.
102 % With time, this test should vanish and only the dependency check
103 % should remain.
104 % \begin{macrocode}
105 \cs_if_exist:NTF \__kernel_dependency_version_check:nn
106 {
107 \__kernel_dependency_version_check:nn {2023-10-10}
108 %<dvipdfmx> {l3backend-dvipdfmx.def}
109 %<dvips> {l3backend-dvips.def}
110 %<dvisvgm> {l3backend-dvisvgm.def}
111 %<luatex> {l3backend-luatex.def}
112 %<pdftex> {l3backend-pdftex.def}
113 %<xetex> {l3backend-xetex.def}
114 }
115 {
116 \cs_if_exist_use:cF { @latex@error } { \errmessage }
117 {
118 Mismatched~LaTeX~support~files~detected. \MessageBreak
119 Loading~aborted!
120 }
121 { \use:c { @ehd } }
122 \tex_endinput:D
123 }
124 % \end{macrocode}
125 %
126 % The order of the backend code here is such that we get somewhat logical
127 % outcomes in terms of code sharing whilst keeping things readable. (Trying to
128 % mix all of the code by concept is almost unmanageable.) The key parts which
129 % are shared are
130 % \begin{itemize}
131 % \item Color support is either \texttt{dvips}-like or \LuaTeX{}/pdfTeX{}-like.
132 % \item \LuaTeX{}/pdfTeX{} and \texttt{dvipdfmx}/\XeTeX{} share drawing routines.
133 % \item \XeTeX{} is the same as \texttt{dvipdfmx} other than image size
134 % extraction so takes most of the same code.
135 % \end{itemize}
136 %
137 % \begin{macro}
138 % {
139 % \__kernel_backend_literal:e,
140 % \__kernel_backend_literal:n
141 % }
142 % The one shared function for all backends is access to the basic
143 % \tn{special} primitive: it has slightly odd expansion behavior
144 % so a wrapper is provided.
145 % \begin{macrocode}
146 \cs_new_eq:NN \__kernel_backend_literal:e \tex_special:D
147 \cs_new_protected:Npn \__kernel_backend_literal:n #1
148 { \__kernel_backend_literal:e { \exp_not:n {#1} } }
149 % \end{macrocode}
150 % \end{macro}
151 %
152 % \begin{macro}{\__kernel_backend_first_shipout:n}
153 % We need to write at first shipout in a few places. As we want to use the
154 % most up-to-date method,
155 % \begin{macrocode}
156 \cs_if_exist:NTF \@ifl@t@r
157 {
158 \@ifl@t@r \fmtversion { 2020-10-01 }
159 {
160 \cs_new_protected:Npn \__kernel_backend_first_shipout:n #1
161 { \hook_gput_code:nnn { shipout / firstpage } { l3backend } {#1} }
162 }
163 { \cs_new_eq:NN \__kernel_backend_first_shipout:n \AtBeginDvi }
164 }
165 { \cs_new_eq:NN \__kernel_backend_first_shipout:n \use:n }
166 % \end{macrocode}
167 % \end{macro}
168 %
169 % \subsection{\texttt{dvips} backend}
170 %
171 % \begin{macrocode}
172 %<*dvips>
173 % \end{macrocode}
174 %
175 % \begin{macro}
176 % {\__kernel_backend_literal_postscript:n, \__kernel_backend_literal_postscript:e}
177 % Literal PostScript can be included using a few low-level formats. Here,
178 % we use the form with no positioning: this is overall more convenient as
179 % a wrapper. Note that this does require that where position is important,
180 % an appropriate wrapper is included.
181 % \begin{macrocode}
182 \cs_new_protected:Npn \__kernel_backend_literal_postscript:n #1
183 { \__kernel_backend_literal:n { ps:: #1 } }
184 \cs_generate_variant:Nn \__kernel_backend_literal_postscript:n { e }
185 % \end{macrocode}
186 % \end{macro}
187 %
188 % \begin{macro}{\__kernel_backend_postscript:n, \__kernel_backend_postscript:e}
189 % PostScript data that does have positioning, and also applying
190 % a shift to |SDict| (which is not done automatically by
191 % |ps:| or |ps::|, in contrast to |!| or |"|).
192 % \begin{macrocode}
193 \cs_new_protected:Npn \__kernel_backend_postscript:n #1
194 { \__kernel_backend_literal:n { ps: SDict ~ begin ~ #1 ~ end } }
195 \cs_generate_variant:Nn \__kernel_backend_postscript:n { e }
196 % \end{macrocode}
197 % \end{macro}
198 %
199 % PostScript for the header: a small saving but makes the code clearer.
200 % This is held until the start of shipout such that a document with no
201 % actual output does not write anything.
202 % \begin{macrocode}
203 \bool_if:NT \g__kernel_backend_header_bool
204 {
205 \__kernel_backend_first_shipout:n
206 { \__kernel_backend_literal:n { header = l3backend-dvips.pro } }
207 }
208 % \end{macrocode}
209 %
210 % \begin{macro}
211 % {
212 % \__kernel_backend_align_begin:,
213 % \__kernel_backend_align_end:
214 % }
215 % In \texttt{dvips} there is no built-in saving of the current
216 % position, and so some additional PostScript is required to set up the
217 % transformation matrix and also to restore it afterwards. Notice the use
218 % of the stack to save the current position \enquote{up front} and to
219 % move back to it at the end of the process. Notice that the |[begin]|/^^A
220 % |[end]| pair here mean that we can use a run of PostScript statements
221 % in separate lines: not \emph{required} but does make the code and
222 % output more clear.
223 % \begin{macrocode}
224 \cs_new_protected:Npn \__kernel_backend_align_begin:
225 {
226 \__kernel_backend_literal:n { ps::[begin] }
227 \__kernel_backend_literal_postscript:n { currentpoint }
228 \__kernel_backend_literal_postscript:n { currentpoint~translate }
229 }
230 \cs_new_protected:Npn \__kernel_backend_align_end:
231 {
232 \__kernel_backend_literal_postscript:n { neg~exch~neg~exch~translate }
233 \__kernel_backend_literal:n { ps::[end] }
234 }
235 % \end{macrocode}
236 % \end{macro}
237 %
238 % \begin{macro}{\__kernel_backend_scope_begin:, \__kernel_backend_scope_end:}
239 % Saving/restoring scope for general operations needs to be done with
240 % \texttt{dvips} positioning (try without to see this!). Thus we need the
241 % |ps:| version of the special here. As only the graphics state is ever
242 % altered within this pairing, we use the lower-cost |g|-versions.
243 % \begin{macrocode}
244 \cs_new_protected:Npn \__kernel_backend_scope_begin:
245 { \__kernel_backend_literal:n { ps:gsave } }
246 \cs_new_protected:Npn \__kernel_backend_scope_end:
247 { \__kernel_backend_literal:n { ps:grestore } }
248 % \end{macrocode}
249 % \end{macro}
250 %
251 % \begin{macrocode}
252 %</dvips>
253 % \end{macrocode}
254 %
255 % \subsection{\LuaTeX{} and \pdfTeX{} backends}
256 %
257 % \begin{macrocode}
258 %<*luatex|pdftex>
259 % \end{macrocode}
260 %
261 % Both \LuaTeX{} and \pdfTeX{} write PDFs directly rather than via an
262 % intermediate file. Although there are similarities, the move of \LuaTeX{}
263 % to have more code in Lua means we create two independent files using
264 % shared DocStrip code.
265 %
266 % \begin{macro}{\__kernel_backend_literal_pdf:n, \__kernel_backend_literal_pdf:e}
267 % This is equivalent to \verb|\special{pdf:}| but the engine can
268 % track it. Without the \texttt{direct} keyword everything is kept in
269 % sync: the transformation matrix is set to the current point automatically.
270 % Note that this is still inside the text (\texttt{BT} \dots \texttt{ET}
271 % block).
272 % \begin{macrocode}
273 \cs_new_protected:Npn \__kernel_backend_literal_pdf:n #1
274 {
275 %<*luatex>
276 \tex_pdfextension:D literal
277 %</luatex>
278 %<*pdftex>
279 \tex_pdfliteral:D
280 %</pdftex>
281 { \exp_not:n {#1} }
282 }
283 \cs_new_protected:Npn \__kernel_backend_literal_pdf:e #1
284 {
285 %<*luatex>
286 \tex_pdfextension:D literal
287 %</luatex>
288 %<*pdftex>
289 \tex_pdfliteral:D
290 %</pdftex>
291 {#1}
292 }
293 % \end{macrocode}
294 % \end{macro}
295 %
296 % \begin{macro}{\__kernel_backend_literal_page:n, \__kernel_backend_literal_page:e}
297 % Page literals are pretty simple. To avoid an expansion, we write out
298 % by hand.
299 % \begin{macrocode}
300 \cs_new_protected:Npn \__kernel_backend_literal_page:n #1
301 {
302 %<*luatex>
303 \tex_pdfextension:D literal ~
304 %</luatex>
305 %<*pdftex>
306 \tex_pdfliteral:D
307 %</pdftex>
308 page { \exp_not:n {#1} }
309 }
310 \cs_new_protected:Npn \__kernel_backend_literal_page:e #1
311 {
312 %<*luatex>
313 \tex_pdfextension:D literal ~
314 %</luatex>
315 %<*pdftex>
316 \tex_pdfliteral:D
317 %</pdftex>
318 page {#1}
319 }
320 % \end{macrocode}
321 % \end{macro}
322 %
323 % \begin{macro}{\__kernel_backend_scope_begin:, \__kernel_backend_scope_end:}
324 % Higher-level interfaces for saving and restoring the graphic state.
325 % \begin{macrocode}
326 \cs_new_protected:Npn \__kernel_backend_scope_begin:
327 {
328 %<*luatex>
329 \tex_pdfextension:D save \scan_stop:
330 %</luatex>
331 %<*pdftex>
332 \tex_pdfsave:D
333 %</pdftex>
334 }
335 \cs_new_protected:Npn \__kernel_backend_scope_end:
336 {
337 %<*luatex>
338 \tex_pdfextension:D restore \scan_stop:
339 %</luatex>
340 %<*pdftex>
341 \tex_pdfrestore:D
342 %</pdftex>
343 }
344 % \end{macrocode}
345 % \end{macro}
346 %
347 % \begin{macro}{\__kernel_backend_matrix:n, \__kernel_backend_matrix:e}
348 % Here the appropriate function is set up to insert an affine matrix
349 % into the PDF. With \pdfTeX{} and \LuaTeX{} in direct PDF output mode there
350 % is a primitive for this, which only needs the rotation/scaling/skew part.
351 % \begin{macrocode}
352 \cs_new_protected:Npn \__kernel_backend_matrix:n #1
353 {
354 %<*luatex>
355 \tex_pdfextension:D setmatrix
356 %</luatex>
357 %<*pdftex>
358 \tex_pdfsetmatrix:D
359 %</pdftex>
360 { \exp_not:n {#1} }
361 }
362 \cs_new_protected:Npn \__kernel_backend_matrix:e #1
363 {
364 %<*luatex>
365 \tex_pdfextension:D setmatrix
366 %</luatex>
367 %<*pdftex>
368 \tex_pdfsetmatrix:D
369 %</pdftex>
370 {#1}
371 }
372 % \end{macrocode}
373 % \end{macro}
374 %
375 % \begin{macrocode}
376 %</luatex|pdftex>
377 % \end{macrocode}
378 %
379 % \subsection{\texttt{dvipdfmx} backend}
380 %
381 % \begin{macrocode}
382 %<*dvipdfmx|xetex>
383 % \end{macrocode}
384 %
385 % The \texttt{dvipdfmx} shares code with the PDF mode one (using the common
386 % section to this file) but also with \XeTeX{}. The latter is close
387 % to identical to \texttt{dvipdfmx} and so all of the code here is extracted
388 % for both backends, with some \texttt{clean up} for \XeTeX{} as
389 % required.
390 %
391 % \begin{macro}{\__kernel_backend_literal_pdf:n, \__kernel_backend_literal_pdf:e}
392 % Undocumented but equivalent to \pdfTeX{}'s |literal| keyword. It's similar to
393 % be not the same as the documented |contents| keyword as that adds a |q|/|Q|
394 % pair.
395 % \begin{macrocode}
396 \cs_new_protected:Npn \__kernel_backend_literal_pdf:n #1
397 { \__kernel_backend_literal:n { pdf:literal~ #1 } }
398 \cs_generate_variant:Nn \__kernel_backend_literal_pdf:n { e }
399 % \end{macrocode}
400 % \end{macro}
401 %
402 % \begin{macro}{\__kernel_backend_literal_page:n}
403 % Whilst the manual says this is like |literal direct| in \pdfTeX{},
404 % it closes the |BT| block!
405 % \begin{macrocode}
406 \cs_new_protected:Npn \__kernel_backend_literal_page:n #1
407 { \__kernel_backend_literal:n { pdf:literal~direct~ #1 } }
408 % \end{macrocode}
409 % \end{macro}
410 %
411 % \begin{macro}{\__kernel_backend_scope_begin:, \__kernel_backend_scope_end:}
412 % Scoping is done using the backend-specific specials. We use the versions
413 % originally from \texttt{xdvidfpmx} (\texttt{x:}) as these are well-tested
414 % \enquote{in the wild}.
415 % \begin{macrocode}
416 \cs_new_protected:Npn \__kernel_backend_scope_begin:
417 { \__kernel_backend_literal:n { x:gsave } }
418 \cs_new_protected:Npn \__kernel_backend_scope_end:
419 { \__kernel_backend_literal:n { x:grestore } }
420 % \end{macrocode}
421 % \end{macro}
422 %
423 % \begin{macrocode}
424 %</dvipdfmx|xetex>
425 % \end{macrocode}
426 %
427 % \subsection{\texttt{dvisvgm} backend}
428 %
429 % \begin{macrocode}
430 %<*dvisvgm>
431 % \end{macrocode}
432 %
433 % \begin{macro}{\__kernel_backend_literal_svg:n, \__kernel_backend_literal_svg:e}
434 % Unlike the other backends, the requirements for making SVG files mean
435 % that we can't conveniently transform all operations to the current point.
436 % That makes life a bit more tricky later as that needs to be accounted for.
437 % A new line is added after each call to help to keep the output readable
438 % for debugging.
439 % \begin{macrocode}
440 \cs_new_protected:Npn \__kernel_backend_literal_svg:n #1
441 { \__kernel_backend_literal:n { dvisvgm:raw~ #1 { ?nl } } }
442 \cs_generate_variant:Nn \__kernel_backend_literal_svg:n { e }
443 % \end{macrocode}
444 % \end{macro}
445 %
446 % \begin{variable}{\g__kernel_backend_scope_int, \l__kernel_backend_scope_int}
447 % In SVG, we need to track scope nesting as properties attach to scopes; that
448 % requires a pair of \texttt{int} registers.
449 % \begin{macrocode}
450 \int_new:N \g__kernel_backend_scope_int
451 \int_new:N \l__kernel_backend_scope_int
452 % \end{macrocode}
453 % \end{variable}
454 %
455 % \begin{macro}{\__kernel_backend_scope_begin:, \__kernel_backend_scope_end:}
456 % \begin{macro}{\__kernel_backend_scope_begin:n, \__kernel_backend_scope_begin:e}
457 % \begin{macro}{\__kernel_backend_scope:n, \__kernel_backend_scope:e}
458 % In SVG, the need to attach concepts to a scope means we need to be sure we
459 % will close all of the open scopes. That is easiest done if we only need
460 % an outer \enquote{wrapper} \texttt{begin}/\texttt{end} pair, and within
461 % that we apply operations as a simple scoped statements. To keep down the
462 % non-productive groups, we also have a \texttt{begin} version that does take
463 % an argument.
464 % \begin{macrocode}
465 \cs_new_protected:Npn \__kernel_backend_scope_begin:
466 {
467 \__kernel_backend_literal_svg:n { <g> }
468 \int_set_eq:NN
469 \l__kernel_backend_scope_int
470 \g__kernel_backend_scope_int
471 \group_begin:
472 \int_gset:Nn \g__kernel_backend_scope_int { 1 }
473 }
474 \cs_new_protected:Npn \__kernel_backend_scope_end:
475 {
476 \prg_replicate:nn
477 { \g__kernel_backend_scope_int }
478 { \__kernel_backend_literal_svg:n { </g> } }
479 \group_end:
480 \int_gset_eq:NN
481 \g__kernel_backend_scope_int
482 \l__kernel_backend_scope_int
483 }
484 \cs_new_protected:Npn \__kernel_backend_scope_begin:n #1
485 {
486 \__kernel_backend_literal_svg:n { <g ~ #1 > }
487 \int_set_eq:NN
488 \l__kernel_backend_scope_int
489 \g__kernel_backend_scope_int
490 \group_begin:
491 \int_gset:Nn \g__kernel_backend_scope_int { 1 }
492 }
493 \cs_generate_variant:Nn \__kernel_backend_scope_begin:n { e }
494 \cs_new_protected:Npn \__kernel_backend_scope:n #1
495 {
496 \__kernel_backend_literal_svg:n { <g ~ #1 > }
497 \int_gincr:N \g__kernel_backend_scope_int
498 }
499 \cs_generate_variant:Nn \__kernel_backend_scope:n { e }
500 % \end{macrocode}
501 % \end{macro}
502 % \end{macro}
503 % \end{macro}
504 %
505 % \begin{macrocode}
506 %</dvisvgm>
507 % \end{macrocode}
508 %
509 % \begin{macrocode}
510 %</package>
511 % \end{macrocode}
512 %
513 % \end{implementation}
514 %
515 % \PrintIndex