summaryrefslogtreecommitdiff
path: root/tex/context/base/core-grd.tex
blob: 22a392bc23710a56bd0b847a2c861800ba0f7778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
%D \module
%D   [       file=core-grd,
%D        version=1998.03.10,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Grid Snapping (Experimental),
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for 
%C details. 

\writestatus{loading}{Context Core Macros / Grid Snapping}

\unprotect

%D Moved from supp-box:

%D \macros
%D  {startbaselinecorrection,baselinecorrection,
%D   showbaselinecorrection,offbaselinecorrection}
%D
%D Spacing around ruled boxes can get pretty messed up. The
%D next macro tries as good as possible to fix this.
%D
%D \startbuffer[1]
%D \startbaselinecorrection
%D \ruledhbox{Rule Brittanica}
%D \stopbaselinecorrection
%D \stopbuffer
%D
%D \typebuffer[1]
%D
%D The macros put some white space around the box:
%D
%D \haalbuffer[1]
%D
%D A simple alternative is \type {\baselinecorrection}, which
%D only looks at the previous line.
%D
%D \startbuffer[2]
%D \baselinecorrection
%D \ruledhbox{Rule Brittanica}
%D \baselinecorrection
%D \stopbuffer
%D
%D \typebuffer[2]
%D
%D This time the last preceding line gets a correction,%
%D dependant on the depth.
%D
%D \haalbuffer[2]
%D
%D One can make the correction visible by saying \type
%D {\showbaselinecorrection}. Part of the correction is
%D calculated from the dimensions of a~(. One can disble the
%D correction by calling \type {\offbaselinecorrection}.
%D
%D When visualize the first example looks like:
%D
%D {\showbaselinecorrection\haalbuffer[1]}
%D
%D and the second one comes out as:
%D
%D {\showbaselinecorrection\haalbuffer[2]}

\definecolor[GridLineColor][red]
\definecolor[GridTextColor][blue]

\def\setbaselinecorrections%
  {\setbox0\hbox{\setstrut\strut}%
   \setbox2\hbox{(}%
   \dimen0\ht0\advance\dimen0 -\ht2
   \ifdim\dimen0<\zeropoint\dimen0\zeropoint\fi
   \dimen2\dp0\advance\dimen2 -\dp2
   \ifdim\dimen2<\zeropoint\dimen2\zeropoint\fi
   \edef\thetopbaselinecorrection   {\the\dimen0}\dimen0-\dimen0
   \edef\thebotbaselinecorrection   {\the\dimen2}\dimen2-\dimen2
   \edef\thenegtopbaselinecorrection{\the\dimen0}%
   \edef\thenegbotbaselinecorrection{\the\dimen2}}

\def\dotopbaselinecorrection   {\kern\thetopbaselinecorrection}
\def\dobotbaselinecorrection   {\kern\thebotbaselinecorrection}
\def\donegtopbaselinecorrection{\kern\thenegtopbaselinecorrection}
\def\donegbotbaselinecorrection{\kern\thenegbotbaselinecorrection}

\def\showbaselinecorrection%
  {\def\dobaselinecorrection% visualization is not watertight!
     {\bgroup
      \kern-\prevdepth
      \setbox0=\null
      \wd0=\hsize\dp0=\dp\strutbox
      \nointerlineskip
      \ruledvbox{\box0}%
      \egroup
      \prevdepth=\dp\strutbox}%
   \def\dotopbaselinecorrection%
     {\hrule\!!height\thetopbaselinecorrection}%
   \def\dobotbaselinecorrection%
     {\hrule\!!height\thebotbaselinecorrection}}

\def\dobaselinecorrection%
  {\kern-\prevdepth
   \kern\dp\strutbox
   \prevdepth=\dp\strutbox}

\def\baselinecorrection%
  {\endgraf
   \ifvmode
     \ifdim\prevdepth<\maxdimen
       \ifdim\prevdepth<\zeropoint \else
         \ifdim\prevdepth<\dp\strutbox
           \dobaselinecorrection
         \fi
       \fi
     \fi
   \fi}

% Beware, keep this one as it is, see for instance module
% m-steps.tex, where we apply a \localhsize to the \vbox, in
% order to follow narrower and side floats !

\def\startbaselinecorrection%
  {\baselinecorrection
   \ifvmode
     \bgroup
     \setbox\scratchbox=\vbox\bgroup
     \ignorespaces
     \let\stopbaselinecorrection=\dostopbaselinecorrection
   \else
     \let\stopbaselinecorrection=\relax
   \fi}

%D We do a bit more checking than needed. The pageborder check
%D is not needed, but I want to look the visualization as good
%D as possible too.

\def\dostopbaselinecorrection% I have to check columns yet.
  {\endgraf
   \egroup
   \topbaselinecorrection
   \box\scratchbox
   \botbaselinecorrection
   \egroup}

\let\stopbaselinecorrection=\relax

\def\offbaselinecorrection% Can be used inside correction.
  {\def\startbaselinecorrection%
     {\bgroup
      \let\stopbaselinecorrection=\egroup}}

%D \macros
%D  {topbaselinecorrection,botbaselinecorrection}
%D
%D The actual top and bottom corrections are implemented as:

\def\topbaselinecorrection%
  {\ifvmode
     \bgroup
     \setbaselinecorrections
     \ifdim\pagegoal<\maxdimen
       \ifdim\pagetotal<\pagegoal
         \dimen2=\ht\scratchbox
         \advance\dimen2 by \dp\scratchbox
         \advance\dimen2 by \parskip
         \advance\dimen2 by \thetopbaselinecorrection
         \advance\dimen2 by \thebotbaselinecorrection
         \dimen0=\pagetotal
         \advance\dimen0 by \dimen2
%        \ifdim\dimen0<\pagegoal % does more harm than good
           \witruimte
           \nointerlineskip
           \dotopbaselinecorrection
%        \else
%          \ifbinnenkolommen      
%            % \vskip\dimen2           % this could definitely
%            % \penalty\outputpenalty  % be improved
%          \else
%            %                         % 
%            % \vfill\eject            % pretty old and wrong
%            %
%            % \nobreak                % needed for headings
%            % \vskip\parskip          % but often splits 
%            % \vskip\dimen2           % normal paragraphs
%            % \penalty\outputpenalty  % and therefore 
%            % \vskip-\dimen2          % obsolete
%            %
%            % do nothing, sub optimal spacing after headings 
%            % still to be sorted out; use manuals as test case
%          \fi
%        \fi
       \else                           % probably a preceding
         \witruimte                    % one-liner
         \nointerlineskip
         \dotopbaselinecorrection
       \fi
     \fi
     \egroup
   \fi}

\def\botbaselinecorrection%
  {\ifvmode
     \bgroup
     \setbaselinecorrections
     \dobotbaselinecorrection
     \allowbreak % new, otherwise problems when many in a row
     \prevdepth\dp\strutbox
     \egroup
   \fi}

%D An old one, still to be translated:
%D
%D \macros
%D   {getnoflines, getrawnoflines}
%D
%D Het commando \type{\getnoflines} converteert een hoogte
%D (dimensie) in een aantal regels en kent dit toe aan
%D \type{\noflines}.
%D
%D \starttypen
%D \getnoflines{dimensie}
%D \stoptypen
%D
%D Er wordt gedeeld door \type{\openlineheight} en een hoogte
%D van~0pt komt overeen met 0~regels. The raw alternative 
%D does not round. 

\newcount\noflines
\newdimen\noflinesheight

\def\dogetnoflines#1#2%
  {\noflinesheight=#2\relax
   \ifdim\noflinesheight=\zeropoint
     \noflines=0
   \else
     \divide\noflinesheight by \openlineheight
     \noflines=\noflinesheight
     #1\ifdim\noflines\openlineheight=#2\relax \else
       \advance\noflines by 1
     \fi\fi
   \fi}

\def\getnoflines   {\dogetnoflines\iftrue } % compensated 
\def\getrawnoflines{\dogetnoflines\iffalse} % no compensation 

%D Still very experimental and therefore undocumented.

\newif\ifgridsnapping  % UNDER DEVELOPMENT, USE WITH CARE
\newif\ifforcepresnap  \forcepresnaptrue  % false in mixed single/double
\newif\ifstrutsnapping \strutsnappingtrue % sometimes handy to be false

\def\positiveextrasnap {\gdef\extrasnapsign{+}}
\def\negativeextrasnap {\gdef\extrasnapsign{-}}

\def\extrasnapreset  {\global\chardef\@@extrasnap=0
                      \positiveextrasnap}
\def\extrasnapbefore {\global\chardef\@@extrasnap=1 }
\def\extrasnaparound {\global\chardef\@@extrasnap=2 }
\def\extrasnapafter  {\global\chardef\@@extrasnap=3 }

\def\enablepresnapcorrection  {\global\chardef\@@presnap=0 }
\def\disablepresnapcorrection {\global\chardef\@@presnap=1 }

\extrasnapreset \enablepresnapcorrection

\newif\iftracegridsnapping
\newif\ifshowfuzzyskips

\chardef\@@alignsnap   =0
\chardef\@@alignsnapbox=0

\let\presnapskip \!!zeropoint  \def\presnap {--}
\let\postsnapskip\!!zeropoint  \let\postsnap\presnap

\def\tracesnapping%
  {\iftracegridsnapping
     \llap
       {\infofont
        \doglobal\increment\currentsnap
        \color[GridLineColor]{\vl\presnapskip
                    \vl\presnap
                    \vl\postsnap
                    \vl\ifcase\@@extrasnap
                            00\or
               \extrasnapsign0\or
  \extrasnapsign\extrasnapsign\or
               0\extrasnapsign\fi
                    \vl\currentsnap\vl}}%
   \fi}

\def\snaptogrid% [#1]#2 -> #2 == \hbox|\vbox
  {\dosingleempty\dosnaptogrid}

\def\dosnaptogrid[#1]% 
  {\ifgridsnapping
     \iffirstargument\doifsomething{#1}{\verplaatsopgrid[#1]}\fi
     \expandafter\dodosnaptogrid
   \fi}

% \def\dodosnaptogrid% 
%   {\dowithnextbox
%      {\bgroup
%       \ifcase\@@alignsnap\else % 1=top 2=high 3=middle 4=low
%         \getnoflines{\ht\nextbox}
%         \setbox\nextbox=\vbox to \noflines\lineheight
%           {\ifnum\@@alignsnap=1 \kern\lineheight\kern-\topskip\fi
%            \ifnum\@@alignsnap>2 \vfill\fi
%            \box\nextbox
%            \ifnum\@@alignsnap<4 \vfill\fi}
%       \fi
%       \forgetall
%       \par
%       \ifvbox\nextbox
%         \setbox\nextbox=\hbox{\box\nextbox}% don't ask
%       \fi
%       \scratchskip=\lastskip
%       \edef\presnapskip{\the\lastskip}%
%       % mixing single/double columns sometimes goes wrong,
%       % check 'som' document
%       \ifbinnenkolommen
%         \forcepresnaptrue
%       \fi
%       \ifforcepresnap
%         \vbox{\hsize\zeropoint\strut}\nobreak\vskip-\openlineheight
%       \fi
%       %
%       \ifdim\ht\nextbox>\ht\strutbox
%         \scratchdimen=\ht\nextbox
%         \ifcase\@@presnap
%           \ifdim\scratchskip>\zeropoint\relax
%             \scratchcounter=\scratchskip
%             \advance\scratchcounter by -\openlineheight
%             \ifnum\scratchcounter<0
%               \scratchcounter=-\scratchcounter
%             \fi
%             \ifnum\scratchcounter<10 % \lastkip is about \openlineheight
%               \advance\scratchdimen by -\openstrutdepth
%               \edef\presnapskip{*\presnapskip}%
%             \else\ifdim\scratchskip>\openlineheight
%                                   %<\openlineheight \else
%               \advance\scratchdimen by -\openstrutdepth
%               \edef\presnapskip{*\presnapskip}%
%             \fi\fi
%           \fi
%         \fi
%         \getnoflines\scratchdimen
%         \advance\noflines by -1
%         \ifnum\noflines>0
%           \scratchdimen=\noflines\lineheight
%         \else
%           \scratchdimen=\zeropoint
%         \fi
%       \else
%         \scratchdimen=\zeropoint
%       \fi
%       \ifnum\@@extrasnap=1 \advance\scratchdimen by \extrasnapsign\lineheight \fi
%       \ifnum\@@extrasnap=2 \advance\scratchdimen by \extrasnapsign.5\lineheight \fi
%       \edef\presnap{\the\scratchdimen}%
%       \ifstrutsnapping
%         \ifdim\scratchdimen=\zeropoint\else\vskip\scratchdimen\fi
%       \fi
%       \ifdim\dp\nextbox>\dp\strutbox
%         \getnoflines{\dp\nextbox}%
%         \advance\noflines by -1
%         \ifnum\noflines>0
%           \scratchdimen=\noflines\lineheight
%         \else
%           \scratchdimen=\zeropoint
%         \fi
%       \else
%         \scratchdimen=\zeropoint
%       \fi
%       \ifnum\@@extrasnap=2 \advance\scratchdimen by \extrasnapsign.5\lineheight \fi
%       \ifnum\@@extrasnap=3 \advance\scratchdimen by \extrasnapsign\lineheight \fi
%       \edef\postsnap{\the\scratchdimen}%
%       \ifstrutsnapping
%         \ht\nextbox=\ht\strutbox
%         \dp\nextbox=\dp\strutbox
%       \else
%         \scratchdimen=\presnap
%         \advance\scratchdimen by \ht\strutbox
%         \ht\nextbox=\scratchdimen
%         \scratchdimen=\postsnap
%         \advance\scratchdimen by \dp\strutbox
%         \dp\nextbox=\scratchdimen
%       \fi
%       \hbox{\tracesnapping\box\nextbox}%
%       \ifstrutsnapping
%         \ifdim\scratchdimen=\zeropoint\else\vskip\scratchdimen\fi
%       \fi
%       \extrasnapreset
%       \enablepresnapcorrection
%       \global\chardef\@@alignsnap0
%       \egroup}}

\def\dodosnaptogrid
  {\dowithnextbox
     {\bgroup
      \ifcase\@@alignsnapbox\relax
        \ifcase\@@alignsnap\else % 1=top 2=high 3=middle 4=low
          \getnoflines{\ht\nextbox}
          \setbox\nextbox\vbox to \noflines\lineheight
            {\ifnum\@@alignsnap=1 \kern\lineheight\kern-\topskip\fi
             \ifnum\@@alignsnap>2 \vfill\fi
             \box\nextbox
             \ifnum\@@alignsnap<4 \vfill\fi}
        \fi
        \forgetall
        \par
        \ifvbox\nextbox
          \setbox\nextbox\hbox{\box\nextbox}% don't ask
        \fi
        \scratchskip\lastskip
        \edef\presnapskip{\the\lastskip}%
        % mixing single/double columns sometimes goes wrong,
        % check 'som' document
        \ifbinnenkolommen
          \forcepresnaptrue
        \fi
        \ifforcepresnap
          \vbox{\hsize\zeropoint\strut}\nobreak\vskip-\openlineheight
        \fi
        %
        \ifdim\ht\nextbox>\ht\strutbox
          \scratchdimen\ht\nextbox
          \ifcase\@@presnap
            \ifdim\scratchskip>\zeropoint\relax
              \scratchcounter\scratchskip
              \advance\scratchcounter -\openlineheight
              \ifnum\scratchcounter<0
                \scratchcounter-\scratchcounter
              \fi
              \ifnum\scratchcounter<10 % \lastkip is about \openlineheight
                \advance\scratchdimen -\openstrutdepth
                \edef\presnapskip{*\presnapskip}%
              \else\ifdim\scratchskip>\openlineheight
                                    %<\openlineheight \else
                \advance\scratchdimen -\openstrutdepth
                \edef\presnapskip{*\presnapskip}%
              \fi\fi
            \fi
          \fi
          \getnoflines\scratchdimen
            \advance\noflines -1
          \ifnum\noflines>0
            \scratchdimen\noflines\lineheight
          \else
            \scratchdimen\zeropoint
          \fi
        \else
          \scratchdimen\zeropoint
        \fi
        \ifnum\@@extrasnap=1 \advance\scratchdimen \extrasnapsign\lineheight \fi
        \ifnum\@@extrasnap=2 \advance\scratchdimen \extrasnapsign.5\lineheight \fi
        \edef\presnap{\the\scratchdimen}%
        \ifstrutsnapping
          \ifdim\scratchdimen=\zeropoint\else\vskip\scratchdimen\fi
        \fi
        \ifdim\dp\nextbox>\dp\strutbox
          \getnoflines{\dp\nextbox}%
          \advance\noflines -1
          \ifnum\noflines>0
            \scratchdimen\noflines\lineheight
          \else
            \scratchdimen\zeropoint
          \fi
        \else
          \scratchdimen\zeropoint
        \fi
        \ifnum\@@extrasnap=2 \advance\scratchdimen \extrasnapsign.5\lineheight \fi
        \ifnum\@@extrasnap=3 \advance\scratchdimen \extrasnapsign\lineheight \fi
        \edef\postsnap{\the\scratchdimen}%
        \ifstrutsnapping
          \ht\nextbox\ht\strutbox
          \dp\nextbox\dp\strutbox
        \else
          \scratchdimen\presnap
          \advance\scratchdimen \ht\strutbox
          \ht\nextbox\scratchdimen
          \scratchdimen\postsnap
          \advance\scratchdimen \dp\strutbox
          \dp\nextbox\scratchdimen
        \fi
        \hbox{\tracesnapping\box\nextbox}%
        \ifstrutsnapping
          \ifdim\scratchdimen=\zeropoint\else\vskip\scratchdimen\fi
        \fi
      \else
        \scratchdimen\ht\nextbox
       %\advance\scratchdimen\dp\nextbox % add dp as option 
        \ifcase\@@alignsnapbox\or
          \getrawnoflines\scratchdimen
        \else
          \getnoflines   \scratchdimen
        \fi  
        \scratchdimen\noflines\lineheight\relax
        \ifnum\pagetotal>\zeropoint\else % disable this as option 
          \advance\scratchdimen-\strutheight
          \advance\scratchdimen+\topskip
        \fi
        \setbox\nextbox\vbox to \scratchdimen
          {\ifnum\@@alignsnapbox>2 \vfill\fi % 3 4 
           \box\nextbox
           \ifnum\@@alignsnapbox<4 \vfill\fi % 2 3 
           \kern\zeropoint}% 
        \ifnum\@@alignsnapbox>2 \def\presnap {++}\fi 
        \ifnum\@@alignsnapbox<4 \def\postsnap{++}\fi 
        \setbox\nextbox\hbox{\tracesnapping\box\nextbox}%
        \dp\nextbox\zeropoint 
        \box\nextbox 
      \fi
      \extrasnapreset
      \enablepresnapcorrection
      \global\chardef\@@alignsnap0
      \global\chardef\@@alignsnapbox0
      \egroup}}

\def\snaptomathgrid% probably not working ok
  {\ifgridsnapping
     \dowithnextbox
       {\blanko[\v!regel]
        \snaptogrid\vbox{\box\nextbox}
        \blanko[\v!regel]}
     \vbox\bgroup
       \let\setdisplayskips=\relax
       \abovedisplayskip\zeropoint
       \abovedisplayshortskip\zeropoint
       \belowdisplayskip\zeropoint
       \belowdisplayshortskip\zeropoint
       \@EA\let\@EA\next
   \fi}

\def\topsnaptogrid%
  {\ifgridsnapping
     \dowithnextbox
       {\scratchdimen=\ht\nextbox
        \advance\scratchdimen by -\ht\strutbox
        \advance\scratchdimen by \topskip
        \ht\nextbox=\scratchdimen
        \dp\nextbox=\zeropoint
        \box\nextbox
        \kern\lineheight
        \kern-\topskip
        \nointerlineskip}
       \hbox
   \fi}

\def\centertogrid%
  {\ifgridsnapping
     \dowithnextbox
       {\bgroup
        \par
        \scratchdimen=\ht\nextbox
        \advance\scratchdimen by \dp\nextbox
        \getnoflines\scratchdimen
        \setbox\nextbox=\vbox to \noflines\lineheight
          {\forgetall
           \vskip\zeropoint plus \ht\nextbox
           \copy\nextbox
           \kern.5\dp\strutbox % VOORLOPIGE WAARDE
           \vskip\zeropoint plus \dp\nextbox}
        \noindent\snaptogrid\vbox{\box\nextbox}%
        \egroup}
       \hbox
   \fi}

\ifx\startbaselinecorrection\undefined \wait \fi % change order

\let\normalstartbaselinecorrection=\startbaselinecorrection

\def\startbaselinecorrection%
  {\ifgridsnapping
     \centertogrid\bgroup
     \let\stopbaselinecorrection=\egroup
   \else
     \normalstartbaselinecorrection
   \fi}

\def\setgridbox#1#2#3%
  {\setbox#1=\ruledvbox to #3 % given size 
     {\forgetall
      \offinterlineskip
      \hsize=#2
      \baselinerulefalse
      \ruledvbox % calculated size 
        {\getnoflines{#3}%
         \vskip\topskip
         \vskip-\ht\strutbox
         \scratchdimen=#2\advance\scratchdimen by \lineheight
         \dorecurse
           {\noflines}
           {\strut
            \hskip-.5\lineheight
            \rlap
              {\hskip\scratchdimen\hskip2pt\infofont
               \hbox to 1em{\hss\recurselevel}}%
            \vrule
              \!!height .5\testrulewidth
              \!!depth .5\testrulewidth
              \!!width \scratchdimen
            \par}}
      \vfill}}

%D Some intervention macros:

\def\gridwarning#1{\message{[beware of #1 extra snap]}}

% \def\doverplaatsopgrid[#1]%
%   {\ifgridsnapping
%      \global\chardef\@@alignsnap0
%      \processaction
%        [#1]
%        [  \v!boven=>\gridwarning+\positiveextrasnap\extrasnapbefore,
%           \v!onder=>\gridwarning+\positiveextrasnap\extrasnapafter,
%           \v!beide=>\positiveextrasnap\extrasnaparound,
%          -\v!boven=>\gridwarning-\negativeextrasnap\extrasnapbefore,
%          -\v!onder=>\gridwarning-\negativeextrasnap\extrasnapafter,
%          -\v!beide=>\negativeextrasnap\extrasnaparound,
%          \v!pagina=>\global\chardef\@@alignsnap1, % topskip
%            \v!hoog=>\global\chardef\@@alignsnap2, 
%          \v!midden=>\global\chardef\@@alignsnap3, 
%            \v!laag=>\global\chardef\@@alignsnap4, 
%           \v!reset=>\positiveextrasnap\extrasnapreset,
%         \s!default=>\positiveextrasnap\extrasnaparound,
%         \s!unknown=>\positiveextrasnap\extrasnapreset]%
%    \fi}

\def\doverplaatsopgrid[#1]% some day : speed up 
  {\ifgridsnapping
     \global\chardef\@@alignsnap0 
     \global\chardef\@@alignsnapbox0 
     \donefalse 
     \ExpandFirstAfter\processallactionsinset[#1]
       [  \v!boven=>\gridwarning+\positiveextrasnap\extrasnapbefore,
          \v!onder=>\gridwarning+\positiveextrasnap\extrasnapafter,
          \v!beide=>\positiveextrasnap\extrasnaparound,
         -\v!boven=>\gridwarning-\negativeextrasnap\extrasnapbefore,
         -\v!onder=>\gridwarning-\negativeextrasnap\extrasnapafter,
         -\v!beide=>\negativeextrasnap\extrasnaparound,
         \v!pagina=>\global\chardef\@@alignsnap1, % topskip
           \v!hoog=>\global\chardef\@@alignsnap2, 
         \v!midden=>\global\chardef\@@alignsnap3, 
           \v!laag=>\global\chardef\@@alignsnap4, 
        \v!passend=>\global\chardef\@@alignsnapbox1, % new 
           \v!ruim=>\global\chardef\@@alignsnapbox2, % new 
          \v!reset=>\positiveextrasnap\extrasnapreset,
           \v!geen=>\global\chardef\@@alignsnap0
                    \global\chardef\@@alignsnapbox0,
      \v!standaard=>\positiveextrasnap\extrasnaparound,
        \s!default=>\positiveextrasnap\extrasnaparound,
        \s!unknown=>\positiveextrasnap\extrasnapreset]%
   \fi}

\def\verplaatsopgrid%
  {\dosingleempty\doverplaatsopgrid}

\def\doplaatsopgrid[#1]%
  {\doverplaatsopgrid[#1]\snaptogrid\vbox}

\def\plaatsopgrid%
  {\dosingleempty\doplaatsopgrid}

%D Snapping is rather robust as long as we use whole lines.
%D Half lines of white space can however be handled when they
%D come in pairs. The corrections needed when crossing page
%D boundaries in the middle of such a pair, are handled by
%D macros that are (named) sort of fuzzy. This fuzzy mechanism
%D was written as an extension to the grid typesetting needed
%D for typesetting (part of) the \MAPS.
%D
%D \starttypen
%D \setuptyping
%D   [before={\blank[halfline]},
%D    after={\blank[halfline]}]
%D \stoptypen

\newif\iffuzzysnapdone
\newif\iffuzzysnapping
\newif\iffuzzysnapped

\chardef\fuzzysnappedleft=0 % ==1 when fuzzybegin still open

\newpersistentmark\fuzzymark % (!)
\newcount\fuzzymarker
\newbox\fuzzysnapbox
\newbox\fuzzysnapsplit

%\def\dosyncfuzzyvskip%
%  {\ifvmode\ifdim\lastskip<\lineheight\ifdim\lastskip>\zeropoint
%     \bgroup
%     \endgraf
%     \forgetall
%     \vbox{\strut}
%     \kern-2\lineheight
%     \nobreak
%     \vskip\lineheight 
%     \egroup
%   \fi\fi\fi}

%\def\fuzzyvskip#1%
%  {\iffuzzysnapdone
%     \endfuzzysnapping
%     \vskip#1\relax
%     \dosyncfuzzyvskip  % NEW
%     \global\fuzzysnapdonefalse
%   \else
%     \vskip#1\relax
%     \beginfuzzysnapping
%     \global\fuzzysnapdonetrue
%   \fi}

\def\dosyncfuzzyvskip%
  {\ifvmode\ifdim\lastskip<\lineheight\ifdim\lastskip>\zeropoint
     \bgroup
       \endgraf\forgetall\vbox{\strut}\nobreak\vskip\lineheight 
     \egroup
   \fi\fi\fi}

\def\fuzzyvskip#1%
  {\iffuzzysnapdone
     \dosyncfuzzyvskip  % NEWER
     \endfuzzysnapping
     \vskip#1\relax
     \global\fuzzysnapdonefalse
   \else
     \vskip#1\relax
     \beginfuzzysnapping
     \global\fuzzysnapdonetrue
   \fi}

\def\setfuzzymark#1#2#3% #1/#2 => error recovery
  {\ifgridsnapping
     \global\fuzzysnappingtrue
     \global\advance\fuzzymarker by \ifodd\fuzzymarker#1\else#2\fi
     \nobreak
     \ifshowfuzzyskips
       \hbox{\color[GridTextColor]
         {\llap{\infofont#3\vl\the\fuzzymarker}\nobreak
          \vrule\!!width\hsize\!!height.1\lineheight}}
       \nobreak
     \fi
    %[\the\fuzzymarker]
    %\expandafter\fuzzymark\expandafter{\the\fuzzymarker}%
     \expandafter\setmark\expandafter\fuzzymark\expandafter{\the\fuzzymarker}%
     \nobreak
  \fi}

\def\beginfuzzysnapping% odd
  {\setfuzzymark21\v!start}

\def\endfuzzysnapping% even
  {\setfuzzymark12\v!stop}

\def\removelastfuzzyvskip%
  {\ifgridsnapping
     \iffuzzysnapping
       \ifdim\lastskip<\openlineheight
       \else
         \removelastskip
       \fi
     \else
       \removelastskip
     \fi
   \else
     \removelastskip
   \fi}

\def\docheckfuzzysnap#1%
  {\bgroup
   \mindermeldingen
   \setbox\fuzzysnapbox=\copy#1\relax
   \setbox\fuzzysnapsplit=\vsplit\fuzzysnapbox to 1\lineheight
   \let\topfuzzymark=\empty % indeed here ... no real mark
   \getsplitmarks\fuzzymark
%   \ifcase0\topfuzzymark
   \ifcase0\getsplittopmark\fuzzymark
     \global\chardef\fuzzysnappedleft=0
     \global\fuzzysnappedfalse
%   \else\ifodd\topfuzzymark
   \else\ifodd\getsplittopmark\fuzzymark
     \global\chardef\fuzzysnappedleft=1
      \global\fuzzysnappedtrue
   \else
     \global\chardef\fuzzysnappedleft=2
      \global\fuzzysnappedtrue
   \fi\fi
   \iffuzzysnapped \else
     \doloop
       {\ifvoid\fuzzysnapbox
          \exitloop
        \else
          \setbox\fuzzysnapsplit=\vsplit\fuzzysnapbox to \lineheight
         %\let\topfuzzymark=\empty % ... but not here
          \getsplitmarks\fuzzymark
%          \ifcase0\topfuzzymark
          \ifcase0\getsplittopmark\fuzzymark
            % continue
%          \else\ifodd\topfuzzymark
          \else\ifodd\getsplittopmark\fuzzymark
            \exitloop
          \else
            \global\chardef\fuzzysnappedleft=1
            \global\fuzzysnappedtrue
            \exitloop
          \fi\fi
        \fi}%
   \fi
   \egroup}

\def\getfuzzysnapcorrection#1%
  {\global\let\presnapcorrection =\relax
   \global\let\postsnapcorrection=\relax
   \ifgridsnapping\iffuzzysnapping
     \docheckfuzzysnap{#1}%
     \iffuzzysnapped
       \iftracegridsnapping
         \gdef\presnapcorrection%
           {\color[GridLineColor]{\hrule\!!height.5\openlineheight\!!width\hsize}}%
       \else
         \gdef\presnapcorrection{\kern.5\openlineheight}%
       \fi
       \gdef\postsnapcorrection{\kern-.5\openlineheight}% get the height ok
     \fi
   \fi\fi}

\def\fuzzysnappedbox#1#2% \box<n> \unvbox<n>
  {\getfuzzysnapcorrection{#2}%
   \presnapcorrection
   #1#2%
   \postsnapcorrection}

\def\adaptfuzzypagegoal%
  {\ifgridsnapping\iffuzzysnapping\ifcase\fuzzysnappedleft\or % see dopagecontents
     \scratchdimen\pagegoal
     \advance\scratchdimen -.5\openlineheight
     \global\pagegoal\scratchdimen
     \global\advance\vsize -.5\openlineheight
     \global\chardef\fuzzysnappedleft0
   \fi\fi\fi}

\protect

\endinput