Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: tagpdf-mc-generic.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-generic} module\\ Code related to Marked Content (mc-chunks), generic mode ^^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 % \end{documentation}
52 % \begin{implementation}
53 % \section{Marked content code -- generic mode}
54 % \begin{macrocode}
55 %<@@=tag>
56 %<*generic>
57 \ProvidesExplPackage {tagpdf-mc-code-generic} {2026-05-17} {1.0c}
58 {part of tagpdf - code related to marking chunks - generic mode}
59 %</generic>
60 %<*debug>
61 \ProvidesExplPackage {tagpdf-debug-generic} {2026-05-17} {1.0c}
62 {part of tagpdf - debugging code related to marking chunks - generic mode}
63 %</debug>
64 % \end{macrocode}
65 %
66 %\subsection{Variables}
67 %
68 % \begin{macrocode}
69 %<*generic>
70 % \end{macrocode}
71 %
72 % \begin{variable}{\l_@@_mc_ref_abspage_tl}
73 % We need a ref-label system to ensure that the MCID cnt
74 % restarts at 0 on a new page
75 % This will be used to store the tagabspage attribute retrieved from
76 % a label.
77 % \begin{macrocode}
78 \tl_new:N \l_@@_mc_ref_abspage_tl
79 % \end{macrocode}
80 % \end{variable}
81 %
82 %
83 % \begin{variable}{\l_@@_mc_tmpa_tl}
84 % temporary variable
85 % \begin{macrocode}
86 \tl_new:N \l_@@_mc_tmpa_tl
87 % \end{macrocode}
88 % \end{variable}
89
90 % \begin{variable}
91 % {
92 % \g_@@_mc_marks,
93 % }
94 % a marks register to keep track of the mc's at page breaks and a sequence
95 % to keep track of the data for the continuation
96 % extra-tmb. We probably will need to track mc-marks
97 % in more than one stream, so the seq contains the name of the stream.
98 %
99 % \begin{macrocode}
100 \newmarks \g_@@_mc_marks
101 % \end{macrocode}
102 % \end{variable}
103 %
104 % \begin{variable}
105 % {
106 % \g_@@_mc_main_marks_seq,
107 % \g_@@_mc_footnote_marks_seq,
108 % \g_@@_mc_multicol_marks_seq
109 % }
110 % Each stream has an associated global seq variable holding the
111 % bottom marks from the/a previous chunk in the stream.
112 % We provide three by default: main, footnote and multicol.
113 % TODO: perhaps an interface for more streams will be needed.
114 % \begin{macrocode}
115 \seq_new:N \g_@@_mc_main_marks_seq
116 \seq_new:N \g_@@_mc_footnote_marks_seq
117 \seq_new:N \g_@@_mc_multicol_marks_seq
118 % \end{macrocode}
119 % \end{variable}
120 % \begin{macro}{\tag_mc_new_stream:n}
121 % \begin{macrocode}
122 \cs_new_protected:Npn \tag_mc_new_stream:n #1
123 {
124 \seq_new:c { g_@@_mc_multicol_#1_seq }
125 }
126 % \end{macrocode}
127 % \end{macro}
128 % \begin{variable}{\l_@@_mc_firstmarks_seq,\l_@@_mc_botmarks_seq}
129 % The marks content contains a number of data which we will have to access and
130 % compare, so we will store it locally in two sequences.
131 % topmarks is unusable in LaTeX so we ignore it.
132 %
133 % \begin{macrocode}
134 \seq_new:N \l_@@_mc_firstmarks_seq
135 \seq_new:N \l_@@_mc_botmarks_seq
136 % \end{macrocode}
137 % \end{variable}
138 %
139 % \subsection{Functions}
140 %
141 % \begin{macro}{\@@_mc_begin_marks:nn,\@@_mc_artifact_begin_marks:n,\@@_mc_end_marks:}
142 % Generic mode need to set marks for the page break and split stream handling.
143 % We always set two marks to be able to detect the case when no mark is on a
144 % page/galley. MC-begin commands will set (b,-,data) and (b,+,data),
145 % MC-end commands will set (e,-,data) and (e,+,data).
146 % \begin{macrocode}
147 \cs_new_protected:Npn \@@_mc_begin_marks:nn #1 #2 %#1 tag, #2 label
148 {
149 \tex_marks:D \g_@@_mc_marks
150 {
151 b-, %first of begin pair
152 \int_use:N\c@g_@@_MCID_abs_int, %mc-num
153 \g_@@_struct_stack_current_tl, %structure num
154 #1, %tag
155 \bool_if:NT \l_@@_mc_key_stash_bool{stash}, % stash info
156 #2, %label
157 }
158 \tex_marks:D \g_@@_mc_marks
159 {
160 b+, % second of begin pair
161 \int_use:N\c@g_@@_MCID_abs_int, %mc-num
162 \g_@@_struct_stack_current_tl, %structure num
163 #1, %tag
164 \bool_if:NT \l_@@_mc_key_stash_bool{stash}, % stash info
165 #2, %label
166 }
167 }
168 \cs_generate_variant:Nn \@@_mc_begin_marks:nn {oo}
169 \cs_new_protected:Npn \@@_mc_artifact_begin_marks:n #1 %#1 type
170 {
171 \tex_marks:D \g_@@_mc_marks
172 {
173 b-, %first of begin pair
174 \int_use:N\c@g_@@_MCID_abs_int, %mc-num
175 -1, %structure num
176 #1 %type
177 }
178 \tex_marks:D \g_@@_mc_marks
179 {
180 b+, %first of begin pair
181 \int_use:N\c@g_@@_MCID_abs_int, %mc-num
182 -1, %structure num
183 #1 %Type
184 }
185 }
186
187 \cs_new_protected:Npn \@@_mc_end_marks:
188 {
189 \tex_marks:D \g_@@_mc_marks
190 {
191 e-, %first of end pair
192 \int_use:N\c@g_@@_MCID_abs_int, %mc-num
193 \g_@@_struct_stack_current_tl, %structure num
194 }
195 \tex_marks:D \g_@@_mc_marks
196 {
197 e+, %second of end pair
198 \int_use:N\c@g_@@_MCID_abs_int, %mc-num
199 \g_@@_struct_stack_current_tl, %structure num
200 }
201 }
202 % \end{macrocode}
203 % \end{macro}
204 % \begin{macro}{\@@_mc_disable_marks:}
205 % This disables the marks. They can't be reenabled, so it should only
206 % be used in groups.
207 % \begin{macrocode}
208 \cs_new_protected:Npn \@@_mc_disable_marks:
209 {
210 \cs_set_eq:NN \@@_mc_begin_marks:nn \use_none:nn
211 \cs_set_eq:NN \@@_mc_artifact_begin_marks:n \use_none:n
212 \cs_set_eq:NN \@@_mc_end_marks: \prg_do_nothing:
213 }
214 % \end{macrocode}
215 % \end{macro}
216 % \begin{macro}{\@@_mc_get_marks:}
217 % This stores the current content of the marks in the sequences. It naturally
218 % should only be used in places where it makes sense.
219 % \begin{macrocode}
220 \cs_new_protected:Npn \@@_mc_get_marks:
221 {
222 \exp_args:NNe
223 \seq_set_from_clist:Nn \l_@@_mc_firstmarks_seq
224 { \tex_firstmarks:D \g_@@_mc_marks }
225 \exp_args:NNe
226 \seq_set_from_clist:Nn \l_@@_mc_botmarks_seq
227 { \tex_botmarks:D \g_@@_mc_marks }
228 }
229 % \end{macrocode}
230 % \end{macro}
231 %
232 % \begin{macro}{\@@_mc_store:nnn}
233 % This inserts the mc-chunk \meta{mc-num} into the structure {struct-num}
234 % after the \meta{mc-prev}.
235 % The structure must already exist.
236 % The additional mcid dictionary is stored in a property. The item is retrieved
237 % when the kid entry is built. We test if there is already
238 % an addition and append if needed.
239 % \begin{macrocode}
240 \cs_new_protected:Npn \@@_mc_store:nnn #1 #2 #3 %#1 mc-prev, #2 mc-num #3 structure-num
241 {
242 %\prop_show:N \g_@@_struct_cont_mc_prop
243 \prop_get:NnNTF \g_@@_struct_cont_mc_prop {#1} \l_@@_tmpa_tl
244 {
245 \prop_gput:Nne \g_@@_struct_cont_mc_prop {#1}{ \l_@@_tmpa_tl \@@_struct_mcid_dict:n {#2}}
246 }
247 {
248 \prop_gput:Nne \g_@@_struct_cont_mc_prop {#1}{ \@@_struct_mcid_dict:n {#2}}
249 }
250 \prop_gput:Nee \g_@@_mc_parenttree_prop
251 {#2}
252 {#3}
253 }
254 \cs_generate_variant:Nn \@@_mc_store:nnn {eee}
255 % \end{macrocode}
256 % \end{macro}
257 %
258 % \begin{macro}{\@@_mc_insert_extra_tmb:n,\@@_mc_insert_extra_tme:n}
259 % These two functions should be used in the output routine at the place
260 % where a mc-literal could be missing due to a page break or some other split.
261 % They check (with the help of the marks) if a extra-tmb or extra-tme is needed.
262 % The tmb command stores also the mc into the structure, the tme has to store
263 % the data for a following extra-tmb.
264 % The argument takes a stream name like main or footnote to allow different handling
265 % there.
266 % The content of the marks must be stored before (with |\@@_mc_get_marks:|
267 % or manually)
268 % into |\l_@@_mc_firstmarks_seq| and |\l_@@_mc_botmarks_seq| so that the tests
269 % can use them.
270 %
271 % \begin{macrocode}
272 \cs_new_protected:Npn \@@_mc_insert_extra_tmb:n #1 % #1 stream: e.g. main or footnote
273 {
274 \@@_check_typeout_v:n {=>~ first~ \seq_use:Nn \l_@@_mc_firstmarks_seq {,~}}
275 \@@_check_typeout_v:n {=>~ bot~ \seq_use:Nn \l_@@_mc_botmarks_seq {,~}}
276 \@@_check_if_mc_tmb_missing:TF
277 {
278 \@@_check_typeout_v:n {=>~ TMB~ ~ missing~ --~ inserted}
279 %test if artifact
280 \int_compare:nNnTF { \seq_item:cn { g_@@_mc_#1_marks_seq } {3} } = {-1}
281 {
282 \tl_set:Ne \l_@@_tmpa_tl { \seq_item:cn { g_@@_mc_#1_marks_seq } {4} }
283 \@@_mc_handle_artifact:N \l_@@_tmpa_tl
284 }
285 {
286 \exp_args:Ne
287 \@@_mc_bdc_mcid:n
288 {
289 \seq_item:cn { g_@@_mc_#1_marks_seq } {4}
290 }
291 \str_if_eq:eeTF
292 {
293 \seq_item:cn { g_@@_mc_#1_marks_seq } {5}
294 }
295 {}
296 {
297 %store
298 \@@_mc_store:eee
299 {
300 \seq_item:cn { g_@@_mc_#1_marks_seq } {2}
301 }
302 { \int_eval:n{\c@g_@@_MCID_abs_int} }
303 {
304 \seq_item:cn { g_@@_mc_#1_marks_seq } {3}
305 }
306 }
307 {
308 %stashed -> warning!!
309 }
310 }
311 }
312 {
313 \@@_check_typeout_v:n {=>~ TMB~ not~ missing}
314 }
315 }
316
317 \cs_new_protected:Npn \@@_mc_insert_extra_tme:n #1 % #1 stream, eg. main or footnote
318 {
319 \@@_check_if_mc_tme_missing:TF
320 {
321 \@@_check_typeout_v:n {=>~ TME~ ~ missing~ --~ inserted}
322 \@@_mc_emc:
323 \seq_gset_eq:cN
324 { g_@@_mc_#1_marks_seq }
325 \l_@@_mc_botmarks_seq
326 }
327 {
328 \@@_check_typeout_v:n {=>~ TME~ not~ missing}
329 }
330 }
331 % \end{macrocode}
332 % \end{macro}
333 %
334 % \subsection{Looking at MC marks in boxes}
335 % \begin{macro}{\@@_add_missing_mcs:Nn}
336 % Assumptions:
337 % \begin{itemize}
338 % \item
339 % test for tagging active outside;
340 % \item
341 % mark retrieval also outside.
342 % \end{itemize}
343 %
344 % This takes a box register as its first argument (or the register
345 % number in a count register, as used by \pkg{multicol}). It adds
346 % an extra tmb at the top of the box if necessary and similarly
347 % an extra tme at the end. This is done by adding hboxes in a way
348 % that the positioning and the baseline of the given box is not
349 % altered. The result is written back to the box.
350 %
351 % The second argument is the stream this box belongs to und is
352 % currently either \texttt{main} for the main galley,
353 % \texttt{footnote} for footnote note text, or \texttt{multicol}
354 % for boxes produced for columns in that environment. Other streams
355 % may follow over time.
356 % \begin{macrocode}
357 \cs_new_protected:Npn\@@_add_missing_mcs:Nn #1 #2 {
358 \vbadness \@M
359 \vfuzz \c_max_dim
360 \vbox_set_to_ht:Nnn #1 { \box_ht:N #1 } {
361 \hbox_set:Nn \l_@@_tmpa_box { \@@_mc_insert_extra_tmb:n {#2} }
362 \hbox_set:Nn \l_@@_tmpb_box { \@@_mc_insert_extra_tme:n {#2} }
363 \int_compare:nNnT {\l_@@_loglevel_int} > { 0 }
364 {
365 \seq_log:c { g_@@_mc_#2_marks_seq}
366 }
367 % \end{macrocode}
368 % The box placed on the top gets zero size and thus will not affect
369 % the box dimensions of the box we are modifying.
370 % \begin{macrocode}
371 \box_set_ht:Nn \l_@@_tmpa_box \c_zero_dim
372 \box_set_dp:Nn \l_@@_tmpa_box \c_zero_dim
373 % \end{macrocode}
374 % The box added at the bottom will get the depth of the original
375 % box. This way we can arrange that from the outside everything
376 % looks as before.
377 % \begin{macrocode}
378 \box_set_ht:Nn \l_@@_tmpb_box \c_zero_dim
379 \box_set_dp:Nn \l_@@_tmpb_box { \box_dp:N #1 }
380 % \end{macrocode}
381 % We need to set \cs{boxmaxdepth} in case the original box has an
382 % unusually large depth, otherwise that depth is not preserved when
383 % we string things together.
384 % \begin{macrocode}
385 \boxmaxdepth \@maxdepth
386 \box_use_drop:N \l_@@_tmpa_box
387 \vbox_unpack_drop:N #1
388 % \end{macrocode}
389 % Back up by the depth of the box as we add that later again.
390 % \begin{macrocode}
391 \tex_kern:D -\box_dp:N \l_@@_tmpb_box
392 % \end{macrocode}
393 % And we don't want any glue added when we add the box.
394 % \begin{macrocode}
395 \nointerlineskip
396 \box_use_drop:N \l_@@_tmpb_box
397 }
398 }
399 % \end{macrocode}
400 % \end{macro}
401
402 % \begin{macro}{\tag_mc_add_missing_to_stream:Nn,\@@_add_missing_mcs_to_stream:Nn}
403 % This is the main command to add mc to the stream. It is therefore
404 % guarded by the mc-boolean.
405 %
406 % If we aren't in the main stream then processing is a bit more
407 % complicated because to get at the marks in the box we need to
408 % artificially split it and then look at the split marks.
409 %
410 % First argument is the box to update and the second is the \enquote{stream}.
411 % In lua mode the command is a no-op.
412 % \begin{macrocode}
413 \cs_new_protected:Npn \@@_add_missing_mcs_to_stream:Nn #1#2
414 {
415 \@@_check_if_active_mc:T {
416 % \end{macrocode}
417 % First set up a temp box for trial splitting.
418 % \begin{macrocode}
419 \vbadness\maxdimen
420 \box_set_eq:NN \l_@@_tmpa_box #1
421 % \end{macrocode}
422 % Split the box to the largest size available. This should give us
423 % all content (but to be sure that there is no issue we could test
424 % out test box is empty now (not done).
425 % \begin{macrocode}
426 \vbox_set_split_to_ht:NNn \l_@@_tmpa_box \l_@@_tmpa_box \c_max_dim
427 % \end{macrocode}
428 % As a side effect of this split we should now have the first and
429 % bottom split marks set up. We use this to set up
430 % \cs{l_@@_mc_firstmarks_seq}
431 % \begin{macrocode}
432 \exp_args:NNe
433 \seq_set_from_clist:Nn \l_@@_mc_firstmarks_seq
434 { \tex_splitfirstmarks:D \g_@@_mc_marks }
435 % \end{macrocode}
436 % Some debugging info:
437 % \begin{macrocode}
438 % \iow_term:n { First~ mark~ from~ this~ box: }
439 % \seq_log:N \l_@@_mc_firstmarks_seq
440 % \end{macrocode}
441 % If this mark was empty then clearly the bottom mark will too be
442 % empty. Thus in this case we make use of the saved bot mark from
443 % the previous chunk. Note that if this is the first chunk in the
444 % stream the global seq would contain a random value, but then we
445 % can't end in this branch because the basis assumption is that
446 % streams are properly marked up so the first chunk would always
447 % have a mark at the beginning!
448 % \begin{macrocode}
449 \seq_if_empty:NTF \l_@@_mc_firstmarks_seq
450 {
451 \@@_check_typeout_v:n
452 {
453 No~ marks~ so~ use~ saved~ bot~ mark:~
454 \seq_use:cn {g_@@_mc_#2_marks_seq} {,~} \iow_newline:
455 }
456 \seq_set_eq:Nc \l_@@_mc_firstmarks_seq {g_@@_mc_#2_marks_seq}
457 % \end{macrocode}
458 % We also update the bot mark to the same value so that we can
459 % later apply \cs{@@_add_missing_mcs:Nn} with the data
460 % structures in place (see assumptions made there).
461 % \begin{macrocode}
462 \seq_set_eq:NN \l_@@_mc_botmarks_seq \l_@@_mc_firstmarks_seq
463 }
464 % \end{macrocode}
465 % If there was a first mark then there is also a bot mark (and it
466 % can't be the same as our marks always come in pairs).
467 % So if that branch is chosen we update
468 % \cs{l_@@_mc_botmarks_seq} from the bot mark.
469 % \begin{macrocode}
470 {
471 \@@_check_typeout_v:n
472 {
473 Pick~ up~ new~ bot~ mark!
474 }
475 \exp_args:NNe
476 \seq_set_from_clist:Nn \l_@@_mc_botmarks_seq
477 { \tex_splitbotmarks:D \g_@@_mc_marks }
478 }
479 % \end{macrocode}
480 % Finally we call \cs{@@_add_missing_mcs:Nn} to add any missing
481 % tmb/tme as needed,
482 % \begin{macrocode}
483 \@@_add_missing_mcs:Nn #1 {#2}
484 %%
485 \seq_gset_eq:cN {g_@@_mc_#2_marks_seq} \l_@@_mc_botmarks_seq
486 %%
487 }
488 }
489 \cs_set_eq:NN \tag_mc_add_missing_to_stream:Nn \@@_add_missing_mcs_to_stream:Nn
490 % \end{macrocode}
491 % \end{macro}
492 %
493 % \begin{macro}[pTF]{\@@_mc_if_in:,\tag_mc_if_in:}
494 % This is a test if a mc is open or not. It depends simply on a global boolean:
495 % mc-chunks are added linearly so nesting should not be relevant.
496 %
497 % One exception are header and footer (perhaps they are more, but for now
498 % it doesn't seem so, so there are no dedicated code to handle this situation):
499 % When they are built and added to the page we could be both inside or outside a mc-chunk.
500 % But header and footer should ignore this and not push/pop or warn about nested mc.
501 % It is therefore important there to set and reset the boolean manually.
502 % See the tagpddocu-patches.sty for an example.
503 % \begin{macrocode}
504 \prg_new_conditional:Nnn \@@_mc_if_in: {p,T,F,TF}
505 {
506 \bool_if:NTF \g_@@_in_mc_bool
507 { \prg_return_true: }
508 { \prg_return_false: }
509 }
510
511 \prg_new_eq_conditional:NNn \tag_mc_if_in: \@@_mc_if_in: {p,T,F,TF}
512 % \end{macrocode}
513 % \end{macro}
514 %
515 % \begin{macro}{ \@@_mc_bmc:n,\@@_mc_emc:,\@@_mc_bdc:nn}
516 % These are the low-level commands. There are now equal to the
517 % pdfmanagement commands generic mode, but we use an indirection
518 % in case luamode need something else.
519 % change 04.08.2018: the commands do not check the validity of the arguments or try
520 % to escape them, this should be done before using them.
521 % change 2023-08-18: we are delaying the writing to the shipout.
522 % \begin{macrocode}
523 % #1 tag, #2 properties
524 \cs_set_eq:NN \@@_mc_bmc:n \pdf_bmc:n
525 \cs_set_eq:NN \@@_mc_emc: \pdf_emc:
526 \cs_set_eq:NN \@@_mc_bdc:nn \pdf_bdc:nn
527 \cs_set_eq:NN \@@_mc_bdc_shipout:ee \pdf_bdc_shipout:ee
528 % \end{macrocode}
529 % \end{macro}
530 %
531 % \begin{macro}
532 % {
533 % \@@_mc_bdc_mcid:nn,\@@_mc_bdc_mcid:n,
534 % \@@_mc_handle_mcid:nn,\@@_mc_handle_mcid:oo
535 % }
536 %
537 % This create a BDC mark with an |/MCID| key. Most of the work here is to get
538 % the current number value for the MCID: they must be numbered by page
539 % starting with 0 and then successively.
540 % The first argument is the tag, e.g. |P| or |Span|, the second is used to pass
541 % more properties.
542 % Starting with texlive 2023 this is much simpler and faster as we can use
543 % delay the numbering to the shipout.
544 % We also define a wrapper around the low-level command as luamode will need
545 % something different.
546 % \begin{macrocode}
547 \hook_gput_code:nnn {shipout/before}{tagpdf}{ \flag_clear:n { @@/mcid } }
548 \cs_set_protected:Npn \@@_mc_bdc_mcid:nn #1 #2
549 {
550 \int_gincr:N \c@g_@@_MCID_abs_int
551 \@@_property_record:eo
552 {
553 mcid-\int_use:N \c@g_@@_MCID_abs_int
554 }
555 { \c_@@_property_mc_clist }
556 \@@_mc_bdc_shipout:ee
557 {#1}
558 {
559 /MCID~\flag_height:n { @@/mcid }
560 \flag_raise:n { @@/mcid }~ #2
561 }
562 }
563 \cs_new_protected:Npn \@@_mc_bdc_mcid:n #1
564 {
565 \@@_mc_bdc_mcid:nn {#1} {}
566 }
567
568 \cs_new_protected:Npn \@@_mc_handle_mcid:nn #1 #2 %#1 tag, #2 properties
569 {
570 \@@_mc_bdc_mcid:nn {#1} {#2}
571 }
572
573 \cs_generate_variant:Nn \@@_mc_handle_mcid:nn {oo}
574 % \end{macrocode}
575 % \end{macro}
576
577 % \begin{macro}{\@@_mc_handle_stash:n,\@@_mc_handle_stash:e}
578 % This is the handler which puts a mc into the
579 % the current structure. The argument is the number of the mc.
580 % Beside storing the mc into the structure, it also has to record the
581 % structure for the parent tree.
582 % The name is a bit confusing, it does \emph{not} handle mc with the stash key
583 % \ldots.
584 % TODO: why does luamode use it for begin + use, but generic mode only for begin?
585 % \begin{macrocode}
586 \cs_new_protected:Npn \@@_mc_handle_stash:n #1 %1 mcidnum
587 {
588 \@@_check_mc_used:n {#1}
589 \@@_struct_kid_mc_gput_right:nn
590 { \g_@@_struct_stack_current_tl }
591 {#1}
592 \prop_gput:Nee \g_@@_mc_parenttree_prop
593 {#1}
594 { \g_@@_struct_stack_current_tl }
595 }
596 \cs_generate_variant:Nn \@@_mc_handle_stash:n { e }
597 % \end{macrocode}
598 % \end{macro}
599
600 % \begin{macro}
601 % {
602 % \@@_mc_bmc_artifact:,
603 % \@@_mc_bmc_artifact:n,
604 % \@@_mc_handle_artifact:N
605 % }
606 % Two commands to create artifacts, one without type, and one with.
607 % We define also a wrapper handler as luamode will need a different definition.
608 % TODO: perhaps later: more properties for artifacts
609 % \begin{macrocode}
610 \cs_new_protected:Npn \@@_mc_bmc_artifact:
611 {
612 \@@_mc_bmc:n {Artifact}
613 }
614 \cs_new_protected:Npn \@@_mc_bmc_artifact:n #1
615 {
616 \@@_mc_bdc:nn {Artifact}{/Type/#1}
617 }
618 \cs_new_protected:Npn \@@_mc_handle_artifact:N #1
619 % #1 is a var containing the artifact type
620 {
621 \int_gincr:N \c@g_@@_MCID_abs_int
622 \tl_if_empty:NTF #1
623 { \@@_mc_bmc_artifact: }
624 { \exp_args:No\@@_mc_bmc_artifact:n {#1} }
625 }
626 % \end{macrocode}
627 % \end{macro}
628
629 % \begin{macro}{ \@@_get_data_mc_tag: }
630 % This allows to retrieve the active mc-tag.
631 % It is use by the get command.
632 % \begin{macrocode}
633 \cs_new:Nn \@@_get_data_mc_tag: { \g_@@_mc_key_tag_tl }
634 %</generic>
635 % \end{macrocode}
636 % \end{macro}
637 %
638 %
639 % \begin{macro}{\tag_mc_begin:n,\tag_mc_end:}
640 % These are the core public commands to open and close an mc.
641 % They don't need to be in the same group or grouping level,
642 % but the code expect that they are issued linearly. The tag and
643 % the state is passed to the end command through a global var and
644 % a global boolean.
645 % \begin{macrocode}
646 %<base>\cs_new_protected:Npn \tag_mc_begin:n #1 { \@@_whatsits: \int_gincr:N \c@g_@@_MCID_abs_int }
647 %<base>\cs_new_protected:Nn \tag_mc_end:{ \@@_whatsits: }
648 %<*generic|debug>
649 %<*generic>
650 \cs_set_protected:Npn \tag_mc_begin:n #1 %#1 keyval
651 {
652 \@@_check_if_active_mc:T
653 {
654 %</generic>
655 %<*debug>
656 \cs_set_protected:Npn \tag_mc_begin:n #1 %#1 keyval
657 {
658 \@@_check_if_active_mc:TF
659 {
660 \@@_debug_mc_begin_insert:n { #1 }
661 %</debug>
662 \group_begin: %hm
663 \@@_check_mc_if_nested:
664 \bool_gset_true:N \g_@@_in_mc_bool
665 % \end{macrocode}
666 % set default MC tags to structure:
667 % \begin{macrocode}
668 \tl_set_eq:NN \l_@@_mc_key_tag_tl \g_@@_struct_tag_tl
669 \tl_gset_eq:NN\g_@@_mc_key_tag_tl \g_@@_struct_tag_tl
670 \tl_if_empty:NTF\l_@@_mc_lang_tl
671 {
672 \keys_set:nn { @@ / mc }{ #1 }
673 }
674 {
675 \keys_set:nn { @@ / mc }{ lang=\l_@@_mc_lang_tl, #1 }
676 }
677 \bool_if:NTF \l_@@_mc_artifact_bool
678 { %handle artifact
679 \@@_mc_handle_artifact:N \l_@@_mc_artifact_type_tl
680 \exp_args:No
681 \@@_mc_artifact_begin_marks:n { \l_@@_mc_artifact_type_tl }
682 }
683 { %handle mcid type
684 \@@_check_mc_tag:N \l_@@_mc_key_tag_tl
685 \@@_mc_handle_mcid:oo
686 { \l_@@_mc_key_tag_tl }
687 { \l_@@_mc_key_properties_tl }
688 \@@_mc_begin_marks:oo{\l_@@_mc_key_tag_tl}{\l_@@_mc_key_label_tl}
689 \tl_if_empty:NF {\l_@@_mc_key_label_tl}
690 {
691 \@@_mc_handle_mc_label:e { \l_@@_mc_key_label_tl }
692 }
693 % \end{macrocode}
694 % check if the MC can be used here. This is guarded by the stash boolean.
695 % \begin{macrocode}
696 \bool_if:NF \l_@@_mc_key_stash_bool
697 {
698 \socket_use:nn{tag/check/parent-child}
699 {
700 \@@_mc_check_parent_child:o
701 { \g_@@_struct_stack_current_tl }
702 }
703 \@@_mc_handle_stash:e { \int_use:N \c@g_@@_MCID_abs_int }
704
705 }
706 }
707 \group_end:
708 }
709 %<*debug>
710 {
711 \@@_debug_mc_begin_ignore:n { #1 }
712 }
713 %</debug>
714 }
715 %<*generic>
716 \cs_set_protected:Nn \tag_mc_end:
717 {
718 \@@_check_if_active_mc:T
719 {
720 %</generic>
721 %<*debug>
722 \cs_set_protected:Nn \tag_mc_end:
723 {
724 \@@_check_if_active_mc:TF
725 {
726 \@@_debug_mc_end_insert:
727 %</debug>
728 \@@_check_mc_if_open:
729 \bool_gset_false:N \g_@@_in_mc_bool
730 \tl_gset:Nn \g_@@_mc_key_tag_tl { }
731 \@@_mc_emc:
732 \@@_mc_end_marks:
733 }
734 %<*debug>
735 {
736 \@@_debug_mc_end_ignore:
737 }
738 %</debug>
739 }
740 %</generic|debug>
741 % \end{macrocode}
742 % \end{macro}
743
744 %
745 % \subsection{Keys}
746 % Definitions are different in luamode.
747 % |tag| and |raw| are expanded as |\lua_now:e| in lua does it too and
748 % we assume that their values are safe.
749 % \begin{macro}
750 % {
751 % tag (mc-key),
752 % raw (mc-key),
753 % alt (mc-key),
754 % actualtext (mc-key),
755 % label (mc-key),
756 % artifact (mc-key)
757 % }
758 % \begin{macrocode}
759 %<*generic>
760 \keys_define:nn { @@ / mc }
761 {
762 tag .code:n = % the name (H,P,Span) etc
763 {
764 \tl_set:Ne \l_@@_mc_key_tag_tl { #1 }
765 \tl_gset:Ne \g_@@_mc_key_tag_tl { #1 }
766 },
767 raw .code:n =
768 {
769 \tl_put_right:Ne \l_@@_mc_key_properties_tl { #1 }
770 },
771 alt .code:n = % Alt property
772 {
773 \str_set_convert:Noon
774 \l_@@_tmpa_str
775 { #1 }
776 { default }
777 { utf16/hex }
778 \tl_put_right:Nn \l_@@_mc_key_properties_tl { /Alt~< }
779 \tl_put_right:No \l_@@_mc_key_properties_tl { \l_@@_tmpa_str>~ }
780 },
781 alttext .meta:n = {alt=#1},
782 % \end{macrocode}
783 % lang is not according to the spec, but it works in acrobat \ldots.
784 % We assume that this are simple strings that do not need escaping.
785 % \begin{macrocode}
786 lang .code:n = % Lang property
787 {
788 \tl_put_right:Ne \l_@@_mc_key_properties_tl { /Lang~(#1) }
789 },
790 actualtext .code:n = % ActualText property
791 {
792 \tl_if_empty:oF{#1}
793 {
794 \str_set_convert:Noon
795 \l_@@_tmpa_str
796 { #1 }
797 { default }
798 { utf16/hex }
799 \tl_put_right:Nn \l_@@_mc_key_properties_tl { /ActualText~< }
800 \tl_put_right:No \l_@@_mc_key_properties_tl { \l_@@_tmpa_str>~ }
801 }
802 },
803 label .tl_set:N = \l_@@_mc_key_label_tl,
804 artifact .code:n =
805 {
806 \exp_args:Nne
807 \keys_set:nn
808 { @@ / mc }
809 { __artifact-bool, __artifact-type=#1 }
810 },
811 artifact .default:n = {notype}
812 }
813 %</generic>
814 % \end{macrocode}
815 % \end{macro}
816 % \end{implementation}
817 % \PrintIndex