<gh_stars>1-10
% fontsize.tex
% Copyright (C) 2018-2020 <NAME>.
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License (LPPL),
% either version 1.3c of this license or (at your option)
% any later version. The latest version of this license
% is in the file:
%
%   http://www.latex-project.org/lppl.txt
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is <NAME>.
%
% This work consists of the files listed in the README.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Deprecations
%

\tl_map_function:nN { \upshape \itshape \swshape \scshape }
  \cs_undefine:N


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Font Setter
%

\dim_new:N \l__soranus_font_size_dim
\dim_new:N \l__soranus_font_baseline_dim
\str_new:N \l__soranus_font_weight_str
\str_new:N \l__soranus_font_width_str
\str_new:N \l__soranus_font_shape_str
\str_new:N \l__soranus_font_caps_str

\keys_define:nn { soranus / font }
  {
    size .dim_set:N = \l__soranus_font_size_dim,
    size .initial:n = 10pt,

    baseline .dim_set:N = \l__soranus_font_baseline_dim,
    baseline .initial:n = 12pt,

    weight .choices:nn =
      {
        ultra-light,
        extra-light,
        light,
        semi-light,
        medium,
        semi-bold,
        bold,
        extra-bold,
        ultra-bold
      }
      {
        \str_set:Nx \l__soranus_font_weight_str
          {
            \str_case:nn { #1 }
              {
                { ultra-light } { ul }
                { extra-light } { el }
                { light } { l }
                { semi-light } { sl }
                { medium } { m }
                { semi-bold } { sb }
                { bold } { b }
                { extra-bold } { eb }
                { ultra-bold } { ub }
              }
          }
      },
    weight .initial:n = medium,

      width .choices:nn =
        {
          ultra-condensed,
          extra-condensed,
          condensed,
          semi-condensed,
          medium,
          semi-expanded,
          expanded,
          extra-expanded,
          ultra-expanded
        }
        {
          \str_set:Nx \l__soranus_font_width_str
            {
              \str_case:nn { #1 }
                {
                  { ultra-condensed } { uc }
                  { extra-condensed } { ec }
                  { condensed } { c }
                  { semi-condensed } { sc }
                  { medium } { m }
                  { semi-expanded } { sx }
                  { expanded } { x }
                  { extra-expanded } { ex }
                  { ultra-expanded } { ux }
                }
            }
        },
      width .initial:n = medium,

      caps .choices:nn =
        {
          upper-lowercase,
          small-caps
        }
        {
          \str_set:Nx \l__soranus_font_caps_str
            {
              \str_case:nn { #1 }
                {
                  { upper-lowercase } { }
                  { small-caps } { sc }
                }
            }
        },
      caps .initial:n = upper-lowercase,

      shape .choices:nn =
        {
          upright,
          italic,
          slanted,
          swash
        }
        {
          \str_set:Nx \l__soranus_font_shape_str
            {
              \str_case:nn { #1 }
                {
                  { upright } { n }
                  { italic } { it }
                  { slanted } { sl }
                  { swash } { sw }
                }
            }
        },
      shape .initial:n = upright
  }

% I forbid normal font switches in the middle of a math environment.
\msg_new:nnn { soranus } { font-switch-in-math-mode }
  { Normal~font~switch~commands~are~forbidden~in~math~mode. }

\cs_new_protected:Nn \__soranus_font_set:n
  {
    \mode_if_math:TF
      { \msg_error:nn { soranus } { font-switch-in-math-mode } }
      {
        \keys_set:nn { soranus / font } { #1 }
        \__soranus_font_set_aux:VVxx
          \l__soranus_font_size_dim
          \l__soranus_font_baseline_dim
          {
            \str_if_eq:VnTF \l__soranus_font_weight_str { m }
              { \str_use:N \l__soranus_font_width_str }
              {
                \str_if_eq:VnTF \l__soranus_font_width_str { m }
                  { \str_use:N \l__soranus_font_weight_str }
                  {
                    \str_use:N \l__soranus_font_weight_str
                    \str_use:N \l__soranus_font_width_str
                  }
              }
          }
          {
            \str_if_eq:VnTF \l__soranus_font_shape_str { n }
              { \str_if_eq:VnTF \l__soranus_font_caps_str { sc } { sc } { n } }
              {
                \str_use:N \l__soranus_font_caps_str
                \str_use:N \l__soranus_font_shape_str
              }
          }
      }
  }

\cs_new_protected:Nn \__soranus_font_set_aux:nnnn
  {
    \fontsize { #1 } { #2 }
    \fontseriesforce { #3 }
    \fontshape { #4 }
    \selectfont
  }

\cs_generate_variant:Nn \__soranus_font_set_aux:nnnn { VVxx }


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Designer Interface
%

\NewDocumentCommand \SetFont { m }
  { \__soranus_font_set:n { #1 } }


%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
% Normal Size
%
% Only \normalsize is defined, to keep 2e happy.

\RenewDocumentCommand \normalsize { }
  { \fontsize { 10pt } { 12pt } \selectfont }
