%% This file is xepersian-mathsdigitspec.sty
%%
%% Copyright 2009 by Vafa Khalighi
%%
%%%% It may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{xepersian-mathsdigitspec}
  [2009/01/05 v0.2 Unicode Persian maths digits in XeLaTeX (Author: Vafa Khalighi)]
\@zf@mathfalse
\newif\if@um@fontspec@feature
\def\um@Loop#1\um@Pool{#1\um@Loop#1\um@Pool}
\def\um@Break#1\um@Pool{}
\long\def\um@FOR#1=[#2:#3]\do#4{%
   #1=#2\relax
   \um@Loop #4%
     \advance#1\@ne
     \ifnum#1>#3\relax
       \expandafter\um@Break
     \fi
   \um@Pool}
\newcommand\um@PackageInfo[1]{\PackageInfo{xepersian-mathsdigitspec}{#1}}
\def\um@usv@num{`\0}
\newcommand\SetMathCode[4]{%
  \XeTeXmathcode#1="\mathchar@type#2 \csname sym#3\endcsname #4\relax}
\newcommand\SetMathCharDef[4]{%
  \XeTeXmathchardef#1="\mathchar@type#2 \csname sym#3\endcsname #4\relax}
\newcommand\setdigitfont[2][]{%
  \csname S@\f@size\endcsname
  \def\um@ScriptFeatures{ScriptStyle}%
  \def\um@ScriptScriptFeatures{ScriptScriptStyle}%
  \def\um@ScriptFont{#2}%
  \def\um@ScriptScriptFont{#2}%
  \setkeys*[um]{options}{#1}%
  \edef\@tempa{\noexpand\zf@fontspec{%
   Mapping=parsidigits,SizeFeatures={%
      {Size=\tf@size-},%
      {Size=\sf@size-\tf@size,%
        Font=\um@ScriptFont,%
        \um@ScriptFeatures},%
      {Size=-\sf@size,%
        Font=\um@ScriptScriptFont,%
        \um@ScriptScriptFeatures}},%
    \XKV@rm}{#2}}\@tempa
    \def\um@digitfont{um@digitfont}%
    \um@PackageInfo{Defining the default Persian maths digits font as '#2'}%
    \DeclareSymbolFont{momayez}{EU1}{\zf@family}{m}{n}%
    \SetMathCharDef{\momayez}{\mathpunct}{momayez}{"066B}%
\DeclareSymbolFont{\um@digitfont}
  {\encodingdefault}{\zf@family}{\mddefault}{\updefault}%
\um@def@numbers
}
\newcommand\um@zf@feature[2]{%
  \define@key[zf]{options}{#1}[]{%
    \if@um@fontspec@feature
      #2
    \fi}}
\um@zf@feature{ScriptStyle}{%
  \zf@update@ff{+ssty=0}}
\um@zf@feature{ScriptScriptStyle}{%
  \zf@update@ff{+ssty=1}}
\newcommand\um@setmathcode[3][1]{%
  \@for\um@inp:=#2\do{%
    \um@FOR\@tempcnta=[1:#1]\do{%
      \SetMathCode{\numexpr\um@inp+\@tempcnta-1\relax}
        {\mathalpha}{\um@digitfont}{\numexpr#3+\@tempcnta-1\relax}}}}
\newcommand\um@def@numbers{%
  \um@setmathcode[10]{\um@usv@num}{\um@usv@num}}
\ifx\newcommand\undefined\else
  \newcommand{\ZifferAn}{}
\fi
\mathchardef\ziffer@DotOri="013A
{\ZifferAn
 \catcode`\.=\active\gdef.{\begingroup\obeyspaces\futurelet\n\ziffer@dcheck}}
\def\ziffer@dcheck{\ziffer@check\ZifferLeer\ziffer@DotOri}
\def\ziffer@check#1#2{%
  \ifx\n1\endgroup#1\else
    \ifx\n2\endgroup#1\else
      \ifx\n3\endgroup#1\else
        \ifx\n4\endgroup#1\else
          \ifx\n5\endgroup#1\else
            \ifx\n6\endgroup#1\else
              \ifx\n7\endgroup#1\else
                \ifx\n8\endgroup#1\else
                  \ifx\n9\endgroup#1\else
                    \ifx\n0\endgroup#1\else
                      \endgroup#2%
                    \fi
                  \fi
                \fi
              \fi
            \fi
          \fi
        \fi
      \fi
    \fi
  \fi}
\mathcode`.="8000\relax
\def\ZifferLeer{\ifx\momayez\undefied .\else \momayez\fi}
\endinput
