page-box.mkvi /size: 10 Kb    last modification: 2020-07-01 14:35
1
%D \module
2
%D [ file=page-box,
3
%D version=2011.12.05, % moved from other page-* files
4
%D title=\CONTEXT\ Page Macros,
5
%D subtitle=Page Boxing,
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
\writestatus
{
loading
}{
ConTeXt
Page
Macros
/
Boxing
}
15 16
\unprotect
17 18
%D The often two step approach (_indeed) saves skipping tokens
19
%D which is nicer for tracing.
20 21
%D Centering the paper area on the print area is determined
22
%D by the \type {top}, \type {bottom}, \type {left} and \type
23
%D {right} parameters.
24 25
\def
\page_boxes_apply_center
#
box
% we could test for a difference (saves packing)
26
{
\printpaperwidth
\v_page_target_scale
\printpaperwidth
\relax
27
\printpaperheight
\v_page_target_scale
\printpaperheight
\relax
28
\ifdim
\printpaperheight
>
\ht
#
box
\relax
29
\page_boxes_apply_center_indeed
#
box
%
30
\else\ifdim
\printpaperwidth
>
\wd
#
box
\relax
31
\page_boxes_apply_center_indeed
#
box
%
32
\fi\fi
}
33 34
\def
\page_boxes_apply_center_indeed_l_r
#
box
%
35
{
\v_page_target_left
36
\v_page_target_left_fill
37
\box
#
box
%
38
\v_page_target_right_fill
39
\v_page_target_right
}
40 41
\def
\page_boxes_apply_center_indeed_r_l
#
box
%
42
{
\v_page_target_right
43
\v_page_target_right_fill
44
\box
#
box
%
45
\v_page_target_left_fill
46
\v_page_target_left
}
47 48
\def
\page_boxes_apply_center_indeed
#
box
% we could test for a difference (saves packing)
49
{
\setbox
#
box
\vpack
to
\printpaperheight
50
{
\v_page_target_top
51
\v_page_target_top_fill
52
\hpack
to
\printpaperwidth
53
{
\ifconditional
\c_page_target_print_doublesided
54
\doifbothsides
55
{
\page_boxes_apply_center_indeed_l_r
#
box
}
%
56
{
\page_boxes_apply_center_indeed_l_r
#
box
}
%
57
{
\page_boxes_apply_center_indeed_r_l
#
box
}
%
58
\else
59
\page_boxes_apply_center_indeed_l_r
#
box
%
60
\fi
}
%
61
\par
62
\v_page_target_bottom_fill
63
\v_page_target_bottom
}}
64 65
\def
\page_boxes_apply_offset
% #box
66
{
\ifzeropt
\topoffset
67
\ifzeropt
\backoffset
68
\doubleexpandafter\gobbleoneargument
69
\else
70
\doubleexpandafter
\page_boxes_apply_offset_indeed
71
\fi
72
\else
73
\expandafter
\page_boxes_apply_offset_indeed
74
\fi
}
75 76
\def
\page_boxes_apply_offset_indeed
#
box
%
77
{
\scratchwidth
\wd
#
box
%
78
\scratchheight
\ht
#
box
%
79
\scratchdepth
\dp
#
box
%
80
\setbox
#
box
\vpack
81
{
\offinterlineskip
82
\vskip
\topoffset
83
\hskip
\doifbothsides
\backoffset
\backoffset
{
-
\backoffset
}
%
84
\box
#
box
}
%
85
\wd
#
box
\scratchwidth
86
\ht
#
box
\scratchheight
87
\dp
#
box
\scratchdepth
}
88 89
\def
\page_boxes_apply_replicate
90
{
\ifnum
\layoutparameter
\c!nx
>
\plusone
91
\expandafter
\page_boxes_apply_replicate_indeed
92
\else\ifnum
\layoutparameter
\c!ny
>
\plusone
93
\doubleexpandafter
\page_boxes_apply_replicate_indeed
94
\else
95
\doubleexpandafter\gobbleoneargument
96
\fi\fi
}
97 98
\def
\page_boxes_apply_replicate_indeed
#
box
%
99
{
\setbox
#
box
\vpack
100
{
\offinterlineskip
101
\dorecurse
{
\layoutparameter
\c!ny
}
102
{
\hpack
{
\dorecurse
{
\layoutparameter
\c!nx
}{
\copy
#
box
\kern
\layoutparameter
\c!dx
}
\unskip
}
%
103
\vskip
\layoutparameter
\c!dy
}
104
\unskip
}}
105 106
\def
\page_boxes_apply_orientate_paper
#
box
%
107
{
\ifnum
\number
\c_page_target_paper_orientation
\number
\c_page_target_paper_reverse
>
\zerocount
108
\page_boxes_apply_orientate_indeed
\c_page_target_paper_orientation
\c_page_target_paper_reverse
{
#
box
}
%
109
\fi
}
110 111
\def
\page_boxes_apply_orientate_print
#
box
%
112
{
\ifnum
\number
\c_page_target_print_orientation
\number
\c_page_target_print_reverse
>
\zerocount
113
\page_boxes_apply_orientate_indeed
\c_page_target_print_orientation
\c_page_target_print_reverse
{
#
box
}
%
114
\fi
}
115 116
\def
\page_boxes_apply_orientate_indeed
#
odd
#
even
#
box
%
117
{
\setbox
#
box
\vpack
118
{
\edef
\somerotation
{
\number
\ifdoublesided\ifodd
\realpageno
#
odd
\else
#
even
\fi\else
#
odd
\fi
}
%
119
\dorotatebox
\somerotation
\hpack
{
\box
#
box
}}}
120 121
\def
\page_boxes_apply_mirror
#
box
%
122
{
\setbox
#
box
\vpack
{
\mirror
{
\box
#
box
}}}
123 124
\def
\page_boxes_apply_mirror_paper
#
box
{
\ifconditional
\c_page_target_paper_mirror
\page_boxes_apply_mirror
{
#
box
}
\fi
}
125
\def
\page_boxes_apply_mirror_print
#
box
{
\ifconditional
\c_page_target_print_mirror
\page_boxes_apply_mirror
{
#
box
}
\fi
}
126 127
\let
\page_boxes_apply_negate
\negatecolorbox
128 129
\def
\page_boxes_apply_negate_print
#
box
%
130
{
\ifconditional
\c_page_target_print_negate
131
\page_boxes_apply_negate
#
box
%
132
\else
\ifconditional
\c_page_target_paper_negate
133
\page_boxes_apply_negate
#
box
%
134
\fi\fi
}
135 136
\let
\p_page_layouts_scale
\relax
137
\let
\p_page_layouts_sx
\relax
138
\let
\p_page_layouts_sy
\relax
139 140
\def
\page_boxes_apply_scale
#
box
%
141
{
\edef
\p_page_layouts_scale
{
\layoutparameter
\c!scale
}
%
142
\ifdim
\p_page_layouts_scale
\points
=
\onepoint
143
\edef
\p_page_layouts_sx
{
\layoutparameter
\c!sx
}
%
144
\edef
\p_page_layouts_sy
{
\layoutparameter
\c!sy
}
%
145
\ifdim
\p_page_layouts_sx
\points
=
\onepoint
146
\ifdim
\layoutparameter
\c!sy
\points
=
\onepoint
147
% no scaling done (and no copying either)
148
\else
149
\page_boxes_apply_scale_indeed
\p_page_layouts_sx
\p_page_layouts_sy
{
#
box
}
%
150
\fi
151
\else
152
\page_boxes_apply_scale_indeed
\p_page_layouts_sx
\p_page_layouts_sy
{
#
box
}
%
153
\fi
154
\else
155
\page_boxes_apply_scale_indeed
\p_page_layouts_scale
\p_page_layouts_scale
{
#
box
}
%
156
\fi
}
157 158
\def
\page_boxes_apply_scale_indeed
#
sx
#
sy
#
box
%
159
{
\setbox
#
box
\vpack
{
\scale
[
\c!sx
=#
sx
,
\c!sy
=#
sy
]
{
\box
#
box
}}
% can be a fast one
160
\paperwidth
#
sx
\paperwidth
161
\paperheight
#
sy
\paperheight
}
162 163
% \setuppagenumbering[alternative=doublesided]
164
% \setupcolors[state=start]
165
% \setuppapersize[A4][A4,oversized]
166
% \setuplayout[location=middle,clipoffset=5mm]
167
% \setupbackgrounds
168
% [page]
169
% [frame=on,rulethickness=1mm,
170
% backgroundoffset=10mm,background=color,backgroundcolor=red]
171
% \starttext \dorecurse{10}{\input tufte \par} \stoptext
172 173
% best use private variables
174 175
\newconditional
\c_page_boxes_clip_print
\settrue
\c_page_boxes_clip_print
176 177
\def
\page_boxes_apply_clip_print_left
% box
178
{
\ifconditional
\c_page_boxes_clip_print
179
\expandafter
\page_boxes_apply_clip_print_indeed
\expandafter
\conditionalfalse
180
\else
181
\expandafter
\gobbleoneargument
182
\fi
}
183 184
\def
\page_boxes_apply_clip_print_right
% box
185
{
\ifconditional
\c_page_boxes_clip_print
186
\expandafter
\page_boxes_apply_clip_print_indeed
\expandafter
\conditionaltrue
187
\else
188
\expandafter
\gobbleoneargument
189
\fi
}
190 191
\def
\page_boxes_apply_clip_print_indeed
#
right
#
box
% can be made more efficient, see other clipper
192
{
\scratchoffset
\pagebackgroundoffset
% can be zero which is valid % maybe another variable
193
\scratchwidth
\wd
#
box
%
194
\scratchheight
\ht
#
box
%
195
\scratchdepth
\dp
#
box
%
196
\setbox
#
box
\hpack
197
{
\advance
\scratchheight
\dimexpr
\scratchdepth
+
2
\scratchoffset
\relax
198
\advance
\scratchwidth
\scratchoffset
199
\ifconditional
#
right
\relax
200
\scratchdimen
-
\scratchoffset
201
\kern
\scratchdimen
202
\else
203
\scratchdimen
\zeropoint
204
\fi
205
\lower
\scratchoffset
\hpack
206
{
\clip
207
[
\c!hoffset
=
\scratchdimen
,
208
\c!voffset
=
-
\scratchoffset
,
209
\c!width
=
\scratchwidth
,
210
\c!height
=
\scratchheight
]
%
211
{
\box
#
box
}}}
%
212
\wd
#
box
\scratchwidth
213
\ht
#
box
\scratchheight
214
\dp
#
box
\scratchdepth
}
215 216
\def
\page_boxes_apply_clip_paper
217
{
\scratchoffset
\layoutparameter
\c!clipoffset
\relax
218
\ifdim
\scratchoffset
>
\zeropoint
219
\expandafter
\page_boxes_apply_clip_paper_indeed
220
\else
221
\expandafter
\gobbleoneargument
222
\fi
}
223 224
\def
\page_boxes_apply_clip_paper_indeed
#
box
%
225
{
\scratchwidth
\wd
#
box
%
226
\scratchheight
\ht
#
box
%
227
\scratchdepth
\dp
#
box
%
228
\setbox
#
box
\hpack
229
{
\advance
\scratchheight
\dimexpr
\scratchdepth
+
2
\scratchoffset
\relax
230
\advance
\scratchwidth
\scratchoffset
231
\doifbothsides
232
{
\advance
\scratchwidth\scratchoffset
233
\scratchdimen
-
\scratchoffset
234
\kern
\scratchdimen
}
%
235
{
\scratchdimen
\zeropoint
}
236
{
\scratchdimen
-
\scratchoffset
237
\kern
\scratchdimen
}
%
238
\lower
\scratchoffset
\hpack
239
{
\clip
240
[
\c!hoffset
=
\scratchdimen
,
241
\c!voffset
=
-
\scratchoffset
,
242
\c!width
=
\scratchwidth
,
243
\c!height
=
\scratchheight
]
%
244
{
\box
#
box
}}}
%
245
\wd
#
box
\scratchwidth
246
\ht
#
box
\scratchheight
247
\dp
#
box
\scratchdepth
}
248 249
% nearly always some displacement so no speedup test needed
250 251
\def
\page_boxes_apply_offsets
#
1
%
252
{
\setbox
#
1
\vpack
to
\paperheight
253
{
\hsize
\paperwidth
254
\vkern
\topspace
255
% \parfillskip\zeropoint
256
\hkern
\doifbothsides
\backspace
\backspace
{
\dimexpr
\paperwidth
-
\backspace
-
\makeupwidth
\relax
}
%
257
\box
#
1
}
%
258
\dp
#
1
\zeropoint
}
259 260
% \def\page_boxes_apply_offset#box%
261
% {\scratchwidth \wd#box%
262
% \scratchheight\ht#box%
263
% \scratchdepth \dp#box%
264
% \setbox#box\vpack
265
% {\offinterlineskip
266
% \vkern\topoffset
267
% \hkern\doifbothsides\backoffset\backoffset{-\backoffset}%
268
% \box#box}%
269
% \wd#box\scratchwidth
270
% \ht#box\scratchheight
271
% \dp#box\scratchdepth}
272 273
% \let\page_boxes_apply_clip_paper \gobbleoneargument
274
% \let\page_boxes_apply_clip_print_left \gobbleoneargument
275
% \let\page_boxes_apply_clip_print_right\gobbleoneargument
276 277
%D This is rather specialized:
278 279
\newconditional
\c_page_areas_enabled
280
\newbox
\b_page_areas_registered
281 282
\def
\enabletextarearegistration
283
{
\global
\settrue
\c_page_areas_enabled
}
284 285
\def
\page_areas_registered_box
286
{
\ifconditional
\c_page_areas_enabled
287
\expandafter
\page_areas_registered_box_indeed
288
\else
289
\expandafter
\page_areas_registered_box_forget
290
\fi
}
291 292
\def
\page_areas_registered_box_forget
#
1
#
2
#
3
% #1=lower-dp #2=correct-ht #3=box
293
{
\box
#
3
}
294 295
\def
\page_areas_registered_box_indeed
#
1
#
2
#
3
% #1=lower-dp #2=correct-ht #3=box
296
{
\anch_mark_flow_box
{
#
3
}}
297 298
\def
\page_areas_register_boxed
#
1
%
299
{
\begingroup
300
\setbox
\scratchbox
\vpack
{
#
1
}
%
301
\wd
\scratchbox
\makeupwidth
% somehow a space creeps in
302
\vpack
{
\page_areas_registered_box
0
0
\scratchbox
}
% 0 0 will go
303
\endgroup
}
304 305
% \def\page_areas_register_direct#1%
306
% {\xypos{pbd:\realfolio:b}% we could save bytes by only saving the y
307
% \endgraf
308
% \begingroup
309
% \scratchdimen\dimexpr\MPy{pbd:\realfolio:b}-\MPy{pbd:\realfolio:e}\relax
310
% \setbox\scratchbox\emptyhbox
311
% \wd\scratchbox\makeupwidth
312
% \ht\scratchbox\scratchdimen
313
% \vsmash{\page_areas_registered_box00\scratchbox}%
314
% \endgroup
315
% #1%
316
% \endgraf
317
% \xypos{pbd:\realfolio:e}}
318 319
\def
\page_areas_register_direct
#
1
#
2
%
320
{
\scratchdepth
\dp
#
2
%
321
\dp
#
2
\strutdepth
322
\anch_mark_flow_only
{
#
2
}
%
323
\dp
#
2
\scratchdepth
324
#
1
#
2
}
325 326
\protect
\endinput
327