%D \module
%D   [       file=syst-con,
%D        version=2000.12.10, % actually very old -)
%D          title=\CONTEXT\ System Macros,
%D       subtitle=Conversions,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{Context System Macro's / Conversions}

\unprotect

%D When the number of conversions grew, it did no longer make
%D sense to spread them over multiple files. So, instead of
%D defining these in \type {font-ini}, we now have a dedicated
%D module.

\catcode127=12 % other, just to be sure

%D \macros
%D   {lchexnumber,uchexnumber,lchexnumbers,uchexnumbers}
%D
%D In addition to the uppercase hex conversion, as needed in
%D math families, we occasionally need a lowercase one, for
%D instance when we want to compose gbsong fontnames.
%D
%D The ugly indirectness is needed to get rid of \TEX\
%D induced spaces and \type {\relax}'s.
%D
%D \starttyping
%D [\uchexnumber{0}]
%D [\uchexnumber\scratchcounter]
%D [\uchexnumber\zerocount]
%D [\uchexnumber{\number0}]
%D [\uchexnumber{\number\scratchcounter}]
%D [\uchexnumber{\number\zerocount}]
%D [\uchexnumber{\the\scratchcounter}]
%D [\uchexnumber{\the\zerocount}]
%D [\expandafter\uchexnumber\expandafter{\number0}]
%D [\expandafter\uchexnumber\expandafter{\number\scratchcounter}]
%D [\expandafter\uchexnumber\expandafter{\number\zerocount}]
%D [\expandafter\uchexnumber\expandafter{\the\scratchcounter}]
%D [\expandafter\uchexnumber\expandafter{\the\zerocount}]
%D \stoptyping

\let\lchexnumber \gobbleoneargument
\let\uchexnumber \gobbleoneargument
\let\lchexnumbers\gobbleoneargument
\let\uchexnumbers\gobbleoneargument

%D \macros
%D   {octnumber}
%D
%D For unicode remapping purposes, we need octal numbers.

\let\octnumber\gobbleoneargument

%D \macros
%D   {hexstringtonumber}
%D
%D This macro converts a two character hexadecimal number into
%D a decimal number, thereby taking care of lowercase characters
%D as well.

\let\hexstringtonumber\gobbleoneargument

%D \macros
%D   {rawcharacter}
%D
%D This macro can be used to produce proper 8 bit characters
%D that we sometimes need in backends and round||trips.

\let\rawcharacter\gobbleoneargument

%D \macros
%D   {twodigits, threedigits}
%D
%D These macros provides two or three digits always:

\def\twodigits  #1{\ifnum             #1<10     0\fi\number#1}
\def\threedigits#1{\ifnum#1<100 \ifnum#1<10 0\fi0\fi\number#1}

%D \macros{modulonumber}
%D
%D In the conversion macros described in \type {core-con} we
%D need a wrap||around method. The following solution is
%D provided by Taco.
%D
%D The \type {modulonumber} macro expands to the mathematical
%D modulo of a positive integer. It is crucial for it's
%D application that this macro is fully exandable.
%D
%D The expression inside the \type {\numexpr} itself is
%D somewhat bizarre because \ETEX\ uses a rounding
%D division instead of truncation. If \ETEX's division
%D would have behaved like \TEX's normal\type{\divide}, then
%D the expression could have been somewhat simpler, like
%D \type {#2-(#2/#1)*#1}. This works just as well, but a bit
%D more complex.

\beginETEX

\def\modulonumber#1#2%
   {\the\numexpr#2-((((#2+(#1/2))/#1)-1)*#1)\relax}

\endETEX

%D \macros{modulatednumber}
%D
%D Modulo numbers run from zero to one less than the limit,
%D but for conversion sets, we need a value between 1 and the
%D limit. The \type{\modulatednumber} arranges that. This
%D macro also needs to be fully expandable, resulting in
%D two \type{\numexpr}s.

\beginETEX

\def\modulatednumber#1#2%
  {\ifnum\the\numexpr\modulonumber{#1}{#2}\relax=0 #1%
   \else \the\numexpr\modulonumber{#1}{#2}\relax  \fi}

\endETEX

%D When not running \ETEX\ you're left with the maximum:

\beginTEX

\def\modulatednumber#1#2%
  {\ifnum#2>#1 #1\else#2\fi}

\endTEX

%D Plugins

\loadmarkfile{syst-con}

\let\hexnumber\uchexnumber

\protect \endinput
