Debian Codegraph @

texmf-dist/source/latex/tagpdf/tagpdf.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: tagpdf.dtx
4 %
5 % Copyright (C) 2019-2026 Ulrike Fischer
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "tagpdf bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/tagpdf
22 %
23 % for those people who are interested.
24 %<*driver>
25 \DocumentMetadata{tagging=on}
26 \documentclass{l3in2edoc}
27 \usepackage{array,booktabs,caption}
28 \hypersetup{pdfauthor=Ulrike Fischer,
29 pdftitle=tagpdf (main module)}
30 \input{tagpdf-docelements}
31 \begin{document}
32 \DocInput{\jobname.dtx}
33 \end{document}
34 %</driver>
35 % \fi
36 % \title{^^A
37 % The \pkg{tagpdf} main module
38 % }
39 %
40 % \author{^^A
41 % Ulrike Fischer\thanks
42 % {^^A
43 % E-mail:
44 % \href{mailto:[email protected]}
45 % {[email protected]}^^A
46 % }^^A
47 % }
48 %
49 % \date{Version 1.0c, released 2026-05-17}
50 % \maketitle%
51 %
52 % \begin{documentation}
53 % \begin{function}{\tag_suspend:n, \tag_resume:n,\tag_stop:n, \tag_start:n }%
54 % \begin{syntax}
55 % \cs{tag_suspend:n} \Arg{label}\\
56 % \cs{tag_resume:n} \Arg{label}\\
57 % \cs{tag_stop:n} \Arg{label} (\emph{deprecated})\\
58 % \cs{tag_start:n} \Arg{label} (\emph{deprecated})
59 % \end{syntax}
60 % We need commands to stop tagging in some places.
61 % They switches three local booleans and also stop the counting
62 % of paragraphs. If they are nested an inner \cs{tag_resume:n} will not
63 % restart tagging.
64 % \meta{label} is only used in debugging messages to allow to follow the nesting
65 % and to identify which code is disabling the tagging.
66 % The label is not expanded so can be a single token, e.g. |\caption|.
67 % |\tag_suspend:n| and |\tag_resume:n| are the l3-layer variants of
68 % |\SuspendTagging| and |\ResumeTagging| and will be provided by the kernel
69 % in the next release.
70 % \end{function}
71 %
72 % \begin{function}{
73 % \tag_stop:, \tag_start:,
74 % \tagstop, \tagstart }
75 % \emph{deprecated} These are variants of the above commands
76 % without the debuging level. They are now deprecated and it
77 % is recommended to use the kernel command
78 % |\SuspendTagging|, |\ResumeTagging|, |\tag_suspend:n| and |\tag_resume:n|
79 % instead.
80 % \end{function}
81 %
82 %
83 % \begin{setupkeydecl}{activate/spaces}
84 % |activate/spaces| activates the additional parsing needed for
85 % interword spaces. It replaces the deprecated key |interwordspace|.
86 % \end{setupkeydecl}
87 %
88 % \begin{setupkeydecl}{activate/mc,
89 % activate-mc (deprecated),
90 % }
91 % A key to activate the marked content code. It should be used
92 % only in special cases, e.g. for debugging.
93 % \end{setupkeydecl}
94 %
95 % \begin{setupkeydecl}{
96 % activate/tree,
97 % activate-tree (deprecated),
98 % }
99 % This key activates the code that finalize the
100 % various trees. It should be used
101 % only in special cases, e.g. for debugging.
102 % \end{setupkeydecl}
103 % \begin{setupkeydecl}{
104 % activate/struct,
105 % activate-struct (deprecated),
106 % }
107 % This key activates the code for structures.
108 % It should be used
109 % only in special cases, e.g. for debugging.
110 % \end{setupkeydecl}
111 %
112 % \begin{setupkeydecl}{
113 % activate/all,
114 % activate-all (deprecated),
115 % }
116 % This is a meta key for the three previous keys
117 % and is normally what should be used to activate tagging.
118 % \end{setupkeydecl}
119 %
120 %\begin{setupkeydecl}{activate/struct-dest,no-struct-dest (deprecated)}
121 % The key allows to suppress the creation of structure destinations
122 % \end{setupkeydecl}
123 %
124 % \begin{setupkeydecl}{debug/log}
125 % The |debug/log| key takes currently the values |none|, |v|, |vv|, |vvv|, |all|.
126 % More details are in \texttt{tagpdf-checks}.
127 % \end{setupkeydecl}
128 %
129 % \begin{setupkeydecl}{activate/tagunmarked, tagunmarked (deprecated) }
130 % This key allows to set if (in luamode) unmarked text should be
131 % marked up as artifact. The initial value is true.
132 % \end{setupkeydecl}
133 %
134 % \begin{setupkeydecl}{activate/softhyphen}
135 % This key allows to activates automatic handling of hyphens inserted
136 % by hyphenation. It is only used in luamode.
137 % It accepts the following values:
138 % \begin{itemize}
139 % \item \texttt{false} and \texttt{off} deactivates the automatic
140 % handling.
141 % \item \texttt{char} Replaces the hyphens by U+00AD if the font contains this glyph.
142 % Note that in some newer fonts U+00AD is an invisible character and so this could led
143 % to disappearing hyphens.
144 % \item \texttt{artifact} (default value) This keeps the hard hyphen U+002D but
145 % surrounds it by an Artifact-MC. This only works if the document is tagged.
146 % \item \texttt{true}, \texttt{on} do the same as \texttt{artifact}.
147 % \end{itemize}
148 %
149 % If tagging is not activate the softhyphen handling is not activated, by default. The hyphen
150 % stays a hard-hyphen. If is possible to use the \texttt{char} mode with
151 % \begin{verbatim}
152 % \DocumentMetadata{tagging=off,tagging-setup={activate/softhyphen=char},}
153 % \end{verbatim}
154 % As \cs{tagpdfsetup} is deactivated at the begin of the class,
155 % there is no interface to switch in the document.
156 %
157 % If tagging is active the default is \texttt{artifact} mode, this requires that \texttt{tagunmarked}
158 % is active too. The mode can be switched at any time with \cs{tagpdfsetup}.
159 % Switching on and off the softhyphen is a global operation,
160 % a switch between artifact and char more is a local operation.
161 % \end{setupkeydecl}
162 %
163 % \begin{setupkeydecl}{activate/collect-link-artifacts}
164 % PDF/UA requires that link annotations are in a Link (or Reference) structure element even
165 % if there are connected to text that is marked as artifact, as is the text in the
166 % header and footer. Fulfilling this requirement
167 % has sadly the bad side effect that a screen reader will, e.g.,
168 % read suddenly \enquote{Link} between paragraphs when it encounters a header
169 % at a page break. Leaving the annotations untagged gives a better reading but
170 % leads to complains of the UA-validators.
171 % With lualatex it is possible to detect such untagged link annotations and to move them into
172 % a structure at the end of the document, so that they at least no longer interrupt the reading.
173 % The setting is on by default, but can be deactivated (in the preamble) with this key.
174 % It knows currently only the value \texttt{off}.
175 % \end{setupkeydecl}
176 %
177 %
178 % \begin{setupkeydecl}{page/tabsorder, tabsorder (deprecated)}
179 % This sets the tabsorder on a page. The values are |row|, |column|, |structure| (default)
180 % or |none|. Currently this is set more or less globally. More finer control can be
181 % added if needed.
182 % \end{setupkeydecl}
183 %
184 % \begin{function}{tagstruct,tagstructobj,tagabspage,tagmcabs,tagmcid}
185 % These are attributes used by the label/ref system.
186 % \end{function}
187 % \end{documentation}
188 % \begin{implementation}
189 % \section{Initialization and test if pdfmanagement is active.}
190 % \begin{macrocode}
191 %<@@=tag>
192 %<*package>
193 \ProvidesExplPackage {tagpdf} {2026-05-17} {1.0c}
194 { LaTeX kernel code for PDF tagging }
195
196 \IfPDFManagementActiveF
197 {
198 \PackageError{tagpdf}
199 {
200 PDF~resource~management~is~no~active!\MessageBreak
201 tagpdf~will~no~work.
202 }
203 {
204 Activate~it~with \MessageBreak
205 \string\DocumentMetadata{<options>}\MessageBreak
206 before~\string\documentclass
207 }
208 }
209 %</package>
210 % \end{macrocode}
211 %<*debug>
212 % \begin{macrocode}
213 \ProvidesExplPackage {tagpdf-debug} {2026-05-17} {1.0c}
214 { debug code for tagpdf }
215 \@ifpackageloaded{tagpdf}{}{\PackageWarning{tagpdf-debug}{tagpdf~not~loaded,~quitting}\endinput}
216 % \end{macrocode}
217 %</debug>
218 % We map the internal module name \enquote{tag} to \enquote{tagpdf} in messages.
219 % \begin{macrocode}
220 %<*package>
221 \prop_gput:Nnn \g_msg_module_name_prop { tag }{ tagpdf }
222 %</package>
223 % \end{macrocode}
224 % Debug mode has its special mapping:
225 % \begin{macrocode}
226 %<*debug>
227 \prop_gput:Nnn \g_msg_module_type_prop { tag / debug} {}
228 \prop_gput:Nnn \g_msg_module_name_prop { tag / debug }{tagpdf~DEBUG}
229 %</debug>
230 % \end{macrocode}
231 %\section{base package}
232 % To avoid to have to test everywhere if tagpdf has been loaded and is active,
233 % we define a base package with dummy functions
234 % \begin{macrocode}
235 %<*base>
236 \ProvidesExplPackage {tagpdf-base} {2026-05-17} {1.0c}
237 {part of tagpdf - provide base, no-op versions of the user commands }
238 %</base>
239 % \end{macrocode}
240 %\section{Package options}
241 % The boolean is kept for now for compatibility, can go in 2026.
242 % \begin{macrocode}
243 %<*package>
244 \bool_new:N\g_@@_mode_lua_bool
245 \sys_if_engine_luatex:T {\bool_gset_true:N \g_@@_mode_lua_bool}
246 \DeclareOption {luamode} { }
247 \DeclareOption {genericmode}{ }
248 \ProcessOptions
249 % \end{macrocode}
250 % \section{Packages}
251 % To be on the safe side for now, load also the base definitions
252 % \begin{macrocode}
253 \RequirePackage{tagpdf-base}
254 %</package>
255 % \end{macrocode}
256 % The no-op version should behave a near enough to the real code as
257 % possible, so we define a command which a special in the relevant backends:
258 % \begin{macrocode}
259 %<*base>
260 \cs_new_protected:Npn \@@_whatsits: {}
261 \AddToHook{begindocument}
262 {
263 \str_case:onF { \c_sys_backend_str }
264 {
265 { luatex } { \cs_set_protected:Npn \@@_whatsits: {} }
266 { dvisvgm } { \cs_set_protected:Npn \@@_whatsits: {} }
267 }
268 {
269 \cs_set_protected:Npn \@@_whatsits: {\tex_special:D {} }
270 }
271 }
272 %</base>
273 % \end{macrocode}
274 %
275 % \subsection{a LastPage label}
276 % With LaTeX 2025-06-01 we no longer need a special version as the label is now
277 % written directly. To avoid problems with the xr package, we undefine the label command
278 % before reading the aux-file.
279 % \begin{macrocode}
280 %<*package>
281 \AddToHook{begindocument/before}{\cs_undefine:N\r@@@@tag@LastPage}
282 \AddToHook{enddocument/afterlastpage}
283 {\property_record:nn{@tag@LastPage}{abspage,tagmcabs,tagstruct}}
284 % \end{macrocode}
285 %
286 % \section{Variables}
287 % \begin{variable}
288 % {
289 % \l_@@_tmpa_tl,
290 % \l_@@_tmpb_tl,
291 % \l_@@_tmpc_tl,
292 % \l_@@_tmp_unused_tl
293 % \l_@@_Ref_tmpa_tl,
294 % \l_@@_get_tmpc_tl,
295 % \l_@@_get_parent_tmpa_tl,
296 % \l_@@_get_parent_tmpb_tl,
297 % \l_@@_get_parent_tmpc_tl,
298 % \l_@@_get_child_tmpa_tl,
299 % \l_@@_get_child_tmpb_tl,
300 % \l_@@_get_child_tmpc_tl,
301 % \l_@@_tmpa_str,
302 % \l_@@_tmpa_prop,
303 % \l_@@_tmpa_seq,
304 % \l_@@_tmpb_seq,
305 % \l_@@_tmpa_clist,
306 % \l_@@_tmpa_int,
307 % \l_@@_tmpa_box,
308 % \l_@@_tmpb_box
309 % }
310 % A few temporary variables
311 % \begin{macrocode}
312 \tl_new:N \l_@@_tmpa_tl
313 \tl_new:N \l_@@_tmpb_tl
314 \tl_new:N \l_@@_tmpc_tl
315 \tl_new:N \l_@@_tmp_unused_tl
316 \tl_new:N \l_@@_Ref_tmpa_tl
317 \tl_new:N \l_@@_get_tmpc_tl
318 \tl_new:N \l_@@_get_parent_tmpa_tl
319 \tl_new:N \l_@@_get_parent_tmpb_tl
320 \tl_new:N \l_@@_get_parent_tmpc_tl
321 \tl_new:N \l_@@_get_child_tmpa_tl
322 \tl_new:N \l_@@_get_child_tmpb_tl
323 \tl_new:N \l_@@_get_child_tmpc_tl
324 \str_new:N \l_@@_tmpa_str
325 \prop_new:N \l_@@_tmpa_prop
326 \seq_new:N \l_@@_tmpa_seq
327 \seq_new:N \l_@@_tmpb_seq
328 \clist_new:N \l_@@_tmpa_clist
329 \int_new:N \l_@@_tmpa_int
330 \box_new:N \l_@@_tmpa_box
331 \box_new:N \l_@@_tmpb_box
332 % \end{macrocode}
333 % \end{variable}
334 % Attribute lists for the label command. We have a list for
335 % mc-related labels, and one for structures.
336 % \begin{macro}{\c_@@_property_mc_clist,\c_@@_property_struct_clist}
337 % \begin{macrocode}
338 \clist_const:Nn \c_@@_property_mc_clist {tagabspage,tagmcabs,tagmcid}
339 \clist_const:Nn \c_@@_property_struct_clist {tagstruct,tagstructobj}
340 % \end{macrocode}
341 % \end{macro}
342 %
343 % \begin{variable}{\l_@@_loglevel_int}
344 % This integer hold the log-level and so allows to
345 % control the messages.
346 % TODO: a list which log-level shows what is needed. The current behaviour
347 % is quite ad-hoc.
348 % \begin{macrocode}
349 \int_new:N \l_@@_loglevel_int
350 % \end{macrocode}
351 % \end{variable}
352 %
353 % \begin{variable}
354 % {
355 % \g_@@_active_space_bool,
356 % \g_@@_active_mc_bool,
357 % \g_@@_active_tree_bool,
358 % \g_@@_active_struct_bool,
359 % \g_@@_active_struct_dest_bool
360 % }
361 % These booleans should help to control the global behaviour of tagpdf.
362 % Ideally it should more or less do nothing if all are false.
363 % The space-boolean controls the interword space code,
364 % the mc-boolean activates \cs{tag_mc_begin:n},
365 % the tree-boolean activates writing the finish code and the pdfmanagement related
366 % commands, the struct-boolean activates the storing of the structure data.
367 % In a normal document all should be active, the split is only there for
368 % debugging purpose.
369 % Structure destination will be activated automatically,
370 % but with the boolean struct-dest-boolean one can suppress them.
371 % Also we assume currently that they are set only at begin document. But
372 % if some control passing over groups are needed they could be perhaps
373 % used in a document too.
374 % TODO: check if they are used everywhere as needed and as wanted.
375 % \begin{macrocode}
376 \bool_new:N \g_@@_active_space_bool
377 \bool_new:N \g_@@_active_mc_bool
378 \bool_new:N \g_@@_active_tree_bool
379 \bool_new:N \g_@@_active_struct_bool
380 \bool_new:N \g_@@_active_struct_dest_bool
381 \bool_gset_true:N \g_@@_active_struct_dest_bool
382 % \end{macrocode}
383 % \end{variable}
384
385 % \begin{variable}
386 % {
387 % \l_@@_active_mc_bool,
388 % \l_@@_active_struct_bool,
389 % \l_@@_active_socket_bool
390 % }
391 % These booleans should help to control the \emph{local} behaviour of tagpdf.
392 % In some cases it could e.g. be necessary to stop tagging completely.
393 % As local booleans they respect groups.
394 % TODO: check if they are used everywhere as needed and as wanted.
395 % \begin{macrocode}
396 \bool_new:N \l_@@_active_mc_bool
397 \bool_set_true:N \l_@@_active_mc_bool
398 \bool_new:N \l_@@_active_struct_bool
399 \bool_set_true:N \l_@@_active_struct_bool
400 \bool_new:N \l_@@_active_socket_bool
401 % \end{macrocode}
402 % \end{variable}
403 %
404 % \begin{variable}{\g_@@_tagunmarked_bool}
405 % This boolean controls if the code should try to automatically
406 % tag parts not in mc-chunk. It is currently only used in luamode.
407 % It would be possible to used it in generic mode, but this would create
408 % quite a lot empty artifact mc-chunks.
409 % \begin{macrocode}
410 \bool_new:N \g_@@_tagunmarked_bool
411 % \end{macrocode}
412 % \end{variable}
413 %
414 % \begin{variable}{\g_@@_softhyphen_bool}
415 % This boolean controls if the code should try to automatically
416 % handle hyphens from hyphenation. It is currently only used in luamode.
417 % \begin{macrocode}
418 \bool_new:N \g_@@_softhyphen_bool
419 \bool_gset_true:N \g_@@_softhyphen_bool
420 % \end{macrocode}
421 % \end{variable}
422 %
423 % \begin{variable}{\g_@@_unique_cnt_int}
424 % If tagpdf has to create unique names (e.g. for object names when embedding files)
425 % it can use this integer to get an unique name. At every use it should be increased
426 % \begin{macrocode}
427 \int_new:N \g_@@_unique_cnt_int
428 % \end{macrocode}
429 % \end{variable}
430
431 % \section{Variants of l3 commands}
432 % \begin{macrocode}
433 \prg_generate_conditional_variant:Nnn \pdf_object_if_exist:n {e}{T,F,TF}
434 \cs_generate_variant:Nn \pdf_object_ref:n {e}
435 \cs_generate_variant:Nn \pdfannot_dict_put:nnn {nne}
436 \cs_generate_variant:Nn \pdffile_embed_stream:nnn {nee,oee}
437 \cs_generate_variant:Nn \prop_gput:Nnn {Nee,Nen} %** unneeded
438 \cs_generate_variant:Nn \prop_put:Nnn {Nee} %** unneeded
439 \cs_generate_variant:Nn \prop_item:Nn {No,Ne} %** unneeded
440 \cs_generate_variant:Nn \seq_set_split:Nnn{Nno}
441 \cs_generate_variant:Nn \str_set_convert:Nnnn {Nonn, Noon, Nnon }
442 \cs_generate_variant:Nn \clist_map_inline:nn {on}
443 \cs_generate_variant:Nn \pdffile_embed_file:nnn {eee}
444 % \end{macrocode}
445 %
446 % \section{Label and Reference commands}
447 % The code uses mostly the kernel properties but need a few local variants.
448 % \begin{macro}{\@@_property_record:nn}
449 % The command to record a property while preserving the spaces similar to the standard
450 % \cs{label}.
451 % \begin{macrocode}
452 \cs_new_protected:Npn \@@_property_record:nn #1#2
453 {
454 \@bsphack
455 \property_record:nn{#1}{#2}
456 \@esphack
457 }
458
459 % \end{macrocode}
460 % And a few variants
461 % \begin{macrocode}
462 \cs_generate_variant:Nn \property_ref:nnn {enn}
463 \cs_generate_variant:Nn \property_ref:nn {en}
464 \cs_generate_variant:Nn \@@_property_record:nn {en,eo}
465 % \end{macrocode}
466 % \end{macro}
467 %
468 % \begin{macro}{\@@_property_ref_lastpage:nn}
469 % A command to retrieve the lastpage label, this will be adapted when there
470 % is a proper, kernel lastpage label.
471 % \begin{macrocode}
472 \cs_new:Npn \@@_property_ref_lastpage:nn #1 #2
473 {
474 \property_ref:nnn {@tag@LastPage}{#1}{#2}
475 }
476 % \end{macrocode}
477 %\end{macro}
478
479 % \section{Setup label attributes}
480 % \begin{macro}{tagstruct,tagstructobj,tagabspage,tagmcabs,tagmcid}
481 % This are attributes used by the label/ref system.
482 % With structures we store the structure number |tagstruct|
483 % and the object reference |tagstructobj|.
484 % The second is needed to be able to reference a structure which hasn't been
485 % created yet. The alternative would be to create the object in such cases,
486 % but then we would have to check the object existence all the time.
487 %
488 % With mc-chunks we store the absolute page number |tagabspage|,
489 % the absolute id |tagmcabc|, and the id on the page |tagmcid|.
490 % \begin{macrocode}
491 \property_new:nnnn
492 { tagstruct } { now }
493 {1} { \int_use:N \c@g_@@_struct_abs_int }
494 \property_new:nnnn { tagstructobj } { now } {}
495 {
496 \pdf_object_ref_indexed:nn { @@/struct } { \c@g_@@_struct_abs_int }
497 }
498 \property_new:nnnn
499 { tagabspage } { shipout }
500 {0} { \int_use:N \g_shipout_readonly_int }
501 \property_new:nnnn { tagmcabs } { now }
502 {0} { \int_use:N \c@g_@@_MCID_abs_int }
503
504 \flag_new:n { @@/mcid }
505 \property_new:nnnn {tagmcid } { shipout }
506 {0} { \flag_height:n { @@/mcid } }
507
508 % \end{macrocode}
509 % \end{macro}
510 %
511
512 %\section{Commands to fill seq and prop}
513 % With most engines these are simply copies of the expl3 commands,
514 % but luatex will overwrite them, to store the data also in lua tables.
515 % \begin{macro}
516 % {
517 % \@@_prop_new:N
518 % ,\@@_prop_new_linked:N
519 % ,\@@_seq_new:N
520 % ,\@@_prop_gput:Nnn
521 % ,\@@_seq_gput_right:Nn
522 % ,\@@_seq_item:cn
523 % ,\@@_prop_item:cn
524 % ,\@@_seq_show:N
525 % ,\@@_prop_show:N
526 % }
527 % \begin{macrocode}
528 \cs_set_eq:NN \@@_prop_new:N \prop_new:N
529 \cs_set_eq:NN \@@_prop_new_linked:N \prop_new_linked:N
530 \cs_set_eq:NN \@@_seq_new:N \seq_new:N
531 \cs_set_eq:NN \@@_prop_gput:Nnn \prop_gput:Nnn
532 \cs_set_eq:NN \@@_seq_gput_right:Nn \seq_gput_right:Nn
533 \cs_set_eq:NN \@@_seq_gput_left:Nn \seq_gput_left:Nn
534 \cs_set_eq:NN \@@_seq_item:cn \seq_item:cn
535 \cs_set_eq:NN \@@_prop_item:cn \prop_item:cn
536 \cs_set_eq:NN \@@_seq_show:N \seq_show:N
537 \cs_set_eq:NN \@@_prop_show:N \prop_show:N
538 % cnx temporary needed for latex-lab-graphic code
539 \cs_generate_variant:Nn \@@_prop_gput:Nnn { Nen, Nee, Nne, Nno, cnn, cen, cne, cno, cnx}
540 \cs_generate_variant:Nn \@@_seq_gput_right:Nn { Ne , No, cn, ce }
541 \cs_generate_variant:Nn \@@_seq_gput_left:Nn { ce }
542 \cs_generate_variant:Nn \@@_prop_new:N { c }
543 \cs_generate_variant:Nn \@@_seq_new:N { c }
544 \cs_generate_variant:Nn \@@_seq_show:N { c }
545 \cs_generate_variant:Nn \@@_prop_show:N { c }
546 %</package>
547 % \end{macrocode}
548 % \end{macro}
549 %
550 % \section{General tagging commands}
551 %
552 % \begin{macro}{
553 % \tag_suspend:n,\tag_resume:n,
554 % \tag_stop:,\tag_start:,
555 % \tag_stop:n,\tag_start:n}
556 % We need commands to stop tagging in some places.
557 % They switch local booleans and also stop the counting of paragraphs.
558 % The commands keep track of the nesting with a local
559 % counter. Tagging only is only restarted at the outer level,
560 % if the current level is 1.
561 % The commands with argument allow to give a label. This is only used
562 % in debugging messages to allow to follow the nesting. The label is
563 % not expand so can e.g. be a single command token.
564 %
565 % \begin{variable}{\l_@@_tag_stop_int}
566 % When stop/start pairs are nested we do not want the inner
567 % start command to restart tagging. To control this we use a
568 % local int: The stop command will increase it. The starting
569 % will decrease it and only restart tagging, if it is zero.
570 % This will replace the label version.
571 % \begin{macrocode}
572 %<*package|debug>
573 %<package>\int_new:N \l_@@_tag_stop_int
574 % \end{macrocode}
575 % \end{variable}
576 %
577 % \begin{macrocode}
578 \cs_set_protected:Npn \tag_stop:
579 {
580 %<debug> \msg_note:nne {tag / debug }{tag-suspend}{ \int_use:N \l_@@_tag_stop_int }
581 \int_incr:N \l_@@_tag_stop_int
582 \bool_set_false:N \l_@@_active_struct_bool
583 \bool_set_false:N \l_@@_active_mc_bool
584 \bool_set_false:N \l_@@_active_socket_bool
585 \@@_stop_para_ints:
586 }
587 \cs_set_protected:Npn \tag_start:
588 {
589 \int_if_zero:nF { \l_@@_tag_stop_int } { \int_decr:N \l_@@_tag_stop_int }
590 \int_if_zero:nT { \l_@@_tag_stop_int }
591 {
592 \bool_set_true:N \l_@@_active_struct_bool
593 \bool_set_true:N \l_@@_active_mc_bool
594 \bool_set_true:N \l_@@_active_socket_bool
595 \@@_start_para_ints:
596 }
597 %<debug> \msg_note:nne {tag / debug }{tag-resume}{ \int_use:N \l_@@_tag_stop_int }
598 }
599 \cs_set_eq:NN\tagstop\tag_stop:
600 \cs_set_eq:NN\tagstart\tag_start:
601 % \end{macrocode}
602 % \begin{macrocode}
603 \cs_set_protected:Npn \tag_suspend:n #1
604 {
605 %<debug> \msg_note:nnee {tag / debug }{tag-suspend}
606 %<debug> { \int_use:N \l_@@_tag_stop_int }{\exp_not:n{#1}}
607 \int_incr:N \l_@@_tag_stop_int
608 \bool_set_false:N \l_@@_active_struct_bool
609 \bool_set_false:N \l_@@_active_mc_bool
610 \bool_set_false:N \l_@@_active_socket_bool
611 \@@_stop_para_ints:
612 }
613 \cs_set_eq:NN \tag_stop:n \tag_suspend:n
614 \cs_set_protected:Npn \tag_resume:n #1
615 {
616 \int_if_zero:nF { \l_@@_tag_stop_int } { \int_decr:N \l_@@_tag_stop_int }
617 \int_if_zero:nT { \l_@@_tag_stop_int }
618 {
619 \bool_set_true:N \l_@@_active_struct_bool
620 \bool_set_true:N \l_@@_active_mc_bool
621 \bool_set_true:N \l_@@_active_socket_bool
622 \@@_start_para_ints:
623 }
624 %<debug> \msg_note:nnee {tag / debug }{tag-resume}
625 %<debug> { \int_use:N \l_@@_tag_stop_int }{\exp_not:n{#1}}
626 }
627 \cs_set_eq:NN \tag_start:n \tag_resume:n
628 %</package|debug>
629 %<*base>
630 \cs_new_protected:Npn \tag_stop:{}
631 \cs_new_protected:Npn \tag_start:{}
632 \cs_new_protected:Npn \tagstop{}
633 \cs_new_protected:Npn \tagstart{}
634 \cs_new_protected:Npn \tag_stop:n #1 {}
635 \cs_new_protected:Npn \tag_start:n #1 {}
636 % \end{macrocode}
637 % Until the commands are provided by the kernel we provide them here too
638 % \begin{macrocode}
639 \cs_set_eq:NN \tag_suspend:n \tag_stop:n
640 \cs_set_eq:NN \tag_resume:n \tag_start:n
641 %</base>
642 % \end{macrocode}
643 % \end{macro}
644 %
645 % \section{Handling link artifacts}
646 % Links that should be artifacts, e.g. in header or an overlay frame,
647 % can not be simply kept untagged as the validators then complain about the link
648 % annotations that are not in a Link structure.
649 % But tagging them gives a bad reading experience as they can interrupt the text.
650 % Probably long term the best is not to interrupt tagging inside an Artifact
651 % structure and to move these structures to the end of the structure tree to keep them
652 % out of the way.
653 % But with luatex we have an easy option to detect untagged link annotations and so
654 % can move them to the end of the structure tree too and this is done here.
655 %
656 % \begin{macrocode}
657 %<*package>
658 \cs_new_protected:Npn \@@_activate_linkbin:
659 {
660 \sys_if_engine_luatex:T
661 {
662 \tagstructbegin{tag=\UseStructureName{link},stash}
663 \tl_const:Ne \c__tag_struct_link_bin_tl {\int_use:N\c@g__tag_struct_abs_int}%
664 \tagstructend
665 \AddToHook{tagpdf/finish/before}[tagpdf/linkbin]
666 {
667 \par
668 \int_compare:nNnT {\directlua{tex.sprint(ltx.__tag.func.linkbin())}}>{0}
669 {
670 \tagstructbegin{tag=Artifact,title=Ignored~link~annotations}
671 \tag_struct_use_num:o{\c__tag_struct_link_bin_tl}
672 \tagstructend
673 }
674 }
675 }
676 }
677 \AddToHook{begindocument/end}[tagpdf/linkbin]{\@@_activate_linkbin:}
678 %</package>
679 % \end{macrocode}
680 %
681 % \section{Keys for tagpdfsetup}
682 %
683 % TODO: the log-levels must be sorted
684 %
685 % \begin{setupkeydecl}
686 % {
687 % activate/mc,
688 % activate/tree,
689 % activate/struct,
690 % activate/all,
691 % activate/struct-dest,
692 % }
693 % Keys to (globally) activate tagging.
694 % |activate/spaces| activates the additional parsing needed for
695 % interword spaces. It is defined in tagpdf-space.
696 % |activate/struct-dest| allows to activate or suppress structure destinations.
697 % \begin{macrocode}
698 %<*package>
699 \keys_define:nn { @@ / setup }
700 {
701 activate/mc .bool_gset:N = \g_@@_active_mc_bool,
702 activate/tree .bool_gset:N = \g_@@_active_tree_bool,
703 activate/struct .bool_gset:N = \g_@@_active_struct_bool,
704 activate/all .meta:n =
705 {activate/mc={#1},activate/tree={#1},activate/struct={#1}},
706 activate/all .default:n = true,
707 activate/struct-dest .bool_gset:N = \g_@@_active_struct_dest_bool,
708 % \end{macrocode}
709 % old, deprecated names
710 % \begin{macrocode}
711 activate-mc .bool_gset:N = \g_@@_active_mc_bool,
712 activate-tree .bool_gset:N = \g_@@_active_tree_bool,
713 activate-struct .bool_gset:N = \g_@@_active_struct_bool,
714 activate-all .meta:n =
715 {activate/mc={#1},activate/tree={#1},activate/struct={#1}},
716 activate-all .default:n = true,
717 no-struct-dest .bool_gset_inverse:N = \g_@@_active_struct_dest_bool,
718 % \end{macrocode}
719 % \end{setupkeydecl}
720 % \begin{setupkeydecl}{debug/show}
721 % Subkeys/values are defined in various other places.
722 % \begin{macrocode}
723 debug/show .choice:,
724 % \end{macrocode}
725 % \end{setupkeydecl}
726 % \begin{setupkeydecl}{debug/log, debug/uncompress, log (deprecated), uncompress (deprecated)}
727 % The |log| takes currently the values |none|, |v|, |vv|, |vvv|, |all|.
728 % The description of the log levels is in tagpdf-checks.
729 % \begin{macrocode}
730 debug/log .choice:,
731 debug/log / none .code:n = {\int_set:Nn \l_@@_loglevel_int { 0 }},
732 debug/log / v .code:n =
733 {
734 \int_set:Nn \l_@@_loglevel_int { 1 }
735 \cs_set_protected:Nn \@@_check_typeout_v:n { \iow_term:e {##1} }
736 },
737 debug/log / vv .code:n = {\int_set:Nn \l_@@_loglevel_int { 2 }},
738 debug/log / vvv .code:n = {\int_set:Nn \l_@@_loglevel_int { 3 }},
739 debug/log / all .code:n = {\int_set:Nn \l_@@_loglevel_int { 10 }},
740 debug/uncompress .code:n = { \pdf_uncompress: },
741 % \end{macrocode}
742 % deprecated but still needed as the documentmetadata key argument uses it.
743 % \begin{macrocode}
744 log .meta:n = {debug/log={#1}},
745 uncompress .code:n = { \pdf_uncompress: },
746 % \end{macrocode}
747 % \end{setupkeydecl}
748 % \begin{setupkeydecl}{activate/tagunmarked,tagunmarked (deprecated)}
749 % This key allows to set if (in luamode) unmarked text should be
750 % marked up as artifact. The initial value is true.
751 % \begin{macrocode}
752 activate/tagunmarked .bool_gset:N = \g_@@_tagunmarked_bool,
753 activate/tagunmarked .initial:n = true,
754 % \end{macrocode}
755 % deprecated name
756 % \begin{macrocode}
757 tagunmarked .bool_gset:N = \g_@@_tagunmarked_bool,
758 % \end{macrocode}
759 % \end{setupkeydecl}
760 % \begin{setupkeydecl}{activate/softhyphen}
761 % This key activates (in luamode) the handling of soft hyphens.
762 % \begin{macrocode}
763 activate/softhyphen .choice:,
764 activate/softhyphen/false .code:n = {\bool_gset_false:N \g_@@_softhyphen_bool},
765 activate/softhyphen/off .code:n = {\bool_gset_false:N \g_@@_softhyphen_bool},
766 activate/softhyphen/char .code:n =
767 {
768 \bool_gset_true:N \g_@@_softhyphen_bool
769 \sys_if_engine_luatex:T
770 {
771 \lua_now:e
772 {
773 tex.setattribute
774 (
775 luatexbase.attributes.l_@@_softhyphen_attr,
776 1
777 )
778 }
779 }
780 },
781 activate/softhyphen/artifact .code:n =
782 {
783 \bool_gset_true:N \g_@@_softhyphen_bool
784 \sys_if_engine_luatex:T
785 {
786 \lua_now:e
787 {
788 tex.setattribute
789 (
790 luatexbase.attributes.l_@@_softhyphen_attr,
791 -2147483647
792 )
793 }
794 }
795 },
796 activate/softhyphen/true .code:n =
797 {
798 \bool_gset_true:N \g_@@_softhyphen_bool
799 \sys_if_engine_luatex:T
800 {
801 \lua_now:e
802 {
803 tex.setattribute
804 (
805 luatexbase.attributes.l_@@_softhyphen_attr,
806 -2147483647
807 )
808 }
809 }
810 },
811 activate/softhyphen/on .code:n =
812 {
813 \bool_gset_true:N \g_@@_softhyphen_bool
814 \sys_if_engine_luatex:T
815 {
816 \lua_now:e
817 {
818 tex.setattribute
819 (
820 luatexbase.attributes.l_@@_softhyphen_attr,
821 -2147483647
822 )
823 }
824 }
825 },
826 activate/softhyphen .default:n = artifact,
827 % \end{macrocode}
828 % \end{setupkeydecl}
829 % \begin{setupkeydecl}{activate/collect-link-artifacts}
830 % \begin{macrocode}
831 activate/collect-link-artifacts/off .code:n =
832 {\RemoveFromHook{begindocument/end}[tagpdf/linkbin]},
833 % \end{macrocode}
834 % \end{setupkeydecl}
835 % \begin{setupkeydecl}{page/tabsorder,tabsorder (deprecated)}
836 % This sets the tabsorder on a page. The values are |row|, |column|, |structure| (default)
837 % or |none|. Currently this is set more or less globally. More finer control can be
838 % added if needed.
839 % \begin{macrocode}
840 page/tabsorder .choice:,
841 page/tabsorder / row .code:n =
842 \pdfmanagement_add:nnn { Page } {Tabs}{/R},
843 page/tabsorder / column .code:n =
844 \pdfmanagement_add:nnn { Page } {Tabs}{/C},
845 page/tabsorder / structure .code:n =
846 \pdfmanagement_add:nnn { Page } {Tabs}{/S},
847 page/tabsorder / none .code:n =
848 \pdfmanagement_remove:nn {Page} {Tabs},
849 page/tabsorder .initial:n = structure,
850 % \end{macrocode}
851 % deprecated name
852 % \begin{macrocode}
853 tabsorder .meta:n = {page/tabsorder={#1}},
854 }
855 % \end{macrocode}
856 % \end{setupkeydecl}
857 % \section{loading of engine/more dependent code}
858 % \begin{macrocode}
859 \sys_if_engine_luatex:T
860 {
861 \file_input:n {tagpdf-luatex.def}
862 }
863 %</package>
864 % \end{macrocode}
865 % \begin{macrocode}
866 %<*mcloading>
867 \bool_if:NTF \g_@@_mode_lua_bool
868 {
869 \RequirePackage {tagpdf-mc-code-lua}
870 }
871 {
872 \RequirePackage {tagpdf-mc-code-generic} %
873 }
874 %</mcloading>
875 %<*debug>
876 \bool_if:NTF \g_@@_mode_lua_bool
877 {
878 \RequirePackage {tagpdf-debug-lua}
879 }
880 {
881 \RequirePackage {tagpdf-debug-generic} %
882 }
883 %</debug>
884 % \end{macrocode}
885 % \end{implementation}
886 % \PrintIndex