\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{LibreBskvl}
    [2020/07/27 (autoinst)  Style file for LibreBskvl fonts.]

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

\DeclareOptionX{lining}{\edef\LibreBskvl@figurestyle{LF}}
\DeclareOptionX{proportional}{\edef\LibreBskvl@figurealign{}}
\DeclareOptionX{mainfont}{
    \renewcommand{\familydefault}{\rmdefault}
}
\DeclareOptionX{bold}{\edef\bfseries@rm{bold}}
\DeclareOptionX{regular}{\edef\mdseries@rm{regular}}
\ExecuteOptionsX{lining,proportional,bold,regular}
\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 "\LibreBskvl@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 "\LibreBskvl@scale" we will pass to the .fd files.
\ifthenelse{\equal{\LibreBskvl@scale}{MatchLowercase}}
    {   \newlength{\LibreBskvl@currentx}
        \settoheight{\LibreBskvl@currentx}{x}
        \newlength{\LibreBskvl@xheight}
        \settoheight{\LibreBskvl@xheight}
            {{\font\testfont=LibreBskvl-Regular-lf-ot1 at \f@size pt\testfont x}}
        \renewcommand*{\LibreBskvl@scale}
            {\strip@pt\dimexpr\number\numexpr\number\dimexpr\LibreBskvl@currentx\relax*65536/\number\dimexpr\LibreBskvl@xheight\relax\relax sp\relax}}
    {}

\renewcommand*
    {\rmdefault}
    {LibreBskvl-\LibreBskvl@figurealign\LibreBskvl@figurestyle}

\endinput
