typo-bal.mkxl /size: 4856 b    last modification: 2025-02-21 11:03
1%D \module
2%D   [       file=typo-bal,
3%D        version=2024.11.27,
4%D          title=\CONTEXT\ Typesetting Macros,
5%D       subtitle=Balancing,
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 / Balancing}
15
16\unprotect
17
18%D Experimental:
19%D
20%D \starttyping
21%D \starttext
22%D
23%D \definebalancing
24%D   [demo]
25%D
26%D \setupbalancing
27%D   [demo]
28%D   [align={tolerant,stretch},
29%D    after=\page]
30%D
31%D \startbalancing[demo][1]
32%D     \dorecurse {10} {
33%D         \samplefile{tufte}\par
34%D         \startlinecorrection
35%D             \externalfigure[dummy]
36%D         \stoplinecorrection
37%D         \samplefile{ward}\par
38%D     }%
39%D \stopbalancing
40%D
41%D \startbalancing[demo]
42%D     \dorecurse {5} {
43%D         \blank
44%D         \samplefile{knuthmath}
45%D     }%
46%D \stopbalancing
47%D
48%D \startbalancing[demo][2]
49%D     \dorecurse {12} {
50%D         \samplefile{ward}\par
51%D         \startlinecorrection
52%D             \externalfigure[dummy]
53%D         \stoplinecorrection
54%D         \samplefile{davis}\par
55%D     }%
56%D \stopbalancing
57%D
58%D \startsetups MyBalance
59%D    \balancetolerance        2000
60%D    \balanceemergencystretch 2\lineheight
61%D    \balancebottomskip       0pt plus 2\lineheight
62%D \stopsetups
63%D
64%D \placebalancing[demo] % [setups=MyBalance]
65%D
66%D \stoptext
67%D \stoptyping
68
69% todo: \newmvl
70% todo: more than two
71
72\installcorenamespace{balancing}
73
74\installcommandhandler \??balancing {balancing} \??balancing
75
76\setupbalancing
77  [\c!distance=\emwidth,
78   \c!height=\textheight,
79   \c!setups=,
80   \c!n=\plustwo]
81
82% \appendtoks
83% \to \everydefinebalancing
84
85\permanent\tolerant\protected\def\startbalancing[#1]#*[#2]% [name] [n] | [name] | [n]
86  {\par
87   \begingroup
88   \ifchknumber#2\or
89     \scratchcounter\lastchknumber
90     \cdef\currentbalancing{#1}%
91   \orelse\ifchknumber#1\or
92     \scratchcounter\lastchknumber
93     \lettonothing\currentbalancing
94   \orelse\ifparameter#1\or
95     \scratchcounter\plusone
96     \cdef\currentbalancing{#1}%
97   \else
98     \scratchcounter\plusone
99     \lettonothing\currentbalancing
100   \fi
101   \setrigidcolumnhsize\hsize{\balancingparameter\c!distance}\plustwo % {\balancingparameter\c!n}%
102   \usealignparameter\balancingparameter
103   \beginmvl \scratchcounter}
104
105\permanent\protected\def\stopbalancing
106  {\endmvl
107   \endgroup
108   \par}
109
110\permanent\tolerant\protected\def\placebalancing[#1]#*[#2]%
111  {\begingroup
112   \cdef\currentbalancing{#1}%
113   \ifparameter#2\or
114     \setupcurrentbalancing[#2]%
115   \fi
116   % todo: loop and use box stack
117   \setbox\scratchboxone \flushmvl \plusone
118   \setbox\scratchboxtwo \flushmvl \plustwo
119   %
120 % \ifcstok{\balancingparameter\c!height}\v!max
121 %   % can overflow so we need slack
122 %   \balancevsize\htdp\ifdim\htdp\scratchboxone>\htdp\scratchboxtwo\scratchboxone\else\scratchboxtwo\fi
123 % \else
124 %   \balancevsize\lastnamedcs
125 % \fi
126   %
127   \balancevsize            {\balancingparameter\c!height}%
128   \balancetolerance        \plustwothousand
129   \balanceemergencystretch 2\lineheight
130   \balancetopskip          \strutht
131   \balancebottomskip       \zeropoint \s!plus 2\lineheight
132   %
133   \usesetupsparameter\balancingparameter
134   %
135   \setbox\scratchboxthree\ifvoid\scratchboxone\voidbox\else\vbalance\scratchboxone exactly\relax\fi
136   \setbox\scratchboxfour \ifvoid\scratchboxtwo\voidbox\else\vbalance\scratchboxtwo exactly\relax\fi
137   %
138   \doloop {
139     \ifvoid\scratchboxthree
140       \ifvoid\scratchboxfour
141         \donefalse
142       \else
143         \donetrue
144       \fi
145     \else
146       \donetrue
147     \fi
148     \ifdone
149       \balancingparameter\c!before\relax
150       \hpack to \hsize \bgroup
151         \setrigidcolumnhsize\hsize{\balancingparameter\c!distance}\plustwo % {\balancingparameter\c!n}%
152         \hbox to \hsize{\ifvoid\scratchboxthree\else\vbalancedtop\scratchboxthree\fi}%
153         \hss
154         \hbox to \hsize{\ifvoid\scratchboxfour \else\vbalancedtop\scratchboxfour \fi}%
155       \egroup
156       \balancingparameter\c!after\relax
157     \else
158       \exitloop
159     \fi
160   }%
161   \endgroup}
162
163%D \starttyping
164%D \definebalancing
165%D   [demo]
166%D   [align={tolerant,stretch},
167%D    after=\page]
168%D
169%D \startbalancing[demo][1]
170%D     \samplefile{tufte}\par
171%D     \the\prevdepth\par
172%D \stopbalancing
173%D \startbalancing[demo][2]
174%D     xxxxxxxxxxxxxx
175%D \stopbalancing
176%D \startbalancing[demo]
177%D     \the\prevdepth\par
178%D     \samplefile{knuthmath}
179%D \stopbalancing
180%D
181%D \placebalancing[demo]
182%D
183%D \samplefile{tufte}\par
184%D \the\prevdepth\par
185%D \the\prevdepth\par
186%D \samplefile{knuthmath}
187%D \stoptyping
188
189\protect \endinput
190
191% timestamp: Nov 2024, LEPROUS – Atonement (Drum Playthrough by Baard Kolstad)
192