spac-grd.mkxl /size: 10 Kb    last modification: 2023-12-21 09:44
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\permanent\let\poplastnode\relax
26
27\permanent\protected\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\defcsname\??lastnodepusher\the\kernnodecode\endcsname
37  {\enforced\permanent\protected\edef\poplastnode{\kern\the\lastkern\relax}%
38   \kern-\lastkern}
39
40\defcsname\??lastnodepusher\the\gluenodecode\endcsname
41  {\enforced\permanent\protected\edef\poplastnode{\vskip\the\lastskip\relax}%
42   \vskip-\lastskip}
43
44\defcsname\??lastnodepusher\the\penaltynodecode\endcsname
45  {\enforced\permanent\protected\edef\poplastnode{\penalty\the\lastpenalty\relax}%
46   \nobreak}
47
48\defcsname\??lastnodepusher\s!unknown\endcsname
49  {\enforced\permanent\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\newdimension\d_spac_lines_correction_before
105\newdimension\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% experiment, todo: proper mkiv mechanism
119%
120% \input ward \par
121% \startframedtext test  \stopframedtext
122% \input ward \par
123% \startlinecorrection \framed{xxx} \stoplinecorrection
124% \input ward \par
125%
126% \setupwhitespace[big]
127%
128% \input ward \par
129% \startframedtext test  \stopframedtext
130% \input ward \par
131% \startlinecorrection \framed{xxx} \stoplinecorrection
132% \input ward \par
133
134% to be redone:
135
136\permanent\protected\def\dotopbaselinecorrection{\expandafter\blank\expandafter[\the\d_spac_lines_correction_before]}
137\permanent\protected\def\dobotbaselinecorrection{\expandafter\blank\expandafter[\the\d_spac_lines_correction_after ]}
138
139\permanent\def\showbaselinecorrection
140  {\enforced\permanent\protected\def\dobaselinecorrection % visualization is not watertight!
141     {\bgroup
142      \ifdim\prevdepth>\zeropoint
143        \kern-\prevdepth
144      \fi
145      \setbox\scratchbox\emptyhbox
146      \wd\scratchbox\hsize
147      \dp\scratchbox\strutdp
148      \nointerlineskip
149      \forgetall
150      \ruledvpack{\box\scratchbox}%
151      \egroup
152      \prevdepth\strutdp}%
153   \enforced\permanent\protected\def\dotopbaselinecorrection{\hrule\s!height\d_spac_lines_correction_before}%
154   \enforced\permanent\protected\def\dobotbaselinecorrection{\hrule\s!height\d_spac_lines_correction_after }}
155
156% \def\dobaselinecorrection % beware, this one is redefined below
157%   {\ifdim\prevdepth>\zeropoint\kern-\prevdepth\fi
158%    \kern\strutdp
159%    \prevdepth\strutdp}
160
161\permanent\protected\def\baselinecorrection
162  {\endgraf
163   \ifvmode
164     \ifdim\prevdepth<\maxdimen
165       \ifdim\prevdepth<\zeropoint \else
166         \ifdim\prevdepth<\strutdepth \relax
167           \pushlastnode
168           \dobaselinecorrection
169           \poplastnode
170         \fi
171       \fi
172     \fi
173   \fi}
174
175\permanent\protected\def\pagebaselinecorrection
176  {\ifdim\pagegoal<\maxdimen
177     \ifdim\pagetotal>\lineheight % or \topskip
178       \scratchdimen\pagetotal
179       \advanceby\scratchdimen\lineheight
180       \ifdim\scratchdimen<\pagegoal
181         \baselinecorrection
182       \fi
183     \fi
184   \fi}
185
186\permanent\protected\def\startbaselinecorrection
187  {\bgroup
188   \enforced\let\stopbaselinecorrection\egroup
189   \ifcase\baselinecorrectionmode
190   \or % normal
191     \baselinecorrection
192     \ifvmode
193       \setbox\scratchbox\vbox\bgroup\ignorespaces
194       \enforced\let\stopbaselinecorrection\donormalstopbaselinecorrection
195     \fi
196   \or % off
197   \or % force
198     \baselinecorrection
199     \ifvmode
200       \setbox\scratchbox\vbox\bgroup\ignorespaces
201       \enforced\let\stopbaselinecorrection\doforcedstopbaselinecorrection
202     \fi
203   \fi}
204
205\permanent\let\stopbaselinecorrection\relax
206
207\permanent\protected\def\donormalstopbaselinecorrection % I have to check columns yet.
208  {\egroup
209   \topbaselinecorrection
210   \box\scratchbox
211   \botbaselinecorrection
212   \egroup}
213
214\permanent\protected\def\doforcedstopbaselinecorrection % I have to check columns yet.
215  {\egroup
216   \forcedtopbaselinecorrection
217   \box\scratchbox
218   \forcedbotbaselinecorrection
219   \egroup}
220
221%D We do a bit more checking than needed. The pageborder check
222%D is not needed, but I want to look the visualization as good
223%D as possible too.
224
225\setnewconstant\baselinecorrectionmode\plusone
226
227\permanent\protected\def\onbaselinecorrection   {\baselinecorrectionmode\plusone  }
228\permanent\protected\def\offbaselinecorrection  {\baselinecorrectionmode\plustwo  }
229\permanent\protected\def\forcebaselinecorrection{\baselinecorrectionmode\plusthree}
230
231%D \macros
232%D  {topbaselinecorrection,botbaselinecorrection}
233%D
234%D The actual top and bottom corrections are implemented as:
235
236\permanent\protected\def\topbaselinecorrection
237  {\ifvmode \ifdim\pagegoal<\maxdimen
238     \forcedtopbaselinecorrection
239   \fi \fi}
240
241% \permanent\protected\def\forcedtopbaselinecorrection
242%   {\ifvmode
243%      \bgroup
244%      \spac_lines_initialize_corrections
245%      \whitespace % no longer ok
246%      \nointerlineskip
247%      \dotopbaselinecorrection
248%      \egroup
249%    \fi}
250
251\permanent\protected\def\botbaselinecorrection
252  {\ifvmode
253     \bgroup
254     \spac_lines_initialize_corrections
255     \dobotbaselinecorrection
256     \allowbreak % new, otherwise problems when many in a row
257     \prevdepth\strutdp
258     \egroup
259   \fi}
260
261% nointerlineskip
262%
263% startpacked
264%     \startlinecorrection \framed{test} \stoplinecorrection
265%     \startlinecorrection \framed{test} \stoplinecorrection
266% \stoppacked
267
268\permanent\protected\def\forcedtopbaselinecorrection
269  {\ifvmode
270     \bgroup
271     \spac_lines_initialize_corrections
272     \vspacing[\v!white]
273    %\nointerlineskip %
274     \dotopbaselinecorrection
275     \egroup
276  \fi}
277
278\aliased\let\forcedbotbaselinecorrection\botbaselinecorrection
279
280\permanent\protected\def\dobaselinecorrection
281  {\ifdim\prevdepth>\zeropoint
282     \vspacing[\the\dimexpr-\prevdepth+\strutdp\relax]%
283   \else
284     \vspacing[\the\dimexpr\strutdp\relax]%
285   \fi
286   \prevdepth\strutdp}
287
288% \permanent\protected\def\dobaselinecorrection
289%   {\scratchdimen\dimexpr\ifdim\prevdepth>\zeropoint-\prevdepth+\fi\strutdp\relax
290%    \vspacing[\the\scratchdimen]%
291%    \prevdepth\strutdp}
292%
293% some day:
294%
295% \permanent\protected\def\dobaselinecorrection
296%   {\clf_checkstrutdepth\strutdp}
297%
298% \permanent\protected\def\baselinecorrection
299%   {\endgraf
300%    \ifvmode
301%      \ifdim\prevdepth<\maxdimen
302%        \ifdim\prevdepth<\zeropoint \else
303%          \ifdim\prevdepth<\strutdepth \relax
304%            % works in mvl:
305%            % \forcestrutdepth
306%            % doesn't work immediately (unless enforced with penalty):
307%            % \pushlastnode
308%            % \dobaselinecorrection
309%            % \poplastnode
310%            % works ok:
311%              \clf_checkstrutdepth\strutdp
312%          \fi
313%        \fi
314%      \fi
315%    \fi}
316
317%D For the moment only with placefloats (should work in mvl and boxes):
318
319\permanent\protected\def\checkprevdepth
320  {\endgraf
321   \ifvmode
322     \ifdim\prevdepth<\maxdimen
323       \ifdim\prevdepth<\zeropoint \else
324         \ifdim\prevdepth<\strutdepth \relax
325           \clf_checkstrutdepth\strutdp
326         \fi
327       \fi
328     \fi
329   \fi}
330
331\aliased\let\normalstartbaselinecorrection\startbaselinecorrection
332
333\pushoverloadmode
334
335\permanent\protected\def\startbaselinecorrection
336  {\ifgridsnapping
337     \snaptogrid[\v!normal]\vbox\bgroup
338     \enforced\let\stopbaselinecorrection\egroup
339   \else
340     \normalstartbaselinecorrection
341   \fi}
342
343\popoverloadmode
344
345% This is new (and experimental) and might replace some of the above. beware it doesn't always work
346% out well, e.g. when used grouped and such (e.g. before display math doesn't work out well).
347
348% \permanent\protected\def\fakenextstrutline
349%   {\par
350%    \begingroup
351%    \reseteverypar
352%    \dontleavehmode\hpack to \zeropoint{\page_sides_anchor\hss\strut}% just a tracer
353%    \vskip-\parskip
354%    \vskip-\struttotal
355%    \endgroup}
356
357% \permanent\protected\def\fakenextstrutline
358%   {\par
359%    \begingroup
360%    \reseteverypar
361%    \dontleavehmode\hpack{\strut}\par
362%    \clf_fakenextstrutline
363%    \ifdim\pagetotal>\lineheight
364%      \pagetotal\dimexpr\pagetotal-\lineheight\relax
365%    \fi
366%    \endgroup}
367
368% \permanent\protected\def\fakenextstrutline
369%   {\par
370%    \begingroup
371%    \reseteverypar
372%    \forgetall
373%    \dontleavehmode\hpack{\strut}\par
374%    \clf_removelastline
375%    \endgroup}
376
377\protect \endinput
378