%% mfb-oldstyle.sty
%% LaTeX support for MFB Oldstyle, a revival of Century Oldstyle
%% (Morris Fuller Benton, American Type Founders, 1909).
%% MFB Oldstyle is in the public domain (CC0 1.0), and so is this file.
%%
%% pdfLaTeX and LaTeX use the Type 1 fonts: the NFSS families MFBOldstyle-LF,
%% -TLF, -OsF and -TOsF (with small caps) and MFBOldstyle-Sup, -Inf, -Numr and
%% -Dnom, in the encodings OT1, T1, LY1, T5 and TS1. XeLaTeX and LuaLaTeX use
%% the OpenType fonts through fontspec, unless the option type1 is given.
\NeedsTeXFormat{LaTeX2e}[2020/10/01]
\ProvidesPackage{mfb-oldstyle}
    [2026/10/14 v2.001 MFB Oldstyle fonts]

\RequirePackage{xkeyval}
\RequirePackage{iftex}


%   ----  Options  ----

\newcommand*{\MFBOldstyle@scale}{1}
\DeclareOptionX{scale}{\renewcommand*{\MFBOldstyle@scale}{#1}}
\DeclareOptionX{scaled}{\renewcommand*{\MFBOldstyle@scale}{#1}}

\edef\MFBOldstyle@figurestyle{LF}
\edef\MFBOldstyle@figurealign{}
\DeclareOptionX{lining}{\edef\MFBOldstyle@figurestyle{LF}}
\DeclareOptionX{lf}{\edef\MFBOldstyle@figurestyle{LF}}
\DeclareOptionX{oldstyle}{\edef\MFBOldstyle@figurestyle{OsF}}
\DeclareOptionX{osf}{\edef\MFBOldstyle@figurestyle{OsF}}
\DeclareOptionX{tabular}{\edef\MFBOldstyle@figurealign{T}}
\DeclareOptionX{proportional}{\edef\MFBOldstyle@figurealign{}}
\DeclareOptionX{mainfont}{\renewcommand{\familydefault}{\rmdefault}}
\DeclareOptionX{bold}{\def\MFBOldstyle@bfseries{bold}}
\DeclareOptionX{regular}{\def\MFBOldstyle@mdseries{regular}}
\newif\ifMFBOldstyle@dlig
\DeclareOptionX{dlig}{\MFBOldstyle@dligtrue}
\def\MFBOldstyle@superiors{none}
\DeclareOptionX{superiors}[all]{\def\MFBOldstyle@superiors{#1}}
\DeclareOptionX{nosuperiors}{\def\MFBOldstyle@superiors{none}}
\newif\ifMFBOldstyle@typeone
\DeclareOptionX{type1}{\MFBOldstyle@typeonetrue}

\ProcessOptionsX\relax

\newif\ifMFBOldstyle@otf
\iftutex
    \ifMFBOldstyle@typeone\else\MFBOldstyle@otftrue\fi
\fi


%   ----  Helpers  ----

\ExplSyntaxOn
\cs_new_protected:Npn \MFBOldstyle@figureversions
  {
    \RequirePackage { figureversions }
    \figureversions_new_figurestyle:nnn { superior } { Sup } { Sup }
    \figureversions_new_figurestyle:nnn { inferior } { Inf } { Inf }
    \figureversions_new_figurestyle:nnn { numerators } { Numr } { Numr }
    \figureversions_new_figurestyle:nnn { numerator } { Numr } { Numr }
    \figureversions_new_figurestyle:nnn { denominators } { Dnom } { Dnom }
    \figureversions_new_figurestyle:nnn { denominator } { Dnom } { Dnom }
  }
%   \MFBOldstyle@ifdigits{<text>}{<true>}{<false>}: whether <text> is all digits
\regex_const:Nn \c__mfboldstyle_digits_regex { \A [0-9]+ \Z }
\cs_new_protected:Npn \MFBOldstyle@ifdigits #1
  { \regex_match:NnTF \c__mfboldstyle_digits_regex {#1} }
%   \MFBOldstyle@iffamily{<true>}{<false>}: whether the current family is ours
\cs_new_protected:Npn \MFBOldstyle@iffamily
  { \exp_args:NV \str_if_in:nnTF \f@family { MFBOldstyle- } }
\ExplSyntaxOff

\def\MFBOldstyle@fontaxes{%
    \RequirePackage{fontaxes}%
    \fa@naming@exception{figures}{{superior}{proportional}}{Sup}%
    \fa@naming@exception{figures}{{superior}{tabular}}{Sup}%
    \fa@naming@exception{figures}{{inferior}{proportional}}{Inf}%
    \fa@naming@exception{figures}{{inferior}{tabular}}{Inf}%
    \fa@naming@exception{figures}{{numerators}{proportional}}{Numr}%
    \fa@naming@exception{figures}{{numerator}{proportional}}{Numr}%
    \fa@naming@exception{figures}{{numerators}{tabular}}{Numr}%
    \fa@naming@exception{figures}{{numerator}{tabular}}{Numr}%
    \fa@naming@exception{figures}{{denominators}{proportional}}{Dnom}%
    \fa@naming@exception{figures}{{denominator}{proportional}}{Dnom}%
    \fa@naming@exception{figures}{{denominators}{tabular}}{Dnom}%
    \fa@naming@exception{figures}{{denominator}{tabular}}{Dnom}%
}


\ifMFBOldstyle@otf
%   ----  XeLaTeX and LuaLaTeX: the OpenType fonts  ----

\RequirePackage{fontspec}
\def\MFBOldstyle@OsF{OsF}
\edef\MFBOldstyle@numbers{%
    \ifx\MFBOldstyle@figurestyle\MFBOldstyle@OsF OldStyle\else Lining\fi,%
    \ifx\MFBOldstyle@figurealign\@empty Proportional\else Monospaced\fi}
\edef\MFBOldstyle@next{%
    \noexpand\setmainfont{MFBOldstyle}[
        Extension      = .otf,
        UprightFont    = *-Regular,
        ItalicFont     = *-Italic,
        BoldFont       = *-Bold,
        BoldItalicFont = *-BoldItalic,
        Scale          = \MFBOldstyle@scale,
        Numbers        = {\MFBOldstyle@numbers},
        \ifMFBOldstyle@dlig Ligatures = {TeX, Discretionary},\fi
    ]}
\MFBOldstyle@next

\IfFileExists{figureversions.sty}{\RequirePackage{figureversions}}{}
\def\MFBOldstyle@figures#1{%
    \addfontfeatures{VerticalPosition=\csname MFBOldstyle@position@#1\endcsname}}
\def\MFBOldstyle@position@superior{Superior}
\def\MFBOldstyle@position@inferior{ScientificInferior}
\def\MFBOldstyle@position@numerators{Numerator}
\def\MFBOldstyle@position@denominators{Denominator}

\else
%   ----  pdfLaTeX and LaTeX: the Type 1 fonts  ----

%   T1 unless the document has chosen an encoding already (LaTeX's defaults
%   are OT1, and TU with XeLaTeX and LuaLaTeX, which Type 1 fonts lack)
\edef\MFBOldstyle@encoding{\encodingdefault}
\def\MFBOldstyle@OTone{OT1}
\def\MFBOldstyle@TU{TU}
\ifx\MFBOldstyle@encoding\MFBOldstyle@OTone
    \RequirePackage[T1]{fontenc}
\else\ifx\MFBOldstyle@encoding\MFBOldstyle@TU
    \RequirePackage[T1]{fontenc}
\fi\fi

\ifMFBOldstyle@dlig
    \def\MFBOldstyle@dlig{-dlig}
\fi
\ifdefined\MFBOldstyle@bfseries
    \edef\bfseries@rm{\MFBOldstyle@bfseries}
\fi
\ifdefined\MFBOldstyle@mdseries
    \edef\mdseries@rm{\MFBOldstyle@mdseries}
\fi

%   \oldstylenums in our families: the old-style figures of their TS1 fonts
%   (LaTeX would take them from another family, as the fonts lack some of
%   TS1's symbols)
\NewCommandCopy\MFBOldstyle@oldstylenums\oldstylenums
\DeclareRobustCommand\oldstylenums[1]{%
    \ifmmode
        \MFBOldstyle@oldstylenums{#1}%
    \else
        \MFBOldstyle@iffamily
            {{\fontencoding{TS1}\selectfont#1}}
            {\MFBOldstyle@oldstylenums{#1}}%
    \fi}

\IfFileExists{figureversions.sty}
    {\MFBOldstyle@figureversions}
    {\IfFileExists{fontaxes.sty}{\MFBOldstyle@fontaxes}{}}
\ifdefined\fontfigurestyle
    \def\MFBOldstyle@figures#1{\fontfigurestyle{#1}\selectfont}
\else
    \def\MFBOldstyle@figures#1{%
        \fontfamily{MFBOldstyle-\csname MFBOldstyle@family@#1\endcsname}\selectfont}
    \def\MFBOldstyle@family@superior{Sup}
    \def\MFBOldstyle@family@inferior{Inf}
    \def\MFBOldstyle@family@numerators{Numr}
    \def\MFBOldstyle@family@denominators{Dnom}
\fi

%   scale=MatchLowercase: the ratio of the current x-height to ours, found
%   with plain TeX's \font so as not to load the .fd files before the scale
%   is known
\def\MFBOldstyle@match{MatchLowercase}
\ifx\MFBOldstyle@scale\MFBOldstyle@match
    \newlength{\MFBOldstyle@currentx}
    \settoheight{\MFBOldstyle@currentx}{x}
    \newlength{\MFBOldstyle@xheight}
    \settoheight{\MFBOldstyle@xheight}
        {{\font\testfont=MFBOldstyle-Regular-lf-ot1 at \f@size pt\testfont x}}
    \renewcommand*{\MFBOldstyle@scale}
        {\strip@pt\dimexpr\number\numexpr\number\dimexpr\MFBOldstyle@currentx\relax
            *65536/\number\dimexpr\MFBOldstyle@xheight\relax\relax sp\relax}
\fi

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

\fi


%   ----  Superior and inferior figures, numerators, denominators  ----

\DeclareRobustCommand\supfigures{\@nomath\supfigures\MFBOldstyle@figures{superior}}
\DeclareRobustCommand\inffigures{\@nomath\inffigures\MFBOldstyle@figures{inferior}}
\let\sufigures\supfigures
\let\infigures\inffigures
\DeclareTextFontCommand{\textsup}{\supfigures}
\DeclareTextFontCommand{\textinf}{\inffigures}
\let\textsu\textsup
\let\textsuperior\textsup
\let\textin\textinf
\let\textinferior\textinf

%   \textfrac{3}{4}: numerator figures, fraction slash, denominator figures
\ProvideDocumentCommand\textfrac{mm}{%
    \mbox{{\MFBOldstyle@figures{numerators}#1}\textfractionsolidus
        {\MFBOldstyle@figures{denominators}#2}}}

%   Footnote marks that are numbers (superiors=footnotes or all), and
%   \textsuperscript and \textsubscript of numbers (superiors=all), in the
%   superior and inferior figures. Anything else, and the starred forms, as
%   LaTeX sets it: the fonts have superior and inferior figures only, so
%   \textsuperscript{1,2} is LaTeX's superscript.
\def\MFBOldstyle@footnotes{footnotes}
\def\MFBOldstyle@all{all}
\def\MFBOldstyle@none{none}
\ifx\MFBOldstyle@superiors\MFBOldstyle@footnotes\else
\ifx\MFBOldstyle@superiors\MFBOldstyle@all\else
\ifx\MFBOldstyle@superiors\MFBOldstyle@none\else
    \PackageError{mfb-oldstyle}
        {Unknown value `\MFBOldstyle@superiors' of the option superiors}
        {Use superiors=footnotes, superiors=all or superiors=none.}
    \let\MFBOldstyle@superiors\MFBOldstyle@footnotes
\fi\fi\fi

\ifx\MFBOldstyle@superiors\MFBOldstyle@none\else
    \@ifundefined{ftntm@font}%
        {\let\footnotemarkfont\normalfont}%
        {\let\footnotemarkfont\ftntm@font}
    \def\MFBOldstyle@fnmark#1{%
        \MFBOldstyle@ifdigits{#1}
            {{\footnotemarkfont\supfigures#1}}
            {\@textsuperscript{\footnotemarkfont#1}}}
    \def\@makefnmark{%
        \hbox{\expandafter\MFBOldstyle@fnmark\expandafter{\@thefnmark}}}
\fi

\ifx\MFBOldstyle@superiors\MFBOldstyle@all
    \RenewDocumentCommand\textsuperscript{sm}{%
        \IfBooleanTF{#1}
            {\@textsuperscript{\selectfont#2}}
            {\MFBOldstyle@ifdigits{#2}{\textsup{#2}}{\@textsuperscript{\selectfont#2}}}}
    \RenewDocumentCommand\textsubscript{sm}{%
        \IfBooleanTF{#1}
            {\@textsubscript{\selectfont#2}}
            {\MFBOldstyle@ifdigits{#2}{\textinf{#2}}{\@textsubscript{\selectfont#2}}}}
\fi

\endinput
