\ProvidesPackage{libertine}[%
   2012/11/26 (Bob Tennent)  Supports Libertine and Biolinum fonts for all LaTeX engines. ] 

%  with (partial) compatibilty with libertine-type1, biolinum-type1,    
%  libertine, libertine-legacy and libertineotf packages                

\RequirePackage{ifxetex,ifluatex,xkeyval,textcomp}

\newif\iflibertine@otf
\ifxetex
  \libertine@otftrue
\else\ifluatex
  \libertine@otftrue
\else  % [pdf]LaTeX
  \libertine@otffalse
\fi\fi

\newif\iflibertine@sf \libertine@sffalse
\newif\iflibertine@rm \libertine@rmfalse
\newif\iflibertine@sfdefault \libertine@sfdefaultfalse
\newif\iflibertine@lining \libertine@liningfalse
\newif\ifbiolinum@lining \biolinum@liningfalse
\newif\iflibertine@tabular \libertine@tabularfalse
\newif\ifbiolinum@tabular \biolinum@tabularfalse
\newif\iflibertine@semibold \libertine@semiboldfalse

\DeclareOptionX{biolinum}{\libertine@sftrue}
\DeclareOptionX{sf}{\libertine@sftrue}
\DeclareOptionX{libertine}{\libertine@rmtrue}
\DeclareOptionX{rm}{\libertine@rmtrue}
\DeclareOptionX{sfdefault}{\libertine@sftrue\libertine@sfdefaulttrue}
\DeclareOptionX{type1}{\libertine@otffalse}
\DeclareOptionX{lining}{\libertine@liningtrue\biolinum@liningtrue}
\DeclareOptionX{nf}{\libertine@liningtrue\biolinum@liningtrue}
\DeclareOptionX{lf}{\libertine@liningtrue\biolinum@liningtrue}
\DeclareOptionX{oldstyle}{\libertine@liningfalse\biolinum@liningfalse}
\DeclareOptionX{osf}{\libertine@liningfalse\biolinum@liningfalse}
\DeclareOptionX{sflining}{\biolinum@liningtrue}
\DeclareOptionX{sflf}{\biolinum@liningtrue}
\DeclareOptionX{sfoldstyle}{\biolinum@liningfalse}
\DeclareOptionX{sfosf}{\biolinum@liningfalse}
\DeclareOptionX{tabular}{\libertine@tabulartrue\biolinum@tabulartrue}
\DeclareOptionX{t}{\libertine@tabulartrue\biolinum@tabulartrue}
\DeclareOptionX{proportional}{\libertine@tabularfalse\biolinum@tabularfalse}
\DeclareOptionX{p}{\libertine@tabularfalse\biolinum@tabularfalse}
\DeclareOptionX{sftabular}{\biolinum@tabulartrue}
\DeclareOptionX{sft}{\biolinum@tabulartrue}
\DeclareOptionX{sfproportional}{\biolinum@tabularfalse}
\DeclareOptionX{sfp}{\biolinum@tabularfalse}
\DeclareOptionX{semibold}{\libertine@semiboldtrue}
\DeclareOptionX{sb}{\libertine@semiboldtrue}
\DeclareOptionX{bold}{\libertine@semiboldfalse}

\newcommand*{\LinuxLibertineT@scale}{1}  % not adjustable
\newcommand*{\LinuxBiolinumT@scale}{1}
\DeclareOptionX{scaled}{\renewcommand*{\LinuxBiolinumT@scale}{#1}}
\DeclareOptionX{scale}{\renewcommand*{\LinuxBiolinumT@scale}{#1}}

\ExecuteOptionsX{oldstyle,proportional,bold}
\ProcessOptionsX\relax

\iflibertine@otf
  \iflibertine@lining
    \def\libertine@figurestyle{Lining}
  \else
    \def\libertine@figurestyle{OldStyle}
  \fi
  \iflibertine@tabular
    \def\libertine@figurealign{Monospaced} 
  \else
    \def\libertine@figurealign{Proportional} 
  \fi
  \ifbiolinum@lining
    \def\biolinum@figurestyle{}  % lnum feature missing in RB (and hence RBO)
  \else
    \def\biolinum@figurestyle{OldStyle}
  \fi
  \ifbiolinum@tabular
    \def\biolinum@figurealign{Monospaced} 
  \else
    \def\biolinum@figurealign{Proportional} 
  \fi
  \iflibertine@semibold
     \def\libertine@boldstyle{RZ} 
  \else
    \def\libertine@boldstyle{RB} 
  \fi

\else % type1

  \iflibertine@lining
    \def\libertine@figurestyle{LF} 
  \else
    \def\libertine@figurestyle{OsF} 
  \fi
  \iflibertine@tabular
    \def\libertine@figurealign{T} 
  \else
    \def\libertine@figurealign{} 
  \fi
  \ifbiolinum@lining
    \def\biolinum@figurestyle{LF} 
  \else
    \def\biolinum@figurestyle{OsF} 
  \fi
  \ifbiolinum@tabular
    \def\biolinum@figurealign{T} 
  \else
    \def\biolinum@figurealign{} 
  \fi
  \iflibertine@semibold
    \renewcommand*{\bfdefault}{sb} 
  \else
    \renewcommand*{\bfdefault}{b} 
  \fi
\fi


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

\iflibertine@rm\else\iflibertine@sf\else
  \libertine@rmtrue\libertine@sftrue  % both false -> both true
\fi\fi

% Set main and/or sans fonts:

\iflibertine@otf

  \ifxetex\XeTeXtracingfonts=1\fi
  \defaultfontfeatures{
     Ligatures = TeX ,
     Extension = .otf ,
     SmallCapsFeatures={Letters=SmallCaps} 
  }
  \iflibertine@rm
    \setmainfont
          [ Numbers = {\libertine@figurealign,\libertine@figurestyle},
            UprightFont    = *_R,
            ItalicFont     = *_RI,
            BoldFont       = *_\libertine@boldstyle, 
            BoldItalicFont = *_\libertine@boldstyle I
          ] {LinLibertine530}
  \fi
  \iflibertine@sf
    \setsansfont
          [ Numbers   = {\biolinum@figurealign, \biolinum@figurestyle} ,
            Scale     = \LinuxBiolinumT@scale ,
            UprightFont    = *_R ,
            ItalicFont     = *_RI ,
            BoldFont       = *_RB , 
            BoldItalicFont = *_RBO   % oblique to fake italic
          ] {LinBiolinum530}
    % grab the \sfdefault in case it changes subsequently:
    \let\biolinum@family\sfdefault
    \iflibertine@sfdefault\renewcommand*\familydefault{\biolinum@family}\fi
  \fi
  \newfontfamily\libertine
        [ Numbers = {\libertine@figurealign,\libertine@figurestyle},
          UprightFont    = *_R,
          ItalicFont     = *_RI,
          BoldFont       = *_\libertine@boldstyle, 
          BoldItalicFont = *_\libertine@boldstyle I
        ] {LinLibertine530}
  \newfontfamily\libertineSB
        [ Numbers = {\libertine@figurealign,\libertine@figurestyle},
          UprightFont    = *_R,
          ItalicFont     = *_RI,
          BoldFont       = *_RZ, 
          BoldItalicFont = *_RZI
        ] {LinLibertine530}
  \newfontfamily\libertineOsF
        [ Numbers = {\libertine@figurealign,OldStyle},
          UprightFont    = *_R,
          ItalicFont     = *_RI,
          BoldFont       = *_\libertine@boldstyle, 
          BoldItalicFont = *_\libertine@boldstyle I
        ] {LinLibertine530}
  \newfontfamily\libertineLF
        [ Numbers = {\libertine@figurealign,Lining},
          UprightFont    = *_R,
          ItalicFont     = *_RI,
          BoldFont       = *_\libertine@boldstyle, 
          BoldItalicFont = *_\libertine@boldstyle I
        ] {LinLibertine530}
  \newfontfamily\biolinum
        [ Numbers   = {\biolinum@figurealign, \biolinum@figurestyle} ,
          Scale     = \LinuxBiolinumT@scale ,
          UprightFont    = *_R ,
          ItalicFont     = *_RI ,
          BoldFont       = *_RB , 
          BoldItalicFont = *_RBO   % oblique to fake italic
        ] {LinBiolinum530}
  \newfontfamily\biolinumOsF
        [ Numbers   = {\biolinum@figurealign, OldStyle} ,
          Scale     = \LinuxBiolinumT@scale ,
          UprightFont    = *_R ,
          ItalicFont     = *_RI ,
          BoldFont       = *_RB , 
          BoldItalicFont = *_RBO   % oblique to fake italic
        ] {LinBiolinum530}
  \newfontfamily\biolinumLF
        [ Numbers   = {\biolinum@figurealign} , % lnum feature missing
          Scale     = \LinuxBiolinumT@scale ,
          UprightFont    = *_R ,
          ItalicFont     = *_RI ,
          BoldFont       = *_RB , 
          BoldItalicFont = *_RBO   % oblique to fake italic
        ] {LinBiolinum530}

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

\else % type1

  \def\libertine@family{LinuxLibertineT-\libertine@figurealign\libertine@figurestyle}
  \def\biolinum@family{LinuxBiolinumT-\biolinum@figurealign\biolinum@figurestyle}

  \iflibertine@rm \renewcommand*\rmdefault{\libertine@family} \fi
  \iflibertine@sf \renewcommand*\sfdefault{\biolinum@family} \fi 
  \iflibertine@sfdefault \renewcommand*\familydefault{\biolinum@family} \fi
  \newcommand*\libertine{\fontfamily{\libertine@family}\selectfont}
  \newcommand*\libertineSB{\fontfamily{\libertine@family}\selectfont\renewcommand\bfdefault{sb}}
  \newcommand*\libertineOsF{\fontfamily{LinuxLibertineT-\libertine@figurealign OsF}\selectfont}
  \newcommand*\libertineLF{\fontfamily{LinuxLibertineT-\libertine@figurealign LF}\selectfont}
  \newcommand*\biolinum{\fontfamily{\biolinum@family}\selectfont}
  \newcommand*\biolinumOsF{\fontfamily{LinuxBiolinumT-\biolinum@figurealign OsF}\selectfont}
  \newcommand*\biolinumLF{\fontfamily{LinuxBiolinumT-\biolinum@figurealign LF}\selectfont}

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

\fi
\DeclareTextFontCommand{\textsu}{\sufigures}
\let\textsuperior\textsu

% Define \oldstylenums[f] and \liningnums[f]

\iflibertine@otf
  \iflibertine@rm
    \renewcommand*\oldstylenums[1]{{\libertineOsF #1}}
    \renewcommand*\liningnums[1]{{\libertineLF #1}}
  \fi
  \iflibertine@sf
    \newcommand*\oldstylenumsf[1]{{\biolinumOsF #1}}
    \newcommand*\liningnumsf[1]{{\biolinumLF #1}}
  \fi
  \iflibertine@rm
    \iflibertine@sf\else % Libertine only
      \newcommand*\oldstylenumsf [1]{\textsf{\oldstylenums{#1}}}
      \newcommand*\liningnumsf [1]{\textsf{\liningnums{#1}}}
    \fi    
  \else
    \iflibertine@sf % Biolinum only
      %  \oldstynums and \liningnums defined by \fontspec ?
    \fi
  \fi
\else % type1
  \iflibertine@rm
    \iflibertine@sf % Both Libertine and Biolinum
      \renewcommand*\oldstylenums [1]
        {{\libertineOsF#1}}
      \newcommand*\liningnums [1]
        {{\libertineLF#1}}
      \newcommand*\oldstylenumsf [1]
        {{\biolinumOsF#1}}
      \newcommand*\liningnumsf [1]
        {{\biolinumLF#1}}
   \else % Libertine only 
      \renewcommand*\oldstylenums [1]
        {{\libertineOsF#1}}
      \newcommand*\liningnums [1]
        {{\libertineLF#1}}
      \newcommand*\oldstylenumsf [1]{\textsf{\oldstylenums{#1}}}
      \newcommand*\liningnumsf [1]{\textsf{#1}}
   \fi
  \else % Biolinum only
    % \oldstylenums defined by \textcomp?
    \newcommand*\liningnums [1]{#1}
    \newcommand*\oldstylenumsf [1]
      {{\biolinumOsF#1}}
    \newcommand*\liningnumsf [1]
      {{\biolinumLF#1}}
  \fi
\fi

% Define \tabularnums[f] and \proportionalnums[f]

\iflibertine@otf
  \newfontfamily\libertinetabular
    [ Numbers = {Monospaced,\libertine@figurestyle},
      UprightFont = *_R,
      ItalicFont = *_RI,
      BoldFont = *_\libertine@boldstyle,
      BoldItalicFont = *_\libertine@boldstyle I 
    ] {LinLibertine530}
  \newfontfamily\libertineproportional
    [ Numbers = {Proportional,\libertine@figurestyle},
      UprightFont    = *_R,
      ItalicFont     = *_RI,
      BoldFont       = *_\libertine@boldstyle, 
      BoldItalicFont = *_\libertine@boldstyle I
    ] {LinLibertine530}
  \newfontfamily\biolinumtabular
    [ Numbers = {Monospaced,\biolinum@figurestyle},
      Scale     = \LinuxBiolinumT@scale ,
      UprightFont    = *_R,
      ItalicFont     = *_RI,
      BoldFont       = *_RB,
      BoldItalicFont = *_RBO   % oblique to fake italic
    ] {LinBiolinum530}
  \newfontfamily\biolinumproportional
    [ Numbers = {Proportional,\biolinum@figurestyle},
      Scale     = \LinuxBiolinumT@scale ,
      UprightFont    = *_R,
      ItalicFont     = *_RI,
      BoldFont       = *_RB,
      BoldItalicFont = *_RBO   % oblique to fake italic
    ] {LinBiolinum530}
  \iflibertine@rm
    \newcommand*\tabularnums[1]{{\libertinetabular #1}}
    \newcommand*\proportionalnums[1]{{\libertineproportional #1}}
  \fi
  \iflibertine@sf
    \newcommand*\tabularnumsf[1]{{\biolinumtabular #1}}
    \newcommand*\proportionalnumsf[1]{{\biolinumproportional #1}}
  \fi
\else % type1
  \iflibertine@rm
      \newcommand*\tabularnums[1]
        {{\fontfamily{LinuxLibertineT-T\libertine@figurestyle}\selectfont#1}}
      \newcommand*\proportionalnums[1]
        {{\fontfamily{LinuxLibertineT-\libertine@figurestyle}\selectfont#1}}
  \fi
  \iflibertine@sf
    \newcommand*\tabularnumsf[1]
      {{\fontfamily{LinuxBiolinumT-T\biolinum@figurestyle}\selectfont#1}}
    \newcommand*\proportionalnumsf[1]
      {{\fontfamily{LinuxBiolinumT-\biolinum@figurestyle}\selectfont#1}}
  \fi
\fi

\iflibertine@otf
  \def\lib@fxl{fxl}
  \def\lib@fxb{fxb}

  \newcommand*\DeclareTextGlyphY[3]{%
      % \DeclareTextGlyphY{fxl}{s_t}{64262}
      % #1=basefont #2=glyphname, #3=position
      \def\lib@temp{#1}%
      \ifx\lib@temp\lib@fxl \@namedef{#1@#2}{{\libertine\char#3\relax}}\else%
        \ifx\lib@temp\lib@fxb \@namedef{#1@#2}{{\biolinum\char#3\relax}}\else%
      \fi\fi}
  %
  \input{fxluni.inc}
  \input{fxbuni.inc}
  %
  \DeclareRobustCommand*\libertineGlyph[1]{\@nameuse{fxl@#1}}
  \DeclareRobustCommand*\biolinumGlyph[1]{\@nameuse{fxb@#1}}
\fi


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

\endinput
