%
% Copyright (C) 2012-2021 Javier Bezos http://www.texnia.com
%
% This file may be distributed and/or modified under the conditions of
% the MIT License. A version can be found at the end of this file.
%
% Repository: https://github.com/jbezos/esindex
%

\ProvidesPackage{soulpos}[2021/10/15 v1.2 Fancy underlining]

\newcount\ulp@subcnt
\newcount\ulp@cnt

\newwrite\ulp@out
\newread\ulp@in

\ifx\savepos\@undefined
  \let\ulp@savepos\pdfsavepos
  \let\ulp@lastxpos\pdflastxpos
  \let\ulp@lastypos\pdflastypos
\else
  \let\ulp@savepos\savepos
  \let\ulp@lastxpos\lastxpos
  \let\ulp@lastypos\lastypos
\fi

\AtBeginDocument{\ulp@begindoc}

\def\ulp@begindoc{%
  \@ifundefined{SOUL@}%
    {\PackageInfo{soulpos}{Loading soulutf8}%
     \RequirePackage{soulutf8}}{}%
  \openout\ulp@out=\jobname.upa\relax
  \openin\ulp@in=\jobname.upb\relax}

\let\ulp@afterend\relax

\AtEndDocument{%
  \closeout\ulp@out
  \closein\ulp@in
  \write\@auxout{\string\ulp@afterend}%
  \def\ulp@afterend{%
    \ulp@cnt\z@
    \message{* soulpos - computing points - it may take a few seconds *}%
    \immediate\openout\ulp@out=\jobname.upb\relax
    \begingroup
      \makeatletter
      \InputIfFileExists{\jobname.upa}{}{}%
    \endgroup
    \immediate\closeout\ulp@out}}

% Underlining options
% --------------------

\RequirePackage{keyval}

\define@key{soulpos}{overdraw}[true]{\@nameuse{ulp@pos@#1}}
\def\ulp@pos@false{\def\ulp@pos{\z@}}
\def\ulp@pos@true{\def\ulp@pos{\@ne}}

\newif\ifulp@includehyphens
\ulp@includehyphenstrue

\define@key{soulpos}{hyphens}[include]{\@nameuse{ulp@hyp@#1}}
\def\ulp@hyp@include{\ulp@includehyphenstrue}
\def\ulp@hyp@exclude{\ulp@includehyphensfalse}

\define@key{soulpos}{xoffset}[0pt]{\def\ulp@skern{#1}\def\ulp@ekern{#1}}
\define@key{soulpos}{xoffset-start}[0pt]{\def\ulp@skern{#1}}
\define@key{soulpos}{xoffset-end}[0pt]{\def\ulp@ekern{#1}}

\define@key{soulpos}{gap}[0pt]{\def\ulp@sgap{#1}\def\ulp@egap{#1}}
\define@key{soulpos}{gap-start}[0pt]{\def\ulp@sgap{#1}}
\define@key{soulpos}{gap-end}[0pt]{\def\ulp@egap{#1}}

% Computing start/end points
% ---------------------------

\newcommand\ulpostolerance{0}%

\let\ulp@Gb\relax
\def\ulp@Pb#1{\expandafter\gdef\csname ulp@@#1\endcsname}

\def\ulp@writeb{%
  \count@\ulp@xlast
  \advance\count@-\ulp@xfirst
  \immediate\write\ulp@out{%
    \string\ulp@Pb{\ulp@ulsub}%
    {{\the\count@}\ulp@startprev\ulp@endprev}\@percentchar}}

\def\ulp@Ga#1#2{%
  \advance\ulp@cnt\@ne
  \def\ulp@pos{#1}%
  \immediate\write\ulp@out{\string\ulp@Gb\string{\@percentchar\iffalse}\fi}%
  #2%
  \ulp@writeb
  \immediate\write\ulp@out{\iffalse{\fi\string}\@percentchar}}

\def\ulp@Pa#1#2#3#4{%
  \@tempswafalse
  \ifnum#2=\@ne
    \@tempswatrue
  \else
    \count@\ulp@ylast\relax
    \advance\count@-#4\relax
    \ifnum\ulpostolerance<\count@
      \ulp@writeb
      \@tempswatrue
    \else\ifnum-\ulpostolerance>\count@
      \ulp@writeb
      \@tempswatrue
    \fi\fi
  \fi
  \if@tempswa
    \def\ulp@xfirst{#3}%
    \def\ulp@startprev{#1}%
    \ifnum\ulp@pos=\z@
      \edef\ulp@ulsub{#2}%
    \fi
  \fi
  \def\ulp@endprev{#1}%
  \def\ulp@xlast{#3}%
  \def\ulp@ylast{#4}%
  \ifnum\ulp@pos=\@ne
    \edef\ulp@ulsub{#2}%
  \fi}

% Writing points to .upa
% ----------------------

\def\ulp@stepput{\ulp@step\ulp@put}
\def\ulp@step{\global\advance\ulp@subcnt\@ne}

% 0: text, 1: space, 2: discretionary, 3 exhyph

\newdimen\ulwidth

\def\ulp@fetch#1#2#3{%
   \ulwidth#1sp\relax
   \def\ulstarttype{#2}%
   \def\ulendtype{#3}}

\def\ulp@put#1{%
  \hb@xt@\z@\bgroup
    \ifcase\ulp@pos\else\hss\fi % over
    \@ifundefined{ulp@@\the\ulp@subcnt}{}%
      {\expandafter\expandafter\expandafter
         \ulp@fetch\csname ulp@@\the\ulp@subcnt\endcsname
       \ulp@draw
       \global\expandafter\let\csname ulp@@\the\ulp@subcnt\endcsname\@undefined}%
    \ifcase\ulp@pos\hss\fi % over
    \ulp@savepos
    \protected@write\ulp@out{}%
      {\string\ulp@Pa\number#1{\the\ulp@subcnt}%
      {\noexpand\the\ulp@lastxpos}{\noexpand\the\ulp@lastypos}%
       \@percentchar}%
  \egroup}

% The command for defining underlines
% ------------------------------------

\def\ulp@preamble{%
      \ifeof\ulp@in
      \else
        \begingroup
          \makeatletter
          \catcode`\^^M=9  % avoids extra \par's
          \read\ulp@in to\@tempa
          \@tempa
        \endgroup
      \fi
      \global\advance\ulp@cnt\@ne
      \ulp@subcnt\z@
      \ifdim\ulp@sgap>0pt  % sacar también desde aquí
        \ifdim\lastskip>5sp
          \unskip
          \hskip\ulp@sgap
        \fi
      \fi
      \protected@write\ulp@out{}{%
         \string\ulp@Ga\ulp@pos
         \string{\@percentchar\iffalse}\fi}%
      \SOUL@setkern{-\ulp@skern}%
      \ulp@stepput\z@
      \SOUL@setkern\ulp@skern}

\def\ulp@postamble{%
     \SOUL@setkern\ulp@ekern
     \ulp@stepput\z@
     \SOUL@setkern{-\ulp@ekern}%
     \write\ulp@out{\iffalse{\fi\string}\@percentchar}%
     \ifdim\ulp@egap>0pt
       \global\skip@\ulp@egap
       \aftergroup\SOUL@socheck
     \fi}%

\def\ulp@everyspace#1{%
     \ulp@stepput\@ne
     #1\space
     \ulp@stepput\@ne}%

\def\ulp@everyhyphen{%
     \ifulp@includehyphens
       \ulp@step
     \else
       \ulp@stepput\tw@
     \fi
     \ulp@step
     \discretionary
       {\SOUL@setkern\SOUL@hyphkern
        \SOUL@sethyphenchar
        \global\advance\ulp@subcnt\m@ne
        \ifulp@includehyphens
          \ulp@put\tw@
        \fi
        \ulp@step}%
       {\ulp@put\tw@}%
       {}}%

\def\ulp@everyexhyphen#1{%
     \SOUL@setkern\SOUL@hyphkern
     \ifulp@includehyphens
       \hbox{#1}%
       \ulp@stepput\thr@@
     \else
       \ulp@stepput\thr@@
       \hbox{#1}%
     \fi
     \ulp@step
     \discretionary{}%
       {\ulp@put\thr@@}%
       {\SOUL@setkern\SOUL@charkern}}%

\newcommand\ulposdef[1]{%
  \@ifnextchar[{\ulp@def#1}{\ulp@def#1[]}}

\def\ulp@def#1[#2]#3{
  \DeclareRobustCommand*#1{%
    \def\SOUL@preamble{%
      \def\ulp@skern{0pt}%
      \def\ulp@ekern{0pt}%
      \def\ulp@sgap{0pt}%
      \def\ulp@egap{0pt}%
      \def\ulp@pos{\z@}%
      \setkeys{soulpos}{#2}%
      \def\ulp@draw{#3}%
      \ulp@preamble}%
    \let\SOUL@postamble\ulp@postamble
    \let\SOUL@everyspace\ulp@everyspace
    \let\SOUL@everyhyphen\ulp@everyhyphen
    \let\SOUL@everyexhyphen\ulp@everyexhyphen
    \let\SOUL@everysyllable\@empty
    \def\SOUL@everytoken{\the\SOUL@token}%
  \SOUL@}}

\newcommand\ifulstarttype[1]{%
  \ifnum\ulstarttype=#1\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\newcommand\ifulendtype[1]{%
  \ifnum\ulendtype=#1\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\endinput

MIT License
-----------

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


