%D \module
%D   [       file=m-simulate,
%D        version=2016.02.18, % follow up on m-visual of 2000.01.10
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=Visualization and Faking Redone,
%D         author={Hans Hagen \& Ton Otten},
%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.

\unprotect

\definepalet
  [fakerule]
  [fr1c=darkred,
   fr2c=darkgreen,
   fr3c=darkblue,
   fr4c=darkyellow,
   fr5c=darkgray,
   fr6c=darkred,
   fr7c=darkgreen,
   fr8c=darkblue,
   fr9c=darkyellow,
   fr0c=darkgray]

\installcorenamespace {simulatewords}

\installcommandhandler \??simulatewords {simulatewords} \??simulatewords

\setupsimulatewords
  [\c!n       =\plusten,
   \c!m       =40,
   \c!min     =\plusone,
   \c!max     =\plusfive,
   \c!minwidth=0.50\emwidth,
   \c!maxwidth=1.25\emwidth,
   \c!color   =\empty,
   \c!hyphen  =\v!yes,
   \c!distance=-.2\onebasepoint]

\protected\def\dosimulatedword
  {\clf_fakeword
      factor \exheight
      name   {\p_line}
      min    \scratchdimenone
      max    \scratchdimentwo
   \relax}

\def\simulatedhyphen
  {\kern.2\points-\kern.2\points}

%tolerant\protected\def\simulatewords[#1]#*[#2]%
\tolerant\protected\def\simulatewords[#1]#,[#2]% keep space
  {\begingroup
   \ifparameter#2\or
     \edef\currentsimulatewords{#1}%
     \setupcurrentsimulatewords[#2]%
   \orelse\ifhastok={#1}%
     \lettonothing\currentsimulatewords
     \setupcurrentsimulatewords[#1]%
   \else
     \lettonothing\currentsimulatewords
   \fi
   \dontleavehmode
   \begstrut
   \scratchcounterone\simulatewordsparameter\c!n
   \scratchcountertwo\simulatewordsparameter\c!m
   \scratchmin       \simulatewordsparameter\c!min
   \scratchmax       \simulatewordsparameter\c!max
   \scratchdimenone  \simulatewordsparameter\c!minwidth
   \scratchdimentwo  \simulatewordsparameter\c!maxwidth
   \scratchdimenthree\simulatewordsparameter\c!distance
   \edef\p_random{\simulatewordsparameter\c!random}%
   \edef\p_color {\simulatewordsparameter\c!color}%
   \edef\p_line  {\simulatewordsparameter\c!line}%
   \edef\p_hyphen{\simulatewordsparameter\c!hyphen}%
   \edef\p_line  {fake:\ifx\p_line\v!yes word\else rule\fi}%
   \ifx\p_color\v!text
     % no need
   \else
     \setuppalet[fakerule]%
     \ifempty\p_color
       \directcolored[fakerulecolor]%
     \orelse\ifx\p_color\v!auto
       % different
     \else
       \directcolored[\p_color]%
     \fi
   \fi
   \edef\p_hyphen
     {\ifx\p_hyphen\v!yes
        \discretionary{\bf\simulatedhyphen}{}{\kern\scratchdimenthree}%
      \orelse\ifx\p_hyphen\v!margin
        \discretionary{\hpack to \zeropoint{\bf\simulatedhyphen\hss}}{}{\kern\scratchdimenthree}%
      \orelse\ifx\p_hyphen\v!none
        \allowbreak\kern\scratchdimenthree
      \orelse\ifx\p_hyphen\v!no
        \allowbreak\kern\scratchdimenthree
      \orelse\ifx\p_hyphen\v!both
        \discretionary{\bf\simulatedhyphen}{\bf\simulatedhyphen}{\kern\scratchdimenthree}%
      \orelse\ifempty\p_hyphen
        \allowbreak\kern\scratchdimenthree
      \else
        \discretionary{\p_hyphen}{}{\kern\scratchdimenthree}%
      \fi}%
   \def\truncated##1##2\relax{##1}%
   % trialtypesetting: tricky
   \ifempty\p_random\else
     \pushrandomseed
     \setrandomseed\p_random
   \fi
   \localcontrolledloop \plusone \randomnumber\scratchcounterone\scratchcountertwo \plusone {%
     \let\dosimulatedhyphen\relax
     \localcontrolledloop \plusone \randomnumber\scratchmin\scratchmax \plusone {%
       \dosimulatedhyphen
       \begingroup
         \ifx\p_color\v!auto
           \directcolored[fr\expandafter\truncated\the\currentloopiterator\relax c]%
         \fi
         \dosimulatedword
       \endgroup
       \let\dosimulatedhyphen\p_hyphen
     }%
     \relax
     \space
   }%
   \ifempty\p_random\else
     \poprandomseed
   \fi
   \removeunwantedspaces
   \endstrut
   \endgroup}

%D For old times sake:

\protected\def\fakewords#1#2%
  {\simulatewords
     [\c!n=#1,%
      \c!m=#2,%
      \c!min=\plusone,%
      \c!max=\plusfive,%
      \c!hyphen=\v!none,%
      \c!color=\v!text,%
      \c!line=\v!yes]}

%D Lines

\tolerant\protected\def\fakeline[#1]%
  {\par
   \dontleavehmode
   \hpack\bgroup
     \scratchwidth \availablehsize
     \scratchheight.8\strutht
     \scratchdepth .8\strutdp
     \ifparameter#1\or
       \letsimulatewordsparameter\c!width \scratchwidth  % no need for _reference here
       \letsimulatewordsparameter\c!height\scratchheight % idem
       \letsimulatewordsparameter\c!depth \scratchdepth  % idem
       \setupcurrentsimulatewords[#1]%
       \scratchwidth \simulatewordsparameter\c!width
       \scratchheight\simulatewordsparameter\c!height
       \scratchdepth \simulatewordsparameter\c!depth
     \fi
     \edef\p_color{\simulatewordsparameter\c!color}%
     \begingroup
     \ifempty\p_color
       \directcolored[fakerulecolor]%
     \orelse\ifx\p_color\v!auto
       % text color
     \else
       \directcolored[\p_color]%
     \fi
     \vrule
        \s!width \scratchwidth
        \s!height\scratchheight
        \s!depth \scratchdepth
     \endgroup
     \hskip-\scratchwidth
     \directcolored[\s!white]%
     \scratchdimen.5\onepoint
     \vrule
        \s!width \scratchwidth
        \s!height\scratchdimen
        \s!depth \scratchdimen
   \egroup
   \par}

\definesimulatewords[math][\c!n=1,\c!m=3]

\tolerant\protected\def\fakemath[#1]#*[#2]%
  {\begingroup
   \ifmmode
     \let\simulate_end_math\relax
   \else
     \startimath
     \let\simulate_end_math\stopimath
   \fi
     \scratchdistance\glyphyscaled\Umathaxis\mathstyle
     \scratchheight2.5\scratchdistance
     \scratchdepth 0.5\scratchdistance
     \getrandomdimen\scratchwidth{\simulatewordsparameter\c!minwidth}{\simulatewordsparameter\c!maxwidth}%
     \ifparameter#2\or
       \cdef\currentsimulatewords{#1}%
       \letsimulatewordsparameter\c!width \scratchwidth  % no need for _reference here
       \letsimulatewordsparameter\c!height\scratchheight % idem
       \letsimulatewordsparameter\c!depth \scratchdepth  % idem
       \setupcurrentsimulatewords[#2]%
     \orelse\ifhastok={#1}%
       \cdef\currentsimulatewords{math}%
       \letsimulatewordsparameter\c!width \scratchwidth  % no need for _reference here
       \letsimulatewordsparameter\c!height\scratchheight % idem
       \letsimulatewordsparameter\c!depth \scratchdepth  % idem
       \setupcurrentsimulatewords[#1]%
     \else
       \cdef\currentsimulatewords{math}%
       \letsimulatewordsparameter\c!width \scratchwidth  % no need for _reference here
       \letsimulatewordsparameter\c!height\scratchheight % idem
       \letsimulatewordsparameter\c!depth \scratchdepth  % idem
     \fi
     \scratchwidth \simulatewordsparameter\c!width
     \scratchheight\simulatewordsparameter\c!height
     \scratchdepth \simulatewordsparameter\c!depth
     \edef\p_color{\simulatewordsparameter\c!color}%
     \edef\p_random{\simulatewordsparameter\c!random}%
     \ifempty\p_random\else
       \pushrandomseed
       \setrandomseed\p_random
     \fi
     \mathord{%
       \begingroup
       \kern.1\emwidth
       \ifempty\p_color
         \directcolored[fakerulecolor]%
       \orelse\ifx\p_color\v!auto
         % text color
       \else
         \directcolored[\p_color]%
       \fi
       \vrule
          \s!width \scratchwidth
          \s!height\scratchheight
          \s!depth \scratchdepth
       \endgroup
       \hskip-\scratchwidth
       \directcolored[\s!white]%
       \scratchdimen.5\onepoint
       \vrule
          \s!width  \scratchwidth
          \s!height \scratchdimen
          \s!depth  \scratchdimen
          \s!yoffset\scratchdistance
     % \hskip-\scratchwidth
     % \divideby\scratchdimen\plustwo
     % \vrule
     %    \s!width  \scratchwidth
     %    \s!height \scratchdimen
     %    \s!depth  \scratchdimen
       \kern.1\emwidth
     }%
     \ifempty\p_random\else
       \poprandomseed
     \fi
   \simulate_end_math
   \endgroup}

\tolerant\protected\def\fakeinlinemath[#1]#*[#2]%
  {\begingroup
   \ifmmode
     \let\simulate_end_math\relax
   \else
     \startimath
     \let\simulate_end_math\stopimath
   \fi
   \ifparameter#2\or
     \edef\currentsimulatewords{#1}%
     \setupcurrentsimulatewords[#2]%
   \orelse\ifhastok={#1}%
     \cdef\currentsimulatewords{math}%
     \setupcurrentsimulatewords[#1]%
   \orelse\ifchknum#1\or
     \cdef\currentsimulatewords{math}%
     \setsimulatewordsparameter\c!n{#1}%
     \setsimulatewordsparameter\c!m{#1}%
   \else
     \cdef\currentsimulatewords{math}%
   \fi
   \scratchcounterone\simulatewordsparameter\c!n
   \scratchcountertwo\simulatewordsparameter\c!m
   \scratchcounterthree
     \ifnum\scratchcounterone=\scratchcountertwo
       \scratchcounterone
     \else
       \randomnumber\scratchcounterone\scratchcountertwo\relax
     \fi
   \localcontrolledloop \plusone \scratchcounterthree \plusone {%
      \fakemath[\currentsimulatewords][\c!random=]\relax
      \ifnum\currentloopiterator<\scratchcounterthree
        +%
      \fi
   }%
   \simulate_end_math
   \endgroup}

\tolerant\protected\def\fakedisplaymath[#1]#*[#2]%
  {\begingroup
   \ifmmode
     \let\simulate_end_math\relax
   \else
     \startimath
     \let\simulate_end_math\stopimath
   \fi
   \begingroup
   \forcedisplaymath
   \ifparameter#2\or
     \edef\currentsimulatewords{#1}%
     \setupcurrentsimulatewords[#2]%
   \orelse\ifhastok={#1}%
     \cdef\currentsimulatewords{math}%
     \setupcurrentsimulatewords[#1]%
   \else
     \cdef\currentsimulatewords{math}%
   \fi
   \scratchcounterone\simulatewordsparameter\c!n
   \scratchcountertwo\simulatewordsparameter\c!m
   \scratchcounterthree\randomnumber\scratchcounterone\scratchcountertwo\relax
   \localcontrolledloop \plusone \scratchcounterthree \plusone {%
      \fakemath[\currentsimulatewords][\c!random=]\relax
      \ifnum\currentloopiterator<\scratchcounterthree
        +%
      \fi
   }%
   \endgroup
   \simulate_end_math
   \endgroup}

\protect

\continueifinputfile{m-simulate.mkxl}

\starttext

\showframe

    \dorecurse{5}{\fakemath[color=red]\ and } \fakemath

    \dorecurse{5}{\fakeinlinemath[color=red]\ and } \fakeinlinemath

    \im { \fakemath[m=1]^{\fakemath} }

    \def\Snip{\fakeinlinemath}

    \im { \Snip[1]^{\Snip[1]} + \Snip[2] = \Snip[2] + \int \Snip[2]}

    \im { \Snip[1]^{\Snip} + \Snip = \Snip + \int \Snip}

    \im { \Snip[3]^{\Snip[5]} + \Snip = \Snip + \int \Snip}

    \page
    \simulatewords[n=50,m=100,min=1,max=5,color=auto,hyphen=margin,line=yes]\par
    \simulatewords[n=50,m=100,min=3,max=9,color=auto,hyphen=margin,line=yes]\par
    \simulatewords[n=50,m=100,min=3,max=9,color=auto,hyphen=margin,line=yes]\par
    \page
    \simulatewords[n=50,m=100,min=2,max=6,hyphen=yes]\par
    \simulatewords[n=50,m=100,min=2,max=6,hyphen=both]\par
    \simulatewords[n=50,m=100,min=2,max=6,hyphen=both]\par

    \startcolor[red]
    \simulatewords[n=50,m=100,min=2,max=6,color=text]\par
    \stopcolor

    \definesimulatewords[foo][n=50,m=100,min=2,max=6,color=text]

    \startcolor[green]
    \simulatewords[foo][color=darkyellow,random=1234]
    \stopcolor

    \page

    \fakeline

    \startnarrower
        \fakeline
    \stopnarrower

\stoptext