summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/lowlevel/lowlevel-localboxes.tex
blob: a0ca159755e4b0eafc4958b565d1cfbcacf5d8c2 (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
% language=us runpath=texruns:manuals/lowlevel

% \unprotect \pushoverloadmode
% \popoverloadmode \protect

\environment lowlevel-style

\startdocument
  [title=localboxes,
   color=middlered]

\startsectionlevel[title=Introduction]

The \LUATEX\ engine inherited a few features from other engines and adding local
boxes to paragraphs is one of them. This concept comes from \OMEGA\ but over time
it has been made a bit more robust, also by using native par nodes instead of
whatsit nodes that are used to support \TEX's extensions. In another low level
manual we discuss paragraph properties and these local boxes are also part of
that so you might want to catch up on that. Local boxes are stored in an initial
par node with an adequate subtype but users wont' notice this (unless they mess
around in \LUA). The inline par nodes have a different subtype and are injected
with the \typ {\localinterlinepenalty}, \typ {\localbrokenpenalty}, \typ
{\localleftbox}, \typ {\localrightbox} and \LUAMETATEX\ specific \typ
{\localmiddlebox} primitives. WHen these primitives are used in vertical mode
they just set registers.

The original (\OMEGA) idea was that local boxes are used for repetitive
punctuation (like quotes) at the left and|/|or right end of the lines that make
up a paragraph. That means that when these primitives inject nodes they actually
introduce states so that a stretch of text can be marked.

When this mechanism was cleaned up in \LUAMETATEX\ I decided to investigate if
other usage made sense. After all, it is a feature that introduces some extra
code and it then pays of to use it when possible. Among the extensions are a
callback that is triggered when the left and right boxes get added and
experiments with that showed some potential but in order to retain performance as
well as limit extensive node memory usage (par nodes are large) a system of
indices was added. All this will be illustrated below. Warning: the mechanism in
\LUAMETATEX\ is not compatible with \LUATEX.

{\em This is a preliminary, uncorrected manual.}

\stopsectionlevel

\startsectionlevel[title=The basics]

This mechanism uses a mix of setting (pseudo horizontal) box registers that get
associated with (positions in a) paragraph. When the lines resulting from
breaking the list gets packaged into an horizontal (line) box, the local left and
right boxes get prepended and appended to the textual part (inside the left,
right and parfills kips and left or right hanging margins). When assigning the
current local boxes to the paragraph node(s) references to the pseudo registers
are used and the packaging actually copies them. This mix of referencing and
copying is somewhat tricky but the engine does it best to hide this for the user.

This mechanism is rather useless when not wrapped into some high level mechanism
because by default setting these boxes wipes the existing value. In \LUAMETATEX\
you can actually access the boxes so prepending and appending is possible but
experiments showed that this could come with a huge performance hit when the
lists are not cleaned up during a run. This is why we have introduced indices:
when you assign local boxes using the index option that specific index will be
replaced and therefore we have a more sparse solution. So, contrary to \LUATEX,
in \LUAMETATEX\ the local box registers have a linked lists of local boxes tagged
by index. Unless you manipulate in \LUA, this is hidden from the user. One can
access the boxes from the \TEX\ the but there can be no confusion with \LUATEX\
here because there we don't have access. This is why usage as in \LUATEX\ will
also work in \LUAMETATEX.

This mechanism obeys grouping as is demonstrated in the next three examples. The
first example is:

\startbuffer[example-1]
\start
    \dorecurse{10}{test #1.1 }
    \localleftbox{\blackrule[width=2em,color=darkred] }
    \dorecurse{20}{test #1.2 }
    \removeunwantedspaces
    \localrightbox{ \blackrule[width=3em,color=darkblue]}
    \dorecurse{20}{test #1.3 }
\stop
    \dorecurse{20}{test #1.4 }
    % par ends here
\stopbuffer

\typebuffer[example-1][option=TEX]

The next example differs in a subtle way: watch the \type {keep} keyword,
it makes the setting retain after the group ends.

\startbuffer[example-2]
\start
    \start
        \dorecurse{10}{test #1.1 }
        \localleftbox keep {\blackrule[width=2em,color=darkred] }
        \dorecurse{20}{test #1.2 }
        \removeunwantedspaces
        \localrightbox { \blackrule[width=3em,color=darkblue]}
        \dorecurse{20}{test #1.3 }
    \stop
        \dorecurse{20}{test #1.4 }
\stop
% par ends here
\stopbuffer

\typebuffer[example-2][option=TEX]

The third example has two times \type {keep}. This option is \LUAMETATEX\
specific.

\startbuffer[example-3]
\start
    \start
        \dorecurse{10}{test #1.1 }
        \localleftbox keep {\blackrule[width=2em,color=darkred] }
        \dorecurse{20}{test #1.2 }
        \removeunwantedspaces
        \localrightbox keep { \blackrule[width=3em,color=darkblue]}
        \dorecurse{20}{test #1.3 }
    \stop
        \dorecurse{20}{test #1.4 }
\stop
% par ends here
\stopbuffer

\typebuffer[example-3][option=TEX]

\startplacefigure % [location=page]
    \startcombination[nx=1,ny=3]
        {\vbox{\hsize\textwidth\getbuffer[example-1]}} {\bf Example 1}
        {\vbox{\hsize\textwidth\getbuffer[example-2]}} {\bf Example 2}
        {\vbox{\hsize\textwidth\getbuffer[example-3]}} {\bf Example 3}
    \stopcombination
\stopplacefigure

One (nasty) side effect is that when you set these boxes ungrouped they are
applied to whatever follows, which is why resetting them is built in the relevant
parts of \CONTEXT. The next examples are typeset grouped an demonstrate the use
of indices:

\startbuffer
\dorecurse{20}{before #1 }
\localleftbox{\bf \darkred L 1 }%
\localleftbox{\bf \darkred L 2 }%
\dorecurse{20}{after #1 }
\stopbuffer

\typebuffer[option=TEX] \start \getbuffer \par \stop

Indices can be set for both sides:

\startbuffer
\dorecurse{5}{\localrightbox index #1{ \bf \darkgreen R #1}}%
\dorecurse{20}{before #1 }
\dorecurse{5}{\localleftbox index #1{\bf \darkred L #1 }}%
\dorecurse{20}{after #1 }
\stopbuffer

\typebuffer[option=TEX] \start \getbuffer \par \stop

We can instruct this mechanism to hook the local box into the main
par node by using the \type {par} keyword. Keep in mind that these
local boxes only come into play when the lines are broken, so till
then changing them is possible.

\startbuffer
\dorecurse{3}{\localrightbox index #1{ \bf \darkgreen R #1}}%
\dorecurse{20}{before #1 }
\dorecurse{2}{\localleftbox par index #1{\bf \darkred L #1 }}%
\dorecurse{20}{after #1 }
\stopbuffer

\typebuffer[option=TEX] \start \getbuffer \par \stop

\stopsectionlevel

\startsectionlevel[title=The interface]

{\em The interface described here is experimental.}

Because it is hard to foresee if this mechanism will be used at all the \CONTEXT\
interface is somewhat low level: one can build functionality on top of it. In the
previous section we saw examples of local boxes being part of the text but one
reason for extending the interface was to see if we can also use this engine
feature for efficiently placing marginal content.

\startbuffer[definition]
\definelocalboxes
  [lefttext]
  [location=lefttext,width=3em,color=darkblue]
\definelocalboxes
  [lefttextx]
  [location=lefttext,width=3em,color=darkblue]

\definelocalboxes
  [righttext]
  [location=righttext,width=3em,color=darkyellow]
\definelocalboxes
  [righttextx]
  [location=righttext,width=3em,color=darkyellow]
\stopbuffer

\typebuffer[definition][option=TEX]

\getbuffer[definition]

The order of definition matters! Here the \type {x} variants have a larger index
number. There can (currently) be at most 256 indices. The defined local boxes
are triggered with \type {\localbox}:

\startbuffer[example]
\startnarrower
\dorecurse{20}{before #1 }%
\localbox[lefttext]{[L] }%
\localbox[lefttextx]{[LL] }%
\localbox[righttext]{ [RR]}%
\localbox[righttextx]{ [R]}%
\dorecurse{20}{ after #1}%
\stopnarrower
\stopbuffer

\typebuffer[example][option=TEX]

Watch how we obey the margins:

\getbuffer[example]

Here these local boxes have dimensions. The predefined margin variants are
virtual. Here we set up the style and color:

\startbuffer[definition]
\setuplocalboxes
  [leftmargin]
  [style=\bs,
   color=darkgreen]
\setuplocalboxes
  [rightmargin]
  [style=\bs,
   color=darkred]
\stopbuffer

\typebuffer[definition][option=TEX]

\startbuffer[example]
\dorecurse{2}{
    \dorecurse{10}{some text #1.##1 }%
    KEY#1.1%
    \localmargintext[leftmargin]{L #1.1}%
    \localmargintext[rightmargin]{R #1.1}%
    \dorecurse{10}{some text #1.##1 }%
    KEY#1.2%
    \localmargintext[leftmargin]{L #1.2}%
    \localmargintext[rightmargin]{R #1.2}%
    \dorecurse{10}{some text #1.##1 }%
    \blank
}
\stopbuffer

\typebuffer[example][option=TEX]

You can also use \type {leftedge} and \type {rightedge} but using them here would
put them outside the page.

{\getbuffer[definition,example]}

In previous examples you can see that setting something at the left will lag behind
so deep down we use another trick here: \type {\localmiddlebox}. When these boxes
get placed a callback can be triggered and in \CONTEXT\ we use that to move these
middle boxes to the margins.

Next we implement line numbers. Watch out: this will not replace the existing
mechanisms, it's just an alternative as we have alternative table mechanisms. We
have a repertoire of helpers for constructing the result:

\startbuffer[definition]
\definelocalboxes
  [linenumberleft]
  [command=\LeftNumber,
   location=middle,
   distance=\leftmargindistance,
   width=3em,
   style=\bs,
   color=darkred]

\definelocalboxes
  [linenumberright] % [linenumberleft]
  [command=\RightNumber,
   location=middle,
   distance=\rightmargindistance,
   width=3em,
   style=\bf,
   color=darkgreen]

\definecounter[MyLineNumberL]
\definecounter[MyLineNumberR]

\setupcounter
  [MyLineNumberL]
  [numberconversion=characters]

\setupcounter
  [MyLineNumberR]
  [numberconversion=romannumerals]

\def\LineNumberL
  {\incrementcounter[MyLineNumberL]%
   \convertedcounter[MyLineNumberL]}

\def\LineNumberR
  {\incrementcounter[MyLineNumberR]%
   \convertedcounter[MyLineNumberR]}

\protected\def\LeftNumber
  {\setbox\localboxcontentbox\hbox
     to \localboxesparameter{width}
     {(\LineNumberL\hss\strut)}%
   \localmarginlefttext\zeropoint}

\protected\def\RightNumber
  {\setbox\localboxcontentbox\hbox
     to \localboxesparameter{width}
     {(\strut\hss\LineNumberR)}%
   \localmarginrighttext\zeropoint}
\stopbuffer

\typebuffer[definition][option=TEX]

\startbuffer[example]
\localbox[linenumberleft]{}%
\localbox[linenumberright]{}%
\dorecurse{2}{
    \samplefile{tufte}
    \par
}
\resetlocalbox[linenumberleft]%
\resetlocalbox[linenumberright]%
\stopbuffer

\typebuffer[example][option=TEX]

We use our tufte example to illustrate the usage:

\getbuffer[definition]

{\getbuffer[example]}

For convenience we support ranges like this (we've reset the line number counters
here):

\resetcounter[MyLineNumberL]
\resetcounter[MyLineNumberR]

\startbuffer[example]
\startlocalboxrange[linenumberleft]%
\startlocalboxrange[linenumberright]%
\dorecurse{2}{
    \samplefile{tufte}
    \par
}
\stoplocalboxrange
\stoplocalboxrange
\stopbuffer

\typebuffer[example][option=TEX]

{\getbuffer[example]}

\stopsectionlevel

\startsectionlevel[title=The helpers]

For the moment we have these helpers:

\starttabulate[|l|;|]
\NC \type {\localboxindex}            \NC integer   \NC \NR
\NC \type {\localboxlinenumber}       \NC integer   \NC \NR
\NC
\NC \type {\localboxlinewidth}        \NC dimension \NC \NR
\NC \type {\localboxlocalwidth}       \NC dimension \NC \NR
\NC \type {\localboxprogress}         \NC dimension \NC \NR
\NC \type {\localboxleftoffset}       \NC dimension \NC \NR
\NC \type {\localboxrightoffset}      \NC dimension \NC \NR
\NC
\NC \type {\localboxleftskip}         \NC dimension \NC \NR
\NC \type {\localboxrightskip}        \NC dimension \NC \NR
\NC \type {\localboxlefthang}         \NC dimension \NC \NR
\NC \type {\localboxrighthang}        \NC dimension \NC \NR
\NC
\NC \type {\localboxindent}           \NC dimension \NC \NR
\NC \type {\localboxparfillleftskip}  \NC dimension \NC \NR
\NC \type {\localboxparfillrightskip} \NC dimension \NC \NR
\NC \type {\localboxovershoot}        \NC dimension \NC \NR
\NC
\stoptabulate

The progress and offsets are accumulated values of the normalized indent, hangs,
skips etc. The line number is the position in the paragraph. In the callback we
set the box register \type {\localboxcontentbox} and use it after the command has
been applied. In the line number example you can see how we set its final
content, so these boxes are sort of dynamic. Normally in the middle case no
content is passed and in the par builder a middle is not taken into account when
calculating the line width.

\stopsectionlevel

\stopdocument


%     implement { name = "localboxmarkonce",