typo-ada.mkxl /size: 7061 b    last modification: 2024-01-16 10:22
1%D \module
2%D   [       file=typo-ada,
3%D        version=2022.04.04,
4%D          title=\CONTEXT\ Typesetting Macros,
5%D       subtitle=Adaptive,
6%D         author=Hans Hagen,
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\writestatus{loading}{ConTeXt Typesetting Macros / Adaptive}
15
16% Musical timestamp: the postponed by COVID concert of Gazpacho in Zwolle NL: a joy
17% to see dedicated and talented musicians perform their work in perfection and as a
18% well balanced and equiped team.
19
20\unprotect
21
22\ifdefined\d_adaptive_width \else
23    \newdimension\d_adaptive_width
24    \newdimension\d_adaptive_height
25    \newdimension\d_adaptive_depth
26    \newdimension\d_adaptive_line
27    \newdimension\d_adaptive_hsize
28    \newbox      \b_adaptive_box
29\fi
30
31\aliasdimension\usedadaptivewidth \d_adaptive_width
32\aliasdimension\usedadaptiveheight\d_adaptive_height
33\aliasdimension\usedadaptivedepth \d_adaptive_depth
34\aliasdimension\usedadaptiveline  \d_adaptive_line
35\aliasdimension\usedadaptivehsize \d_adaptive_hsize
36
37\registerctxluafile{typo-ada}{autosuffix}
38
39\aliased\let\usedadaptivebox\b_adaptive_box
40
41\mutable\lettonothing\m_adaptive_color
42\mutable\lettonothing\m_adaptive_alternative
43
44\permanent\def\usedadaptivecolor      {\m_adaptive_color}
45\permanent\def\usedadaptivealternative{\m_adaptive_alternative}
46
47\protected\untraced\def\usedadaptivetotal{\dimexpr\usedadaptiveheight+\usedadaptivedepth\relax}
48
49\definesystemattribute[adaptive][public]
50
51\installcorenamespace{adaptive}
52
53\installcommandhandler \??adaptive {adaptive} \??adaptive
54
55% \appendtoks
56%     %
57% \to \everydefineadaptive
58
59\setupadaptive
60  [\c!foregroundstyle=,
61   \c!foregroundcolor=,
62   \c!color=gray,
63   \c!alternative=,
64   \c!rulethickness=\linewidth,
65   \c!method=1,
66   \c!stretch=,
67   \c!shrink=,
68  %\c!strut=\v!no,
69   \c!margin=2\emwidth]
70
71\permanent\protected\def\adaptivehbox{\typo_adaptive_box\zerocount}
72\permanent\protected\def\adaptivevbox{\typo_adaptive_box\plusone  }
73
74\aliased\let\adaptivebox\adaptivehbox
75
76\tolerant\def\typo_adaptive_box#1#*[#S#2]#*[#S#3]%
77   {\ifcase#1\dontleavehmode\fi
78    \begingroup
79    \ifhastok={#2}%
80      \lettonothing\currentadaptive
81      \setupcurrentadaptive[#2]%
82    \else
83      \cdef\currentadaptive{#2}%
84      \setupcurrentadaptive[#3]%
85    \fi
86    \edef\p_setups{\adaptiveparameter\c!setups}%
87    \dowithnextboxcontent
88      {\useadaptivestyleandcolor\c!foregroundstyle\c!foregroundcolor
89       \ifcstok{\adaptiveparameter\c!strut}\v!yes
90         \ifcase#1%
91           \strut
92         \else
93            % maybe \begstrut .. \endstrut
94         \fi
95       \fi}
96      {\ifempty\p_setups
97         % there is no need for postprocessing
98       \else
99         \clf_setadaptive {
100           method         \adaptiveparameter\c!method
101           setups        {\p_setups}%
102           color         {\adaptiveparameter\c!color}%
103           alternative   {\adaptiveparameter\c!alternative}%
104           rulethickness  \adaptiveparameter\c!rulethickness
105         }%
106       \fi
107       \scratchdimen\adaptiveparameter\c!margin\relax
108       \edef\p_stretch{\adaptiveparameter\c!stretch}%
109       \edef\p_shrink {\adaptiveparameter\c!shrink}%
110       \uleaders
111         \s!callback \numexpr \adaptiveattribute \relax
112         \ifcase#1%
113           \hbox
114             \s!spread \scratchdimen
115             \bgroup
116               \hss\box\nextbox\hss
117             \egroup
118           \hskip
119         \else
120           \vbox
121             \s!spread \scratchdimen
122             \bgroup
123               \vss\box\nextbox\vss
124             \egroup
125           \vskip
126         \fi
127           \zeropoint
128           \s!plus  \ifempty\p_stretch\scratchdimen\else\p_stretch\fi
129           \s!minus \ifempty\p_shrink \scratchdimen\else\p_shrink \fi
130         \endgroup}%
131    \ifcase#1\hbox\else\vbox\fi}
132
133%D \startbuffer
134%D y\adaptive\hbox to5cm{x\rightarrowfill x}y\par
135%D y\adaptive\hbox to5cm{x\leftarrowfill  x}y\par
136%D \stopbuffer
137%D
138%D \typebuffer \startlinecorrection\getbuffer\stoplinecorrection
139
140\permanent\protected\def\adaptive
141  {\dowithnextbox{\adaptivecheckbox\nextbox\box\nextbox}}
142
143%D Here is a test case, expect more:
144%D
145%D \starttyping
146%D \startMPdefinitions
147%D     def TickTock =
148%D         interim linecap := squared;
149%D         save p ; path p ; p := fullsquare xysized(AdaptiveWidth,.9(AdaptiveHeight+AdaptiveDepth)) ;
150%D         fill p withcolor AdaptiveColor ;
151%D         draw bottomboundary (p enlarged (-AdaptiveThickness) )
152%D             withdashes (3*AdaptiveThickness)
153%D             withpen pencircle scaled AdaptiveThickness
154%D             withcolor white ;
155%D     enddef ;
156%D \stopMPdefinitions
157%D
158%D \startsetups adaptive:test
159%D     \setbox\usedadaptivebox\hbox to \usedadaptivewidth yoffset -.9\usedadaptivedepth \bgroup
160%D         \hss
161%D         \startMPcode
162%D             TickTock ;
163%D         \stopMPcode
164%D         \hss
165%D    \egroup
166%D \stopsetups
167%D
168%D \definecolor[adaptive:tick][.25(blue,green)]
169%D \definecolor[adaptive:tock][.75(blue,green)]
170%D
171%D \defineadaptive
172%D   [tick]
173%D   [setups=adaptive:test,
174%D    color=adaptive:tick,
175%D    foregroundcolor=white,
176%D    foregroundstyle=\infofont,
177%D    strut=yes]
178%D
179%D \defineadaptive
180%D   [tock]
181%D   [tick]
182%D   [color=adaptive:tock]
183%D
184%D \dorecurse{5}{
185%D     \dostepwiserecurse{1}{150}{1}{%
186%D         this~#1.##1 is~#1.##1 test~#1.##1
187%D         \ifodd##1\relax
188%D             \adaptivebox[tick]{\hss tick #1.##1\hss}
189%D         \else
190%D             \adaptivebox[tock]{\hss tock #1.##1\hss}
191%D         \fi
192%D     }
193%D     \blank
194%D }
195%D
196%D \setupexternalfigures
197%D   [location={default,global}]
198%D
199%D \startsetups adaptive:test
200%D     \setbox\usedadaptivebox\hbox to \usedadaptivewidth yoffset -\usedadaptivedepth \bgroup
201%D         \externalfigure
202%D           [cow.pdf]
203%D           [width=\usedadaptivewidth,
204%D            height=\dimexpr\usedadaptiveheight+\usedadaptivedepth\relax]%
205%D    \egroup
206%D \stopsetups
207%D
208%D \startsetups adaptive:test
209%D     \setbox\usedadaptivebox\hbox to \usedadaptivewidth yoffset .25ex \bgroup
210%D         \startMPcode
211%D             drawarrow ((0,0) -- (\the\usedadaptivewidth,0)) randomized (ExHeight/2);
212%D         \stopMPcode
213%D    \egroup
214%D \stopsetups
215%D
216%D \dorecurse{5}{
217%D     \dostepwiserecurse{1}{150}{1}{%
218%D         this~#1.##1 is~#1.##1 test~#1.##1
219%D         {\adaptivebox[strut=yes,setups=adaptive:test]{}}
220%D     }
221%D     \blank
222%D }
223%D
224%D \page
225%D
226%D \startsetups adaptive:test
227%D     \setbox\usedadaptivebox\vbox to \usedadaptivetotal \bgroup
228%D         \externalfigure
229%D           [cow.pdf]
230%D           [width=\usedadaptivewidth,
231%D            height=\usedadaptivetotal]%
232%D    \egroup
233%D \stopsetups
234%D
235%D \ruledvbox to \textheight {
236%D     \par \begstrut \samplefile{tufte} \endstrut \par
237%D     \adaptivevbox[strut=yes,setups=adaptive:test]{\hsize\textwidth\hss}
238%D     \par \begstrut \samplefile{tufte} \endstrut
239%D }
240%D
241%D \stoptyping
242
243\protect
244