\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{ebgaramond}
    [2024/04/23 (Bob Tennent and autoinst)  Style file for EB Garamond fonts.]

\RequirePackage{ifxetex,ifluatex,xkeyval,textcomp}

\newif\ifebgaramond@otf 
\ifxetex
  \ebgaramond@otftrue
\else\ifluatex
  \ebgaramond@otftrue
\else  % [pdf]LaTeX
  \ebgaramond@otffalse
\fi\fi

\newif\ifebgaramond@lining \ebgaramond@liningfalse
\newif\ifebgaramond@tabular \ebgaramond@tabularfalse
\newif\ifebgaramond@medium \ebgaramond@mediumfalse
\newif\ifebgaramond@semibold \ebgaramond@semiboldfalse
\newif\ifebgaramond@extrabold \ebgaramond@extraboldfalse

\newcommand*{\EBGaramond@scale}{1}
\newcommand*{\EBGaramondInitials@scale}{1}
\RequirePackage{xkeyval}
\DeclareOptionX{scaled}{\renewcommand*{\EBGaramond@scale}{#1}\renewcommand*{\EBGaramondInitials@scale}{#1}}
\DeclareOptionX{scale}{\renewcommand*{\EBGaramond@scale}{#1}\renewcommand*{\EBGaramondInitials@scale}{#1}}
\DeclareOptionX{type1}{\ebgaramond@otffalse}
\DeclareOptionX{lining}{\ebgaramond@liningtrue}
\DeclareOptionX{nf}{\ebgaramond@liningtrue}
\DeclareOptionX{lf}{\ebgaramond@liningtrue}
\DeclareOptionX{oldstyle}{\ebgaramond@liningfalse}
\DeclareOptionX{osf}{\ebgaramond@liningfalse}
\DeclareOptionX{tabular}{\ebgaramond@tabulartrue}
\DeclareOptionX{t}{\ebgaramond@tabulartrue}
\DeclareOptionX{proportional}{\ebgaramond@tabularfalse}
\DeclareOptionX{p}{\ebgaramond@tabularfalse}
\DeclareOptionX{medium}{\ebgaramond@mediumtrue}
\DeclareOptionX{m}{\ebgaramond@mediumtrue}
\DeclareOptionX{semibold}{\ebgaramond@semiboldtrue}
\DeclareOptionX{sb}{\ebgaramond@semiboldtrue}
\DeclareOptionX{extrabold}{\ebgaramond@extraboldtrue}
\DeclareOptionX{eb}{\ebgaramond@extraboldtrue}


\ExecuteOptionsX{oldstyle,proportional}
\ProcessOptionsX\relax

\ifebgaramond@otf
  \ifebgaramond@lining
    \def\ebgaramond@figurestyle{Lining}
  \else
    \def\ebgaramond@figurestyle{OldStyle} 
  \fi
  \ifebgaramond@tabular
    \def\ebgaramond@figurealign{Monospaced} 
  \else
    \def\ebgaramond@figurealign{Proportional} 
  \fi
  \ifebgaramond@medium
    \def\ebgaramond@regularstyle{Medium}
  \else
    \def\ebgaramond@regularstyle{Regular}
  \fi
  \ifebgaramond@semibold
    \def\ebgaramond@boldstyle{SemiBold}
  \else\ifebgaramond@extrabold
    \def\ebgaramond@boldstyle{ExtraBold}
  \else
    \def\ebgaramond@boldstyle{Bold}
  \fi\fi
    
\else % type1
  \ifebgaramond@lining
    \def\ebgaramond@figurestyle{LF}
  \else
    \def\ebgaramond@figurestyle{OsF}
  \fi
  \ifebgaramond@tabular
    \def\ebgaramond@figurealign{T}
  \else
    \def\ebgaramond@figurealign{}
  \fi
  \ifebgaramond@medium
    \def\mdseries@rm{medium}
  \else
    \def\mdseries@rm{m}
  \fi
  \def\seriesdefault{\mdseries@rm}
  \ifebgaramond@semibold
    \def\bfseries@rm{sb}
  \else\ifebgaramond@extrabold
    \def\bfseries@rm{eb}
  \else
    \def\bfseries@rm{b}
  \fi\fi

\fi

\ifebgaramond@otf
  \RequirePackage{fontspec}
\else
  \RequirePackage{fontenc,fontaxes,mweights}
\fi

\ifebgaramond@otf
  \def\ebgaramond@regular{Regular}

  \defaultfontfeatures{
        Ligatures = TeX ,
        Scale     = \EBGaramond@scale ,
        SmallCapsFeatures={\ifluatex Renderer=Basic\fi},
        Extension = .otf }
  \setmainfont
      [ Numbers = {\ebgaramond@figurealign,\ebgaramond@figurestyle},
        UprightFont    = *-\ebgaramond@regularstyle ,
        ItalicFont     = *-\ifx\ebgaramond@regularstyle\ebgaramond@regular Italic\else\ebgaramond@regularstyle Italic\fi,
        BoldFont    = *-\ebgaramond@boldstyle ,
        BoldItalicFont     = *-\ebgaramond@boldstyle Italic ,
        FontFace={m}{sw}{Font=*-\ifx\ebgaramond@regularstyle\ebgaramond@regular Italic\else\ebgaramond@regularstyle Italic\fi,Style=Swash},
        FontFace={b}{sw}{Font=*-\ebgaramond@boldstyle Italic,Style=Swash},
      ]
      {EBGaramond}
  \newfontfamily\ebgaramond
      [ Numbers = {\ebgaramond@figurealign,\ebgaramond@figurestyle},
        UprightFont    = *-\ebgaramond@regularstyle ,
        ItalicFont     = *-\ifx\ebgaramond@regularstyle\ebgaramond@regular Italic\else\ebgaramond@regularstyle Italic\fi ,
        BoldFont    = *-\ebgaramond@boldstyle ,
        BoldItalicFont     = *-\ebgaramond@boldstyle Italic ,
      ]
      {EBGaramond}
  \providecommand\sufigures{\addfontfeatures{VerticalPosition=Superior}}
  \providecommand\infigures{\addfontfeatures{VerticalPosition=ScientificInferior}}
  \newfontface\initials{EBGaramond-Initials}
  \DeclareTextFontCommand{\oldstylenums}{\addfontfeatures{Numbers=OldStyle}}
  \DeclareTextFontCommand{\liningnums}{\addfontfeatures{Numbers=Lining}}
  \DeclareTextFontCommand{\tabularnums}{\addfontfeatures{Numbers=Tabular}}
  \DeclareTextFontCommand{\proportionalnums}{\addfontfeatures{Numbers=Proportional}}
  \DeclareTextFontCommand{\textin}{\initials}

\else % type1

  \def\ebgaramond@family{EBGaramond-\ebgaramond@figurealign\ebgaramond@figurestyle}
  \renewcommand*\rmdefault{\ebgaramond@family}
  \newcommand*\ebgaramond{\fontfamily{\ebgaramond@family}\selectfont}

  \fa@naming@exception{figures}{{superior}{proportional}}{Sup}
  \fa@naming@exception{figures}{{superior}{tabular}}{Sup}
  \def\sufigures{\@nomath\sufigures\fontfigurestyle{superior}\selectfont}

  \fa@naming@exception{figures}{{inferior}{proportional}}{Inf}
  \fa@naming@exception{figures}{{inferior}{tabular}}{Inf}
  \def\infigures{\@nomath\infigures\fontfigurestyle{inferior}\selectfont}

  \ifx\swshape\@undefined
    \DeclareRobustCommand\swshape{\not@math@alphabet\swshape\relax
        \fontprimaryshape\itdefault\fontsecondaryshape\swdefault\selectfont}
     \fa@naming@exception{shape}{{n}{sw}}{nw}
    \fa@naming@exception{shape}{{it}{sw}}{sw}
  \fi


  \def\initials{\fontfamily{EBGaramondInitials-TLF}\fontseries{m}\selectfont}
  \DeclareTextFontCommand{\oldstylenums}{\fontfamily{EBGaramond-\ebgaramond@figurealign OsF}\selectfont}
  \DeclareTextFontCommand{\liningnums}{\fontfamily{EBGaramond-\ebgaramond@figurealign LF}\selectfont}
  \DeclareTextFontCommand{\tabularnums}{\fontfamily{EBGaramond-T\ebgaramond@figurestyle}\selectfont}
  \DeclareTextFontCommand{\proportionalnums}{\fontfamily{EBGaramond-\ebgaramond@figurestyle}\selectfont}
  \DeclareTextFontCommand{\textin}{\initials}

\fi

\DeclareTextFontCommand{\textsu}{\sufigures}
\DeclareTextFontCommand{\textinf}{\infigures}
\DeclareTextFontCommand{\textsw}{\swshape}

\ifebgaramond@lining
  \def\ebgaramondlgr@figurestyle{LF}
\else
  \def\ebgaramondlgr@figurestyle{OsF}
\fi
\ifebgaramond@tabular
  \def\ebgaramondlgr@figurealign{T}
\else
  \def\ebgaramondlgr@figurealign{}
\fi
\def\ebgaramondlgr{\fontencoding{LGR}\fontfamily{EBGaramond-\ebgaramondlgr@figurealign\ebgaramondlgr@figurestyle}\selectfont}

\ifebgaramond@otf
  \defaultfontfeatures{}
\fi

\endinput
