\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{ebgaramond}
    [2014/07/02 (Bob Tennent)  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

\newcommand*{\EBGaramond@scale}{1}
\RequirePackage{xkeyval}
\DeclareOptionX{scaled}{\renewcommand*{\EBGaramond@scale}{#1}}
\DeclareOptionX{scale}{\renewcommand*{\EBGaramond@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}


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

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

\ifebgaramond@otf
  \ifxetex\XeTeXtracingfonts=1\fi
  \defaultfontfeatures{
        Ligatures = TeX ,
        Scale     = \EBGaramond@scale ,
        Extension = .otf }
  \setmainfont
      [ Numbers = {\ebgaramond@figurealign,\ebgaramond@figurestyle},
        UprightFont    = *-Regular ,
        ItalicFont     = *-Italic ,
        BoldFont    = *-Regular ,
        BoldItalicFont     = *-Italic ,
      ]
      {EBGaramond12}
  \newfontfamily\ebgaramond
      [ Numbers = {\ebgaramond@figurealign,\ebgaramond@figurestyle},
        UprightFont    = *-Regular ,
        ItalicFont     = *-Italic ,
        BoldFont    = *-Regular ,
        BoldItalicFont     = *-Italic ,
      ]
      {EBGaramond12}
  \providecommand\sufigures{\addfontfeatures{VerticalPosition=Superior}}
  \providecommand\infigures{\addfontfeatures{VerticalPosition=ScientificInferior}}
  \newfontfamily\swshape
      [ Style={Swash},
        Numbers = {\ebgaramond@figurealign,\ebgaramond@figurestyle},
        UprightFont    = *-Italic ,
        ItalicFont     = *-Italic ,
        BoldFont    = *-Italic ,
        BoldItalicFont     = *-Italic ,
      ]
      {EBGaramond12}
  \newfontfamily\ebgaramondOsF
      [ Numbers = {\ebgaramond@figurealign,OldStyle},
        UprightFont    = *-Regular ,
        ItalicFont     = *-Italic ,
        BoldFont    = *-Regular ,
        BoldItalicFont     = *-Italic ,
      ]
      {EBGaramond12}
  \newfontfamily\ebgaramondLF
      [ Numbers = {\ebgaramond@figurealign,Lining},
        UprightFont    = *-Regular ,
        ItalicFont     = *-Italic ,
        BoldFont    = *-Regular ,
        BoldItalicFont     = *-Italic ,
      ]
      {EBGaramond12}
  \newfontface\initials{EBGaramondInitials}
  \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}

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

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

  \def\initials{\fontfamily{EBGaramondInitials-TLF}\selectfont}

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

\fi

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

\ifebgaramond@otf
  \newfontfamily\ebgaramondtabular
    [ Numbers = {Monospaced,\ebgaramond@figurestyle},
        UprightFont    = *-Regular ,
        ItalicFont     = *-Italic ,
        BoldFont    = *-Regular ,
        BoldItalicFont     = *-Italic ,
    ]
    {EBGaramond12}
  \newfontfamily\ebgaramondproportional
    [ Numbers = {Proportional,\ebgaramond@figurestyle},
        UprightFont    = *-Regular ,
        ItalicFont     = *-Italic ,
        BoldFont    = *-Regular ,
        BoldItalicFont     = *-Italic ,
    ]
    {EBGaramond12}
  \DeclareTextFontCommand{\tabularnums}{\ebgaramondtabular}
  \DeclareTextFontCommand{\proportionalnums}{\ebgaramondproportional}
\else % type1
  \DeclareTextFontCommand{\tabularnums}{\fontfamily{EBGaramond-T\ebgaramond@figurestyle}\selectfont}
  \DeclareTextFontCommand{\proportionalnums}{\fontfamily{EBGaramond-\ebgaramond@figurestyle}\selectfont}
\fi

\def\EBGaramond@MT@Hook{%
  \DeclareMicrotypeAlias{EBGaramond12}{EBGaramond}
  \DeclareMicrotypeAlias{EBGaramond-LF}{EBGaramond}
  \DeclareMicrotypeAlias{EBGaramond-OsF}{EBGaramond}
  \DeclareMicrotypeAlias{EBGaramond-Sup}{EBGaramond}
  \DeclareMicrotypeAlias{EBGaramond-TLF}{EBGaramond}
  \DeclareMicrotypeAlias{EBGaramond-TOsF}{EBGaramond}
  \DeclareMicrotypeAlias{EBGaramondInitials-TLF}{EBGaramondInitials}
}
\@ifpackageloaded{microtype}
{\EBGaramond@MT@Hook}
{\@ifundefined{Microtype@Hook}
  {\let\Microtype@Hook\EBGaramond@MT@Hook}
  {\g@addto@macro\Microtype@Hook{\EBGaramond@MT@Hook}}} 

\ifebgaramond@otf
  \defaultfontfeatures{}
\fi

\endinput
