%D \module
%D   [       file=typo-rub,
%D        version=2016.10.10,
%D          title=\CONTEXT\ Typesetting Macros,
%D       subtitle=Rubies,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D Experimental and unfinished.

% todo: distance

\writestatus{loading}{ConTeXt Typesetting Macros / Rubies}

\unprotect

\registerctxluafile{typo-rub}{autosuffix}

\definesystemattribute[ruby][public]

\installcorenamespace {ruby}
\installcorenamespace {rubyanalyze}
\installcorenamespace {rubyplacement}
\installcorenamespace {rubynumber}

\installcommandhandler \??ruby {ruby} \??ruby

% \appendtoks
%     \clf_defineruby
%         align   {\rubyparameter\c!align}
%         stretch {\rubyparameter\c!stretch}
%         hoffset  \rubyparameter\c!hoffset
%         voffset  \rubyparameter\c!voffset
%     \relax
% \to \everydefineruby

\permanent\tolerant\protected\def\ruby[#1]#:#2#3%
  {\dontleavehmode
   \begingroup
   \enforced\let\ruby\noruby
   \cdef\currentruby{#1}%
   \edef\p_location{\rubyparameter\c!location}%
 % \enforced\let|\relax
   \enforced\letcharcode\barasciicode\relax
   \ifcsname\??rubyanalyze\p_location\endcsname
     \expandafter\lastnamedcs\else\expandafter\typo_ruby_analyze
   \fi{#2}{#3}%
   \endgroup}

\permanent\tolerant\protected\def\noruby[#1]#:#2#3%
  {#2}

\def\typo_ruby_analyze#1#2%
  {\clf_ruby
     base    {#1}%
     comment {#2}
   \relax}

\defcsname\??rubyanalyze\v!top\endcsname#1#2%
  {\clf_ruby
     align   {\rubyparameter\c!align}%
     stretch {\rubyparameter\c!stretch}%
     hoffset {\rubyparameter\c!hoffset}%
     voffset {\rubyparameter\c!voffset}%
     base    {#1}%
     comment {#2}
   \relax}

\defcsname\??rubyplacement\v!top\endcsname#1#2%
  {\setbox\scratchbox\hbox\bgroup
     \userubystyleandcolor\c!style\c!color
     #2%
   \egroup
   \clf_setruby \scratchbox
   \relax
   #1}

\defcsname\??rubyplacement\v!right\endcsname#1#2%
  {#1%
   \edef\p_distance{\rubyparameter\c!distance}%
   \ifempty\p_distance\orelse\ifx\p_distance\v!none\else\hskip{\p_distance}\relax\fi
   \begingroup
   \userubystyleandcolor\c!style\c!color
   \rubyparameter\c!left#2\rubyparameter\c!right
   \endgroup}

\defcsname\??rubyplacement\v!left\endcsname#1#2%
  {\begingroup
   \userubystyleandcolor\c!style\c!color
   \rubyparameter\c!left#2\rubyparameter\c!right
   \endgroup
   \edef\p_distance{\rubyparameter\c!distance}%
   \ifempty\p_distance\orelse\ifx\p_distance\v!none\else\hskip{\p_distance}\relax\fi
   #1}

\permanent\protected\def\setruby#1#2%
  {\begingroup
   \ifcsname\??rubyplacement\p_location\endcsname
     \lastnamedcs{#1}{#2}%
   \else
     #1%
   \fi
   \endgroup}

\permanent\tolerant\protected\def\startruby[#1]%
  {\dontleavehmode
   \begingroup
   \enforced\aliased\startruby\begingroup
   \cdef\currentruby{#1}%
   \clf_startruby
        align   {\rubyparameter\c!align}%
        stretch {\rubyparameter\c!stretch}%
        hoffset {\rubyparameter\c!hoffset}%
        voffset {\rubyparameter\c!voffset}%
   \relax}

\permanent\protected\def\stopruby
  {\endgroup}

\setupruby
  [\c!style=\txx,
   \c!location=\v!top,
   \c!left=(,
   \c!right=),
   \c!distance=\zeropoint, % \v!none means no skip at all so no break either
   \c!hoffset=\zeropoint,
   \c!voffset=-2\exheight]

%D Experiment too:

\permanent\protected\def\numberedruby[#1]#2%
  {\doifnotcounter{\??rubynumber#1}{\definecounter[\??rubynumber#1]\setcounter[\??rubynumber#1][1]}%
   \ruby[#1]{#2}{\convertedcounter[\??rubynumber#1]}%
   \incrementcounter[\??rubynumber#1]%
   \relax}

\protect \endinput

% \usemodule[art-01]\setupbodyfont[dejavu,12pt]
%
% \defineruby[auto]  [align=auto,color=darkred]
% \defineruby[left]  [align=flushleft,color=darkred]
% \defineruby[right] [align=flushright,color=darkred]
% \defineruby[spread][stretch=yes]
%
% \showframe \showglyphs \showfontkerns \setupinterlinespace[22pt]
%
% \starttext
%
% \startbuffer
%     \dorecurse{20}{\ruby{XYZ}{a|bc|d} }\par
%     \dorecurse{20}{\ruby{PQR}{p|q|r} }\par
%     \dorecurse{20}{\ruby{XYZ}{1|22|333} }\par
%     \dorecurse{20}{\ruby{XYZ}{111|222|333} }\par
%     \dorecurse{20}{\ruby{XYZ}{foobar} }\par
%     \dorecurse{20}{\ruby{XYZ}{fooledbar} }\par
%     \dorecurse{20}{\ruby[spread]{XYZ}{fooledbar} }\par
%     \dorecurse{20}{\ruby{extremely}{wide} }\par
%     \dorecurse{20}{\ruby{wide}{extremely} }\par
%  stopbuffer
%
% \testfeatureonce{1}{\start \setupinterlinespace[16pt] \setupruby[location=none]                   \getbuffer \stop \page}
% \testfeatureonce{1}{\start \setupinterlinespace[16pt] \setupruby[location=right]                  \getbuffer \stop \page}
% \testfeatureonce{1}{\start \setupinterlinespace[28pt] \setupruby[align=auto,color=darkred]        \getbuffer \stop \page}
% \testfeatureonce{1}{\start \setupinterlinespace[28pt] \setupruby[align=flushleft,color=darkgreen] \getbuffer \stop \page}
% \testfeatureonce{1}{\start \setupinterlinespace[28pt] \setupruby[align=flushright,color=darkblue] \getbuffer \stop \page}
% \testfeatureonce{1}{\start \setupinterlinespace[28pt] \setupruby[align=middle,color=darkyellow]   \getbuffer \stop \page}
%
% \stoptext
