% \iffalse meta-comment
%
%% File: tagpdf-space.dtx
%
% Copyright (C) 2019-2023 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>
\DocumentMetadata{}
\documentclass{l3doc}
\usepackage{array,booktabs,caption}
\hypersetup{pdfauthor=Ulrike Fischer,
 pdftitle=tagpdf-space module (tagpdf)}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
% \title{^^A
%   The \pkg{tagpdf-space} module\\ Code related to real space chars   ^^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.98k, released 2023-08-04}
% \maketitle
% \begin{documentation}
% \begin{function}{interwordspace (setup-key)}
% This key allows to activate/deactivate the real space chars if the engine supports
% it. The allowed values are |true|, |on|, |false|, |off|.
% \end{function}
% \begin{function}{show-spaces (setup-key)}
% This key works only with luatex and shows with small red
% bars where spaces have been inserted. This is only for debugging and
% is not completly reliable (and change affect other literals and tagging), so it
% should be used with care.
% \end{function}
% \end{documentation}
% \begin{implementation}
%    \begin{macrocode}
%<@@=tag>
%<*header>
\ProvidesExplPackage {tagpdf-space-code} {2023-08-04} {0.98k}
 {part of tagpdf - code related to real space chars}
%</header>
%    \end{macrocode}
% \section{Code for interword spaces}
% The code is engine/backend dependant. Basically only pdftex and luatex
% support real space chars. Most of the code for luatex which uses attributes
% is in the lua code, here are only the keys.
%
% \begin{macro}{interwordspace (setup-key),show-spaces (setup-key)}
%    \begin{macrocode}
%<*package>
\keys_define:nn { @@ / setup }
  {
    interwordspace .choices:nn = { true, on }
      { \msg_warning:nnx {tag}{sys-no-interwordspace}{\c_sys_engine_str}  },
    interwordspace .choices:nn = { false, off }
      { \msg_warning:nnx {tag}{sys-no-interwordspace}{\c_sys_engine_str}  },
    interwordspace .default:n = true,
    show-spaces .bool_set:N = \l_@@_showspaces_bool
  }
\sys_if_engine_pdftex:T
  {
    \sys_if_output_pdf:TF
      {
        \pdfglyphtounicode{space}{0020}
        \keys_define:nn { @@ / setup }
          {
            interwordspace .choices:nn = { true, on }  { \pdfinterwordspaceon },
            interwordspace .choices:nn = { false, off }{ \pdfinterwordspaceon },
            interwordspace .default:n = true,
            show-spaces .bool_set:N = \l_@@_showspaces_bool
          }
      }
      {
        \keys_define:nn { @@ / setup }
          {
            interwordspace .choices:nn = { true, on, false, off }
              { \msg_warning:nnn {tag}{sys-no-interwordspace}{dvi}  },
            interwordspace .default:n = true,
            show-spaces .bool_set:N = \l_@@_showspaces_bool
          }
      }
  }


\sys_if_engine_luatex:T
  {
    \keys_define:nn { @@ / setup }
      {
        interwordspace .choices:nn =
                                 { true, on }
                                 {
                                   \bool_gset_true:N \g_@@_active_space_bool
                                   \lua_now:e{ltx.@@.func.markspaceon()}
                                 },
        interwordspace .choices:nn =
                                 { false, off }
                                 {
                                  \bool_gset_false:N \g_@@_active_space_bool
                                  \lua_now:e{ltx.@@.func.markspaceoff()}
                                 },
        interwordspace .default:n = true,
        show-spaces      .choice:,
        show-spaces  / true  .code:n =
                                 {\lua_now:e{ltx.@@.trace.showspaces=true}},
        show-spaces  / false .code:n =
                                 {\lua_now:e{ltx.@@.trace.showspaces=nil}},
        show-spaces .default:n = true
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_fakespace:}
% For luatex we need a command for the fake space as equivalent of the pdftex
% primitive.
%    \begin{macrocode}
\sys_if_engine_luatex:T
  {
    \cs_new_protected:Nn \@@_fakespace:
      {
        \group_begin:
        \lua_now:e{ltx.@@.func.fakespace()}
        \skip_horizontal:n{\c_zero_skip}
        \group_end:
      }
  }
%</package>
%    \end{macrocode}
% \end{macro}
% \end{implementation}
% \PrintIndex
