% \iffalse meta-comment
% 
% File: linguistix-malayalam.dtx
% 
% ----------------------------------------------------------
% The LaTeX bundle LinguisTiX v0.9a
% Copyright © 2026 निरंजन
% 
% This program is free software: you can redistribute it
% and/or modify it under the terms of the GNU General Public
% License as published by the Free Software Foundation,
% either version 3 of the License, or (at your option) any
% later version.
% 
% This program is distributed in the hope that it will be
% useful, but WITHOUT ANY WARRANTY; without even the implied
% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
% PURPOSE. See the GNU General Public License for more
% details.
% 
% You should have received a copy of the GNU General Public
% License along with this program. If not, see
% <https://www.gnu.org/licenses/>.
% ----------------------------------------------------------
% 
% \fi
% \iffalse
%<*driver>
\documentclass{l3doc}
\usepackage{linguistix}
\usepackage{xcolor}
\usepackage{fontawesome5}
\usepackage[verbose=silent]{microtype}
\usepackage{cleveref}
\usepackage{hyperxmp}

\makeatletter
\def\@xobeysp{\hphantom{X}}%
\makeatother

\linguistix{old style one}
\loadlanguages{british,malayalam,marathi}

\setquotestyle{british}

\colorlet{lngxredcolor}{red!50!black}
\colorlet{lngxgreencolor}{green!50!black}
\colorlet{lngxbluecolor}{blue!50!black}

\urlstyle{tt}

\hypersetup{%
  unicode,%
  colorlinks,%
  linkcolor              = {lngxredcolor},
  citecolor              = {lngxgreencolor},
  urlcolor               = {lngxbluecolor},
  pdftitle               = {linguistix-malayalam},%
  pdfauthor              = {निरंजन},%
  pdfsubject             = {%
    മലയാളം (Malayalam) with LinguisTiX.%
  },%
  pdfcreator             = {निरंजन},%
  pdfkeywords            = {Linguistics, LaTeX},%
  pdfcopyright           = {%
    The LaTeX bundle linguistix\textLF
    Copyright © 2026 निरंजन\textLF
    This program is free software: you can redistribute it
    and/or modify it under the terms of the GNU General
    Public License as published by the Free Software
    Foundation, either version 3 of the License, or (at your
    option) any later version.%
  },%
  pdflicenseurl          = {gnu.org/licenses/gpl-3.0.txt}%
}%

\colorlet{lngxpurplecolor}{blue!50!red}

\def\lngxtval{%
  \begingroup
  \color{lngxredcolor}
  true%
  \endgroup
  \,|\,false%
}

\def\lngxfval{%
  true%
  \begingroup
  \color{lngxredcolor}
  \,|\,false%
  \endgroup
}

\ExplSyntaxOn
\msg_redirect_name:nnn { l3doc }
                       { foreign-internal }
                       { none }
\ExplSyntaxOff

\makeatletter
\NewDocumentCommand \supportfootnote { +m } {%
  \begingroup
  \def\thefootnote{}%
  \let\@makefntext\relax
  \def\Hy@Warning##1{}%
  \footnotetext{%
    \hspace*{-\parindent}%
    #1%
  }%
  \endgroup
}
\makeatother

\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
% \title{മലയാളം\,--\,\lngxmalayalamlogo}
% \author{निरंजन}
% \date{^^A
%   \today\ (v0.9a)\\[1ex]^^A
%   {^^A
%     \small\faIcon{home}\quad
%     \url{^^A
%       https://ctan.org/pkg/linguistix^^A
%     }\\[0.5ex]^^A
%     \small\faIcon{git-alt}\quad
%     \url{^^A
%       https://puszcza.gnu.org.ua/projects/linguistix^^A
%     }\\[0.5ex]^^A
%     \small\faIcon[regular]{comments}\quad
%     \url{^^A
%       https://matrix.to/\#/\#linguistix:matrix.org^^A
%     }^^A
%   }^^A
% }
%
% \maketitle
%
% \supportfootnote{^^A
%   The \lngxpkg\ bundle
%
%   \noindent
%   Copyright © 2025, 2026 निरंजन
%
%   This program is free software: you can redistribute it
%   and/or modify it under the terms of the \textsc{gnu}
%   General Public License as published by the Free Software
%   Foundation, either version 3 of the License, or (at your
%   option) any later version.
%
%   This program is distributed in the hope that it will be
%   useful, but \emph{\textbf{without any warranty}};
%   without even the implied warranty of
%   \emph{\textbf{merchantability}} or \emph{\textbf{fitness
%   for a particular purpose}}. See the \textsc{gnu} General
%   Public License for more details.
%
%   You should have received a copy of the \textsc{gnu}
%   General Public License along with this program. If not,
%   see \url{https://www.gnu.org/licenses/}.^^A
% }
%
% \begin{implementation}
% \noindent I initialise the package with basic information.
%    \begin{macrocode}
%<*malayalam>
\ProvidesExplPackage{linguistix-malayalam}
                    {2026-04-24}
                    {v0.9a}
                    {മലയാളം (Malayalam)}
%    \end{macrocode}
% Average height of Malayalam conjuncts could be more than
% that of Latin letters with descenders. This requires minor
% adjustments in the baseline stretch. To do it uniformly
% for footnotes also, I use the \pkg{setspace} package (if
% not loaded already).
%    \begin{macrocode}

\IfPackageLoadedF { setspace } {
  \RequirePackage { setspace }
}
%    \end{macrocode}
% We need to set the spacing between lines for which I use
% an internal floating point number. It is declared here.
%    \begin{macrocode}

\fp_gzero_new:N \g_മലയാളം_വരികൾക്കിടയിലെ_അകലം_fp
%    \end{macrocode}
% This macro grabs an argument, (g-)sets it as the value of
% \cs{g_മലയാളം_വരികൾക്കിടയിലെ_അകലം_fp} and resets the
% baseline stretch with \cs{setstretch} command. If the
% current class is \cls{memoir}, then there is a method to
% do that without any package. I use that.
%    \begin{macrocode}

\cs_new_protected:Npn \മലയാളം_വരികൾക്കിടയിലെ_അകലം:n #1 {
  \fp_gset:Nn \g_മലയാളം_വരികൾക്കിടയിലെ_അകലം_fp { #1 }
  \IfClassLoadedTF { memoir } {
    \setSingleSpace {
      \fp_use:N \g_മലയാളം_വരികൾക്കിടയിലെ_അകലം_fp
    }
    \SingleSpacing
  } {
    \setstretch {
      \fp_use:N \g_മലയാളം_വരികൾക്കിടയിലെ_അകലം_fp
    }
  }
}

\cs_gset_eq:NN \വരികൾക്കിടയിലെഅകലം
               \മലയാളം_വരികൾക്കിടയിലെ_അകലം:n
%    \end{macrocode}
% Here I define two keys for adjusting the space between
% lines.
%    \begin{macrocode}

\keys_define:nn { മലയാളം } {
  വരികൾക്കിടയിലെ~ അകലം
  .code:n                = {
    \മലയാളം_വരികൾക്കിടയിലെ_അകലം:n { #1 }
  },
  വരികൾക്കിടയിലെ~ അകലം
  .initial:n             = { 1.2 }
}
%    \end{macrocode}
% The \pkg{babel} package defines |\extrasxxxx| commands for
% languages where the additional code that should go with a
% particular language (|xxxx| in this case) is set. The
% |\addto| command is used for appending to the same. I
% start with defining additional macros for Malayalam.
%    \begin{macrocode}

\addto { \extrasmalayalam } {
%    \end{macrocode}
% I have added the command for using the socket for native
% numbering. It will pickup its value from the current
% assignment of the plugs and produce the results
% accordingly.
%    \begin{macrocode}
  \socket_use:n { lngx / native-numbering }
%    \end{macrocode}
% By default, \LaTeX\ prints roman numeral (in capital) as
% part numbers. They are not suitable for Malayalam. Thus we
% change them to arabic instead.
%    \begin{macrocode}
  \cs_set:Npn \thepart { \lngx_counter:n { part } }
%    \end{macrocode}
% The default \LaTeX\ produces Latin numerals, roman (small
% and capital both) alphabets and a-z alphabets with
% enumerate at different levels of nesting. In Malayalam,
% both are irrelevant. Thus I renew all of the concerned
% commands and change them to print Malayalam numbers. Since
% Malayalam doesn’t have much variety available for
% counters, I have chosen a simpler style, i.e. 1, 1.1,
% 1.1.1 and 1.1.1.1.
%    \begin{macrocode}
  \cs_set:Npn \theenumi { \lngx_counter:n { enumi } }
  \cs_set:Npn \theenumii {
    \lngx_counter:n { enumi } .
    \lngx_counter:n { enumii }
  }
%    \end{macrocode}
% A period is added after each ‘label’ in enumerate. In
% default \LaTeX, the label for second level enumeration is
% printed inside brackets. We don’t need it in Malayalam. So
% I change the \cs{labelenumii} command and add a period.
% It is not added for the first level because that’s
% \LaTeX-default too. Similarly, this is extended to all the
% other levels.
%    \begin{macrocode}
  \cs_set:Npn \labelenumii { \theenumii . }
  \cs_set:Npn \theenumiii {
    \lngx_counter:n { enumi } .
    \lngx_counter:n { enumii } .
    \lngx_counter:n { enumiii }
  }
  \cs_set:Npn \labelenumiii { \theenumiii . }
  \cs_set:Npn \theenumiv {
    \lngx_counter:n { enumi } .
    \lngx_counter:n { enumii } .
    \lngx_counter:n { enumiii } .
    \lngx_counter:n { enumiv }
  }
  \cs_set:Npn \labelenumiv { \theenumiv . }
%    \end{macrocode}
% The \pkg{expex} package has an independent mechanism of
% defining and using counters. I define a set called
% |മലയാളം| here and make it the default when Malayalam is
% used. Note that this change will go with the
% \cs{lngx_misc_reset:} command. Refer to |linguistix.pdf|
% for more information on this.
%    \begin{macrocode}
  \IfPackageLoadedT { expex } {
    \definelabeltype { മലയാളം } {
      labelgen                 = { list },
      labellist                = {
        ക, ഖ, ഗ, ഘ, ങ,
        ച, ഛ, ജ, ഝ, ഞ,
        ട, ഠ, ഡ, ഢ, ണ,
        ത, ഥ, ദ, ധ, ന,
        പ, ഫ, ബ, ഭ, മ,
        യ, ര, റ, ല, ള, ഴ, വ, ശ, ഷ, സ, ഹ
      },
      labelformat              = {A.},
      fullrefformat            = {XA},
      labelalign               = {left},
      labelwidth               = {1.5em}
    }
    \lingset {
      labeltype                = { മലയാളം }
    }
  }
%    \end{macrocode}
% Malayalam doesn’t distinguish between Italic and Upright.
% So I redefine \cs{emph} to produce the argument in bold
% instead. This also goes when \cs{lngx_misc_reset:} is
% used.
%    \begin{macrocode}
  \cs_gset_eq:NN \emph \textbf
}
%    \end{macrocode}
%    \begin{macrocode}

\tl_if_eq:NnTF \g_lngx_main_language_tl { malayalam } {
%    \end{macrocode}
% The |\arraystretch| command needs to be reset to a larger
% value so that it can incorporate Malayalam’s vertical
% conjuncts.
%    \begin{macrocode}
  \cs_set:Npn \arraystretch { 1.2 }
%    \end{macrocode}
% The Rachana Institute of Typography has created a variety
% of fonts that have full glyph set of Malayalam. Rachana
% and MeeraNew have the most robust feature-set and hence
% they are set in main and sans families respectively. In
% the context of mono, despite being a little less
% feature-rich, tnjoy looks like a better fit. So I have
% used it.
%    \begin{macrocode}
  \lngx_set_keys:n {
    text~ main~ font         = { RIT-Rachana-Regular.ttf },
    text~ sans~ font         = { RIT-MeeraNew.ttf },
    text~ mono~ font         = { RIT-tnjoy-regular.ttf }
  }
%    \end{macrocode}
% RIT’s fonts have Italic shapes, but since Indian scripts
% do not have the tradition of Italic shapes, their regular
% versions are used where Italic would be expected. MeeraNew
% doesn’t provide a bold font, so a slight boldness is
% faked.
%    \begin{macrocode}
  \clist_map_inline:nn {
    upright,
    italic,
    slanted,
    swash,
    small~ caps
  } {
    \lngx_set_keys:n {
      text~ #1               = { RIT-Rachana-Regular.ttf },
      text~ bold~ #1         = { RIT-Rachana-Bold.ttf },
      text~ sans~ #1         = { RIT-MeeraNew.ttf },
      text~ sans~ bold~ #1   = { RIT-MeeraNew.ttf },
      text~ sans~ bold~ #1~
      features               = {
        FakeBold               = { 1.2 }
      },
      text~ mono~ #1         = { RIT-tnjoy-regular.ttf },
      text~ mono~ bold~ #1   = { RIT-tnjoy-bold.ttf }
    }
  }
%    \end{macrocode}
% Using the |text extra features| key, I declare
% \textsc{nfss} families called |മലയാളം_main|,
% |മലയാളം_sans|, |മലയാളം_mono| that will be used
% later. Also, I use the key for interword-spacing here.
%    \begin{macrocode}
  \lngx_set_keys:n {
    text~ main~ extra~
    features                 = {
      NFSSFamily               = { മലയാളം_main },
      Scale                    = { 1.3 }
    },
    text~ sans~ extra~
    features                 = {
      NFSSFamily               = { മലയാളം_sans },
      Scale                    = { 1.3 }
    },
    text~ mono~ extra~
    features                 = {
      NFSSFamily               = { മലയാളം_mono },
      Scale                    = { 1.3 }
    }
  }
} {
%    \end{macrocode}
% In the false branch (i.e., if Malayalam is not the main
% language), I use the macros that set the ‘other’
% (non-main) fonts for Malayalam. The fonts are the same, but
% the key-value interface is not available, so the code is
% slightly verbose. Before that we load the |onchar| option
% with |ids| and |fonts| options.
%    \begin{macrocode}
  \babelprovide [
    onchar                   = { ids~ fonts }
  ] { malayalam }
  \lngx_other_main_font:nnn { malayalam } {
    NFSSFamily               = { മലയാളം_main },
    Scale                    = { 1.3 },
    UprightFont              = { RIT-Rachana-Regular.ttf },
    ItalicFont               = { RIT-Rachana-Regular.ttf },
    BoldFont                 = { RIT-Rachana-Bold.ttf    },
    BoldItalicFont           = { RIT-Rachana-Bold.ttf    },
    SlantedFont              = { RIT-Rachana-Regular.ttf },
    BoldSlantedFont          = { RIT-Rachana-Bold.ttf    },
    SwashFont                = { RIT-Rachana-Regular.ttf },
    BoldSwashFont            = { RIT-Rachana-Bold.ttf    },
    SmallCapsFont            = { RIT-Rachana-Regular.ttf }
  } { RIT-Rachana-Regular.ttf }
  \lngx_other_sans_font:nnn { malayalam } {
    NFSSFamily               = { മലയാളം_sans },
    Scale                    = { 1.3 },
    UprightFont              = { RIT-MeeraNew.ttf },
    ItalicFont               = { RIT-MeeraNew.ttf },
    BoldFont                 = { RIT-MeeraNew.ttf },
    BoldFeatures             = { FakeBold = { 1.2 } },
    BoldItalicFont           = { RIT-MeeraNew.ttf },
    BoldItalicFeatures       = { FakeBold = { 1.2 } },
    SlantedFont              = { RIT-MeeraNew.ttf },
    BoldSlantedFeatures      = { FakeBold = { 1.2 } },
    BoldSlantedFont          = { RIT-MeeraNew.ttf },
    SwashFont                = { RIT-MeeraNew.ttf },
    BoldSwashFont            = { RIT-MeeraNew.ttf },
    BoldSwashFeatures        = { FakeBold = { 1.2 } },
    SmallCapsFont            = { RIT-MeeraNew.ttf }
  } { RIT-Rachana-Regular.ttf }
  \lngx_other_mono_font:nnn { malayalam } {
    NFSSFamily               = { മലയാളം_mono },
    Scale                    = { 1.3 },
    UprightFont              = { RIT-tnjoy-regular.ttf },
    ItalicFont               = { RIT-tnjoy-regular.ttf },
    BoldFont                 = { RIT-tnjoy-bold.ttf },
    BoldItalicFont           = { RIT-tnjoy-bold.ttf },
    SlantedFont              = { RIT-tnjoy-regular.ttf },
    BoldSlantedFont          = { RIT-tnjoy-bold.ttf },
    SwashFont                = { RIT-tnjoy-regular.ttf },
    BoldSwashFont            = { RIT-tnjoy-bold.ttf },
    SmallCapsFont            = { RIT-tnjoy-regular.ttf }
  } { RIT-Rachana-Regular.ttf }
}
%    \end{macrocode}
% In order to allow Malayalam in math mode, I use the
% following code. Since this setting has to be done after
% the initialisation, I use a hook for lazy loading.
%    \begin{macrocode}

\IfPackageLoadedT { lua-unicode-math } {
  \DeclareSymbolFont { മലയാളം }
                     { TU }
                     { മലയാളം_main }
                     { m }
                     { n }
}

\hook_gput_code:nnn { begindocument / end } { . } {
  \IfPackageLoadedF { lua-unicode-math } {
    \DeclareSymbolFont { മലയാളം }
                       { TU }
                       { മലയാളം_main }
                       { m }
                       { n }
  }
  \int_step_inline:nnn { "0D00 } { "0D7F } {
    \Umathcode #1 = "0 ~ \use:c { symമലയാളം } ~ #1
  }
}
%</malayalam>
%    \end{macrocode}
% \end{implementation}
% \Finale
