summaryrefslogtreecommitdiff
path: root/tex/context/base/pack-bck.mkvi
blob: bb4b72252a2fe4db9882a5a0a5b317f83a14fd5a (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
%D \module
%D   [       file=pack-bck, % moved from  pack-rul,
%D        version=20111115, % 1998.10.16,
%D          title=\CONTEXT\ Packaging Macros,
%D       subtitle=Simple Backgrounds,
%D         author=Hans Hagen \& Wolfgang Schuster,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Packaging Macros / Simple Backgrounds}

%D The code here is already pretty old and is used for simple
%D backgrounds. As it is still used WS adapted the code to the
%D new command handler methods so that it gets a second life. So
%D it made sense to mkvi ir as well. Some more code is moved here
%D too.

%D \macros
%D   {setupbackground,startbackground,background}
%D
%D The section deals with backgrounds in the running text. This
%D means that texts is to be collected and split over pages. To
%D show what can be done, we provide this part of the
%D documentation with some gray background and a red frame.
%D Both the background and frame can have all characteristics
%D of \type{\framed}.
%D
%D \starttyping
%D \setupbackground
%D   [backgroundoffset=4pt,
%D    background=color,
%D    frame=on,
%D    framecolor=red,
%D    leftoffset=2pt]
%D \stoptyping
%D
%D The implementation is not that sophisticated, but suffices.
%D The main problem with this kind of functionality is to get
%D the spacing all right.
%D
%D Specifying the background is more or less the same as
%D specifying a framed box.
%D
%D \showsetup{setupbackground}
%D
%D {\em This mechanism is sort of obsolete and textbackgrounds should
%D be used instead.}

\unprotect

\installcorenamespace {background}

\installframedcommandhandler \??background {background} \??background

% The mode is not that public an dmostlu an initial mode:
%
% 0 : no split
% 1 : no split, honoring leftskip (messy)
% 2 : split
% 3 : split, honoring leftskip (messy)
%
% While processing the modes will change from 3->1 or 2->0. Don't
% depend on the mode being public currently as it could become a
% key.

\setnewconstant\backgroundsplitmode\plusthree

\appendtoks
    \setuevalue{\e!start\currentbackground}{\pack_backgrounds_start [\currentbackground]}%
    \setuevalue{\e!stop \currentbackground}{\pack_backgrounds_stop                      }%
    \setuevalue        {\currentbackground}{\pack_backgrounds_direct[\currentbackground]}%
\to \everydefinebackground

\unexpanded\def\pack_backgrounds_start[#tag]%
  {\endgraf
   \begingroup
   \def\currentbackground{#tag}%
   \doifelse{\backgroundparameter\c!state}\v!start
     {\dosingleempty\pack_backgrounds_start_indeed}
     {\dosingleempty\pack_backgrounds_start_ignore}}

\def\pack_backgrounds_start_indeed[#settings]%
  {\setupcurrentbackground[#settings,\c!state=\v!start,\c!offset=\v!overlay]%
   \let\pack_backgrounds_stop\pack_backgrounds_stop_indeed
   \setbox0\vbox\bgroup
     \vbox to \lineheight{}\vskip\zeropoint
     \blank[\v!disable]
     \leftskip \backgroundparameter\c!leftoffset
     \rightskip\backgroundparameter\c!rightoffset}

\def\pack_backgrounds_start_ignore[#settings]%
  {\let\pack_backgrounds_stop\pack_backgrounds_stop_ignore}

\let\pack_backgrounds_stop\relax

\unexpanded\def\pack_backgrounds_stop_indeed % we shoul duse the fast background variant of framed
  {\endgraf
   \removelastskip
   \egroup
   \dimen2\leftskip % new **
   \forgetall
   \ifinsidefloat
     \backgroundsplitmode\zerocount
   \fi
   \ifcase\backgroundsplitmode
     \inheritedbackgroundframed{\box0}%
   \or
     \hskip\dimen2
     \inheritedbackgroundframed{\box0}%
   \else
     \splitmaxdepth\boxmaxdepth
     \splittopskip\topskip
     \setbox2\vsplit0 to \lineheight % get rid of fake line
     \doloop
       {\ifdim\pagetotal=\zeropoint % empty page
          \scratchdimen\textheight
          \backgroundsplitmode\plusone % split to max height
        \else
          \setbox2\vbox{\backgroundparameter\c!before}%
          \scratchdimen\dimexpr\pagegoal-\ht2-\pagetotal\relax
          \backgroundsplitmode\plustwo % split to partial height
        \fi
        \advance\scratchdimen\dimexpr-\backgroundparameter\c!topoffset-\backgroundparameter\c!bottomoffset\relax
        \ifdim\scratchdimen>2\lineheight\relax % reasonable, will be configurable
          \ifdim\ht0>\scratchdimen % larger than page
            \setbox2\vsplit0 to \scratchdimen
          \else
            \setbox2\box0
            \backgroundsplitmode\zerocount % no split
          \fi
          \setbox2\vbox \ifcase\backgroundsplitmode\or to \textheight \fi % max split
            {\vskip\backgroundparameter\c!topoffset
             \unvcopy2
             \prevdepth\dp2
             \obeydepth
             \vskip\backgroundparameter\c!bottomoffset
             \vfill}
          \backgroundparameter\c!before
          \ifcase\backgroundsplitmode\or\or % partial split
            \ifdim\pagegoal<\maxdimen
              \pagegoal=1.2\pagegoal % be a bit more tolerant
            \fi
          \fi
          \startlinecorrection
            \ifnum\backgroundsplitmode=\plusthree
              \hskip\dimen2 % new **
            \fi
            \inheritedbackgroundframed{\box2}%
          \stoplinecorrection
          \ifcase\backgroundsplitmode % no split
            \backgroundparameter\c!after
          \else % some split
            \page_otr_fill_and_eject_page % no \page !
          \fi
        \else
          \page
        \fi
        \ifdim\ht0>\zeropoint \else
          \exitloop
        \fi}%
   \fi
   \endgroup
   \endgraf}

\unexpanded\def\pack_backgrounds_stop_ignore
  {\endgroup
   \endgraf}

\unexpanded\def\pack_backgrounds_direct[#tag]%
  {\begingroup
   \def\currentbackground{#tag}%
   \doifelse{\backgroundparameter\c!state}\c!start
     \pack_backgrounds_direct_indeed
     \pack_backgrounds_direct_ignore}

\def\pack_backgrounds_direct_indeed
  {\dowithnextboxcs\pack_backgrounds_direct_finish\vbox}

\def\pack_backgrounds_direct_finish
  {\setupcurrentbackground[\c!offset=\v!overlay]%
   \inheritedbackgroundframed{\flushnextbox}%
   \endgroup}

\def\pack_backgrounds_direct_ignore
  {\endgroup}

\definebackground[\v!background]

\ifdefined\startbackground \else
    \expandafter\let\startbackground\csname\e!start\v!background\endcsname
    \expandafter\let\stopbackground \csname\e!stop \v!background\endcsname
    \expandafter\let\background     \csname        \v!background\endcsname
\fi

\setupbackground
  [\c!leftoffset=.5\bodyfontsize,
   \c!rightoffset=\backgroundparameter\c!leftoffset,
   \c!topoffset=\zeropoint,
   \c!bottomoffset=\backgroundparameter\c!topoffset,
   \c!state=\v!start,
   \c!radius=.5\bodyfontsize,
   \c!corner=\v!rectangular,
   \c!frame=\v!off,
   \c!depth=\zeropoint,
 % \c!color=,
   \c!background=\v!color,
   \c!backgroundcolor=lightgray,
   \c!before=,
   \c!after=]

%D \startbuffer
%D \setupbackground[state=start,frame=on]
%D
%D \startbackground
%D \input ward
%D \stopbackground
%D
%D \background{\input ward\par}
%D
%D \setupbackground[state=stop]
%D
%D \startbackground
%D \input ward
%D \stopbackground
%D
%D \background{\input ward\par}
%D \stopbuffer
%D
%D \typebuffer \getbuffer

%D \macros
%D   {backgroundline}
%D
%D For the moment an undocumented feature, but a candidate for going public.

\unexpanded\def\backgroundline[#color]%
  {\dontleavehmode
   \dowithnextbox{\pack_backgrounds_add_to_nextbox{#color}}\hbox}

\unexpanded\def\pack_backgrounds_add_to_nextbox#color% handy helper
  {\hbox
     {\dousecolorparameter{#color}%
      \vrule
        \!!width \nextboxwd
        \!!height\nextboxht
        \!!depth \nextboxdp
      \hskip-\nextboxwd
      \flushnextbox}}

\protect \endinput