typo-ada.mkxl /size: 8691 b    last modification: 2025-02-21 11:03
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\permanent\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!width=8\lineheight,  % default same as external figure
69  %\c!height=6\lineheight, % default same as external figure
70  %\c!strut=\v!no,
71   \c!margin=2\emwidth]
72
73\permanent\protected\def\adaptivehbox{\typo_adaptive_box\zerocount}
74\permanent\protected\def\adaptivevbox{\typo_adaptive_box\plusone  }
75
76\aliased\let\adaptivebox\adaptivehbox
77
78\tolerant\def\typo_adaptive_box#1#*[#S#2]#*[#S#3]%
79   {\ifcase#1\dontleavehmode\fi
80    \begingroup
81    \ifhastok={#2}%
82      \lettonothing\currentadaptive
83      \setupcurrentadaptive[#2]%
84    \else
85      \cdef\currentadaptive{#2}%
86      \setupcurrentadaptive[#3]%
87    \fi
88    \edef\p_setups{\adaptiveparameter\c!setups}%
89    \dowithnextboxcontent
90      {\useadaptivestyleandcolor\c!foregroundstyle\c!foregroundcolor
91       \ifcstok{\adaptiveparameter\c!strut}\v!yes
92         \ifcase#1%
93           \strut
94         \else
95            % maybe \begstrut .. \endstrut
96         \fi
97       \fi}
98      {\ifempty\p_setups
99         % there is no need for postprocessing
100       \else
101         \clf_setadaptive {
102           method         \adaptiveparameter\c!method
103           setups        {\p_setups}%
104           location      {\adaptiveparameter\c!location}%
105           color         {\adaptiveparameter\c!color}%
106           alternative   {\adaptiveparameter\c!alternative}%
107           rulethickness {\adaptiveparameter\c!rulethickness}%
108         }%
109       \fi
110       \scratchdimen{\adaptiveparameter\c!margin}%
111       % maybe when method == 2 then no \hss
112       \uleaders
113         \s!callback {\adaptiveattribute}%
114         \ifcase#1%
115           \hbox
116             \s!spread \scratchdimen
117             \bgroup
118               \hss\box\nextbox\hss
119             \egroup
120           \hskip
121         \else
122           \vbox
123             \s!spread \scratchdimen
124             \bgroup
125               \vss\box\nextbox\vss
126             \egroup
127           \vskip
128         \fi
129           \zeropoint
130           \s!plus  \ifchkdimexpr\adaptiveparameter\c!stretch\or\lastchkdimension\else\scratchdimen\fi
131           \s!minus \ifchkdimexpr\adaptiveparameter\c!shrink \or\lastchkdimension\else\scratchdimen\fi
132         \endgroup}%
133    \ifcase#1\hbox\else\vbox\fi}
134
135%D \startbuffer
136%D y\adaptive\hbox to5cm{x\rightarrowfill x}y\par
137%D y\adaptive\hbox to5cm{x\leftarrowfill  x}y\par
138%D \stopbuffer
139%D
140%D \typebuffer \startlinecorrection\getbuffer\stoplinecorrection
141
142\permanent\protected\def\adaptive
143  {\dowithnextbox{\adaptivecheckbox\nextbox\box\nextbox}}
144
145%D Here is a test case, expect more:
146%D
147%D \starttyping
148%D \startMPdefinitions
149%D     def TickTock =
150%D         interim linecap := squared;
151%D         save p ; path p ; p := fullsquare xysized(AdaptiveWidth,.9(AdaptiveHeight+AdaptiveDepth)) ;
152%D         fill p withcolor AdaptiveColor ;
153%D         draw bottomboundary (p enlarged (-AdaptiveThickness) )
154%D             withdashes (3*AdaptiveThickness)
155%D             withpen pencircle scaled AdaptiveThickness
156%D             withcolor white ;
157%D     enddef ;
158%D \stopMPdefinitions
159%D
160%D \startsetups adaptive:test
161%D     \setbox\usedadaptivebox\hbox to \usedadaptivewidth yoffset -.9\usedadaptivedepth \bgroup
162%D         \hss
163%D         \startMPcode
164%D             TickTock ;
165%D         \stopMPcode
166%D         \hss
167%D    \egroup
168%D \stopsetups
169%D
170%D \definecolor[adaptive:tick][.25(blue,green)]
171%D \definecolor[adaptive:tock][.75(blue,green)]
172%D
173%D \defineadaptive
174%D   [tick]
175%D   [setups=adaptive:test,
176%D    color=adaptive:tick,
177%D    foregroundcolor=white,
178%D    foregroundstyle=\infofont,
179%D    strut=yes]
180%D
181%D \defineadaptive
182%D   [tock]
183%D   [tick]
184%D   [color=adaptive:tock]
185%D
186%D \dorecurse{5}{
187%D     \dostepwiserecurse{1}{150}{1}{%
188%D         this~#1.##1 is~#1.##1 test~#1.##1
189%D         \ifodd##1\relax
190%D             \adaptivebox[tick]{\hss tick #1.##1\hss}
191%D         \else
192%D             \adaptivebox[tock]{\hss tock #1.##1\hss}
193%D         \fi
194%D     }
195%D     \blank
196%D }
197%D
198%D \setupexternalfigures
199%D   [location={default,global}]
200%D
201%D \startsetups adaptive:test
202%D     \setbox\usedadaptivebox\hbox to \usedadaptivewidth yoffset -\usedadaptivedepth \bgroup
203%D         \externalfigure
204%D           [cow.pdf]
205%D           [width=\usedadaptivewidth,
206%D            height=\usedadaptiveheight+\usedadaptivedepth]%
207%D    \egroup
208%D \stopsetups
209%D
210%D \startsetups adaptive:test
211%D     \setbox\usedadaptivebox\hbox to \usedadaptivewidth yoffset .25ex \bgroup
212%D         \startMPcode
213%D             drawarrow ((0,0) -- (\the\usedadaptivewidth,0)) randomized (ExHeight/2);
214%D         \stopMPcode
215%D    \egroup
216%D \stopsetups
217%D
218%D \dorecurse{5}{
219%D     \dostepwiserecurse{1}{150}{1}{%
220%D         this~#1.##1 is~#1.##1 test~#1.##1
221%D         {\adaptivebox[strut=yes,setups=adaptive:test]{}}
222%D     }
223%D     \blank
224%D }
225%D
226%D \page
227%D
228%D % How about stretch on an float (make it fit)
229%D
230%D \startsetups adaptive:test
231%D     \setbox\usedadaptivebox\vbox to \usedadaptivetotal \bgroup
232%D         \externalfigure
233%D           [cow.pdf]
234%D           [width=\usedadaptivewidth,
235%D            height=\usedadaptivetotal]%
236%D    \egroup
237%D \stopsetups
238%D
239%D \ruledvbox to \textheight {
240%D     \par \begstrut \samplefile{tufte} \endstrut \par
241%D     \adaptivevbox[strut=yes,setups=adaptive:test]{\hsize\textwidth\hss}
242%D     \par \begstrut \samplefile{tufte} \endstrut
243%D }
244%D
245%D \stoptyping
246
247%D This kind of usage for \type {\uluaders} is a bit weird but the fact that we
248%D can put a callback on it combined with it being something with optional glue
249%D has some potential anyway. Consider it a playground for myself. I considered
250%D extending inserts with seomething like this. Very little code is involved.
251%D
252%D \starttyping
253%D \dorecurse {2} {
254%D     \setexcept[left] {\strut\red   EXCEPT L#1.1}%
255%D     \samplefile{tufte}
256%D     \dorecurse{12}{\setexcept[right]{\strut\blue  EXCEPT R#1.##1}}%
257%D     \setexcept[left] {\strut\green EXCEPT L#1.2}%
258%D     \samplefile{tufte}
259%D     \blank
260%D     \samplefile{tufte}
261%D }
262%D \stoptyping
263%D
264%D The ideas to keep the except within the text areas and force a page break
265%D otherwise: the except depth is part of the page total & goal check.
266
267\def\typo_adaptive_except
268  {\finalizeobjectbox\nextbox
269   \setbox\nextbox\hpack{\box\nextbox}%
270   \smashbox\nextbox
271   \clf_setadaptive {method \plustwo location {\currentexceptlocation}}%
272   \uleaders
273     nobreak % no a break in the par builder
274     line    % kick in early (post line break, not in vpackage)
275     \s!callback {\adaptiveattribute}%
276     \hpack{\box\nextbox}%
277     \hskip\zeropoint
278   \endgroup}
279
280\permanent\tolerant\protected\def\setexcept[#1]%
281   {\dontleavehmode
282    \begingroup
283    % todo: outer and inner
284    \cdef\currentexceptlocation{#1}%
285    \dowithnextboxcs\typo_adaptive_except\hbox}
286
287\protect
288