spac-grd.mkiv /size: 9403 b    last modification: 2021-10-28 13:50
1%D \module
2%D   [       file=spac-grd,
3%D        version=2009.10.16, % 1998.03.10, was core-grd.tex
4%D          title=\CONTEXT\ Spacing Macros,
5%D       subtitle=Grid Snapping,
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%D This module will be merged into spac-ver.mkiv.
15
16\writestatus{loading}{ConTeXt Spacing Macros / Grid Snapping}
17
18\unprotect
19
20%D A rather crappy macro that we need to avoid and as such it will probably
21%D disappear:
22
23\installcorenamespace{lastnodepusher}
24
25\let\poplastnode\relax
26
27\unexpanded\def\pushlastnode
28  {\csname\??lastnodepusher
29     \ifcsname\??lastnodepusher\the\lastnodetype\endcsname
30       \the\lastnodetype
31     \else
32       \s!unknown
33     \fi
34   \endcsname}
35
36\setvalue{\??lastnodepusher\number\kernnodecode}%
37  {\unexpanded\edef\poplastnode{\kern\the\lastkern\relax}%
38   \kern-\lastkern}
39
40\setvalue{\??lastnodepusher\number\gluenodecode}%
41  {\unexpanded\edef\poplastnode{\vskip\the\lastskip\relax}%
42   \vskip-\lastskip}
43
44\setvalue{\??lastnodepusher\number\penaltynodecode}%
45  {\unexpanded\edef\poplastnode{\penalty\the\lastpenalty\relax}%
46   \nobreak}
47
48\setvalue{\??lastnodepusher\s!unknown}%
49  {\let\poplastnode\relax}
50
51%D Moved from supp-box:
52
53%D \macros
54%D  {startbaselinecorrection,baselinecorrection,
55%D   showbaselinecorrection,offbaselinecorrection}
56%D
57%D Spacing around ruled boxes can get pretty messed up. The
58%D next macro tries as good as possible to fix this.
59%D
60%D \startbuffer[1]
61%D \startbaselinecorrection
62%D \ruledhbox{Rule Brittanica}
63%D \stopbaselinecorrection
64%D \stopbuffer
65%D
66%D \typebuffer[1]
67%D
68%D The macros put some white space around the box:
69%D
70%D \getbuffer[1]
71%D
72%D A simple alternative is \type {\baselinecorrection}, which
73%D only looks at the previous line.
74%D
75%D \startbuffer[2]
76%D \baselinecorrection
77%D \ruledhbox{Rule Brittanica}
78%D \baselinecorrection
79%D \stopbuffer
80%D
81%D \typebuffer[2]
82%D
83%D This time the last preceding line gets a correction,%
84%D dependant on the depth.
85%D
86%D \getbuffer[2]
87%D
88%D One can make the correction visible by saying \type
89%D {\showbaselinecorrection}. Part of the correction is
90%D calculated from the dimensions of a~(. One can disble the
91%D correction by calling \type {\offbaselinecorrection}.
92%D
93%D When visualize the first example looks like:
94%D
95%D {\showbaselinecorrection\getbuffer[1]}
96%D
97%D and the second one comes out as:
98%D
99%D {\showbaselinecorrection\getbuffer[2]}
100
101% \definecolor[GridLineColor][red]
102% \definecolor[GridTextColor][blue]
103
104\newdimen\d_spac_lines_correction_before
105\newdimen\d_spac_lines_correction_after
106
107\newbox  \b_spac_lines_correction_before
108\newbox  \b_spac_lines_correction_after
109
110\def\spac_lines_initialize_corrections
111  {\setbox\b_spac_lines_correction_before\hpack{\setstrut\strut}%
112   \setbox\b_spac_lines_correction_after \hbox {(}%
113   \d_spac_lines_correction_before\dimexpr\ht\b_spac_lines_correction_before-\ht\b_spac_lines_correction_after\relax
114   \d_spac_lines_correction_after \dimexpr\dp\b_spac_lines_correction_before-\dp\b_spac_lines_correction_after\relax
115   \ifdim\d_spac_lines_correction_before<\zeropoint\d_spac_lines_correction_before\zeropoint\fi
116   \ifdim\d_spac_lines_correction_after <\zeropoint\d_spac_lines_correction_after \zeropoint\fi}
117
118%unexpanded\def\dotopbaselinecorrection{\kern\d_spac_lines_correction_before}
119%unexpanded\def\dobotbaselinecorrection{\kern\d_spac_lines_correction_after }
120
121% experiment, todo: proper mkiv mechanism
122%
123% \input ward \par
124% \startframedtext test  \stopframedtext
125% \input ward \par
126% \startlinecorrection \framed{xxx} \stoplinecorrection
127% \input ward \par
128%
129% \setupwhitespace[big]
130%
131% \input ward \par
132% \startframedtext test  \stopframedtext
133% \input ward \par
134% \startlinecorrection \framed{xxx} \stoplinecorrection
135% \input ward \par
136
137% to be redone:
138
139\unexpanded\def\dotopbaselinecorrection{\expandafter\blank\expandafter[\the\d_spac_lines_correction_before]}
140\unexpanded\def\dobotbaselinecorrection{\expandafter\blank\expandafter[\the\d_spac_lines_correction_after ]}
141
142\def\showbaselinecorrection
143  {\def\dobaselinecorrection % visualization is not watertight!
144     {\bgroup
145      \ifdim\prevdepth>\zeropoint
146        \kern-\prevdepth
147      \fi
148      \setbox\scratchbox\emptyhbox
149      \wd\scratchbox\hsize
150      \dp\scratchbox\strutdp
151      \nointerlineskip
152      \forgetall
153      \ruledvpack{\box\scratchbox}%
154      \egroup
155      \prevdepth\strutdp}%
156   \def\dotopbaselinecorrection
157     {\hrule\s!height\d_spac_lines_correction_before}%
158   \def\dobotbaselinecorrection
159     {\hrule\s!height\d_spac_lines_correction_after}}
160
161\def\dobaselinecorrection % beware, this one is redefined / used locally elsewhere
162  {\ifdim\prevdepth>\zeropoint\kern-\prevdepth\fi
163   \kern\strutdp
164   \prevdepth\strutdp}
165
166\def\baselinecorrection
167  {\endgraf
168   \ifvmode
169     \ifdim\prevdepth<\maxdimen
170       \ifdim\prevdepth<\zeropoint \else
171         \ifdim\prevdepth<\strutdepth \relax
172           \pushlastnode
173           \dobaselinecorrection
174           \poplastnode
175         \fi
176       \fi
177     \fi
178   \fi}
179
180\def\pagebaselinecorrection
181  {\ifdim\pagegoal<\maxdimen
182     \ifdim\pagetotal>\lineheight % or \topskip
183       \scratchdimen\pagetotal
184       \advance\scratchdimen\lineheight
185       \ifdim\scratchdimen<\pagegoal
186         \baselinecorrection
187       \fi
188     \fi
189   \fi}
190
191\unexpanded\def\startbaselinecorrection
192  {\bgroup
193   \let\stopbaselinecorrection\egroup
194   \ifcase\baselinecorrectionmode
195   \or % normal
196     \baselinecorrection
197     \ifvmode
198       \setbox\scratchbox\vbox\bgroup\ignorespaces
199       \let\stopbaselinecorrection\donormalstopbaselinecorrection
200     \fi
201   \or % off
202   \or % force
203     \baselinecorrection
204     \ifvmode
205       \setbox\scratchbox\vbox\bgroup\ignorespaces
206       \let\stopbaselinecorrection\doforcedstopbaselinecorrection
207     \fi
208   \fi}
209
210\let\stopbaselinecorrection\relax
211
212\def\donormalstopbaselinecorrection % I have to check columns yet.
213  {\egroup
214   \topbaselinecorrection
215   \box\scratchbox
216   \botbaselinecorrection
217   \egroup}
218
219\def\doforcedstopbaselinecorrection % I have to check columns yet.
220  {\egroup
221   \forcedtopbaselinecorrection
222   \box\scratchbox
223   \forcedbotbaselinecorrection
224   \egroup}
225
226%D We do a bit more checking than needed. The pageborder check
227%D is not needed, but I want to look the visualization as good
228%D as possible too.
229
230\setnewconstant\baselinecorrectionmode\plusone
231
232\def\onbaselinecorrection   {\baselinecorrectionmode\plusone  }
233\def\offbaselinecorrection  {\baselinecorrectionmode\plustwo  }
234\def\forcebaselinecorrection{\baselinecorrectionmode\plusthree}
235
236%D \macros
237%D  {topbaselinecorrection,botbaselinecorrection}
238%D
239%D The actual top and bottom corrections are implemented as:
240
241\def\topbaselinecorrection
242  {\ifvmode \ifdim\pagegoal<\maxdimen
243     \forcedtopbaselinecorrection
244   \fi \fi}
245
246\def\forcedtopbaselinecorrection
247  {\ifvmode
248     \bgroup
249     \spac_lines_initialize_corrections
250     \whitespace % no longer ok
251     \nointerlineskip
252     \dotopbaselinecorrection
253     \egroup
254   \fi}
255
256\def\botbaselinecorrection
257  {\ifvmode
258     \bgroup
259     \spac_lines_initialize_corrections
260     \dobotbaselinecorrection
261     \allowbreak % new, otherwise problems when many in a row
262     \prevdepth\strutdp
263     \egroup
264   \fi}
265
266\let\forcedbotbaselinecorrection\botbaselinecorrection
267
268% nointerlineskip
269%
270% startpacked
271%     \startlinecorrection \framed{test} \stoplinecorrection
272%     \startlinecorrection \framed{test} \stoplinecorrection
273% \stoppacked
274
275\def\forcedtopbaselinecorrection
276  {\ifvmode
277     \bgroup
278     \spac_lines_initialize_corrections
279     \vspacing[\v!white]
280%      \nointerlineskip %
281     \dotopbaselinecorrection
282     \egroup
283\fi}
284
285\def\dobaselinecorrection
286  {\ifdim\prevdepth>\zeropoint
287     \vspacing[\the\dimexpr-\prevdepth+\strutdp\relax]%
288   \else
289     \vspacing[\the\dimexpr\strutdp\relax]%
290   \fi
291   \prevdepth\strutdp}
292
293\let\normalstartbaselinecorrection\startbaselinecorrection
294
295\unexpanded\def\startbaselinecorrection
296  {\ifgridsnapping
297     \snaptogrid[\v!normal]\vbox\bgroup
298     \let\stopbaselinecorrection\egroup
299   \else
300     \normalstartbaselinecorrection
301   \fi}
302
303%D Different in LMTX, maybe here we can use this in \MKIV:
304%D
305%D \let\checkprevdepth\forcestrutdepth % mlv only
306%D
307%D as we have sync issues that makes it not work otherwise.
308
309\let\checkprevdepth\baselinecorrection
310
311% This is new (and experimental) and might replace some of the above. beware it doesn't always work
312% out well, e.g. when used grouped and such (e.g. before display math doesn't work out well).
313
314% \unexpanded\def\fakenextstrutline
315%   {\par
316%    \begingroup
317%    \reseteverypar
318%    \dontleavehmode\hpack to \zeropoint{\page_sides_anchor\hss\strut}% just a tracer
319%    \vskip-\parskip
320%    \vskip-\struttotal
321%    \endgroup}
322
323% \unexpanded\def\fakenextstrutline
324%   {\par
325%    \begingroup
326%    \reseteverypar
327%    \dontleavehmode\hpack{\strut}\par
328%    \clf_fakenextstrutline
329%    \ifdim\pagetotal>\lineheight
330%      \pagetotal\dimexpr\pagetotal-\lineheight\relax
331%    \fi
332%    \endgroup}
333
334% \unexpanded\def\fakenextstrutline
335%   {\par
336%    \begingroup
337%    \reseteverypar
338%    \forgetall
339%    \dontleavehmode\hpack{\strut}\par
340%    \clf_removelastline
341%    \endgroup}
342
343\protect \endinput
344