\ProvidesPackage{alegreya}[%
   2013/11/08 (Bob Tennent)  Supports Alegreya fonts for all LaTeX engines. ] 

\RequirePackage{ifxetex,ifluatex,textcomp}

\newif\ifalegreya@ttf
\ifxetex
  \alegreya@ttftrue
\else\ifluatex
  \alegreya@ttftrue
\else  % [pdf]LaTeX
  \alegreya@ttffalse
\fi\fi

\newif\ifalegreya@lining \alegreya@liningtrue
\newif\ifalegreya@tabular \alegreya@tabularfalse
\newif\ifalegreya@black \alegreya@blackfalse

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

\DeclareOptionX{type1}{\alegreya@ttffalse}

\DeclareOptionX{lining}[true]{%
\@nameuse{if#1}%
  \alegreya@liningtrue
\else
  alegreya@liningfalse
\fi
}
\DeclareOptionX{lf}[true]{%
\@nameuse{if#1}%
  \alegreya@liningtrue
\else
  alegreya@liningfalse
\fi
}
\DeclareOptionX{oldstyle}[true]{%
\@nameuse{if#1}%
  \alegreya@liningfalse
\else
  \alegreya@liningtrue
\fi
}

\DeclareOptionX{osf}[true]{%
\@nameuse{if#1}%
  \alegreya@liningfalse
\else
  \alegreya@liningtrue
\fi
}

\DeclareOptionX{tabular}[true]{%
\@nameuse{if#1}%
  \alegreya@tabulartrue
\else
  alegreya@tabularfalse
\fi
}
\DeclareOptionX{tf}[true]{%
\@nameuse{if#1}%
  \alegreya@tabulartrue
\else
  alegreya@tabularfalse
\fi
}
\DeclareOptionX{proportional}[true]{%
\@nameuse{if#1}%
  \alegreya@tabularfalse
\else
  \alegreya@tabulartrue
\fi
}

\DeclareOptionX{pf}[true]{%
\@nameuse{if#1}%
  \alegreya@tabularfalse
\else
  \alegreya@tabulartrue
\fi
}

\DeclareOptionX{black}[true]{%
\@nameuse{if#1}%
  \alegreya@blacktrue
\else
  \alegreya@blackfalse
\fi
}

\ProcessOptionsX\relax

\ifalegreya@ttf
  \ifalegreya@lining
    \def\alegreya@figurestyle{Lining}
  \else
    \def\alegreya@figurestyle{OldStyle}
  \fi
  \ifalegreya@tabular
    \def\alegreya@figurealign{Monospaced}
  \else
    \def\alegreya@figurealign{Proportional}
  \fi
  \ifalegreya@black
    \def\alegreya@boldstyle{Black}
  \else
    \def\alegreya@boldstyle{Bold}
  \fi

\else % type1

  \ifalegreya@lining
    \def\alegreya@figurestyle{LF} 
  \else
    \def\alegreya@figurestyle{OsF} 
  \fi
  \ifalegreya@tabular
    \def\alegreya@figurealign{T}
  \else
    \def\alegreya@figurealign{}
  \fi
  \ifalegreya@black
    \def\bfseries@rm{k}
  \else
    \def\bfseries@rm{b}
  \fi
\fi


\ifalegreya@ttf
  \RequirePackage{fontspec}
\else
  \RequirePackage{fontenc,fontaxes,mweights}
\fi

\ifalegreya@ttf
  \ifxetex\XeTeXtracingfonts=1\fi
  \defaultfontfeatures{
     Ligatures = TeX ,
     Extension = .ttf ,
     Scale     = \Alegreya@scale ,
  }
  \setmainfont
      [ Numbers = {\alegreya@figurealign,\alegreya@figurestyle},
        UprightFont    = *-Regular ,
        ItalicFont     = *-Italic ,
        BoldFont       = *-\alegreya@boldstyle , 
        BoldItalicFont = *-\alegreya@boldstyle Italic ]
      {Alegreya}
  \providecommand\useosf{%
    \edef\alegreya@figurestyle{OldStyle}
    \defaultfontfeatures{
       Ligatures = TeX ,
       Extension = .ttf ,
       Scale = \Alegreya@scale ,
    }
    \setmainfont
        [ Numbers = {\alegreya@figurealign,\alegreya@figurestyle},
          UprightFont    = *-Regular ,
          ItalicFont     = *-Italic ,
          BoldFont       = *-\alegreya@boldstyle , 
          BoldItalicFont = *-\alegreya@boldstyle Italic ]
        {Alegreya}
    \defaultfontfeatures{}
  }
  \newfontfamily\alegreya
      [ Numbers = {\alegreya@figurealign,\alegreya@figurestyle},
        UprightFont    = *-Regular ,
        ItalicFont     = *-Italic ,
        BoldFont       = *-\alegreya@boldstyle , 
        BoldItalicFont = *-\alegreya@boldstyle Italic ]
      {Alegreya}
  \newfontfamily\alegreyaOsF
        [ Numbers = {Proportional,OldStyle},
        UprightFont    = *-Regular ,
        ItalicFont     = *-Italic ,
        BoldFont       = *-\alegreya@boldstyle , 
        BoldItalicFont = *-\alegreya@boldstyle Italic ]
      {Alegreya}
  \newfontfamily\alegreyaLF
        [ Numbers = {Proportional,Lining},
        UprightFont    = *-Regular ,
        ItalicFont     = *-Italic ,
        BoldFont       = *-\alegreya@boldstyle , 
        BoldItalicFont = *-\alegreya@boldstyle Italic ]
      {Alegreya}
  \newfontfamily\alegreyaTOsF
        [ Numbers = {Monospaced,OldStyle},
        UprightFont    = *-Regular ,
        ItalicFont     = *-Italic ,
        BoldFont       = *-\alegreya@boldstyle , 
        BoldItalicFont = *-\alegreya@boldstyle Italic ]
      {Alegreya}
  \newfontfamily\alegreyaTLF
        [ Numbers = {Monospaced,Lining},
        UprightFont    = *-Regular ,
        ItalicFont     = *-Italic ,
        BoldFont       = *-\alegreya@boldstyle , 
        BoldItalicFont = *-\alegreya@boldstyle Italic ]
      {Alegreya}
  \newfontfamily\alegreyaBlck
      [ Numbers = {\alegreya@figurealign,\alegreya@figurestyle},
        UprightFont    = *-Black ,
        ItalicFont     = *-BlackItalic ,
        BoldFont       = *-Black ,
        BoldItalicFont = *-BlackItalic ]
      {Alegreya}
  \newfontfamily\alegreyasc
      [ UprightFont    = *-Regular ,
        ItalicFont     = *-Italic ,
        BoldFont       = *-\alegreya@boldstyle , 
        BoldItalicFont = *-\alegreya@boldstyle Italic ]
      {AlegreyaSC}
  \newfontfamily\alegreyaBlcksc
      [ UprightFont    = *-Black ,
        ItalicFont     = *-BlackItalic ,
        BoldFont       = *-Black , 
        BoldItalicFont = *-BlackItalic ]
      {AlegreyaSC}

  \providecommand\sufigures{\addfontfeatures{VerticalPosition=Superior}}  


\else % type1

  \def\alegreyafamily{Alegreya-\alegreya@figurealign \alegreya@figurestyle}
  \providecommand*\alegreya{\fontfamily{\alegreyafamily}\selectfont}
  \providecommand*\alegreyaLF{\fontfamily{Alegreya-LF}\selectfont}
  \providecommand*\alegreyaOsF{\fontfamily{Alegreya-OsF}\selectfont}
  \providecommand*\alegreyaTLF{\fontfamily{Alegreya-TLF}\selectfont}
  \providecommand*\alegreyaTOsF{\fontfamily{Alegreya-TOsF}\selectfont}
  \providecommand*\alegreyaBlck{\fontseries{k}\selectfont}
  \providecommand*\alegreyasc{\fontfamily{AlegreyaSC-OsF}\selectfont}
  \providecommand*\alegreyaBlcksc{\fontfamily{AlegreyaSC-OsF}\fontseries{k}\selectfont}

  \fa@naming@exception{figures}{{superior}{proportional}}{Sup}
  \fa@naming@exception{figures}{{superior}{tabular}}{Sup}
  \providecommand\sufigures{\@nomath\sufigures\fontfigurestyle{superior}\selectfont}
  \renewcommand*\rmdefault{\alegreyafamily}
  \providecommand*\useosf{\edef\alegreya@figurestyle{OsF}\renewcommand*{\rmdefault}{Alegreya-\alegreya@figurealign OsF}}

\fi
\DeclareTextFontCommand{\textsu}{\sufigures}
\renewcommand*{\scshape}{\alegreyasc}

% turn off defaults in case other fonts are selected:
\ifalegreya@ttf
  \defaultfontfeatures{}
\fi

\endinput

