% \iffalse meta-comment
%
%% File: latex-lab-text.dtx (C) Copyright 2023-2025 LaTeX Project
%
% 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
%
%
% The development version of the bundle can be found below
%
%    https://github.com/latex3/latex2e/required/latex-lab
%
% for those people who are interested or want to report an issue.
%
\def\ltlabtextdate{2025-05-10}
\def\ltlabtextversion{0.85f}

%<*driver>
\DocumentMetadata{tagging=on,pdfstandard=ua-2}
\documentclass[kernel]{l3doc}
\usepackage{latex-lab-testphase-l3doc}
% for the phoneme argument
\usepackage{fontspec}
\setmonofont{NewCMMono10-Book.otf}

%\DeclareUnicodeCharacter{02C8}{\texttt{\textquotesingle}}
%\DeclareUnicodeCharacter{02D0}{\texttt{:}}
%\DeclareUnicodeCharacter{025B}{$\epsilon$}

\EnableCrossrefs
\CodelineIndex
\begin{document}
  \DocInput{latex-lab-text.dtx}
\end{document}
%</driver>
%
% \fi
%
% \title{The \textsf{latex-lab-text} package\\
% Changes and additions to the kernel related to the tagging of various small text commands}
% \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
% \date{v\ltlabtextversion\ \ltlabtextdate}
%
% \maketitle
%
% \newcommand{\xt}[1]{\textsl{\textsf{#1}}}
% \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}}
% \newcommand{\docclass}{document class \marginpar{\raggedright document class
% customizations}}
%
% \providecommand\hook[1]{\texttt{#1}}
%
% \begin{abstract}
% \end{abstract}
%
% \section{Introduction}
%
% The followings contains small changes to improve the tagging of
% (inline) text commands.
%
% While the tagging of the \LaTeX{} logo is quite straightforward, tagging of
% \cs{emph} as \texttt{EM} and \cs{textbf} as \texttt{Strong}
% can have unwanted side-effect as both tags are inline tags and so not every inner
% structure is allowed. Probably both commands will need an optional argument or a
% starred variant which suppress the tagging.
%
% \section{Implementation}
%    \begin{macrocode}
%<*package>
%<@@=tag>
%    \end{macrocode}
%    \begin{macrocode}
\ProvidesExplPackage {latex-lab-testphase-text} {\ltlabtextdate} {\ltlabtextversion}
  {Code related to the tagging of various small text commands}
%    \end{macrocode}

% \subsection{The \LaTeX\ and \TeX\ logo}
% This uses the generic hooks, so that it catches also other definitions.
% For \cs{LaTeX} we stop tagging to avoid that the inner \cs{TeX} leads to a double tagging.
%    \begin{macrocode}
\AddToHook{cmd/LaTeX/before}
  {
    \mode_leave_vertical:
    \tag_mc_end_push:
    \tag_struct_begin:n{tag=Span,actualtext=LaTeX,phoneme=[ˈlaːtɛx]}
    \tag_mc_begin:n{}
    \tag_suspend:n{LaTeX}
  }
\AddToHook{cmd/LaTeX/after}
 {
   \tag_resume:n{LaTeX}
   \tag_mc_end:
   \tag_struct_end:
   \tag_mc_begin_pop:n{}
 }
\AddToHook{cmd/TeX/before}
 {
   \mode_leave_vertical:
   \tag_mc_end_push:
   \tag_struct_begin:n{tag=Span,actualtext=TeX}
   \tag_mc_begin:n{}
 }
\AddToHook{cmd/TeX/after}
 {
   \tag_mc_end:
   \tag_struct_end:
   \tag_mc_begin_pop:n{}
 }
%    \end{macrocode}
%
% \subsection{Emphasizing}
% We tag \cs{emph} but leave commands like \cs{textbf} alone, as it is not
% clear if they always have a semantic meaning.
%    \begin{macrocode}
\AddToHook{cmd/emph/before}
  {
    \mode_leave_vertical:
    \tag_mc_end_push:
    \tag_struct_begin:n{tag=Em}
    \tag_mc_begin:n{}
  }

\AddToHook{cmd/emph/after}
  {
    \tag_mc_end:
    \tag_struct_end:
    \tag_mc_begin_pop:n{}
  }
%    \end{macrocode}
%
% \subsection{cs{mbox}}
% When used in math and when luamml is active \cs{mbox} has to be correctly annoted.
% For this we add a socket that luamml can activate.
% We test for the socket until 2025-06-01.
% \changes{v0.85c}{2024-12-03}{Add luamml socket to \cs{mbox}}
%    \begin{macrocode}
\DeclareRobustCommand\mbox[1]
  {
   \leavevmode
   \tag_socket_use:nnn {math/luamml/hbox}
    {}{ \hbox{#1} }
  }
%    \end{macrocode}
% And the same for \cs{makebox}
%    \begin{macrocode}
\long\def\@imakebox[#1][#2]#3{%
  \tag_socket_use:nnn {math/luamml/hbox}{}
   {
     \@begin@tempboxa\hbox{#3}%
       \setlength\@tempdima{#1}%       support calc
       \hb@xt@\@tempdima{%
        \expandafter\ifx\csname bm@#2\endcsname\relax
          \bm@c
          \@latex@warning{Unexpected~alignment~#2}%
        \else
          \csname bm@#2\endcsname
        \fi}%
     \@end@tempboxa
   }}
%    \end{macrocode}
%
% \subsection{phantom commands}
%
% In text mode we do not want the content of a phantom command to create a structure,
% so we suspend tagging
% \changes{0.85d}{2025-01-27}{Suspend tagging in \cs{makeph@nt}}
% TODO: consider if it makes sense to set measuring to true instead.
% \cs{mathsm@sh} is redefined in latex-lab-math.
% \begin{macro}[no-user-doc]{\makeph@nt}
%    \begin{macrocode}
\def\makeph@nt#1{%
  \setbox\z@\hbox
    {\SuspendTagging{\makeph@nt}%
     \color@begingroup#1\color@endgroup\ResumeTagging{\makeph@nt}}\finph@nt}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}[no-user-doc]{\@kernel@mathph@nt,\mathph@nt}
%    \begin{macrocode}
\def\@kernel@mathph@nt#1#2{%
  \setbox\z@\hbox{\SuspendTagging{\mathph@nt}$\m@th#1{#2}$\ResumeTagging{\mathph@nt}}\finph@nt}
\def\mathph@nt#1#2
 {
  \setbox \z@ = \hbox {
    $
    \m@th
    #1
    {
% nested phantoms should not be processed by luamml
      \let\mathph@nt\@kernel@mathph@nt
      #2
    }
    \UseTaggingSocket{math/luamml/save/nNn}{{mathphant} #1 {mphantom}}
    $
  }
   \UseTaggingSocket{math/luamml/finph@nt}{}{\finph@nt}
 }
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%    \begin{macrocode}
%<*latex-lab>
\ProvidesFile{text-latex-lab-testphase.ltx}
        [\ltlabtextdate\space v\ltlabtextversion\space
         code related to the tagging of various small text commands]

\RequirePackage{latex-lab-testphase-text}

%</latex-lab>
%    \end{macrocode}
