%
% Copyright (C) 2008-2001 by Azzeddine LAZREK. All rights reserved.
%
% ksdprc.tex
% This file belongs to the CurExt package.
%


%
\def\amdebutkashid{
 \amnbkashid=-1
 \immediate\openout\amflkashid=kashida.txt\relax
}

%
\def\amkashida{\@ifnextchar _ {\aamkashida}{\bamkashida}}

%
\def\bamkashida#1{%
 \amsize=#1
 \advance\amsize by 1.5pt
 \amtraitkashid
 \setbox\amctbox=\hbox{${{\amopkashid\ampos}}$}
 \amwdt=\ht\amctbox
 \advance\amwdt by \dp\amctbox
 \divide\amwdt by 2
 \lower \amwdt \hbox{\box\amctbox}
}

% kashida
\def\aamkashida_#1^#2{\amkashidasub{\amrl{#1}}{\amrl{#2}}.{}{}}

%
{ \catcode`\<=12
\gdef\amkashidasub#1#2.#3#4{%
 \setbox\amclbox=\hbox{${{#1}}$}
 \setbox\amchbox=\hbox{${{#2}}$}
 \ifdim\wd\amclbox<\wd\amchbox
  \setbox\amclbox=\hbox to \wd\amchbox{\hfill\box\amclbox\hfill}
  \amwdt=\wd\amchbox
 \else
  \setbox\amchbox=\hbox to \wd\amclbox{\hfill\box\amchbox\hfill}
  \amwdt=\wd\amclbox
 \fi
 \global\amsize=\amwdt
 \global\advance\amsize by 1.5pt
 \amtraitkashid
 \setbox\amctbox=\hbox{${{\amopkashid\ampos}}$}
 \setbox\amctbox=\hbox{\hfill\box\amctbox}
 \setbox\ambox=\vbox{%
        \hbox{\box\amchbox}
        \copy\amctbox
        \hbox{\box\amclbox}}
 \amwdt=\ht\ambox
 \advance\amwdt by \dp\ambox
 \divide\amwdt by 2
 \lower \amwdt \hbox{\ \box\ambox}
} }

% sum
\def\amcsum_#1^#2{\amcsums{\amrl{#1}}{\amrl{#2}}.{}{}}

%
{ \catcode`\<=12
\gdef\amcsums#1#2.#3#4{%
 \setbox\amclbox=\hbox{${{#1}}$}
 \setbox\amchbox=\hbox{${{#2}}$}
 \ifdim\wd\amclbox<\wd\amchbox
  \setbox\amclbox=\hbox to \wd\amchbox{\hfill\box\amclbox\hfill}
  \amwdt=\wd\amchbox
 \else
  \setbox\amchbox=\hbox to \wd\amclbox{\hfill\box\amchbox\hfill}
  \amwdt=\wd\amclbox
 \fi
 \global\amsize=\amwdt
 \global\advance\amsize by 1.5pt
 \amtraitkashid
 \setbox\amctbox=\hbox{${{\amopkashid\ampos\ammgmued}}$}
 \setbox\amctbox=\hbox{\hfill\box\amctbox}
 \setbox\ambox=\vbox{%
        \hbox{\box\amchbox}
        \copy\amctbox
        \hbox{\box\amclbox}}
 \amwdt=\ht\ambox
 \advance\amwdt by \dp\ambox
 \divide\amwdt by 2
 \lower \amwdt \hbox{\ \box\ambox}
}
}

% product
\def\amcprod_#1^#2{\amcprods{\amrl{#1}}{\amrl{#2}}.{}{}}

%
{ \catcode`\<=12
\gdef\amcprods#1#2.#3#4{%
 \setbox\amclbox=\hbox{${{#1}}$}
 \setbox\amchbox=\hbox{${{#2}}$}
 \ifdim\wd\amclbox<\wd\amchbox
  \setbox\amclbox=\hbox to \wd\amchbox{\hfill\box\amclbox\hfill}
  \amwdt=\wd\amchbox
 \else
  \setbox\amchbox=\hbox to \wd\amclbox{\hfill\box\amchbox\hfill}
  \amwdt=\wd\amclbox
 \fi
 \global\amsize=\amwdt
 \global\advance\amsize by 1.5pt
 \amtraitkashid
 \setbox\amctbox=\hbox{${{\gdaafd\amopkashid\ampos\amgdaad}}$}
 \setbox\amctbox=\hbox{\hfill\box\amctbox\hfill}
  \setbox\amclbox=\hbox to \wd\amctbox{\hfill\box\amclbox\hfill}
  \setbox\amchbox=\hbox to \wd\amctbox{\hfill\box\amchbox\hfill}
 \setbox\ambox=\vbox{%
        \hbox{\box\amchbox}
        \copy\amctbox
        \hbox{\box\amclbox}}
 \amwdt=\ht\ambox
 \advance\amwdt by \dp\ambox
 \divide\amwdt by 2
 \lower \amwdt \hbox{\box\ambox}
} }

% limit
\def\amclim_#1{\amclims{\amrl{#1}}.{}{}}

%
{ \catcode`\<=12
\gdef\amclims#1.#2#3{%
 \setbox\amclbox=\hbox{${{#1}}$}
 \amwdt=\wd\amclbox
 \global\amsize=\amwdt
 \global\advance\amsize by 1.5pt
 \amtraitkashid
 \setbox\amctbox=\hbox{${{\nhaytf\amopkashid\ampos\amnhaytd}}$}
 \setbox\amctbox=\hbox{\hfill\box\amctbox\hfill}
 \setbox\amclbox=\hbox to \wd\amctbox{\hfill\box\amclbox\hfill}
 \setbox\ambox=\vbox{%
        \copy\amctbox
        \hbox{\box\amclbox}}
 \amwdt=\ht\ambox
 \advance\amwdt by \dp\ambox
 \divide\amwdt by 2
 \lower \amwdt \hbox{\box\ambox}
}
}

%
\def\amtraitkashid{
 \ifnum\amnbkashid=-1
 \global\advance\amnbkashid by 1
 \global\ampos=\amnbkashid
 \immediate\write\amflkashid{\the\amsize}
 \else
 \ifnum\amnbkashid=255
 \ambornekashid
 \ifnum\amposinf=-1
 \global\ampos=\ampossup
 \else
 \ifnum\ampossup=256
 \global\ampos=\amposinf
 \else
 \global\ampos=\ampossup
 \fi
 \fi
 \else
 \ampresentkashid
 \ifnum\ampos=-1
 \global\advance\amnbkashid by 1
 \global\ampos=\amnbkashid
 \immediate\write\amflkashid{\the\amsize}
 \fi
 \fi
 \fi
}

%
\def\ampresentkashid{
 \global\ampos=-1
 \immediate\closeout\amflkashid
 \immediate\openout\amflkashid=buffer.txt\relax
 \immediate\openin\amrf=kashida.txt\relax
 \amposf=-1
 \moretrue
 \loop
 { \endlinechar=-1
   \global
   \read\amrf to \amvalf
 }
 \ifeof\amrf \global\morefalse
 \else
 \immediate\write\amflkashid{\amvalf}
 \advance\amposf by 1
 \ifdim\amvalf=\amsize \global\ampos=\amposf \fi
 \fi \ifmore \repeat
 \immediate\closein\amrf
 \immediate\closeout\amflkashid
 \immediate\openout\amflkashid=kashida.txt\relax
 \immediate\openin\amrf=buffer.txt\relax
 \moretrue
 \loop
 { \endlinechar=-1
   \global
   \read\amrf to \amvalf
 }
  \ifeof\amrf \global\morefalse
  \else \immediate\write\amflkashid{\amvalf}
  \fi
 \ifmore \repeat
 \immediate\closein\amrf
}

%
{ \catcode`\<=12
\gdef\ambornekashid{
 \global\amposinf=-1
 \global\amvalinf=0.0pt
 \global\ampossup=256
 \global\amvalsup=2560pt
 \immediate\closeout\amflkashid
 \immediate\openout\amflkashid=buffer.txt\relax
 \immediate\openin\amrf=kashida.txt\relax
 \amposf=-1
 \moretrue
 \loop
 { \endlinechar=-1
   \global
   \read\amrf to \amvalf
 }
 \ifeof\amrf \global\morefalse
 \else
 \immediate\write\amflkashid{\amvalf}
 \advance\amposf by 1
 \ifdim\amvalf<\amsize
 \ifdim\amvalinf<\amvalf \global\amposinf=\amposf \global\amvalinf=\amvalf \fi
 \else
 \ifdim\amvalf<\amvalsup \global\ampossup=\amposf \global\amvalsup=\amvalf \fi
 \fi
 \fi \ifmore \repeat
 \immediate\closein\amrf
 \immediate\closeout\amflkashid
 \immediate\openout\amflkashid=kashida.txt\relax
 \immediate\openin\amrf=buffer.txt\relax
 \moretrue
 \loop
 { \endlinechar=-1
   \global
   \read\amrf to \amvalf
 }
  \ifeof\amrf \global\morefalse
  \else \immediate\write\amflkashid{\amvalf}
  \fi
 \ifmore \repeat
 \immediate\closein\amrf
 }
}

%
{ \catcode`\<=12
\gdef\amrestekashid{
 \global\advance\amnbkashid by -1
 \loop
  \global\advance\amnbkashid by 1
  \ifnum\amnbkashid<255
   \immediate\write\amflkashid{0.0pt}
 \repeat
 \immediate\closeout\amflkashid
}
}

%
\def\amfinalkashid{
 \immediate\openout\amflkashid=kashida.mf\relax
 \immediate\openin\amrf=kashida.txt\relax
 \amposf=-1
 \moretrue
 \loop
 { \endlinechar=-1
   \global
   \read\amrf to \amvalf
 }
  \ifeof\amrf \global\morefalse
  \else
   \advance\amposf by 1
   \immediate\write\amflkashid{%
        tuk[\the\amposf]\amdieze := \amvalf\amdieze ;}
  \fi
 \ifmore \repeat
 \immediate\closein\amrf
 \immediate\closein\amflkashid
}

%
\def\amopkashid#1{ \ifcase#1
 \amkashidaa \or \amkashidab \or \amkashidac \or \amkashidad \or
 \amkashidae \or \amkashidaf \or \amkashidag \or \amkashidah \or
 \amkashidai \or \amkashidaj \or \amkashidak \or \amkashidal \or
 \amkashidam \or \amkashidan \or \amkashidao \or \amkashidap \or
 \amkashidaq \or \amkashidar \or \amkashidas \or \amkashidat \or
 \amkashidau \or \amkashidav \or \amkashidaw \or \amkashidax \or
 \amkashiday \or \amkashidaz \or
 \amkashidba \or \amkashidbb \or \amkashidbc \or \amkashidbd \or
 \amkashidbe \or \amkashidbf \or \amkashidbg \or \amkashidbh \or
 \amkashidbi \or \amkashidbj \or \amkashidbk \or \amkashidbl \or
 \amkashidbm \or \amkashidbn \or \amkashidbo \or \amkashidbp \or
 \amkashidbq \or \amkashidbr \or \amkashidbs \or \amkashidbt \or
 \amkashidbu \or \amkashidbv \or \amkashidbw \or \amkashidbx \or
 \amkashidby \or \amkashidbz \or
 \amkashidca \or \amkashidcb \or \amkashidcc \or \amkashidcd \or
 \amkashidce \or \amkashidcf \or \amkashidcg \or \amkashidch \or
 \amkashidci \or \amkashidcj \or \amkashidck \or \amkashidcl \or
 \amkashidcm \or \amkashidcn \or \amkashidco \or \amkashidcp \or
 \amkashidcq \or \amkashidcr \or \amkashidcs \or \amkashidct \or
 \amkashidcu \or \amkashidcv \or \amkashidcw \or \amkashidcx \or
 \amkashidcy \or \amkashidcz \or
 \amkashidda \or \amkashiddb \or \amkashiddc \or \amkashiddd \or
 \amkashidde \or \amkashiddf \or \amkashiddg \or \amkashiddh \or
 \amkashiddi \or \amkashiddj \or \amkashiddk \or \amkashiddl \or
 \amkashiddm \or \amkashiddn \or \amkashiddo \or \amkashiddp \or
 \amkashiddq \or \amkashiddr \or \amkashidds \or \amkashiddt \or
 \amkashiddu \or \amkashiddv \or \amkashiddw \or \amkashiddx \or
 \amkashiddy \or \amkashiddz \or
 \amkashidea \or \amkashideb \or \amkashidec \or \amkashided \or
 \amkashidee \or \amkashidef \or \amkashideg \or \amkashideh \or
 \amkashidei \or \amkashidej \or \amkashidek \or \amkashidel \or
 \amkashidem \or \amkashiden \or \amkashideo \or \amkashidep \or
 \amkashideq \or \amkashider \or \amkashides \or \amkashidet \or
 \amkashideu \or \amkashidev \or \amkashidew \or \amkashidex \or
 \amkashidey \or \amkashidez \or
 \amkashidfa \or \amkashidfb \or \amkashidfc \or \amkashidfd \or
 \amkashidfe \or \amkashidff \or \amkashidfg \or \amkashidfh \or
 \amkashidfi \or \amkashidfj \or \amkashidfk \or \amkashidfl \or
 \amkashidfm \or \amkashidfn \or \amkashidfo \or \amkashidfp \or
 \amkashidfq \or \amkashidfr \or \amkashidfs \or \amkashidft \or
 \amkashidfu \or \amkashidfv \or \amkashidfw \or \amkashidfx \or
 \amkashidfy \or \amkashidfz \or
 \amkashidga \or \amkashidgb \or \amkashidgc \or \amkashidgd \or
 \amkashidge \or \amkashidgf \or \amkashidgg \or \amkashidgh \or
 \amkashidgi \or \amkashidgj \or \amkashidgk \or \amkashidgl \or
 \amkashidgm \or \amkashidgn \or \amkashidgo \or \amkashidgp \or
 \amkashidgq \or \amkashidgr \or \amkashidgs \or \amkashidgt \or
 \amkashidgu \or \amkashidgv \or \amkashidgw \or \amkashidgx \or
 \amkashidgy \or \amkashidgz \or
 \amkashidha \or \amkashidhb \or \amkashidhc \or \amkashidhd \or
 \amkashidhe \or \amkashidhf \or \amkashidhg \or \amkashidhh \or
 \amkashidhi \or \amkashidhj \or \amkashidhk \or \amkashidhl \or
 \amkashidhm \or \amkashidhn \or \amkashidho \or \amkashidhp \or
 \amkashidhq \or \amkashidhr \or \amkashidhs \or \amkashidht \or
 \amkashidhu \or \amkashidhv \or \amkashidhw \or \amkashidhx \or
 \amkashidhy \or \amkashidhz \or
 \amkashidia \or \amkashidib \or \amkashidic \or \amkashidid \or
 \amkashidie \or \amkashidif \or \amkashidig \or \amkashidih \or
 \amkashidii \or \amkashidij \or \amkashidik \or \amkashidil \or
 \amkashidim \or \amkashidin \or \amkashidio \or \amkashidip \or
 \amkashidiq \or \amkashidir \or \amkashidis \or \amkashidit \or
 \amkashidiu \or \amkashidiv \or \amkashidiw \or \amkashidix \or
 \amkashidiy \or \amkashidiz \or
 \amkashidja \or \amkashidjb \or \amkashidjc \or \amkashidjd \or
 \amkashidje \or \amkashidjf \or \amkashidjg \or \amkashidjh \or
 \amkashidji \or \amkashidjj \or \amkashidjk \or \amkashidjl \or
 \amkashidjm \or \amkashidjn \or \amkashidjo \or \amkashidjp \or
 \amkashidjq \or \amkashidjr \or \amkashidjs \or \amkashidjt \or
 \amkashidju \or \amkashidjv \fi \relax
}
