\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{ebgaramond}
    [2018/04/07 (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{regular}
  \fi
  \def\seriesdefault{\mdseries@rm}
  \ifebgaramond@semibold
    \def\bfseries@rm{semibold}
  \else\ifebgaramond@extrabold
    \def\bfseries@rm{extrabold}
  \else
    \def\bfseries@rm{bold}
  \fi\fi

\fi

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

\ifebgaramond@otf
  \ifxetex\XeTeXtracingfonts=1\fi
  \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 ,
      ]
      {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}}
  \newfontfamily\swshape
      [ Style={Swash},
        Numbers = {\ebgaramond@figurealign,\ebgaramond@figurestyle},
        UprightFont     = *-\ifx\ebgaramond@regularstyle\ebgaramond@regular Italic\else\ebgaramond@regularstyle Italic\fi ,
        ItalicFont     = *-\ifx\ebgaramond@regularstyle\ebgaramond@regular Italic\else\ebgaramond@regularstyle Italic\fi ,
        BoldFont    = *-\ebgaramond@boldstyle Italic,
        BoldItalicFont     = *-\ebgaramond@boldstyle Italic ,
      ]
      {EBGaramond}
  \newfontfamily\ebgaramondOsF
      [ Numbers = {\ebgaramond@figurealign,OldStyle},
        UprightFont    = *-\ebgaramond@regularstyle ,
        ItalicFont     = *-\ifx\ebgaramond@regularstyle\ebgaramond@regular Italic\else\ebgaramond@regularstyle Italic\fi ,
        BoldFont    = *-\ebgaramond@boldstyle ,
        BoldItalicFont     = *-\ebgaramond@boldstyle Italic ,
      ]
      {EBGaramond}
  \newfontfamily\ebgaramondLF
      [ Numbers = {\ebgaramond@figurealign,Lining},
        UprightFont    = *-\ebgaramond@regularstyle ,
        ItalicFont     = *-\ifx\ebgaramond@regularstyle\ebgaramond@regular Italic\else\ebgaramond@regularstyle Italic\fi ,
        BoldFont    = *-\ebgaramond@boldstyle ,
        BoldItalicFont     = *-\ebgaramond@boldstyle Italic ,
      ]
      {EBGaramond}

  \newfontface\initials{EBGaramond-Initials}
  \DeclareTextFontCommand{\oldstylenums}{\ebgaramondOsF}
  \DeclareTextFontCommand{\liningnums}{\ebgaramondLF}
  \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}

  \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}


  \def\initials{\fontfamily{EBGaramondInitials-TLF}\fontseries{regular}\selectfont}
  \DeclareTextFontCommand{\oldstylenums}{\fontfamily{EBGaramond-\ebgaramond@figurealign OsF}\selectfont}
  \DeclareTextFontCommand{\liningnums}{\fontfamily{EBGaramond-\ebgaramond@figurealign LF}\selectfont}
  \DeclareTextFontCommand{\textin}{\initials}

\fi

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

\ifebgaramond@otf
  \newfontfamily\ebgaramondtabular
    [ Numbers = {Monospaced,\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}
  \newfontfamily\ebgaramondproportional
    [ Numbers = {Proportional,\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}
  \DeclareTextFontCommand{\tabularnums}{\ebgaramondtabular}
  \DeclareTextFontCommand{\proportionalnums}{\ebgaramondproportional}
\else % type1
  \DeclareTextFontCommand{\tabularnums}{\fontfamily{EBGaramond-T\ebgaramond@figurestyle}\selectfont}
  \DeclareTextFontCommand{\proportionalnums}{\fontfamily{EBGaramond-\ebgaramond@figurestyle}\selectfont}
\fi

\ifebgaramond@otf
  \defaultfontfeatures{}
\fi

\endinput
