%%
%% This is file `tagpdf-mc-code-lua.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% tagpdf-mc-luacode.dtx  (with options: `luamode')
%% 
%% Copyright (C) 2019-2024 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.
%% 
%% File: tagpdf-mc-luacode.dtx
\ProvidesExplPackage {tagpdf-mc-code-lua} {2025-03-26} {0.99p}
  {tagpdf - mc code only for the luamode }
\hook_gput_code:nnn{begindocument}{tagpdf/mc}
  {
    \bool_if:NT\g__tag_active_space_bool
      {
        \lua_now:e
          {
            if~luatexbase.callbacktypes.pre_shipout_filter~then~
              luatexbase.add_to_callback("pre_shipout_filter", function(TAGBOX)~
              ltx.__tag.func.space_chars_shipout(TAGBOX)~return~true~
              end, "tagpdf")~
              if~luatexbase.declare_callback_rule~then~
                luatexbase.declare_callback_rule("pre_shipout_filter", "luaotfload.dvi", "after", "tagpdf")
              end~
            end
          }
       \lua_now:e
         {
           if~luatexbase.callbacktypes.pre_shipout_filter~then~
           token.get_next()~
           end
         }\@secondoftwo\@gobble
           {
             \hook_gput_code:nnn{shipout/before}{tagpdf/lua}
               {
                \lua_now:e
                   { ltx.__tag.func.space_chars_shipout (tex.box["ShipoutBox"]) }
               }
           }
      }
    \bool_if:NT\g__tag_active_mc_bool
      {
        \lua_now:e
          {
            if~luatexbase.callbacktypes.pre_shipout_filter~then~
              luatexbase.add_to_callback("pre_shipout_filter", function(TAGBOX)~
              ltx.__tag.func.mark_shipout(TAGBOX)~return~true~
              end, "tagpdf")~
            end
          }
       \lua_now:e
         {
           if~luatexbase.callbacktypes.pre_shipout_filter~then~
           token.get_next()~
           end
         }\@secondoftwo\@gobble
           {
             \hook_gput_code:nnn{shipout/before}{tagpdf/lua}
               {
                 \lua_now:e
                   { ltx.__tag.func.mark_shipout (tex.box["ShipoutBox"]) }
               }
           }
      }
  }
\cs_new_protected:Npn \__tag_add_missing_mcs_to_stream:Nn #1#2 {}
\cs_set_eq:NN \tag_mc_add_missing_to_stream:Nn \__tag_add_missing_mcs_to_stream:Nn
\cs_new_protected:Npn  \tag_mc_new_stream:n #1 {}
\prg_new_conditional:Nnn \__tag_mc_if_in: {p,T,F,TF}
  {
    \int_compare:nNnTF
      { -2147483647 }
      =
      {\lua_now:e
         {
           tex.print(\int_use:N \c_document_cctab,tex.getattribute(luatexbase.attributes.g__tag_mc_type_attr))
         }
      }
      { \prg_return_false:  }
      { \prg_return_true: }
  }

\prg_new_eq_conditional:NNn \tag_mc_if_in: \__tag_mc_if_in: {p,T,F,TF}
\cs_new:Nn \__tag_mc_lua_set_mc_type_attr:n % #1 is a tag name
  {
    %TODO ltx.__tag.func.get_num_from("#1") seems not to return a suitable number??
    \tl_set:Ne\l__tag_tmpa_tl{\lua_now:e{ltx.__tag.func.output_num_from ("#1")} }
    \lua_now:e
      {
        tex.setattribute
         (
          "global",
          luatexbase.attributes.g__tag_mc_type_attr,
          \l__tag_tmpa_tl
         )
      }
    \lua_now:e
      {
        tex.setattribute
         (
           "global",
           luatexbase.attributes.g__tag_mc_cnt_attr,
           \__tag_get_mc_abs_cnt:
         )
      }
  }

\cs_generate_variant:Nn\__tag_mc_lua_set_mc_type_attr:n { o }

\cs_new:Nn \__tag_mc_lua_unset_mc_type_attr:
  {
    \lua_now:e
      {
        tex.setattribute
          (
            "global",
            luatexbase.attributes.g__tag_mc_type_attr,
            -2147483647
          )
      }
     \lua_now:e
      {
        tex.setattribute
          (
            "global",
            luatexbase.attributes.g__tag_mc_cnt_attr,
            -2147483647
          )
      }
  }

\cs_new:Nn \__tag_mc_insert_mcid_kids:n
  {
    \lua_now:e { ltx.__tag.func.mc_insert_kids (#1,0) }
  }

\cs_new:Nn \__tag_mc_insert_mcid_single_kids:n
  {
    \lua_now:e {ltx.__tag.func.mc_insert_kids (#1,1) }
  }
\cs_new_protected:Npn \__tag_mc_handle_stash:n #1 %1 mcidnum
  {
    \__tag_check_mc_used:n { #1 }
    \seq_gput_right:cn % Don't fill a lua table due to the command in the item,
                       % so use the kernel command
      { g__tag_struct_kids_\g__tag_struct_stack_current_tl _seq }
      {
        \__tag_mc_insert_mcid_kids:n {#1}%
      }
    \lua_now:e
      {
        ltx.__tag.func.store_struct_mcabs
          (
            \g__tag_struct_stack_current_tl,#1
          )
      }
    \prop_gput:Nee
      \g__tag_mc_parenttree_prop
      { #1 }
      { \g__tag_struct_stack_current_tl }
  }
\cs_generate_variant:Nn \__tag_mc_handle_stash:n { e }
\cs_set_protected:Nn \tag_mc_begin:n
  {
    \__tag_check_if_active_mc:T
      {
        \group_begin:
        %\__tag_check_mc_if_nested:
        \bool_gset_true:N \g__tag_in_mc_bool
        \bool_set_false:N\l__tag_mc_artifact_bool
        \tl_clear:N \l__tag_mc_key_properties_tl
        \int_gincr:N \c@g__tag_MCID_abs_int
        \tl_set_eq:NN \l__tag_mc_key_tag_tl \g__tag_struct_tag_tl
        \tl_gset_eq:NN\g__tag_mc_key_tag_tl \g__tag_struct_tag_tl
        \lua_now:e
          {
            ltx.__tag.func.store_mc_data(\__tag_get_mc_abs_cnt:,"tag","\g__tag_struct_tag_tl")
          }
        \keys_set:nn { __tag / mc }{ label={}, #1 }
        %check that a tag or artifact has been used
        \__tag_check_mc_tag:N \l__tag_mc_key_tag_tl
        %set the attributes:
        \__tag_mc_lua_set_mc_type_attr:o  { \l__tag_mc_key_tag_tl }
        \bool_if:NF \l__tag_mc_artifact_bool
          { % store the absolute num name in a label:
            \tl_if_empty:NF {\l__tag_mc_key_label_tl}
              {
                \exp_args:NV
                 \__tag_mc_handle_mc_label:e  \l__tag_mc_key_label_tl
              }
           % if not stashed record the absolute number
            \bool_if:NF \l__tag_mc_key_stash_bool
              {
                \exp_args:NV\__tag_struct_get_parentrole:nNN
                  \g__tag_struct_stack_current_tl
                  \l__tag_get_parent_tmpa_tl
                  \l__tag_get_parent_tmpb_tl
                \__tag_check_parent_child:VVnnN
                  \l__tag_get_parent_tmpa_tl
                  \l__tag_get_parent_tmpb_tl
                  {MC}{}
                  \l__tag_parent_child_check_tl
                \int_compare:nNnT {\l__tag_parent_child_check_tl}<{0}
                 {
                   \prop_get:cnN
                    { g__tag_struct_ \g__tag_struct_stack_current_tl _prop}
                    {S}
                    \l__tag_tmpa_tl
                   \msg_warning:nneee
                     { tag }
                     {role-parent-child}
                     { \l__tag_get_parent_tmpa_tl/\l__tag_get_parent_tmpb_tl  }
                     { MC~(real content) }
                     {
                       not~allowed~
                       (struct~\g__tag_struct_stack_current_tl,~\l__tag_tmpa_tl)
                     }
                 }
                \__tag_mc_handle_stash:e { \__tag_get_mc_abs_cnt: }
              }
          }
        \group_end:
     }
  }
\cs_set_protected:Nn \tag_mc_end:
  {
    \__tag_check_if_active_mc:T
      {
        %\__tag_check_mc_if_open:
        \bool_gset_false:N \g__tag_in_mc_bool
        \bool_set_false:N\l__tag_mc_artifact_bool
        \__tag_mc_lua_unset_mc_type_attr:
        \tl_set:Nn  \l__tag_mc_key_tag_tl { }
        \tl_gset:Nn \g__tag_mc_key_tag_tl { }
      }
  }
\cs_set_protected:Npn \tag_mc_reset_box:N #1
  {
    \lua_now:e
     {
       local~type=tex.getattribute(luatexbase.attributes.g__tag_mc_type_attr)
       local~mc=tex.getattribute(luatexbase.attributes.g__tag_mc_cnt_attr)
       ltx.__tag.func.update_mc_attributes(tex.getbox(\int_use:N #1),mc,type)
     }
  }
\cs_new:Npn \__tag_get_data_mc_tag: { \g__tag_mc_key_tag_tl }

\keys_define:nn { __tag / mc }
  {
    tag .code:n = %
      {
        \tl_set:Ne   \l__tag_mc_key_tag_tl { #1 }
        \tl_gset:Ne  \g__tag_mc_key_tag_tl { #1 }
        \lua_now:e
          {
            ltx.__tag.func.store_mc_data(\__tag_get_mc_abs_cnt:,"tag","#1")
          }
      },
    raw .code:n =
      {
        \tl_put_right:Ne \l__tag_mc_key_properties_tl { #1 }
        \lua_now:e
          {
            ltx.__tag.func.store_mc_data(\__tag_get_mc_abs_cnt:,"raw","#1")
          }
      },
    alt .code:n      = % Alt property
      {
        \tl_if_empty:oF{#1}
          {
            \str_set_convert:Noon
              \l__tag_tmpa_str
              { #1 }
              { default }
              { utf16/hex }
            \tl_put_right:Nn \l__tag_mc_key_properties_tl { /Alt~< }
            \tl_put_right:No \l__tag_mc_key_properties_tl { \l__tag_tmpa_str>~ }
            \lua_now:e
              {
                ltx.__tag.func.store_mc_data
                  (
                    \__tag_get_mc_abs_cnt:,"alt","/Alt~<\str_use:N \l__tag_tmpa_str>"
                  )
              }
           }
      },
    alttext .meta:n = {alt=#1},
    actualtext .code:n      = % Alt property
      {
        \tl_if_empty:oF{#1}
          {
            \str_set_convert:Noon
              \l__tag_tmpa_str
              { #1 }
              { default }
              { utf16/hex }
            \tl_put_right:Nn \l__tag_mc_key_properties_tl { /Alt~< }
            \tl_put_right:No \l__tag_mc_key_properties_tl { \l__tag_tmpa_str>~ }
            \lua_now:e
              {
                ltx.__tag.func.store_mc_data
                  (
                    \__tag_get_mc_abs_cnt:,
                    "actualtext",
                    "/ActualText~<\str_use:N \l__tag_tmpa_str>"
                  )
              }
          }
      },
    label .code:n =
      {
        \tl_set:Nn\l__tag_mc_key_label_tl { #1 }
        \lua_now:e
          {
            ltx.__tag.func.store_mc_data
              (
                \__tag_get_mc_abs_cnt:,"label","#1"
              )
          }
      },
    __artifact-store .code:n =
      {
        \lua_now:e
          {
            ltx.__tag.func.store_mc_data
              (
                \__tag_get_mc_abs_cnt:,"artifact","#1"
              )
          }
      },
    artifact .code:n       =
      {
        \exp_args:Nne
          \keys_set:nn
            { __tag / mc}
            { __artifact-bool, __artifact-type=#1, tag=Artifact }
        \exp_args:Nne
          \keys_set:nn
            { __tag / mc }
            { __artifact-store=\l__tag_mc_artifact_type_tl }
      },
    artifact .default:n    = { notype }
  }

%% 
%%
%% End of file `tagpdf-mc-code-lua.sty'.
