| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: l3backend-box.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-box} package\\ Backend box 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-box} implementation} |
| 57 |
% |
| 58 |
% \begin{macrocode} |
| 59 |
%<*package> |
| 60 |
%<@@=box> |
| 61 |
% \end{macrocode} |
| 62 |
% |
| 63 |
% \subsection{\texttt{dvips} backend} |
| 64 |
% |
| 65 |
% \begin{macrocode} |
| 66 |
%<*dvips> |
| 67 |
% \end{macrocode} |
| 68 |
% |
| 69 |
% \begin{macro}{\@@_backend_clip:N} |
| 70 |
% The \texttt{dvips} backend scales all absolute dimensions based on the |
| 71 |
% output resolution selected and any \TeX{} magnification. Thus for any |
| 72 |
% operation involving absolute lengths there is a correction to make. See |
| 73 |
% \texttt{normalscale} from \texttt{special.pro} for the variables, noting |
| 74 |
% that here everything is saved on the stack rather than as a separate |
| 75 |
% variable. Once all of that is done, the actual clipping is trivial. |
| 76 |
% \begin{macrocode} |
| 77 |
\cs_new_protected:Npn \@@_backend_clip:N #1 |
| 78 |
{ |
| 79 |
\__kernel_backend_scope_begin: |
| 80 |
\__kernel_backend_align_begin: |
| 81 |
\__kernel_backend_literal_postscript:n { matrix~currentmatrix } |
| 82 |
\__kernel_backend_literal_postscript:n |
| 83 |
{ Resolution~72~div~VResolution~72~div~scale } |
| 84 |
\__kernel_backend_literal_postscript:n { DVImag~dup~scale } |
| 85 |
\__kernel_backend_literal_postscript:e |
| 86 |
{ |
| 87 |
0 ~ |
| 88 |
\dim_to_decimal_in_bp:n { \box_dp:N #1 } ~ |
| 89 |
\dim_to_decimal_in_bp:n { \box_wd:N #1 } ~ |
| 90 |
\dim_to_decimal_in_bp:n { -\box_ht:N #1 - \box_dp:N #1 } ~ |
| 91 |
rectclip |
| 92 |
} |
| 93 |
\__kernel_backend_literal_postscript:n { setmatrix } |
| 94 |
\__kernel_backend_align_end: |
| 95 |
\hbox_overlap_right:n { \box_use:N #1 } |
| 96 |
\__kernel_backend_scope_end: |
| 97 |
\skip_horizontal:n { \box_wd:N #1 } |
| 98 |
} |
| 99 |
% \end{macrocode} |
| 100 |
% \end{macro} |
| 101 |
% |
| 102 |
% \begin{macro}{\@@_backend_rotate:Nn} |
| 103 |
% \begin{macro}{\@@_backend_rotate_aux:Nn} |
| 104 |
% Rotating using \texttt{dvips} does not require that the box dimensions |
| 105 |
% are altered and has a very convenient built-in operation. Zero rotation |
| 106 |
% must be written as |0| not |-0| so there is a quick test. |
| 107 |
% \begin{macrocode} |
| 108 |
\cs_new_protected:Npn \@@_backend_rotate:Nn #1#2 |
| 109 |
{ \exp_args:NNf \@@_backend_rotate_aux:Nn #1 { \fp_eval:n {#2} } } |
| 110 |
\cs_new_protected:Npn \@@_backend_rotate_aux:Nn #1#2 |
| 111 |
{ |
| 112 |
\__kernel_backend_scope_begin: |
| 113 |
\__kernel_backend_align_begin: |
| 114 |
\__kernel_backend_literal_postscript:e |
| 115 |
{ |
| 116 |
\fp_compare:nNnTF {#2} = \c_zero_fp |
| 117 |
{ 0 } |
| 118 |
{ \fp_eval:n { round ( -(#2) , 5 ) } } ~ |
| 119 |
rotate |
| 120 |
} |
| 121 |
\__kernel_backend_align_end: |
| 122 |
\box_use:N #1 |
| 123 |
\__kernel_backend_scope_end: |
| 124 |
} |
| 125 |
% \end{macrocode} |
| 126 |
% \end{macro} |
| 127 |
% \end{macro} |
| 128 |
% |
| 129 |
% \begin{macro}{\@@_backend_scale:Nnn} |
| 130 |
% The \texttt{dvips} backend once again has a dedicated operation we can |
| 131 |
% use here. |
| 132 |
% \begin{macrocode} |
| 133 |
\cs_new_protected:Npn \@@_backend_scale:Nnn #1#2#3 |
| 134 |
{ |
| 135 |
\__kernel_backend_scope_begin: |
| 136 |
\__kernel_backend_align_begin: |
| 137 |
\__kernel_backend_literal_postscript:e |
| 138 |
{ |
| 139 |
\fp_eval:n { round ( #2 , 5 ) } ~ |
| 140 |
\fp_eval:n { round ( #3 , 5 ) } ~ |
| 141 |
scale |
| 142 |
} |
| 143 |
\__kernel_backend_align_end: |
| 144 |
\hbox_overlap_right:n { \box_use:N #1 } |
| 145 |
\__kernel_backend_scope_end: |
| 146 |
} |
| 147 |
% \end{macrocode} |
| 148 |
% \end{macro} |
| 149 |
% |
| 150 |
% \begin{macrocode} |
| 151 |
%</dvips> |
| 152 |
% \end{macrocode} |
| 153 |
% |
| 154 |
% \subsection{\LuaTeX{} and \pdfTeX{} backends} |
| 155 |
% |
| 156 |
% \begin{macrocode} |
| 157 |
%<*luatex|pdftex> |
| 158 |
% \end{macrocode} |
| 159 |
% |
| 160 |
% \begin{macro}{\@@_backend_clip:N} |
| 161 |
% The general method is to save the current location, define a clipping path |
| 162 |
% equivalent to the bounding box, then insert the content at the current |
| 163 |
% position and in a zero width box. The \enquote{real} width is then made up |
| 164 |
% using a horizontal skip before tidying up. There are other approaches that |
| 165 |
% can be taken (for example using XForm objects), but the logic here shares |
| 166 |
% as much code as possible and uses the same conversions (and so same |
| 167 |
% rounding errors) in all cases. |
| 168 |
% \begin{macrocode} |
| 169 |
\cs_new_protected:Npn \@@_backend_clip:N #1 |
| 170 |
{ |
| 171 |
\__kernel_backend_scope_begin: |
| 172 |
\__kernel_backend_literal_pdf:e |
| 173 |
{ |
| 174 |
0~ |
| 175 |
\dim_to_decimal_in_bp:n { -\box_dp:N #1 } ~ |
| 176 |
\dim_to_decimal_in_bp:n { \box_wd:N #1 } ~ |
| 177 |
\dim_to_decimal_in_bp:n { \box_ht:N #1 + \box_dp:N #1 } ~ |
| 178 |
re~W~n |
| 179 |
} |
| 180 |
\hbox_overlap_right:n { \box_use:N #1 } |
| 181 |
\__kernel_backend_scope_end: |
| 182 |
\skip_horizontal:n { \box_wd:N #1 } |
| 183 |
} |
| 184 |
% \end{macrocode} |
| 185 |
% \end{macro} |
| 186 |
% |
| 187 |
% \begin{macro}{\@@_backend_rotate:Nn} |
| 188 |
% \begin{macro}{\@@_backend_rotate_aux:Nn} |
| 189 |
% \begin{variable}{\l_@@_backend_cos_fp, \l_@@_backend_sin_fp} |
| 190 |
% Rotations are set using an affine transformation matrix which therefore |
| 191 |
% requires sine/cosine values not the angle itself. We store the rounded |
| 192 |
% values to avoid rounding twice. There are also a couple of comparisons to |
| 193 |
% ensure that |-0| is not written to the output, as this avoids any issues |
| 194 |
% with problematic display programs. Note that numbers are compared to~$0$ |
| 195 |
% after rounding. |
| 196 |
% \begin{macrocode} |
| 197 |
\cs_new_protected:Npn \@@_backend_rotate:Nn #1#2 |
| 198 |
{ \exp_args:NNf \@@_backend_rotate_aux:Nn #1 { \fp_eval:n {#2} } } |
| 199 |
\cs_new_protected:Npn \@@_backend_rotate_aux:Nn #1#2 |
| 200 |
{ |
| 201 |
\__kernel_backend_scope_begin: |
| 202 |
\box_set_wd:Nn #1 { 0pt } |
| 203 |
\fp_set:Nn \l_@@_backend_cos_fp { round ( cosd ( #2 ) , 5 ) } |
| 204 |
\fp_compare:nNnT \l_@@_backend_cos_fp = \c_zero_fp |
| 205 |
{ \fp_zero:N \l_@@_backend_cos_fp } |
| 206 |
\fp_set:Nn \l_@@_backend_sin_fp { round ( sind ( #2 ) , 5 ) } |
| 207 |
\__kernel_backend_matrix:e |
| 208 |
{ |
| 209 |
\fp_use:N \l_@@_backend_cos_fp \c_space_tl |
| 210 |
\fp_compare:nNnTF \l_@@_backend_sin_fp = \c_zero_fp |
| 211 |
{ 0~0 } |
| 212 |
{ |
| 213 |
\fp_use:N \l_@@_backend_sin_fp |
| 214 |
\c_space_tl |
| 215 |
\fp_eval:n { -\l_@@_backend_sin_fp } |
| 216 |
} |
| 217 |
\c_space_tl |
| 218 |
\fp_use:N \l_@@_backend_cos_fp |
| 219 |
} |
| 220 |
\box_use:N #1 |
| 221 |
\__kernel_backend_scope_end: |
| 222 |
} |
| 223 |
\fp_new:N \l_@@_backend_cos_fp |
| 224 |
\fp_new:N \l_@@_backend_sin_fp |
| 225 |
% \end{macrocode} |
| 226 |
% \end{variable} |
| 227 |
% \end{macro} |
| 228 |
% \end{macro} |
| 229 |
% |
| 230 |
% \begin{macro}{\@@_backend_scale:Nnn} |
| 231 |
% The same idea as for rotation but without the complexity of signs and |
| 232 |
% cosines. |
| 233 |
% \begin{macrocode} |
| 234 |
\cs_new_protected:Npn \@@_backend_scale:Nnn #1#2#3 |
| 235 |
{ |
| 236 |
\__kernel_backend_scope_begin: |
| 237 |
\__kernel_backend_matrix:e |
| 238 |
{ |
| 239 |
\fp_eval:n { round ( #2 , 5 ) } ~ |
| 240 |
0~0~ |
| 241 |
\fp_eval:n { round ( #3 , 5 ) } |
| 242 |
} |
| 243 |
\hbox_overlap_right:n { \box_use:N #1 } |
| 244 |
\__kernel_backend_scope_end: |
| 245 |
} |
| 246 |
% \end{macrocode} |
| 247 |
% \end{macro} |
| 248 |
% |
| 249 |
% \begin{macrocode} |
| 250 |
%</luatex|pdftex> |
| 251 |
% \end{macrocode} |
| 252 |
% |
| 253 |
% \subsection{\texttt{dvipdfmx}/\XeTeX{} backend} |
| 254 |
% |
| 255 |
% \begin{macrocode} |
| 256 |
%<*dvipdfmx|xetex> |
| 257 |
% \end{macrocode} |
| 258 |
% |
| 259 |
% \begin{macro}{\@@_backend_clip:N} |
| 260 |
% The code here is identical to that for \LuaTeX{}/\pdfTeX{}: unlike rotation and |
| 261 |
% scaling, there is no higher-level support in the backend for clipping. |
| 262 |
% \begin{macrocode} |
| 263 |
\cs_new_protected:Npn \@@_backend_clip:N #1 |
| 264 |
{ |
| 265 |
\__kernel_backend_scope_begin: |
| 266 |
\__kernel_backend_literal_pdf:e |
| 267 |
{ |
| 268 |
0~ |
| 269 |
\dim_to_decimal_in_bp:n { -\box_dp:N #1 } ~ |
| 270 |
\dim_to_decimal_in_bp:n { \box_wd:N #1 } ~ |
| 271 |
\dim_to_decimal_in_bp:n { \box_ht:N #1 + \box_dp:N #1 } ~ |
| 272 |
re~W~n |
| 273 |
} |
| 274 |
\hbox_overlap_right:n { \box_use:N #1 } |
| 275 |
\__kernel_backend_scope_end: |
| 276 |
\skip_horizontal:n { \box_wd:N #1 } |
| 277 |
} |
| 278 |
% \end{macrocode} |
| 279 |
% \end{macro} |
| 280 |
% |
| 281 |
% \begin{macro}{\@@_backend_rotate:Nn} |
| 282 |
% \begin{macro}{\@@_backend_rotate_aux:Nn} |
| 283 |
% Rotating in \texttt{dvipdmfx}/\XeTeX{} can be implemented using either PDF or |
| 284 |
% backend-specific code. The former approach however is not \enquote{aware} |
| 285 |
% of the content of boxes: this means that any embedded links would not be |
| 286 |
% adjusted by the rotation. As such, the backend-native approach is preferred: |
| 287 |
% the code therefore is similar (though not identical) to the \texttt{dvips} |
| 288 |
% version (notice the rotation angle here is positive). As for |
| 289 |
% \texttt{dvips}, zero rotation is written as |0| not |-0|. |
| 290 |
% \begin{macrocode} |
| 291 |
\cs_new_protected:Npn \@@_backend_rotate:Nn #1#2 |
| 292 |
{ \exp_args:NNf \@@_backend_rotate_aux:Nn #1 { \fp_eval:n {#2} } } |
| 293 |
\cs_new_protected:Npn \@@_backend_rotate_aux:Nn #1#2 |
| 294 |
{ |
| 295 |
\__kernel_backend_scope_begin: |
| 296 |
\__kernel_backend_literal:e |
| 297 |
{ |
| 298 |
x:rotate~ |
| 299 |
\fp_compare:nNnTF {#2} = \c_zero_fp |
| 300 |
{ 0 } |
| 301 |
{ \fp_eval:n { round ( #2 , 5 ) } } |
| 302 |
} |
| 303 |
\box_use:N #1 |
| 304 |
\__kernel_backend_scope_end: |
| 305 |
} |
| 306 |
% \end{macrocode} |
| 307 |
% \end{macro} |
| 308 |
% \end{macro} |
| 309 |
% |
| 310 |
% \begin{macro}{\@@_backend_scale:Nnn} |
| 311 |
% Much the same idea for scaling: use the higher-level backend operation to allow |
| 312 |
% for box content. |
| 313 |
% \begin{macrocode} |
| 314 |
\cs_new_protected:Npn \@@_backend_scale:Nnn #1#2#3 |
| 315 |
{ |
| 316 |
\__kernel_backend_scope_begin: |
| 317 |
\__kernel_backend_literal:e |
| 318 |
{ |
| 319 |
x:scale~ |
| 320 |
\fp_eval:n { round ( #2 , 5 ) } ~ |
| 321 |
\fp_eval:n { round ( #3 , 5 ) } |
| 322 |
} |
| 323 |
\hbox_overlap_right:n { \box_use:N #1 } |
| 324 |
\__kernel_backend_scope_end: |
| 325 |
} |
| 326 |
% \end{macrocode} |
| 327 |
% \end{macro} |
| 328 |
% |
| 329 |
% \begin{macrocode} |
| 330 |
%</dvipdfmx|xetex> |
| 331 |
% \end{macrocode} |
| 332 |
% |
| 333 |
% \subsection{\texttt{dvisvgm} backend} |
| 334 |
% |
| 335 |
% \begin{macrocode} |
| 336 |
%<*dvisvgm> |
| 337 |
% \end{macrocode} |
| 338 |
% |
| 339 |
% \begin{macro}{\@@_backend_clip:N} |
| 340 |
% \begin{variable}{\g__kernel_clip_path_int} |
| 341 |
% Clipping in SVG is more involved than with other backends. The first issue |
| 342 |
% is that the clipping path must be defined separately from where it is used, |
| 343 |
% so we need to track how many paths have applied. The naming here uses |
| 344 |
% \texttt{l3cp} as the namespace with a number following. Rather than use |
| 345 |
% a rectangular operation, we define the path manually as this allows it to |
| 346 |
% have a depth: easier than the alternative approach of shifting content |
| 347 |
% up and down using scopes to allow for the depth of the \TeX{} box and |
| 348 |
% keep the reference point the same! |
| 349 |
% \begin{macrocode} |
| 350 |
\cs_new_protected:Npn \@@_backend_clip:N #1 |
| 351 |
{ |
| 352 |
\int_gincr:N \g__kernel_clip_path_int |
| 353 |
\__kernel_backend_literal_svg:e |
| 354 |
{ < clipPath~id = " l3cp \int_use:N \g__kernel_clip_path_int " > } |
| 355 |
\__kernel_backend_literal_svg:e |
| 356 |
{ |
| 357 |
< |
| 358 |
path ~ d = |
| 359 |
" |
| 360 |
M ~ 0 ~ |
| 361 |
\dim_to_decimal:n { -\box_dp:N #1 } ~ |
| 362 |
L ~ \dim_to_decimal:n { \box_wd:N #1 } ~ |
| 363 |
\dim_to_decimal:n { -\box_dp:N #1 } ~ |
| 364 |
L ~ \dim_to_decimal:n { \box_wd:N #1 } ~ |
| 365 |
\dim_to_decimal:n { \box_ht:N #1 + \box_dp:N #1 } ~ |
| 366 |
L ~ 0 ~ |
| 367 |
\dim_to_decimal:n { \box_ht:N #1 + \box_dp:N #1 } ~ |
| 368 |
Z |
| 369 |
" |
| 370 |
/> |
| 371 |
} |
| 372 |
\__kernel_backend_literal_svg:n |
| 373 |
{ < /clipPath > } |
| 374 |
% \end{macrocode} |
| 375 |
% In general the SVG set up does not try to transform coordinates to the |
| 376 |
% current point. For clipping we need to do that, so have a transformation |
| 377 |
% here to get us to the right place, and a matching one just before the |
| 378 |
% \TeX{} box is inserted to get things back on track. The clip path needs to |
| 379 |
% come between those two such that if lines up with the current point, as |
| 380 |
% does the \TeX{} box. |
| 381 |
% \begin{macrocode} |
| 382 |
\__kernel_backend_scope_begin:n |
| 383 |
{ |
| 384 |
transform = |
| 385 |
" |
| 386 |
translate ( { ?x } , { ?y } ) ~ |
| 387 |
scale ( 1 , -1 ) |
| 388 |
" |
| 389 |
} |
| 390 |
\__kernel_backend_scope:e |
| 391 |
{ |
| 392 |
clip-path = |
| 393 |
"url ( \c_hash_str l3cp \int_use:N \g__kernel_clip_path_int ) " |
| 394 |
} |
| 395 |
\__kernel_backend_scope:n |
| 396 |
{ |
| 397 |
transform = |
| 398 |
" |
| 399 |
scale ( -1 , 1 ) ~ |
| 400 |
translate ( { ?x } , { ?y } ) ~ |
| 401 |
scale ( -1 , -1 ) |
| 402 |
" |
| 403 |
} |
| 404 |
\box_use:N #1 |
| 405 |
\__kernel_backend_scope_end: |
| 406 |
} |
| 407 |
\int_new:N \g__kernel_clip_path_int |
| 408 |
% \end{macrocode} |
| 409 |
% \end{variable} |
| 410 |
% \end{macro} |
| 411 |
% |
| 412 |
% \begin{macro}{\@@_backend_rotate:Nn} |
| 413 |
% Rotation has a dedicated operation which includes a center-of-rotation |
| 414 |
% optional pair. That can be picked up from the backend syntax, so there is |
| 415 |
% no need to worry about the transformation matrix. |
| 416 |
% \begin{macrocode} |
| 417 |
\cs_new_protected:Npn \@@_backend_rotate:Nn #1#2 |
| 418 |
{ |
| 419 |
\__kernel_backend_scope_begin:e |
| 420 |
{ |
| 421 |
transform = |
| 422 |
" |
| 423 |
rotate |
| 424 |
( \fp_eval:n { round ( -(#2) , 5 ) } , ~ { ?x } , ~ { ?y } ) |
| 425 |
" |
| 426 |
} |
| 427 |
\box_use:N #1 |
| 428 |
\__kernel_backend_scope_end: |
| 429 |
} |
| 430 |
% \end{macrocode} |
| 431 |
% \end{macro} |
| 432 |
% |
| 433 |
% \begin{macro}{\@@_backend_scale:Nnn} |
| 434 |
% In contrast to rotation, we have to account for the current position in this |
| 435 |
% case. That is done using a couple of translations in addition to the scaling |
| 436 |
% (which is therefore done backward with a flip). |
| 437 |
% \begin{macrocode} |
| 438 |
\cs_new_protected:Npn \@@_backend_scale:Nnn #1#2#3 |
| 439 |
{ |
| 440 |
\__kernel_backend_scope_begin:e |
| 441 |
{ |
| 442 |
transform = |
| 443 |
" |
| 444 |
translate ( { ?x } , { ?y } ) ~ |
| 445 |
scale |
| 446 |
( |
| 447 |
\fp_eval:n { round ( -#2 , 5 ) } , |
| 448 |
\fp_eval:n { round ( -#3 , 5 ) } |
| 449 |
) ~ |
| 450 |
translate ( { ?x } , { ?y } ) ~ |
| 451 |
scale ( -1 ) |
| 452 |
" |
| 453 |
} |
| 454 |
\hbox_overlap_right:n { \box_use:N #1 } |
| 455 |
\__kernel_backend_scope_end: |
| 456 |
} |
| 457 |
% \end{macrocode} |
| 458 |
% \end{macro} |
| 459 |
% |
| 460 |
% \begin{macrocode} |
| 461 |
%</dvisvgm> |
| 462 |
% \end{macrocode} |
| 463 |
% |
| 464 |
% \begin{macrocode} |
| 465 |
%</package> |
| 466 |
% \end{macrocode} |
| 467 |
% |
| 468 |
% \end{implementation} |
| 469 |
% |
| 470 |
% \PrintIndex |