%D \module
%D   [     file=t-cyrillicnumbers.mkvi,
%D      version=2013-03-28 00:11:33+0100,
%D        title=\CONTEXT\ User Module,
%D     subtitle=Cyrillic Numbers,
%D       author=Philipp Gesang,
%D         date=\currentdate,
%D    copyright=Philipp Gesang,
%D      license=2-clause BSD,
%D        email={phg42 dot 2a at gmail dot com}]
%D This module is licensed under the conditions of the BSD license with
%D two clauses. There is a copy in a file named "COPYING" in the
%D t-cyrillicnumbers source tree.

%M \definehead[docsection] [chapter]
%M \setuphead [docsection] [page=no,style=\tf\bold\WORD,color=darkmagenta,alternative=inmargin,before={\blank[3*line]}]
%M \definehead[doctitle]   [coupling=docsection,default=docsection,incrementnumber=no,page=no]
%M \setuplist [docsection] [alternative=b,textstyle=\tf]
%M \setupinteraction[focus=standard,color=darkmagenta]

%D \doctitle{Overview}\placelist[docsection][criterium=all]\page[yes]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%D \docsection{Global strings and namespace initialization}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\writestatus{loading}{Cyrillic Number Support for ConTeXt}

\unprotect

\startinterface all
  \setinterfacevariable {cyrillicnumerals} {cyrillicnumerals}
  \setinterfacevariable {Cyrillicnumerals} {Cyrillicnumerals}
  \setinterfacevariable           {cyrnum} {cyrnum}
  \setinterfacevariable             {dots} {dots}
  \setinterfacevariable        {dotsymbol} {dotsymbol}
  \setinterfacevariable         {penwidth} {penwidth}
  \setinterfacevariable   {preferhundredk} {preferhundredk}
  \setinterfacevariable    {titlolocation} {titlolocation}
  \setinterfacevariable       {titlocolor} {titlocolor}
  \setinterfacevariable        {titlomode} {titlomode}
  \setinterfacevariable        {titlospan} {titlospan}
  \setinterfacevariable            {titlo} {titlo}
\stopinterface

\definenamespace [cyrnum] [
 \v!command=\v!yes,
    comment=cyrillic numbers,
    \s!name=\v!cyrnum,
  \s!parent=\v!cyrnum,
      setup=\v!list,
      style=\v!no,
       type=module,
    version=hg-tip,
]

%D Loading the \LUA\ conversion routines.

\registerctxluafile{cyrillicnumbers}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%D \docsection{Setup command}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%D The\reference[setupdef]{} autogenerated setup command needs to
%D be extended to pass values on to the \LUA\ end.

\def\cyrnumsetvariables{%
  \startluacode
    local tc = thirddata.cyrnum
    tc.placetitlo    = "\cyrnumparameter{\v!titlo}"
    tc.titlolocation = tc.position_synonyms["\cyrnumparameter{\v!titlolocation}"]   or "f"
    tc.prefer100k    = tc.     yes_synonyms["\cyrnumparameter{\v!preferhundredk}"]  or false
    tc.drawdots      = tc.     yes_synonyms["\cyrnumparameter{\v!dots}"]            or false
    tc.dotsymbol     = "\luaescapestring{\cyrnumparameter{\v!dotsymbol}}"

    local span   = "\cyrnumparameter{\v!titlospan}"
    local num = tonumber(span)
    if num then
      tc.titlotype = true
      tc.titlospan = num
    elseif span == "all" then
      tc.titlotype = false
    end
  \stopluacode%
}

\appendtoks \setuevalue{\currentcyrnum}{\cyrnum_cmd[\currentcyrnum]} \to \everydefinecyrnum
\appendtoks \cyrnumsetvariables                                      \to \everysetupcyrnum

%D This allows the module to be loaded with a second set of
%D key-value arguments. If initialized as follows, additional debug
%D information will be output on the terminal.
%D
%D \starttyping
%D \usemodule[cyrillicnumbers][test=yes]
%D \stoptyping

\startmoduletestsection
  \ctxlua{thirddata.cyrnum.debug = true}
\stopmoduletestsection

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%D \docsection{Titlo variants}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%D The {\em Cyrillic Numbers} module allows the titlo, if requested,
%D to be drawn in two different ways. If the setup parameter
%D \type{titlo} has the value {\em mp}, then the variant employing
%D \METAPOST\ is selected. As there are a couple of different
%D styles, it will be necessary that the user determine one by
%D setting the parameter \type{titlomode} to an integer.
%D Additionally it is possible to manipulate the pen width that
%D \METAPOST\ uses when executing the titlo code. {\em NB} as the
%D drawing routines differ, the \type{penwidth} parameter lead to
%D different results with different modes as well, ymmv.
%D
%D \starttyping
%D \setupcyrnum[
%D   titlo=mp,
%D   titlomode=4,
%D   penwidth=4,
%D ]
%D \stoptyping

\startuniqueMPgraphic{cyrnum-titlo1}
  path titlo ;
  save High, Low ;
  save Vfactor ; Vfactor = .15 ;
  save Hfactor ; Hfactor = .90 ;
  titlo := ( 0.00,    -Vfactor) ..
           ( 0.05,  .4*Vfactor) ..
           ( 0.95, -.4*Vfactor) ..
           ( 1.00,     Vfactor) ;
  pickup pensquare yscaled \cyrnumparameter{penwidth} xscaled 1 rotated -30 ;
  draw titlo
       xysized (Hfactor*OverlayWidth,Vfactor*OverlayHeight)
       shifted (.5*(OverlayWidth-Hfactor*OverlayWidth),OverlayHeight-Vfactor*OverlayHeight)
       withcolor \MPcolor{\cyrnumparameter{titlocolor}};
  setbounds currentpicture to boundingbox OverlayBox ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{cyrnum-titlo2}
  path titlo ;
  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
  save High, Low ;
  save Vfactor ; Vfactor = .15 ;
  save Hfactor ; Hfactor = .80 ;
  titlo := ( 0.05,    -Vfactor) --
           ( 0.07,  .7*Vfactor) ..
           ( 0.09,  .8*Vfactor) ..
           ( 0.91, -.8*Vfactor) ..
           ( 0.93, -.7*Vfactor) --
           ( 0.95,     Vfactor) ;
  pickup pensquare yscaled penwidth xscaled 1 rotated 70 ;
  draw titlo
       xysized (Hfactor*OverlayWidth,Vfactor*OverlayHeight)
       shifted (.5*(OverlayWidth-Hfactor*OverlayWidth)-.5*penwidth,OverlayHeight-Vfactor*OverlayHeight)
       withcolor \MPcolor{\cyrnumparameter{titlocolor}};
  setbounds currentpicture to boundingbox OverlayBox ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{cyrnum-titlo3}
  path titlo ;
  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
  save High, Low ;
  save Vfactor ; Vfactor = .15 ;
  save Hfactor ; Hfactor = .80 ;
  titlo := ( 0.05,      -Vfactor) --
           ( 0.07,  0.70*Vfactor) ..
           ( 0.09,  0.90*Vfactor) .. { dir 00 }
           ( 0.49, -0.00*Vfactor) --
           ( 0.49, -0.90*Vfactor) --
           ( 0.51,  0.90*Vfactor) --
           ( 0.51,  0.00*Vfactor) { dir 00 } ..
           ( 0.91, -0.90*Vfactor) ..
           ( 0.93, -0.70*Vfactor) --
           ( 0.95,       Vfactor) ;
  pickup pensquare yscaled penwidth xscaled 1 rotated 70 ;
  draw titlo
       xysized (Hfactor*OverlayWidth,Vfactor*OverlayHeight)
       shifted (.5*(OverlayWidth-Hfactor*OverlayWidth)-.5*penwidth,OverlayHeight-Vfactor*OverlayHeight)
       withcolor \MPcolor{\cyrnumparameter{titlocolor}};
  setbounds currentpicture to boundingbox OverlayBox ;
\stopuniqueMPgraphic

%%% Same as 2 with inner vertical stroke
\startuniqueMPgraphic{cyrnum-titlo4}
  path titlo, stroke ;
  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
  save High, Low ;
  save Vfactor ; Vfactor = .15 ;
  save Hfactor ; Hfactor = .80 ;
  titlo = ( 0.05, -1.00*Vfactor) --
          ( 0.07,  0.70*Vfactor) ..
          ( 0.09,  0.90*Vfactor) { dir 00 } .. { dir 00 }
          ( 0.91, -0.90*Vfactor) ..
          ( 0.93, -0.70*Vfactor) --
          ( 0.95,  1.00*Vfactor) ;
  stroke = ( 0.49*Hfactor*OverlayWidth+.5*penwidth, -0.30*Vfactor*OverlayHeight ) -- %% this is a mess
           ( 0.51*Hfactor*OverlayWidth+.5*penwidth,  0.50*Vfactor*OverlayHeight ) ;
  pickup pensquare yscaled penwidth xscaled 1 rotated 80 ;
  draw titlo
       xysized (Hfactor*OverlayWidth,Vfactor*OverlayHeight)
       shifted (.5*(OverlayWidth-Hfactor*OverlayWidth)-.5*penwidth,OverlayHeight-Vfactor*OverlayHeight)
       withcolor \MPcolor{\cyrnumparameter{titlocolor}};
  draw stroke
       % xysized (Hfactor*OverlayWidth,Vfactor*OverlayHeight) %% doesn’t work as expected somehow
       shifted (.5*(OverlayWidth-Hfactor*OverlayWidth)-.5*penwidth,OverlayHeight-Vfactor*OverlayHeight)
       withcolor \MPcolor{\cyrnumparameter{titlocolor}} ;
  setbounds currentpicture to boundingbox OverlayBox ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{cyrnum-titlo5}
  path titlo ;
  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
  save High, Low ;
  save Vfactor ; Vfactor = .20 ;
  save Hfactor ; Hfactor = .75 ;
  titlo := ( 0.00, -0.8*Vfactor) {dir 90} .. {dir 90}
           ( 1.00,  1.2*Vfactor) ;
  pickup pensquare yscaled penwidth xscaled 2 rotated 85 ;
  draw titlo
       xysized (Hfactor*OverlayWidth-penwidth,Vfactor*OverlayHeight)
       shifted (.5*(OverlayWidth-Hfactor*OverlayWidth),OverlayHeight-Vfactor*OverlayHeight)
       withcolor \MPcolor{\cyrnumparameter{titlocolor}} ;
  setbounds currentpicture to boundingbox OverlayBox ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{cyrnum-titlo6}
  path titlo ;
  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
  save High, Low ;
  save Vfactor ; Vfactor = .20 ;
  save Hfactor ; Hfactor = .85 ;
  titlo := ( 0.03, -0.80*Vfactor) --
           ( 0.03,  0.15*Vfactor) --
           ( 0.00,  0.20*Vfactor) {right} .. {right}
           ( 1.00, -0.20*Vfactor) --
           ( 0.97, -0.15*Vfactor) --
           ( 0.97,  0.80*Vfactor) ;
  pickup pensquare yscaled penwidth xscaled sqrt(2) rotated 90 ;
  draw titlo
       xysized (Hfactor*OverlayWidth-.5*penwidth,Vfactor*OverlayHeight)
       shifted (.5*(OverlayWidth-Hfactor*OverlayWidth),OverlayHeight-(0.5*Vfactor*OverlayHeight))
       withcolor \MPcolor{\cyrnumparameter{titlocolor}} ;
  setbounds currentpicture to boundingbox OverlayBox ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{cyrnum-titlo7}
  path titlo ;
  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
  save High, Low ;
  save Vfactor ; Vfactor = .20 ;
  save Hfactor ; Hfactor = .85 ;
  titlo := ( 0.05, -0.80*Vfactor) --
           ( 0.05, -0.30*Vfactor) {up} .. {right}
           ( 0.07, -0.00*Vfactor) --
           ( 0.93,  0.00*Vfactor) {right} .. {up}
           ( 0.95,  0.30*Vfactor) --
           ( 0.95,  0.80*Vfactor) ;
  pickup pensquare yscaled (0.66*penwidth) xscaled penwidth ;
  draw titlo
       xysized (Hfactor*OverlayWidth-2*penwidth,Vfactor*OverlayHeight)
       shifted ((.5*penwidth+.5*(OverlayWidth-Hfactor*OverlayWidth)),
                (OverlayHeight-(0.7*Vfactor*OverlayHeight)))
       withcolor \MPcolor{\cyrnumparameter{titlocolor}} ;
  setbounds currentpicture to boundingbox OverlayBox ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{cyrnum-titlo8}
  path titlo, stroke ;
  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
  save High, Low ;
  save Vfactor ; Vfactor = .20 ;
  save Hfactor ; Hfactor = .85 ;
  titlo := ( 0.15, -0.10*Vfactor) {dir 25} ..% tension 8 ..
            controls (0.40, 0.50) and (0.00, 0.35) ..
           ( 0.00, -0.00*Vfactor) {down} .. 
            controls (0.00, -0.30) and (0.10, -0.25) ..
           % ( 0.10, -0.20*Vfactor) {right} .. tension 4 .. {dir -120}
           ( 0.30, -0.90*Vfactor) ..
           controls
            ( 0.70,  0.20*Vfactor) and
            ( 0.97,  0.08*Vfactor) ..
           ( 0.97, -0.30*Vfactor) .. tension 6 .. {dir -140}
           ( 0.95, -0.90*Vfactor) ;
  stroke := ((0.45*Hfactor*OverlayWidth),-0.20*Vfactor*OverlayHeight) ..
            ((0.54*Hfactor*OverlayWidth), 0.35*Vfactor*OverlayHeight) ---
            ((0.65*Hfactor*OverlayWidth), 0.90*Vfactor*OverlayHeight) {dir -120} .. {dir 30}
            % ((0.54*Hfactor*OverlayWidth), 0.35*Vfactor*OverlayHeight) ..
            ((0.45*Hfactor*OverlayWidth), 0.90*Vfactor*OverlayHeight)
            ;
  pickup pensquare yscaled 2 xscaled (0.5*penwidth) rotated 10;
  % pickup pensquare yscaled 1 xscaled 2 ;
  draw titlo
       xysized (Hfactor*OverlayWidth,Vfactor*OverlayHeight)
       shifted ((.5*(OverlayWidth-Hfactor*OverlayWidth)),
                (OverlayHeight-(0.7*Vfactor*OverlayHeight)))
       withcolor \MPcolor{\cyrnumparameter{titlocolor}} ;
  pickup pensquare yscaled 2 xscaled (0.5*penwidth) rotated 70;
  draw stroke
       % xysized (Hfactor*OverlayWidth,Vfactor*OverlayHeight)
       shifted ((.6*(OverlayWidth-Hfactor*OverlayWidth)),
                (OverlayHeight-Vfactor*OverlayHeight))
       withcolor \MPcolor{\cyrnumparameter{titlocolor}} ;
       ;
  setbounds currentpicture to boundingbox OverlayBox ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{cyrnum-titlo9}
  path titlo ;
  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
  save High, Low ;
  save Vfactor ; Vfactor = .10 ;
  save Hfactor ; Hfactor = .85 ;
  titlo := ( 0.05, -0.40*Vfactor) --
           ( 0.05,  0.00*Vfactor) --
           ( 0.95,  0.00*Vfactor) --
           ( 0.95, -0.40*Vfactor) ;
  pickup pensquare yscaled (2*penwidth/3) xscaled penwidth ;
  draw titlo
       xysized (Hfactor*OverlayWidth-2*penwidth,Vfactor*OverlayHeight)
       shifted ((.5*penwidth+.5*(OverlayWidth-Hfactor*OverlayWidth)),
                (OverlayHeight-(0.7*Vfactor*OverlayHeight)))
       withcolor \MPcolor{\cyrnumparameter{titlocolor}} ;
  setbounds currentpicture to boundingbox OverlayBox ;
\stopuniqueMPgraphic

%D The \METAPOST\ titlo is drawn as an overlay which in turn
%D serves as the background for a dedicated \type{\framed} macro.
%D This titlo command is accessible in protected mode as
%D \type{\cyrnumdrawtitlo}, which is also called by the
%D \LUA\ functions.

\defineoverlay[cyrnum-titlo-overlay1][\uniqueMPgraphic{cyrnum-titlo1}]
\defineoverlay[cyrnum-titlo-overlay2][\uniqueMPgraphic{cyrnum-titlo2}]
\defineoverlay[cyrnum-titlo-overlay3][\uniqueMPgraphic{cyrnum-titlo3}]
\defineoverlay[cyrnum-titlo-overlay4][\uniqueMPgraphic{cyrnum-titlo4}]
\defineoverlay[cyrnum-titlo-overlay5][\uniqueMPgraphic{cyrnum-titlo5}]
\defineoverlay[cyrnum-titlo-overlay6][\uniqueMPgraphic{cyrnum-titlo6}]
\defineoverlay[cyrnum-titlo-overlay7][\uniqueMPgraphic{cyrnum-titlo7}]
\defineoverlay[cyrnum-titlo-overlay8][\uniqueMPgraphic{cyrnum-titlo8}]
\defineoverlay[cyrnum-titlo-overlay9][\uniqueMPgraphic{cyrnum-titlo9}]

\defineframed[cyrnum_titloframe][
    location=low,
       frame=off,
  background=cyrnum-titlo-overlay\cyrnumparameter{titlomode},
]

%D \macros
%D   {cyrnumdrawtitlo}
%D
%D The macro \type{\cyrnumdrawtitlo} allows for arbitrary titlo
%D placement which might be useful for other text elements besides
%D plain numbers, e.~g. abbreviations {\italic\letterampersand}c.

\let\cyrnumdrawtitlo\cyrnum_titloframe

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%D \docsection{User level interface}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%D \macros
%D   {definecyrnum}
%D
%D User interface to the main conversion routines. The definition
%D macro autogenerates the primary user-level macro, which in turn
%D relies \type{\setupcyrnum} for argument processing (see above on
%D \at{page}[setupdef]).
%D
%D Example usage:
%D
%D \starttyping
%D   \definecyrnum[titlofree]
%D   \setupcyrnum [titlofree][titlo=no]
%D   \starttext
%D     \titlofree{42}
%D   \stoptext
%D \stoptyping

\unexpanded\def\cyrnum_cmd[#id]{%
  \edef\currentcyrnum{#id}%
  \dosingleempty\cyrnum_cmd_parms%
}

\def\cyrnum_cmd_parms[#parms]{%
  \begingroup
  \setupcyrnum[\currentcyrnum][#parms]%
  \cyrnumparameter{command}\begingroup
  \cyrnum_cmd_content%
}

\def\cyrnum_cmd_content#num{%
  \ctxcommand{cyrillicnumerals(\number#num)}%
  \endgroup
  \endgroup%
}

%D \macros
%D    {setupcyrnum}
%D
%D The \type{\setupcyrnum} and \type{\definecyrnum} macros are
%D autogenerated via the namespace.

\setupcyrnum[
            \v!dots=\v!yes,    % yes|no
       \v!dotsymbol=·,
        \v!penwidth=.66pt,
  \v!preferhundredk=no,
         \v!command=,
      \v!titlocolor=black,
   \v!titlolocation=\v!middle, % final|middle -- only effective if using the font’s titlo
       \v!titlomode=5,
       \v!titlospan=3,         % n|all
           \v!titlo=\v!mp,     % font|mp|no
]

%D \macros
%D   {cyrnum}
%D
%D The default user macro is defined as \type{\cyrnum}.

\definecyrnum[\v!cyrnum]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%D \docsection{Conversion routines}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%D \macros
%D   {cyrillicnumerals,Cyrillicnumerals}
%D
%D Conforming to the standard \CONTEXT\ naming scheme as employed
%D with number conversion macros, we define two macros for use with
%D conversions. As there is no definition for uppercase conversion,
%D the macro \type{\Cyrillicnumerals} exists only for compatibility
%D reasons.
%D
%D Also, we setup two corresponding conversions \type{cyr} and
%D \type{Cyr} for use with e.g. enumerations, headings, counters
%D {\italic\letterampersand}c. Instead of these predefined ones
%D the user may consider defining a dedicated conversion routine
%D for each purpose:

%D \starttyping
%D \definecyrnum    [mycyrnum] [titlo=mp,titlomode=7,titlospan=all,penwidth=2]
%D \defineconversion[myconversion][\mycyrnum]
%D
%D \setuphead[section][conversion=myconversion]
%D \stoptyping

\definecyrnum[\v!cyrillicnumerals]
\definecyrnum[\v!Cyrillicnumerals]

\defineconversion[cyrillicnumerals][\cyrillicnumerals]
\defineconversion[Cyrillicnumerals][\Cyrillicnumerals]
\defineconversion[cyr]             [\cyrillicnumerals]
\defineconversion[Cyr]             [\Cyrillicnumerals]

% \protect \endinput

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fun %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\peano#num{\ctxcommand{peanonumerals(\number#num)}}
\defineconversion[peano][\peano]

\def\church#num{\ctxcommand{churchnumerals(\number#num)}}
\defineconversion[church][\church]

\protect \endinput

%   vi:ft=context:fo=co:ts=2:expandtab:sw=2
