m-simulate.mkxl /size: 11 Kb    last modification: 2025-02-21 11:03
1%D \module
2%D   [       file=m-simulate,
3%D        version=2016.02.18, % follow up on m-visual of 2000.01.10
4%D          title=\CONTEXT\ Extra Modules,
5%D       subtitle=Visualization and Faking Redone,
6%D         author={Hans Hagen \& Ton Otten},
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\unprotect
15
16\definepalet
17  [fakerule]
18  [fr1c=darkred,
19   fr2c=darkgreen,
20   fr3c=darkblue,
21   fr4c=darkyellow,
22   fr5c=darkgray,
23   fr6c=darkred,
24   fr7c=darkgreen,
25   fr8c=darkblue,
26   fr9c=darkyellow,
27   fr0c=darkgray]
28
29\installcorenamespace {simulatewords}
30
31\installcommandhandler \??simulatewords {simulatewords} \??simulatewords
32
33\setupsimulatewords
34  [\c!n       =\plusten,
35   \c!m       =40,
36   \c!min     =\plusone,
37   \c!max     =\plusfive,
38   \c!minwidth=0.50\emwidth,
39   \c!maxwidth=1.25\emwidth,
40   \c!color   =\empty,
41   \c!hyphen  =\v!yes,
42   \c!distance=-.2\onebasepoint]
43
44\protected\def\dosimulatedword
45  {\clf_fakeword
46      factor \exheight
47      name   {\p_line}
48      min    \scratchdimenone
49      max    \scratchdimentwo
50   \relax}
51
52\def\simulatedhyphen
53  {\kern.2\points-\kern.2\points}
54
55%tolerant\protected\def\simulatewords[#1]#*[#2]%
56\tolerant\protected\def\simulatewords[#1]#,[#2]% keep space
57  {\begingroup
58   \ifparameter#2\or
59     \edef\currentsimulatewords{#1}%
60     \setupcurrentsimulatewords[#2]%
61   \orelse\ifhastok={#1}%
62     \lettonothing\currentsimulatewords
63     \setupcurrentsimulatewords[#1]%
64   \else
65     \lettonothing\currentsimulatewords
66   \fi
67   \dontleavehmode
68   \begstrut
69   \scratchcounterone\simulatewordsparameter\c!n
70   \scratchcountertwo\simulatewordsparameter\c!m
71   \scratchmin       \simulatewordsparameter\c!min
72   \scratchmax       \simulatewordsparameter\c!max
73   \scratchdimenone  \simulatewordsparameter\c!minwidth
74   \scratchdimentwo  \simulatewordsparameter\c!maxwidth
75   \scratchdimenthree\simulatewordsparameter\c!distance
76   \edef\p_random{\simulatewordsparameter\c!random}%
77   \edef\p_color {\simulatewordsparameter\c!color}%
78   \edef\p_line  {\simulatewordsparameter\c!line}%
79   \edef\p_hyphen{\simulatewordsparameter\c!hyphen}%
80   \edef\p_line  {fake:\ifx\p_line\v!yes word\else rule\fi}%
81   \ifx\p_color\v!text
82     % no need
83   \else
84     \setuppalet[fakerule]%
85     \ifempty\p_color
86       \directcolored[fakerulecolor]%
87     \orelse\ifx\p_color\v!auto
88       % different
89     \else
90       \directcolored[\p_color]%
91     \fi
92   \fi
93   \edef\p_hyphen
94     {\ifx\p_hyphen\v!yes
95        \discretionary{\bf\simulatedhyphen}{}{\kern\scratchdimenthree}%
96      \orelse\ifx\p_hyphen\v!margin
97        \discretionary{\hpack to \zeropoint{\bf\simulatedhyphen\hss}}{}{\kern\scratchdimenthree}%
98      \orelse\ifx\p_hyphen\v!none
99        \allowbreak\kern\scratchdimenthree
100      \orelse\ifx\p_hyphen\v!no
101        \allowbreak\kern\scratchdimenthree
102      \orelse\ifx\p_hyphen\v!both
103        \discretionary{\bf\simulatedhyphen}{\bf\simulatedhyphen}{\kern\scratchdimenthree}%
104      \orelse\ifempty\p_hyphen
105        \allowbreak\kern\scratchdimenthree
106      \else
107        \discretionary{\p_hyphen}{}{\kern\scratchdimenthree}%
108      \fi}%
109   \def\truncated##1##2\relax{##1}%
110   % trialtypesetting: tricky
111   \ifempty\p_random\else
112     \pushrandomseed
113     \setrandomseed\p_random
114   \fi
115   \localcontrolledloop \plusone \randomnumber\scratchcounterone\scratchcountertwo \plusone {%
116     \let\dosimulatedhyphen\relax
117     \localcontrolledloop \plusone \randomnumber\scratchmin\scratchmax \plusone {%
118       \dosimulatedhyphen
119       \begingroup
120         \ifx\p_color\v!auto
121           \directcolored[fr\expandafter\truncated\the\currentloopiterator\relax c]%
122         \fi
123         \dosimulatedword
124       \endgroup
125       \let\dosimulatedhyphen\p_hyphen
126     }%
127     \relax
128     \space
129   }%
130   \ifempty\p_random\else
131     \poprandomseed
132   \fi
133   \removeunwantedspaces
134   \endstrut
135   \endgroup}
136
137%D For old times sake:
138
139\protected\def\fakewords#1#2%
140  {\simulatewords
141     [\c!n=#1,%
142      \c!m=#2,%
143      \c!min=\plusone,%
144      \c!max=\plusfive,%
145      \c!hyphen=\v!none,%
146      \c!color=\v!text,%
147      \c!line=\v!yes]}
148
149%D Lines
150
151\tolerant\protected\def\fakeline[#1]%
152  {\par
153   \dontleavehmode
154   \hpack\bgroup
155     \scratchwidth \availablehsize
156     \scratchheight.8\strutht
157     \scratchdepth .8\strutdp
158     \ifparameter#1\or
159       \letsimulatewordsparameter\c!width \scratchwidth  % no need for _reference here
160       \letsimulatewordsparameter\c!height\scratchheight % idem
161       \letsimulatewordsparameter\c!depth \scratchdepth  % idem
162       \setupcurrentsimulatewords[#1]%
163       \scratchwidth \simulatewordsparameter\c!width
164       \scratchheight\simulatewordsparameter\c!height
165       \scratchdepth \simulatewordsparameter\c!depth
166     \fi
167     \edef\p_color{\simulatewordsparameter\c!color}%
168     \begingroup
169     \ifempty\p_color
170       \directcolored[fakerulecolor]%
171     \orelse\ifx\p_color\v!auto
172       % text color
173     \else
174       \directcolored[\p_color]%
175     \fi
176     \vrule
177        \s!width \scratchwidth
178        \s!height\scratchheight
179        \s!depth \scratchdepth
180     \endgroup
181     \hskip-\scratchwidth
182     \directcolored[\s!white]%
183     \scratchdimen.5\onepoint
184     \vrule
185        \s!width \scratchwidth
186        \s!height\scratchdimen
187        \s!depth \scratchdimen
188   \egroup
189   \par}
190
191\definesimulatewords[math][\c!n=1,\c!m=3]
192
193\tolerant\protected\def\fakemath[#1]#*[#2]%
194  {\begingroup
195   \ifmmode
196     \let\simulate_end_math\relax
197   \else
198     \startimath
199     \let\simulate_end_math\stopimath
200   \fi
201     \scratchdistance\glyphyscaled\Umathaxis\mathstyle
202     \scratchheight2.5\scratchdistance
203     \scratchdepth 0.5\scratchdistance
204     \getrandomdimen\scratchwidth{\simulatewordsparameter\c!minwidth}{\simulatewordsparameter\c!maxwidth}%
205     \ifparameter#2\or
206       \cdef\currentsimulatewords{#1}%
207       \letsimulatewordsparameter\c!width \scratchwidth  % no need for _reference here
208       \letsimulatewordsparameter\c!height\scratchheight % idem
209       \letsimulatewordsparameter\c!depth \scratchdepth  % idem
210       \setupcurrentsimulatewords[#2]%
211     \orelse\ifhastok={#1}%
212       \cdef\currentsimulatewords{math}%
213       \letsimulatewordsparameter\c!width \scratchwidth  % no need for _reference here
214       \letsimulatewordsparameter\c!height\scratchheight % idem
215       \letsimulatewordsparameter\c!depth \scratchdepth  % idem
216       \setupcurrentsimulatewords[#1]%
217     \else
218       \cdef\currentsimulatewords{math}%
219       \letsimulatewordsparameter\c!width \scratchwidth  % no need for _reference here
220       \letsimulatewordsparameter\c!height\scratchheight % idem
221       \letsimulatewordsparameter\c!depth \scratchdepth  % idem
222     \fi
223     \scratchwidth \simulatewordsparameter\c!width
224     \scratchheight\simulatewordsparameter\c!height
225     \scratchdepth \simulatewordsparameter\c!depth
226     \edef\p_color{\simulatewordsparameter\c!color}%
227     \edef\p_random{\simulatewordsparameter\c!random}%
228     \ifempty\p_random\else
229       \pushrandomseed
230       \setrandomseed\p_random
231     \fi
232     \mathord{%
233       \begingroup
234       \kern.1\emwidth
235       \ifempty\p_color
236         \directcolored[fakerulecolor]%
237       \orelse\ifx\p_color\v!auto
238         % text color
239       \else
240         \directcolored[\p_color]%
241       \fi
242       \vrule
243          \s!width \scratchwidth
244          \s!height\scratchheight
245          \s!depth \scratchdepth
246       \endgroup
247       \hskip-\scratchwidth
248       \directcolored[\s!white]%
249       \scratchdimen.5\onepoint
250       \vrule
251          \s!width  \scratchwidth
252          \s!height \scratchdimen
253          \s!depth  \scratchdimen
254          \s!yoffset\scratchdistance
255     % \hskip-\scratchwidth
256     % \divideby\scratchdimen\plustwo
257     % \vrule
258     %    \s!width  \scratchwidth
259     %    \s!height \scratchdimen
260     %    \s!depth  \scratchdimen
261       \kern.1\emwidth
262     }%
263     \ifempty\p_random\else
264       \poprandomseed
265     \fi
266   \simulate_end_math
267   \endgroup}
268
269\tolerant\protected\def\fakeinlinemath[#1]#*[#2]%
270  {\begingroup
271   \ifmmode
272     \let\simulate_end_math\relax
273   \else
274     \startimath
275     \let\simulate_end_math\stopimath
276   \fi
277   \ifparameter#2\or
278     \edef\currentsimulatewords{#1}%
279     \setupcurrentsimulatewords[#2]%
280   \orelse\ifhastok={#1}%
281     \cdef\currentsimulatewords{math}%
282     \setupcurrentsimulatewords[#1]%
283   \orelse\ifchknum#1\or
284     \cdef\currentsimulatewords{math}%
285     \setsimulatewordsparameter\c!n{#1}%
286     \setsimulatewordsparameter\c!m{#1}%
287   \else
288     \cdef\currentsimulatewords{math}%
289   \fi
290   \scratchcounterone\simulatewordsparameter\c!n
291   \scratchcountertwo\simulatewordsparameter\c!m
292   \scratchcounterthree
293     \ifnum\scratchcounterone=\scratchcountertwo
294       \scratchcounterone
295     \else
296       \randomnumber\scratchcounterone\scratchcountertwo\relax
297     \fi
298   \localcontrolledloop \plusone \scratchcounterthree \plusone {%
299      \fakemath[\currentsimulatewords][\c!random=]\relax
300      \ifnum\currentloopiterator<\scratchcounterthree
301        +%
302      \fi
303   }%
304   \simulate_end_math
305   \endgroup}
306
307\tolerant\protected\def\fakedisplaymath[#1]#*[#2]%
308  {\begingroup
309   \ifmmode
310     \let\simulate_end_math\relax
311   \else
312     \startimath
313     \let\simulate_end_math\stopimath
314   \fi
315   \begingroup
316   \forcedisplaymath
317   \ifparameter#2\or
318     \edef\currentsimulatewords{#1}%
319     \setupcurrentsimulatewords[#2]%
320   \orelse\ifhastok={#1}%
321     \cdef\currentsimulatewords{math}%
322     \setupcurrentsimulatewords[#1]%
323   \else
324     \cdef\currentsimulatewords{math}%
325   \fi
326   \scratchcounterone\simulatewordsparameter\c!n
327   \scratchcountertwo\simulatewordsparameter\c!m
328   \scratchcounterthree\randomnumber\scratchcounterone\scratchcountertwo\relax
329   \localcontrolledloop \plusone \scratchcounterthree \plusone {%
330      \fakemath[\currentsimulatewords][\c!random=]\relax
331      \ifnum\currentloopiterator<\scratchcounterthree
332        +%
333      \fi
334   }%
335   \endgroup
336   \simulate_end_math
337   \endgroup}
338
339\protect
340
341\continueifinputfile{m-simulate.mkxl}
342
343\starttext
344
345\showframe
346
347    \dorecurse{5}{\fakemath[color=red]\ and } \fakemath
348
349    \dorecurse{5}{\fakeinlinemath[color=red]\ and } \fakeinlinemath
350
351    \im { \fakemath[m=1]^{\fakemath} }
352
353    \def\Snip{\fakeinlinemath}
354
355    \im { \Snip[1]^{\Snip[1]} + \Snip[2] = \Snip[2] + \int \Snip[2]}
356
357    \im { \Snip[1]^{\Snip} + \Snip = \Snip + \int \Snip}
358
359    \im { \Snip[3]^{\Snip[5]} + \Snip = \Snip + \int \Snip}
360
361    \page
362    \simulatewords[n=50,m=100,min=1,max=5,color=auto,hyphen=margin,line=yes]\par
363    \simulatewords[n=50,m=100,min=3,max=9,color=auto,hyphen=margin,line=yes]\par
364    \simulatewords[n=50,m=100,min=3,max=9,color=auto,hyphen=margin,line=yes]\par
365    \page
366    \simulatewords[n=50,m=100,min=2,max=6,hyphen=yes]\par
367    \simulatewords[n=50,m=100,min=2,max=6,hyphen=both]\par
368    \simulatewords[n=50,m=100,min=2,max=6,hyphen=both]\par
369
370    \startcolor[red]
371    \simulatewords[n=50,m=100,min=2,max=6,color=text]\par
372    \stopcolor
373
374    \definesimulatewords[foo][n=50,m=100,min=2,max=6,color=text]
375
376    \startcolor[green]
377    \simulatewords[foo][color=darkyellow,random=1234]
378    \stopcolor
379
380    \page
381
382    \fakeline
383
384    \startnarrower
385        \fakeline
386    \stopnarrower
387
388\stoptext
389