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