Debian Codegraph @

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

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