pack-bck.mklx /size: 7955 b    last modification: 2025-02-21 11:03
1%D \module
2%D   [       file=pack-bck, % moved from  pack-rul,
3%D        version=20111115, % 1998.10.16,
4%D          title=\CONTEXT\ Packaging Macros,
5%D       subtitle=Simple Backgrounds,
6%D         author=Hans Hagen \& Wolfgang Schuster,
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\writestatus{loading}{ConTeXt Packaging Macros / Simple Backgrounds}
15
16%D The code here is already pretty old and is used for simple backgrounds. As it is
17%D still used WS adapted the code to the new command handler methods so that it gets
18%D a second life. So it made sense to mkvi as well. Some more code is moved here
19%D too.
20
21%D \macros
22%D   {setupbackground,startbackground,background}
23%D
24%D The section deals with backgrounds in the running text. This means that texts is
25%D to be collected and split over pages. To show what can be done, we provide this
26%D part of the documentation with some gray background and a red frame. Both the
27%D background and frame can have all characteristics of \type {\framed}.
28%D
29%D \starttyping
30%D \setupbackground
31%D   [backgroundoffset=4pt,
32%D    background=color,
33%D    frame=on,
34%D    framecolor=red,
35%D    leftoffset=2pt]
36%D \stoptyping
37%D
38%D The implementation is not that sophisticated, but suffices. The main problem with
39%D this kind of functionality is to get the spacing all right.
40%D
41%D Specifying the background is more or less the same as
42%D specifying a framed box.
43%D
44%D \showsetup{setupbackground}
45%D
46%D {\em This mechanism is sort of obsolete and textbackgrounds should be used
47%D instead.}
48
49\unprotect
50
51\installcorenamespace {background}
52
53\installframedcommandhandler \??background {background} \??background
54
55% The mode is not that public and mostly an initial mode:
56%
57% 0 : no split
58% 1 : no split, honoring leftskip (messy)
59% 2 : split
60% 3 : split, honoring leftskip (messy)
61%
62% While processing the modes will change from 3->1 or 2->0. Don't depend on the
63% mode being public currently as it could become a key.
64
65\setnewconstant\backgroundsplitmode\plusthree
66
67\appendtoks
68    \frozen\protected\instance\edefcsname\e!start\currentbackground\endcsname{\pack_backgrounds_start [\currentbackground]}%
69    \frozen\protected\instance\edefcsname\e!stop \currentbackground\endcsname{\pack_backgrounds_stop                      }%
70    \frozen\protected\instance\edefcsname        \currentbackground\endcsname{\pack_backgrounds_direct[\currentbackground]}%
71\to \everydefinebackground
72
73\protected\def\pack_backgrounds_start[#tag]%
74  {\endgraf
75   \begingroup
76   \cdef\currentbackground{#tag}%
77   \ifcstok{\backgroundparameter\c!state}\v!start
78     \expandafter\pack_backgrounds_start_indeed
79   \else
80     \expandafter\pack_backgrounds_start_ignore
81   \fi}
82
83\tolerant\def\pack_backgrounds_start_indeed[#S#settings]%
84  {\setupcurrentbackground[#settings,\c!state=\v!start,\c!offset=\v!overlay]%
85   \let\pack_backgrounds_stop\pack_backgrounds_stop_indeed
86   \setbox\scratchboxone\vbox\bgroup
87     \strut
88     \vskip-2\lineheight
89     \strut
90     \blank[\v!disable]
91     \leftskip \glueexpr\backgroundparameter\c!leftoffset\relax
92     \rightskip\glueexpr\backgroundparameter\c!rightoffset\relax}
93
94\tolerant\def\pack_backgrounds_start_ignore[#S#settings]%
95  {\let\pack_backgrounds_stop\pack_backgrounds_stop_ignore}
96
97\let\pack_backgrounds_stop\relax
98
99\protected\def\pack_backgrounds_stop_indeed % we shoul duse the fast background variant of framed
100  {\endgraf
101   \removelastskip
102   \strut
103   \vskip-2\lineheight
104   \strut
105   % safeguard added
106   \ifdim{\pagetotal+2\lineheight}>\pagegoal
107     \page
108   \fi
109   % till here
110   \egroup
111   \scratchdistance\leftskip % new **
112   \forgetall
113   \ifinsidefloat
114     \backgroundsplitmode\zerocount
115   \fi
116   \ifcase\backgroundsplitmode
117     \inheritedbackgroundframed{\box\scratchboxone}%
118   \or
119     \hskip\scratchdistance
120     \inheritedbackgroundframed{\box\scratchboxone}%
121   \else
122     \splitmaxdepth\boxmaxdepth
123     \splittopskip\topskip
124     \doloop
125       {\ifzeropt\pagetotal % empty page
126          \scratchdimen\textheight
127          \backgroundsplitmode\plusone % split to max height
128        \else
129          \setbox\scratchdimentwo\vbox{\backgroundparameter\c!before}%
130          \scratchdimen{\pagegoal-\ht\scratchdimentwo-\pagetotal}%
131          \backgroundsplitmode\plustwo % split to partial height
132        \fi
133        \ifdim\scratchdimen<\zeropoint
134            \scratchdimen\pagegoal
135        \fi
136        \advanceby\scratchdimen{-\backgroundparameter\c!topoffset-\backgroundparameter\c!bottomoffset}%
137        \ifdim\scratchdimen>2\lineheight\relax % reasonable, will be configurable
138          \ifdim\ht\scratchboxone>\scratchdimen % larger than page
139            \setbox\scratchdimentwo\vsplit\scratchboxone to \scratchdimen
140          \else
141            \setbox\scratchdimentwo\box\scratchboxone
142            \backgroundsplitmode\zerocount % no split
143          \fi
144          \setbox\scratchdimentwo\vbox \ifcase\backgroundsplitmode\or to \textheight \fi % max split
145            {\vskip{\backgroundparameter\c!topoffset}% or \vkern
146             \unvcopy\scratchdimentwo
147             \prevdepth\dp\scratchdimentwo
148             \obeydepth
149             \vskip{\backgroundparameter\c!bottomoffset}% or \vkern
150             \vfill}
151          \backgroundparameter\c!before
152          \ifcase\backgroundsplitmode\or\or % partial split
153            \ifdim\pagegoal<\maxdimen
154              \pagegoal=1.2\pagegoal % be a bit more tolerant
155            \fi
156          \fi
157          \startlinecorrection
158            \ifnum\backgroundsplitmode=\plusthree
159              \hskip\scratchdistance % new ** % or \hkern
160            \fi
161            \inheritedbackgroundframed{\box\scratchdimentwo}%
162          \stoplinecorrection
163          \ifcase\backgroundsplitmode % no split
164            \backgroundparameter\c!after
165          \else % some split
166            \page_otr_fill_and_eject_page % no \page !
167          \fi
168        \else
169          \page
170        \fi
171        \ifdim\ht\scratchboxone>\zeropoint \else
172          \exitloop
173        \fi}%
174   \fi
175   \endgroup
176   \endgraf}
177
178\protected\def\pack_backgrounds_stop_ignore
179  {\endgroup
180   \endgraf}
181
182\protected\def\pack_backgrounds_direct[#tag]%
183  {\begingroup
184   \cdef\currentbackground{#tag}%
185   \ifcstok{\backgroundparameter\c!state}\c!start
186     \expandafter\pack_backgrounds_direct_indeed
187   \else
188     \expandafter\pack_backgrounds_direct_ignore
189   \fi}
190
191\def\pack_backgrounds_direct_indeed
192  {\dowithnextboxcs\pack_backgrounds_direct_finish\vbox}
193
194\def\pack_backgrounds_direct_finish
195  {\setupcurrentbackground[\c!offset=\v!overlay]%
196   \inheritedbackgroundframed{\flushnextbox}%
197   \endgroup}
198
199\def\pack_backgrounds_direct_ignore
200  {\endgroup}
201
202\definebackground[\v!background]
203
204\ifdefined\startbackground \else
205    \permanent\expandafter\let\expandafter\startbackground\csname\e!start\v!background\endcsname
206    \permanent\expandafter\let\expandafter\stopbackground \csname\e!stop \v!background\endcsname
207    \permanent\expandafter\let\expandafter\background     \csname        \v!background\endcsname
208\fi
209
210\setupbackground
211  [\c!leftoffset=.5\bodyfontsize,
212   \c!rightoffset=\backgroundparameter\c!leftoffset,
213   \c!topoffset=\zeropoint,
214   \c!bottomoffset=\backgroundparameter\c!topoffset,
215   \c!state=\v!start,
216   \c!radius=.5\bodyfontsize,
217   \c!corner=\v!rectangular,
218   \c!frame=\v!off,
219   \c!depth=\zeropoint,
220 % \c!color=,
221   \c!background=\v!color,
222   \c!backgroundcolor=lightgray,
223   \c!before=,
224   \c!after=]
225
226%D \startbuffer
227%D \setupbackground[state=start,frame=on]
228%D
229%D \startbackground
230%D \input ward
231%D \stopbackground
232%D
233%D \background{\input ward\par}
234%D
235%D \setupbackground[state=stop]
236%D
237%D \startbackground
238%D \input ward
239%D \stopbackground
240%D
241%D \background{\input ward\par}
242%D \stopbuffer
243%D
244%D \typebuffer \getbuffer
245
246\protect \endinput
247