\NeedsTeXFormat{LaTeX2e}
\def\supsVersion{2.0}
\def\supsDate{2024/01/07}
\ProvidesPackage{superiors}[\supsDate\space-\space\supsVersion
 superiors (License LPPL) Michael Sharpe]
\newif\ifsups@KOMA
\@ifundefined{ftntm@font}{}{\sups@KOMAtrue}

\RequirePackage{xkeyval,xcolor,iftex,etoolbox,ifthen,trace}

\NewCommandCopy\orig@makefnmark\@makefnmark
\def\f@@tn@te{footnote}
\def\sups@id{-1}
\def\sups@scaledval{1}
\def\sups@raised{\z@}
\def\sups@Lspaced{.04em}
\def\sups@Rspaced{.04em}
\def\sups@supscale{1.0}
\def\sups@color{black}
\newif\ifsups@fam %true if a fam specced
\newif\ifsups@otf %true if an otf specced
\newif\ifsups@titlepatch % true if \maketitle should be patched
\sups@titlepatchtrue
%\traceon
\DeclareOptionX{notitlepatch}[true]{%
\@nameuse{if#1}\sups@titlepatchfalse\fi}
\DeclareOptionX{supscale}[1.0]{\def\sups@scaledval{#1}}
\DeclareOptionX{supscaled}[1.0]{\def\sups@scaledval{#1}}
\DeclareOptionX{supscolor}[\z@]{\def\sups@color{#1}}
\DeclareOptionX{raised}[\z@]{\def\sups@raised{#1}}
\DeclareOptionX{supsraised}[\z@]{\def\sups@raised{#1}}
\DeclareOptionX{supsfam}{\def\sups@fam{#1}\sups@famtrue}
\DeclareOptionX{supsotf}{\def\sups@otf{#1}\sups@otftrue}
\DeclareOptionX{supsLspaced}[.04em]{\def\sups@Lspaced{#1}}
\DeclareOptionX{supsRspaced}[.04em]{\def\sups@Rspaced{#1}}
\DeclareOptionX{supspaced}[.04em]{\def\sups@Lspaced{#1}\def\sups@Rspaced{#1}}
% The following options affect the choice of source for the superiors
\DeclareOptionX{newtx}{\def\sups@id{\z@}}
\DeclareOptionX{ntx}{\def\sups@id{\z@}}
\DeclareOptionX{ztm}{\def\sups@id{\z@}}
\DeclareOptionX{newtxtext}{\def\sups@id{\z@}}
\DeclareOptionX{newpx}{\def\sups@id{1}}
\DeclareOptionX{npx}{\def\sups@id{1}}
\DeclareOptionX{zpl}{\def\sups@id{1}}
\DeclareOptionX{newpxtext}{\def\sups@id{1}}
\DeclareOptionX{lbtn}{\def\sups@id{2}}
\DeclareOptionX{libertine}{\def\sups@id{2}}
\DeclareOptionX{libertinus}{\def\sups@id{2}}
\DeclareOptionX{cochineal}{\def\sups@id{3}}
\DeclareOptionX{Cochineal}{\def\sups@id{3}}
\DeclareOptionX{coch}{\def\sups@id{3}}
\DeclareOptionX{stickstoo}{\def\sups@id{4}}
\DeclareOptionX{Stickstoo}{\def\sups@id{4}}
\DeclareOptionX{SticksToo}{\def\sups@id{4}}
\DeclareOptionX{stix2}{\def\sups@id{4}}
\DeclareOptionX{stickstootext}{\def\sups@id{4}}
\DeclareOptionX{etbb}{\def\sups@id{5}}
\DeclareOptionX{ETbb}{\def\sups@id{5}}
\DeclareOptionX{fbb}{\def\sups@id{6}}
\DeclareOptionX{erewhon}{\def\sups@id{7}}
\DeclareOptionX{Erewhon}{\def\sups@id{7}}
\DeclareOptionX{xcharter}{\def\sups@id{8}}
\DeclareOptionX{xch}{\def\sups@id{8}}
\DeclareOptionX{XCharter}{\def\sups@id{8}}
\DeclareOptionX{baskervillef}{\def\sups@id{9}}
\DeclareOptionX{BaskervilleF}{\def\sups@id{9}}
\DeclareOptionX{baskervaldx}{\def\sups@id{10}}
\DeclareOptionX{Baskervaldx}{\def\sups@id{10}}
\DeclareOptionX{garamondx}{\def\sups@id{11}}
\DeclareOptionX{zgm}{\def\sups@id{11}}
\DeclareOptionX{scholax}{\def\sups@id{12}}
\DeclareOptionX{ebg}{\def\sups@id{13}}
\DeclareOptionX{ebgaramond}{\def\sups@id{13}}
\DeclareOptionX{EBGaramond}{\def\sups@id{13}}

\ProcessOptionsX\relax

\iftutex\else\sups@otffalse\fi

\def\sups@getbyndx#1#2{%
  \def\sups@tmpa{}\def\sups@tmpb{}
  \ifcase#1%\sups@id%0
    \def\sups@tmpb{ntxsups}
    \def\sups@tmpa{TeXGyreTermesX-Regular.otf}

    \or %1
    \def\sups@tmpb{zplsups}
    \def\sups@tmpa{TeXGyrePagellaX-Regular.otf}
    
    \or %2
    \def\sups@tmpb{LibertinusSerif-Sup}
    \def\sups@tmpa{LibertinusSerif-Regular.otf}
    
    \or %3
    \def\sups@tmpb{Cochineal-Sup}
    \def\sups@tmpa{Cochineal-Roman.otf}
    
    \or %4
    \def\sups@tmpb{SticksTooText-Sup}
    \def\sups@tmpa{STIXTwoText-Regular.otf}
    
    \or %5
    \def\sups@tmpb{ETbb-Sup}
    \def\sups@tmpa{ETbb-Regular.otf}
    
    \or %6
    \def\sups@tmpb{fbb-Sup}
    \def\sups@tmpa{fbb-Regular.otf}
    
    \or %7
    \def\sups@tmpb{Erewhon-Sup}
    \def\sups@tmpa{Erewhon-Regular.otf}
    
    \or %8
    \def\sups@tmpb{XCharter-Sup}
    \def\sups@tmpa{XCharter-Roman.otf}
     
    \or %9
    \def\sups@tmpb{BaskervilleF-Sup}
    \def\sups@tmpa{BaskervilleF-Regular.otf}
    
    \or %10
    \def\sups@tmpb{Baskervaldx-Sup}
    \def\sups@tmpa{Baskervaldx-Regular.otf}
    
    \or %11
    \def\sups@tmpb{zgm1}
    \def\sups@tmpa{zgm1}
    
    \or %12
    \def\sups@tmpb{TeXGyreScholaX-Sup}
    \def\sups@tmpa{TeXGyreScholaX-Regular.otf}
    
    \or %13
    \def\sups@tmpb{EBGaramond-Sup}
    \def\sups@tmpa{EBGaramond-Regular.otf}

  \fi % \ifcase
  \iftutex
    \NewCommandCopy{#2}{\sups@tmpa}
  \else
    \NewCommandCopy{#2}{\sups@tmpb}
  \fi
}

\ifsups@fam\else%
  \ifsups@otf\else%
    \ifnum\sups@id=11\relax
	  \def\sups@fam{zgm1}
	  \sups@famtrue
	\else
      \ifnum\sups@id=-1\relax
        \def\sups@id{0}
      \fi
      \iftutex
	    \sups@getbyndx{\sups@id}{\sups@otf}
	    \sups@otftrue
	  \else
	    \sups@getbyndx{\sups@id}{\sups@fam}
	    \sups@famtrue
      \fi	  
	\fi
  \fi
\fi

\DeclareRobustCommand{\supsStyle}{%
  \not@math@alphabet\supsStyle\relax
  \ifsups@fam%
    \fontencoding{T1}\fontfamily{\sups@fam}\selectfont 
  \else% must be otf
    \newfontface\supsStylef{\sups@otf}[RawFeature=+sups]%
\supsStylef %
\fi
}
\DeclareTextFontCommand{\textSuperscript}{\supsStyle}
%\iftutex
%  \RequirePackage{realscripts}
%  \@ifundefined{supsSuprstyle}{%
%  \newfontface\supsSuprstyle{\sups@otf}
%[RawFeature=+sups]}{}
%\else %type1
%%  \DeclareFontFamily{U}{zsups}{}%
%%  \DeclareFontShape{U}{zsups}{m}{n}{%
%%    <->  \sups@tfm
%%}{}
%  \DeclareRobustCommand{\supsSuprstyle}{%
%  \not@math@alphabet\supsSuprstyle\relax
%  \fontencoding{T1}\fontfamily{\sups@fam}\selectfont }
%\fi % type1

% The following apples to both tutex and type1

\ExplSyntaxOn
  \NewDocumentCommand \textfnscript {m} {
	 \hbox_set:Nn \l_tmpa_box {{\supsStyle #1}}
     \box_scale:Nnn \l_tmpa_box { \sups@scaledval } { \sups@scaledval } 
	 \hbox_set:Nn \l_tmpa_box
    { \box_move_up:nn \sups@raised {\box_use:N \l_tmpa_box} }
	 \hspace*{\sups@Lspaced}{\color{\sups@color}\box_use:N \l_tmpa_box} \hspace*{\sups@Rspaced}}	
	    
  \cs_set:Npn \@makefnmark {
  \ifx\@mpfn\f@@tn@te% we're not in a minipage  
  \mbox{\footnotemarkfont\textfnscript{\@thefnmark}}
  \else\orig@makefnmark\fi}
  
  \@ifundefined{ftntm@font}{
  \cs_new:Npn \footnotemarkfont {\normalfont}
  }
      {% Unable to run xpatchcmd on \deffootnote
  \cs_new:Npn \footnotemarkfont {\ftntm@font}
  \deffootnote[1em]{1.5em}{1em}{%
  \textfnscript{\thefootnotemark}}
    \deffootnotemark{\textfnscript{\thefootnotemark}}%
      }
  \ifsups@titlepatch    
    \patchcmd{\maketitle}{\@textsuperscript{\normalfont\@thefnmark}}{\footnotemarkfont\textfnscript{\@thefnmark}}{}{}
    \patchcmd{\maketitle}{\@textsuperscript{\normalfont\@thefnmark}}{\footnotemarkfont\textfnscript{\@thefnmark}}{}{}
  \fi
  \ExplSyntaxOff
  
  \iftutex\else
    \ifsups@KOMA
      \def\footnotemarkfont{\ftntm@font}
    \else%
      \def\footnotemarkfont{\normalfont}
    \fi
  \fi
%\def\f@@tn@te{footnote}
%\def\@makefnmark{%
%    \ifx\@mpfn\f@@tn@te% we're not in a minipage
%        \raisebox{\sups@raised}{\hbox%
%        {\sustyle\hspace*{\sups@spaced}\@thefnmark%
%        \hspace*{.03em}}}%
%    \else% minipage---use original def
%        \sups@makefnmark%
%    \fi%
%}

\endinput