% \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 latex-lab bundle is developed in the LaTeX2e GitHub.
% Issues may be reported at
%
%    https://github.com/latex3/latex2e/issues
%
\def\ltlabtextdate{2026-04-27}
\def\ltlabtextversion{0.85g}

%<*driver>
\DocumentMetadata{tagging=on,pdfstandard=ua-2}
\documentclass[kernel]{l3in2edoc}

% 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}
%
% This module 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} 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:
    \UseTaggingSocket{inline/begin}
      {tag=\UseStructureName{span},actualtext=LaTeX,phoneme=[ˈlaːtɛx]}
    \tag_suspend:n{\LaTeX}
  }
\AddToHook{cmd/LaTeX/after}
 {
   \tag_resume:n{\LaTeX}
   \UseTaggingSocket{inline/end}
 }
\AddToHook{cmd/TeX/before}
 {
   \mode_leave_vertical:
   \UseTaggingSocket{inline/begin}
     {tag=\UseStructureName{span},actualtext=TeX}
 }
\AddToHook{cmd/TeX/after}
 {
   \UseTaggingSocket{inline/end}
 }
%    \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:
    \UseTaggingSocket{inline/begin}
      {tag=\UseStructureName{text/emph}}
  }

\AddToHook{cmd/emph/after}
  {
    \UseTaggingSocket{inline/end}
  }
%    \end{macrocode}
%
%\subsection{\cs{textsuperscript} and \cs{textsubscript}}
%    \begin{macrocode}
\sys_if_engine_luatex:TF
 {
   \cs_new:Npn \@@_space_ignore:n #1 {\tag_spacechar_off:#1\tag_spacechar_on:}
 }
 {
   \cs_set_eq:NN \@@_space_ignore:n \use:n
 }  
\protected\def\@textsuperscript#1%
  {%
    \check@mathfonts
    \leavevmode
    \begingroup
    \UseTaggingSocket{inline/begin}{tag=\UseStructureName{span},attribute-class=sup}
    \sbox\z@{\fontsize\sf@size\sf@size#1}%
      \raise\dimexpr\textsuperscript@offset\relax\box\z@
    \UseTaggingSocket{inline/end}
     \@@_space_ignore:n{\textsuperscript@space}
    \endgroup
  }
\protected\def\@textsubscript#1%
  {%
    \check@mathfonts
    \leavevmode
    \begingroup
     \UseTaggingSocket{inline/begin}{tag=\UseStructureName{span},attribute-class=sub}
      \sbox\z@{\fontsize\sf@size\sf@size#1}%
      \lower\dimexpr\textsubscript@offset\relax\box\z@
     \UseTaggingSocket{inline/end}
     \@@_space_ignore:n{\textsuperscript@space}
    \endgroup
  }
%    \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}
% \subsection{The \cs{verb} command}
% 
%    \begin{macrocode}
\def\verb{\relax\ifmmode\hbox\else\leavevmode\null\fi
  \bgroup
    \UseTaggingSocket{inline/begin}{tag=\UseStructureName{text/verb}}%
    \verb@eol@error \let\do\@makeother \dospecials
    \verbatim@font\@noligs
    \language\l@nohyphenation
    \@ifstar\@sverb\@verb}
\def\verb@egroup{\global\let\verb@balance@group\@empty\UseTaggingSocket{inline/end}\egroup}
%    \end{macrocode}
%    \begin{macrocode}
%</package>
%    \end{macrocode}
