summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/tagpdf/tagpdf-mc-generic.dtx
blob: 903b38d5a355c00287d79034b34573869745cffe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
% \iffalse meta-comment
%
%% File: tagpdf-mc.dtx
%
% Copyright (C) 2019-2021 Ulrike Fischer
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
% This file is part of the "tagpdf bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/u-fischer/tagpdf
%
% for those people who are interested.
%<*driver>
\RequirePackage{pdfmanagement-testphase}
\DeclareDocumentMetadata{}
\makeatletter
\declare@file@substitution{doc.sty}{doc-v3beta.sty}
\makeatother
\documentclass{l3doc}
\usepackage{array,booktabs,caption}
\hypersetup{pdfauthor=Ulrike Fischer,
 pdftitle=tagpdf-mc module (tagpdf)}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
% \title{^^A
%   The \pkg{tagpdf-mc-generic} module\\ Code related to Marked Content (mc-chunks), generic mode  ^^A
%   \\ Part of the tagpdf package
% }
%
% \author{^^A
%  Ulrike Fischer\thanks
%    {^^A
%      E-mail:
%        \href{mailto:fischer@troubleshooting-tex.de}
%          {fischer@troubleshooting-tex.de}^^A
%    }^^A
% }
%
% \date{Version 0.92, released 2021-08-27}
% \maketitle
% \begin{documentation}
% \end{documentation}
% \begin{implementation}
% \section{Marked content code -- generic mode}
%    \begin{macrocode}
%<@@=tag>
%<*generic>
\ProvidesExplPackage {tagpdf-mc-code-generic} {2021-08-27} {0.92}
 {part of tagpdf - code related to marking chunks - generic mode}
%</generic>
%    \end{macrocode}
%
%\subsection{Variables}
%
% \begin{variable}{\g_@@_MCID_byabspage_prop}
% This property will hold the current maximum on a page
% it will contain key-value of type \meta{abspagenum}=\meta{max mcid}
%
%    \begin{macrocode}
%<*generic>
\@@_prop_new:N \g_@@_MCID_byabspage_prop
%    \end{macrocode}
% \end{variable}

% \begin{variable}{\l_@@_mc_ref_abspage_tl}
% We need a ref-label system to ensure that the MCID cnt
% restarts at 0 on a new page
% This will be used to store the tagabspage attribute retrieved from
% a label.
%    \begin{macrocode}
\tl_new:N \l_@@_mc_ref_abspage_tl
%    \end{macrocode}
% \end{variable}

% \begin{variable}{\l_@@_mc_tmpa_tl}
% temporary variable
%    \begin{macrocode}
\tl_new:N \l_@@_mc_tmpa_tl
%    \end{macrocode}
% \end{variable}

% \begin{variable}
%   {
%     \g_@@_mc_marks,
%   }
% a marks register to keep track of the mc's at page breaks and a sequence
% to keep track of the data for the continuation
% extra-tmb. We probably will need to track mc-marks
% in more than one stream, so the seq contains the name of the stream.
%
%    \begin{macrocode}
\newmarks  \g_@@_mc_marks
%    \end{macrocode}
% \end{variable}
%
%  \begin{variable}
%   {
%    \g_@@_mc_main_marks_seq,
%    \g_@@_mc_footnote_marks_seq,
%    \g_@@_mc_multicol_marks_seq
%   }
%    Each stream has an associated global seq variable holding the
%    bottom marks from the/a previous chunk in the stream.
%    We provide three by default: main, footnote and multicol.
%    TODO: perhaps an interface for more streams will be needed.
%    \begin{macrocode}
\seq_new:N \g_@@_mc_main_marks_seq
\seq_new:N \g_@@_mc_footnote_marks_seq
\seq_new:N \g_@@_mc_multicol_marks_seq
%    \end{macrocode}
%  \end{variable}

% \begin{variable}{\l_@@_mc_firstmarks_seq,\l_@@_mc_botmarks_seq}
% The marks content contains a number of data which we will have to access and
% compare, so we will store it locally in two sequences.
% topmarks is unusable in LaTeX so we ignore it.
%
%    \begin{macrocode}
\seq_new:N  \l_@@_mc_firstmarks_seq
\seq_new:N  \l_@@_mc_botmarks_seq
%    \end{macrocode}
% \end{variable}
%
% \subsection{Functions}
%
% \begin{macro}{\@@_mc_begin_marks:nn,\@@_mc_artifact_begin_marks:n,\@@_mc_end_marks:}
% Generic mode need to set marks for the page break and split stream handling.
% We always set two marks to be able to detect the case when no mark is on a
% page/galley. MC-begin commands will set (b,-,data) and (b,+,data),
% MC-end commands will set (e,-,data) and (e,+,data).
%    \begin{macrocode}
\cs_new_protected:Npn \@@_mc_begin_marks:nn #1 #2 %#1 tag, #2 label
  {
    \tex_marks:D \g_@@_mc_marks
      {
        b-, %first of begin pair
        \int_use:N\c@g_@@_MCID_abs_int, %mc-num
        \g_@@_struct_stack_current_tl,  %structure num
        #1, %tag
        \bool_if:NT \l_@@_mc_key_stash_bool{stash}, % stash info
        #2, %label
      }
    \tex_marks:D \g_@@_mc_marks
      {
        b+, % second of begin pair
        \int_use:N\c@g_@@_MCID_abs_int, %mc-num
        \g_@@_struct_stack_current_tl,  %structure num
        #1, %tag
        \bool_if:NT \l_@@_mc_key_stash_bool{stash}, % stash info
        #2, %label
      }
  }
\cs_generate_variant:Nn \@@_mc_begin_marks:nn {oo}
\cs_new_protected:Npn \@@_mc_artifact_begin_marks:n #1 %#1 type
  {
    \tex_marks:D \g_@@_mc_marks
      {
        b-, %first of begin pair
        \int_use:N\c@g_@@_MCID_abs_int, %mc-num
        -1, %structure num
        #1 %type
      }
    \tex_marks:D \g_@@_mc_marks
      {
        b+, %first of begin pair
        \int_use:N\c@g_@@_MCID_abs_int, %mc-num
        -1,  %structure num
        #1  %Type
      }
  }

\cs_new_protected:Npn \@@_mc_end_marks:
  {
    \tex_marks:D \g_@@_mc_marks
      {
        e-, %first of end pair
        \int_use:N\c@g_@@_MCID_abs_int, %mc-num
        \g_@@_struct_stack_current_tl,  %structure num
      }
    \tex_marks:D \g_@@_mc_marks
      {
        e+, %second of end pair
        \int_use:N\c@g_@@_MCID_abs_int, %mc-num
        \g_@@_struct_stack_current_tl,  %structure num
      }
  }
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@@_mc_disable_marks:}
% This disables the marks. They can't be reenabled, so it should only
% be used in groups.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_mc_disable_marks:
 {
   \cs_set_eq:NN \@@_mc_begin_marks:nn \use_none:nn
   \cs_set_eq:NN \@@_mc_artifact_begin_marks:n \use_none:n
   \cs_set_eq:NN \@@_mc_end_marks: \prg_do_nothing:
 }
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@@_mc_get_marks:}
% This stores the current content of the marks in the sequences. It naturally
% should only be used in places where it makes sense.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_mc_get_marks:
 {
   \exp_args:NNx
   \seq_set_from_clist:Nn \l_@@_mc_firstmarks_seq
     { \tex_firstmarks:D \g_@@_mc_marks }
   \exp_args:NNx
   \seq_set_from_clist:Nn \l_@@_mc_botmarks_seq
     { \tex_botmarks:D   \g_@@_mc_marks }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_mc_store:nnn}
%  This inserts the mc-chunk \meta{mc-num} into the structure {struct-num}
%  after the \meta{mc-prev}.
%  The structure must already exist.
%  The additional mcid dictionary is stored in a property. The item is retrieved
%  when the kid entry is built. We test if there is already
%  an addition and append if needed.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_mc_store:nnn #1 #2 #3 %#1 mc-prev, #2 mc-num #3 structure-num
  {
    %\prop_show:N \g_@@_struct_cont_mc_prop
    \prop_get:NnNTF \g_@@_struct_cont_mc_prop {#1} \l_@@_tmpa_tl
      {
        \prop_gput:Nnx \g_@@_struct_cont_mc_prop {#1}{ \l_@@_tmpa_tl \@@_struct_mcid_dict:n {#2}}
      }
      {
        \prop_gput:Nnx \g_@@_struct_cont_mc_prop {#1}{ \@@_struct_mcid_dict:n {#2}}
      }
    \prop_gput:Nxx \g_@@_mc_parenttree_prop
      {#2}
      {#3}
  }
\cs_generate_variant:Nn \@@_mc_store:nnn {xxx}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_mc_insert_extra_tmb:n,\@@_mc_insert_extra_tme:n}
% These two functions should be used in the output routine at the place
% where a mc-literal could be missing due to a page break or some other split.
% They check (with the help of the marks) if a extra-tmb or extra-tme is needed.
% The tmb command stores also the mc into the structure, the tme has to store
% the data for a following extra-tmb.
% The argument takes a stream name like main or footnote to allow different handling
% there.
% The content of the marks must be stored before (with |\@@_mc_get_marks:|
% or manually)
% into |\l_@@_mc_firstmarks_seq| and |\l_@@_mc_botmarks_seq| so that the tests
% can use them.
%
%    \begin{macrocode}
\cs_new_protected:Npn \@@_mc_insert_extra_tmb:n #1 % #1 stream: e.g. main or footnote
  {
     \@@_check_typeout_v:n {=>~ first~ \seq_use:Nn \l__tag_mc_firstmarks_seq {,~}}
     \@@_check_typeout_v:n {=>~ bot~ \seq_use:Nn \l__tag_mc_botmarks_seq {,~}}
     \@@_check_if_mc_tmb_missing:TF
       {
         \@@_check_typeout_v:n {=>~ TMB~ ~ missing~ --~ inserted}
         %test if artifact
         \int_compare:nNnTF { \seq_item:cn { g_@@_mc_#1_marks_seq } {3} } = {-1}
           {
              \tl_set:Nx \l_@@_tmpa_tl { \seq_item:cn { g_@@_mc_#1_marks_seq } {4} }
              \@@_mc_handle_artifact:N \l_@@_tmpa_tl
           }
           {
              \exp_args:Nx
              \@@_mc_bdc_mcid:n
                {
                  \seq_item:cn { g_@@_mc_#1_marks_seq } {4}
                }
              \str_if_eq:eeTF
                {
                  \seq_item:cn { g_@@_mc_#1_marks_seq } {5}
                }
                {}
                {
                  %store
                  \@@_mc_store:xxx
                    {
                      \seq_item:cn { g_@@_mc_#1_marks_seq } {2}
                    }
                    { \int_eval:n{\c@g__tag_MCID_abs_int} }
                    {
                      \seq_item:cn { g_@@_mc_#1_marks_seq } {3}
                    }
                }
                {
                   %stashed -> warning!!
                }
           }
       }
       {
         \@@_check_typeout_v:n {=>~ TMB~ not~ missing}
       }
  }

\cs_new_protected:Npn \@@_mc_insert_extra_tme:n #1 % #1 stream, eg. main or footnote
 {
   \@@_check_if_mc_tme_missing:TF
     {
       \@@_check_typeout_v:n {=>~ TME~ ~ missing~ --~ inserted}
       \@@_mc_emc:
       \seq_gset_eq:cN
         {  g_@@_mc_#1_marks_seq }
         \l_@@_mc_botmarks_seq
     }
     {
       \@@_check_typeout_v:n {=>~ TME~ not~ missing}
     }
 }
%    \end{macrocode}
% \end{macro}
%
%  \subsection{Looking at MC marks in boxes}
%  \begin{macro}{\@@_add_missing_mcs:Nn}
%    Assumptions:
%    \begin{itemize}
%    \item
%       test for tagging active outside;
%    \item
%       mark retrieval also outside.
%    \end{itemize}
%
%    This takes a box register as its first argument (or the register
%    number in a count register, as used by \pkg{multicol}). It adds
%    an extra tmb at the top of the box if necessary and similarly
%    an extra tme at the end. This is done by adding hboxes in a way
%    that the positioning and the baseline of the given box is not
%    altered.  The result is written back to the box.
%
%    The second argument is the stream this box belongs to und is
%    currently either \texttt{main} for the main galley,
%    \texttt{footnote} for footnote note text, or \texttt{multicol}
%    for boxes produced for columns in that environment. Other streams
%    may follow over time.
%    \begin{macrocode}
\cs_new:Npn\@@_add_missing_mcs:Nn #1 #2 {
  \vbadness \@M
  \vfuzz    \c_max_dim
  \vbox_set_to_ht:Nnn #1 { \box_ht:N #1 } {
    \hbox_set:Nn \l_@@_tmpa_box { \@@_mc_insert_extra_tmb:n {#2} }
    \hbox_set:Nn \l_@@_tmpb_box { \@@_mc_insert_extra_tme:n {#2} }
    \int_compare:nNnT {\l_@@_loglevel_int} > { 0 }
         {
           \seq_log:c { g_@@_mc_#2_marks_seq}
         }
%    \end{macrocode}
%    The box placed on the top gets zero size and thus will not affect
%    the box dimensions of the box we are modifying.
%    \begin{macrocode}
    \box_set_ht:Nn \l_@@_tmpa_box \c_zero_dim
    \box_set_dp:Nn \l_@@_tmpa_box \c_zero_dim
%    \end{macrocode}
%    The box added at the bottom will get the depth of the original
%    box. This way we can arrange that from the outside everything
%    looks as before.
%    \begin{macrocode}
    \box_set_ht:Nn \l_@@_tmpb_box \c_zero_dim
    \box_set_dp:Nn \l_@@_tmpb_box { \box_dp:N #1 }
%    \end{macrocode}
%    We need to set \cs{boxmaxdepth} in case the original box has an
%    unusually large depth, otherwise that depth is not preserved when
%    we string things together.
%    \begin{macrocode}
    \boxmaxdepth \@maxdepth
    \box_use_drop:N        \l_@@_tmpa_box
    \vbox_unpack_drop:N     #1
%    \end{macrocode}
%    Back up by the depth of the box as we add that later again.
%    \begin{macrocode}
    \tex_kern:D -\box_dp:N \l_@@_tmpb_box
%    \end{macrocode}
%    And we don't want any glue added when we add the box.
%    \begin{macrocode}
    \nointerlineskip
    \box_use_drop:N \l_@@_tmpb_box
  }
}
%    \end{macrocode}
%  \end{macro}

%  \begin{macro}{\@@_add_missing_mcs_to_stream:Nn}
%    This is the main command to add mc to the stream. It is therefor
%    guarded by the mc-boolean.
%
%    If we aren't in the main stream then processing is a bit more
%    complicated because to get at the marks in the box we need to
%    artifically split it and then look at the split marks.
%
%    First argument is the box to update and the second is the \enquote{stream}.
%    In lua mode the command is a no-op.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_add_missing_mcs_to_stream:Nn #1#2
  {
    \@@_check_if_active_mc:T {
%    \end{macrocode}
%    First set up a temp box for trial splitting.
%    \begin{macrocode}
    \vbadness\maxdimen
    \box_set_eq:NN \l_@@_tmpa_box #1
%    \end{macrocode}
%    Split the box to the largest size available. This should give us
%    all content (but to be sure that there is no issue we could test
%    out test box is empty now (not done).
%    \begin{macrocode}
    \vbox_set_split_to_ht:NNn \l_@@_tmpa_box \l_@@_tmpa_box \c_max_dim
%    \end{macrocode}
%    As a side effect of this split we should now have the first and
%    bottom split marks set up. We use this to set up
%    \cs{l_@@_mc_firstmarks_seq}
%    \begin{macrocode}
    \exp_args:NNx
    \seq_set_from_clist:Nn \l_@@_mc_firstmarks_seq
        { \tex_splitfirstmarks:D \g_@@_mc_marks }
%    \end{macrocode}
%    Some debugging info:
%    \begin{macrocode}
%    \iow_term:n { First~ mark~ from~ this~ box: }
%    \seq_log:N \l__tag_mc_firstmarks_seq
%    \end{macrocode}
%    If this mark was empty then clearly the bottom mark will too be
%    empty. Thus in this case we make use of the saved bot mark from
%    the previous chunk. Note that if this is the first chunk in the
%    stream the global seq would contain a random value, but then we
%    can't end in this branch because the basis assumption is that
%    streams are properly marked up so the first chunk would always
%    have a mark at the beginning!
%    \begin{macrocode}
    \seq_if_empty:NTF \l_@@_mc_firstmarks_seq
        {
          \@@_check_typeout_v:n
            {
              No~ marks~ so~ use~ saved~ bot~ mark:~
              \seq_use:cn {g__tag_mc_#2_marks_seq} {,~} \iow_newline:
            }
           \seq_set_eq:Nc \l_@@_mc_firstmarks_seq {g_@@_mc_#2_marks_seq}
%    \end{macrocode}
%    We also update the bot mark to the same value so that we can
%    later apply \cs{@@_add_missing_mcs:Nn} with the data
%    structures in place (see assumptions made there).
%    \begin{macrocode}
          \seq_set_eq:NN \l_@@_mc_botmarks_seq \l_@@_mc_firstmarks_seq
        }
%    \end{macrocode}
%    If there was a first mark then there is also a bot mark (and it
%    can't be the same as our marks always come in pairs).
%    So if that branch is chosen we update
%    \cs{l_@@_mc_botmarks_seq} from the bot mark.
%    \begin{macrocode}
        {
          \@@_check_typeout_v:n
            {
              Pick~ up~ new~ bot~ mark!
            }
          \exp_args:NNx
          \seq_set_from_clist:Nn \l_@@_mc_botmarks_seq
              { \tex_splitbotmarks:D   \g_@@_mc_marks }
        }
%    \end{macrocode}
%    Finally we call \cs{@@_add_missing_mcs:Nn} to add any missing
%    tmb/tme as needed,
%    \begin{macrocode}
    \@@_add_missing_mcs:Nn #1 {#2}
%%
    \seq_gset_eq:cN  {g_@@_mc_#2_marks_seq} \l_@@_mc_botmarks_seq
%%
  }
}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}[pTF]{\@@_mc_if_in:,\tag_mc_if_in:}
% This is a test if a mc is open or not. It depends simply on a global boolean:
% mc-chunks are added linearly so nesting should not be relevant.
%
% One exception are header and footer (perhaps they are more, but for now
% it doesn't seem so, so there are no dedicated code to handle this situation):
% When they are built and added to the page we could be both inside or outside a mc-chunk.
% But header and footer should ignore this and not push/pop or warn about nested mc.
% It is therefore important there to set and reset the boolean manually.
% See the tagpddocu-patches.sty for an example.
%    \begin{macrocode}
\prg_new_conditional:Nnn \@@_mc_if_in: {p,T,F,TF}
  {
    \bool_if:NTF \g_@@_in_mc_bool
      { \prg_return_true:  }
      { \prg_return_false: }
  }

\prg_new_eq_conditional:NNn \tag_mc_if_in: \@@_mc_if_in: {p,T,F,TF}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{ \@@_mc_bmc:n,\@@_mc_emc:,\@@_mc_bdc:nn,\@@_mc_bdc:nx}
% These are the low-level commands. There are now equal to the
% pdfmanagement commands generic mode, but we use an indirection
% in case luamode need something else.
% change 04.08.2018: the commands do not check the validity of the arguments or try
% to escape them, this should be done before using them.
%    \begin{macrocode}
% #1 tag, #2 properties
\cs_set_eq:NN \@@_mc_bmc:n  \pdf_bmc:n
\cs_set_eq:NN \@@_mc_emc:   \pdf_emc:
\cs_set_eq:NN \@@_mc_bdc:nn \pdf_bdc:nn
\cs_generate_variant:Nn \@@_mc_bdc:nn {nx}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \@@_mc_bdc_mcid:nn,\@@_mc_bdc_mcid:n,
%     \@@_mc_handle_mcid:nn,\@@_mc_handle_mcid:VV
%   }
%
% This create a BDC mark with an |/MCID| key. Most of the work here is to get
% the current number value for the MCID: they must be numbered by page
% starting with 0 and then successively.
% The first argument is the tag, e.g. |P| or |Span|, the second is used to pass
% more properties.
% We also define a wrapper around the low-level command as luamode will need
% something different.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_mc_bdc_mcid:nn #1 #2
  {
    \int_gincr:N \c@g_@@_MCID_abs_int
    \tl_set:Nx \l_@@_mc_ref_abspage_tl
      {
        \@@_ref_value:enn %3 args
          {
            mcid-\int_use:N \c@g_@@_MCID_abs_int
          }
          { tagabspage }
          {-1}
      }
    \prop_get:NoNTF
      \g_@@_MCID_byabspage_prop
      {
        \l_@@_mc_ref_abspage_tl
      }
      \l_@@_mc_tmpa_tl
      {
        %key already present, use value for MCID and add 1 for the next
        \int_gset:Nn \g_@@_MCID_tmp_bypage_int { \l_@@_mc_tmpa_tl }
        \@@_prop_gput:Nxx
          \g_@@_MCID_byabspage_prop
          { \l_@@_mc_ref_abspage_tl }
          { \int_eval:n {\l_@@_mc_tmpa_tl +1} }
      }
      {
        %key not present, set MCID to 0 and insert 1
        \int_gzero:N \g_@@_MCID_tmp_bypage_int
        \@@_prop_gput:Nxx
          \g_@@_MCID_byabspage_prop
          { \l_@@_mc_ref_abspage_tl }
          {1}
      }
    \@@_ref_label:en
      {
        mcid-\int_use:N \c@g_@@_MCID_abs_int
      }
      { mc }
     \@@_mc_bdc:nx
       {#1}
       { /MCID~\int_eval:n { \g_@@_MCID_tmp_bypage_int }~ \exp_not:n { #2 } }
 }
\cs_new_protected:Npn \@@_mc_bdc_mcid:n #1
  {
    \@@_mc_bdc_mcid:nn {#1} {}
  }

\cs_new_protected:Npn \@@_mc_handle_mcid:nn #1 #2 %#1 tag, #2 properties
  {
    \@@_mc_bdc_mcid:nn {#1} {#2}
  }

\cs_generate_variant:Nn \@@_mc_handle_mcid:nn {VV}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\@@_mc_handle_stash:n,\@@_mc_handle_stash:x}
% This is the handler which puts a mc into the
% the current structure. The argument is the number of the mc.
% Beside storing the mc into the structure, it also has to record the
% structure for the parent tree.
% The name is a bit confusing, it does \emph{not} handle mc with the stash key
% \ldots.
% TODO: why does luamode use it for begin + use, but generic mode only for begin?
%    \begin{macrocode}
\cs_new_protected:Npn \@@_mc_handle_stash:n #1 %1 mcidnum
  {
    \@@_check_mc_used:n {#1}
    \@@_struct_kid_mc_gput_right:nn
      { \g_@@_struct_stack_current_tl }
      {#1}
   \prop_gput:Nxx \g_@@_mc_parenttree_prop
     {#1}
     { \g_@@_struct_stack_current_tl }
  }
\cs_generate_variant:Nn \@@_mc_handle_stash:n { x }
%    \end{macrocode}
% \end{macro}

% \begin{macro}
%  {
%    \@@_mc_bmc_artifact:,
%    \@@_mc_bmc_artifact:n,
%    \@@_mc_handle_artifact:N
%   }
% Two commands to create artifacts, one without type, and one with.
% We define also a wrapper handler as luamode will need a different definition.
% TODO: perhaps later: more properties for artifacts
%    \begin{macrocode}
\cs_new_protected:Npn  \@@_mc_bmc_artifact:
  {
    \@@_mc_bmc:n {Artifact}
  }
\cs_new_protected:Npn \@@_mc_bmc_artifact:n #1
  {
    \@@_mc_bdc:nn {Artifact}{/Type/#1}
  }
\cs_new_protected:Npn \@@_mc_handle_artifact:N #1
   % #1 is a var containing the artifact type
  {
    \int_gincr:N \c@g_@@_MCID_abs_int
    \tl_if_empty:NTF #1
      { \@@_mc_bmc_artifact: }
      { \exp_args:NV\@@_mc_bmc_artifact:n #1 }
  }
%    \end{macrocode}
% \end{macro}

% \begin{macro}{ \@@_get_data_mc_tag: }
% This allows to retrieve the active mc-tag.
% It is use by the get command.
%    \begin{macrocode}
\cs_new:Nn \@@_get_data_mc_tag: { \g_@@_mc_key_tag_tl }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tag_mc_begin:n,\tag_mc_end:}
% These are the core public commands to open and close an mc.
% They don't need to be in the same group or grouping level,
% but the code expect that they are issued linearly. The tag and
% the state is passed to the end command through a global var and
% a global boolean.
%    \begin{macrocode}
\cs_new_protected:Npn \tag_mc_begin:n #1 %#1 keyval
  {
    \@@_check_if_active_mc:T
      {
        \group_begin: %hm
        \@@_check_mc_if_nested:
        \bool_gset_true:N \g_@@_in_mc_bool
        \keys_set:nn { @@ / mc } {#1}
        \bool_if:NTF \l_@@_mc_artifact_bool
          { %handle artifact
            \@@_mc_handle_artifact:N \l_@@_mc_artifact_type_tl
            \exp_args:NV
            \@@_mc_artifact_begin_marks:n \l_@@_mc_artifact_type_tl
          }
          { %handle mcid type
            \@@_check_mc_tag:N  \l_@@_mc_key_tag_tl
            \@@_mc_handle_mcid:VV
               \l_@@_mc_key_tag_tl
               \l_@@_mc_key_properties_tl
            \@@_mc_begin_marks:oo{\l_@@_mc_key_tag_tl}{\l_@@_mc_key_label_tl}
            \tl_if_empty:NF {\l_@@_mc_key_label_tl}
              {
                \exp_args:NV
                \@@_mc_handle_mc_label:n \l_@@_mc_key_label_tl
              }
            \bool_if:NF \l_@@_mc_key_stash_bool
              {
                \@@_mc_handle_stash:x { \int_use:N \c@g_@@_MCID_abs_int }
              }
          }
        \group_end:
      }
  }
\cs_new_protected:Nn \tag_mc_end:
  {
    \@@_check_if_active_mc:T
      {
        \@@_check_mc_if_open:
        \bool_gset_false:N \g_@@_in_mc_bool
        \tl_gset:Nn  \g_@@_mc_key_tag_tl { }
        \@@_mc_emc:
        \@@_mc_end_marks:
      }
  }
%    \end{macrocode}
% \end{macro}

%
% \subsection{Keys}
% Definitions are different in luamode.
% |tag| and |raw| are expanded as |\lua_now:e| in lua does it too and
% we assume that their values are safe.
% \begin{macro}
%  {
%   tag,raw,
%   alttext,
%   actualtext,
%   label,artifact
%  }
%    \begin{macrocode}
\keys_define:nn { @@ / mc }
  {
    tag .code:n = % the name (H,P,Span) etc
      {
        \tl_set:Nx   \l_@@_mc_key_tag_tl { #1 }
        \tl_gset:Nx  \g_@@_mc_key_tag_tl { #1 }
      },
    raw  .code:n =
      {
        \tl_put_right:Nx \l_@@_mc_key_properties_tl { #1 }
      },
    alttext .code:n      = % Alt property
      {
        \str_set_convert:Noon
          \l_@@_tmpa_str
          { #1 }
          { default }
          { utf16/hex }
        \tl_put_right:Nn \l_@@_mc_key_properties_tl { /Alt~< }
        \tl_put_right:No \l_@@_mc_key_properties_tl { \l_@@_tmpa_str>~ }
      },
    actualtext .code:n      = % ActualText property
      {
        \str_set_convert:Noon
          \l_@@_tmpa_str
          { #1 }
          { default }
          { utf16/hex }
        \tl_put_right:Nn \l_@@_mc_key_properties_tl { /ActualText~< }
        \tl_put_right:No \l_@@_mc_key_properties_tl { \l_@@_tmpa_str>~ }
      },
    label .tl_set:N        = \l_@@_mc_key_label_tl,
    artifact .code:n       =
      {
        \exp_args:Nnx
          \keys_set:nn
            { @@ / mc }
            { __artifact-bool, __artifact-type=#1 }
      },
    artifact .default:n    = {notype}
  }
%</generic>
%    \end{macrocode}
% \end{macro}
% \end{implementation}
% \PrintIndex