pack-bck.mklx /size: 7949 b    last modification: 2023-12-21 09:44
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 an dmostlu 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\defcsname\e!start\currentbackground\endcsname{\pack_backgrounds_start [\currentbackground]}%
69    \frozen\protected\instance\defcsname\e!stop \currentbackground\endcsname{\pack_backgrounds_stop                      }%
70    \frozen\protected\instance\defcsname        \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 \backgroundparameter\c!leftoffset
92     \rightskip\backgroundparameter\c!rightoffset}
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\dimexpr\pagetotal+2\lineheight\relax>\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\dimexpr\pagegoal-\ht\scratchdimentwo-\pagetotal\relax
131          \backgroundsplitmode\plustwo % split to partial height
132        \fi
133        \ifdim\scratchdimen<\zeropoint
134            \scratchdimen\pagegoal
135        \fi
136        \advanceby\scratchdimen\dimexpr
137          -\backgroundparameter\c!topoffset
138          -\backgroundparameter\c!bottomoffset
139        \relax
140        \ifdim\scratchdimen>2\lineheight\relax % reasonable, will be configurable
141          \ifdim\ht\scratchboxone>\scratchdimen % larger than page
142            \setbox\scratchdimentwo\vsplit\scratchboxone to \scratchdimen
143          \else
144            \setbox\scratchdimentwo\box\scratchboxone
145            \backgroundsplitmode\zerocount % no split
146          \fi
147          \setbox\scratchdimentwo\vbox \ifcase\backgroundsplitmode\or to \textheight \fi % max split
148            {\vskip\backgroundparameter\c!topoffset
149             \unvcopy\scratchdimentwo
150             \prevdepth\dp\scratchdimentwo
151             \obeydepth
152             \vskip\backgroundparameter\c!bottomoffset
153             \vfill}
154          \backgroundparameter\c!before
155          \ifcase\backgroundsplitmode\or\or % partial split
156            \ifdim\pagegoal<\maxdimen
157              \pagegoal=1.2\pagegoal % be a bit more tolerant
158            \fi
159          \fi
160          \startlinecorrection
161            \ifnum\backgroundsplitmode=\plusthree
162              \hskip\scratchdistance % new **
163            \fi
164            \inheritedbackgroundframed{\box\scratchdimentwo}%
165          \stoplinecorrection
166          \ifcase\backgroundsplitmode % no split
167            \backgroundparameter\c!after
168          \else % some split
169            \page_otr_fill_and_eject_page % no \page !
170          \fi
171        \else
172          \page
173        \fi
174        \ifdim\ht\scratchboxone>\zeropoint \else
175          \exitloop
176        \fi}%
177   \fi
178   \endgroup
179   \endgraf}
180
181\protected\def\pack_backgrounds_stop_ignore
182  {\endgroup
183   \endgraf}
184
185\protected\def\pack_backgrounds_direct[#tag]%
186  {\begingroup
187   \cdef\currentbackground{#tag}%
188   \ifcstok{\backgroundparameter\c!state}\c!start
189     \expandafter\pack_backgrounds_direct_indeed
190   \else
191     \expandafter\pack_backgrounds_direct_ignore
192   \fi}
193
194\def\pack_backgrounds_direct_indeed
195  {\dowithnextboxcs\pack_backgrounds_direct_finish\vbox}
196
197\def\pack_backgrounds_direct_finish
198  {\setupcurrentbackground[\c!offset=\v!overlay]%
199   \inheritedbackgroundframed{\flushnextbox}%
200   \endgroup}
201
202\def\pack_backgrounds_direct_ignore
203  {\endgroup}
204
205\definebackground[\v!background]
206
207\ifdefined\startbackground \else
208    \permanent\expandafter\let\expandafter\startbackground\csname\e!start\v!background\endcsname
209    \permanent\expandafter\let\expandafter\stopbackground \csname\e!stop \v!background\endcsname
210    \permanent\expandafter\let\expandafter\background     \csname        \v!background\endcsname
211\fi
212
213\setupbackground
214  [\c!leftoffset=.5\bodyfontsize,
215   \c!rightoffset=\backgroundparameter\c!leftoffset,
216   \c!topoffset=\zeropoint,
217   \c!bottomoffset=\backgroundparameter\c!topoffset,
218   \c!state=\v!start,
219   \c!radius=.5\bodyfontsize,
220   \c!corner=\v!rectangular,
221   \c!frame=\v!off,
222   \c!depth=\zeropoint,
223 % \c!color=,
224   \c!background=\v!color,
225   \c!backgroundcolor=lightgray,
226   \c!before=,
227   \c!after=]
228
229%D \startbuffer
230%D \setupbackground[state=start,frame=on]
231%D
232%D \startbackground
233%D \input ward
234%D \stopbackground
235%D
236%D \background{\input ward\par}
237%D
238%D \setupbackground[state=stop]
239%D
240%D \startbackground
241%D \input ward
242%D \stopbackground
243%D
244%D \background{\input ward\par}
245%D \stopbuffer
246%D
247%D \typebuffer \getbuffer
248
249\protect \endinput
250