anch-bck.mklx /size: 22 Kb    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=anch-bck, % moved from anch-pgr (1999.08.01)
3%D        version=2011.12.19,
4%D          title=\CONTEXT\ Anchoring Macros,
5%D       subtitle=Backgrounds,
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% when tested for a while: \MPgetmultipars -> getmultipars
15% when tested for a while: \MPvar          -> mpvar
16
17\writestatus{loading}{ConTeXt Anchoring Macros / Backgrounds}
18
19%D This module is quite okay but at some point I might add methods that use
20%D attributes although not too much is to be expected, apart from better anchoring.
21%D In fact better anchoring can be done independent of that.
22
23\unprotect
24
25% This might be overloaded later on:
26
27% \defineoverlay[\v!text-2][\positionoverlay{\v!text-2}]
28% \defineoverlay[\v!text-1][\positionoverlay{\v!text-1}]
29% \defineoverlay[\v!text+1][\positionoverlay{\v!text+1}]
30% \defineoverlay[\v!text+2][\positionoverlay{\v!text+2}]
31
32% Paragraph positions:
33
34\newinteger\c_anch_backgrounds_text_n
35
36\def\v_anch_backgrounds_text_current    {tbg:0}
37\def\v_anch_backgrounds_anchor_current_b{b:\v_anch_backgrounds_text_current}
38\def\v_anch_backgrounds_anchor_current_e{e:\v_anch_backgrounds_text_current}
39
40\def\anch_backgrounds_text_initialize_next
41  {\global\advanceby\c_anch_backgrounds_text_n\plusone
42   \edef\v_anch_backgrounds_text_current{tbg:\the\c_anch_backgrounds_text_n}}
43
44% The first position can be used in the middle of a paragraph in which case we're
45% too late with initializing par positions. Therefore we check if positions are
46% used at all.
47%
48% tricky: we need to catch newly set! otherwise an old run can have positions
49
50\protected\def\anch_backgrounds_text_initialize
51  {\doifelsepositionsused\enableparpositions\donothing
52   \glet\anch_backgrounds_text_initialize\relax}
53
54\appendtoks
55    \anch_backgrounds_text_initialize
56\to \everystarttext
57
58%D Some MP
59
60\newinteger\MPparcounter
61
62\permanent\def\MPself     {\MPvar{self}}
63\permanent\def\MPbself    {b:\MPvar{self}}
64\permanent\def\MPeself    {e:\MPvar{self}}
65\permanent\def\MPparanchor{p:\number\MPparcounter} % \the
66
67% \carryoverpar is needed for left/right floats
68
69% \definetextbackground[more][state=start,backgroundcolor=red] % location=paragraph
70% \definetextbackground[test][state=start,backgroundcolor=green]
71%
72% \page \placefigure[left]{}{}
73%
74% \starttextbackground[test]
75%   \readfile{ward}{}{}
76%   \starttextbackground[more]
77%     \readfile{ward}{}{}
78%   \stoptextbackground
79%   \readfile{ward}{}{}
80% \stoptextbackground
81%
82% \page \placefigure[right]{}{}
83%
84% \starttextbackground[test]
85%   \readfile{ward}{}{}
86%   \starttextbackground[more]
87%     \readfile{ward}{}{}
88%   \stoptextbackground
89%   \readfile{ward}{}{}
90% \stoptextbackground
91
92\newinteger\c_anch_backgrounds_text_level
93
94\installcorenamespace{textbackground}
95\installcorenamespace{textbackgroundlevel}
96
97\installcommandhandler \??textbackground {textbackground} \??textbackground
98
99\appendtoks
100    \frozen\instance\protected\edefcsname        \currenttextbackground\endcsname{\groupedcommand{\starttextbackground[\currenttextbackground]}{\stoptextbackground}}%
101    \frozen\instance\protected\edefcsname\e!start\currenttextbackground\endcsname{\starttextbackground[\currenttextbackground]}%
102    \frozen\instance\protected\edefcsname\e!stop \currenttextbackground\endcsname{\stoptextbackground}%
103\to \everydefinetextbackground
104
105\newconstant   \c_anch_backgrounds_pos_state
106\newconditional\c_anch_backgrounds_pos_no_shape
107
108\def\anch_backgrounds_bpos
109  {\ifconditional\c_anch_backgrounds_pos_no_shape
110     \bposkind\v_anch_backgrounds_text_current\plusthree
111   \else
112     \bposkind\v_anch_backgrounds_text_current\c_anch_backgrounds_pos_state
113   \fi}
114
115\def\anch_backgrounds_epos
116  {\ifconditional\c_anch_backgrounds_pos_no_shape
117     \eposkind\v_anch_backgrounds_text_current\plusthree
118   \else
119     \eposkind\v_anch_backgrounds_text_current\c_anch_backgrounds_pos_state
120   \fi}
121
122% ugly hack to register usage
123
124\newinteger \c_anch_backgrounds_text_count
125\newinteger \c_anch_backgrounds_text_check
126\newconstant\c_anch_backgrounds_text_state
127
128\permanent\tolerant\protected\def\starttextbackground[#tag]#spacer[#S#settings]%
129  {\begingroup
130   \global\advanceby\c_anch_backgrounds_text_count\plusone
131   \advanceby\c_anch_backgrounds_text_level\plusone
132   \cdef\currenttextbackground{#tag}%
133   \anch_backgrounds_text_initialize_next
134   \ifparameter#settings\or
135     \setupcurrenttextbackground[#settings]%
136   \fi
137   \ifcstok{\textbackgroundparameter\c!state}\v!start
138     \anch_backgrounds_text_preset_yes
139   \else
140     \anch_backgrounds_text_preset_nop
141   \fi
142   \anch_backgrounds_text_start_indeed}
143
144\def\anch_backgrounds_text_level_start
145  {\c_anch_backgrounds_text_check\c_anch_backgrounds_text_count}
146
147\def\anch_backgrounds_text_level_stop
148  {\c_anch_backgrounds_text_state
149     \ifnum\c_anch_backgrounds_text_count>\c_anch_backgrounds_text_check
150       \plusone
151     \else
152       \zerocount
153     \fi}
154
155% todo \backgroundvariable\c!variant
156
157% criterium determines when we fall back on text
158% always is always forcing paragraphs
159
160\let\anch_backgrounds_text_start_indeed\relax
161\let\anch_backgrounds_text_stop_indeed \relax
162
163\defcsname\??textbackgroundlevel\v!text\endcsname
164  {\let\anch_backgrounds_text_start_indeed\anch_backgrounds_text_start_txt
165   \let\anch_backgrounds_text_stop_indeed \anch_backgrounds_text_stop_txt}
166
167\defcsname\??textbackgroundlevel\v!paragraph\endcsname
168  {\ifnum\c_anch_backgrounds_text_level>\textbackgroundparameter\c!criterium\relax
169     \let\anch_backgrounds_text_start_indeed\anch_backgrounds_text_start_txt
170     \let\anch_backgrounds_text_stop_indeed \anch_backgrounds_text_stop_txt
171   \else
172     \let\anch_backgrounds_text_start_indeed\anch_backgrounds_text_start_par
173     \let\anch_backgrounds_text_stop_indeed \anch_backgrounds_text_stop_par
174   \fi}
175
176\defcsname\??textbackgroundlevel\v!always\endcsname
177  {\let\anch_backgrounds_text_start_indeed\anch_backgrounds_text_start_par
178   \let\anch_backgrounds_text_stop_indeed \anch_backgrounds_text_stop_par}
179
180\defcsname\??textbackgroundlevel\v!none\endcsname
181  {\anch_backgrounds_text_preset_nop}
182
183\def\anch_backgrounds_text_preset_nop
184  {\let\anch_backgrounds_text_start_indeed\relax
185   \let\anch_backgrounds_text_stop_indeed \relax}
186
187\def\anch_backgrounds_text_preset_yes
188  {\anch_backgrounds_text_preset_nop
189   \csname\??textbackgroundlevel\textbackgroundparameter\c!location\endcsname
190   \edef\p_anch_backgrounds_text_frame     {\textbackgroundparameter\c!frame}%
191   \edef\p_anch_backgrounds_text_corner    {\textbackgroundparameter\c!corner}%
192   \edef\p_anch_backgrounds_text_background{\textbackgroundparameter\c!background}%
193   \ifx\p_anch_backgrounds_text_frame\v!on
194     \ifx\p_anch_backgrounds_text_corner\v!round
195       \let\p_anch_backgrounds_text_frame\!!plustwo
196     \else
197       \let\p_anch_backgrounds_text_frame\!!plusone
198     \fi
199   \else
200     \let\p_anch_backgrounds_text_frame\!!zerocount
201   \fi
202   \ifx\p_anch_backgrounds_text_background\v!color
203     \let\p_anch_backgrounds_text_background\!!plusone
204   \else
205     \let\p_anch_backgrounds_text_background\!!zerocount
206   \fi
207   \startpositionoverlay{\textbackgroundoverlay{\textbackgroundparameter\c!level}}%
208     \anch_backgrounds_text_meta_graphic % gets expanded directly
209   \stoppositionoverlay}
210
211\def\anch_backgrounds_text_meta_graphic
212  {\normalexpanded
213     {\setMPpositiongraphicrange % needs to be optimized
214        {\v_anch_backgrounds_anchor_current_b}%
215        {\v_anch_backgrounds_anchor_current_e}%
216        {\textbackgroundparameter\c!method}%
217        {self=\v_anch_backgrounds_text_current,
218         mp=\textbackgroundparameter\c!mp,
219         gridtype=\textbackgroundparameter\c!alternative,
220         filltype=\p_anch_backgrounds_text_background,
221         linetype=\p_anch_backgrounds_text_frame,
222         dashtype=\textbackgroundparameter\c!dash,
223         gridcolor=\textbackgroundparameter\c!framecolor,
224         linecolor=\textbackgroundparameter\c!framecolor,
225         lineoffset=\textbackgroundparameter\c!frameoffset,
226         fillcolor=\textbackgroundparameter\c!backgroundcolor,
227         filloffset=\textbackgroundparameter\c!backgroundoffset,
228         gridwidth=\textbackgroundparameter\c!rulethickness,
229         gridshift=\textbackgroundparameter\c!voffset,
230         linewidth=\textbackgroundparameter\c!rulethickness,
231         lineradius=\textbackgroundparameter\c!radius}}}
232
233\permanent\protected\def\stoptextbackground
234  {\anch_backgrounds_text_stop_indeed
235   \carryoverpar\endgroup} % why doesn't this work ?
236
237\permanent\protected\def\starttextbackgroundmanual
238  {\begingroup
239   \c_anch_backgrounds_pos_state\plusone
240   \usetextbackgroundstyleandcolor\c!style\c!color
241   \anch_backgrounds_bpos}
242
243\permanent\protected\def\stoptextbackgroundmanual
244  {\anch_backgrounds_epos
245   \carryoverpar\endgroup}
246
247\def\anch_backgrounds_text_start_txt
248  {\ifvmode\dontleavehmode\fi
249   \begingroup
250   \c_anch_backgrounds_pos_state\plusone
251   \usetextbackgroundstyleandcolor\c!style\c!color
252   \anch_backgrounds_bpos}
253
254\def\anch_backgrounds_text_stop_txt
255  {\anch_backgrounds_epos
256   \carryoverpar\endgroup}
257
258\newgluespec\textbackgroundskip
259
260% maybe we should have a resetter for such compensation struts
261
262\def\anch_backgrounds_reset_attributes
263  {\scratchcounter\c_attr_snapmethod
264   \resetallattributes % \c_attr_linenumber\attributeunsetvalue
265   \c_attr_snapmethod\scratchcounter}
266
267\def\anch_backgrounds_text_start_par % beware .. background shapes
268  {\endgraf % new
269   \textbackgroundparameter\c!before
270   \begingroup
271   \c_anch_backgrounds_pos_state\plustwo
272   \begingroup
273   \anch_backgrounds_reset_attributes
274   \noindent
275   \ifgridsnapping
276     \spac_helpers_assign_skip\textbackgroundskip{\textbackgroundparameter\c!topoffset}%
277     \ifdim\textbackgroundskip>\zeropoint
278       \struttedbox % not always ok (e.g. setups)
279         {\hpack{\raise\textbackgroundskip\hpack{\anch_backgrounds_bpos}}}%
280     \else
281       \anch_backgrounds_bpos
282     \fi
283   \else
284     \anch_backgrounds_bpos
285   \fi
286   \endgraf % we need a vertical nobreak - 29/06/2004
287   \endgroup
288   \nobreak
289   \vskip-\lineheight
290   \nobreak
291   \ifgridsnapping \else
292     \spac_helpers_assign_skip\textbackgroundskip{\textbackgroundparameter\c!topoffset}%
293     \ifdim\textbackgroundskip>\zeropoint
294        \kern\textbackgroundskip\nobreak
295     \fi
296   \fi
297   \doadaptleftskip {\textbackgroundparameter\c!leftoffset}%
298   \doadaptrightskip{\textbackgroundparameter\c!rightoffset}%
299   % new
300   \dosetraggedcommand{\textbackgroundparameter\c!align}%
301   \raggedcommand
302   %
303   \usetextbackgroundstyleandcolor\c!style\c!color
304   \nowhitespace
305   \seteffectivehsize
306   \doinhibitblank %   \blank[\v!disable]% new
307   \par}
308
309\def\anch_backgrounds_text_stop_par
310  {\par
311   \removelastskip % new
312   \spac_helpers_assign_skip\textbackgroundskip{\textbackgroundparameter\c!bottomoffset}%
313   \ifdim\lastskip>\zeropoint
314     \advanceby\textbackgroundskip-\lastskip
315   \fi
316   \ifgridsnapping
317   \orelse\ifdim\textbackgroundskip>\zeropoint
318     \kern\textbackgroundskip\nobreak
319   \fi
320   \nobreak
321   \vskip-\dimexpr\lineheight+\parskip\relax % problem: we loose the hangindent
322   \nobreak
323   \endgroup
324   \begingroup
325   \forgeteverypar % NOT REALLY NEEDED, SAVES HASH/MEM
326   \anch_backgrounds_reset_attributes
327   \nobreak \noindent \strut \hfill \kern\zeropoint
328   % so far
329   \spac_helpers_assign_skip\textbackgroundskip{\textbackgroundparameter\c!bottomoffset}%
330   \ifgridsnapping % experimental, pascal (todo: topoffset in same way)
331     \ifdim\textbackgroundskip>\zeropoint
332       \struttedbox % not always ok (e.g. setups)
333         {\hpack{\lower\textbackgroundskip\hpack{\anch_backgrounds_epos}}}%
334     \else
335       \anch_backgrounds_epos
336     \fi
337   \else
338     \anch_backgrounds_epos
339   \fi
340   \endgraf
341   \carryoverpar\endgroup
342   \endgraf % new
343   \textbackgroundparameter\c!after}
344
345\permanent\protected\def\checkpositionoverlays % overloads \relax in anch-pgr
346  {\ifproductionrun
347     \enabletextarearegistration
348     \enablehiddenbackground
349     \enforced\glet\checkpositionoverlays\relax
350   \fi}
351
352% shape handling
353
354\definesystemattribute[textbackground][public]
355
356\def\anch_backgrounds_bpos
357  {\ifconditional\c_anch_backgrounds_pos_no_shape
358     \c_attr_textbackground\attributeunsetvalue
359     \bposkind\v_anch_backgrounds_text_current\plusthree
360   \orelse\ifnum\c_anch_backgrounds_pos_state=\plusone
361     \bposkind\v_anch_backgrounds_text_current\c_anch_backgrounds_pos_state
362     \clf_registerbackground{\v_anch_backgrounds_text_current}%
363   \else
364     \bposkind\v_anch_backgrounds_text_current\c_anch_backgrounds_pos_state
365     \c_attr_textbackground\attributeunsetvalue
366   \fi}
367
368% plugs into other code
369
370\def\strc_floats_wrap_free_region
371  {\global\setbox\floatbox\hpack % we always need to wrap
372     {\ifconditional\c_strc_floats_trace_free
373        \strc_floats_show_free_region
374      \fi
375      \box\floatbox}}
376
377\startuseMPgraphic{floatfree}
378    draw_free_region(
379        \the\wd\floatbox,
380        \the\ht\floatbox,
381        \the\dp\floatbox,
382        \the\d_free_offset_left,
383        \the\d_free_offset_right,
384        \the\d_free_offset_top,
385        \the\d_free_offset_bottom
386    ) ;
387\stopuseMPgraphic
388
389\def\strc_floats_show_free_region
390  {\lower\dp\floatbox\hpack to \zeropoint {\useMPgraphic{floatfree}}}
391
392\newconditional\c_strc_floats_mark_as_free
393\newconditional\c_strc_floats_trace_free
394\newconstant   \c_free_offset_kind
395\newdimension  \d_free_offset_left
396\newdimension  \d_free_offset_right
397\newdimension  \d_free_offset_top
398\newdimension  \d_free_offset_bottom
399
400\installtextracker{floats.freeregion}
401  {\c_strc_floats_trace_free\conditionaltrue}
402  {\c_strc_floats_trace_free\conditionalfalse}
403
404\def\strc_floats_mark_as_free#1#2#3#4#5%
405  {\c_free_offset_kind  #1%
406   \d_free_offset_left  #2%
407   \d_free_offset_right #3%
408   \d_free_offset_top   #4%
409   \d_free_offset_bottom#5%
410   \strc_floats_wrap_free_region
411   \anch_mark_tagged_box_free
412     \floatbox
413     \c_free_offset_kind
414     \d_free_offset_left
415     \d_free_offset_right
416     \d_free_offset_top
417     \d_free_offset_bottom}
418
419\ifrelax\strc_floats_mark_pag_as_free \else
420    \writestatus{error}{wrong place for pag_as_free}\wait
421\fi
422
423\ifrelax\strc_floats_mark_par_as_free \else
424    \writestatus{error}{wrong place for par_as_free}\wait
425\fi
426
427\def\strc_floats_mark_pag_as_free
428  {\ifpositioning
429     \ifconditional\c_strc_floats_mark_as_free
430       \strc_floats_mark_as_free
431         \plusone
432         \zeropoint
433         \zeropoint
434         \d_strc_floats_top
435         \d_strc_floats_bottom
436     \fi
437   \fi}
438
439\def\strc_floats_mark_par_as_free
440  {\ifpositioning
441     \ifconditional\c_strc_floats_mark_as_free
442       \ifcase\c_page_sides_float_type
443       \or % backspace
444         \strc_floats_mark_as_free
445           \plustwo
446           \zeropoint
447           \d_page_sides_rightoffset
448           \d_page_sides_topskip
449           \d_page_sides_bottomskip
450       \or   % leftedge
451         \strc_floats_mark_as_free
452           \plustwo
453           \zeropoint
454           \d_page_sides_rightoffset
455           \d_page_sides_topskip
456           \d_page_sides_bottomskip
457       \or   % leftmargin
458         \strc_floats_mark_as_free
459           \plustwo
460           \zeropoint
461           \d_page_sides_rightoffset
462           \d_page_sides_topskip
463           \d_page_sides_bottomskip
464       \or   % leftside
465         \strc_floats_mark_as_free
466           \plustwo
467           \d_page_sides_leftskip % maybe too
468           \d_page_sides_margin
469           \d_page_sides_topskip
470           \d_page_sides_bottomskip
471       \or   % rightside
472         \strc_floats_mark_as_free
473           \plusthree
474           \d_page_sides_margin
475           \d_page_sides_rightskip % maybe too
476           \d_page_sides_topskip
477           \d_page_sides_bottomskip
478       \or   % rightmargin
479         \strc_floats_mark_as_free
480           \plusthree
481           \d_page_sides_leftoffset
482           \zeropoint
483           \d_page_sides_topskip
484           \d_page_sides_bottomskip
485       \or   % rightedge
486         \strc_floats_mark_as_free
487           \plusthree
488           \d_page_sides_leftoffset
489           \zeropoint
490           \d_page_sides_topskip
491           \d_page_sides_bottomskip
492       \or   % cutspace
493         \strc_floats_mark_as_free
494           \plusthree
495           \d_page_sides_leftoffset
496           \zeropoint
497           \d_page_sides_topskip
498           \d_page_sides_bottomskip
499       \fi
500     \fi
501   \fi}
502
503% so far
504
505\setuptextbackground
506  [\c!mp=mpos:region:draw,
507   \c!method=mpos:region,
508   \c!state=\v!start,
509   \c!location=\v!text,
510   \c!leftoffset=\!!zeropoint, % 1em,
511   \c!rightoffset=\textbackgroundparameter\c!leftoffset,
512   \c!topoffset=\!!zeropoint,  % \v!medium,
513   \c!bottomoffset=\textbackgroundparameter\c!topoffset,
514   \c!criterium=\plusone,
515   \c!level=-1,
516   \c!alternative=0,
517   \c!align=,
518   \c!dash=0, % to be internationalized
519   \c!background=\v!color,
520   \c!backgroundcolor=lightgray,
521   \c!backgroundoffset=\!!zeropoint,
522   \c!corner=\v!rectangular,
523   \c!radius=.5\bodyfontsize,
524   \c!voffset=\!!zeropoint,
525   \c!frame=\v!on,
526   \c!framecolor=blue,
527   \c!frameoffset=\!!zeropoint,
528   \c!rulethickness=\linewidth]
529
530%D The \METAPOST\ connection:
531
532% gridtype = 1 => baseline
533% gridtype = 2 => betweenline
534
535\setupMPvariables
536  [mpos:region]
537  [mp=mpos:region:unset,
538   gridtype=0,
539   linetype=1,
540   filltype=1,
541   dashtype=0, % 1 = dashed, 2 = dashed with background
542   gridcolor=red,
543   linecolor=blue,
544   fillcolor=lightgray,
545   filloffset=\zeropoint,
546   linewidth=\linewidth,
547   gridwidth=\linewidth,
548   gridshift=\zeropoint,
549   lineradius=.5\bodyfontsize,
550   lineoffset=\zeropoint,
551   dashtype=1]
552
553\startuseMPgraphic{mpos:region:extra}
554    % user stuff
555\stopuseMPgraphic
556
557\startuseMPgraphic{mpos:region:anchor}
558    setbounds currentpicture to multibox ;
559\stopuseMPgraphic
560
561\startMPpositiongraphic{mpos:region}{fillcolor,filloffset,linecolor,gridcolor,linewidth,gridwidth,gridshift,lineradius,lineoffset}
562    \includeMPgraphic{mpos:region:setup} ;
563    \includeMPgraphic{mpos:region:extra} ;
564    getmultipars("self", "anchor") ;
565    \includeMPgraphic{\MPvar{mp}} ;
566    \includeMPgraphic{mpos:region:anchor} ;
567\stopMPpositiongraphic
568
569%D For old times sake:
570
571\startMPpositiongraphic{mpos:box}{fillcolor,linecolor,linewidth}
572    string tag; tag := "\MPvar{self}" ;
573    path box ; box := positionbox(tag) ;
574
575    box := box enlarged  \MPvar{filloffset} ;
576    fill box withcolor \MPvar{fillcolor} ;
577    draw box withcolor \MPvar{linecolor} withpen pencircle scaled \MPvar{linewidth} ;
578
579    positioninregion;
580\stopMPpositiongraphic
581
582\startMPpositionmethod{mpos:region}
583    \MPpositiongraphic{mpos:region}{}%
584\stopMPpositionmethod
585
586\startuseMPgraphic{mpos:region:draw}
587    draw_multi_pars
588\stopuseMPgraphic
589
590\startuseMPgraphic{mpos:region:show}
591    show_multi_pars
592\stopuseMPgraphic
593
594\startuseMPgraphic{mpos:region:kind}
595    show_multi_kind
596\stopuseMPgraphic
597
598\startuseMPgraphic{mpos:region:sideline}
599    draw_multi_side
600\stopuseMPgraphic
601
602\startuseMPgraphic{mpos:region:sideline:dashed}
603    draw_multi_side_path dashed evenly ;
604\stopuseMPgraphic
605
606%D As an example we define a grid background:
607
608\pushoverloadmode
609
610\definetextbackground
611  [\v!grid]
612  [\c!state=\v!stop,
613   \c!location=\v!paragraph,
614   \c!frame=\v!off,
615   \c!framecolor=red,
616   \c!background=,
617   \c!alternative=1]
618
619\ifdefined\basegrid
620    \aliased\letcsname\v!grid\endcsname\basegrid
621\fi
622
623\popoverloadmode
624
625%D Some examples (that might become modules anch-imp-whatever):
626
627\setupMPvariables
628  [mpos:encircle]
629  [fillcolor=lightgray,
630   filloffset=\!!zeropoint,
631   linecolor=blue,
632   lineoffset=5pt,
633   linewidth=1pt]
634
635\startMPpositiongraphic{mpos:encircle}{linecolor,fillcolor,linewidth,lineoffset}
636    getposboxes("self","anchor") ;
637    if nofposboxes = 1 :
638        posboxes[1] := (posboxes[1] enlarged mpvar "lineoffset") cornered mpvar "lineoffset" ;
639        fill posboxes[1] withcolor mpvar "fillcolor" ;
640        draw posboxes[1] withpen pencircle scaled mpvar "linewidth" withcolor mpvar "linecolor"  ;
641    fi ;
642\stopMPpositiongraphic
643
644\setupMPvariables
645  [mpos:connect]
646  [linecolor=red,
647   lineoffset=.25ex,
648   linewidth=1pt]
649
650\startMPpositiongraphic{mpos:connect}{linecolor,lineoffset,linewidth}
651    boxlinewidth  := mpvar "linewidth" ;
652    boxlineoffset := mpvar "lineoffset" ;
653    def boxlineoptions = withcolor mpvar "linecolor" enddef ;
654    getposboxes("from,to","anchor") ;
655    connect_positions ;
656\stopMPpositiongraphic
657
658\protect \endinput
659
660\definetextbackground[underline]    [location=text,alternative=1,background=,frame=off]
661\definetextbackground[overstrike]   [location=text,alternative=2,background=,frame=off]
662\definetextbackground[exlines]      [location=text,alternative=3,background=,frame=off]
663\definetextbackground[strikethrough][location=text,alternative=4,background=,frame=off]
664
665\definestartstop [underline]
666  [before={\starttextbackground[underline]},
667   after=\stoptextbackground]
668
669\definestartstop
670  [overstrike]
671  [before={\starttextbackground[overstrike]},
672   after=\stoptextbackground]
673
674\definestartstop
675  [exlines]
676  [before={\starttextbackground[exlines]},
677   after=\stoptextbackground]
678
679\definestartstop
680  [strikethrough]
681  [before={\starttextbackground[strikethrough]},
682   after=\stoptextbackground]
683
684\definetextbackground
685  [sideline]
686  [mp=mpos:region:sideline,
687   location=paragraph,
688   framecolor=red,
689   frameoffset=5mm]
690
691\definestartstop [sideline]
692  [before={\starttextbackground[sideline]},
693   after=\stoptextbackground]
694
695\starttext
696
697    \startunderline     \input tufte \stopunderline      \blank
698    \startoverstrike    \input tufte \stopoverstrike     \blank
699    \startexlines       \input tufte \stopexlines        \blank
700    \startstrikethrough \input tufte \stopstrikethrough  \blank
701    \startsideline      \input tufte \stopsideline       \blank
702
703    \page
704
705    \startpositionoverlay{text-1}
706        \setMPpositiongraphic{connect-1-b}{mpos:connect}{from=connect-1-b,to=connect-1-e}
707        \setMPpositiongraphic{connect-1-e}{mpos:connect}{from=connect-1-b,to=connect-1-e}
708    \stoppositionoverlay
709
710    \startpositionoverlay{text-1}
711        \setMPpositiongraphic{encircle-1}{mpos:encircle}{self=encircle-1}
712    \stoppositionoverlay
713
714    test \hpos{connect-1-b}{START}
715        \dorecurse{10}{\input ward}
716    \hpos{encircle-1}{\strut HERE}
717        \dorecurse{10}{\input ward}
718    \hpos{connect-1-e}{STOP} test
719
720\stoptext
721