%%
%% This is file `unicode-persianmath.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% xepersian.dtx  (with options: `table,unicode-persianmath.sty')
%% 
%%   __________________________________________________
%%   Vafa Khalighi
%% 
%%   Copyright (c) 2008--2025  Vafa Khalighi
%%   Copyright (c) 2018--2020 bidi-tex GitHub Organization
%% 
%%   It may be distributed and/or modified under the LaTeX Project Public License,
%%   version 1.3c or higher (your choice). The latest version of
%%   this license is at: http://www.latex-project.org/lppl.txt
%% 
%%   This work is “author-maintained” (as per LPPL maintenance status)
%%   by Vafa Khalighi.
%% 
%% 
%% \CheckSum{10792}
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{unicode-persianmath}
  [2025/02/02 v1.2.3 Unicode Persian math]
\def\new@mathgroup{\alloc@8\mathgroup\chardef\@cclvi}
\let\newfam\new@mathgroup
\def\select@group#1#2#3#4{%
 \ifx\math@bgroup\bgroup\else\relax\expandafter\@firstofone\fi
 {%
 \ifmmode
  \ifnum\csname c@mv@\math@version\endcsname<\@cclvi
     \begingroup
       \escapechar\m@ne
       \getanddefine@fonts{\csname c@mv@\math@version\endcsname}#3%
       \globaldefs\@ne  \math@fonts
     \endgroup
     \init@restore@version
     \xdef#1{\noexpand\use@mathgroup\noexpand#2%
             {\number\csname c@mv@\math@version\endcsname}}%
     \global\advance\csname c@mv@\math@version\endcsname\@ne
   \else
     \let#1\relax
     \@latex@error{Too many math alphabets used in
                   version \math@version}%
        \@eha
   \fi
 \else \expandafter\non@alpherr\fi
 #1{#4}%
 }%
}
\def\document@select@group#1#2#3#4{%
 \ifx\math@bgroup\bgroup\else\relax\expandafter\@firstofone\fi
 {%
 \ifmmode
   \ifnum\csname c@mv@\math@version\endcsname<\@cclvi
     \begingroup
       \escapechar\m@ne
       \getanddefine@fonts{\csname c@mv@\math@version\endcsname}#3%
       \globaldefs\@ne  \math@fonts
     \endgroup
     \expandafter\extract@alph@from@version
         \csname mv@\math@version\expandafter\endcsname
         \expandafter{\number\csname
                       c@mv@\math@version\endcsname}%
          #1%
     \global\advance\csname c@mv@\math@version\endcsname\@ne
   \else
     \let#1\relax
     \@latex@error{Too many math alphabets used
                   in version \math@version}%
        \@eha
  \fi
 \else \expandafter\non@alpherr\fi
 #1{#4}%
 }%
}
\ExplSyntaxOn
\bool_set_false:N \g__fontspec_math_bool
\tl_map_inline:nn
 {
  \new@mathgroup\cdp@list\cdp@elt\DeclareMathSizes
  \@DeclareMathSizes\newmathalphabet\newmathalphabet@@\newmathalphabet@@@
  \DeclareMathVersion\define@mathalphabet\define@mathgroup\addtoversion
  \version@list\version@elt\alpha@list\alpha@elt
  \restore@mathversion\init@restore@version\dorestore@version\process@table
  \new@mathversion\DeclareSymbolFont\group@list\group@elt
  \new@symbolfont\SetSymbolFont\SetSymbolFont@\get@cdp
  \DeclareMathAlphabet\new@mathalphabet\SetMathAlphabet\SetMathAlphabet@
  \DeclareMathAccent\set@mathaccent\DeclareMathSymbol\set@mathchar
  \set@mathsymbol\DeclareMathDelimiter\@xxDeclareMathDelimiter
  \@DeclareMathDelimiter\@xDeclareMathDelimiter\set@mathdelimiter
  \set@@mathdelimiter\DeclareMathRadical\mathchar@type
  \DeclareSymbolFontAlphabet\DeclareSymbolFontAlphabet@
 }
 {
  \tl_remove_once:Nn \@preamblecmds {\do#1}
 }
\ExplSyntaxOff
\newcommand\xepersian@PackageInfo[1]{\PackageInfo{unicode-persianmath}{#1}}
\newcommand\SetMathCode[4]{%
  \Umathcode#1="\mathchar@type#2 \csname sym#3\endcsname #4\relax}
\newcommand\SetMathCharDef[4]{%
  \Umathchardef#1="\mathchar@type#2 \csname sym#3\endcsname #4\relax}
\ExplSyntaxOn
\cs_new_eq:NN \orig_mathbf:n \mathbf
\cs_new_eq:NN \orig_mathit:n \mathit
\cs_new_eq:NN \orig_mathrm:n \mathrm
\cs_new_eq:NN \orig_mathsf:n \mathsf
\cs_new_eq:NN \orig_mathtt:n \mathtt
\NewDocumentCommand \new@mathbf { m } {
 \orig_mathbf:n {
   \SetPersianMathDigitsCode{\mathord}{persianoperatorsbf}
   #1
 }
}
\NewDocumentCommand \new@mathit { m } {
 \orig_mathit:n {
   \SetPersianMathDigitsCode{\mathord}{persianoperatorsit}
   #1
 }
}
\NewDocumentCommand \new@mathrm { m } {
 \orig_mathrm:n {
   \SetPersianMathDigitsCode{\mathord}{persianoperators}
   #1
 }
}
\NewDocumentCommand \new@mathsf{ m } {
 \orig_mathsf:n {
   \SetPersianMathDigitsCode{\mathord}{persianoperatorssf}
   #1
 }
}
\NewDocumentCommand \new@mathtt{ m } {
 \orig_mathtt:n {
   \SetPersianMathDigitsCode{\mathord}{persianoperatorstt}
   #1
 }
}

\cs_new:Npn \__xepersian_mathdigitspec_error:nxx    { \msg_error:nnxx    {unicode-persianmath} }
\cs_new:Nn \__xepersian_mathdigitspec_msg_new:nnnn
  { \msg_new:nnxx {#1} {#2} { \tl_trim_spaces:n {#3} } { \tl_trim_spaces:n {#4} } }
\char_set_catcode_space:n {32}
\__xepersian_mathdigitspec_msg_new:nnnn {unicode-persianmath} {char-not-exist}
 {
  The font "#1" does not contain U+#2.
 }
 {
  Select another font and rerun xelatex.
 }
\char_set_catcode_ignore:n {32}

\prg_new_conditional:Nnn \__xepersian_mathdigitspec_primitive_font_char_if_exist:n {p,TF,T,F}
  {
    \tex_iffontchar:D \l_fontspec_font "#1 \scan_stop:
      \prg_return_true:
    \else:
      \prg_return_false:
    \fi:
  }

\clist_new:N \l_xepersian_mathdigitspec_char_clist

\cs_new:Nn \__xepersian_mathdigitspec_char_set_not_exist_error:nn
 {
    \clist_set:Nn \l_xepersian_mathdigitspec_char_clist { #1 }
    \clist_map_inline:Nn \l_xepersian_mathdigitspec_char_clist {
      \__xepersian_mathdigitspec_primitive_font_char_if_exist:nF { ##1 }{ \__xepersian_mathdigitspec_error:nxx {char-not-exist} { #2 } { ##1 } }
    }
 }

\cs_new:Nn \__xepersian_mathdigitspec_char_prepend_not_exist_error:nn
 {
    \clist_put_left:Nn \l_xepersian_mathdigitspec_char_clist { #1 }
    \clist_map_inline:Nn \l_xepersian_mathdigitspec_char_clist {
      \__xepersian_mathdigitspec_primitive_font_char_if_exist:nF { ##1 }{ \__xepersian_mathdigitspec_error:nxx {char-not-exist} { #2 } { ##1 } }
    }
 }

\cs_new:Nn \__xepersian_mathdigitspec_char_append_not_exist_error:nn
 {
    \clist_put_right:Nn \l_xepersian_mathdigitspec_char_clist { #1 }
    \clist_map_inline:Nn \l_xepersian_mathdigitspec_char_clist {
      \__xepersian_mathdigitspec_primitive_font_char_if_exist:nF { ##1 }{ \__xepersian_mathdigitspec_error:nxx {char-not-exist} { #2 } { ##1 } }
    }
 }

\cs_new:Nn \__xepersian_mathdigitspec_char_not_exist_error:n
 {
    \__xepersian_mathdigitspec_char_set_not_exist_error:nn {
      06F0 , 06F1 , 06F2 , 06F3 , 06F4 , 06F5 , 06F6 , 06F7 , 06F8 ,
      06F9 , 066A , 066B
    } { #1 }
 }

\NewDocumentCommand \SetPersianMathDigitsCode { m m }
  {
    \SetMathCode{`0}{#1}{#2}{`۰}
    \SetMathCode{`1}{#1}{#2}{`۱}
    \SetMathCode{`2}{#1}{#2}{`۲}
    \SetMathCode{`3}{#1}{#2}{`۳}
    \SetMathCode{`4}{#1}{#2}{`۴}
    \SetMathCode{`5}{#1}{#2}{`۵}
    \SetMathCode{`6}{#1}{#2}{`۶}
    \SetMathCode{`7}{#1}{#2}{`۷}
    \SetMathCode{`8}{#1}{#2}{`۸}
    \SetMathCode{`9}{#1}{#2}{`۹}
  }

\DeclareDocumentCommand \setmathdigitfont { O{} m O{} }
  {
    \__xepersian_main_setmathdigitfont:nn {#1,#3} {#2}
  }
\cs_new:Nn \__xepersian_main_setmathdigitfont:nn
 {
  \let\glb@currsize\relax
  \fontspec_set_family:Nnn \g__xepersian_mathdigitsfamily_tl {#1} {#2}
  \__xepersian_setmathdigitfont_hook:nn {#1} {#2}
    \xepersian@PackageInfo{Defining the default Persian math digits font as '#2'}
  \DeclareSymbolFont{persianoperators}   {\g_fontspec_encoding_tl}{\g__xepersian_mathdigitsfamily_tl} {m}{n}
  \DeclareSymbolFont{persianoperatorsbf}{\g_fontspec_encoding_tl}{\g__xepersian_mathdigitsfamily_tl}{bx}{n}
  \DeclareSymbolFont{persianoperatorsit}{\g_fontspec_encoding_tl}{\g__xepersian_mathdigitsfamily_tl}{m}{it}
  \__xepersian_mathdigitspec_char_not_exist_error:n { #2 }
  \def\persianmathdigits{%
    \SetPersianMathDigitsCode{\mathalpha}{persianoperators}
    \SetMathCode{`\%}{\mathord}{persianoperators}{`٪}
    \SetMathCharDef{\persiandecimalseparator}{\mathord}{persianoperators}{`٫}
    \tl_set_eq:NN \mathbf \new@mathbf
    \tl_set_eq:NN \mathit \new@mathit
    \tl_set_eq:NN \mathrm \new@mathrm
  }
 }

\cs_set_eq:NN \__xepersian_setmathdigitfont_hook:nn     \use_none:nn

\DeclareDocumentCommand \setmathsfdigitfont { O{} m O{} }
  {
    \__xepersian_main_setmathsfdigitfont:nn {#1,#3} {#2}
  }
\cs_new:Nn \__xepersian_main_setmathsfdigitfont:nn
 {
  \fontspec_set_family:Nnn \g__xepersian_mathsfdigitfamily_tl {#1} {#2}
  \__xepersian_setmathsfdigitfont_hook:nn {#1} {#2}
      \DeclareSymbolFont{persianoperatorssf}{\g_fontspec_encoding_tl}{\g__xepersian_mathsfdigitfamily_tl}{m}{n}
      \__xepersian_mathdigitspec_char_not_exist_error:n { #2 }
    \def\persianmathsfdigits{\tl_set_eq:NN \mathsf \new@mathsf}
 }

\cs_set_eq:NN \__xepersian_setmathsfdigitfont_hook:nn     \use_none:nn

\DeclareDocumentCommand \setmathttdigitfont { O{} m O{} }
  {
    \__xepersian_main_setmathttdigitfont:nn {#1,#3} {#2}
  }
\cs_new:Nn \__xepersian_main_setmathttdigitfont:nn
 {
  \fontspec_set_family:Nnn \g__xepersian_mathttdigitfamily_tl {#1} {#2}
  \__xepersian_setmathttdigitfont_hook:nn {#1} {#2}
      \DeclareSymbolFont{persianoperatorstt}{\g_fontspec_encoding_tl}{\g__xepersian_mathttdigitfamily_tl}{m}{n}
      \__xepersian_mathdigitspec_char_not_exist_error:n { #2 }
    \def\persianmathttdigits{\tl_set_eq:NN \mathtt \new@mathtt}
 }

\cs_set_eq:NN \__xepersian_setmathttdigitfont_hook:nn     \use_none:nn

\ExplSyntaxOff
\mathchardef\decimalseparator@point="013A
\bgroup
  \uccode`\~`\.%
  \uppercase{%
\egroup
  \def~}{\begingroup\obeyspaces\futurelet\@let@decimalsep@token\decimalseparator@pointcheck}
\def\decimalseparator@pointcheck{\decimalseparator@check\MathDecimalSeparator\decimalseparator@point}
\def\decimalseparator@check#1#2{%
  \ifx\@let@decimalsep@token1\endgroup#1\else
    \ifx\@let@decimalsep@token2\endgroup#1\else
      \ifx\@let@decimalsep@token3\endgroup#1\else
        \ifx\@let@decimalsep@token4\endgroup#1\else
          \ifx\@let@decimalsep@token5\endgroup#1\else
            \ifx\@let@decimalsep@token6\endgroup#1\else
              \ifx\@let@decimalsep@token7\endgroup#1\else
                \ifx\@let@decimalsep@token8\endgroup#1\else
                  \ifx\@let@decimalsep@token9\endgroup#1\else
                    \ifx\@let@decimalsep@token0\endgroup#1\else
                     \ifx\@let@decimalsep@token\dot\endgroup#1\else
                       \ifx\@let@decimalsep@token\overline\endgroup#1\else
                         \endgroup#2%
                       \fi
                    \fi
                  \fi
                \fi
              \fi
            \fi
          \fi
        \fi
      \fi
    \fi
   \fi
 \fi}
\AtBeginDocument{\mathcode`.="8000\relax}
\def\MathDecimalSeparator{\ifx\persiandecimalseparator\undefined .\else \persiandecimalseparator\fi}
\def\DefaultMathDecimalSeparator{.}
\def\DefaultInlineMathDigits{\def\SetInlineMathDigits{}}
\def\DefaultDisplayMathDigits{\def\SetDisplayMathDigits{}}
\def\DefaultMathDigits{%
  \DefaultInlineMathDigits%
  \DefaultDisplayMathDigits%
}
\def\PersianInlineMathDigits{\def\SetInlineMathDigits{%
\ifx\persianmathdigits\undefined\else\persianmathdigits\fi%
\ifx\persianmathsfdigits\undefined\else\persianmathsfdigits\fi%
\ifx\persianmathttdigits\undefined\else\persianmathttdigits\fi}}
\def\PersianDisplayMathDigits{\def\SetDisplayMathDigits{%
\ifx\persianmathdigits\undefined\else\persianmathdigits\fi%
\ifx\persianmathsfdigits\undefined\else\persianmathsfdigits\fi%
\ifx\persianmathttdigits\undefined\else\persianmathttdigits\fi}}
\def\PersianMathDigits{%
  \PersianInlineMathDigits%
  \PersianDisplayMathDigits%
}
\def\AutoInlineMathDigits{\def\SetInlineMathDigits{%
\ifx\persianmathdigits\undefined\else\if@nonlatin\persianmathdigits\fi\fi%
\ifx\persianmathsfdigits\undefined\else\if@nonlatin\persianmathsfdigits\fi\fi%
\ifx\persianmathttdigits\undefined\else\if@nonlatin\persianmathttdigits\fi\fi}}
\def\AutoDisplayMathDigits{\def\SetDisplayMathDigits{%
\ifx\persianmathdigits\undefined\else\if@nonlatin\persianmathdigits\fi\fi%
\ifx\persianmathsfdigits\undefined\else\if@nonlatin\persianmathsfdigits\fi\fi%
\ifx\persianmathttdigits\undefined\else\if@nonlatin\persianmathttdigits\fi\fi}}
\def\AutoMathDigits{%
  \AutoInlineMathDigits%
  \AutoDisplayMathDigits%
}
\AutoMathDigits
\everymath\expandafter{\the\everymath\SetInlineMathDigits%
  \let\SetDisplayMathDigits\relax%
}
\everydisplay\expandafter{%
  \the\everydisplay\SetDisplayMathDigits%
  \let\SetInlineMathDigits\relax%
}

\ExplSyntaxOn
\cs_new_eq:NN \orig_MathDecimalSeparator \MathDecimalSeparator

\NewDocumentCommand \SwitchToDefaultMathDigits {  } {
  \int_step_inline:nnnn { `0 } { \c_one_int } { `9 } {
    \SetMathCode{##1}{\mathalpha}{operators}{##1}
  }
  \SetMathCode{`\%}{\mathord}{operators}{`\%}
  \tl_set_eq:NN \MathDecimalSeparator \DefaultMathDecimalSeparator
  \tl_set_eq:NN \mathbf \orig_mathbf:n
  \tl_set_eq:NN \mathit \orig_mathit:n
  \tl_set_eq:NN \mathrm \orig_mathrm:n
  \tl_set_eq:NN \mathsf \orig_mathsf:n
  \tl_set_eq:NN \mathtt \orig_mathtt:n
}

\NewDocumentCommand \SwitchToPersianMathDigits {  } {
  \SetPersianMathDigitsCode{\mathalpha}{persianoperators}
  \SetMathCode{`\%}{\mathord}{persianoperators}{`٪}
  \tl_set_eq:NN \MathDecimalSeparator \orig_MathDecimalSeparator
  \tl_set_eq:NN \mathbf \new@mathbf
  \tl_set_eq:NN \mathit \new@mathit
  \tl_set_eq:NN \mathrm \new@mathrm
  \tl_set_eq:NN \mathsf \new@mathsf
  \tl_set_eq:NN \mathtt \new@mathtt
}
\ExplSyntaxOff
\endinput
%%
%% End of file `unicode-persianmath.sty'.
