% \iffalse meta-comment
%
% File: iwonamath.dtx
% Copyright 2023 by Boris Veytsman
%
% It 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
%
%    https://www.latex-project.org/lppl.txt
%
%<*driver>
\documentclass{l3doc}
\usepackage{iwonamath}
\usepackage{natbib, booktabs}
\usepackage[tableposition=top]{caption}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{iwonamath.sty}
% \title{\pkg{iwonamath}---a scaled version of Iwona math fonts}
% \date{\fileversion, \filedate}
% \author{Boris
% Veytsman\thanks{\href{mailto:borisv@lk.net}{borisv@lk.net},
% \href{mailto:boris@varphi.com}{boris@varphi.com}}}
% \maketitle
% \begin{abstract}
%   \LaTeX\ support for scaled Iwona math fonts for mixing with sans
%   serif text fonts.
% \end{abstract}
% \begin{documentation}
%
%\section{User manual}
%\label{sec:ug}
%
%
% Iwona is a sans serif typeface by Janusz Marian Nowacki.  It has a
% very good math support~\citep{iwona}.  Package \pkg{iwona} integrates
% text and math fonts.  However, the math fonts may provide
% interesting companions for other text fonts.  To enable the
% combination, this package separates math fonts and provides tools
% for the package writers to mix and match them with text.
%
% \begin{variable}{
%   options/light,
%   options/condensed,
%   options/scale,
%   options/Scale,
%   options/delimitershack,
%   options/standardversion}
% The package has the following options:
% \begin{description}
% \item[light] whether the math fonts use the light version of Iwona,
% either |true| or |false| (default)
% \item[condensed] whether the math fonts use the condensed version of
% Iwona, either |true| or |false| (default).
% \item[scale] the scale of the fonts, a number (by default 1).  The
% title cased |Scale| is the synonym for this key.
% \item[delimitershack] whether to use the hack to get \cs{lVert} and
% \cs{rVert} delimiters, absent in the original font (see
% \citep[\S~12.5.5]{TLC3}), either |true| (default) or |false|.
% \item[standardversions] whether to define standard versions |normal|
% and |bold|, either |true| (default) or |false|.  If |false|, then
% the package does not define any math fonts, and it is up to the user
% to deploy \cmd{\DefineIwonaMathVersion} do define them.  
% \end{description}
% \end{variable}
%
%
% \begin{function}{\DefineIwonaMathVersion}
%   \begin{syntax}
%     \cs{DefineIwonaMathVersion}\Arg{key/value pairs}
%   \end{syntax}
%   The main function of the package,
%   \cmd{\DefineIwonaMathVersion} defines a new
%   math version based on Iwona fonts.  
% \end{function}
% The keys are the following (the defaults, where applicable,
% correspond to the package options):
% \begin{variable}{main/name,
%   main/light,
%   main/condensed,
%   main/bold}
%   \begin{description}
%   \item[name] the name of the version.  By default, either |normal|
%   or |bold| depending on the |bold| key described below.
%   \item[light] whether the math fonts use the light version of
%   Iwona, either |true| or |false|
% \item[condensed] whether the math fonts use the condensed version of
% Iwona, either |true| or |false|.
%   \item[bold] whether we define a bold version, either |true| or
%   |false| (default)
%   \end{description}
% \end{variable}
%
% For the boolean keys the setting |=true| can be omitted, so
% |\DefineIwonaMathVersion{light=true}| and
% |\DefineIwonaMathVersion{light}| are equivalent
%
% Note that in the current implementation the parameters |scale| and
% |delimitershack| are the same for all versions defined.
%
% For example, the following invocation defines four math versions,
% |normal|, |bold|, |condensed| and |boldcondensed|, based on Iwona
% light, scaled 1.2:
% \begin{verbatim}
% \usepackage[scale=1.2, light]{iwonamath}
% \DefineIwonaMathVersion{name=condensed, 
%                          condensed=true}
% \DefineIwonaMathVersion{name=boldcondensed, 
%                          bold=true, condensed=true}
% \end{verbatim}
%
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{Implementation}
% \label{sec:impl}
%
% 
%
%\subsection{Setting up}
%\label{sec:settingup}
%
%
% 
% First, we declare who we are:
%    \begin{macrocode}
%<@@=iwonamath>
%<package>\ProvidesExplPackage {iwonamath}
%<fd>\ProvidesExplFile
%<ot1m>{ot1_FAMILY_m.fd}
%<ot1>{ot1_FAMILY_.fd}
%<oml>{oml_FAMILY_.fd}
%<oms>{oms_FAMILY_.fd}
%<omx>{omx_FAMILY_.fd}
%<cmsy>{omsiwonamathcmsy.fd}
%<package|fd>{2024-07-19} {1.1}
%<package|fd>{Scaled Iwona math fonts}
%<*package>
%    \end{macrocode}
%
%
%
%
%\subsection{Options}
%\label{sec:optionSetting}
%
%
% 
% \begin{variable}{\l_@@_scale_tl,
%                  \l_@@_mainlight_bool,
%                  \l_@@_maincondensed_bool,
%                  \l_@@_delimitershack_bool,
%                  \l_@@_standardversions_bool,
%                  options/light,
%                  options/condensed,
%                  options/scale,
%                  options/Scale,
%                  options/delimitershack,
%                  options/standardversion}
%    \begin{macrocode}
\keys_define:nn { iwonamath/options }
{
  scale .tl_set:N = \l_@@_scale_tl,
  Scale .tl_set:N = \l_@@_scale_tl,
  light .bool_set:N = \l_@@_mainlight_bool,
  light .default:n = true,
  condensed .bool_set:N = \l_@@_maincondensed_bool,
  condensed .default:n = true,
  delimitershack .bool_set:N = \l_@@_delimitershack_bool,
  delimitershack .default:n = true,
  standardversions .bool_set:N = \l_@@_standardversions_bool,
  standardversions .default:n = true,
}

\keys_set:nn {iwonamath/options }
{
  scale = 1,
  light = false,
  condensed = false,
  delimitershack = true,
  standardversions = true,
}
%    \end{macrocode}
%   
% \end{variable}
%
%
% Options processing
%    \begin{macrocode}
\IfFormatAtLeastTF { 2022-06-01 }
  { \ProcessKeyOptions [ iwonamath/options ] }
  {
    \RequirePackage { l3keys2e }
    \ProcessKeysOptions { iwonamath/options }
  }
%    \end{macrocode}% 
%
% \begin{variable}{\l_@@_versionname_tl,
%                  \l_@@_light_bool,
%                  \l_@@_condensed_bool,
%                  \l_@@_bold_bool,
%                  main/name,
%                  main/light,
%                  main/condensed,
%                  main/bold}
% Now the options for the main command
%    \begin{macrocode}
\keys_define:nn { iwonamath/main }
{
  name .tl_set:N = \l_@@_versionname_tl,
  light .bool_set:N = \l_@@_light_bool,
  light .default:n = true,
  condensed .bool_set:N = \l_@@_condensed_bool,
  condensed .default:n = true,
  bold .bool_set:N = \l_@@_bold_bool,
  bold .default:n = true,
}
%    \end{macrocode}
%   
% \end{variable}
%
%
%\subsection{Delimiters hack}
%\label{sec:delimiters}
%
% We add delimiters from |cmsy|, as discussed in
% \citep[\S~12.5.5]{TLC3}).  We predefine the symbols for the standard
% versions just in case.  The last line is suggested by Enrico
% Gregorio. 
%    \begin{macrocode}
\bool_if:NTF \l_@@_delimitershack_bool
{
  \DeclareSymbolFont{symbols2}{OMS}{iwonamathcmsy}{m}{n}
  \SetSymbolFont{symbols2}    {bold}{OMS}{cmsy}{b}{n}
  \DeclareMathDelimiter{\lVert}{\mathopen} {symbols2}{"6B}{largesymbols}{"0D}
  \DeclareMathDelimiter{\rVert}{\mathclose}{symbols2}{"6B}{largesymbols}{"0D}
  \DeclareMathDelimiter{\|}{\mathclose}{symbols2}{"6B}{largesymbols}{"0D}
}
{}
%    \end{macrocode}
%
%
%\subsection{The main function}
%\label{sec:main}
%
%
% \begin{macro}{\DefineIwonaMathVersion}
%    \begin{macrocode}
\DeclareDocumentCommand \DefineIwonaMathVersion { m }
{
  \tl_clear:N \l_@@_versionname_tl
  \bool_set_eq:NN \l_@@_light_bool \l_@@_mainlight_bool
  \bool_set_eq:NN \l_@@_condensed_bool \l_@@_maincondensed_bool
  \bool_set_false:N \l_@@_bold_bool 
  \keys_set:nn { iwonamath/main }
  {
    #1
  }

  \tl_if_empty:NTF \l_@@_versionname_tl
  {
    \bool_if:NTF \l_@@_bold_bool
    {
      \tl_set:Nn \l_@@_versionname_tl {bold}
    }
    {
      \tl_set:Nn \l_@@_versionname_tl {normal}
    }
  }
  {}
  
  \tl_set:Nn \l_tmpa_tl {iwonamath}


  \bool_if:NTF \l_@@_light_bool
  {
    \tl_set:Ne \l_tmpa_tl {\l_tmpa_tl l}
  }
  {}
  
  \bool_if:NTF \l_@@_condensed_bool
  {
    \tl_set:Ne \l_tmpa_tl {\l_tmpa_tl c}
  }
  {}

  \DeclareMathVersion{\l_@@_versionname_tl}
  \bool_if:NTF \l_@@_bold_bool
  {
    \SetSymbolFont{operators}{\l_@@_versionname_tl}{OT1}{\l_tmpa_tl m} {b}{n}
    \SetSymbolFont{letters}  {\l_@@_versionname_tl}{OML}{\l_tmpa_tl} {b}{it}
    \SetSymbolFont{symbols}  {\l_@@_versionname_tl}{OMS}{\l_tmpa_tl}{b}{n}
    \SetSymbolFont{largesymbols}{\l_@@_versionname_tl}{OMX}{\l_tmpa_tl}{b}{n}
    \SetMathAlphabet\mathsf{\l_@@_versionname_tl}{OT1}{\l_tmpa_tl}{bx}{n}
    \SetMathAlphabet\mathit{\l_@@_versionname_tl}{OT1}{\l_tmpa_tl}{bx}{it}
    \SetSymbolFont{symbols2}    {\l_@@_versionname_tl}{OMS}{iwonamathcmsy}{b}{n}
  }
  {
    \SetSymbolFont{operators}{\l_@@_versionname_tl}{OT1}{\l_tmpa_tl m}{m}{n}
    \SetSymbolFont{letters}{\l_@@_versionname_tl}{OML}{\l_tmpa_tl} {m}{it}
    \SetSymbolFont{symbols}{\l_@@_versionname_tl}{OMS}{\l_tmpa_tl}{m}{n}
    \SetSymbolFont{largesymbols}{\l_@@_versionname_tl}{OMX}{\l_tmpa_tl}{m}{n}
    \SetMathAlphabet{\mathbf}{\l_@@_versionname_tl}{OT1}{\l_tmpa_tl}{bx}{n}
    \SetMathAlphabet{\mathsf}{\l_@@_versionname_tl}{OT1}{\l_tmpa_tl}{m}{n}
    \SetMathAlphabet{\mathit}{\l_@@_versionname_tl}{OT1}{\l_tmpa_tl}{m}{it}
    \SetMathAlphabet{\mathtt}{\l_@@_versionname_tl}{OT1}{\l_tmpa_tl}{m}{n}
    \SetSymbolFont{symbols2}    {\l_@@_versionname_tl}{OMS}{iwonamathcmsy}{m}{n}
  }

  \bool_if:NTF \l_@@_delimitershack_bool
  {
    
  }
  {}
  
}
%    \end{macrocode}
% \end{macro}
%
%
%
%\subsection{Default versions}
%\label{sec:defaults}
%
% If standard versions are requested, we define them
%    \begin{macrocode}
\bool_if:NTF \l_@@_standardversions_bool
{
  \DefineIwonaMathVersion{}
  \DefineIwonaMathVersion{bold}
}
{}
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
%\subsection{Font definition files}
%\label{sec:fd}
%
%
%
% Now, the fd files.  Sometimes they are defined in special |fdd|
% files; here we use the main |dtx| for this.
%
% First, we check if the size is defined.  If not,
% we define it.
%    \begin{macrocode}
%<*fd>
\tl_if_exist:NTF \l_@@_scale_tl
{}
{
  \tl_new:N \l_@@_scale_tl
  \tl_set:Nn \l_@@_scale_tl {1}
}
%</fd>
%    \end{macrocode}
%
% Our version of |cmsy| just scales the font.  Note that right now the
% scaling is exactly the same as for other iwona math
% characters---maybe we need to fine tune this.
%    \begin{macrocode}
%<*cmsy>
\DeclareFontFamily{OMS}{iwonamathcmsy}{\skewchar\font48 }
\DeclareFontShape{OMS}{iwonamathcmsy}{m}{n}{%
      <-> [\l_@@_scale_tl] cmsy10%
      }{}
\DeclareFontShape{OMS}{iwonamathcmsy}{b}{n}{%
      <-> [\l_@@_scale_tl] cmbsy10%
      }{}
%</cmsy>
%    \end{macrocode}
%
% \begin{table}
%   \centering
%   \caption{Naming scheme for iwona fonts}
%   \label{tab:naming}
%   \begin{tabular}{lllll}
%       \toprule
%       Weight/Shape & \multicolumn{4}{c}{Base}\\
%       \cmidrule{2-5}\\
%                      & Regular & Condensed & Light & Light Condensed \\
%       \midrule
%       m/n & iwonar & iwonacr & iwonal & iwonacl \\
%       m/it & iwonari & iwonacri & iwonali & iwonacli \\
%       b/n & iwonab & iwonacb & iwonam & iwonacm \\
%       b/it & iwonabi & iwonacb & iwonami & iwonacmi \\
%       \bottomrule
%     \end{tabular}
% \end{table}
% 
% Now, we need many files in the different weights and
% condensed/regular status.  It would be too tedious to write all
% them.  So we create a template with the special marks and a bash
% script to generate all |fd| files.  Of course, \TeX\ with enough
% trickery can be used instead of bash, but why bother: we employ
% Makefiles anyways\ldots
%
% The naming scheme for Iwona fonts is shown in
% Table~\ref{tab:naming}.  From this table we see we need three marks:
% |_FAMILY_| for the base family, |_MEDIUM_| for medium font and
% |_BOLD_| for bold font.
%
% We have two |OT1| files: one for default letters, one for |\math...|
% commands.
%    \begin{macrocode}
%<*ot1m>
\DeclareFontFamily{OT1}{_FAMILY_m}{}
\DeclareFontShape{OT1}{_FAMILY_m}{m}{n}{<-> [\l_@@_scale_tl] rm-_MEDIUM_}{}
\DeclareFontShape{OT1}{_FAMILY_m}{b}{n}{<-> [\l_@@_scale_tl] rm-_BOLD_}{}
%</ot1m>
%<*ot1>
\DeclareFontFamily{OT1}{_FAMILY_}{}
\DeclareFontShape{OT1}{_FAMILY_}{m}{n}{<-> [\l_@@_scale_tl] rm-_MEDIUM_}{}
\DeclareFontShape{OT1}{_FAMILY_}{m}{it}{<-> [\l_@@_scale_tl] rm-_MEDIUM_i}{}
\DeclareFontShape{OT1}{_FAMILY_}{b}{n}{<-> [\l_@@_scale_tl] rm-_BOLD_}{}
\DeclareFontShape{OT1}{_FAMILY_}{bx}{n}{<-> [\l_@@_scale_tl] rm-_BOLD_}{}
%</ot1>
%<*oml>
\DeclareFontFamily{OML}{_FAMILY_}{}
\DeclareFontShape{OML}{_FAMILY_}{m}{it}{<-> [\l_@@_scale_tl] mi-_MEDIUM_i}{}
\DeclareFontShape{OML}{_FAMILY_}{b}{it}{<-> [\l_@@_scale_tl] mi-_BOLD_i}{}
\DeclareFontShape{OML}{_FAMILY_}{bx}{it}{<-> [\l_@@_scale_tl] mi-_BOLD_i}{}
%</oml>
%<*oms>
\DeclareFontFamily{OMS}{_FAMILY_}{}
\DeclareFontShape{OMS}{_FAMILY_}{m}{n}{<-> [\l_@@_scale_tl] sy-_MEDIUM_z}{}
\DeclareFontShape{OMS}{_FAMILY_}{b}{n}{<-> [\l_@@_scale_tl] sy-_BOLD_z}{}
\DeclareFontShape{OMS}{_FAMILY_}{bx}{n}{<-> [\l_@@_scale_tl] sy-_BOLD_z}{}
%</oms>
%<*omx>
\DeclareFontFamily{OMX}{_FAMILY_}{}
\DeclareFontShape{OMX}{_FAMILY_}{m}{n}{<-> [\l_@@_scale_tl] ex-_MEDIUM_}{}
\DeclareFontShape{OMX}{_FAMILY_}{b}{n}{<-> [\l_@@_scale_tl] ex-_BOLD_}{}
\DeclareFontShape{OMX}{_FAMILY_}{bx}{n}{<-> [\l_@@_scale_tl] ex-_BOLD_}{}
%</omx>
%    \end{macrocode}
% 
% \end{implementation}
%
% \bibliography{iwonamath}
% \bibliographystyle{plainnat}
%
%  \PrintIndex