summaryrefslogtreecommitdiff
path: root/tex/context/base/strc-ren.mkiv
blob: f22df120f964a6c81c7cdb564c9dedbf0a5847a6 (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
%D \module
%D   [       file=strc-ren,
%D        version=2008.10.20,
%D          title=\CONTEXT\ Structure Macros,
%D       subtitle=Section Rendering,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%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 Structure Macros / Section Rendering}

\unprotect

\newbox\b_strc_rendering_head

\newdimen\d_strc_rendering_local_leftoffset
\newdimen\d_strc_rendering_local_rightoffset

% Martin Kolarik's problem:
%
% \setuphead[section][command=\doTitle]
% \def\doTitle#1#2{\ruledvbox{\forgetall \hsize=4cm \ruledhbox{\ruledvtop{#1}\ruledvtop{#2}}}}
% \section{test test test test test test test test test test test test test test test test test}

\newtoks\everyheadstart

\unexpanded\def\strc_rendering_initialize_style_and_color
  {\ifconditional\headisdisplay
     \expandafter\strc_rendering_initialize_style_and_color_display
   \else
     \expandafter\strc_rendering_initialize_style_and_color_inline
   \fi}

\unexpanded\def\strc_rendering_initialize_style_and_color_display#1#2%
  {\dontconvertfont
   \edef\p_strc_rendering_interlinespace{\headparameter\c!interlinespace}%
   \ifx\p_strc_rendering_interlinespace\empty
     % here the interline space is only set when style sets no space
     \setfalse\fontattributeisset % use the currentfontparameter state instead
     \setfalse\interlinespaceisset
     \useheadstyleandcolor\c!style\c!color\relax
     \ifconditional\fontattributeisset \ifconditional\interlinespaceisset \else
       \setupinterlinespace
     \fi \fi
     \setfalse\fontattributeisset
     \useheadstyleandcolor#1#2\relax
     \ifconditional\fontattributeisset \ifconditional\interlinespaceisset \else
       \setupinterlinespace
     \fi \fi
   \else
     % here the set interline space overloads any other set space in the style
     \setfalse\fontattributeisset
     \useheadstyleandcolor\c!style\c!color\relax
     \ifconditional\fontattributeisset
       \dosetupcheckedinterlinespace\p_strc_rendering_interlinespace
     \fi
     \setfalse\fontattributeisset
     \useheadstyleandcolor#1#2\relax
     \ifconditional\fontattributeisset
       \dosetupcheckedinterlinespace\p_strc_rendering_interlinespace
     \fi
   \fi}

\unexpanded\def\strc_rendering_initialize_style_and_color_inline#1#2%
   {\dontconvertfont
    \setfalse\fontattributeisset
    \useheadstyleandcolor\c!style\c!color\relax
    \ifconditional\fontattributeisset
      \updateraggedskips % \setupspacing
    \fi
    \setfalse\fontattributeisset
    \useheadstyleandcolor#1#2\relax
    \ifconditional\fontattributeisset
      \updateraggedskips % \setupspacing
    \fi}

\let\currentstructurereferenceattribute\attributeunsetvalue

\def\headreferenceattributes
  {\iflocation
   % \ctxlua{structures.lists.taglocation(\nextinternalreference)}% maybe ... tags entry as used
     attr \destinationattribute \currentstructureattribute
     attr \referenceattribute   \currentstructurereferenceattribute
   % attr \internalattribute    \nextinternalreference
   \fi}

\def\setinlineheadreferenceattributes
  {\ifconditional\headisdisplay \else \iflocation
     \attribute\destinationattribute\currentstructureattribute
     \attribute\referenceattribute  \currentstructurereferenceattribute
   % \attribute\internalattribute   \nextinternalreference
   \fi \fi}

\def\docheckheadreference
  {\edef\currentheadinteraction{\headparameter\c!interaction}%
   \ifx\currentheadinteraction\v!list
     % setuphead[<section>][interaction=list,...]
     \strc_references_get_simple_page_reference{bck:\nextinternalreference}%
     \let\currentstructurereferenceattribute\currentreferenceattribute
   \else\ifx\currentheadinteraction\v!reference
     % setuphead[<section>][interaction=reference,...] start<section>[backreference=abc,...]
     \edef\currentheadbackreference{\structurevariable\c!backreference}% weird, was references.backreference
     \ifx\currentheadbackreference\empty \else
       \strc_references_get_simple_page_reference\currentheadbackreference
       \let\currentstructurereferenceattribute\currentreferenceattribute
     \fi
   \else
      % maybe auto: backreference when given, else list
   \fi\fi}

% a bit messy ... empty in place instead of self .. might change (or use special
% whatsig (invisible user one)

\unexpanded\def\strc_rendering_place_head_text
  {\strc_rendering_start_placement
   \setheadmarking
   \doresettructureheadnumbercontent
   \ifconditional\c_strc_sectioning_empty
     \setbox\b_strc_rendering_head\hbox \headreferenceattributes to \zeropoint{\strut}%
   \else
     \docheckheadreference
     \setbox\b_strc_rendering_head\hbox \headreferenceattributes
       {\spac_grids_set_local_snapping{\headparameter\c!internalgrid}%
        \doresettructureheadnumbercontent
        \useheadstyleparameter\c!style
        \setinlineheadreferenceattributes
        \strc_rendering_inject_text}%
   \fi
   \strc_rendering_stop_placement}

\unexpanded\def\strc_rendering_place_head_number_and_text
  {\strc_rendering_start_placement
   \setheadmarking
   \doiftextelse\getheadnumber
     \dosettructureheadnumbercontent
     \doresettructureheadnumbercontent
   \ifconditional\c_strc_sectioning_empty
     \setbox\b_strc_rendering_head\hbox \headreferenceattributes to \zeropoint{\strut}%
   \else % = needed
     \docheckheadreference
     \setbox\b_strc_rendering_head\hbox \headreferenceattributes
       {\spac_grids_set_local_snapping{\headparameter\c!internalgrid}%
        \useheadstyleparameter\c!style
        \setinlineheadreferenceattributes
        \strc_rendering_inject_number_and_text}%
   \fi
   \strc_rendering_stop_placement}

\unexpanded\def\strc_rendering_place_head_empty
  {\hbox \headreferenceattributes {\getheadsyncs}}

%D \starttyping
%D \def\StretchedBox#1%
%D   {\framed
%D      [frame=off,offset=.5em,align=middle,width=broad]
%D      {\sc\def\stretchedspaceamount{.3em}\stretchednormalcase{#1}}}
%D
%D \definehead[MySubject][subject]
%D \setuphead [MySubject][deeptextcommand=\StretchedBox]
%D
%D \MySubject{feeling stretched feeling stretched feeling stretched feeling stretched}
%D \stoptyping

% helpers

% \defineinmargin [ChapterInMargin] [outer] [normal] [distance=0.3em]
%
% \defineheadplacement[MyTest][horizontal]#1#2%
%   {\startlocalheadsetup
%   %\ChapterInMargin{\headhbox{\strut#2}}% proper destination, ref okay
%    \ChapterInMargin{\strut#2}% zero destination, ref okay
%    \stoplocalheadsetup}
%
% \setuphead
%   [chapter]
%   [alternative=MyTest]

\unexpanded\def\headhbox{\hbox\headreferenceattributes}
\unexpanded\def\headvbox{\vbox\headreferenceattributes}

\unexpanded\def\startlocalheadsetup{\bgroup\strc_rendering_initialize_spacing}
\unexpanded\def\stoplocalheadsetup {\egroup}

\unexpanded\def\strc_rendering_initialize_spacing
  {\forgetall % local !
   \edef\p_align{\headparameter\c!align}%
   \ifx\p_align\empty \else
     \setupalign[\p_align]%
   \fi
   \edef\p_tolerance{\headparameter\c!tolerance}%
   \ifx\p_tolerance\empty \else
     \setuptolerance[\p_tolerance]%
   \fi
   \edef\p_strut{\headparameter\c!strut}%
   \ifx\p_strut\v!no
     \setnostrut
   \fi
   \let\\\strc_rendering_shortcut_backslash}

\unexpanded\def\strc_rendering_shortcut_backslash
  {\crlf
   \strut
   \ignorespaces}

\def\strc_rendering_start_placement
  {\bgroup
   \setsystemmode\currenthead
   %
   \strc_rendering_initialize_alternatives
   \strc_rendering_initialize_dimensions
   %
   \strc_rendering_initialize_line_state
   \reseteverypar % needed indeed
   \noindent      % ipv \whitespace elders, na \forgetall !
   \bgroup
   \edef\p_aligntitle{\headparameter\c!aligntitle}%
   \ifx\p_aligntitle\v!yes
     \strc_rendering_initialize_hsize_local
   \else\ifx\p_aligntitle\v!float
     \strc_rendering_initialize_hsize_local
   \else
     \strc_rendering_initialize_hsize_global
   \fi\fi
   \setfalse\inhibitmargindata % brrrr is set in forgetall
   \dontcomplain
   \postponenotes
   \strc_rendering_initialize_interaction
   % delayed
   \let\localheadsetup  \strc_rendering_initialize_spacing % historic name
   \let\headsetupspacing\strc_rendering_initialize_spacing}

\def\strc_rendering_initialize_hsize_local
  {\global\d_strc_rendering_local_leftoffset \leftskip
   \global\d_strc_rendering_local_rightoffset\rightskip
 % \forgetall
 % \leftskip \d_strc_rendering_local_leftoffset  % no stretch
 % \rightskip\d_strc_rendering_local_rightoffset % no stretch
 % \setlocalhsize
 % \hsize\localhsize
 % \forgetbothskips}
   \scratchwidth\availablehsize
   \forgetall
   \hsize\scratchwidth}

\def\strc_rendering_initialize_hsize_global
  {\global\d_strc_rendering_local_leftoffset \zeropoint
   \global\d_strc_rendering_local_rightoffset\zeropoint
   \forgetall}

\def\strc_rendering_initialize_interaction
  {\resetinteractionparameter\c!style
   \resetinteractionparameter\c!color
   \resetinteractionparameter\c!contrastcolor}

% \setuphead[chapter]         [style=\bfd,after=,hang=line] % fit broad 2
% \setuphead[section]         [style=\bfc,after=,hang=line]
% \setuphead[subsection]      [style=\bfb,after=,hang=line]
% \setuphead[subsubsection]   [style=\bfa,after=,hang=line]
% \setuphead[subsubsubsection][style=\bf ,after=,hang=line]
%
% \chapter         {Test} \input tufte \page
% \section         {Test} \input tufte \page
% \subsection      {Test} \input tufte \page
% \subsubsection   {Test} \input tufte \page
% \subsubsubsection{Test} \input tufte \page
%
% \chapter         {Test\\Test} \input tufte \page
% \section         {Test\\Test} \input tufte \page
% \subsection      {Test\\Test} \input tufte \page
% \subsubsection   {Test\\Test} \input tufte \page
% \subsubsubsection{Test\\Test} \input tufte \page

\newdimen\d_strc_rendering_hang_height
\newcount\n_strc_rendering_hang_lines

\newdimen\d_strc_rendering_local_height
\newdimen\d_strc_rendering_local_depth
\newdimen\d_strc_rendering_local_lineheight

\def\strc_rendering_initialize_line_state
  {\global\d_strc_rendering_local_height\strutht
   \global\d_strc_rendering_local_depth\strutdp
   \global\d_strc_rendering_local_lineheight\lineheight}

\def\strc_rendering_check_hang
  {\begingroup
   \openlineheight\d_strc_rendering_local_lineheight
   \d_strc_rendering_hang_height\htdp\b_strc_rendering_head
   \getnoflines\d_strc_rendering_hang_height
   \normalexpanded{\endgroup\n_strc_rendering_hang_lines\the\numexpr\noflines-\plusone\relax}% brrr
   \setbox\b_strc_rendering_head\hbox{\lower\n_strc_rendering_hang_lines\d_strc_rendering_hang_height\box\b_strc_rendering_head}%
   \d_strc_rendering_hang_height\dimexpr\htdp\b_strc_rendering_head-\d_strc_rendering_local_height+\strutdp\relax
   \ht\b_strc_rendering_head\strutht
   \dp\b_strc_rendering_head\strutdp
   \d_strc_rendering_local_depth\strutdp}

\installcorenamespace{headplacementcheckhang}

\setvalue{\??headplacementcheckhang\v!line   }{\strc_rendering_check_hang
                                               \n_strc_rendering_hang_lines\zerocount}
\setvalue{\??headplacementcheckhang\v!broad  }{\strc_rendering_check_hang
                                               \getnoflines\d_strc_rendering_hang_height}
\setvalue{\??headplacementcheckhang\v!fit    }{\strc_rendering_check_hang
                                               \getrawnoflines\d_strc_rendering_hang_height}
\setvalue{\??headplacementcheckhang\v!none   }{\n_strc_rendering_hang_lines\zerocount}
\setvalue{\??headplacementcheckhang          }{\n_strc_rendering_hang_lines\zerocount}
\setvalue{\??headplacementcheckhang\s!unknown}{\strc_rendering_check_hang
                                               \n_strc_rendering_hang_lines\numexpr\p_hang-\plusone\relax}

\def\strc_rendering_initialize_line_hang
   {\ifconditional\headisdisplay
      \edef\p_hang{\headparameter\c!hang}%
      \expandcheckedcsname\??headplacementcheckhang\p_hang\s!unknown
      \relax
    \else
      \n_strc_rendering_hang_lines \zerocount
      \d_strc_rendering_hang_height\zeropoint
    \fi}

\def\strc_rendering_stop_placement
  {\n_strc_rendering_hang_lines\zerocount
   \ifconditional\headisdisplay
     \strc_rendering_initialize_line_hang
     % kind of special, we want to snap heads also according to local specs local
     \ifgridsnapping
       \hbox\bgroup % extra hbox will trigger global snapper on top of local
         \edef\p_grid{\headparameter\c!grid}%
         \ifconditional\headisdisplay
           \ifx\p_grid\empty\else
             \useheadstyleandcolor\c!style\c!color
             \setupinterlinespace
             \useheadstyleandcolor\c!textstyle\c!textcolor
             \setupinterlinespace
           \fi
         \fi
         \snaptogrid[\p_grid]\hbox
           {\hskip\dimexpr\d_strc_rendering_local_leftoffset+\headparameter\c!margin\relax\box\b_strc_rendering_head}%
       \egroup
     \else
       \hbox
         {\hskip\dimexpr\d_strc_rendering_local_leftoffset+\headparameter\c!margin\relax\box\b_strc_rendering_head}%
     \fi
     \flushnotes % new, not really needed
     \endgraf
     \ifvmode
       \ifnum\n_strc_rendering_hang_lines>\zerocount
         \dorecurse\n_strc_rendering_hang_lines{\nointerlineskip\dosomebreak\nobreak\strut\endgraf}% to be checked
       \fi
       \nointerlineskip
       \dosomebreak\nobreak
     \fi
     \getheadsyncs
   \else
     \strut
     \flushnotes % new, here since we're in par mode
     \unhbox\b_strc_rendering_head
     \getheadsyncs
     \hskip\headnumberdistance\!!plus\headnumberdistance\!!minus.25\dimexpr\headnumberdistance\relax
     \strc_sectioning_inject_continuous_signal
   \fi
   \ifconditional\headisdisplay
     \ifvmode
       \ifgridsnapping % important, font related depth, see comment
         \prevdepth\strutdp
       \else
         \prevdepth\d_strc_rendering_local_depth
       \fi
     \fi
   \fi
   \egroup
   \egroup
   \ifconditional\headisdisplay
     \useindentnextparameter\headparameter
   \else
     \nonoindentation % recently added, was a bug
   \fi}

% nice testcase
%
% \setupheads[aligntitle=yes]
%
% \startnarrower
%   \subject{\dorecurse{100}{x }}
%   \section{\dorecurse{100}{x }}
%   \input tufte \par
%   \setupheads[alternative=inmargin]
%   \subject{\dorecurse{100}{x }}
%   \section{\dorecurse{100}{x }}
%   \input tufte \par
% \stopnarrower

% \dodefineheadplacement[sectiona][vertical]{#1->#2}
% \dodefineheadplacement[sectionb][vertical]#1#2{#1->#2}
%
% \setuphead[section][alternative=sectiona]
% \setuphead[subsection][alternative=sectionb]

% \startsetups[\??headrenderings:\v!vertical:\v!sectiona]
%     ... there will be a more public namespace
% \stopsetups

\installcorenamespace{headplacementalternative}
\installcorenamespace{headrenderings}
\installcorenamespace{headalternative}

\installcommandhandler \??headalternative {headalternative} \??headalternative % or just \??head

\setupheadalternative
  [%\c!width=\headparameter\c!width,
   %\c!distance=\headparameter\c!distance,
   \c!alternative=\v!vertical,
   \c!renderingsetup=\??headrenderings:\currentheadalternative]

\let\currentheadalternative         \v!normal
\let\currentheadrenderingsetup      \empty
\let\currentheadrenderingalternative\v!vertical

\unexpanded\def\defineheadplacement % old mechanism
  {\dodoubleargument\strc_rendering_define_placement}

\def\strc_rendering_define_placement[#1][#2]%
  {\doifnextbgroupelse
     {\strc_rendering_define_placement_yes[#1][#2]}%
     {\strc_rendering_define_placement_nop[#1][#2]}}

\def\strc_rendering_define_placement_yes[#1][#2]%
  {\defineheadalternative[#1][\c!alternative=#2,\c!renderingsetup=\??headrenderings:\v!command]%
   \setuvalue{\??headplacementalternative#1}##1##2}

\def\strc_rendering_define_placement_nop[#1][#2]%
  {\defineheadalternative[#1][\c!alternative=#2,\c!renderingsetup=\??headrenderings:\v!command]%
   \setuvalue{\??headplacementalternative#1}}

% these can be used in setups:
%
% \headnumbercontent
% \headtextcontent
%
% \headwidth
% \headtextwidth
% \headnumberdistance
% \headnumberwidth
% \headsetupspacing
%
% \headshownumber
% \headisdisplay

\let\headnumbercontent\empty
\let\headtextcontent  \empty

\newdimen\headwidth
\newdimen\headtextwidth
\newdimen\headnumberdistance
\newdimen\headnumberwidth

% \newconditional\headshownumber % defined already
% \newconditional\headisdisplay  % defined already

\unexpanded\def\strc_rendering_initialize_alternatives
  {\edef\currentheadalternative         {\headparameter           \c!alternative}%
   \edef\currentheadrenderingsetup      {\headalternativeparameter\c!renderingsetup}%
   \edef\currentheadrenderingalternative{\headalternativeparameter\c!alternative}%
   \ifx\currentheadrenderingalternative\empty
     \let\currentheadrenderingalternative\v!vertical
   \fi
   \ifx\currentheadrenderingalternative\v!horizontal
     \global\setfalse\headisdisplay % global
   \else
     \global\settrue\headisdisplay  % global
   \fi}

\unexpanded\def\strc_rendering_initialize_dimensions
  {\headwidth         \headparameter\c!width      \relax  % \zeropoint == unset
   \headnumberwidth   \headparameter\c!numberwidth\relax  % \zeropoint == unset
   \headnumberdistance\headparameter\c!distance   \relax
   \headtextwidth     \headparameter\c!textwidth  \relax} % \zeropoint == unset

\unexpanded\def\headtextcontent
  {\begingroup
     \strc_rendering_initialize_style_and_color\c!textstyle\c!textcolor
     \headparameter\c!commandbefore\relax
     \ifcsname\currentheadhash\c!deeptextcommand\endcsname
       \expandafter\let\expandafter\deepstructuretitlecommand\csname\currentheadhash\c!deeptextcommand\endcsname
     \fi
     \ifconditional\headisdisplay
       % struts can be nilled with \setnostrut
       \headparameter\c!textcommand{\setstrut\begstrut\getheadtitle\endstrut}%
       \global\d_strc_rendering_local_height\strutht
       \global\d_strc_rendering_local_depth\strutdp
       \global\d_strc_rendering_local_lineheight\lineheight
       \headparameter\c!commandafter\relax
       \endgraf
     \else
       \headparameter\c!textcommand{\getheadtitle}%
       \headparameter\c!commandafter\relax
     \fi
   \endgroup}

\unexpanded\def\headnumbercontent
  {\begingroup
     \strc_rendering_initialize_style_and_color\c!numberstyle\c!numbercolor
     \ifcsname\currentheadhash\c!deepnumbercommand\endcsname
       \expandafter\let\expandafter\deepstructurenumbercommand\csname\currentheadhash\c!deepnumbercommand\endcsname
     \fi
     \ifconditional\headisdisplay
       % can be nilled with \setnostrut
       \headparameter\c!numbercommand{\setstrut\begstrut\getheadnumber\endstrut}%
     \else
       \headparameter\c!numbercommand{\getheadnumber}%
     \fi
   \endgroup}

\unexpanded\def\strc_rendering_inject_number_and_text
  {\edef\p_command{\headparameter\c!command}% assumes \unexpanded definition
   \ifx\p_command\empty
     \directsetup\currentheadrenderingsetup
   \else
     \p_command\headnumbercontent\headtextcontent
   \fi}

\unexpanded\def\strc_rendering_inject_text
  {\edef\p_command{\headparameter\c!command}% assumes \unexpanded definition
   \ifx\p_command\empty
     \directsetup\currentheadrenderingsetup
   \else
     \p_command\empty\headtextcontent
   \fi}

\startsetups[\??headrenderings:\v!command]
    \csname\??headplacementalternative\currentheadalternative\endcsname \headnumbercontent \headtextcontent
\stopsetups

% obsolete
%
% \def\normalplacehead % hooks into \c!command
%   {\csname\??headplacementalternative\ifcsname\??headplacementalternative\currentheadalternative\endcsname\currentheadalternative\else\v!normal\fi\endcsname}

\defineheadalternative
  [\v!paragraph]
  [\c!alternative=\v!vertical,
   \c!renderingsetup=\??headrenderings:\v!paragraph]

\startsetups[\??headrenderings:\v!paragraph]
    \vbox {
        \headsetupspacing
        \begstrut
        \ifconditional\headshownumber % \ifheadnumbercontent
            \headnumbercontent
            \hskip\headnumberdistance
        \fi
        \headtextcontent
    }
\stopsetups

% \setuphead
%   [chapter]
%   [numberwidth=2cm,hang=line,after={\blank[3*line]}]
%
% \chapter{Oeps oeps oeps} \input tufte   \section{Oeps}
% \chapter{Oeps oeps oeps} \section{Oeps} \input tufte

\defineheadalternative
  [\v!normal]
  [\c!alternative=\v!vertical,
   \c!renderingsetup=\??headrenderings:\v!normal]

\startsetups[\??headrenderings:\v!normal]
    \vbox {
        \headsetupspacing
        \ifconditional\headshownumber
            \ifdim\headwidth=\zeropoint \else
                \ifdim\headnumberwidth=\zeropoint
                    \ifdim\headtextwidth=\zeropoint \else
                        \headnumberwidth\dimexpr\headwidth-\headtextwidth\relax
                    \fi
                \else
                    \ifdim\headtextwidth=\zeropoint
                        \headtextwidth\dimexpr\headwidth-\headnumberwidth\relax
                    \fi
                \fi
                \hsize\headwidth
            \fi
            \ifdim\headnumberwidth=\zeropoint\else
                \headnumberdistance\zeropoint
            \fi
            \setbox\scratchbox\hbox \ifdim\headnumberwidth=\zeropoint\else to \headnumberwidth\fi{\headnumbercontent}
            \scratchdimen\dimexpr\wd\scratchbox+\headnumberdistance\relax
            \ifdim\headtextwidth=\zeropoint\else
                \hsize\dimexpr\scratchdimen+\headtextwidth\relax
            \fi
            \hangindent\scratchdimen
            \hangafter \plusone
            \noindent
            \box\scratchbox
            \hskip\headnumberdistance
        \else
            \ifdim\headtextwidth=\zeropoint
                \ifdim\headwidth=\zeropoint \else
                    \hsize\headwidth
                \fi
            \else
                \hsize\headtextwidth
            \fi
            \noindent
        \fi
        \headtextcontent
    }
\stopsetups

\defineheadalternative
  [\v!inmargin]
  [\c!alternative=\v!vertical,
   \c!renderingsetup=\??headrenderings:\v!inmargin]

\startsetups[\??headrenderings:\v!inmargin]
    \vbox {
        \headsetupspacing
        \begstrut % use one \strut here!
        \dontleavehmode % in case there is no strut, else side effects with llap
        \ifconditional\headshownumber
            \llap {
                \hbox {
                    \hfill
                    \headnumbercontent
                    \hskip\dimexpr\d_strc_rendering_local_leftoffset+\doifoddpageelse\leftmargindistance\rightmargindistance\relax
                }
            }
        \fi
        \headtextcontent
    }
\stopsetups

\defineheadalternative
  [\v!margin]
  [\v!inmargin]

% \startsetups[\??headrenderings:\v!vertical:\v!margin]
%     \directsetup{\??headrenderings:\v!vertical:\v!inmargin}
% \stopsetups

\defineheadalternative
  [\v!middle]
  [\c!alternative=\v!vertical,
   \c!renderingsetup=\??headrenderings:\v!middle]

\startsetups[\??headrenderings:\v!middle]
    \vbox {
        \headsetupspacing
        \veryraggedcenter
        \let\\\endgraf
        \let\crlf\endgraf
        \ifconditional\headshownumber
            \strut
            \headnumbercontent
            \par
        \fi
        \begstrut
        \headtextcontent
        \endstrut
    }
\stopsetups

\defineheadalternative
  [\v!text]
  [\c!alternative=\v!horizontal,
   \c!renderingsetup=\??headrenderings:\v!text]

\startsetups[\??headrenderings:\v!text]
    \begingroup
    \headsetupspacing % no stretch in distance
    \ifconditional\headshownumber
        \headnumbercontent
        \kern\headnumberdistance
    \fi
    \begstrut
    \headtextcontent
    \endstrut
    \endgroup
\stopsetups

% onder/boven lijnt het nummer op de onderste/bovenste regel uit van een meerregelige kop

\defineheadalternative
  [\v!bottom]
  [\c!alternative=\v!vertical,
   \c!renderingsetup=\??headrenderings:\v!bottom]

\startsetups[\??headrenderings:\v!bottom]
    \ifconditional\headshownumber
        \setbox0\hbox {
            \headnumbercontent
        }
        \setbox2\vbox {
            \headsetupspacing
            \advance\hsize-\wd0\relax
            \headtextcontent
        }
        \hbox {
            \box0
            \hskip\headnumberdistance
            \box2
        }
    \else
        \vbox {
            \headsetupspacing
            \noindent
            \headtextcontent
        }
    \fi
\stopsetups

\defineheadalternative
  [\v!top]
  [\c!alternative=\v!vertical,
   \c!renderingsetup=\??headrenderings:\v!top]

\startsetups[\??headrenderings:\v!top]
    \ifconditional\headshownumber
        \setbox0\hbox {
            \headnumbercontent
        }
        \setbox2\vtop {
            \headsetupspacing
            \advance\hsize-\wd0\relax
            \headtextcontent
        }
        \hbox {
            \box0
            \hskip\headnumberdistance
            \box2
        }
    \else
        \vtop{
            \headsetupspacing
            \noindent
            \headtextcontent
        }
    \fi
\stopsetups

\protect \endinput