pack-bck.mkvi /size: 8825 b    last modification: 2020-07-01 14:35
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
17%D backgrounds. As it is still used WS adapted the code to the
18%D new command handler methods so that it gets a second life. So
19%D it made sense to mkvi as well. Some more code is moved here
20%D too.
21
22%D \macros
23%D   {setupbackground,startbackground,background}
24%D
25%D The section deals with backgrounds in the running text. This
26%D means that texts is to be collected and split over pages. To
27%D show what can be done, we provide this part of the
28%D documentation with some gray background and a red frame.
29%D Both the background and frame can have all characteristics
30%D of \type{\framed}.
31%D
32%D \starttyping
33%D \setupbackground
34%D   [backgroundoffset=4pt,
35%D    background=color,
36%D    frame=on,
37%D    framecolor=red,
38%D    leftoffset=2pt]
39%D \stoptyping
40%D
41%D The implementation is not that sophisticated, but suffices.
42%D The main problem with this kind of functionality is to get
43%D the spacing all right.
44%D
45%D Specifying the background is more or less the same as
46%D specifying a framed box.
47%D
48%D \showsetup{setupbackground}
49%D
50%D {\em This mechanism is sort of obsolete and textbackgrounds should
51%D be used instead.}
52
53\unprotect
54
55\installcorenamespace {background}
56
57\installframedcommandhandler \??background {background} \??background
58
59% The mode is not that public an dmostlu an initial mode:
60%
61% 0 : no split
62% 1 : no split, honoring leftskip (messy)
63% 2 : split
64% 3 : split, honoring leftskip (messy)
65%
66% While processing the modes will change from 3->1 or 2->0. Don't
67% depend on the mode being public currently as it could become a
68% key.
69
70\setnewconstant\backgroundsplitmode\plusthree
71
72\appendtoks
73    \setuevalue{\e!start\currentbackground}{\pack_backgrounds_start [\currentbackground]}%
74    \setuevalue{\e!stop \currentbackground}{\pack_backgrounds_stop                      }%
75    \setuevalue        {\currentbackground}{\pack_backgrounds_direct[\currentbackground]}%
76\to \everydefinebackground
77
78\unexpanded\def\pack_backgrounds_start[#tag]%
79  {\endgraf
80   \begingroup
81   \def\currentbackground{#tag}%
82   \doifelse{\backgroundparameter\c!state}\v!start
83     {\dosingleempty\pack_backgrounds_start_indeed}
84     {\dosingleempty\pack_backgrounds_start_ignore}}
85
86\def\pack_backgrounds_start_indeed[#settings]%
87  {\setupcurrentbackground[#settings,\c!state=\v!start,\c!offset=\v!overlay]%
88   \let\pack_backgrounds_stop\pack_backgrounds_stop_indeed
89   \setbox0\vbox\bgroup
90     \strut
91     \vskip-2\lineheight
92     \strut
93     \blank[\v!disable]
94     \leftskip \backgroundparameter\c!leftoffset
95     \rightskip\backgroundparameter\c!rightoffset}
96
97\def\pack_backgrounds_start_ignore[#settings]%
98  {\let\pack_backgrounds_stop\pack_backgrounds_stop_ignore}
99
100\let\pack_backgrounds_stop\relax
101
102\unexpanded\def\pack_backgrounds_stop_indeed % we shoul duse the fast background variant of framed
103  {\endgraf
104   \removelastskip
105   \strut
106   \vskip-2\lineheight
107   \strut
108   % safeguard added
109   \ifdim\dimexpr\pagetotal+2\lineheight\relax>\pagegoal
110     \page
111   \fi
112   % till here
113   \egroup
114   \dimen2\leftskip % new **
115   \forgetall
116   \ifinsidefloat
117     \backgroundsplitmode\zerocount
118   \fi
119   \ifcase\backgroundsplitmode
120     \inheritedbackgroundframed{\box0}%
121   \or
122     \hskip\dimen2
123     \inheritedbackgroundframed{\box0}%
124   \else
125     \splitmaxdepth\boxmaxdepth
126     \splittopskip\topskip
127     \doloop
128       {\ifzeropt\pagetotal % empty page
129          \scratchdimen\textheight
130          \backgroundsplitmode\plusone % split to max height
131        \else
132          \setbox2\vbox{\backgroundparameter\c!before}%
133          \scratchdimen\dimexpr\pagegoal-\ht2-\pagetotal\relax
134          \backgroundsplitmode\plustwo % split to partial height
135        \fi
136        \ifdim\scratchdimen<\zeropoint
137            \scratchdimen\pagegoal
138        \fi
139        \advance\scratchdimen\dimexpr
140            -\backgroundparameter\c!topoffset
141            -\backgroundparameter\c!bottomoffset
142        \relax
143        \ifdim\scratchdimen>2\lineheight\relax % reasonable, will be configurable
144          \ifdim\ht0>\scratchdimen % larger than page
145            \setbox2\vsplit0 to \scratchdimen
146          \else
147            \setbox2\box0
148            \backgroundsplitmode\zerocount % no split
149          \fi
150          \setbox2\vbox \ifcase\backgroundsplitmode\or to \textheight \fi % max split
151            {\vskip\backgroundparameter\c!topoffset
152             \unvcopy2
153             \prevdepth\dp2
154             \obeydepth
155             \vskip\backgroundparameter\c!bottomoffset
156             \vfill}
157          \backgroundparameter\c!before
158          \ifcase\backgroundsplitmode\or\or % partial split
159            \ifdim\pagegoal<\maxdimen
160              \pagegoal=1.2\pagegoal % be a bit more tolerant
161            \fi
162          \fi
163          \startlinecorrection
164            \ifnum\backgroundsplitmode=\plusthree
165              \hskip\dimen2 % new **
166            \fi
167            \inheritedbackgroundframed{\box2}%
168          \stoplinecorrection
169          \ifcase\backgroundsplitmode % no split
170            \backgroundparameter\c!after
171          \else % some split
172            \page_otr_fill_and_eject_page % no \page !
173          \fi
174        \else
175          \page
176        \fi
177        \ifdim\ht0>\zeropoint \else
178          \exitloop
179        \fi}%
180   \fi
181   \endgroup
182   \endgraf}
183
184\unexpanded\def\pack_backgrounds_stop_ignore
185  {\endgroup
186   \endgraf}
187
188\unexpanded\def\pack_backgrounds_direct[#tag]%
189  {\begingroup
190   \def\currentbackground{#tag}%
191   \doifelse{\backgroundparameter\c!state}\c!start
192     \pack_backgrounds_direct_indeed
193     \pack_backgrounds_direct_ignore}
194
195\def\pack_backgrounds_direct_indeed
196  {\dowithnextboxcs\pack_backgrounds_direct_finish\vbox}
197
198\def\pack_backgrounds_direct_finish
199  {\setupcurrentbackground[\c!offset=\v!overlay]%
200   \inheritedbackgroundframed{\flushnextbox}%
201   \endgroup}
202
203\def\pack_backgrounds_direct_ignore
204  {\endgroup}
205
206\definebackground[\v!background]
207
208\ifdefined\startbackground \else
209    \expandafter\let\expandafter\startbackground\csname\e!start\v!background\endcsname
210    \expandafter\let\expandafter\stopbackground \csname\e!stop \v!background\endcsname
211    \expandafter\let\expandafter\background     \csname        \v!background\endcsname
212\fi
213
214\setupbackground
215  [\c!leftoffset=.5\bodyfontsize,
216   \c!rightoffset=\backgroundparameter\c!leftoffset,
217   \c!topoffset=\zeropoint,
218   \c!bottomoffset=\backgroundparameter\c!topoffset,
219   \c!state=\v!start,
220   \c!radius=.5\bodyfontsize,
221   \c!corner=\v!rectangular,
222   \c!frame=\v!off,
223   \c!depth=\zeropoint,
224 % \c!color=,
225   \c!background=\v!color,
226   \c!backgroundcolor=lightgray,
227   \c!before=,
228   \c!after=]
229
230%D \startbuffer
231%D \setupbackground[state=start,frame=on]
232%D
233%D \startbackground
234%D \input ward
235%D \stopbackground
236%D
237%D \background{\input ward\par}
238%D
239%D \setupbackground[state=stop]
240%D
241%D \startbackground
242%D \input ward
243%D \stopbackground
244%D
245%D \background{\input ward\par}
246%D \stopbuffer
247%D
248%D \typebuffer \getbuffer
249
250%D \macros
251%D   {backgroundline}
252
253% \unexpanded\def\backgroundline[#color]%
254%   {\dontleavehmode
255%    \dowithnextbox{\pack_backgrounds_add_to_nextbox{#color}}\hbox}
256%
257% \unexpanded\def\pack_backgrounds_add_to_nextbox#color% handy helper
258%   {\hpack
259%      {\dousecolorparameter{#color}%
260%       \vrule
261%         \??width \nextboxwd
262%         \??height\nextboxht
263%         \??depth \nextboxdp
264%       \hskip-\nextboxwd
265%       \flushnextbox}}
266
267%D We implement this elsewhere avoiding the rule.
268
269% \unexpanded\def\backgroundline
270%   {\dontleavehmode
271%    \pack_backgrounds_add_to_nextbox\hbox\hpack}
272%
273% \unexpanded\def\pack_backgrounds_add_to_nextbox#box#pack[#color]%
274%   {\dontleavehmode
275%    \dowithnextbox{\pack_backgrounds_add_to_nextbox_indeed#pack{#color}}#box}
276%
277% \unexpanded\def\pack_backgrounds_add_to_nextbox_indeed#pack#color% handy helper
278%   {#pack%
279%      {\dousecolorparameter{#color}%
280%       \vrule
281%         \??width \nextboxwd
282%         \??height\nextboxht
283%         \??depth \nextboxdp
284%       \hskip-\nextboxwd
285%       \flushnextbox}}
286%
287% \unexpanded\def\backgroundhbox{\pack_backgrounds_add_to_nextbox\hbox\hpack}
288% \unexpanded\def\backgroundvtop{\pack_backgrounds_add_to_nextbox\vtop\tpack}
289% \unexpanded\def\backgroundvbox{\pack_backgrounds_add_to_nextbox\vbox\vpack}
290
291\protect \endinput
292