% unipersian-mathdigits.def: Persian math digits for UniPersian.
%
% This file is part of the UniPersian package.
%
% Copyright (C) 2026 Amer Amikhteh.
%
% Distributed and modifiable under the LaTeX Project Public License,
% version 1.3c or later: https://www.latex-project.org/lppl/
% This work is provided without warranty.
%
% This work has the LPPL maintenance status "maintained".
% The current maintainer is Amer Amikhteh.


\ProvidesFile{unipersian-mathdigits.def}[2026/12/04 v1.0.0 Persian math digits]
\makeatletter
\newif\ifuper@mathdigits@font
\ifTUTeX
\ExplSyntaxOn
\NewDocumentCommand \setmathdigitfont { O{} m}
{
    \fontspec_set_family:Nnn \l_tmpa_tl {#1} {#2}
    \cs_if_exist:NTF \symuper@mathdigits
    { \SetSymbolFont{uper@mathdigits}{normal}{\g_fontspec_encoding_tl}{\l_tmpa_tl}{m}{n} }
    { \DeclareSymbolFont{uper@mathdigits}{\g_fontspec_encoding_tl}{\l_tmpa_tl}{m}{n} }
    \SetSymbolFont{uper@mathdigits}{bold}{\g_fontspec_encoding_tl}{\l_tmpa_tl}{m}{n}
    \uper@mathdigits@fonttrue
}
\cs_new_protected:Npn \uper_set_math_family:nnn #1#2#3
{
    \fontspec_set_family:Nnn \l_tmpa_tl {#2} {#3}
    \cs_if_exist:cTF {symuper@mathdigits@#1}
    {\SetSymbolFont{uper@mathdigits@#1}{normal}{\g_fontspec_encoding_tl}{\l_tmpa_tl}{m}{n}}
    {\DeclareSymbolFont{uper@mathdigits@#1}{\g_fontspec_encoding_tl}{\l_tmpa_tl}{m}{n}}
    \SetSymbolFont{uper@mathdigits@#1}{bold}{\g_fontspec_encoding_tl}{\l_tmpa_tl}{m}{n}
}
\NewDocumentCommand \setmathsfdigitfont { O{} m}
{\uper_set_math_family:nnn {sf} {#1} {#2}}
\NewDocumentCommand \setmathttdigitfont { O{} m}
{\uper_set_math_family:nnn {tt} {#1} {#2}}
\ExplSyntaxOff
\@onlypreamble\setmathdigitfont
\@onlypreamble\setmathsfdigitfont
\@onlypreamble\setmathttdigitfont

\def\uper@mathdigits@persian{%
    \ifuper@mathdigits@font
    \count@=`0\relax
    \loop
    \Umathcode\count@=7 \symuper@mathdigits
    \numexpr"06F0+\count@-`0\relax
    \advance\count@\@ne
    \ifnum\count@<`:\repeat
    \fi
}
\def\uper@mathdigits@auto{%
    \bbl@ifsamestring{\languagename}{persian}%
    {\uper@mathdigits@persian}{}%
}
\def\DefaultMathDigits{\let\uper@mathdigits@setup\relax}
\def\PersianMathDigits{\let\uper@mathdigits@setup\uper@mathdigits@persian}
\def\AutoMathDigits{\let\uper@mathdigits@setup\uper@mathdigits@auto}
\AutoMathDigits
\def\uper@mathdigits@alphabet#1{%
    \ifx\uper@mathdigits@setup\uper@mathdigits@persian
        \uper@mathdigits@alphabet@set{#1}%
    \else\ifx\uper@mathdigits@setup\uper@mathdigits@auto
            \bbl@ifsamestring{\languagename}{persian}%
            {\uper@mathdigits@alphabet@set{#1}}{}%
        \fi\fi
}
\def\uper@mathdigits@alphabet@set#1{%
    \ifcsname symuper@mathdigits@#1\endcsname
    \count@=`0\relax
    \loop
    \Umathcode\count@=0 \csname symuper@mathdigits@#1\endcsname
    \numexpr"06F0+\count@-`0\relax
    \advance\count@\@ne
    \ifnum\count@<`:\repeat
    \fi
}
\AddToHook{begindocument/end}{%
    \everymath\expandafter{\the\everymath\uper@mathdigits@setup}%
    \everydisplay\expandafter{\the\everydisplay\uper@mathdigits@setup}%
    \ifcsname symuper@mathdigits@sf\endcsname
        \let\uper@mathsf\mathsf
        \RenewDocumentCommand\mathsf{m}{%
            \begingroup\uper@mathdigits@alphabet{sf}\uper@mathsf{#1}\endgroup}%
    \fi
    \ifcsname symuper@mathdigits@tt\endcsname
        \let\uper@mathtt\mathtt
        \RenewDocumentCommand\mathtt{m}{%
            \begingroup\uper@mathdigits@alphabet{tt}\uper@mathtt{#1}\endgroup}%
    \fi
}
\else
\NewDocumentCommand\setmathdigitfont{O{} m O{}}{%
    \PackageError{unipersian}{Math digit fonts require XeTeX or LuaTeX}%
    {Use xelatex or lualatex to select a Unicode font.}%
}
\NewDocumentCommand\setmathsfdigitfont{O{} m O{}}{%
    \PackageError{unipersian}{Math digit fonts require XeTeX or LuaTeX}%
    {Use xelatex or lualatex to select a Unicode font.}%
}
\NewDocumentCommand\setmathttdigitfont{O{} m O{}}{%
    \PackageError{unipersian}{Math digit fonts require XeTeX or LuaTeX}%
    {Use xelatex or lualatex to select a Unicode font.}%
}
\def\DefaultMathDigits{}
\def\uper@mathdigits@unsupported{%
    \PackageError{unipersian}{Persian math digits require XeTeX or LuaTeX}%
    {Use xelatex or lualatex with a Persian math digit font.}%
}
\let\PersianMathDigits\uper@mathdigits@unsupported
\let\AutoMathDigits\uper@mathdigits@unsupported
\fi
\makeatother
\endinput
