Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: tagpdf-mc-shared.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-mc module (tagpdf)}
30 \begin{document}
31 \DocInput{\jobname.dtx}
32 \end{document}
33 %</driver>
34 % \fi
35 % \title{^^A
36 % The \pkg{tagpdf-mc-shared} module\\ Code related to Marked Content (mc-chunks), code shared by all modes ^^A
37 % }
38 %
39 % \author{^^A
40 % Ulrike Fischer\thanks
41 % {^^A
42 % E-mail:
43 % \href{mailto:[email protected]}
44 % {[email protected]}^^A
45 % }^^A
46 % }
47 %
48 % \date{Version 1.0c, released 2026-05-17}
49 % \maketitle
50 % \begin{documentation}
51 % \section{Public Commands}
52 % \begin{function}{\tag_mc_begin:n,\tag_mc_end:}
53 % \begin{syntax}
54 % \cs{tag_mc_begin:n} \Arg{key-values}\\
55 % \cs{tag_mc_end:}
56 % \end{syntax}
57 % These commands insert the end code of the marked content.
58 % They don't end a group and in generic mode it doesn't matter
59 % if they are in another group as the starting commands.
60 % In generic mode both commands check if they are correctly nested
61 % and issue a warning if not.
62 % \end{function}
63 %
64 % \begin{function}{\tag_mc_use:n}
65 % \begin{syntax}
66 % \cs{tag_mc_use:n} \Arg{label}
67 % \end{syntax}
68 % These command allow to record a marked content that was stashed away before
69 % into the current structure. A marked content can be used only once --
70 % the command will issue a warning if an mc is use a second time.
71 % \end{function}
72 %
73 % \begin{function}[added = 2019-11-20]
74 % {
75 % \tag_mc_artifact_group_begin:n, \tag_mc_artifact_group_end:
76 % }
77 % \begin{syntax}
78 % \cs{tag_mc_artifact_group_begin:n} \Arg{name}\\
79 % \cs{tag_mc_artifact_group_end:}
80 % \end{syntax}
81 % This command pair creates a group with an artifact marker at the begin
82 % and the end. Inside the group the tagging commands are disabled.
83 % It allows to mark a complete region as artifact without having to worry
84 % about user commands with tagging commands.
85 % \meta{name} should be a value allowed also for the |artifact| key.
86 % It pushes and pops mc-chunks at the begin and end.
87 % TODO: document is in tagpdf.tex
88 % \end{function}
89 %
90 % \begin{function}[added = 2021-04-22]
91 % {
92 % \tag_mc_end_push:, \tag_mc_begin_pop:n
93 % }
94 % \begin{syntax}
95 % \cs{tag_mc_end_push:} \\
96 % \cs{tag_mc_begin_pop:n} \Arg{key-values}
97 % \end{syntax}
98 % If there is an open mc chunk,
99 % \cs{tag_mc_end_push:} ends it and pushes its tag of the (global) stack.
100 % If there is no open chunk, it puts $-1$ on the stack (for debugging)
101 % \cs{tag_mc_begin_pop:n} removes a value from the stack. If it is different from
102 % $-1$ it opens a tag with it.
103 % The reopened mc chunk looses info like the alt text for now.
104 % \end{function}
105 %
106 % \begin{function}[pTF, EXP]{\tag_mc_if_in:}
107 % \begin{syntax}
108 % \cs{tag_mc_if_in:TF} \Arg{true code} \Arg{false code}
109 % \end{syntax}
110 % Determines if a mc-chunk is open.
111 % \end{function}
112 %
113 % \begin{function}[ EXP,added=2023-06-11]{\tag_mc_reset_box:N}
114 % \begin{syntax}
115 % \cs{tag_mc_reset_box:N} \meta{box}
116 % \end{syntax}
117 % This resets in lua mode the mc attributes to the one currently in use.
118 % It does nothing in generic mode.
119 % \end{function}
120 %
121 % \begin{function}[added=2024-11-18]{\tag_mc_add_missing_to_stream:Nn}
122 % \begin{syntax}
123 % \cs{tag_mc_add_missing_to_stream:Nn} \meta{box} \Arg{stream name}
124 % \end{syntax}
125 % This command is only needed in generic mode, in lua mode it gobbles its arguments.
126 % In generic mode it adds MC literals to the stream that are missing because of
127 % page breaks.
128 % The first argument is the box with the stream, the second a
129 % string representing the stream. Predeclared are the names
130 % \texttt{main}, \texttt{footnote} and \texttt{multicol}.
131 % If more streams should be handle the underlying interface must be enabled
132 % with \cs{tag_mc_new_stream:n}
133 % The command is only for packages doing deep manipulations
134 % of the output routine!
135 % Example of use are in the multicol package and in tagpdf itself.
136 % \end{function}
137 %
138 % \begin{function}[added=2024-11-18]{\tag_mc_new_stream:n}
139 % \begin{syntax}
140 % \cs{tag_mc_new_stream:n} \Arg{stream name}
141 % \end{syntax}
142 % This declares the interface needed to handle
143 % a new stream with \cs{tag_mc_add_missing_to_stream:Nn}.
144 % Predeclared are the names \texttt{main}, \texttt{footnote} and \texttt{multicol}.
145 % \end{function}
146 %
147 % \section{Public keys}
148 % The following keys can be used with |\tag_mc_begin:n|, |\tagmcbegin|,
149 % |\tag_mc_begin_pop:n|,
150 %
151 % \begin{function}{tag (mc-key)}
152 % This key is required, unless artifact is used.
153 % The value is a tag like |P| or |H1| without a slash at the begin,
154 % this is added by the code.
155 % It is possible to setup new tags.
156 % The value of the key is expanded, so it can be a command.
157 % The expansion is passed unchanged to the PDF,
158 % so it should with a starting slash give a valid PDF name
159 % (some ascii with numbers like \texttt{H4} is fine).
160 % \end{function}
161 %
162 % \begin{function}{artifact (mc-key)}
163 % This will setup the marked content as an artifact. The key should be used
164 % for content that should be ignored.
165 % The key can take one of the values |pagination|,
166 % |layout|, |page|, |background| and |notype|
167 % (this is the default).
168 % \end{function}
169 %
170 % \begin{function}{raw (mc-key)}
171 % This key allows to add more entries to the properties dictionary.
172 % The value must be correct, low-level PDF. E.g.
173 % \verb+raw=/Alt (Hello)+ will insert an alternative Text.
174 % \end{function}
175 %
176 % \begin{function}{alt (mc-key)}
177 % This key inserts an \texttt{/Alt} value in the property dictionary of the BDC operator.
178 % The value is handled as verbatim string, commands are not expanded.
179 % The value will be expanded first once. If it is empty, nothing will happen.
180 % \end{function}
181 %
182 % \begin{function}{actualtext (mc-key)}
183 % This key inserts an \texttt{/ActualText} value in the property dictionary
184 % of the BDC operator. The value is handled as verbatim string,
185 % commands are not expanded.
186 % The value will be expanded first once. If it is empty, nothing will happen.
187 % \end{function}
188 %
189 % \begin{function}{label (mc-key)}
190 % This key sets a label by which one can call the marked content
191 % later in another structure
192 % (if it has been stashed with the |stash| key).
193 % Internally the label name will start with \texttt{tagpdf-}.
194 % \end{function}
195 %
196 % \begin{function}{stash (mc-key)}
197 % This \enquote{stashes} an mc-chunk: it is not inserted into the current structure.
198 % It should be normally be used along with a label to be able to use the mc-chunk
199 % in another place.
200 % \end{function}
201 % \end{documentation}
202 % \begin{implementation}
203 % The code is split into three parts: code shared by all engines,
204 % code specific to luamode and code not used by luamode.
205 % \section{Marked content code -- shared}
206 % \begin{macrocode}
207 %<@@=tag>
208 %<*header>
209 \ProvidesExplPackage {tagpdf-mc-code-shared} {2026-05-17} {1.0c}
210 {part of tagpdf - code related to marking chunks -
211 code shared by generic and luamode }
212 %</header>
213 % \end{macrocode}
214 %
215 % \subsection{Variables and counters}
216 % MC chunks must be counted.
217 % I use a latex counter for the absolute count, so that it is added to
218 % |\cl@@ckpt| and restored e.g. in tabulars and align.
219 % |\int_new:N \c@g_@@_MCID_abs_int| and
220 % |\tl_put_right:Nn\cl@@ckpt{\@elt{g_@@_MCID_abs_int}}|
221 % would work too, but as the name is not expl3 then too, why bother?
222 % The absolute counter can be used to label and to check if the page
223 % counter needs a reset.
224 %
225 % \begin{variable}{g_@@_MCID_abs_int}
226 % \begin{macrocode}
227 %<*base>
228 \newcounter { g_@@_MCID_abs_int }
229 % \end{macrocode}
230 % \end{variable}
231 % \begin{macro}{\@@_get_data_mc_counter:}
232 % This command allows \cs{tag_get:n} to get the current
233 % state of the mc counter with the keyword |mc_counter|.
234 % By comparing the numbers it can be used to check the number of
235 % structure commands in a piece of code.
236 % \begin{macrocode}
237 \cs_new:Npn \@@_get_data_mc_counter:
238 {
239 \int_use:N \c@g_@@_MCID_abs_int
240 }
241 %</base>
242 % \end{macrocode}
243 % \end{macro}
244 %
245 %
246 % \begin{macro}{\@@_get_mc_abs_cnt:}
247 % A (expandable) function to get the current value of the cnt.
248 % TODO: duplicate of the previous one, this should be cleaned up.
249 % \begin{macrocode}
250 %<*shared>
251 \cs_new:Npn \@@_get_mc_abs_cnt: { \int_use:N \c@g_@@_MCID_abs_int }
252 % \end{macrocode}
253 % \end{macro}
254 %
255 %
256 % \begin{variable}{\g_@@_in_mc_bool}
257 % This booleans record if a mc is open, to test nesting.
258 % \begin{macrocode}
259 \bool_new:N \g_@@_in_mc_bool
260 % \end{macrocode}
261 % \end{variable}
262 % \begin{variable}{\g_@@_mc_parenttree_prop}
263 % For every chunk we need to know the structure it is in, to
264 % record this in the parent tree. We store this in a property.\\
265 % key: absolute number of the mc (tagmcabs)\\
266 % value: the structure number the mc is in
267 % \begin{macrocode}
268 \@@_prop_new_linked:N \g_@@_mc_parenttree_prop
269 % \end{macrocode}
270 % \end{variable}
271 %
272 %\begin{variable}{\g_@@_mc_parenttree_prop}
273 % Some commands (e.g. links) want to close a previous mc and reopen it after
274 % they did their work. For this we create a stack:
275 % \begin{macrocode}
276 \seq_new:N \g_@@_mc_stack_seq
277 % \end{macrocode}
278 % \end{variable}
279 %
280 %\begin{variable}{\l_@@_mc_artifact_type_tl}
281 % Artifacts can have various types like Pagination or Layout. This stored
282 % in this variable.
283 % \begin{macrocode}
284 \tl_new:N \l_@@_mc_artifact_type_tl
285 % \end{macrocode}
286 % \end{variable}
287 %
288 %\begin{variable}{\l_@@_mc_key_stash_bool,\l_@@_mc_artifact_bool}
289 %This booleans store the stash and artifact status of the mc-chunk.
290 % \begin{macrocode}
291 \bool_new:N \l_@@_mc_key_stash_bool
292 \bool_new:N \l_@@_mc_artifact_bool
293 % \end{macrocode}
294 % \end{variable}
295 %
296 % \begin{variable}{\l_@@_mc_lang_tl}
297 % a variable to set a Lang on the mc. This is not conforming to the spec!
298 % But it seems to work in acrobat.
299 % \begin{macrocode}
300 \tl_new:N \l_@@_mc_lang_tl
301 % \end{macrocode}
302 % \end{variable}
303 %
304 % \begin{variable}
305 % {
306 % \l_@@_mc_key_tag_tl,
307 % \g_@@_mc_key_tag_tl,
308 % \l_@@_mc_key_label_tl,
309 % \l_@@_mc_key_properties_tl
310 % }
311 % Variables used by the keys. |\l_@@_mc_key_properties_tl|
312 % will collect a number of values. TODO: should this be a pdfdict now?
313 % \begin{macrocode}
314 \tl_new:N \l_@@_mc_key_tag_tl
315 \tl_new:N \g_@@_mc_key_tag_tl
316 \tl_new:N \l_@@_mc_key_label_tl
317 \tl_new:N \l_@@_mc_key_properties_tl
318 % \end{macrocode}
319 % \end{variable}
320 %
321 %
322 % \subsection{Functions}
323 %
324 % \begin{macro}{\@@_mc_handle_mc_label:e}
325 % The commands labels a mc-chunk. It is used if the user explicitly
326 % labels the mc-chunk with the |label| key. The argument is the
327 % value provided by the user. It stores the attributes\\
328 % |tagabspage|: the absolute page, |\g_shipout_readonly_int|,\\
329 % |tagmcabs|: the absolute mc-counter |\c@g_@@_MCID_abs_int|.
330 % The reference command is based on l3ref.
331 % \begin{macrocode}
332 \cs_new:Npn \@@_mc_handle_mc_label:e #1
333 {
334 \@@_property_record:en{tagpdf-#1}{tagabspage,tagmcabs}
335 }
336 % \end{macrocode}
337 % \end{macro}
338 %
339 % \begin{macro}{\@@_mc_set_label_used:n}
340 % Unlike with structures we can't check if a labeled mc has been used by
341 % looking at the P key, so we use a dedicated csname for the test
342 % \begin{macrocode}
343 \cs_new_protected:Npn \@@_mc_set_label_used:n #1 %#1 labelname
344 {
345 \tl_new:c { g_@@_mc_label_\tl_to_str:n{#1}_used_tl }
346 }
347 %</shared>
348 % \end{macrocode}
349 %
350 % \end{macro}
351 % \begin{macro}{\tag_mc_use:n}
352 % These command allow to record a marked content that was stashed away before
353 % into the current structure. A marked content can be used only once --
354 % the command will issue a warning if an mc is use a second time.
355 % The argument is a label name set with the |label| key.
356 %
357 % TODO: is testing for struct the right test?
358 % \begin{macrocode}
359 %<base>\cs_new_protected:Npn \tag_mc_use:n #1 { \@@_whatsits: }
360 %<*shared>
361 \cs_set_protected:Npn \tag_mc_use:n #1 %#1: label name
362 {
363 \@@_check_if_active_struct:T
364 {
365 \tl_set:Ne \l_@@_tmpa_tl { \property_ref:nnn{tagpdf-#1}{tagmcabs}{} }
366 \tl_if_empty:NTF\l_@@_tmpa_tl
367 {
368 \msg_warning:nnn {tag} {mc-label-unknown} {#1}
369 }
370 {
371 \cs_if_free:cTF { g_@@_mc_label_\tl_to_str:n{#1}_used_tl }
372 {
373 \@@_mc_handle_stash:e { \l_@@_tmpa_tl }
374 \@@_mc_set_label_used:n {#1}
375 }
376 {
377 \msg_warning:nnn {tag}{mc-used-twice}{#1}
378 }
379 }
380 }
381 }
382 %</shared>
383 % \end{macrocode}
384 % \end{macro}
385 % \begin{macro}
386 % {
387 % \tag_mc_artifact_group_begin:n,
388 % \tag_mc_artifact_group_end:
389 % }
390 % This opens an artifact of the type given in the argument,
391 % and then stops all tagging. It creates a group.
392 % It pushes and pops mc-chunks at the begin and end.
393 % \begin{macrocode}
394 %<base>\cs_new_protected:Npn \tag_mc_artifact_group_begin:n #1 {}
395 %<base>\cs_new_protected:Npn \tag_mc_artifact_group_end:{}
396 %<*shared>
397 \cs_set_protected:Npn \tag_mc_artifact_group_begin:n #1
398 {
399 \tag_mc_end_push:
400 \tag_mc_begin:n {artifact=#1}
401 \group_begin:
402 \tag_suspend:n{artifact-group}
403 }
404
405 \cs_set_protected:Npn \tag_mc_artifact_group_end:
406 {
407 \tag_resume:n{artifact-group}
408 \group_end:
409 \tag_mc_end:
410 \tag_mc_begin_pop:n{}
411 }
412 %</shared>
413 % \end{macrocode}
414 % \end{macro}
415 % \begin{macro}{\tag_mc_reset_box:N}
416 % This allows to reset the mc-attributes in box. On base and generic mode it should do
417 % nothing.
418 % \begin{macrocode}
419 %<base>\cs_new_protected:Npn \tag_mc_reset_box:N #1 {}
420 % \end{macrocode}
421 % \end{macro}
422 % \begin{macro}{\tag_mc_end_push:, \tag_mc_begin_pop:n}
423 %
424 % \begin{macrocode}
425 %<base>\cs_new_protected:Npn \tag_mc_end_push: {}
426 %<base>\cs_new_protected:Npn \tag_mc_begin_pop:n #1 {}
427 %<*shared>
428 \cs_set_protected:Npn \tag_mc_end_push:
429 {
430 \@@_check_if_active_mc:T
431 {
432 \@@_mc_if_in:TF
433 {
434 \seq_gpush:Ne \g_@@_mc_stack_seq { \tag_get:n {mc_tag} }
435 \@@_check_mc_pushed_popped:nn
436 { pushed }
437 { \tag_get:n {mc_tag} }
438 \tag_mc_end:
439 }
440 {
441 \seq_gpush:Nn \g_@@_mc_stack_seq {-1}
442 \@@_check_mc_pushed_popped:nn { pushed }{-1}
443 }
444 }
445 }
446
447 \cs_set_protected:Npn \tag_mc_begin_pop:n #1
448 {
449 \@@_check_if_active_mc:T
450 {
451 \seq_gpop:NNTF \g_@@_mc_stack_seq \l_@@_tmpa_tl
452 {
453 \tl_if_eq:NnTF \l_@@_tmpa_tl {-1}
454 {
455 \@@_check_mc_pushed_popped:nn {popped}{-1}
456 }
457 {
458 \@@_check_mc_pushed_popped:nn {popped}{\l_@@_tmpa_tl}
459 \tag_mc_begin:n {tag=\l_@@_tmpa_tl,#1}
460 }
461 }
462 {
463 \@@_check_mc_pushed_popped:nn {popped}{empty~stack,~nothing}
464 }
465 }
466 }
467 % \end{macrocode}
468 % \end{macro}
469 %
470 % \begin{macro}{\@@_mc_check_parent_child:n}
471 % This checks if an MC can be used in a structure.
472 % \begin{macrocode}
473 \cs_new_protected:Npn \@@_mc_check_parent_child:n #1
474 % #1 structure number of parent
475 {
476 % \end{macrocode}
477 % This records if logging is on
478 % \begin{macrocode}
479 \int_compare:nNnT {\l_@@_loglevel_int} > { 0 }
480 {
481 \prop_get:cnN{g_@@_struct_#1_prop}{tag}\l_@@_get_parent_tmpa_tl
482 \msg_note:nnee
483 { tag }
484 { role-parent-child-check }
485 {
486 \quark_if_no_value:NTF \l__tag_get_parent_tmpa_tl
487 {??}
488 {
489 \exp_last_unbraced:No\use_ii:nn
490 { \l_@@_get_parent_tmpa_tl }
491 :
492 \exp_last_unbraced:No\use_i:nn
493 { \l_@@_get_parent_tmpa_tl }
494 }
495 }
496 {
497 MC~(real~content)
498 }
499 }
500 \@@_struct_get_role:nnNN
501 {#1}
502 {rolemap}
503 \l_@@_get_parent_tmpa_tl
504 \l_@@_get_parent_tmpb_tl
505 \@@_role_check_parent_child:ooooN
506 { \l_@@_get_parent_tmpa_tl }
507 { \l_@@_get_parent_tmpb_tl }
508 { MC } %
509 { } %
510 \l_@@_parent_child_check_tl
511 % \end{macrocode}
512 % if the return value is 7 we have to check against the parentrole field.
513 % TODO ruby and warichu use 7 too, that should be changed!
514 % \begin{macrocode}
515 \int_compare:nNnT {\l_@@_parent_child_check_tl} = { \c_@@_role_rule_checkparent_tl }
516 {
517 \@@_struct_get_role:nnNN
518 {#1}
519 {parentrole}
520 \l_@@_get_parent_tmpa_tl
521 \l_@@_get_parent_tmpb_tl
522 \@@_role_check_parent_child:ooooN
523 { \l_@@_get_parent_tmpa_tl }
524 { \l_@@_get_parent_tmpb_tl }
525 { MC } %
526 { } %
527 \l_@@_parent_child_check_tl
528 }
529 \@@_check_forbidden_parent_child:nnee
530 {\l_@@_parent_child_check_tl}
531 {#1}
532 {
533 \l_@@_get_parent_tmpb_tl : \l_@@_get_parent_tmpa_tl
534 }
535 {
536 MC~(real content)
537 }
538 }
539 \cs_generate_variant:Nn \@@_mc_check_parent_child:n {o}
540 % \end{macrocode}
541 % \end{macro}
542 %
543 % \subsection{Keys}
544 % This are the keys where the code can be shared between the modes.
545 %
546 % \begin{macro}{stash (mc-key),__artifact-bool,__artifact-type}
547 % the two internal artifact keys are use to define the public |artifact|.
548 % For now we add support for the subtypes Header and Footer.
549 % Watermark,PageNum, LineNum,Redaction,Bates will be added if some use case
550 % emerges. If some use case for /BBox and /Attached emerges, it will be perhaps
551 % necessary to adapt the code.
552 % \begin{macrocode}
553 \keys_define:nn { @@ / mc }
554 {
555 stash .bool_set:N = \l_@@_mc_key_stash_bool,
556 __artifact-bool .bool_set:N = \l_@@_mc_artifact_bool,
557 __artifact-type .choice:,
558 __artifact-type / pagination .code:n =
559 {
560 \tl_set:Nn \l_@@_mc_artifact_type_tl { Pagination }
561 },
562 __artifact-type / pagination/header .code:n =
563 {
564 \tl_set:Nn \l_@@_mc_artifact_type_tl { Pagination/Subtype/Header }
565 },
566 __artifact-type / pagination/footer .code:n =
567 {
568 \tl_set:Nn \l_@@_mc_artifact_type_tl { Pagination/Subtype/Footer }
569 },
570 __artifact-type / layout .code:n =
571 {
572 \tl_set:Nn \l_@@_mc_artifact_type_tl { Layout }
573 },
574 __artifact-type / page .code:n =
575 {
576 \tl_set:Nn \l_@@_mc_artifact_type_tl { Page }
577 },
578 __artifact-type / background .code:n =
579 {
580 \tl_set:Nn \l_@@_mc_artifact_type_tl { Background }
581 },
582 __artifact-type / notype .code:n =
583 {
584 \tl_set:Nn \l_@@_mc_artifact_type_tl {}
585 },
586 __artifact-type / .code:n =
587 {
588 \tl_set:Nn \l_@@_mc_artifact_type_tl {}
589 },
590 }
591 % \end{macrocode}
592 % \end{macro}
593 % \begin{macrocode}
594 %</shared>
595 % \end{macrocode}
596 % \end{implementation}
597 % \PrintIndex