\NeedsTeXFormat{LaTeX2e}

\def\fileversion{1.0}
\def\filedate{2024/06/19}
\ProvidesPackage{gelasiomath}[%
\filedate\space v\fileversion (Michael Sharpe) Use xch small caps in gelasio text and supply matching math package]

\message{`gelasiomath'v\fileversion,\space\filedate\space Load\space XCharter\space  small\space  caps\space  as\space  target\space  for\space  scshape\space and\space load\space newtx\space with\space appropriate\space options.\space  (msharpe)}

\RequirePackage{iftex,etoolbox,xkeyval,ifthen,xparse}
\def\gelm@scale{1} % default value for scale option

\DeclareOptionX{scale}{\def\gelm@scale{#1}}
\newif\ifgelm@scosf
\DeclareOptionX{scosf}{\gelm@scosftrue}
%\newif\ifgelm@gelscfigs\gelm@gelscfigstrue
%\DeclareOptionX{nogelfigs}{\gelm@gelscfigsfalse}

\DeclareOptionX*{\PassOptionsToPackage{\CurrentOption}{newtx}}

\ProcessOptionsX \relax
\@tempdima=\gelm@scale\p@
\@tempdimb=.92\@tempdima % for gelasio text scale
\@tempdimc=.938\@tempdima % for xcharter text
\edef\gel@scale{\strip@pt\@tempdimb}
\edef\XCharter@scale{\strip@pt\@tempdimc}
\usepackage[gelasio,textscale=\gel@scale,mathscale=\gelm@scale]{newtx}
%\AtBeginDocument{%
%  \expandafter\ifx\csname Gelasio@scale\endcsname\relax
%    \edef\Gelasio@scale{1}
%    \edef\XCharter@scale{1.02}
%  \else
%    \@tempdima=\Gelasio@scale\p@
%    \@tempdima=1.02\@tempdima%
%    \edef\XCharter@scale{\strip@pt\@tempdima}
%  \fi
%} % AtBeginDocument
\def\gelm@scit{scit}% for testing value of \f@shape
\def\gelm@sc{sc}% for testing value of \f@shape

\iftutex
\AtBeginDocument{%
\def\gelm@tufig{\addfontfeatures{RawFeature = -smcp , \ifgelm@scosf Numbers =  OldStyle, \fi}} 
\addfontfeatures{UprightFeatures={SmallCapsFont={XCharter-Roman}, SmallCapsFeatures={%
\ifgelm@scosf Numbers = OldStyle ,\fi
 Letters=SmallCaps, 
Scale=\XCharter@scale}},%
ItalicFeatures={SmallCapsFont={XCharter-Italic}, SmallCapsFeatures={%
\ifgelm@scosf Numbers = OldStyle ,\fi
Letters=SmallCaps, Scale=\XCharter@scale}},%
BoldFeatures={SmallCapsFont={XCharter-Bold}, SmallCapsFeatures={%
\ifgelm@scosf Numbers = OldStyle ,\fi
Letters=SmallCaps, Scale=\XCharter@scale}},%
BoldItalicFeatures={SmallCapsFont={XCharter-BoldItalic}, SmallCapsFeatures={%
\ifgelm@scosf Numbers = OldStyle ,\fi
Letters=SmallCaps, Scale=\XCharter@scale}}%
}} 
\ifgelm@scosf
  \AtBeginDocument{%
 	\xpretocmd{\scshape}
  	{\addfontfeatures{Numbers=OldStyle}}
  	{}{}%
  }%
\fi

\else %not tutex
 \AtBeginDocument{%
  \ifgelm@scosf%
    \def\gelm@fig{OsF}
  \else % use default settings from gelasio.sty
    \def\gelm@fig{\gelasio@figurealign\gelasio@figurestyle}
  \fi
  \edef\XCharter@@scale{s*[\csname XCharter@scale\endcsname]}%
  \let\scshape\relax
  \DeclareRobustCommand\scshape{%
    \not@math@alphabet\scshape\relax
    \fontfamily{XCharter-\gelm@fig}\fontshape\scdefault\selectfont}
  %\DeclareTextCommand{\textsc}{\scshape}
 }
\fi

%\newtoks\gel@SC % store for processed string
%\newtoks\gel@src % store for source string
%\def\gel@Append#1{% append #1 to token register \gel@SC
%\toks0={#1}%
%\edef\gel@tmpa{\noexpand\gel@SC={\the\gel@SC \the\toks0}}%
%\gel@tmpa}
%
%\def\gel@Pop#1(into:)#2{%
%    \edef\gel@act{\noexpand\gel@SplitOff\the#1%
%    (gel@head:)\noexpand#2(gel@tail:)\noexpand#1}%
%    \gel@act}
%\def\gel@SplitOff#1#2(gel@head:)#3(gel@tail:)#4{\def#3{\noexpand #1}#4={#2}}
%
%\def\nxt|#1#2\@nil{%
%\toks0={#2}\edef\param{\the\toks0}%
%\ifx\param\@empty%
%  \textsc{\the\gel@SC} % done
%\else% continue parsing for capital letters
%  \ifthenelse{\isblank
%  \ifcat A#1 % #1 is letter
%    \if#1\uppercase{#1}%
%      \gel@Append{\textulc{#1}}%
%    \else
%      \gel@Append{#1}%
%    \fi
%  \else % not a letter
%    \gel@Append{#1}%
%  \fi
%  \nxt|#2\@nil%
%\fi}
%
%\def\textSC#1{\gel@SC={}\nxt|#1\@nil}
\def\textSC#1{\textsc{\capsescape{#1}}}
\iftutex%
  \def\textULC#1{{\gelasio\gelm@tufig%
  \ifthenelse{\equal{\f@shape}{\gelm@scit}}{%shape is scit
  \itshape}{% not so
  \ifthenelse{\equal{\f@shape}{\gelm@sc}}{% shape is sc 
  \upshape}}%
  \textulc{#1}}}

\else
  \def\textULC#1{{\fontfamily{Gelasio-\gelm@fig}%
  \ifthenelse{\equal{\f@shape}{\gelm@scit}}{%shape is scit
  \fontshape{it}}{% not so
  \ifthenelse{\equal{\f@shape}{\gelm@sc}}{% shape is sc 
  \fontshape{n}}{}}%
  \selectfont #1}}
\fi
\ExplSyntaxOn
\NewDocumentCommand{\capsescape}{ m }
 {
  \gel_capsescape:n { #1 }
 }

\tl_new:N \l__gel_capsescape_input_tl
\cs_new_protected:Npn \gel_capsescape:n #1
 {
  % store the string in a variable
  \tl_set:Nn \l__gel_capsescape_input_tl {#1}
  \regex_replace_all:nnN
    % search for capital letters, figures and more
    { ([A-Z\d]+|\cC.\{?[A-Z]+\}?) }
    % replace the match with \textULC{<match>}
    { \c{textULC}\cB\{\1\cE\} }
    \l__gel_capsescape_input_tl
  \tl_use:N \l__gel_capsescape_input_tl
 }
\ExplSyntaxOff


% galasio.sty does not offer macros for numerators, denominators or inferiors
\AtBeginDocument{%
\ifgelasio@otf
    \DeclareRobustCommand{\lfstyle}{%
      \not@math@alphabet\lfstyle\relax
      \addfontfeatures{Numbers={Proportional,Lining}}%
    }
    
   \DeclareRobustCommand{\tlfstyle}{%
      \not@math@alphabet\tlfstyle\relax
      \addfontfeatures{Numbers={Monospaced,Lining}}%
    }
   \DeclareRobustCommand{\osfstyle}{%
      \not@math@alphabet\osfstyle\relax
      \addfontfeatures{Numbers={Proportional,OldStyle}}%
    }
   \DeclareRobustCommand{\tosfstyle}{%
      \not@math@alphabet\tosfstyle\relax
      \addfontfeatures{Numbers={Monospaced,OldStyle}}%
    }
  \providecommand\nufigures{%
  \addfontfeatures{VerticalPosition=Numerator}}
  \providecommand\defigures{%
  \addfontfeatures{VerticalPosition=Denominator}}
  \providecommand\infigures{%
  \addfontfeatures{VerticalPosition=Inferior}}
  
  \DeclareTextFontCommand{\textnum}{\nufigures}
  \DeclareTextFontCommand{\textde}{\defigures}
  \DeclareTextFontCommand{\textinf}{\infigures}
  \NewCommandCopy\textnu\textnum
  \@ifundefined{tx@sfrSC}{}{%
  \newcommand*{\textsmde}[1]{%
  \setbox0=\hbox{\scalebox\tx@sfrSC{\textde{#1}}}\box0}}

\else % type1  --we define \text.. by lowering \textsu
    % missing font switches for type1
    \DeclareRobustCommand{\lfstyle}{%
      \not@math@alphabet\lfstyle\relax
      \fontfamily{Gelasio-LF}\selectfont %
    }
    \DeclareRobustCommand{\tlfstyle}{%
      \not@math@alphabet\tlfstyle\relax
      \fontfamily{Gelasio-TLF}\selectfont %
    }
    \DeclareRobustCommand{\osfstyle}{%
      \not@math@alphabet\osfstyle\relax
      \fontfamily{Gelasio-OsF}\selectfont %
    }
    \DeclareRobustCommand{\tosfstyle}{%
      \not@math@alphabet\tosfstyle\relax
      \fontfamily{Gelasio-TOsF}\selectfont %
    }    
  \newbox\tx@box
  \newcommand*{\textde}[1]{{\setbox0=\hbox{\textsu{#1}}\setbox1=\hbox{\raise -.763ex \box0}\dp1=0pt\ht1=.977ex\box1\relax}}
 
  \providecommand*\textnum[1]{\raisebox{.507ex}{\textde{#1}}}
  \NewCommandCopy\textnu\textnum
  \providecommand*\textinf[1]{\raisebox{-.237ex}{\textde{#1}}}
  \let\textnu\textnum
%  \providecommand*\textde[1]{{\setbox0=\hbox{\textsu{#1}}%
%  \setbox1=\hbox{\lower.763ex\box0}\dp0=0pt\relax\box1\relax}}
  %\raisebox{-.763ex}{\textsu{#1}}}
   % the following will be used only by \textsfrac
  \@ifundefined{tx@sfrSC}{}{%
  \newcommand*{\textsmde}[1]{%
  \setbox0=\hbox{\scalebox\tx@sfrSC{\textde{#1}}}\box0}%
    }
\fi
\DeclareTextFontCommand{\textlf}{\lfstyle}
\DeclareTextFontCommand{\texttlf}{\tlfstyle}
\DeclareTextFontCommand{\textosf}{\osfstyle}
\DeclareTextFontCommand{\texttosf}{\tosfstyle}
} %\AtBeginDocument
%\ifnum\ntx@id=16\relax
%\else % create \textsmsmde for other than minion and gelasio
%% We move the small font to the text baseline and scale it by \tx@sfrSC, for use only in the \textsfrac macro.
%\ifnum\ntx@id=4\relax
%\else
%  \ifnum\tx@debase=0
%    \newcommand*{\textsmde}[1]{%
%    \setbox0=\hbox{\scalebox\tx@sfrSC{\textde{#1}}}\box0}
%  \else
%    \newdimen\tx@u \newdimen\tx@v
%    \tx@u=-\tx@debase pt
%    \tx@toex{\strip@pt\tx@u}\tx@y=\tx@x
%    %\showthe\tx@y
%    \tx@v=\strip@pt\tx@y ex
%    \newcommand*{\textsmde}[1]{%
%    \setbox2=\hbox{\textde{#1}}%
%    \setbox1=\hbox{\raise \tx@v \box2}%
%    \setbox0=\hbox{\scalebox{\tx@sfrSC}{\box1}}\box0}
%  \fi %\ifnum\tx@debase=0
%\fi %\ifnum\ntx@id=4
%\fi %\ifnum\ntx@id=16
\iftutex%
  \def\gel@frslash{\symbol{"2044}}
\else%
  \def\gel@frslash{%
   {\fontencoding{TS1}\selectfont\char"2F}}
\fi

\newcommand*{\geltextfrac}[3][]{% like \textfrac[1]{7}{8}
  \mbox{%
    \ifthenelse{\not\equal{#1}{}}%Test for integer portion [optional #1]
      {\textlf{#1}\kern.02em}{}%         
    \textnum{#2}% numerator
    \StrRight{#2}{1}[\@digit@]%
    % look for  1, 2, 3, 7, 8
    \ifcase\@digit@% 0
    \or%\kern.03em% 1
    \or\kern.04em% 2
    \or%\kern-.01em% 3
    \or\kern.02em% 4
    \or\kern-.01em% 5
    \or%\kern.01em% 6
    \or\kern-.08em% 7
     \or\kern.01em% 8
    \or\kern-.02em% 9
   \fi%
%    \kern\ntx@foresolidus{\fontencoding{TS1}\selectfont\char"2F}%
%    \kern\ntx@aftsolidus%
   \gel@frslash%
	\StrLeft{#3}{1}[\@digit@]%
    % look for 1, 4, 7
    \ifcase\@digit@% 0
    \or\kern .01em% 1
    \or\kern .01em% 2
    \or\kern .01em% 3
    \or\kern -.01em% 4
    \or%\kern -.02em% 5
    \or%\kern-.03em% 6
    \or\kern .06em% 7
   \or%\kern .01em% 8
   \or\kern .01em% 9
    \fi%
    \textde{#3}%       denominator [#3]
  }%mbox
} % \geltextfrac
\@ifundefined{textfrac}{\NewCommandCopy\textfrac\geltextfrac}%
{\RenewCommandCopy\textfrac\geltextfrac}

\endinput