%%
%% This is file `luamplib.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% luamplib.dtx  (with options: `package')
%% 
%% See source file 'luamplib.dtx' for licencing and contact information.
%% 
\ifcsname ProvidesPackage\endcsname
  \NeedsTeXFormat{LaTeX2e}
  \ProvidesPackage{luamplib}
    [2025/02/18 v2.37.1 mplib package for LuaTeX]
\fi
\ifdefined\newluafunction\else
  \input ltluatex
\fi
\ifnum\outputmode=0
  \ifdefined\AddToHookNext
    \def\luamplibatnextshipout{\AddToHookNext{shipout/background}}
    \def\luamplibatfirstshipout{\AddToHook{shipout/firstpage}}
    \def\luamplibateveryshipout{\AddToHook{shipout/background}}
  \else
    \input atbegshi.sty
    \def\luamplibatnextshipout#1{\AtBeginShipoutNext{\AtBeginShipoutAddToBox{#1}}}
    \let\luamplibatfirstshipout\AtBeginShipoutFirst
    \def\luamplibateveryshipout#1{\AtBeginShipout{\AtBeginShipoutAddToBox{#1}}}
  \fi
\fi
\directlua{require("luamplib")}
\ifx\pdfoutput\undefined
  \let\pdfoutput\outputmode
\fi
\ifx\pdfliteral\undefined
  \protected\def\pdfliteral{\pdfextension literal}
\fi
\def\mplibsetformat#1{\directlua{luamplib.setformat("#1")}}
\ifnum\pdfoutput>0
  \let\mplibtoPDF\pdfliteral
\else
  \def\mplibtoPDF#1{\special{pdf:literal direct #1}}
  \ifcsname PackageInfo\endcsname
    \PackageInfo{luamplib}{only dvipdfmx is supported currently}
  \else
    \immediate\write-1{luamplib Info: only dvipdfmx is supported currently}
  \fi
\fi
\def\mplibforcehmode{\let\prependtomplibbox\leavevmode}
\def\mplibnoforcehmode{\let\prependtomplibbox\relax}
\mplibnoforcehmode
\def\mplibsetupcatcodes{%
  %catcode`\{=12 %catcode`\}=12
  \catcode`\#=12 \catcode`\^=12 \catcode`\~=12 \catcode`\_=12
  \catcode`\&=12 \catcode`\$=12 \catcode`\%=12 \catcode`\^^M=12
}
\def\mplibputtextbox#1{\vbox to 0pt{\vss\hbox to 0pt{\raise\dp#1\copy#1\hss}}}
\protected\def\usemplibgroup#1#{\usemplibgroupmain}
\def\usemplibgroupmain#1{%
  \mplibstarttousemplibgroup
  \csname luamplib.group.#1\endcsname
  \mplibstoptousemplibgroup
}
\def\mplibstarttousemplibgroup{\prependtomplibbox\hbox dir TLT\bgroup}
\def\mplibstoptousemplibgroup{\egroup}
\protected\def\mplibgroup#1{%
  \begingroup
  \def\MPllx{0}\def\MPlly{0}%
  \def\mplibgroupname{#1}%
  \mplibgroupgetnexttok
}
\def\mplibgroupgetnexttok{\futurelet\nexttok\mplibgroupbranch}
\def\mplibgroupskipspace{\afterassignment\mplibgroupgetnexttok\let\nexttok= }
\def\mplibgroupbranch{%
  \ifx [\nexttok
    \expandafter\mplibgroupopts
  \else
    \ifx\mplibsptoken\nexttok
      \expandafter\expandafter\expandafter\mplibgroupskipspace
    \else
      \let\mplibgroupoptions\empty
      \expandafter\expandafter\expandafter\mplibgroupmain
    \fi
  \fi
}
\def\mplibgroupopts[#1]{\def\mplibgroupoptions{#1}\mplibgroupmain}
\def\mplibgroupmain{\setbox\mplibscratchbox\hbox\bgroup\ignorespaces}
\protected\def\endmplibgroup{\egroup
  \directlua{ luamplib.registergroup(
    \the\mplibscratchbox, '\mplibgroupname', {\mplibgroupoptions}
  )}%
  \endgroup
}
{\def\:{\global\let\mplibsptoken= } \: }
\protected\def\mppattern#1{%
  \begingroup
  \def\mplibpatternname{#1}%
  \mplibpatterngetnexttok
}
\def\mplibpatterngetnexttok{\futurelet\nexttok\mplibpatternbranch}
\def\mplibpatternskipspace{\afterassignment\mplibpatterngetnexttok\let\nexttok= }
\def\mplibpatternbranch{%
  \ifx [\nexttok
    \expandafter\mplibpatternopts
  \else
    \ifx\mplibsptoken\nexttok
      \expandafter\expandafter\expandafter\mplibpatternskipspace
    \else
      \let\mplibpatternoptions\empty
      \expandafter\expandafter\expandafter\mplibpatternmain
    \fi
  \fi
}
\def\mplibpatternopts[#1]{%
  \def\mplibpatternoptions{#1}%
  \mplibpatternmain
}
\def\mplibpatternmain{%
  \setbox\mplibscratchbox\hbox\bgroup\ignorespaces
}
\protected\def\endmppattern{%
  \egroup
  \directlua{ luamplib.registerpattern(
    \the\mplibscratchbox, '\mplibpatternname', {\mplibpatternoptions}
  )}%
  \endgroup
}
\def\mpfiginstancename{@mpfig}
\protected\def\mpfig{%
  \begingroup
  \futurelet\nexttok\mplibmpfigbranch
}
\def\mplibmpfigbranch{%
  \ifx *\nexttok
    \expandafter\mplibprempfig
  \else
    \ifx [\nexttok
      \expandafter\expandafter\expandafter\mplibgobbleoptsmpfig
    \else
      \expandafter\expandafter\expandafter\mplibmainmpfig
    \fi
  \fi
}
\def\mplibgobbleoptsmpfig[#1]{\mplibmainmpfig}
\def\mplibmainmpfig{%
  \begingroup
  \mplibsetupcatcodes
  \mplibdomainmpfig
}
\long\def\mplibdomainmpfig#1\endmpfig{%
  \endgroup
  \directlua{
    local legacy = luamplib.legacyverbatimtex
    local everympfig = luamplib.everymplib["\mpfiginstancename"] or ""
    local everyendmpfig = luamplib.everyendmplib["\mpfiginstancename"] or ""
    luamplib.legacyverbatimtex = false
    luamplib.everymplib["\mpfiginstancename"] = ""
    luamplib.everyendmplib["\mpfiginstancename"] = ""
    luamplib.process_mplibcode(
    "beginfig(0) "..everympfig.." "..[===[\unexpanded{#1}]===].." "..everyendmpfig.." endfig;",
    "\mpfiginstancename")
    luamplib.legacyverbatimtex = legacy
    luamplib.everymplib["\mpfiginstancename"] = everympfig
    luamplib.everyendmplib["\mpfiginstancename"] = everyendmpfig
  }%
  \endgroup
}
\def\mplibprempfig#1{%
  \begingroup
  \mplibsetupcatcodes
  \mplibdoprempfig
}
\long\def\mplibdoprempfig#1\endmpfig{%
  \endgroup
  \directlua{
    local legacy = luamplib.legacyverbatimtex
    local everympfig = luamplib.everymplib["\mpfiginstancename"]
    local everyendmpfig = luamplib.everyendmplib["\mpfiginstancename"]
    luamplib.legacyverbatimtex = false
    luamplib.everymplib["\mpfiginstancename"] = ""
    luamplib.everyendmplib["\mpfiginstancename"] = ""
    luamplib.process_mplibcode([===[\unexpanded{#1}]===],"\mpfiginstancename")
    luamplib.legacyverbatimtex = legacy
    luamplib.everymplib["\mpfiginstancename"] = everympfig
    luamplib.everyendmplib["\mpfiginstancename"] = everyendmpfig
  }%
  \endgroup
}
\protected\def\endmpfig{endmpfig}
\unless\ifcsname ver@luamplib.sty\endcsname
  \def\mplibcodegetinstancename[#1]{\xdef\currentmpinstancename{#1}\mplibcodeindeed}
  \protected\def\mplibcode{%
    \begingroup
    \futurelet\nexttok\mplibcodebranch
  }
  \def\mplibcodebranch{%
    \ifx [\nexttok
      \expandafter\mplibcodegetinstancename
    \else
      \global\let\currentmpinstancename\empty
      \expandafter\mplibcodeindeed
    \fi
  }
  \def\mplibcodeindeed{%
    \begingroup
    \mplibsetupcatcodes
    \mplibdocode
  }
  \long\def\mplibdocode#1\endmplibcode{%
    \endgroup
    \directlua{luamplib.process_mplibcode([===[\unexpanded{#1}]===],"\currentmpinstancename")}%
    \endgroup
  }
  \protected\def\endmplibcode{endmplibcode}
\else
  \newenvironment{mplibcode}[1][]{%
    \xdef\currentmpinstancename{#1}%
    \mplibtmptoks{}\ltxdomplibcode
  }{}
  \def\ltxdomplibcode{%
    \begingroup
    \mplibsetupcatcodes
    \ltxdomplibcodeindeed
  }
  \def\mplib@mplibcode{mplibcode}
  \long\def\ltxdomplibcodeindeed#1\end#2{%
    \endgroup
    \mplibtmptoks\expandafter{\the\mplibtmptoks#1}%
    \def\mplibtemp@a{#2}%
    \ifx\mplib@mplibcode\mplibtemp@a
      \directlua{luamplib.process_mplibcode([===[\the\mplibtmptoks]===],"\currentmpinstancename")}%
      \end{mplibcode}%
    \else
      \mplibtmptoks\expandafter{\the\mplibtmptoks\end{#2}}%
      \expandafter\ltxdomplibcode
    \fi
  }
\fi
\def\mplibshowlog#1{\directlua{
    local s = string.lower("#1")
    if s == "enable" or s == "true" or s == "yes" then
      luamplib.showlog = true
    else
      luamplib.showlog = false
    end
}}
\def\mpliblegacybehavior#1{\directlua{
    local s = string.lower("#1")
    if s == "enable" or s == "true" or s == "yes" then
      luamplib.legacyverbatimtex = true
    else
      luamplib.legacyverbatimtex = false
    end
}}
\def\mplibverbatim#1{\directlua{
    local s = string.lower("#1")
    if s == "enable" or s == "true" or s == "yes" then
      luamplib.verbatiminput = true
    else
      luamplib.verbatiminput = false
    end
}}
\newtoks\mplibtmptoks
\ifcsname ver@luamplib.sty\endcsname
  \protected\def\everymplib{%
    \begingroup
    \mplibsetupcatcodes
    \mplibdoeverymplib
  }
  \protected\def\everyendmplib{%
    \begingroup
    \mplibsetupcatcodes
    \mplibdoeveryendmplib
  }
  \newcommand\mplibdoeverymplib[2][]{%
    \endgroup
    \directlua{
      luamplib.everymplib["#1"] = [===[\unexpanded{#2}]===]
    }%
  }
  \newcommand\mplibdoeveryendmplib[2][]{%
    \endgroup
    \directlua{
      luamplib.everyendmplib["#1"] = [===[\unexpanded{#2}]===]
    }%
  }
\else
  \def\mplibgetinstancename[#1]{\def\currentmpinstancename{#1}}
  \protected\def\everymplib#1#{%
    \ifx\empty#1\empty \mplibgetinstancename[]\else \mplibgetinstancename#1\fi
    \begingroup
    \mplibsetupcatcodes
    \mplibdoeverymplib
  }
  \long\def\mplibdoeverymplib#1{%
    \endgroup
    \directlua{
      luamplib.everymplib["\currentmpinstancename"] = [===[\unexpanded{#1}]===]
    }%
  }
  \protected\def\everyendmplib#1#{%
    \ifx\empty#1\empty \mplibgetinstancename[]\else \mplibgetinstancename#1\fi
    \begingroup
    \mplibsetupcatcodes
    \mplibdoeveryendmplib
  }
  \long\def\mplibdoeveryendmplib#1{%
    \endgroup
    \directlua{
      luamplib.everyendmplib["\currentmpinstancename"] = [===[\unexpanded{#1}]===]
    }%
  }
\fi
\def\mpdim#1{ runscript("luamplibdimen{#1}") }
\def\mpcolor#1#{\domplibcolor{#1}}
\def\domplibcolor#1#2{ runscript("luamplibcolor{#1{#2}}") }
\def\mplibnumbersystem#1{\directlua{
  local t = "#1"
  if t == "binary" then t = "decimal" end
  luamplib.numbersystem = t
}}
\def\mplibmakenocache#1{\mplibdomakenocache #1,*,}
\def\mplibdomakenocache#1,{%
  \ifx\empty#1\empty
    \expandafter\mplibdomakenocache
  \else
    \ifx*#1\else
      \directlua{luamplib.noneedtoreplace["#1.mp"]=true}%
      \expandafter\expandafter\expandafter\mplibdomakenocache
    \fi
  \fi
}
\def\mplibcancelnocache#1{\mplibdocancelnocache #1,*,}
\def\mplibdocancelnocache#1,{%
  \ifx\empty#1\empty
    \expandafter\mplibdocancelnocache
  \else
    \ifx*#1\else
      \directlua{luamplib.noneedtoreplace["#1.mp"]=false}%
      \expandafter\expandafter\expandafter\mplibdocancelnocache
    \fi
  \fi
}
\def\mplibcachedir#1{\directlua{luamplib.getcachedir("\unexpanded{#1}")}}
\def\mplibtextextlabel#1{\directlua{
    local s = string.lower("#1")
    if s == "enable" or s == "true" or s == "yes" then
      luamplib.textextlabel = true
    else
      luamplib.textextlabel = false
    end
}}
\def\mplibcodeinherit#1{\directlua{
    local s = string.lower("#1")
    if s == "enable" or s == "true" or s == "yes" then
      luamplib.codeinherit = true
    else
      luamplib.codeinherit = false
    end
}}
\def\mplibglobaltextext#1{\directlua{
    local s = string.lower("#1")
    if s == "enable" or s == "true" or s == "yes" then
      luamplib.globaltextext = true
    else
      luamplib.globaltextext = false
    end
}}
\ifx\mplibscratchbox\undefined \newbox\mplibscratchbox \fi
\def\mplibstarttoPDF#1#2#3#4{%
  \prependtomplibbox
  \hbox dir TLT\bgroup
  \xdef\MPllx{#1}\xdef\MPlly{#2}%
  \xdef\MPurx{#3}\xdef\MPury{#4}%
  \xdef\MPwidth{\the\dimexpr#3bp-#1bp\relax}%
  \xdef\MPheight{\the\dimexpr#4bp-#2bp\relax}%
  \parskip0pt%
  \leftskip0pt%
  \parindent0pt%
  \everypar{}%
  \setbox\mplibscratchbox\vbox\bgroup
  \noindent
}
\def\mplibstoptoPDF{%
  \par
  \egroup %
  \setbox\mplibscratchbox\hbox %
    {\hskip-\MPllx bp%
     \raise-\MPlly bp%
     \box\mplibscratchbox}%
  \setbox\mplibscratchbox\vbox to \MPheight
    {\vfill
     \hsize\MPwidth
     \wd\mplibscratchbox0pt%
     \ht\mplibscratchbox0pt%
     \dp\mplibscratchbox0pt%
     \box\mplibscratchbox}%
  \wd\mplibscratchbox\MPwidth
  \ht\mplibscratchbox\MPheight
  \box\mplibscratchbox
  \egroup
}
\def\mplibtextext#1#2#3#4#5{%
  \begingroup
  \setbox\mplibscratchbox\hbox
    {\font\temp=#1 at #2bp%
     \temp
     #3}%
  \setbox\mplibscratchbox\hbox
    {\hskip#4 bp%
     \raise#5 bp%
     \box\mplibscratchbox}%
  \wd\mplibscratchbox0pt%
  \ht\mplibscratchbox0pt%
  \dp\mplibscratchbox0pt%
  \box\mplibscratchbox
  \endgroup
}
\openin0=luamplib.cfg
\ifeof0 \else
  \closein0
  \input luamplib.cfg
\fi
\def\luamplibtagtextbegin#1{}
\let\luamplibtagtextend\relax
\let\luamplibtagasgroupbegin\relax
\let\luamplibtagasgroupend\relax
\ifcsname SuspendTagging\endcsname\else\endinput\fi
\ifcsname ver@tagpdf.sty\endcsname \else
  \ExplSyntaxOn
  \keys_define:nn{luamplib/notag}
    {
      ,alt          .code:n = { }
      ,actualtext   .code:n = { }
      ,artifact     .code:n = { }
      ,text         .code:n = { }
      ,correct-BBox .code:n = { }
      ,tag          .code:n = { }
      ,debug        .code:n = { }
      ,instance     .code:n = { \tl_gset:Nn \currentmpinstancename {#1} }
      ,instancename .meta:n = { instance = {#1} }
      ,unknown      .code:n = { \tl_gset:Ne \currentmpinstancename {\l_keys_key_str} }
    }
  \RenewDocumentCommand\mplibcode{O{}}
    {
      \tl_gset_eq:NN \currentmpinstancename \c_empty_tl
      \keys_set:ne{luamplib/notag}{#1}
      \mplibtmptoks{}\ltxdomplibcode
    }
  \ExplSyntaxOff
  \let\mplibalttext \luamplibtagtextbegin
  \let\mplibactualtext \mplibalttext
  \endinput\fi
\let\mplibstarttoPDForiginal\mplibstarttoPDF
\let\mplibstoptoPDForiginal\mplibstoptoPDF
\let\mplibputtextboxoriginal\mplibputtextbox
\let\mplibstarttousemplibgrouporiginal\mplibstarttousemplibgroup
\let\mplibstoptousemplibgrouporiginal\mplibstoptousemplibgroup
\ExplSyntaxOn
\tl_new:N \l__luamplib_tag_alt_tl
\tl_new:N \l__luamplib_tag_alt_dflt_tl
\tl_set:Nn\l__luamplib_tag_alt_dflt_tl {metapost~figure}
\tl_new:N \l__luamplib_tag_actual_tl
\tl_new:N \l__luamplib_tag_struct_tl
\tl_set:Nn\l__luamplib_tag_struct_tl {Figure}
\bool_new:N \l__luamplib_tag_usetext_bool
\bool_new:N \l__luamplib_tag_BBox_bool
\bool_set_true:N \l__luamplib_tag_BBox_bool
\seq_new:N\l__luamplib_tag_bboxcorr_seq
\bool_new:N\l__luamplib_tag_bboxcorr_bool
\bool_new:N \l__luamplib_tag_debug_bool
\tl_new:N \l__luamplib_BBox_label_tl
\tl_new:N \l__luamplib_BBox_llx_tl
\tl_new:N \l__luamplib_BBox_lly_tl
\tl_new:N \l__luamplib_BBox_urx_tl
\tl_new:N \l__luamplib_BBox_ury_tl
\cs_set_nopar:Npn \luamplibtagtextbegin #1
{
  \bool_if:NTF \l__luamplib_tag_usetext_bool
  {
    \tag_mc_end_push:
    \tag_mc_begin:n{}
    \tag_struct_begin:n{tag=NonStruct,stash}
    \def\myboxnum{#1}
    \edef\mystructnum{\tag_get:n{struct_num}}
    \edef\statebeforebox{\inteval{\tag_get:n{struct_counter}+\tag_get:n{mc_counter}}}
  }
  {
    \tag_if_active:TF
      { \bool_set_true:N \l_tmpa_bool }
      { \bool_set_false:N \l_tmpa_bool }
    \SuspendTagging{luamplib.textext}
  }
}
\cs_set_nopar:Npn \luamplibtagtextend
{
  \bool_if:NTF \l__luamplib_tag_usetext_bool
  {
    \edef\stateafterbox{\inteval{\tag_get:n{struct_counter}+\tag_get:n{mc_counter}}}
    \tag_if_active:T {
      \int_compare:nNnTF
      {\stateafterbox}
      =
      {\statebeforebox}
      { \cs_gset_nopar:cpe {luamplib.notagbox.\myboxnum} {\mystructnum} }
      { \cs_gset_nopar:cpe {luamplib.tagbox.\myboxnum} {\mystructnum} }
    }
    \tag_struct_end:
    \tag_mc_end:
    \tag_mc_begin_pop:n{}
  }
  {
    \bool_if:NT \l_tmpa_bool
      { \ResumeTagging{luamplib.textext} }
  }
}
\msg_new:nnn {luamplib}{figure-text-reuse}
{
  textext~box~#1~probably~is~incorrectly~tagged.\\
  Reusing~a~box~in~text-keyed~figures~is~strongly~discouraged.
}
\cs_set_nopar:Npn \mplibputtextbox #1
{
  \vbox to 0pt{\vss\hbox to 0pt{%
    \bool_if:NTF \l__luamplib_tag_usetext_bool
    {
      \ResumeTagging{luamplib.puttextbox}
      \tag_mc_end:
      \cs_if_exist:cTF {luamplib.tagbox.#1}
      {
        \tag_struct_use_num:n {\csname luamplib.tagbox.#1\endcsname}
        \raise\dp#1\copy#1
      }
      {
        \cs_if_exist:cF {luamplib.notagbox.#1}
        {
          \msg_warning:nnn{luamplib}{figure-text-reuse}{#1}
        }
        \tag_mc_begin:n{}
        \int_set:Nn \l_tmpa_int {#1}
        \tag_mc_reset_box:N \l_tmpa_int
        \raise\dp#1\copy#1
        \tag_mc_end:
      }
      \tag_mc_begin:n{artifact}
    }
    {
      \int_set:Nn \l_tmpa_int {#1}
      \tag_mc_reset_box:N \l_tmpa_int
      \raise\dp#1\copy#1
    }
  \hss}}
}
\cs_new_nopar:Npn \__luamplib_tagging_begin_figure:
{
  \tag_if_active:T
  {
    \tag_mc_end_push:
    \tl_if_empty:NT\l__luamplib_tag_alt_tl
    {
      \msg_warning:nne{luamplib}{alt-text-missing}{\l__luamplib_tag_alt_dflt_tl}
      \tl_set:Ne\l__luamplib_tag_alt_tl {\l__luamplib_tag_alt_dflt_tl}
    }
    \tag_struct_begin:n
    {
      tag=\l__luamplib_tag_struct_tl,
      alt=\l__luamplib_tag_alt_tl,
    }
    \tag_mc_begin:n{}
  }
}
\cs_new_nopar:Npn \__luamplib_tagging_end_figure:
{
  \tag_if_active:T
  {
    \tag_mc_end:
    \tag_struct_end:
    \tag_mc_begin_pop:n{}
  }
}
\cs_new_nopar:Npn \__luamplib_tagging_begin_actualtext:
{
  \tag_if_active:T
  {
    \tag_mc_end_push:
    \tag_struct_begin:n
    {
      tag=Span,
      actualtext=\l__luamplib_tag_actual_tl,
    }
    \tag_mc_begin:n{}
  }
}
\cs_set_eq:NN \__luamplib_tagging_end_actualtext: \__luamplib_tagging_end_figure:
\cs_new_nopar:Npn \__luamplib_tagging_begin_artifact:
{
  \tag_if_active:T
  {
    \tag_mc_end_push:
    \tag_mc_begin:n{artifact}
  }
}
\cs_new_nopar:Npn \__luamplib_tagging_end_artifact:
{
  \tag_if_active:T
  {
    \tag_mc_end:
    \tag_mc_begin_pop:n{}
  }
}
\cs_set_eq:NN \luamplibtaggingbegin \__luamplib_tagging_begin_figure:
\cs_set_eq:NN \luamplibtaggingend \__luamplib_tagging_end_figure:
\keys_define:nn{luamplib/tag}
  {
    ,alt .code:n =
      {
        \tl_set:Ne\l__luamplib_tag_alt_tl{\text_purify:n{#1}}
      }
    ,actualtext .code:n =
      {
        \bool_set_false:N \l__luamplib_tag_BBox_bool
        \tl_set:Ne\l__luamplib_tag_actual_tl{\text_purify:n{#1}}
        \cs_set_eq:NN \luamplibtaggingbegin \__luamplib_tagging_begin_actualtext:
        \cs_set_eq:NN \luamplibtaggingend \__luamplib_tagging_end_actualtext:
        \tag_if_active:T {\noindent}
      }
    ,artifact .code:n =
      {
        \bool_set_false:N \l__luamplib_tag_BBox_bool
        \cs_set_eq:NN \luamplibtaggingbegin \__luamplib_tagging_begin_artifact:
        \cs_set_eq:NN \luamplibtaggingend \__luamplib_tagging_end_artifact:
      }
    ,text .code:n =
      {
        \bool_set_false:N \l__luamplib_tag_BBox_bool
        \bool_set_true:N  \l__luamplib_tag_usetext_bool
        \cs_set_eq:NN \luamplibtaggingbegin \__luamplib_tagging_begin_artifact:
        \cs_set_eq:NN \luamplibtaggingend \__luamplib_tagging_end_artifact:
        \tag_if_active:T {\noindent}
      }
    ,tag .code:n =
      {
        \str_case:nnF {#1}
          {
            {text}
            {
              \bool_set_false:N \l__luamplib_tag_BBox_bool
              \bool_set_true:N  \l__luamplib_tag_usetext_bool
              \cs_set_eq:NN \luamplibtaggingbegin \__luamplib_tagging_begin_artifact:
              \cs_set_eq:NN \luamplibtaggingend \__luamplib_tagging_end_artifact:
              \tag_if_active:T {\noindent}
            }
            {false}
            {
              \SuspendTagging{luamplib.tagfalse}
            }
          }
          {
            \tl_set:Nn\l__luamplib_tag_struct_tl{#1}
          }
      }
    ,correct-BBox .code:n =
      {
        \bool_set_true:N \l__luamplib_tag_bboxcorr_bool
        \seq_set_split:Nnn \l__luamplib_tag_bboxcorr_seq{~}{#1~0pt~0pt~0pt~0pt}
      }
    ,debug .code:n =
      { \bool_set_true:N \l__luamplib_tag_debug_bool }
    ,instance .code:n =
      { \tl_gset:Nn \currentmpinstancename {#1} }
    ,instancename .meta:n = { instance = {#1} }
    ,unknown .code:n =
      { \tl_gset:Ne \currentmpinstancename {\l_keys_key_str} }
  }
\cs_new_nopar:Npn \luamplibtaggingBBox
{
  \bool_lazy_and:nnT
  {\tag_if_active_p:}
  {\l__luamplib_tag_BBox_bool}
  {
    \tl_set:Ne \l__luamplib_BBox_label_tl {luamplib.BBox.\tag_get:n{struct_num}}
    \tex_savepos:D
    \property_record:ee{\l__luamplib_BBox_label_tl}{xpos,ypos,abspage}
    \tl_set:Ne \l__luamplib_BBox_llx_tl
      {
        \dim_to_decimal_in_bp:n
        { \property_ref:een {\l__luamplib_BBox_label_tl}{xpos}{0}sp }
      }
    \tl_set:Ne \l__luamplib_BBox_lly_tl
      {
        \dim_to_decimal_in_bp:n
        { \property_ref:een {\l__luamplib_BBox_label_tl}{ypos}{0}sp - \dp\mplibscratchbox }
      }
    \tl_set:Ne \l__luamplib_BBox_urx_tl
      {
        \dim_to_decimal_in_bp:n
        { \l__luamplib_BBox_llx_tl bp + \wd\mplibscratchbox }
      }
    \tl_set:Ne \l__luamplib_BBox_ury_tl
      {
        \dim_to_decimal_in_bp:n
        { \l__luamplib_BBox_lly_tl bp + \ht\mplibscratchbox + \dp\mplibscratchbox }
      }
    \bool_if:NT \l__luamplib_tag_bboxcorr_bool
      {
        \tl_set:Ne \l__luamplib_BBox_llx_tl
          {
            \fp_eval:n
            {
              \l__luamplib_BBox_llx_tl
              +
              \dim_to_decimal_in_bp:n {\seq_item:Nn \l__luamplib_tag_bboxcorr_seq {1} }
            }
          }
        \tl_set:Ne \l__luamplib_BBox_lly_tl
          {
            \fp_eval:n
            {
              \l__luamplib_BBox_lly_tl
              +
              \dim_to_decimal_in_bp:n {\seq_item:Nn \l__luamplib_tag_bboxcorr_seq {2} }
            }
          }
        \tl_set:Ne \l__luamplib_BBox_urx_tl
          {
            \fp_eval:n
            {
              \l__luamplib_BBox_urx_tl
              +
              \dim_to_decimal_in_bp:n {\seq_item:Nn \l__luamplib_tag_bboxcorr_seq {3} }
            }
          }
        \tl_set:Ne \l__luamplib_BBox_ury_tl
          {
            \fp_eval:n
            {
              \l__luamplib_BBox_ury_tl
              +
              \dim_to_decimal_in_bp:n {\seq_item:Nn \l__luamplib_tag_bboxcorr_seq {4} }
            }
          }
      }
    \prop_gput:cne
      { g__tag_struct_\tag_get:n{struct_num}_prop }
      {A}
      {
        << /O /Layout /BBox [
          \l__luamplib_BBox_llx_tl\c_space_tl
          \l__luamplib_BBox_lly_tl\c_space_tl
          \l__luamplib_BBox_urx_tl\c_space_tl
          \l__luamplib_BBox_ury_tl
        ] >>
      }
    \bool_if:NT \l__luamplib_tag_debug_bool
      {
        \iow_log:e
          {
            luamplib/tag/debug:~BBox~of~structure~\tag_get:n{struct_num}~is~
            \l__luamplib_BBox_llx_tl\c_space_tl
            \l__luamplib_BBox_lly_tl\c_space_tl
            \l__luamplib_BBox_urx_tl\c_space_tl
            \l__luamplib_BBox_ury_tl
          }
        \use:e
        {
          \exp_not:N\AddToHookNext{shipout/foreground}
          {
            \exp_not:N\int_compare:nNnT
            {\exp_not:N\g_shipout_readonly_int}
            =
            {\property_ref:een{\l__luamplib_BBox_label_tl}{abspage}{0}}
            {
              \exp_not:N\put
              (\l__luamplib_BBox_llx_tl bp, \dim_eval:n{\l__luamplib_BBox_lly_tl bp -\paperheight})
              {
                \exp_not:N\opacity_select:n{0.5} \exp_not:N\color_select:n{red}
                \exp_not:N\rule
                  {\dim_eval:n {\l__luamplib_BBox_urx_tl bp - \l__luamplib_BBox_llx_tl bp}}
                  {\dim_eval:n {\l__luamplib_BBox_ury_tl bp - \l__luamplib_BBox_lly_tl bp}}
              }
            }
          }
        }
      }
  }
}
\cs_set_nopar:Npn \luamplibtagasgroupbegin
{
  \bool_if:NT \l__luamplib_tag_usetext_bool
  {
    \ResumeTagging{luamplib.asgroup}
    \tag_mc_begin:n{}
  }
}
\cs_set_nopar:Npn \luamplibtagasgroupend
{
  \bool_if:NT \l__luamplib_tag_usetext_bool
  {
    \tag_mc_end:
    \SuspendTagging{luamplib.asgroup}
  }
}
\cs_set_nopar:Npn \mplibstarttousemplibgroup
{
  \prependtomplibbox\hbox dir TLT\bgroup
  \luamplibtaggingbegin
  \setbox\mplibscratchbox\hbox\bgroup
  \bool_if:NT \l__luamplib_tag_usetext_bool
  {
    \tag_mc_end:
    \tag_mc_begin:n{}
  }
}
\cs_set_nopar:Npn \mplibstoptousemplibgroup
{
  \bool_if:NT \l__luamplib_tag_usetext_bool
  {
    \tag_mc_end:
    \tag_mc_begin:n{artifact}
  }
  \egroup
  \luamplibtaggingBBox
  \unhbox\mplibscratchbox
  \luamplibtaggingend
  \egroup
}
\cs_set_nopar:Npn \mplibstarttoPDF #1 #2 #3 #4
  {
    \prependtomplibbox
    \hbox dir TLT\bgroup
    \luamplibtaggingbegin % begin tagging
    \xdef\MPllx{#1}\xdef\MPlly{#2}%
    \xdef\MPurx{#3}\xdef\MPury{#4}%
    \xdef\MPwidth{\the\dimexpr#3bp-#1bp\relax}%
    \xdef\MPheight{\the\dimexpr#4bp-#2bp\relax}%
    \parskip0pt
    \leftskip0pt
    \parindent0pt
    \everypar{}%
    \setbox\mplibscratchbox\vbox\bgroup
    \SuspendTagging{luamplib.mplibtopdf}% stop tag inside figure
    \noindent
  }
\cs_set_nopar:Npn \mplibstoptoPDF
  {
    \par
    \egroup
    \setbox\mplibscratchbox\hbox
      {\hskip-\MPllx bp
       \raise-\MPlly bp
       \box\mplibscratchbox}%
    \setbox\mplibscratchbox\vbox to \MPheight
      {\vfill
       \hsize\MPwidth
       \wd\mplibscratchbox0pt
       \ht\mplibscratchbox0pt
       \dp\mplibscratchbox0pt
       \box\mplibscratchbox}%
    \wd\mplibscratchbox\MPwidth
    \ht\mplibscratchbox\MPheight
    \luamplibtaggingBBox % BBox
    \box\mplibscratchbox
    \luamplibtaggingend % end tagging
    \egroup
  }
\RenewDocumentCommand\mplibcode{O{}}
  {
    \msg_set:nnn {luamplib}{alt-text-missing}
    {
      Alternative~text~for~mplibcode~is~missing.\\
      Using~the~default~value~'##1'~instead.
    }
    \tl_gset_eq:NN \currentmpinstancename \c_empty_tl
    \keys_set:ne{luamplib/tag}{#1}
    \tl_if_empty:NF \currentmpinstancename
      { \tl_set:Nn\l__luamplib_tag_alt_dflt_tl {metapost~figure~\currentmpinstancename} }
    \mplibtmptoks{}\ltxdomplibcode
  }
\RenewDocumentCommand\mpfig{s O{}}
  {
    \begingroup
    \IfBooleanTF{#1}
    {\mplibprempfig *}
    {
      \msg_set:nnn {luamplib}{alt-text-missing}
      {
        Alternative~text~for~mpfig~is~missing.\\
        Using~the~default~value~'##1'~instead.
      }
      \keys_set:ne{luamplib/tag}{#2}
      \tl_if_empty:NF \mpfiginstancename
        { \tl_set:Nn\l__luamplib_tag_alt_dflt_tl {metapost~figure~\mpfiginstancename} }
      \mplibmainmpfig
    }
  }
\RenewDocumentCommand\usemplibgroup{O{} m}
  {
    \begingroup
    \msg_set:nnn {luamplib}{alt-text-missing}
    {
      Alternative~text~for~usemplibgroup~is~missing.\\
      Using~the~default~value~'##1'~instead.
    }
    \keys_set:ne{luamplib/tag}{#1}
    \tl_set:Nn\l__luamplib_tag_alt_dflt_tl {metapost~figure~#2}
    \mplibstarttousemplibgroup
    \csname luamplib.group.#2\endcsname
    \mplibstoptousemplibgroup
    \endgroup
  }
\cs_new_nopar:Npn \mplibalttext #1
{
  \tl_set:Ne \l__luamplib_tag_alt_tl {\text_purify:n{#1}}
}
\cs_new_nopar:Npn \mplibactualtext #1
{
  \tl_set:Ne \l__luamplib_tag_actual_tl {\text_purify:n{#1}}
}
\ExplSyntaxOff
\endinput
%%
%% End of file `luamplib.sty'.
