\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{CascadiaMono}
    [2023/09/26 (autoinst)  Style file for CascadiaMono fonts.]

\RequirePackage{xkeyval}
\newcommand*{\CascadiaMono@scale}{1}
\DeclareOptionX{scale}{\renewcommand*{\CascadiaMono@scale}{#1}}
\DeclareOptionX{scaled}{\renewcommand*{\CascadiaMono@scale}{#1}}

\DeclareOptionX{lining}{\edef\CascadiaMono@figurestyle{LF}}
\DeclareOptionX{tabular}{\edef\CascadiaMono@figurealign{T}}
\DeclareOptionX{mainfont}{
    \renewcommand{\familydefault}{\ttdefault}
}
\DeclareOptionX{semibold}{\edef\bfseries@tt{semibold}}
\DeclareOptionX{bold}{\edef\bfseries@tt{bold}}
\DeclareOptionX{regular}{\edef\mdseries@tt{regular}}
\ExecuteOptionsX{lining,tabular}
\ProcessOptionsX\relax

\RequirePackage{fontenc,textcomp}
\RequirePackage{ifthen}
\RequirePackage{mweights}

\IfFileExists{fontaxes.sty}{
    \RequirePackage{fontaxes}
    \fa@naming@exception{figures}{{superior}{proportional}}{Sup}
    \fa@naming@exception{figures}{{superior}{tabular}}{Sup}
    \def\supfigures{\@nomath\supfigures
        \fontfigurestyle{superior}\selectfont}
    \let\sufigures\supfigures
    \DeclareTextFontCommand{\textsup}{\supfigures}
    \let\textsu\textsup
    \let\textsuperior\textsup

}{}

%   Here we implement the scale=MatchLowercase option.
%   If this is given, we must compute the correct value of
%   the "\CascadiaMono@scale" parameter before loading the .fd files;
%   but to determine that value we of course need the font's x-height.
%   To avoid triggering the loading of .fd files,
%   we use plain TeX's \font primitive to load the testfont.
%   We then compute the ratio of the current x-height to our font's x-height;
%   this is the "\CascadiaMono@scale" we will pass to the .fd files.
\ifthenelse{\equal{\CascadiaMono@scale}{MatchLowercase}}
    {   \newlength{\CascadiaMono@currentx}
        \settoheight{\CascadiaMono@currentx}{x}
        \newlength{\CascadiaMono@xheight}
        \settoheight{\CascadiaMono@xheight}
            {{\font\testfont=CascadiaMono-Regular-tlf-ot1 at \f@size pt\testfont x}}
        \renewcommand*{\CascadiaMono@scale}
            {\strip@pt\dimexpr\number\numexpr\number\dimexpr\CascadiaMono@currentx\relax*65536/\number\dimexpr\CascadiaMono@xheight\relax\relax sp\relax}}
    {}

\renewcommand*
    {\ttdefault}
    {CascadiaMono-\CascadiaMono@figurealign\CascadiaMono@figurestyle}

\endinput
