summaryrefslogtreecommitdiff
path: root/tex/context/base/strc-ren.mkiv
blob: 04782fe7b08e8dc621d23a0b67b3d8d2771da509 (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
%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 / Hans Hagen]
%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

\chardef\headtimingmode=0

% \chardef\headtimingmode=1 % 0 also works ok now too
%
% 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}

\newevery \everyheadstart \relax

\def\placeheadmargintexts
  {\the\everyheadstart
   \doif{\structureheadparameter\c!margintext}\v!yes\placemargincontent}

\def\setupheadcomponentfont#1#2%
  {\ifconditional\structureheadisdisplay
     \edef\askedstructureheadinterlinespace{\structureheadparameter\c!interlinespace}%
     \ifx\askedstructureheadinterlinespace\empty
       % here the interline space is only set when style sets no space
       \setfalse\fontattributeisset
       \setfalse\interlinespaceisset
       \dosetstructureheadattributes\c!style\c!color
       \ifconditional\fontattributeisset \ifconditional\interlinespaceisset \else
         \setupinterlinespace
       \fi \fi
       \setfalse\fontattributeisset \dosetstructureheadattributes#1#2%
       \ifconditional\fontattributeisset \ifconditional\interlinespaceisset \else
         \setupinterlinespace
       \fi \fi
     \else
       % here the set interline space overloads any other set space in the style
       \setfalse\fontattributeisset
       \dosetstructureheadattributes\c!style\c!color
       \ifconditional\fontattributeisset
         \dosetupcheckedinterlinespace\askedstructureheadinterlinespace
       \fi
       \setfalse\fontattributeisset
       \dosetstructureheadattributes#1#2%
       \ifconditional\fontattributeisset
         \dosetupcheckedinterlinespace\askedstructureheadinterlinespace
       \fi
     \fi
   \else
     \setfalse\fontattributeisset
     \dosetstructureheadattributes\c!style\c!color
     \ifconditional\fontattributeisset
       \setupspacing
     \fi
     \setfalse\fontattributeisset
     \dosetstructureheadattributes#1#2%
     \ifconditional\fontattributeisset
       \setupspacing
     \fi
   \fi
   \dontconvertfont}

\def\doplaceheadtextcomponent#1#2%
  {\begingroup
     \setupheadcomponentfont\c!textstyle\c!textcolor
     % \ifcase\headtimingmode#1\fi % can introduce cr
     \structureheadparameter\c!commandbefore
     \placeheadmargintexts
     \ifconditional\structureheadisdisplay % \ifdisplaysectionhead
       \structureheadparameter\c!textcommand% struts can be nilled with \setnostrut
         {\setstrut
          \begstrut
          \ifcase\headtimingmode\hbox{#1}\fi
          \executeifdefined{\??nh\currentstructurehead\c!deeptextcommand}\firstofoneargument{#2}%
          \endstrut}% \hbox prevents break
       \xdef\localheadheight    {\the\strutht}%
       \xdef\localheaddepth     {\the\strutdp}%
       \xdef\localheadlineheight{\the\lineheight}%
       % == \globallet\localheaddepth\strutdepth
     \else
       \ifcase\headtimingmode#1\fi
       \structureheadparameter\c!textcommand
          {\executeifdefined{\??nh\currentstructurehead\c!deeptextcommand}\firstofoneargument{#2}}%
     \fi
     \structureheadparameter\c!commandafter
     \ifconditional\structureheadisdisplay % \ifdisplaysectionhead
       \endgraf
     \fi
   \endgroup}

\def\doplaceheadnumbercomponent#1#2%
  {\begingroup
     \setupheadcomponentfont\c!numberstyle\c!numbercolor
     % \getvalue{\??ko\currentstructurehead\c!commandbefore}% strange, why here? moved 21/11/2005
     \placeheadmargintexts
     \ifconditional\structureheadisdisplay % \ifdisplaysectionhead
       % can be nilled with \setnostrut
       \structureheadparameter\c!numbercommand
         {\setstrut
          \begstrut
          \executeifdefined{\??nh\currentstructurehead\c!deepnumbercommand}\firstofoneargument{#2}%
          \endstrut}%
     \else
       \structureheadparameter\c!numbercommand
         {\executeifdefined{\??nh\currentstructurehead\c!deepnumbercommand}\firstofoneargument{#2}}%
     \fi
   \endgroup}

% \newif\ifheadnumbercontent
% \newif\ifemptyhead
% \newif\ifdisplaysectionhead

\def\structureheadattribute{\iflocation attr \destinationattribute \currentstructureattribute\fi}

\def\doplacestructureheadtext#1#2#3% nodes, text, endstuff
  {\beginheadplacement
   \doresettructureheadnumbercontent
   \ifconditional\structureheadleaveempty
     \setbox\sectionheadbox\ifvertical\vbox\else\hbox\fi \structureheadattribute to \zeropoint {#1}%
     \makestrutofbox\sectionheadbox
   \else
     \setbox\sectionheadbox\ifvertical\vbox\else\hbox\fi \structureheadattribute
       {\doresettructureheadnumbercontent
        \ifcase\headtimingmode\or#1\fi                          % outerside font determines distance
        \dosetfontattribute{\??nh\currentstructurehead}\c!style % but we don't want color to influence user command, todo: get the if-else out of it
        \structureheadparameter\c!command{}{\doplaceheadtextcomponent{#1}{#2}}}%
   \fi
   \endheadplacement{#3}}

\def\doplacestructureheadnumbertext#1#2#3#4% nodes number text nodes
  {\beginheadplacement
   \doiftextelse{#2}\dosettructureheadnumbercontent\doresettructureheadnumbercontent
   \ifconditional\structureheadleaveempty
     \setbox\sectionheadbox\ifvertical\vbox\else\hbox\fi \structureheadattribute to \zeropoint{#1}%
     \makestrutofbox\sectionheadbox
   \else % = needed
     \setbox\sectionheadbox\ifvertical\vbox\else\hbox\fi \structureheadattribute
       {\ifcase\headtimingmode\or#1\fi
        \dosetfontattribute{\??nh\currentstructurehead}\c!style
        \structureheadparameter\c!command{\doplaceheadnumbercomponent{#1}{#2}}{\doplaceheadtextcomponent{#1}{#3}}}%
   \fi
   \endheadplacement{#4}}

\def\placestructureheadnumbertext
  {\doplacestructureheadnumbertext\empty\getstructureheadnumber\getstructureheadtitle\getstructureheadsyncs}

\def\placestructureheadtext
  {\doplacestructureheadtext\empty\getstructureheadtitle\getstructureheadsyncs}

\def\placestructureheadempty
  {\hbox \structureheadattribute {\getstructureheadsyncs}}

%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

\let\headlastlinewidth\!!zeropoint

\def\localheadheight    {\strutht}
\def\localheaddepth     {\strutdp}
\def\localheadlineheight{\lineheight}

\def\dolocalheadsetup  %  koppeling met standaard kopcommando / engels
  {\forgetall          %  traag dus ...
   \doifsomething{\structureheadparameter\c!align    }     {\normalexpanded{\noexpand\setupalign    [\structureheadparameter\c!align    ]}}%
   \doifsomething{\structureheadparameter\c!tolerance}     {\normalexpanded{\noexpand\setuptolerance[\structureheadparameter\c!tolerance]}}%
   \doif         {\structureheadparameter\c!strut    }\v!no{\setnostrut}% new
   \def\\{\crlf\strut\ignorespaces}}

\def\beginheadplacement
  {\bgroup
   \setsystemmode\currentstructurehead
   \xdef\localheadheight    {\the\strutht}%
   \xdef\localheaddepth     {\the\strutdp}%
   \xdef\localheadlineheight{\the\lineheight}%
   % == \globallet\localheaddepth\strutdp
   \everypar\emptytoks % needed indeed
   \noindent           % ipv \whitespace elders, na \forgetall !
   \bgroup
   \doifinsetelse{\structureheadparameter\c!aligntitle}{\v!yes,\v!float}% new
     {\skip0 1\leftskip
      \skip2 1\rightskip
      \xdef\localheadskip{\the\skip0}%
      \forgetall
      \leftskip\skip0
      \rightskip\skip2
      \setlocalhsize\hsize\localhsize
      \forgetbothskips}
     {\globallet\localheadskip\!!zeropoint
      \forgetall}%
   \dontcomplain
   \postponenotes
   \iflocation
     \ifconditional\structureheadisdisplay % \ifdisplaysectionhead
     \else
       \noninterferingmarks
     \fi
   \fi
   \resetinteractionparameter\c!style
   \resetinteractionparameter\c!color
   \resetinteractionparameter\c!contrastcolor
   %\strictouterreferencestrue % tzt instelling
   \let\localheadsetup\dolocalheadsetup}

% \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

\def\hangheadplacement
  {\scratchdimen\localheadlineheight
   \bgroup
   \openlineheight\scratchdimen
   \scratchdimen\htdp0%
   \getnoflines\scratchdimen
   \advance\noflines\minusone
   \normalexpanded{\egroup\noflines\the\noflines}% brrr
   \setbox0\hbox{\lower\noflines\scratchdimen\box0}%
   \scratchdimen\dimexpr\htdp0-\localheadheight+\strutdp\relax
   \ht0 \strutht
   \dp0 \strutdp
   \edef\localheaddepth{\the\strutdp}}

\newconditional\continuoussectionhead % oeps, \newif\ifcontinuoushead got lost
\newbox\sectionheadbox

\def\endheadplacement#1%
  {\noflines\zerocount
   \ifconditional\structureheadisdisplay % \ifdisplaysectionhead
     % new (todo tight == one following line up)
     \processaction
       [\structureheadparameter\c!hang]
       [    \v!line=>\hangheadplacement\noflines\zerocount,
           \v!broad=>\hangheadplacement\getnoflines\scratchdimen,
             \v!fit=>\hangheadplacement\getrawnoflines\scratchdimen,
            \v!none=>\noflines\zerocount,
         \v!default=>\noflines\zerocount,
         \v!unknown=>\hangheadplacement\noflines\numexpr0\commalistelement-1\relax]%
     % so far
     \let\headlastlinewidth\!!zeropoint
     % kind of special, we want to snap heads also according to local specs local
     \ifgridsnapping
       \begingroup
       \edef\currentstructureheadgridsnapping{\structureheadparameter\c!grid}%
       \ifconditional\structureheadisdisplay
         \ifx\currentstructureheadgridsnapping\empty\else
           \dosetstructureheadattributes\c!style\c!color
           \setupinterlinespace
           \dosetstructureheadattributes\c!textstyle\c!textcolor
           \setupinterlinespace
         \fi
       \fi
       \snaptogrid[\currentstructureheadgridsnapping]\hbox
         {\hskip\localheadskip\hskip\structureheadparameter\c!margin\box\sectionheadbox}%
       \endgroup
     \else
       \hbox
         {\hskip\localheadskip\hskip\structureheadparameter\c!margin\box\sectionheadbox}%
     \fi
     \flushnotes % new, not really needed
     \endgraf
     \ifvmode
       \ifnum\noflines>\zerocount
         \dorecurse\noflines{\nointerlineskip\dosomebreak\nobreak\strut\endgraf}% to be checked
       \fi
       \nointerlineskip
       \dosomebreak\nobreak
     \fi
     #1%
   \else
     \strut
     \flushnotes % new, here since we're in par mode
     \unhbox\sectionheadbox
     \globallet\headlastlinewidth\!!zeropoint
     #1%
     \hskip\numberheaddistance\!!plus\numberheaddistance\!!minus.25\dimexpr\numberheaddistance\relax
     \hskip\continuousstructureheadsignal\ignorespaces
   \fi
   \ifconditional\structureheadisdisplay % \ifdisplaysectionhead
     \ifvmode
       \ifgridsnapping % important, font related depth, see comment
         \prevdepth\strutdp
       \else
         \prevdepth\localheaddepth
       \fi
     \fi
   \fi
   \egroup
   \egroup
   \ifconditional\structureheadisdisplay % \ifdisplaysectionhead
     \checknextindentation[\structureheadparameter\c!indentnext]%
   \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

\let\numberheadalternative\v!normal

\def\defineheadplacement
  {\dodoubleargument\dodefineheadplacement}

\def\dodefineheadplacement[#1][#2]% #3#4
  {\setvalue{\??ns:#1}{#2}%
   \setvalue{\??ns::#1}}

\def\presetnumberheadalternative
  {\doifelsevalue{\??ns:\numberheadalternative}\v!horizontal\setfalse\settrue\structureheadisdisplay}

% \def\normalplacehead
%   {\executeifdefined
%      {\??ns::\numberheadalternative}
%      {\getvalue{\??ns::\v!normal}}}

\def\normalplacehead
  {\csname\??ns::\ifcsname\??ns::\numberheadalternative\endcsname\numberheadalternative\else\v!normal\fi\endcsname}

\defineheadplacement[\v!paragraph][\v!vertical]#1#2%
  {\vbox
     {\localheadsetup
      \begstrut
      \ifconditional\structureheadshownumber % \ifheadnumbercontent
        #1\hskip\numberheaddistance
      \fi
      #2}}

% \defineheadplacement[\v!normal][\v!vertical]#1#2%
%   {\ifconditional\structureheadshownumber % \ifheadnumbercontent
%      \setbox0\hbox{{#1}\hskip\numberheaddistance}%
%      \vbox
%        {\localheadsetup
%         \hangindent 1\wd0
%         \hangafter 1
%         \noindent
%         \unhbox0    %  don't use \strut's here!
%         #2}%
%    \else
%      \vbox
%        {\localheadsetup\noindent#2}%
%    \fi}
%
% enhanced version:

% \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

\defineheadplacement[\v!normal][\v!vertical]#1#2%
  {\vbox
     {\localheadsetup
      \edef\headwidth      {\structureheadparameter\c!width      }%
      \edef\headnumberwidth{\structureheadparameter\c!numberwidth}%
      \edef\headtextwidth  {\structureheadparameter\c!textwidth  }%
      \ifconditional\structureheadshownumber % \ifheadnumbercontent
        \ifx\headwidth\empty
        \else
          \ifx\headnumberwidth\empty
            \ifx\headtextwidth\empty\else
              \edef\headnumberwidth{\the\dimexpr\headwidth-\headtextwidth\relax}%
            \fi
          \else
            \ifx\headtextwidth\empty
              \edef\headtextwidth{\the\dimexpr\headwidth-\headnumberwidth\relax}%
            \fi
          \fi
          \hsize\headwidth
        \fi
        \ifx\headnumberwidth\empty\else
          \let\numberheaddistance\!!zeropoint
        \fi
        \setbox\scratchbox\hbox \ifx\headnumberwidth\empty\else to \headnumberwidth\fi{{#1}}%
        \scratchdimen\dimexpr\wd\scratchbox+\numberheaddistance\relax
        \ifx\headtextwidth\empty\else
          \hsize\dimexpr\scratchdimen+\headparameter\c!textwidth\relax
        \fi
        \hangindent\scratchdimen
        \hangafter \plusone
        \noindent
        \box\scratchbox\hskip\numberheaddistance
      \else
        \ifx\headtextwidth\empty
          \ifx\headwidth\empty
          \else
            \hsize\headwidth
          \fi
        \else
          \hsize\headtextwidth
        \fi
        \noindent
      \fi
      #2}}

\def\placeheadmargin#1#2%
  {\vbox
     {\localheadsetup
      \begstrut % use one \strut here!
      \dontleavehmode % in case there is no strut, else side effects with llap
      \ifconditional\structureheadshownumber % \ifheadnumbercontent
        \llap{\hbox to 5em{\hfill{#1}\hskip\localheadskip\hskip\leftmargindistance}}% introduces whitespace
        % maybe better:
        % \inleftmargin{\hbox{\hss{#1}\hskip\localheadskip}}%
      \fi
      {#2}}}

\defineheadplacement[\v!inmargin][\v!vertical]#1#2{\placeheadmargin{#1}{#2}}
\defineheadplacement[\v!margin]  [\v!vertical]#1#2{\placeheadmargin{#1}{#2}}

\defineheadplacement[\v!middle][\v!vertical]#1#2%
  {\vbox
     {\localheadsetup
      \veryraggedcenter
      \let\\\endgraf
      \let\crlf\endgraf
      \ifconditional\structureheadshownumber % \ifheadnumbercontent
        \strut#1\par
      \fi
      \begstrut#2}}

\defineheadplacement[\v!text][\v!horizontal]#1#2%
  {\bgroup
   \localheadsetup % no stretch in distance
   \ifconditional\structureheadshownumber % \ifheadnumbercontent
     {#1}\kern\numberheaddistance
   \fi
   {\begstrut#2}%
   \egroup}

\def\placeheadlohi#1#2#3%
  {\ifconditional\structureheadshownumber % \ifheadnumbercontent
     \setbox0\hbox{#2}
     \setbox2=#1{\localheadsetup\advance\hsize-\wd0\relax#3}%
     \hbox{\box0\hskip\numberheaddistance\box2}%
   \else
     #1{\localheadsetup\noindent#3}%
   \fi}

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

\defineheadplacement[\v!bottom][\v!vertical]#1#2{\placeheadlohi\vbox{#1}{#2}}
\defineheadplacement[\v!top]   [\v!vertical]#1#2{\placeheadlohi\vtop{#1}{#2}}

\protect \endinput