Debian Codegraph @

texmf-dist/source/latex-dev/latex-lab/latex-lab-graphic.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: latex-lab-graphic.dtx (C) Copyright 2022-2026 LaTeX Project
4 %
5 % It may be distributed and/or modified under the conditions of the
6 % LaTeX Project Public License (LPPL), either version 1.3c of this
7 % license or (at your option) any later version. The latest version
8 % of this license is in the file
9 %
10 % https://www.latex-project.org/lppl.txt
11 %
12 %
13 % The latex-lab bundle is developed in the LaTeX2e GitHub.
14 % Issues may be reported at
15 %
16 % https://github.com/latex3/latex2e/issues
17 \def\ltlabgraphicdate{2026-04-28}
18 \def\ltlabgraphicversion{0.80i}
19 %
20 %<*driver>
21 \DocumentMetadata{tagging=on,pdfstandard=ua-2}
22 \documentclass{l3in2edoc}
23
24 \EnableCrossrefs
25 \CodelineIndex
26 \begin{document}
27 \DocInput{latex-lab-graphic.dtx}
28 \end{document}
29 %</driver>
30 %
31 % \fi
32 %
33 %
34 % \title{The \textsf{latex-lab-graphic} package\\
35 % Tagging of included graphics }
36 % \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
37 % \date{v\ltlabgraphicversion\ \ltlabgraphicdate}
38 %
39 % \maketitle
40 %
41 % \newcommand{\xt}[1]{\textsl{\textsf{#1}}}
42 % \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}}
43 % \newcommand{\docclass}{document class \marginpar{\raggedright document class
44 % customizations}}
45 %
46 %
47 % \newcommand\tikzname{ti\textit{k}Z}
48 % \begin{documentation}
49 % \begin{abstract}
50 % The following code implements a first draft for the tagging of graphics
51 % included with \cs{includegraphics} and the \env{picture} environment.
52 % \end{abstract}
53 %
54 %
55 % \section{Introduction}
56 %
57 % Tagging of pictures is non trivial.
58 %
59 % \begin{enumerate}
60 % \item Pictures generally can have various purposes and so different \emph{tagging modes} must
61 % be provided:
62 % \begin{itemize}
63 % \item The pictures can be purely \emph{ornamental} and \emph{decorative},
64 % e.g., (always/usually) some page border.
65 % This should normally be tagged as \emph{artifact}, either in a \texttt{artifact}
66 % MC-chunk or as an \texttt{Artifact} structure\footnote{The second option exists only in PDF 2.0}.
67 %
68 % \item The pictures can be \emph{illustrative figures}.
69 % This should normally be tagged as a \texttt{Figure} structure with \emph{alternative text}.
70 %
71 % \item The pictures can represent a symbol. This should be tagged as a \texttt{Span}
72 % structure element
73 % with an \texttt{/ActualText} mapping to some Unicode Codepoint(s)
74 % (or perhaps even directly in the stream with a Span BDC with an \texttt{/ActualText}).
75 %
76 % \item The pictures can also be intended for consumption as normal \emph{text}
77 % For example the todonotes package uses a
78 % \tikzname{} picture to surround the text in a node with some colored frame and
79 % background.
80 % In this case the text (which can contain more elements like lists)
81 % should be tagged as usual and put in an \texttt{Aside} structure while the decorative
82 % elements should be marked up as artifacts.
83 %
84 % \item There are also case where no tagging is wanted, e.g. because the tagging
85 % is done by surrounding commands, in this case the begin/end sockets should be transparent and
86 % do nothing.
87 %
88 % \item And naturally there can be more complicated scenarios with
89 % mixtures of these elements, e.g., some text
90 % in nodes in a \tikzname{} picture can be meaningful
91 % while other nodes still should be tagged as artifact.
92 % \end{itemize}
93 %
94 % \item The various packages that allow to draw pictures uses lots of boxes and
95 % moves them around and that makes is not easy to get the tagging right --
96 % especially with pdflatex where one has to insert the literals at the right time.
97 %
98 % \item When the picture is tagged as \texttt{Figure}, the PDF-UA standards require
99 % an attribute with a \texttt{BBox} key in the structure. That BBox describes
100 % the placement of the picture on the page,
101 % and this typically require some low-level hacking into the picture code to calculate
102 % the values.
103 % \end{enumerate}
104 %
105 % The code here handles directly only the tagging of pictures included with \cs{includegraphics}
106 % and the \env{picture} environment. But the used method and the documentation tries
107 % to stay as general as possible, to make it easy to adapt the code to pictures drawn
108 % with other packages like \pkg{l3draw}, \pkg{tikz}, \pkg{pstricks}, \pkg{luamplibs} or similar packages.
109 %
110 % \section{General implementation needs and ideas}
111 %
112 % \subsection{User interfaces}
113 %
114 % The tagging of pictures can not be done fully automatically: user
115 % have to add alternative text or mark up a picture as an artifact.
116 % It is important that the relevant user interfaces are similar across
117 % all picture/graphic packages to make it
118 % easy for authors to adapt their documents.
119 %
120 % We therefore recommend package authors to implement an interface
121 % a key-value interface like the following.
122 %
123 % \subsubsection{Tagging mode of individual graphics}
124 % To change the tagging mode of an individual graphic, the keys
125 % \texttt{artifact} (decorations), \texttt{alt} (illustrative),
126 % and \texttt{actualtext} (symbol) should be used. The keys \texttt{alt}
127 % (and also \texttt{actualtext}) take a text as argument.
128 %
129 % \begin{verbatim}
130 % \includegraphics[artifact]{example-image}
131 % \begin{tikzpicture}[artifact] ... \end{tikzpicture}
132 % \tikz[artifact]{...}
133 % \begin{picture}[artifact]...\end{picture}
134 %
135 % \includegraphics[alt=example image]{example-image}
136 % \begin{tikzpicture}[alt=\myalttext] ... \end{tikzpicture}
137 % \tikz[alt=example image]{...}
138 % \begin{picture}[alt=example image]...\end{picture}
139 %
140 % \includegraphics[actualtext=A]{example-image-A}
141 % \begin{tikzpicture}[actualtext=A] ... \end{tikzpicture}
142 % \tikz[actualtext=A]{...}
143 % \begin{picture}[actualtext=A]...\end{picture}
144 % \end{verbatim}
145 %
146 % Additionally, a key \texttt{tagging-setup} can be
147 % provided that allows to handle more complicated cases, e.g.,
148 %
149 % \begin{verbatim}
150 % \includegraphics[tagging-setup={off}]{example-image} %no tagging at all
151 % \begin{tikzpicture}[tagging-setup={artifact}] ... \end{tikzpicture}
152 % \begin{tikzpicture}[tagging-setup={text}] ... \end{tikzpicture}
153 % \begin{tikzpicture}[tagging-setup={alt=Water (H20),tag=Formula}] ... \end{tikzpicture}
154 % \end{verbatim}
155 %
156 % This key is fully implemented for \cs{includegraphics} and \env{picture} but only
157 % partially in the \tikzname{} module.
158 %
159 % \subsubsection{Setting the tagging mode for a scope}\label{sec:scope}
160 %
161 % Packages can also provide interfaces to change the tagging mode for all pictures or
162 % nodes in a scope.
163 %
164 % If this is done side-effects on the tagging of other picture
165 % types must be considered:
166 % If the generic sockets declared below are used
167 % (which are then used also by \cs{includegraphics}, \env{picture}
168 % and perhaps more environments) a change of the tagging mode can
169 % affect all pictures types using these sockets in the same scope.
170 %
171 % This means that packages that want to offer \enquote{scope support}
172 % but restrict it to their own picture type should either use
173 % their own sockets modelled after the generic sockets,
174 % or use some specific variable to control the change of the tagging mode.
175 %
176 % With \tikzname{} \enquote{scope support} could be implemented rather easily as it has a setup command anyway.
177 % So with the implementation in latex-lab-tikz, all these work as expected
178 % \begin{verbatim}
179 % \tikzset{artifact}
180 % \tikzset{alt=a text}
181 % \tikzset{actualtext=A}
182 % \tikzset{tagging-setup=text}
183 % \end{verbatim}
184 %
185 % With \cs{includegraphics} the standard \cs{setkeys} can be used:
186 % \begin{verbatim}
187 % \setkeys{Gin}{alt=a text}
188 % \setkeys{Gin}{artifact}
189 % \setkeys{Gin}{actualtext=A}
190 % \end{verbatim}
191 % This will then also affect \env{picture} environments in the scope.
192 %
193 % \subsection{Default tagging mode}
194 %
195 % If none of the keys are used, the picture code must chose a default tagging mode.
196 % The best one depends on the graphic type and
197 % should be chosen as needed and then documented.
198 %
199 % With \cs{includegraphics} we use as default the illustrative mode,
200 % tag it as \texttt{Figure} structure, use the file name as alternative text
201 % and issue a warning that a real alternative text is missing.
202 %
203 % With the \env{picture} enviroment we use as default the illustrative mode
204 % to, but use a fix text as alternative text and issue a warning too.
205 %
206 % With \tikzname{} (a first implementation is currently in latex-lab-tikz),
207 % the default is the text mode, which means that a screen reader will read
208 % the text in the nodes in the order they appear in the code. With this default,
209 % e.g. \cs{todo}'s from the \env{todonotes} are correctly tagged.
210 %
211 %
212 % In this implementation the \texttt{artifact}
213 % key can also be applied on nodes and will remove them from the tagging.
214 % \begin{verbatim}
215 % \begin{tikzpicture}
216 % \node[draw=red](x){Important!};
217 % \node[artifact,fill=blue,anchor=west] at (x.east){\phantom{Ip}};
218 % \end{tikzpicture}
219 % \end{verbatim}
220
221 % \subsection{Sockets, plugs and commands}
222 %
223 % \begin{description}
224 % \item[sockets]
225 % The example implementations make use of up-to five \emph{tagging sockets}.
226 % The sockets are all used by the various graphic codes inside a group:
227 % \begin{itemize}
228 % \item An initialization (\texttt{init}) socket that handles
229 % the key-val argument and setups the tagging mode by switching
230 % the plugs of the other tagging sockets.
231 % \item Two sockets that are used at the \texttt{begin} and \texttt{end} of
232 % the picture and setup the main structure element.
233 % The \texttt{end} socket typically also executes if needed the code to calculate the BBox
234 % attribute and add it to the structure.
235 % After the \texttt{begin} socket tagging is suspended, and before the
236 % \texttt{end} socket resumed.
237 % \item Two sockets for the text mode that are used around places where a
238 % picture contains text. Tagging must be resumed before this sockets
239 % if they should do anything at all.
240 % \end{itemize}
241 %
242 % The sockets take arguments that allows some configuration (e.g. to use
243 % a special command for the BBox calculation) and the plugs are coded so that
244 % they can be used in more than one picture type (they are shared here
245 % between \cs{includegraphics} and \env{picture}) but there is no obligation
246 % to use them in every graphic code.
247 % A package can declare its own sockets and plugs. See \ref{sec:scope} for some discussion
248 % why this can be a good idea.
249 %
250 % \item[plugs]
251 % The main \texttt{begin} and \texttt{end} sockets should normally have
252 % four \emph{plugs}
253 % for the different modes:
254 % \texttt{alt}, \texttt{actualtext}, \texttt{artifact} and \texttt{text}.
255 % To disable tagging, \cs{SuspendTagging} can be used,
256 % alternatively for sockets with zero or one argument,
257 % the predefined \texttt{noop} plug can be assigned,
258 % and for sockets with two arguments (if the socket has been declared with
259 % \cs{NewTaggingSocket} the \texttt{transparent}
260 % socket which lets pass through the
261 % second argument.
262 %
263 % The initialization socket and the texts sockets typically have only one
264 % additional \emph{plug} that is used when tagging is active.
265 %
266 % \item[commands] A command to store the position of a reference
267 % point on the page and a command to calculate the BBox from this reference
268 % point and the size of the picture are needed. The second command must also add the
269 % attribute to the main structure element (how to do this can be seen in the implementation).
270 % These commands are typically specific for a picture type.
271 %
272 %\end{description}
273 %
274 % We now describe how this general principles have be implemented
275 % in the concrete examples of the \cs{includegraphics} command, the \env{picture} environment,
276 % TODO! and a simple environment for l3draw commands.
277 %
278 % \section{Sockets, plugs, commands}
279 %
280 % The code defines the following generic sockets and commands.
281 %
282 % \begin{itemize}
283 % \item \texttt{graphic/init} with the plug \texttt{default}
284 % \item \texttt{graphic/begin} (one argument) with the plugs \texttt{alt},
285 % \texttt{actualtext}, \texttt{artifact}, \texttt{text}, \texttt{off}. The argument
286 % allows to set a default alternative text.
287 % \item \texttt{graphic/end} (two arguments) with the plugs \texttt{alt},
288 % \texttt{actualtext}, \texttt{artifact}, \texttt{text}, \texttt{off}. The first argument
289 % of the socket typically receives the command to calculate the BBox. This command
290 % often contains a savepos command and so has to go before the graphic box. The second
291 % argument allows to insert the box between this command and the tagging commands.%
292 % \item \texttt{graphic/text/begin} no argument, with the plug \texttt{default}
293 % \item \texttt{graphic/text/end} no argument with the plug \texttt{default}
294 % \end{itemize}
295 %
296 % \begin{function}{\l_tag_graphic_mode_tl}
297 % This variable holds the current active graphic mode. It is e.g. used
298 % to test if text should resume tagging.
299 % \end{function}
300 %
301 % \section{Make \cs{includegraphics} tagging aware}
302 %
303 % Tagging of graphics included with \cs{includegraphics} is at a first glance
304 % easier to handle than, e.g., a \tikzname{} graphic, as there is only a simple
305 % box with a picture and no text content to consider.
306 % One would think that adding some structure commands around a box shouldn't pose much problems.
307 %
308 % But things are actually not so easy.
309 %
310 % At first such graphics are inserted into the PDF as XObjects and
311 % there are two ways to add an such an XObject to a structure:
312 % similar to text as a marked content item (by surrounding
313 % it with \cs{tagmcbegin} and \cs{tagmcend}) or by referencing the XObject
314 % with an OBJR object (similar to a link annotation). Which method is more sensible
315 % (and if it actually matters) is unknown.
316 % Currently the first method is used as the second would changes in the backend files.
317 %
318 % At second---and this is actually a \emph{much} bigger problem\footnote{Which shows
319 % also in the amount of code dedicated here only to this problem.}---if the graphic
320 % is tagged as an illustrative picture the
321 % \texttt{Figure} structure element should have an attribute with an \texttt{BBox} entry.
322 % The value of this \texttt{BBox} is an array of four numbers that gives the
323 % coordinates of the left, bottom, right, and top edges
324 % of the structure element’s bounding box on the page.
325 % That is the rectangle that completely encloses
326 % its \emph{visible} content and so has not necessarily the same size as the TeX bounding box:
327 % if \texttt{viewport} or \texttt{trim} is used and the
328 % graphic is not clipped, the visible content can be larger.
329 % It turned out to be extremely tricky to get a sensible result, and there are still
330 % open problems and restrictions.
331 %
332 %
333 % \subsection{The BBox calculation}
334 % The reference point on the page is retrieved with \verb+\tex_savepos:D+ and a property
335 % just before the graphic.
336 %
337 % Getting from this the \texttt{BBox} can be quite straightforward for a graphic that is
338 % used once as is. But graphics can be trimmed, scaled, reflected, rotated and reused
339 % in various ways. These transformations typically involve a mix of \TeX{} commands
340 % that shift a box or change the bounding box and backend commands that insert
341 % a pdfliteral with a transformation matrix. Calculating
342 % the correct \texttt{BBox} in all cases is not possible
343 % without rewriting large parts of the graphics and graphicx packages. Problematic are
344 % \begin{itemize}
345 % \item manipulations through external box commands
346 % (\cs{rotatebox}, \cs{reflectbox}, \cs{scalebox}). Their implementation
347 % do not pass the transformation matrix in a way
348 % that allows to track the changes for the \texttt{BBox}
349 % of an included graphic: sometimes
350 % the values are set to late (after the box is already stored), and often the
351 % values are not grouped and can leak out from earlier uses of the commands.
352 %
353 % \item some combination of keys in the optional argument of \cs{includegraphics}.
354 % Examples are \texttt{origin} and multiple calls to \texttt{scale}
355 % and \texttt{angle}) as they internally call the box commands.
356 % Examples of failing combinations can be found in the test file
357 % \texttt{graphic-faults}.
358 %
359 % \item graphics that are stored in a box and reused: to get the \texttt{BBox}
360 % one has to set a label that stores the position with \cs{pdfsavepos},
361 % and if a box is reused one gets multiply defined labels.
362 % One possible solution here is to make
363 % use of the new delayed \cs{pdfliteral}. It allows to change the label names
364 % in the shipout, but this requires careful tracking the box usages
365 % and so various kernel changes.
366 % \end{itemize}
367 %
368 %
369 % Therefore a correct BBox is currently
370 % implemented only for simple \cs{includegraphics} and the keys
371 % \texttt{viewport}, \texttt{trim},
372 % \texttt{scale} and \texttt{angle} (used at most once).
373 %
374 % Currently not supported are
375 % \begin{itemize}
376 % \item graphics inside \cs{rotatebox}, \cs{reflectbox}, \cs{scalebox}.
377 %
378 % TODO: A new implementation with \texttt{l3graphics} and \texttt{l3box} is
379 % probably needed here.
380 %
381 % \item multiple uses of the \texttt{scale} and \texttt{angle} keys
382 %
383 % \item multiple use of graphics stored in boxes. For such graphics automated tagging
384 % should be probably deactivated when storing the content
385 % and tagging should be added around the \cs{usebox}. (How to
386 % proceed when content is saved in boxes needs generally more testing).
387 % \end{itemize}
388 %
389 %
390 % \subsection{User interface}
391 % As suggested above the code (re)defines keys for \cs{includegraphics}
392 % to add the recommended interfaces \texttt{alt}, \texttt{artifact} and \texttt{actualtext}.
393 % It also offers some keys specific to \cs{includegraphics}:
394 % \begin{description}
395 % \item[\texttt{alt}] This key is already defined in the graphicx package
396 % but redefined here to switch to the illustrative mode and to add its value
397 % as alternative text.
398 % \item[\texttt{actualtext}] This switches to the actualtext mode.
399 % This is useful for small graphics that represent single chars or a short word like a
400 % logo. If \texttt{actualtext} is used, the graphics is not enclosed in \texttt{Figure}
401 % structure but in a \texttt{Span} structure and no \texttt{/BBox} attribute is added.%
402 % \footnote{This in accordance with (the draft of) PDF/UA-2 but violates perhaps PDF/UA-1.}
403 % \item[\texttt{artifact}] This tags the graphic as an artifact.
404 % \item[\texttt{tagging-setup}] This key takes as argument a key-list,
405 % which can contain the following keys:
406 % \begin{description}
407 % \item[\texttt{alt=\meta{text}}] This a second way to tag the graphic as figure with
408 % alternative text. \texttt{alt} without value will tag as figure but not change
409 % the text variable, \texttt{alt=} will empty the text variable (and typically trigger
410 % a warning).
411 % \item[\texttt{actualtext=\meta{text}}] This a second way to tag the graphic as a
412 % symbol with actualtext.
413 % \item[\texttt{artifact}] This a second way to tag the graphic as artifact.
414 % \item[\texttt{text}] This switches to text mode.
415 % \item[\texttt{off}] When used tagging will be stopped completely. It is then the
416 % responsibility of the surrounding code to add appropriate tagging commands.
417 % \item[\texttt{tag=\meta{name}}] This switches to the illustrative mode but uses
418 % \meta{name} as tag name in the structure instead of the default \texttt{Figure}.
419 % This can for example be used to tag an image of a formula with \texttt{Formula}.
420 % \end{description}
421 % \item[\texttt{adjust-BBox}] If the calculated \texttt{/BBox} values are wrong
422 % they can be corrected with this key. It expects four dimensions that are added to the
423 % \texttt{/BBox} values.
424 % \end{description}
425 %
426 % The code also add to the \texttt{debug} key of \cs{DocumentMetadata}
427 % the value \texttt{BBox}.
428 % This adds a half transparent red layer showing the calculated BBox.
429 % \begin{verbatim}
430 % \DocumentMetadata{tagging=on,debug=BBox}
431 % \end{verbatim}
432 %
433 % \subsection{Hooks}
434 % The three key definitions \texttt{alt}, \texttt{actualtext} and \texttt{artifact} used by
435 % \cs{includegraphics} contain hooks, named \texttt{Gin/alt},
436 % \texttt{Gin/actualtext}, and \texttt{Gin/artifact}, respectively.
437 % The first two take two arguments: the (with \verb+\pdf_purify:nN+) purified value of the key
438 % that is also used in the PDF and the raw value of the key.
439 % The hooks are processed even if tagging is not activated.
440 % With them, it is for example possible to store the alternative text:
441 % \begin{verbatim}
442 % \AddToHookWithArguments{Gin/alt}{\gdef\myalttext{#2}}
443 % \includegraphics[alt=Hello World]{example-image-A}
444 % The alt text of the graphic was \myalttext.
445 % \end{verbatim}
446 %
447 % Please note
448 % \begin{itemize}
449 % \item The hooks are also executed if the keys are set with \verb+\setkeys{Gin}+.
450 % \item The hooks are not executed in the picture environment or in tikz pictures
451 % as they use different keys.
452 % \item The hooks are obviously not executed if the keys are not used.
453 % That means they can't be used directly to issue a warning or an error
454 % that an alt text is missing. For this some additional logic is needed, e.g.,
455 % a counter in the \texttt{cmd/includegraphics/before} hook, that allows to identify
456 % the graphics and to store the alt or actual text individually.
457 % \end{itemize}
458 %
459 % \section{Make \env{picture} tagging aware}
460 %
461 % \subsection{User interface}
462 % The original \env{picture} environment doesn't have a optional argument
463 % with key-value value processing.
464 % So the code changes that and then provides the keys already discussed for \cs{includegraphics}.
465 %
466 % \subsection{BBox calculation}
467 %
468 % The BBox calculation is much simpler than for \cs{includegraphics} as the code simply takes
469 % the declared size from the \env{picture} arguments.
470 %
471 % \subsection{Tagging in text mode}
472 %
473 % A \env{picture} can contain \cs{put} commands with text content, tagging this in
474 % text mode could make sense in some cases. But it is not quite clear if one can/should
475 % redefine the \cs{put} command. If text tagging is wanted we suggest to
476 % define a dedicated command along these lines:
477 % \begin{verbatim}
478 % \NewDocumentCommand\picturenode{O{}r()m}
479 % {
480 % \group_begin:
481 % \keys_set:nn{tag/graphic}{#1}
482 % \str_if_eq:VnT\l_tag_graphic_mode_tl {text}
483 % {\tag_resume:n{\picturenode}}
484 % \tag_socket_use:n{graphic/text/begin}
485 % \put(#2){#3}
486 % \tag_socket_use:n{graphic/text/end}
487 % \group_end:
488 % }
489 %
490 % \end{verbatim}
491 %
492 % \section{Make an l3draw environment tagging aware}
493 % The newest l3draw version (in the latex3 github)
494 % has all needed data to define a command to retrieve the BBox
495 % and to build a tagging aware environment. l3draw currently has
496 % no dedicated function to add text, instead one has to store the text
497 % in a box and then reuse it, so similar to the \env{picture} environment
498 % a dedicated command with tagging awareness is suggested.
499 %
500 % A command to calculate the BBox can for example be defined like this
501 % \begin{verbatim}
502 % \cs_new:Npn\draw_tag_bbox_attribute:
503 % {
504 % \tl_set:Ne \g__tag_graphic_lx_tl
505 % {
506 % \dim_to_decimal_in_bp:n
507 % {
508 % \property_ref:een {draw.\int_use:N\g_draw_id_int}{xpos}{0}sp
509 % +
510 % \g_draw_bb_xmin_dim
511 % }
512 % }
513 % \tl_set:Ne \g__tag_graphic_ly_tl
514 % {
515 % \dim_to_decimal_in_bp:n
516 % {
517 % \property_ref:een {draw.\int_use:N\g_draw_id_int}{ypos}{0}sp
518 % +
519 % \g_draw_bb_ymin_dim
520 % }
521 % }
522 % \tl_set:Ne \g__tag_graphic_ux_tl
523 % {
524 % \dim_to_decimal_in_bp:n
525 % {
526 % \property_ref:een {draw.\int_use:N\g_draw_id_int}{xpos}{0}sp
527 % +
528 % \g_draw_bb_xmax_dim
529 % }
530 % }
531 % \tl_set:Ne \g__tag_graphic_uy_tl
532 % {
533 % \dim_to_decimal_in_bp:n
534 % {
535 % \property_ref:een {draw.\int_use:N\g_draw_id_int}{ypos}{0}sp
536 % +
537 % \g_draw_bb_ymax_dim
538 % }
539 % }
540 % \bool_if:NT\l__tag_graphic_debug_bool
541 % {
542 % \__tag_graphic_show_bbox:VVVVne
543 % \g__tag_graphic_lx_tl
544 % \g__tag_graphic_ly_tl
545 % \g__tag_graphic_ux_tl
546 % \g__tag_graphic_uy_tl
547 % {red}
548 % {draw.\int_use:N\g_draw_id_int}
549 % }
550 % \tag_struct_gput:ene
551 % {\tag_get:n{struct_num}}
552 % {attribute}
553 % {
554 % /O /Layout /BBox~
555 % [
556 % \dim_to_decimal_in_bp:n
557 % {
558 % \property_ref:een {draw.\int_use:N\g_draw_id_int}{xpos}{0}sp
559 % +
560 % \g_draw_bb_xmin_dim
561 % }
562 % \c_space_tl
563 % \dim_to_decimal_in_bp:n
564 % {
565 % \property_ref:een {draw.\int_use:N\g_draw_id_int}{ypos}{0}sp
566 % +
567 % \g_draw_bb_ymin_dim
568 % }
569 % \c_space_tl
570 % \dim_to_decimal_in_bp:n
571 % {
572 % \property_ref:een {draw.\int_use:N\g_draw_id_int}{xpos}{0}sp
573 % +
574 % \g_draw_bb_xmax_dim
575 % }
576 % \c_space_tl
577 % \dim_to_decimal_in_bp:n
578 % {
579 % \property_ref:een {draw.\int_use:N\g_draw_id_int}{ypos}{0}sp
580 % +
581 % \g_draw_bb_ymax_dim
582 % }
583 % ]
584 % }
585 % }
586 % \end{verbatim}
587 %
588 % A tagging aware environment can then be defined like this
589 % \begin{verbatim}
590 % \NewDocumentEnvironment{tagged-draw}{O{}}
591 % {\leavevmode
592 % \tag_socket_use:nn{graphic/init}{#1}
593 % \tag_socket_use:nn{graphic/begin}{tagged-draw~environment}
594 % \tag_suspend:n{\draw} %
595 % \draw_begin:\ignorespaces
596 % }
597 % {
598 % \draw_end:
599 % \tag_resume:n{\draw}
600 % \tag_socket_use:nnn{graphic/end}{\draw_tag_bbox_attribute:}{}
601 % }
602 % \end{verbatim}
603 %
604 % And a command for tagged text nodes could look like this
605 % \begin{verbatim}
606 % \cs_new_protected:Npn \draw_text_node:nnn #1#2#3 %#1 keyval, #2 point, #3 text
607 % {
608 % \group_begin:
609 % \keys_set:nn{tag/graphic}{#1}
610 % \str_if_eq:VnT\l_tag_graphic_mode_tl {text}
611 % {\tag_resume:n{\draw_node:nn}}
612 % \tag_socket_use:n{graphic/text/begin}
613 % \hbox_set:Nn \l_tmpa_box{#3}
614 % \draw_box_use:Nn\l_tmpa_box {#2}
615 % \tag_socket_use:n{graphic/text/end}
616 % \group_end:
617 % }
618 % \end{verbatim}
619 % \begin{macrocode}
620 %<@@=tag>
621 %<*package>
622 % \end{macrocode}
623 % \end{documentation}
624 % \begin{implementation}
625 % \section{Implementation}
626 % \begin{macrocode}
627 \ProvidesExplPackage {latex-lab-testphase-graphic} {\ltlabgraphicdate} {\ltlabgraphicversion}
628 {Code related to the tagging of graphics}
629 % \end{macrocode}
630 % a variant
631 % \begin{macrocode}
632 \cs_generate_variant:Nn \tag_socket_use:nn {ne}
633 % \end{macrocode}
634 %
635 % \begin{macro}{\@@_graphic_savepos:n}
636 % this is the command which stores the position. Similar to
637 % zref-savepos it uses two savepos commands
638 % for the case that bidi changes the processing order.
639 % \begin{macrocode}
640 \cs_new_protected:Npn\@@_graphic_savepos:n #1
641 {
642 \tex_savepos:D
643 \property_record:nn{#1}{xpos,ypos,abspage}
644 \tex_savepos:D
645 }
646 \cs_generate_variant:Nn \@@_graphic_savepos:n {e}
647 % \end{macrocode}
648 % \end{macro}
649 %
650 % \subsection{Variables}
651 % \begin{variable}
652 % {
653 % \l_@@_graphic_alt_tl,
654 % \l_@@_graphic_actual_tl,
655 % \l_tag_graphic_mode_tl,
656 % }
657 %
658 % These variables are related to the tagging.
659 % Variables for the alt text, the actualtext and the structure tag.
660 % The variable that holds the tagging mode is public,
661 % so that commands can test for it, and e.g. restart
662 % tagging in text mode.
663 %
664 % \begin{macrocode}
665 \tl_new:N \l_@@_graphic_alt_tl
666 \tl_new:N \l_@@_graphic_actual_tl
667 % \end{macrocode}
668 % \end{variable}
669 %
670 % \begin{variable}{\l_@@_graphic_debug_bool}
671 % A boolean for debug code
672 % \begin{macrocode}
673 \bool_new:N \l_@@_graphic_debug_bool
674 % \end{macrocode}
675 % \end{variable}
676 %
677 % The rest of the variables are related to the BBox calculation in \cs{includegraphics}.
678 % \begin{variable}{\g_@@_graphic_int}
679 % This is used to get unique labels in the savepos code.
680 % \begin{macrocode}
681 \int_new:N\g_@@_graphic_int
682 % \end{macrocode}
683 % \end{variable}
684
685 % \begin{variable}
686 % {
687 % \g_@@_graphic_lx_tl,
688 % \g_@@_graphic_ly_tl,
689 % \g_@@_graphic_ux_tl,
690 % \g_@@_graphic_uy_tl,
691 % \l_@@_graphic_bboxcorr_seq
692 % \l_@@_graphic_bboxcorr_bool
693 % }
694 % This commands will hold the calculated BBox values. Local variables
695 % would probably work too, but global variables can be easier retrieved
696 % in tests and debugging code ...
697 % \begin{macrocode}
698 \tl_new:N \g_@@_graphic_lx_tl
699 \tl_new:N \g_@@_graphic_ly_tl
700 \tl_new:N \g_@@_graphic_ux_tl
701 \tl_new:N \g_@@_graphic_uy_tl
702 \seq_new:N\l_@@_graphic_bboxcorr_seq
703 \bool_new:N\l_@@_graphic_bboxcorr_bool
704 % \end{macrocode}
705 % \end{variable}
706 % \begin{variable}{\l_@@_graphic_currentlabel_tl}
707 % This holds the label name of the savepos.
708 % \begin{macrocode}
709 \tl_new:N \l_@@_graphic_currentlabel_tl
710 % \end{macrocode}
711 % \end{variable}
712 %
713 %
714 % \begin{variable}
715 % {
716 % \l_@@_graphic_sin_fp
717 % ,\l_@@_graphic_cos_fp
718 % ,\l_@@_graphic_scale_fp
719 % ,\l_@@_graphic_lxly_fp
720 % ,\l_@@_graphic_lxuy_fp
721 % ,\l_@@_graphic_uxly_fp
722 % ,\l_@@_graphic_uxuy_fp
723 % ,\l_@@_graphic_ux_fp
724 % ,\l_@@_graphic_ly_fp
725 % ,\l_@@_graphic_lx_fp
726 % ,\l_@@_graphic_uy_fp
727 % ,\l_@@_graphic_trim_ux_fp
728 % ,\l_@@_graphic_trim_ly_fp
729 % ,\l_@@_graphic_trim_lx_fp
730 % ,\l_@@_graphic_trim_uy_fp
731 % }
732 % A bunch of fp-variables (we don't use tl-vars,
733 % to avoid to have to take care about minus signs everywhere)
734 % \begin{macrocode}
735 \fp_new:N\l_@@_graphic_sin_fp
736 \fp_new:N\l_@@_graphic_cos_fp
737 \fp_new:N\l_@@_graphic_lxly_fp
738 \fp_new:N\l_@@_graphic_lxuy_fp
739 \fp_new:N\l_@@_graphic_uxly_fp
740 \fp_new:N\l_@@_graphic_uxuy_fp
741 \fp_new:N\l_@@_graphic_ux_fp
742 \fp_new:N\l_@@_graphic_ly_fp
743 \fp_new:N\l_@@_graphic_lx_fp
744 \fp_new:N\l_@@_graphic_uy_fp
745 % \end{macrocode}
746 % this holds the scale value. Either \cs{Gin@scalex} or (if that is !) \cs{Gin@scaley}
747 % \begin{macrocode}
748 \fp_new:N\l_@@_graphic_scale_fp
749 % \end{macrocode}
750 % the follow variables hold the four trim values (or the equivalent calculated
751 % values if viewport is used.
752 % \begin{macrocode}
753 \fp_new:N\l_@@_graphic_trim_ux_fp
754 \fp_new:N\l_@@_graphic_trim_ly_fp
755 \fp_new:N\l_@@_graphic_trim_lx_fp
756 \fp_new:N\l_@@_graphic_trim_uy_fp
757 % \end{macrocode}
758 % \end{variable}
759 %
760 % \subsection{Tagging sockets}
761 % The sockets can perhaps not be shared between \cs{includegraphics} and \env{picture}
762 % but for now we try to do it, with the exception of the init socket.
763 %
764 % The begin socket takes an argument to allow to pass some configuration.
765 % The end socket takes two argument to allow to calculate the BBox
766 % before outputting the box.
767 % \begin{socketdecl}{tagsupport/graphic/init,tagsupport/graphic/begin,
768 % tagsupport/graphic/end,tagsupport/graphic/text/begin,tagsupport/graphic/graphic/text/end}
769 % \begin{macrocode}
770 \NewTaggingSocket{graphic/init}{1}
771 \NewTaggingSocket{graphic/begin}{1}
772 \NewTaggingSocket{graphic/end}{2}
773 \NewTaggingSocket{graphic/text/begin}{0}
774 \NewTaggingSocket{graphic/text/end}{0}
775 % \end{macrocode}
776 %\end{socketdecl}
777 %
778 % \subsection{Tagging plugs}
779 %
780 % \subsubsection{Initialization of the tagging mode}
781 % \begin{plugdecl}{default (tagsupport/graphic/init),default (tagsupport/graphic/init)}
782 % \begin{macrocode}
783 \NewTaggingSocketPlug{graphic/init}{default}
784 {
785 \keys_set:nn{tag/graphic}{#1}
786 \ExpandArgs{no}\AssignTaggingSocketPlug{graphic/begin}{\l_tag_graphic_mode_tl}
787 \ExpandArgs{no}\AssignTaggingSocketPlug{graphic/end}{\l_tag_graphic_mode_tl}
788 }
789 \AssignTaggingSocketPlug{graphic/init}{default}
790 % \end{macrocode}
791 % \end{plugdecl}
792 %
793 % \subsubsection{Main begin and end sockets}
794 % \begin{plugdecl}{figure (tagsupport/graphic/begin),figure (tagsupport/graphic/end)}
795 % These plugs handle the graphic as a figure.
796 % Around the graphic is a \texttt{Figure} environment which will
797 % use an alt text given in the optional argument and internally tagging is suspended.
798 % The Bbox will be set (after the second compilation) to the size of the declared size.
799 % \changes{v0.80i}{2026-04-28}{Use \cs{pdf_purify:nN} for the alt and actualtext.}
800 % \begin{macrocode}
801 \msg_new:nnn { tag } { alt-text-missing }
802 {
803 Alternative~text~for~graphic~is~missing.\\
804 Using~'#1'~instead.
805 }
806 \NewTaggingSocketPlug{graphic/begin}{alt}
807 {
808 \tag_mc_end_push:
809 \tl_if_empty:NT\l_@@_graphic_alt_tl
810 {
811 \msg_warning:nne{tag}{alt-text-missing}{#1}
812 \pdf_purify:nN {#1}\l_@@_graphic_alt_tl
813 }
814 \tag_struct_begin:n
815 {
816 tag=\UseStructureName{graphic},
817 alt=\l_@@_graphic_alt_tl,
818 }
819 \tag_mc_begin:n{}
820 }
821 \NewTaggingSocketPlug{graphic/end}{alt}
822 {
823 #1
824 #2
825 \tag_mc_end:
826 \tag_struct_end:
827 \tag_mc_begin_pop:n{}
828 }
829 % \end{macrocode}
830 % \end{plugdecl}
831 %
832 % \begin{plugdecl}{actualtext (tagsupport/graphic/begin),actualtext (tagsupport/graphic/end)}
833 % This plug handles the picture as a symbol with an actualtext.
834 % It tags the content as a Span and expects an actualtext.
835 % Internally tagging is suspended.
836 % \begin{macrocode}
837 \NewTaggingSocketPlug{graphic/begin}{actualtext}
838 {
839 \tag_mc_end_push:
840 \tag_struct_begin:n{tag=\UseStructureName{graphic/symbol},actualtext=\l_@@_graphic_actual_tl }
841 \tag_mc_begin:n{}
842 }
843 \NewTaggingSocketPlug{graphic/end}{actualtext}
844 {
845 #2
846 \tag_mc_end:
847 \tag_struct_end:
848 \tag_mc_begin_pop:n{}
849 }
850 % \end{macrocode}
851 % \end{plugdecl}
852 %
853 % \begin{plugdecl}{artifact (tagsupport/graphic/begin),artifact (tagsupport/graphic/end)}
854 % This plug handles the picture as an artifact, as decoration.
855 % So it is surrounded by an artifact MC and internal text does not restart tagging.
856 % \begin{macrocode}
857 \NewTaggingSocketPlug{graphic/begin}{artifact}
858 {
859 \tag_mc_end_push:
860 \tag_mc_begin:n{artifact}
861 }
862 \NewTaggingSocketPlug{graphic/end}{artifact}
863 {
864 #2
865 \tag_mc_end:
866 \tag_mc_begin_pop:n{}
867 }
868 % \end{macrocode}
869 % \end{plugdecl}
870 %
871 % \begin{plugdecl}{text (tagsupport/graphic/begin),text (tagsupport/graphic/end)}
872 % This plug can be used for text tagging.
873 % It basically does the same as the artifact plug. The main reason that it
874 % exists is that it looks more consistent and
875 % that we can test for the plug name and so restart tagging
876 % in places where this is wanted. (tagging can not be resumed inside a tagging hook,
877 % so has to use some external method).
878 % \begin{macrocode}
879 \NewTaggingSocketPlug{graphic/begin}{text}
880 {
881 \tag_mc_end_push:
882 \tag_mc_begin:n{artifact}
883 }
884 \NewTaggingSocketPlug{graphic/end}{text}
885 {
886 #2
887 \tag_mc_end:
888 \tag_mc_begin_pop:n{}
889 }
890 % \end{macrocode}
891 % \end{plugdecl}
892 %
893 % \begin{plugdecl}{off (tagsupport/graphic/begin),off (tagsupport/graphic/end)}
894 % This plug can be used for text tagging.
895 % It basically does the same as the artifact plug. The main reason that it
896 % exists is that it looks more consistent and
897 % that we can test for the plug name and so restart tagging
898 % in places where this is wanted. (tagging can not be resumed inside a tagging hook,
899 % so has to use some external method).
900 % \begin{macrocode}
901 \NewTaggingSocketPlug{graphic/begin}{off}{}
902 \NewTaggingSocketPlug{graphic/end}{off}{#2}
903 % \end{macrocode}
904 % \end{plugdecl}
905
906 %
907 % By default we use the alt plugs
908 % \begin{macrocode}
909 \AssignTaggingSocketPlug{graphic/begin}{alt}
910 \AssignTaggingSocketPlug{graphic/end}{alt}
911 % \end{macrocode}
912 %
913 % \begin{plugdecl}{default (tagsupport/graphic/text/begin),
914 % default (tagsupport/picture/text/end)}
915 % These sockets are used inside the text
916 % plugs and ends the previous mc and restarts it after the text.
917 % Not used by \cs{includegraphics}. Unclear if they can be used by
918 % the \env{picture} environment.
919 % \begin{macrocode}
920 \NewTaggingSocketPlug{graphic/text/begin}{default}
921 {
922 \tag_mc_end:
923 \tag_mc_begin:n{}
924 }
925 \NewTaggingSocketPlug{graphic/text/end}{default}
926 {
927 \tag_mc_end:
928 \tag_mc_begin:n{artifact}
929 }
930 \AssignTaggingSocketPlug{graphic/text/begin}{default}
931 \AssignTaggingSocketPlug{graphic/text/end}{default}
932 % \end{macrocode}
933 % \end{plugdecl}
934 %
935 % \subsection{Generic keys}
936 % The keys are used by picture and \cs{includegraphics} (through the
937 % \texttt{tagging-setup} key
938 % \changes{0.80h}{2025-03-08}{new mode key, that doesn't set the text values.}
939 % \begin{macrocode}
940 \keys_define:nn{tag/graphic}
941 {
942 ,mode .code:n =
943 {
944 \tl_set:Ne\l_tag_graphic_mode_tl{#1}
945 }
946 ,alt .code:n =
947 {
948 \pdf_purify:nN {#1}\l_@@_graphic_alt_tl
949 \tl_set:Nn\l_tag_graphic_mode_tl{alt}
950 }
951 ,artifact .code:n =
952 {
953 \tl_set:Nn\l_tag_graphic_mode_tl{artifact}
954 }
955 ,actualtext .code:n =
956 {
957 \pdf_purify:nN {#1}\l_@@_graphic_actual_tl
958 \tl_set:Nn\l_tag_graphic_mode_tl{actualtext}
959 }
960 ,text .code:n =
961 {
962 \tl_set:Nn\l_tag_graphic_mode_tl{text}
963 }
964 ,off .code:n =
965 {
966 \tl_set:Nn\l_tag_graphic_mode_tl{off}
967 }
968 ,adjust-BBox .code:n =
969 {
970 \bool_set_true:N \l_@@_graphic_bboxcorr_bool
971 \seq_set_split:Nnn\l_@@_graphic_bboxcorr_seq{~}{#1~0pt~0pt~0pt~0pt}
972 }
973 ,
974 tagging-setup .code:n=
975 {
976 \keys_set:nn { tag/graphic }{#1}
977 }
978 % \end{macrocode}
979 % only for legacy. Should perhaps be removed?
980 % \begin{macrocode}
981 ,tag .code:n =
982 {
983 \str_case:nnF {#1}
984 {
985 {artifact}
986 {
987 \tl_set:Nn\l_tag_graphic_mode_tl{artifact}
988 }
989 {false}{\tag_suspend:n{picture}}
990 }
991 {
992 \AssignStructureRole{graphic}{#1}
993 \tl_set:Nn\l_tag_graphic_mode_tl{alt}
994 }
995 }
996 }
997 % \end{macrocode}
998 %
999 % \subsection{Tagging support for \cs{includegraphics}}
1000 %
1001 % \subsubsection{User interface: Additional keys.}
1002 %
1003 % We also ensure that graphicx is loaded for the keyval support.
1004 % At first a command to hold the tagging mode.
1005 % \begin{macrocode}
1006 \tl_new:N \l_tag_graphic_mode_tl
1007 \tl_set:Nn \l_tag_graphic_mode_tl {alt} %TODO think about the right default.
1008 % \end{macrocode}
1009 % \changes{0.80h}{2025-07-28}{Add hooks to key, tagging issue 957}
1010 % \begin{macrocode}
1011 \hook_new_with_args:nn {Gin/alt} {2}
1012 \hook_new:n {Gin/artifact}
1013 \hook_new_with_args:nn {Gin/actualtext}{2}
1014 \AddToHook{package/graphicx/after}[latex-lab]
1015 {
1016 \define@key{Gin}{alt}
1017 {
1018 \pdf_purify:nN {#1}\l_@@_graphic_alt_tl
1019 \exp_args:Nnno\hook_use:nnw {Gin/alt}{2}{\l_@@_graphic_alt_tl}{#1}
1020 \tl_set:Nn\l_tag_graphic_mode_tl {alt}
1021 }
1022 \define@key{Gin}{artifact}[]
1023 {
1024 \hook_use:n{Gin/artifact}
1025 \tl_set:Nn\l_tag_graphic_mode_tl{artifact}
1026 }
1027 \define@key{Gin}{actualtext}
1028 {
1029 \pdf_purify:nN {#1}\l_@@_graphic_actual_tl
1030 \exp_args:Nnno\hook_use:nnw {Gin/actualtext}{1}{\l_@@_graphic_actual_tl}{#1}
1031 \tl_set:Nn\l_tag_graphic_mode_tl{actualtext}
1032 }
1033 \define@key{Gin}{tagging-setup}
1034 {
1035 \keys_set:nn { tag/graphic}{#1}
1036 }
1037 \define@key{Gin}{adjust-BBox}
1038 {
1039 \bool_set_true:N \l_@@_graphic_bboxcorr_bool
1040 \seq_set_split:Nnn\l_@@_graphic_bboxcorr_seq{~}{#1~0pt~0pt~0pt~0pt}
1041 }
1042 % \end{macrocode}
1043 % only for legacy, no longer documented
1044 % \begin{macrocode}
1045 \define@key{Gin}{tag}
1046 {
1047 \str_case:nnF {#1}
1048 {
1049 {artifact}
1050 {
1051 \tl_set:Nn\l_tag_graphic_mode_tl{artifact}
1052 }
1053 {false}{\tag_suspend:n{Gin}}
1054 }
1055 {
1056 \AssignStructureRole{graphic}{#1}
1057 \tl_set:Nn\l_tag_graphic_mode_tl{alt}
1058 }
1059 }
1060 }
1061 \AddToHook{package/graphics/after}[latex-lab]
1062 {\RequirePackage{graphicx}}
1063 % \end{macrocode}
1064 %
1065 %
1066 % \subsubsection{Patching graphics commands}
1067 % All changes are currently done in \cs{Gin@setfile}.
1068 % We mainly have to add the sockets in the right place and
1069 % to rearrange a bit the \cs{ifGin@draft} test.
1070 %
1071 % \begin{macrocode}
1072 \AddToHook{package/graphics/after}
1073 {
1074 \def\Gin@setfile#1#2#3{%
1075 \ifx\\#2\\\Gread@false\fi
1076 \ifGin@bbox\else
1077 \ifGread@
1078 \csname Gread@%
1079 \expandafter\ifx\csname Gread@#1\endcsname\relax
1080 eps%
1081 \else
1082 #1%
1083 \fi
1084 \endcsname{\Gin@base#2}%
1085 \else
1086 \Gin@nosize{#3}%
1087 \fi
1088 \fi
1089 \Gin@viewport@code
1090 \Gin@nat@height\Gin@ury bp%
1091 \advance\Gin@nat@height-\Gin@lly bp%
1092 \Gin@nat@width\Gin@urx bp%
1093 \advance\Gin@nat@width-\Gin@llx bp%
1094 \Gin@req@sizes
1095 \expandafter\ifx\csname Ginclude@#1\endcsname\relax
1096 \Gin@drafttrue
1097 \expandafter\ifx\csname Gread@#1\endcsname\relax
1098 \@latex@error{Can not include graphics of type: #1}\@ehc
1099 \global\expandafter\let\csname Gread@#1\endcsname\@empty
1100 \fi
1101 \fi
1102 \leavevmode
1103 % \end{macrocode}
1104 % Here the tagging begins. We want to catch also the draft box,
1105 % and for luatex tagging must be started before the \cs{setbox}.
1106 % \changes{0.80h}{2025-03-08}{use mode key to avoid to reset the alt key value}
1107 % \begin{macrocode}
1108 \tag_socket_use:ne {graphic/init} {tagging-setup={mode=\l_tag_graphic_mode_tl}}
1109 \tag_socket_use:nn {graphic/begin} {\Gin@base\Gin@ext}
1110 % \end{macrocode}
1111 % We store also the draft box in a box and do not output it directly
1112 % so that we can calculate its BBox too.
1113 % \begin{macrocode}
1114 \ifGin@draft
1115 \setbox\z@
1116 \hb@xt@\Gin@req@width{%
1117 \vrule\hss
1118 \vbox to \Gin@req@height{%
1119 \hrule \@width \Gin@req@width
1120 \vss
1121 \edef\@tempa{#3}%
1122 \rlap{ \ttfamily\expandafter\strip@prefix\meaning\@tempa}%
1123 \vss
1124 \hrule}%
1125 \hss\vrule}%
1126 \else
1127 \@addtofilelist{#3}%
1128 \ProvidesFile{#3}[Graphic~file~(type~#1)]%
1129 \setbox\z@\hbox{\csname Ginclude@#1\endcsname{#3}}%
1130 \dp\z@\z@
1131 \ht\z@\Gin@req@height
1132 \wd\z@\Gin@req@width
1133 \fi
1134 % \end{macrocode}
1135 % This ends the tagging.
1136 % \begin{macrocode}
1137 \tag_socket_use:nnn{graphic/end}
1138 { \Gin@tag@bbox@attribute }
1139 { \box\z@ }
1140 }
1141 }
1142 % \end{macrocode}
1143 %
1144 %
1145 % \subsubsection{Calculating the BBox}
1146 %
1147 % This is the large code part.
1148 %
1149 % \begin{macro}{\@@_graphic_get_trim:}
1150 % Graphics can be trimmed with the trim and the viewport key.
1151 % If the graphic is not clipped the values must be taken into
1152 % account when rotating.
1153 % If viewport is used we have to calculate the trim.
1154 %
1155 % \begin{macrocode}
1156 \cs_new_protected:Npn \@@_graphic_get_trim:
1157 {
1158 \legacy_if:nTF {Gin@clip}
1159 % \end{macrocode}
1160 % Setting to 0 is not strictly needed but looks cleaner.
1161 % \begin{macrocode}
1162 {
1163 \fp_zero:N\l_@@_graphic_trim_lx_fp
1164 \fp_zero:N\l_@@_graphic_trim_ly_fp
1165 \fp_zero:N\l_@@_graphic_trim_ux_fp
1166 \fp_zero:N\l_@@_graphic_trim_uy_fp
1167 }
1168 {
1169 \fp_set:Nn \l_@@_graphic_trim_lx_fp {\l_@@_graphic_scale_fp*\Gin@vllx}
1170 \fp_set:Nn \l_@@_graphic_trim_ly_fp {\l_@@_graphic_scale_fp*\Gin@vlly}
1171 \fp_set:Nn \l_@@_graphic_trim_ux_fp {\l_@@_graphic_scale_fp*\Gin@vurx}
1172 \fp_set:Nn \l_@@_graphic_trim_uy_fp {\l_@@_graphic_scale_fp*\Gin@vury}
1173 \cs_if_exist:NT \Gin@ollx
1174 {
1175 \fp_set:Nn \l_@@_graphic_trim_ux_fp {\l_@@_graphic_scale_fp* (\Gin@ourx-(\Gin@urx)) }
1176 \fp_set:Nn \l_@@_graphic_trim_uy_fp {\l_@@_graphic_scale_fp* (\Gin@oury-(\Gin@ury)) }
1177 }
1178 }
1179 }
1180 % \end{macrocode}
1181 % \end{macro}
1182 %
1183 % \begin{macro}{\@@_graphic_get_scale:}
1184 % \begin{macrocode}
1185 \cs_new_protected:Npn \@@_graphic_get_scale:
1186 {
1187 \fp_set:Nn \l_@@_graphic_scale_fp
1188 {
1189 \str_if_eq:eeTF {\Gin@scalex} { ! }
1190 { \Gin@scaley }
1191 { \Gin@scalex }
1192 }
1193 }
1194 % \end{macrocode}
1195 % \end{macro}
1196 %
1197 % \begin{macro}{\@@_graphic_applyangle:nnnn}
1198 % This takes the current BBox and rotates it according to the use angle.
1199 % This is the most laborious code, as we have to take also the trim values into
1200 % account. We have to compare the values after the rotation to find the right corners
1201 % for the BBox. Not sure, if this is the most effective code,
1202 % the l3draw package has similar code to calculate a rotation,
1203 % this can perhaps be reused ...
1204 % \begin{macrocode}
1205 \cs_new_protected:Npn \@@_graphic_applyangle:nnnn #1#2#3#4 %lx,ly,ux,uy
1206 {
1207 \bool_lazy_and:nnT
1208 {\cs_if_exist_p:N \Grot@angle }
1209 {! \int_compare_p:nNn { \Grot@angle }={0}}
1210 {
1211 \fp_set:Nn \l_@@_graphic_sin_fp { sind(\Grot@angle) }
1212 \fp_set:Nn \l_@@_graphic_cos_fp { cosd(\Grot@angle) }
1213 \fp_set:Nn \l_@@_graphic_lx_fp {#1}
1214 \fp_set:Nn \l_@@_graphic_ly_fp {#2}
1215 \fp_set:Nn \l_@@_graphic_ux_fp {#3}
1216 \fp_set:Nn \l_@@_graphic_uy_fp {#4}
1217 % \end{macrocode}
1218 % get the x coordinates (cos,-sin)
1219 % \begin{macrocode}
1220 \fp_set:Nn\l_@@_graphic_lxly_fp
1221 {
1222 -\l_@@_graphic_trim_lx_fp * \l_@@_graphic_cos_fp
1223 +\l_@@_graphic_trim_ly_fp * \l_@@_graphic_sin_fp
1224 }
1225 \fp_set:Nn\l_@@_graphic_lxuy_fp
1226 {
1227 (-\l_@@_graphic_trim_lx_fp) * \l_@@_graphic_cos_fp
1228 +
1229 (\l_@@_graphic_uy_fp-\l_@@_graphic_ly_fp-\l_@@_graphic_trim_ly_fp)
1230 * (-\l_@@_graphic_sin_fp)
1231 }
1232 \fp_set:Nn\l_@@_graphic_uxly_fp
1233 {
1234 (\l_@@_graphic_ux_fp-\l_@@_graphic_lx_fp-\l_@@_graphic_trim_lx_fp)
1235 * \l_@@_graphic_cos_fp
1236 +
1237 (\l_@@_graphic_trim_ly_fp) * (\l_@@_graphic_sin_fp)
1238 }
1239 \fp_set:Nn\l_@@_graphic_uxuy_fp
1240 {
1241 (\l_@@_graphic_ux_fp-\l_@@_graphic_lx_fp-\l_@@_graphic_trim_lx_fp)
1242 * \l_@@_graphic_cos_fp
1243 +
1244 (\l_@@_graphic_uy_fp-\l_@@_graphic_ly_fp-\l_@@_graphic_trim_ly_fp)
1245 * (-\l_@@_graphic_sin_fp)
1246 }
1247 \tl_gset:Ne\g_@@_graphic_lx_tl
1248 {
1249 \fp_eval:n
1250 {
1251 min
1252 (
1253 \l_@@_graphic_lxly_fp,
1254 \l_@@_graphic_lxuy_fp,
1255 \l_@@_graphic_uxly_fp,
1256 \l_@@_graphic_uxuy_fp,
1257 )
1258 +\l_@@_graphic_lx_fp
1259 +\l_@@_graphic_trim_lx_fp
1260 }
1261 }
1262 \tl_gset:Ne\g_@@_graphic_ux_tl
1263 {
1264 \fp_eval:n
1265 {
1266 max
1267 (
1268 \l_@@_graphic_lxly_fp,
1269 \l_@@_graphic_lxuy_fp,
1270 \l_@@_graphic_uxly_fp,
1271 \l_@@_graphic_uxuy_fp
1272 )
1273 +\l_@@_graphic_lx_fp
1274 +\l_@@_graphic_trim_lx_fp
1275 }
1276 }
1277 % \end{macrocode}
1278 % get the y coordinates (sin,cos)
1279 % \begin{macrocode}
1280 \fp_set:Nn\l_@@_graphic_lxly_fp
1281 {
1282 -\l_@@_graphic_trim_lx_fp * \l_@@_graphic_sin_fp
1283 -\l_@@_graphic_trim_ly_fp * \l_@@_graphic_cos_fp
1284 }
1285 \fp_set:Nn\l_@@_graphic_lxuy_fp
1286 {
1287 - \l_@@_graphic_trim_lx_fp * \l_@@_graphic_sin_fp
1288 +
1289 (\l_@@_graphic_uy_fp-\l_@@_graphic_ly_fp-\l_@@_graphic_trim_ly_fp)
1290 * \l_@@_graphic_cos_fp
1291 }
1292 \fp_set:Nn\l_@@_graphic_uxly_fp
1293 {
1294 (\l_@@_graphic_ux_fp-\l_@@_graphic_lx_fp-\l_@@_graphic_trim_lx_fp)
1295 * \l_@@_graphic_sin_fp
1296 - \l_@@_graphic_trim_ly_fp * \l_@@_graphic_cos_fp
1297 }
1298 \fp_set:Nn\l_@@_graphic_uxuy_fp
1299 {
1300 (\l_@@_graphic_ux_fp-\l_@@_graphic_lx_fp-\l_@@_graphic_trim_lx_fp)
1301 * \l_@@_graphic_sin_fp
1302 +
1303 (\l_@@_graphic_uy_fp-\l_@@_graphic_ly_fp-\l_@@_graphic_trim_ly_fp)
1304 * \l_@@_graphic_cos_fp
1305 }
1306 \tl_gset:Ne\g_@@_graphic_ly_tl
1307 {
1308 \fp_eval:n
1309 {
1310 min
1311 (
1312 \l_@@_graphic_lxly_fp,
1313 \l_@@_graphic_lxuy_fp,
1314 \l_@@_graphic_uxly_fp,
1315 \l_@@_graphic_uxuy_fp
1316 )
1317 + \l_@@_graphic_ly_fp + \l_@@_graphic_trim_ly_fp
1318 }
1319 }
1320 \tl_gset:Ne\g_@@_graphic_uy_tl
1321 {
1322 \fp_eval:n
1323 {
1324 max
1325 (
1326 \l_@@_graphic_lxly_fp,
1327 \l_@@_graphic_lxuy_fp,
1328 \l_@@_graphic_uxly_fp,
1329 \l_@@_graphic_uxuy_fp,
1330 )
1331 + \l_@@_graphic_ly_fp + \l_@@_graphic_trim_ly_fp
1332 }
1333 }
1334 }
1335 }
1336 \cs_generate_variant:Nn\@@_graphic_applyangle:nnnn {VVVV}
1337 % \end{macrocode}
1338 % \end{macro}
1339 % \begin{macro}{\@@_graphic_applycorr:NNNN}
1340 % This command is used to add at the end the correction values. Quite dump ...
1341 % \begin{macrocode}
1342 \cs_new_protected:Npn \@@_graphic_applycorr:NNNN #1 #2 #3 #4
1343 {
1344 \bool_if:NT\l_@@_graphic_bboxcorr_bool
1345 {
1346 \tl_set:Ne #1
1347 {
1348 \fp_eval:n
1349 {
1350 #1
1351 +
1352 \dim_to_decimal_in_bp:n {\seq_item:Nn \l_@@_graphic_bboxcorr_seq {1} }
1353 }
1354 }
1355 \tl_set:Ne #2
1356 {
1357 \fp_eval:n
1358 {
1359 #2
1360 +
1361 \dim_to_decimal_in_bp:n {\seq_item:Nn \l_@@_graphic_bboxcorr_seq {2} }
1362 }
1363 }
1364 \tl_set:Ne #3
1365 {
1366 \fp_eval:n
1367 {
1368 #3
1369 +
1370 \dim_to_decimal_in_bp:n {\seq_item:Nn \l_@@_graphic_bboxcorr_seq {3} }
1371 }
1372 }
1373 \tl_set:Ne #4
1374 {
1375 \fp_eval:n
1376 {
1377 #4
1378 +
1379 \dim_to_decimal_in_bp:n {\seq_item:Nn \l_@@_graphic_bboxcorr_seq {4} }
1380 }
1381 }
1382 }
1383 }
1384 % \end{macrocode}
1385 % \end{macro}
1386 %
1387 % \begin{macro}[no-user-doc]{\Gin@tag@bbox@attribute}
1388 % This is the main command to calculate and set the Bbox attribute of the
1389 % \cs{includegraphics} command. It also sets the reference point on the page.
1390 %
1391 % \begin{macrocode}
1392 \cs_new_protected:Npn \Gin@tag@bbox@attribute
1393 {
1394 \__tag_graphic_get_scale:
1395 \@@_graphic_get_trim:
1396 \int_gincr:N\g_@@_graphic_int
1397 \tl_set:Ne\l_@@_graphic_currentlabel_tl {_@@_graphic.\int_use:N \g_@@_graphic_int}
1398 \@@_graphic_savepos:e { \l_@@_graphic_currentlabel_tl }
1399 \tl_gset:Ne\g_@@_graphic_lx_tl
1400 {
1401 \dim_to_decimal_in_bp:n
1402 { \property_ref:een {\l_@@_graphic_currentlabel_tl}{xpos}{0}sp }
1403 }
1404 \tl_gset:Ne\g_@@_graphic_ly_tl
1405 {
1406 \dim_to_decimal_in_bp:n
1407 { \property_ref:een {\l_@@_graphic_currentlabel_tl}{ypos}{0}sp }
1408 }
1409 \tl_gset:Ne\g_@@_graphic_ux_tl
1410 {
1411 \fp_eval:n
1412 {
1413 \g_@@_graphic_lx_tl
1414 +
1415 \dim_to_decimal_in_bp:n { \Gin@req@width }
1416 }
1417 }
1418 \tl_gset:Ne\g_@@_graphic_uy_tl
1419 {
1420 \fp_eval:n
1421 {
1422 \g_@@_graphic_ly_tl
1423 +
1424 \dim_to_decimal_in_bp:n { \Gin@req@height }
1425 }
1426 }
1427 % \end{macrocode}
1428 % If the graphics is not clipped we must add the trim values.
1429 % \begin{macrocode}
1430 \legacy_if:nF {Gin@clip}
1431 {
1432 \tl_gset:Ne\g_@@_graphic_ux_tl
1433 {
1434 \fp_eval:n
1435 {
1436 \g_@@_graphic_ux_tl
1437 +
1438 \l_@@_graphic_trim_ux_fp
1439 }
1440 }
1441 \tl_gset:Ne\g_@@_graphic_lx_tl
1442 {
1443 \fp_eval:n
1444 {
1445 \g_@@_graphic_lx_tl
1446 -
1447 \l_@@_graphic_trim_lx_fp
1448 }
1449 }
1450 \tl_gset:Ne\g_@@_graphic_uy_tl
1451 {
1452 \fp_eval:n
1453 {
1454 \g_@@_graphic_uy_tl
1455 +
1456 \l_@@_graphic_trim_uy_fp
1457 }
1458 }
1459 \tl_gset:Ne\g_@@_graphic_ly_tl
1460 {
1461 \fp_eval:n
1462 {
1463 \g_@@_graphic_ly_tl
1464 -
1465 \l_@@_graphic_trim_ly_fp
1466 }
1467 }
1468 }
1469 % \end{macrocode}
1470 % If there is an angle we now rotate the values.
1471 % \begin{macrocode}
1472 \@@_graphic_applyangle:VVVV
1473 \g_@@_graphic_lx_tl
1474 \g_@@_graphic_ly_tl
1475 \g_@@_graphic_ux_tl
1476 \g_@@_graphic_uy_tl
1477 % \end{macrocode}
1478 % At last we have to add the correction values
1479 % \begin{macrocode}
1480 \@@_graphic_applycorr:NNNN
1481 \g_@@_graphic_lx_tl
1482 \g_@@_graphic_ly_tl
1483 \g_@@_graphic_ux_tl
1484 \g_@@_graphic_uy_tl
1485 % \end{macrocode}
1486 % \begin{macrocode}
1487 \bool_if:NT\l_@@_graphic_debug_bool
1488 {
1489 \@@_graphic_show_bbox:VVVVne
1490 \g_@@_graphic_lx_tl
1491 \g_@@_graphic_ly_tl
1492 \g_@@_graphic_ux_tl
1493 \g_@@_graphic_uy_tl
1494 {red}
1495 {\l_@@_graphic_currentlabel_tl}
1496 }
1497 % \end{macrocode}
1498 % Now we add the attribute. We do it manually as it had to be delayed until now.
1499 % The structure and the mc must be open earlier, before the \cs{setbox} (at least
1500 % for luatex it has to).
1501 % \begin{macrocode}
1502 \tag_struct_gput:ene{\tag_get:n{struct_num}}{attribute}
1503 {
1504 /O /Layout /BBox~
1505 [
1506 \g_@@_graphic_lx_tl\c_space_tl
1507 \g_@@_graphic_ly_tl\c_space_tl
1508 \g_@@_graphic_ux_tl\c_space_tl
1509 \g_@@_graphic_uy_tl
1510 ]
1511 }
1512 }
1513 % \end{macrocode}
1514 % \end{macro}
1515 %
1516 % \subsection{Support for the picture environment}
1517 %
1518 % \subsubsection{User interface}
1519 %
1520 % The original picture has no key-val argument yet. In the new optional
1521 % argument uses the generic tag/graphic keys.
1522 % We could perhaps use the \texttt{Gin} keys instead, but there could
1523 % be side-effects if some uses other \texttt{Gin} keys like
1524 % \texttt{angle}, so better stay on the safe side.
1525 %
1526 %
1527 % \subsubsection{Calculation of the BBox}
1528 %
1529 % \begin{macro}[no-user-doc]{\picture@tag@bbox@attribute}
1530 % Picture needs a similar command to calculate the BBox. But here we stay simple
1531 % and use simply the size of the picbox.
1532 %
1533 % \begin{macrocode}
1534 \newcommand\picture@tag@bbox@attribute
1535 {
1536 \int_gincr:N\g_@@_graphic_int
1537 \tl_set:Ne\l_@@_graphic_currentlabel_tl {_@@_graphic.\int_use:N \g_@@_graphic_int}
1538 \@@_graphic_savepos:e { \l_@@_graphic_currentlabel_tl }
1539 \tl_gset:Ne \g_@@_graphic_lx_tl
1540 {
1541 \dim_to_decimal_in_bp:n
1542 { \property_ref:een {\l_@@_graphic_currentlabel_tl}{xpos}{0}sp }
1543 }
1544 \tl_gset:Ne \g_@@_graphic_ly_tl
1545 {
1546 \dim_to_decimal_in_bp:n
1547 { \property_ref:een {\l_@@_graphic_currentlabel_tl}{ypos}{0}sp - \dp\@picbox }
1548 }
1549 \tl_gset:Ne \g_@@_graphic_ux_tl
1550 {
1551 \dim_to_decimal_in_bp:n
1552 {
1553 \g_@@_graphic_lx_tl bp + \wd\@picbox
1554 }
1555 }
1556 \tl_gset:Ne \g_@@_graphic_uy_tl
1557 {
1558 \dim_to_decimal_in_bp:n
1559 {
1560 \g_@@_graphic_ly_tl bp + \ht\@picbox + \dp\@picbox
1561 }
1562 }
1563 \@@_graphic_applycorr:NNNN
1564 \g_@@_graphic_lx_tl
1565 \g_@@_graphic_ly_tl
1566 \g_@@_graphic_ux_tl
1567 \g_@@_graphic_uy_tl
1568 \bool_if:NT\l_@@_graphic_debug_bool
1569 {
1570 \@@_graphic_show_bbox:VVVVne
1571 \g_@@_graphic_lx_tl
1572 \g_@@_graphic_ly_tl
1573 \g_@@_graphic_ux_tl
1574 \g_@@_graphic_uy_tl
1575 {red}
1576 {\l_@@_graphic_currentlabel_tl}
1577 }
1578 % \end{macrocode}
1579 % this stores the attribute in the structure.
1580 % \begin{macrocode}
1581 \tag_struct_gput:ene{\tag_get:n{struct_num}}{attribute}
1582 {
1583 /O /Layout /BBox~
1584 [
1585 \g_@@_graphic_lx_tl\c_space_tl
1586 \g_@@_graphic_ly_tl\c_space_tl
1587 \g_@@_graphic_ux_tl\c_space_tl
1588 \g_@@_graphic_uy_tl
1589 ]
1590 }
1591 }
1592 % \end{macrocode}
1593 % \end{macro}
1594 %
1595 % \subsubsection{Patching the commands}
1596 % We redefine \cs{picture} to accept an optional argument.
1597 % We also ensure that we are in
1598 % hmode, so that stopping tagging doesn't confuse the paratags.
1599 % \begin{macrocode}
1600 \RenewDocumentCommand\picture{O{}m}
1601 {
1602 \leavevmode
1603 \tag_socket_use:nn{graphic/init}{#1}
1604 \pictur@#2
1605 }
1606 % \end{macrocode}
1607 % inside the picture box we stop tagging.
1608 % \begin{macrocode}
1609 \def\@picture(#1,#2)(#3,#4){%
1610 \@defaultunitsset\@picht{#2}\unitlength
1611 \@defaultunitsset\@tempdimc{#1}\unitlength
1612 \tag_socket_use:nn{graphic/begin}{picture~environment}
1613 \tag_suspend:n{\@picture} %do not tag inside the picture box
1614 \setbox\@picbox\hb@xt@\@tempdimc\bgroup
1615 \@defaultunitsset\@tempdimc{#3}\unitlength
1616 \hskip -\@tempdimc
1617 \@defaultunitsset\@tempdimc{#4}\unitlength
1618 \lower\@tempdimc\hbox\bgroup
1619 \ignorespaces}
1620 % \end{macrocode}
1621 %
1622 % \begin{macrocode}
1623 \def\endpicture{%
1624 \egroup\hss\egroup
1625 \ht\@picbox\@picht\dp\@picbox\z@
1626 \tag_resume:n{\@picture}
1627 \tag_socket_use:nnn{graphic/end}
1628 {\picture@tag@bbox@attribute}
1629 {\mbox{\box\@picbox}}}
1630 % \end{macrocode}
1631 %
1632 %
1633 % \subsection{Debugging code}
1634 % This command puts a transparent layer in the size of the BBox over an graphic.
1635 % \begin{macro}{\@@_graphic_show_bbox:nnnnnn}
1636 % \begin{macrocode}
1637 \cs_new_protected:Npn \@@_graphic_show_bbox:nnnnnn #1#2#3#4#5#6%#5 color, #6 label name
1638 {
1639 \iow_log:n {tag/graphic~debug:~BBox~of~graphics~#6~is~#1~#2~#3~#4}
1640 \hook_gput_code:nnn
1641 {shipout/foreground}
1642 {tag/graphic}
1643 {
1644 \int_compare:nNnT
1645 {\g_shipout_readonly_int}
1646 =
1647 {\property_ref:een{#6}{abspage}{0}}
1648 {
1649 \put
1650 (#1 bp,\dim_eval:n{-\paperheight + \dim_eval:n{#2 bp}})
1651 {
1652 \opacity_select:n{0.5}\color_select:n{#5}
1653 \rule
1654 {\dim_eval:n {#3 bp-\dim_eval:n{#1 bp}}}
1655 {\dim_eval:n {#4 bp-\dim_eval:n{#2 bp}}}
1656 }
1657 }
1658 }
1659 }
1660 \cs_generate_variant:Nn \@@_graphic_show_bbox:nnnnnn {VVVVne,nnnnne}
1661 % \end{macrocode}
1662 % \end{macro}
1663
1664 % \begin{macrocode}
1665 %</package>
1666 % \end{macrocode}
1667 % \end{implementation}